-
Notifications
You must be signed in to change notification settings - Fork 33
/
configure
executable file
·266 lines (215 loc) · 6.41 KB
/
configure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/bin/sh
# A handwritten ./configure script, because I kind of hate autotools.
# Update (2023): Autotools=still awful. But writing this was a mistake.
# © 2020–2023 Eddie Antonio Santos <[email protected]>
set -e
################################# CONSTANTS ##################################
version="$(<VERSION tr -d '\r')"
################################# VARIABLES ##################################
# A series of #define lines to write in src/cimg_config.h
defines=""
# A space-separated list of linker flags to link with required libraries
libs=""
# A space-separated list of compiler flags to configure include paths
includes=""
# The program used for pandoc
pandoc=""
# What flag to use to compile using C++11 features.
cxxstd=""
#################################### MAIN ####################################
main() {
############################ Check for features ############################
require_system_header sys/ioctl.h
require_system_header sys/time.h
require_system_header term.h
require_system_header sysexits.h
# Figure out how to compile C++ 11 code
if use_cxxflag -std=c++11 ; then
cxxstd="-std=c++11"
elif use_cxxflag -std=c++0x ; then
cxxstd="-std=c++0x"
else
error "unable to find a C++11 compiler"
fi
# Link with -lcurses (required)
if link_lib_against_test_program tigetnum ncurses ; then
libs="${libs} -lncurses"
elif link_lib_against_test_program tigetnum curses ; then
libs="${libs} -lcurses"
else
error "ncurses is required for compilation"
fi
# Link with -lpng (optional)
if link_lib_against_test_program_using_pkgconfig png_create_info_struct png ; then
defines="${defines}#define cimg_use_png 1${NEWLINE}"
extend_libs_and_includes_using_pkgconfig png
fi
# Link with -ljpg (optional)
if link_lib_against_test_program_using_pkgconfig jpeg_set_defaults jpeg ; then
defines="${defines}#define cimg_use_jpeg 1${NEWLINE}"
extend_libs_and_includes_using_pkgconfig jpeg
fi
# Check if pandoc is installed (optional)
if check_for_program pandoc ; then
pandoc="pandoc"
else
echo "warning: ignoring requirement for pandoc..." 1>&2
pandoc="true"
fi
############################## Generate files ##############################
generate_file config.mk <<EOF
# AUTOGENERATED FILE! DO NOT MODIFY!
PACKAGE_VERSION = ${version}
PANDOC = ${pandoc}
LIBS = ${libs}
INCLUDE_FLAGS = ${includes}
CXXSTD = ${cxxstd}
EOF
generate_file src/config.h <<EOF
/* AUTOGENERATED FILE; DO NOT MODIFY */
#ifndef CONFIG_H
#define CONFIG_H
#define PACKAGE_VERSION "${version}"
#endif /* CONFIG_H */
EOF
generate_file src/cimg_config.h <<EOF
/* AUTOGENERATED FILE; DO NOT MODIFY */
#ifndef CIMG_CONFIG_H
#define CIMG_CONFIG_H
/* IMPORTANT! Include this BEFORE CImg.h. */
${defines}
#endif /* CIMG_CONFIG_H */
EOF
}
############################# INTERNAL CONSTANTS #############################
# see: man 3 sysexits
EX_UNAVAILABLE=69
# (nice)
NEWLINE="$(printf '\n ')" && NEWLINE="${NEWLINE% }"
# Use the default C and C++ compilers
CC="${CC:-gcc}"
CXX="${CXX:-c++}"
TMPDIR="${TMPDIR:-/tmp}"
# Colors
BOLDRED="$(tput setaf 1 || :)$(tput bold || :)"
RESET="$(tput sgr0 || :)"
############################## HELPER FUNCTIONS ##############################
require_system_header() {
header="$1"
if ! compile_with_header "$header" ; then
error "failed to find required header: $header"
fi
}
use_cxxflag() {
flag="$1"
printf 'checking to compile C++ with %s...' "${flag}"
compile_test_cxx_program "${flag}" <<EOF
int main(void) {
void *p = nullptr;
return p != 0;
}
EOF
}
# Create a test program that uses a library function and link against that
# library. Returns zero on if the library was linked succesfully, non-zero
# otherwise.
link_lib_against_test_program() {
funcname=$1
lib=$2
printf 'checking to link with lib%s [using %s()]... ' "$lib" "$funcname"
compile_test_program -l"${lib}" <<EOF
char ${funcname}(void);
int main(void) {
return ${funcname}();
}
EOF
}
# Create a test program that uses a library function and link against that
# library. Returns zero on if the library was linked succesfully, non-zero
# otherwise.
link_lib_against_test_program_using_pkgconfig() {
funcname=$1
lib=$2
printf 'checking to link with lib%s using pkg-config [using %s()]... ' "$lib" "$funcname"
# Contrary to best practices, pkg-config actually depends on its output
# being broken by spaces to create multiple arguments from one invocation,
# so ignore the lint:
# shellcheck disable=SC2046
compile_test_program $(pkg-config --cflags --libs "lib${lib}") <<EOF
char ${funcname}(void);
int main(void) {
return ${funcname}();
}
EOF
}
# Add the appropriate flags for the given library using pkg-config(1)
extend_libs_and_includes_using_pkgconfig() {
lib=$1
libs="${libs} $(pkg-config --libs "lib${lib}")"
includes="${includes} $(pkg-config --cflags "lib${lib}")"
}
# Check if the program is on the path.
check_for_program() {
program="$1"
printf 'checking for %s... ' "${program}"
if which "${program}" >/dev/null 2>&1 ; then
echo "${program}"
return 0
else
echo "no"
return 1
fi
}
# Creates a file with contents from stdin.
generate_file() {
filename="$1"
echo "creating $filename"
cat > "$filename"
}
# Print an error message and quit
error() {
echo "$0: ${BOLDRED}error${RESET}:" "$@" 1>&2
exit ${EX_UNAVAILABLE}
}
# Create a test program that includes the given system header.
compile_with_header() {
header=$1
printf 'checking for %s... ' "$header"
# We include <Availability.h> on Apple to workaround issue an issue with
# term.h on newer macOS.
# See: https://github.com/eddieantonio/imgcat/issues/51
compile_test_program -c <<EOF
#ifdef __APPLE__
#include <Availability.h>
#endif
#include <${header}>
EOF
}
# Compiles a test C program given on stdin.
# Any arguments are passed to the compiler.
compile_test_program() {
filename="${TMPDIR}/conftest.$$.c"
cat > "${filename}"
if "${CC}" -o /dev/null "${filename}" "$@" ; then
echo "yes"
return 0
else
echo "no"
return 1
fi
}
# Compiles a test C++ program given on stdin.
# Any arguments are passed to the compiler.
compile_test_cxx_program() {
filename="${TMPDIR}/conftest.$$.cc"
cat > "${filename}"
if "${COMPILER:=${CC}}" -o /dev/null "${filename}" "$@" ; then
echo "yes"
return 0
else
echo "no"
return 1
fi
}
############################### Run configure! ###############################
main