5 -- Execute this file to configure the build system.
10 bugreport = "chaz@dogcows.com"
16 This script prepares ]]..project..[[ for building on your system.
17 Usage: ./configure [OPTION]... [VAR=VALUE]...
19 This is NOT an autoconf-generated configure script, though it was written
20 to be familiar by supporting many of the same options.
23 -h, --help display this help and exit
24 --host=HOST cross-compile the program to run on HOST
26 --prefix=DIR base directory to install programs to
27 --bindir=DIR directory to install executables
28 --datadir=DIR directory to install shared data files
29 --mandir=DIR directory to install manual pages
31 --disable-dependency-tracking speed up one-time builds
32 --enable-link-sh decrease the number of direct dependencies
35 --enable-debug include debugging symbols and code paths
36 --enable-double-precision use doubles instead of floats
37 --enable-profile compile in gprof profiling instructions
38 --enable-clock_gettime use clock_gettime() for timing
39 --enable-threads use threads for concurrency
40 --enable-hotloading watch assets for automatic reloading
42 --with-gtk use the gtk2 toolkit (overrides --with-qt4)
43 --with-qt4 use the qt4 gui toolkit
49 -- Define some useful functions.
53 for _,value in ipairs(arg) do
54 print("fatal: "..tostring(value))
59 function FileExists(file)
60 return os.execute("test -f "..file) == 0
63 function ReadCommand(command)
64 local fd = io.popen(command)
66 local stdout = fd:read("*l")
73 function TryCommand(command)
74 return os.execute(command.." 2>&1 >/dev/null") == 0
78 str = str:gsub("^%s+", "")
79 return str:gsub("%s+$", "")
83 str = str:gsub("%s+", " ")
89 -- Perform a quick sanity check.
92 if not FileExists("configure") or not FileExists("Makefile") then
93 Die("You must `cd' to the project root where the Makefile is.")
98 -- Parse the command-line options.
104 local directories = {}
105 local definitions = {}
107 local function AddFeature(feature, value)
108 if value == "yes" or value == "" then value = true end
109 if value == "no" then value = false end
110 features[feature] = value
113 local function AddPackage(package, value)
114 if value == "yes" or value == "" then value = true end
115 if value == "no" then value = false end
116 packages[package] = value
119 local function AddDirectory(directory, path)
120 directories[directory] = path
123 local function AddDefinition(key, value)
124 definitions[key] = value
128 ["--help"] = function() ShowHelp() os.exit(0) end,
129 ["--host=(.+)"] = function(arg) host = arg cross_compile = true end,
130 ["--enable%-([%w_-]+)=?(.*)"] = AddFeature,
131 ["--disable%-([%w_-]+)"] = function(arg) AddFeature(arg, "no") end,
132 ["--with%-([%w_-]+)=?(.*)"] = AddPackage,
133 ["--without%-([%w_-]+)"] = function(arg) AddPackage(arg, "no") end,
134 ["--(%l+)dir=(.+)"] = AddDirectory,
135 ["--prefix=(.+)"] = function(arg) prefix = arg end,
136 ["--exec-prefix=(.+)"] = function(arg) eprefix = arg end,
137 ["([%w_]+)=(.*)"] = AddDefinition,
139 handlers["-h"] = handlers["--help"]
141 local function ParseArg(arg)
142 for key,value in pairs(handlers) do
143 local matches = {arg:match(key)}
145 value(unpack(matches))
149 print("warning: unknown or incomplete argument "..arg)
153 -- Define some default values.
155 prefix = "/usr/local"
168 features["dependency-tracking"] = true
171 -- Read the arguments from the command-line.
172 for _,arg in ipairs(arg) do
176 function GetFeature(feature) return features[feature] end
177 function GetPackage(package) return packages[package] end
179 for key,value in pairs(directories) do
180 _G[key.."dir"] = value
182 for key,value in pairs(definitions) do
187 -- Define the dependent values.
189 package = project:lower()
190 tarname = package.."-"..version
192 if not host then host = ReadCommand("tools/config.guess") end
193 alt_host = ReadCommand("tools/config.sub "..host)
195 if not eprefix then eprefix = prefix end
196 if not bindir then bindir = eprefix.."/bin" end
197 if not sbindir then sbindir = eprefix.."/sbin" end
198 if not libexecdir then libexecdir = eprefix.."/libexec" end
199 if not sysconfdir then sysconfdir = prefix.."/etc" end
200 if not localstatedir then localstatedir = prefix.."/var" end
201 if not libdir then libdir = eprefix.."/lib" end
202 if not includedir then includedir = prefix.."/include" end
203 if not datarootdir then datarootdir = prefix.."/share" end
204 if not datadir then datadir = datarootdir.."/"..package end
205 if not infodir then infodir = datarootdir.."/info" end
206 if not localedir then localedir = datarootdir.."/locale" end
207 if not mandir then mandir = datarootdir.."/man" end
208 if not docdir then docdir = datarootdir.."/doc/"..package end
215 define.DEP_TRACKING = GetFeature("dependency-tracking")
220 -- Determine the target platform.
223 if host:match("mingw32") then platform = "win32"
224 elseif host:match("netbsd") then platform = "netbsd" end
228 -- Define the check function.
232 local path = os.getenv("PATH")
234 -- 1. List of possible command names.
235 -- 2. Command arguments.
236 function FindCommand(commands, args)
237 if not args then args = "" end
238 for _,command in ipairs(commands) do
240 for dir in path:gmatch("[^:]+") do
241 if FileExists(dir.."/"..command) and TryCommand(command.." "..args) then
253 -- Look for a working toolchain.
256 print("Please wait...")
259 tmpname = os.tmpname()..".c"
260 tmpfile, err = io.open(tmpname, "w")
266 printf("Hello world!\n");
272 function extra() if not cross_compile then return "gcc", "cc" end end
279 extra()}, tmpname.." -o "..tmpname..".tmp")
281 os.remove(tmpname..".tmp")
282 if not CC then Die("Can't find a working C compiler.") end
284 Die("failed to create temporary file: "..err)
288 tmpname = os.tmpname()..".c"
289 tmpfile, err = io.open(tmpname, "w")
295 std::cout << "Hello world!" << std::endl;
301 function extra() if not cross_compile then return "g++", "c++" end end
308 extra()}, tmpname.." -o "..tmpname..".tmp")
310 os.remove(tmpname..".tmp")
311 if not CXX then Die("Can't find a working C++ compiler.") end
313 Die("failed to create temporary file: "..err)
318 function extra() if not cross_compile then return "ar" end end
323 extra()}, "--version")
324 if not AR then Die("Can't find a working archiver.") end
329 function extra() if not cross_compile then return "ranlib" end end
330 RANLIB = FindCommand({
334 extra()}, "--version")
335 if not RANLIB then Die("Can't find a working library indexer.") end
338 -- Check for WINDRES.
339 if platform == "win32" then
340 function extra() if not cross_compile then return "windres" end end
341 WINDRES = FindCommand({
344 alt_host.."-windres",
345 extra()}, "--version")
346 if not WINDRES then Die("Can't find a working win32 resource compiler.") end
351 -- Configure the features and packages.
354 if GetFeature("debug") then
355 cflags = cflags.." -O0 -Wall -Wno-uninitialized"
361 if GetFeature("double-precision") then
362 config.USE_DOUBLE_PRECISION = true
365 if GetFeature("profile") then
366 cflags = cflags.." -pg"
367 config.PROFILING_ENABLED = true
370 if GetFeature("extra-warnings") then
371 cflags = cflags.." -Wextra -Wno-unused-parameter"
374 if GetFeature("link-sh") then
378 if GetFeature("clock_gettime") then
382 if GetFeature("threads") then
383 config.USE_THREADS = true
386 if GetFeature("hotloading") then
387 print("FYI: Hotloading is very experimental and only works on Linux.");
388 config.USE_HOTLOADING = true
391 if GetPackage("gtk") then
395 if GetPackage("qt4") then
401 -- Check for the libraries we need.
405 local command = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH pkg-config"
406 local deps = "sdl gl glu libpng openal vorbisfile lua"
408 if GetPackage("gtk") then
409 deps = deps.." gtk+-2.0"
410 elseif GetPackage("qt4") then
411 deps = deps.." QtGui"
414 local pc_cflags = ReadCommand(command.." --cflags "..deps)
415 if not pc_cflags then Die("Couldn't determine CFLAGS.") end
416 CFLAGS = CFLAGS.." "..pc_cflags
417 CXXFLAGS = CXXFLAGS.." "..pc_cflags
419 local pc_libs = ReadCommand(command.." --libs "..deps)
420 if not pc_libs then Die("Couldn't determine LDFLAGS or LIBS.") end
421 for lib in pc_libs:gmatch("%-l%S+") do
422 LIBS = LIBS.." "..lib
424 for ldflag in pc_libs:gmatch("%-[^l]%S*") do
425 LDFLAGS = LDFLAGS.." "..ldflag
428 if platform == "win32" then
429 LIBS = LIBS.." -lws2_32"
430 exe_extension = ".exe"
436 CFLAGS = Reduce(CFLAGS.." "..cflags)
437 CXXFLAGS = Reduce(CXXFLAGS.." "..cflags)
438 LDFLAGS = Reduce(LDFLAGS)
443 -- Define the exports and definitions.
446 config.YOINK_DATADIR = datadir
447 export.YOINK_DATADIR = datadir
450 local vmajor = ReadCommand("v=$(echo "..version.." | cut -d. -f1); echo ${v:-0}")
451 local vminor = ReadCommand("v=$(echo "..version.." | cut -d. -f2); echo ${v:-0}")
452 local vrevis = ReadCommand("v=$(echo "..version.." | cut -d. -f3); echo ${v:-0}")
454 config.VERSION_MAJOR = tonumber(vmajor)
455 config.VERSION_MINOR = tonumber(vminor)
456 config.VERSION_REVISION = tonumber(vrevis)
460 -- Determine and define the git revision.
461 local head = ReadCommand("git describe master")
462 config.YOINK_GITHEAD = head
465 config.PACKAGE = project
466 config.PACKAGE_NAME = project
467 config.VERSION = version
468 config.PACKAGE_VERSION = version
469 config.PACKAGE_STRING = project.." "..version
470 config.PACKAGE_BUGREPORT = bugreport
472 define.PACKAGE = project
473 define.TARNAME = tarname
475 define.PLATFORM = platform
479 define.RANLIB = RANLIB
480 define.WINDRES = WINDRES
481 define.CFLAGS = CFLAGS
482 define.CXXFLAGS = CXXFLAGS
483 define.LDFLAGS = LDFLAGS
485 define.prefix = prefix
486 define.bindir = bindir
487 define.datadir = datadir
488 define.mandir = mandir
489 define.EXEEXT = exe_extension
491 export.PACKAGE_BUGREPORT = bugreport
495 -- All done; output the configuration files.
498 -- Print the program options.
499 output = io.open("config.h", "w")
500 for key,value in pairs(config) do
502 if type(value) == "boolean" then
504 output:write("#define "..key)
506 output:write("#undef "..key)
508 elseif type(value) == "string" then
509 output:write(string.format("#define %s %q", key, tostring(value)))
511 output:write(string.format("#define %s %s", key, tostring(value)))
517 -- Print the make definitions.
518 output = io.open("config.mk", "w")
519 for key,value in pairs(define) do
521 value = tostring(value)
522 output:write(string.format("%s = %s\n", key, value))
527 -- Print the exported variables.
528 output = io.open("config.sed", "w")
529 for key,value in pairs(export) do
530 key = key:gsub("/", "\\/")
531 value = value:gsub("/", "\\/")
532 output:write(string.format("s/@%s@/%s/g\n", key, value))
539 Configuration complete! You can review your configuration in `config.mk'.
541 To finish the installation, type: