5e691c171eacb6b4b11a9f02aba5e4285de63638
[chaz/yoink] / configure
1 #!/usr/bin/env lua
2
3 --
4 -- Yoink
5 -- Execute this file to configure the build system.
6 --
7
8 project = "Yoink"
9 package = project:lower()
10 version = "0.1"
11 bugreport = "chaz@dogcows.com"
12
13
14 function ShowHelp()
15 print([[
16
17 This script prepares ]]..project..[[ for building on your system.
18 Usage: ./configure [OPTION]... [VAR=VALUE]...
19
20 This is NOT an autoconf-generated configure script, though it was written
21 to be familiar by supporting many of the same options.
22
23 Basic configuration:
24 -h, --help display this help and exit
25 --host=HOST cross-compile the program to run on HOST
26
27 --prefix=DIR base directory to install programs to
28 --bindir=DIR directory to install executables
29 --datadir=DIR directory to install shared data files
30 --mandir=DIR directory to install manual pages
31
32 --disable-dependency-tracking speed up one-time builds
33 --enable-link-sh decrease the number of direct dependencies
34
35 Program options:
36 --enable-debug include debugging symbols and code paths
37 --enable-double-precision use doubles instead of floats
38 --enable-profile compile in gprof profiling instructions
39 --enable-clock_gettime use clock_gettime() for timing
40 --enable-threads use threads for concurrency
41 --enable-hotloading watch assets for automatic reloading
42
43 --with-gtk use gtk2 toolkit (overrides --with-qt4)
44 --with-qt4 use the qt4 gui toolkit
45 ]])
46 end
47
48
49 --
50 -- Define some useful functions.
51 --
52
53 function Die(...)
54 for _,value in ipairs(arg) do
55 print("fatal: "..tostring(value))
56 end
57 os.exit(1)
58 end
59
60 function FileExists(file)
61 return os.execute("test -f "..file) == 0
62 end
63
64 function ReadCommand(command)
65 local fd = io.popen(command)
66 if fd then
67 local stdout = fd:read("*l")
68 fd:close()
69 return stdout
70 end
71 return nil
72 end
73
74 function TryCommand(command)
75 return os.execute(command.." 2>&1 >/dev/null") == 0
76 end
77
78 function Trim(str)
79 str = str:gsub("^%s+", "")
80 return str:gsub("%s+$", "")
81 end
82
83 function Reduce(str)
84 str = str:gsub("%s+", " ")
85 return Trim(str)
86 end
87
88
89 --
90 -- Perform a quick sanity check.
91 --
92
93 if not FileExists("configure") or not FileExists("Makefile") then
94 Die("You must `cd' to the project root where the Makefile is.")
95 end
96
97
98 --
99 -- Parse the command-line options.
100 --
101
102 do
103 local features = {}
104 local packages = {}
105 local directories = {}
106 local definitions = {}
107
108 local function AddFeature(feature, value)
109 if value == "yes" or value == "" then value = true end
110 if value == "no" then value = false end
111 features[feature] = value
112 end
113
114 local function AddPackage(package, value)
115 if value == "yes" or value == "" then value = true end
116 if value == "no" then value = false end
117 packages[package] = value
118 end
119
120 local function AddDirectory(directory, path)
121 directories[directory] = path
122 end
123
124 local function AddDefinition(key, value)
125 definitions[key] = value
126 end
127
128 local handlers = {
129 ["--help"] = function() ShowHelp() os.exit(0) end,
130 ["--host=(.+)"] = function(arg) host = arg cross_compile = true end,
131 ["--enable%-([%w_-]+)=?(.*)"] = AddFeature,
132 ["--disable%-([%w_-]+)"] = function(arg) AddFeature(arg, "no") end,
133 ["--with%-([%w_-]+)=?(.*)"] = AddPackage,
134 ["--without%-([%w_-]+)"] = function(arg) AddPackage(arg, "no") end,
135 ["--(%l+)dir=(.+)"] = AddDirectory,
136 ["--prefix=(.+)"] = function(arg) prefix = arg end,
137 ["--exec-prefix=(.+)"] = function(arg) eprefix = arg end,
138 ["([%w_]+)=(.*)"] = AddDefinition,
139 }
140 handlers["-h"] = handlers["--help"]
141
142 local function ParseArg(arg)
143 for key,value in pairs(handlers) do
144 local matches = {arg:match(key)}
145 if matches[1] then
146 value(unpack(matches))
147 return
148 end
149 end
150 print("warning: unknown or incomplete argument "..arg)
151 end
152
153
154 -- Define some default values.
155
156 prefix = "/usr/local"
157
158 CC = ""
159 CXX = ""
160 AR = ""
161 RANLIB = ""
162 WINDRES = ""
163
164 CFLAGS = "-g -O2"
165 CXXFLAGS = CFLAGS
166 LDFLAGS = ""
167 LIBS = ""
168
169 features["dependency-tracking"] = true
170
171
172 -- Read the arguments from the command-line.
173 for _,arg in ipairs(arg) do
174 ParseArg(arg)
175 end
176
177 function GetFeature(feature) return features[feature] end
178 function GetPackage(package) return packages[package] end
179
180 for key,value in pairs(directories) do
181 _G[key.."dir"] = value
182 end
183 for key,value in pairs(definitions) do
184 _G[key] = value
185 end
186
187
188 -- Define the dependent values.
189
190 if not host then host = ReadCommand("build/config.guess") end
191 alt_host = ReadCommand("build/config.sub "..host)
192
193 if not eprefix then eprefix = prefix end
194 if not bindir then bindir = eprefix.."/bin" end
195 if not sbindir then sbindir = eprefix.."/sbin" end
196 if not libexecdir then libexecdir = eprefix.."/libexec" end
197 if not sysconfdir then sysconfdir = prefix.."/etc" end
198 if not localstatedir then localstatedir = prefix.."/var" end
199 if not libdir then libdir = eprefix.."/lib" end
200 if not includedir then includedir = prefix.."/include" end
201 if not datarootdir then datarootdir = prefix.."/share" end
202 if not datadir then datadir = datarootdir.."/"..package end
203 if not infodir then infodir = datarootdir.."/info" end
204 if not localedir then localedir = datarootdir.."/locale" end
205 if not mandir then mandir = datarootdir.."/man" end
206 if not docdir then docdir = datarootdir.."/doc/"..package end
207
208 cflags = ""
209 config = {}
210 define = {}
211 export = {}
212
213 define.DEP_TRACKING = GetFeature("dependency-tracking")
214 end
215
216
217 --
218 -- Determine the target platform.
219 --
220
221 if host:match("mingw32") then platform = "win32"
222 elseif host:match("netbsd") then platform = "netbsd" end
223
224
225 --
226 -- Define the check function.
227 --
228
229 do
230 local path = os.getenv("PATH")
231
232 -- 1. List of possible command names.
233 -- 2. Command arguments.
234 function FindCommand(commands, args)
235 if not args then args = "" end
236 for _,command in ipairs(commands) do
237 if command then
238 for dir in path:gmatch("[^:]+") do
239 if FileExists(dir.."/"..command) and TryCommand(command.." "..args) then
240 return command
241 end
242 end
243 end
244 end
245 return nil
246 end
247 end
248
249
250 --
251 -- Look for a working toolchain.
252 --
253
254 print("Please wait...")
255
256 -- Check for CC.
257 tmpname = os.tmpname()..".c"
258 tmpfile, err = io.open(tmpname, "w")
259 if tmpfile then
260 tmpfile:write([[
261 #include <stdio.h>
262 int main()
263 {
264 printf("Hello world!\n");
265 return 0;
266 }]])
267 tmpfile:close()
268
269 function extra() if not cross_compile then return "gcc", "cc" end end
270 CC = FindCommand({
271 CC,
272 host.."-gcc",
273 host.."-cc",
274 alt_host.."-gcc",
275 alt_host.."-cc",
276 extra()}, tmpname.." -o "..tmpname..".tmp")
277 os.remove(tmpname)
278 os.remove(tmpname..".tmp")
279 if not CC then Die("Can't find a working C compiler.") end
280 else
281 Die("failed to create temporary file: "..err)
282 end
283
284 -- Check for CXX.
285 tmpname = os.tmpname()..".c"
286 tmpfile, err = io.open(tmpname, "w")
287 if tmpfile then
288 tmpfile:write([[
289 #include <iostream>
290 int main()
291 {
292 std::cout << "Hello world!" << std::endl;
293 return 0;
294 }]])
295 tmpfile:close()
296
297 function extra() if not cross_compile then return "g++", "c++" end end
298 CXX = FindCommand({
299 CXX,
300 host.."-g++",
301 host.."-c++",
302 alt_host.."-g++",
303 alt_host.."-c++",
304 extra()}, tmpname.." -o "..tmpname..".tmp")
305 os.remove(tmpname)
306 os.remove(tmpname..".tmp")
307 if not CXX then Die("Can't find a working C++ compiler.") end
308 else
309 Die("failed to create temporary file: "..err)
310 end
311
312 -- Check for AR.
313 do
314 function extra() if not cross_compile then return "ar" end end
315 AR = FindCommand({
316 AR,
317 host.."-ar",
318 alt_host.."-ar",
319 extra()}, "--version")
320 if not AR then Die("Can't find a working archiver.") end
321 end
322
323 -- Check for RANLIB.
324 do
325 function extra() if not cross_compile then return "ranlib" end end
326 RANLIB = FindCommand({
327 RANLIB,
328 host.."-ranlib",
329 alt_host.."-ranlib",
330 extra()}, "--version")
331 if not RANLIB then Die("Can't find a working library indexer.") end
332 end
333
334 -- Check for WINDRES.
335 if platform == "win32" then
336 function extra() if not cross_compile then return "windres" end end
337 WINDRES = FindCommand({
338 WINDRES,
339 host.."-windres",
340 alt_host.."-windres",
341 extra()}, "--version")
342 if not WINDRES then Die("Can't find a working win32 resource compiler.") end
343 end
344
345
346 --
347 -- Configure the features and packages.
348 --
349
350 if GetFeature("debug") then
351 cflags = cflags.." -O0 -Wall -Wno-uninitialized"
352 config.DEBUG = true
353 else
354 config.NDEBUG = true
355 end
356
357 if GetFeature("double-precision") then
358 config.USE_DOUBLE_PRECISION = true
359 end
360
361 if GetFeature("profile") then
362 cflags = cflags.." -pg"
363 config.PROFILING_ENABLED = true
364 end
365
366 if GetFeature("extra-warnings") then
367 cflags = cflags.." -Wextra -Wno-unused-parameter"
368 end
369
370 if GetFeature("link-sh") then
371 -- TODO
372 end
373
374 if GetFeature("clock_gettime") then
375 -- TODO
376 end
377
378 if GetFeature("threads") then
379 config.USE_THREADS = true
380 end
381
382 if GetFeature("hotloading") then
383 print("FYI: Hotloading is very experimental and only works on Linux.");
384 config.USE_HOTLOADING = true
385 end
386
387 if GetPackage("gtk") then
388 -- TODO
389 end
390
391 if GetPackage("qt4") then
392 -- TODO
393 end
394
395
396 --
397 -- Check for the libraries we need.
398 --
399
400 do
401 local command = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH pkg-config"
402 local deps = "sdl gl glu libpng openal vorbisfile lua"
403
404 if GetPackage("gtk") then
405 deps = deps.." gtk+-2.0"
406 elseif GetPackage("qt4") then
407 deps = deps.." QtGui"
408 end
409
410 local pc_cflags = ReadCommand(command.." --cflags "..deps)
411 if not pc_cflags then Die("Couldn't determine CFLAGS.") end
412 CFLAGS = CFLAGS.." "..pc_cflags
413 CXXFLAGS = CXXFLAGS.." "..pc_cflags
414
415 local pc_libs = ReadCommand(command.." --libs "..deps)
416 if not pc_libs then Die("Couldn't determine LDFLAGS or LIBS.") end
417 for lib in pc_libs:gmatch("%-l%S+") do
418 LIBS = LIBS.." "..lib
419 end
420 for ldflag in pc_libs:gmatch("%-[^l]%S*") do
421 LDFLAGS = LDFLAGS.." "..ldflag
422 end
423
424 if platform == "win32" then
425 LIBS = LIBS.." -lws2_32"
426 exe_extension = ".exe"
427 else
428 exe_extension = ""
429 end
430 end
431
432 CFLAGS = Reduce(CFLAGS.." "..cflags)
433 CXXFLAGS = Reduce(CXXFLAGS.." "..cflags)
434 LDFLAGS = Reduce(LDFLAGS)
435 LIBS = Reduce(LIBS)
436
437
438 --
439 -- Define the exports and definitions.
440 --
441
442 config.YOINK_DATADIR = datadir
443 export.YOINK_DATADIR = datadir
444
445 do
446 local vmajor = ReadCommand("v=$(echo "..version.." | cut -d. -f1); echo ${v:-0}")
447 local vminor = ReadCommand("v=$(echo "..version.." | cut -d. -f2); echo ${v:-0}")
448 local vrevis = ReadCommand("v=$(echo "..version.." | cut -d. -f3); echo ${v:-0}")
449
450 config.VERSION_MAJOR = tonumber(vmajor)
451 config.VERSION_MINOR = tonumber(vminor)
452 config.VERSION_REVISION = tonumber(vrevis)
453 end
454
455 do
456 -- Determine and define the git revision.
457 local head = ReadCommand("git log -n1 --date=short --pretty=format:\"%h (%ad)\"")
458 config.YOINK_GITHEAD = head
459 end
460
461 config.PACKAGE = project
462 config.PACKAGE_NAME = project
463 config.VERSION = version
464 config.PACKAGE_VERSION = version
465 config.PACKAGE_STRING = project.." "..version
466 config.PACKAGE_BUGREPORT = bugreport
467
468 define.TARGET = host
469 define.PLATFORM = platform
470 define.CC = CC
471 define.CXX = CXX
472 define.AR = AR
473 define.RANLIB = RANLIB
474 define.WINDRES = WINDRES
475 define.CFLAGS = CFLAGS
476 define.CXXFLAGS = CXXFLAGS
477 define.LDFLAGS = LDFLAGS
478 define.LIBS = LIBS
479 define.prefix = prefix
480 define.bindir = bindir
481 define.datadir = datadir
482 define.mandir = mandir
483 define.EXEEXT = exe_extension
484
485
486 --
487 -- All done; output the configuration files.
488 --
489
490 -- Print the program options.
491 output = io.open("config.h", "w")
492 for key,value in pairs(config) do
493 key = tostring(key)
494 if type(value) == "boolean" then
495 if value then
496 output:write("#define "..key)
497 else
498 output:write("#undef "..key)
499 end
500 elseif type(value) == "string" then
501 output:write(string.format("#define %s %q", key, tostring(value)))
502 else
503 output:write(string.format("#define %s %s", key, tostring(value)))
504 end
505 output:write("\n")
506 end
507 output:close()
508
509 -- Print the make definitions.
510 output = io.open("config.mk", "w")
511 for key,value in pairs(define) do
512 key = tostring(key)
513 value = tostring(value)
514 output:write(string.format("%s = %s\n", key, value))
515 end
516 output:close()
517
518
519 -- Print the exported variables.
520 output = io.open("config.sed", "w")
521 for key,value in pairs(export) do
522 key = key:gsub("/", "\\/")
523 value = value:gsub("/", "\\/")
524 output:write(string.format("s/@%s@/%s/g\n", key, value))
525 end
526 output:close()
527
528
529 print([[
530
531 Configuration complete! You can review your configuration in `config.mk'.
532
533 To finish the installation, type:
534 make
535 make install
536 ]])
537
This page took 0.054965 seconds and 3 git commands to generate.