5 -- Execute this file to configure the build system.
9 package = project:lower()
11 bugreport = "chaz@dogcows.com"
17 This script prepares ]]..project..[[ for building on your system.
18 Usage: ./configure [OPTION]... [VAR=VALUE]...
20 This is NOT an autoconf-generated configure script, though it was written
21 to be familiar by supporting many of the same options.
24 -h, --help display this help and exit
25 --host=HOST cross-compile the program to run on HOST
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
32 --disable-dependency-tracking speed up one-time builds
33 --enable-link-sh decrease the number of direct dependencies
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
43 --with-gtk use the gtk2 toolkit (overrides --with-qt4)
44 --with-qt4 use the qt4 gui toolkit
50 -- Define some useful functions.
54 for _,value in ipairs(arg) do
55 print("fatal: "..tostring(value))
60 function FileExists(file)
61 return os.execute("test -f "..file) == 0
64 function ReadCommand(command)
65 local fd = io.popen(command)
67 local stdout = fd:read("*l")
74 function TryCommand(command)
75 return os.execute(command.." 2>&1 >/dev/null") == 0
79 str = str:gsub("^%s+", "")
80 return str:gsub("%s+$", "")
84 str = str:gsub("%s+", " ")
90 -- Perform a quick sanity check.
93 if not FileExists("configure") or not FileExists("Makefile") then
94 Die("You must `cd' to the project root where the Makefile is.")
99 -- Parse the command-line options.
105 local directories = {}
106 local definitions = {}
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
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
120 local function AddDirectory(directory, path)
121 directories[directory] = path
124 local function AddDefinition(key, value)
125 definitions[key] = value
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,
140 handlers["-h"] = handlers["--help"]
142 local function ParseArg(arg)
143 for key,value in pairs(handlers) do
144 local matches = {arg:match(key)}
146 value(unpack(matches))
150 print("warning: unknown or incomplete argument "..arg)
154 -- Define some default values.
156 prefix = "/usr/local"
169 features["dependency-tracking"] = true
172 -- Read the arguments from the command-line.
173 for _,arg in ipairs(arg) do
177 function GetFeature(feature) return features[feature] end
178 function GetPackage(package) return packages[package] end
180 for key,value in pairs(directories) do
181 _G[key.."dir"] = value
183 for key,value in pairs(definitions) do
188 -- Define the dependent values.
190 if not host then host = ReadCommand("tools/config.guess") end
191 alt_host = ReadCommand("tools/config.sub "..host)
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
213 define.DEP_TRACKING = GetFeature("dependency-tracking")
218 -- Determine the target platform.
221 if host:match("mingw32") then platform = "win32"
222 elseif host:match("netbsd") then platform = "netbsd" end
226 -- Define the check function.
230 local path = os.getenv("PATH")
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
238 for dir in path:gmatch("[^:]+") do
239 if FileExists(dir.."/"..command) and TryCommand(command.." "..args) then
251 -- Look for a working toolchain.
254 print("Please wait...")
257 tmpname = os.tmpname()..".c"
258 tmpfile, err = io.open(tmpname, "w")
264 printf("Hello world!\n");
270 function extra() if not cross_compile then return "gcc", "cc" end end
277 extra()}, tmpname.." -o "..tmpname..".tmp")
279 os.remove(tmpname..".tmp")
280 if not CC then Die("Can't find a working C compiler.") end
282 Die("failed to create temporary file: "..err)
286 tmpname = os.tmpname()..".c"
287 tmpfile, err = io.open(tmpname, "w")
293 std::cout << "Hello world!" << std::endl;
299 function extra() if not cross_compile then return "g++", "c++" end end
306 extra()}, tmpname.." -o "..tmpname..".tmp")
308 os.remove(tmpname..".tmp")
309 if not CXX then Die("Can't find a working C++ compiler.") end
311 Die("failed to create temporary file: "..err)
316 function extra() if not cross_compile then return "ar" end end
321 extra()}, "--version")
322 if not AR then Die("Can't find a working archiver.") end
327 function extra() if not cross_compile then return "ranlib" end end
328 RANLIB = FindCommand({
332 extra()}, "--version")
333 if not RANLIB then Die("Can't find a working library indexer.") end
336 -- Check for WINDRES.
337 if platform == "win32" then
338 function extra() if not cross_compile then return "windres" end end
339 WINDRES = FindCommand({
342 alt_host.."-windres",
343 extra()}, "--version")
344 if not WINDRES then Die("Can't find a working win32 resource compiler.") end
349 -- Configure the features and packages.
352 if GetFeature("debug") then
353 cflags = cflags.." -O0 -Wall -Wno-uninitialized"
359 if GetFeature("double-precision") then
360 config.USE_DOUBLE_PRECISION = true
363 if GetFeature("profile") then
364 cflags = cflags.." -pg"
365 config.PROFILING_ENABLED = true
368 if GetFeature("extra-warnings") then
369 cflags = cflags.." -Wextra -Wno-unused-parameter"
372 if GetFeature("link-sh") then
376 if GetFeature("clock_gettime") then
380 if GetFeature("threads") then
381 config.USE_THREADS = true
384 if GetFeature("hotloading") then
385 print("FYI: Hotloading is very experimental and only works on Linux.");
386 config.USE_HOTLOADING = true
389 if GetPackage("gtk") then
393 if GetPackage("qt4") then
399 -- Check for the libraries we need.
403 local command = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH pkg-config"
404 local deps = "sdl gl glu libpng openal vorbisfile lua"
406 if GetPackage("gtk") then
407 deps = deps.." gtk+-2.0"
408 elseif GetPackage("qt4") then
409 deps = deps.." QtGui"
412 local pc_cflags = ReadCommand(command.." --cflags "..deps)
413 if not pc_cflags then Die("Couldn't determine CFLAGS.") end
414 CFLAGS = CFLAGS.." "..pc_cflags
415 CXXFLAGS = CXXFLAGS.." "..pc_cflags
417 local pc_libs = ReadCommand(command.." --libs "..deps)
418 if not pc_libs then Die("Couldn't determine LDFLAGS or LIBS.") end
419 for lib in pc_libs:gmatch("%-l%S+") do
420 LIBS = LIBS.." "..lib
422 for ldflag in pc_libs:gmatch("%-[^l]%S*") do
423 LDFLAGS = LDFLAGS.." "..ldflag
426 if platform == "win32" then
427 LIBS = LIBS.." -lws2_32"
428 exe_extension = ".exe"
434 CFLAGS = Reduce(CFLAGS.." "..cflags)
435 CXXFLAGS = Reduce(CXXFLAGS.." "..cflags)
436 LDFLAGS = Reduce(LDFLAGS)
441 -- Define the exports and definitions.
444 config.YOINK_DATADIR = datadir
445 export.YOINK_DATADIR = datadir
448 local vmajor = ReadCommand("v=$(echo "..version.." | cut -d. -f1); echo ${v:-0}")
449 local vminor = ReadCommand("v=$(echo "..version.." | cut -d. -f2); echo ${v:-0}")
450 local vrevis = ReadCommand("v=$(echo "..version.." | cut -d. -f3); echo ${v:-0}")
452 config.VERSION_MAJOR = tonumber(vmajor)
453 config.VERSION_MINOR = tonumber(vminor)
454 config.VERSION_REVISION = tonumber(vrevis)
458 -- Determine and define the git revision.
459 local head = ReadCommand("git log -n1 --date=short --pretty=format:\"%h (%ad)\"")
460 config.YOINK_GITHEAD = head
463 config.PACKAGE = project
464 config.PACKAGE_NAME = project
465 config.VERSION = version
466 config.PACKAGE_VERSION = version
467 config.PACKAGE_STRING = project.." "..version
468 config.PACKAGE_BUGREPORT = bugreport
471 define.PLATFORM = platform
475 define.RANLIB = RANLIB
476 define.WINDRES = WINDRES
477 define.CFLAGS = CFLAGS
478 define.CXXFLAGS = CXXFLAGS
479 define.LDFLAGS = LDFLAGS
481 define.prefix = prefix
482 define.bindir = bindir
483 define.datadir = datadir
484 define.mandir = mandir
485 define.EXEEXT = exe_extension
487 export.PACKAGE_BUGREPORT = bugreport
491 -- All done; output the configuration files.
494 -- Print the program options.
495 output = io.open("config.h", "w")
496 for key,value in pairs(config) do
498 if type(value) == "boolean" then
500 output:write("#define "..key)
502 output:write("#undef "..key)
504 elseif type(value) == "string" then
505 output:write(string.format("#define %s %q", key, tostring(value)))
507 output:write(string.format("#define %s %s", key, tostring(value)))
513 -- Print the make definitions.
514 output = io.open("config.mk", "w")
515 for key,value in pairs(define) do
517 value = tostring(value)
518 output:write(string.format("%s = %s\n", key, value))
523 -- Print the exported variables.
524 output = io.open("config.sed", "w")
525 for key,value in pairs(export) do
526 key = key:gsub("/", "\\/")
527 value = value:gsub("/", "\\/")
528 output:write(string.format("s/@%s@/%s/g\n", key, value))
535 Configuration complete! You can review your configuration in `config.mk'.
537 To finish the installation, type: