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 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("build/config.guess") end
191 alt_host = ReadCommand("build/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");
269 function extra() if not cross_compile then return "gcc", "cc" end end
276 extra()}, tmpname.." -o "..tmpname..".tmp")
278 os.remove(tmpname..".tmp")
279 if not CC then Die("Can't find a working C compiler.") end
281 Die("failed to create temporary file: "..err)
285 tmpname = os.tmpname()..".c"
286 tmpfile, err = io.open(tmpname, "w")
292 std::cout << "Hello world!" << std::endl;
297 function extra() if not cross_compile then return "g++", "c++" end end
304 extra()}, tmpname.." -o "..tmpname..".tmp")
306 os.remove(tmpname..".tmp")
307 if not CXX then Die("Can't find a working C++ compiler.") end
309 Die("failed to create temporary file: "..err)
314 function extra() if not cross_compile then return "ar" end end
319 extra()}, "--version")
320 if not AR then Die("Can't find a working archiver.") end
325 function extra() if not cross_compile then return "ranlib" end end
326 RANLIB = FindCommand({
330 extra()}, "--version")
331 if not RANLIB then Die("Can't find a working library indexer.") end
334 -- Check for WINDRES.
335 if platform == "win32" then
336 function extra() if not cross_compile then return "windres" end end
337 WINDRES = FindCommand({
340 alt_host.."-windres",
341 extra()}, "--version")
342 if not WINDRES then Die("Can't find a working win32 resource compiler.") end
347 -- Configure the features and packages.
350 if GetFeature("debug") then
351 cflags = cflags.." -O0 -Wall -Wno-uninitialized"
357 if GetFeature("double-precision") then
358 config.USE_DOUBLE_PRECISION = true
361 if GetFeature("profile") then
362 cflags = cflags.." -pg"
363 config.PROFILING_ENABLED = true
366 if GetFeature("extra-warnings") then
367 cflags = cflags.." -Wextra -Wno-unused-parameter"
370 if GetFeature("link-sh") then
374 if GetFeature("clock_gettime") then
378 if GetFeature("threads") then
379 config.USE_THREADS = true
382 if GetFeature("hotloading") then
383 print("FYI: Hotloading is very experimental and only works on Linux.");
384 config.USE_HOTLOADING = true
387 if GetPackage("gtk") then
391 if GetPackage("qt4") then
397 -- Check for the libraries we need.
401 local command = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH pkg-config"
402 local deps = "sdl gl glu libpng openal vorbisfile lua"
404 if GetPackage("gtk") then
405 deps = deps.." gtk+-2.0"
406 elseif GetPackage("qt4") then
407 deps = deps.." QtGui"
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
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
420 for ldflag in pc_libs:gmatch("%-[^l]%S*") do
421 LDFLAGS = LDFLAGS.." "..ldflag
424 if platform == "win32" then
425 LIBS = LIBS.." -lws2_32"
426 exe_extension = ".exe"
432 CFLAGS = Reduce(CFLAGS.." "..cflags)
433 CXXFLAGS = Reduce(CXXFLAGS.." "..cflags)
434 LDFLAGS = Reduce(LDFLAGS)
439 -- Define the exports and definitions.
442 config.YOINK_DATADIR = datadir
443 export.YOINK_DATADIR = datadir
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}")
450 config.VERSION_MAJOR = tonumber(vmajor)
451 config.VERSION_MINOR = tonumber(vminor)
452 config.VERSION_REVISION = tonumber(vrevis)
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
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
469 define.PLATFORM = platform
473 define.RANLIB = RANLIB
474 define.WINDRES = WINDRES
475 define.CFLAGS = CFLAGS
476 define.CXXFLAGS = CXXFLAGS
477 define.LDFLAGS = LDFLAGS
479 define.prefix = prefix
480 define.bindir = bindir
481 define.datadir = datadir
482 define.mandir = mandir
483 define.EXEEXT = exe_extension
487 -- All done; output the configuration files.
490 -- Print the program options.
491 output = io.open("config.h", "w")
492 for key,value in pairs(config) do
494 if type(value) == "boolean" then
496 output:write("#define "..key)
498 output:write("#undef "..key)
500 elseif type(value) == "string" then
501 output:write(string.format("#define %s %q", key, tostring(value)))
503 output:write(string.format("#define %s %s", key, tostring(value)))
509 -- Print the make definitions.
510 output = io.open("config.mk", "w")
511 for key,value in pairs(define) do
513 value = tostring(value)
514 output:write(string.format("%s = %s\n", key, value))
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))
531 Configuration complete! You can review your configuration in `config.mk'.
533 To finish the installation, type: