5 -- Execute this file to configure the build system.
8 -- Define project, version, tarname, website, and contact.
11 tarname = project:lower():gsub("%s+", "-")
12 website = "http://www.dogcows.com/yoink"
13 contact = "onefriedrice@brokenzipper.com"
16 -- This script will create three different config files from three tables
17 -- with values determined by the host and configuration options:
19 -- The table `config' contains key-value pairs to be written to the file
20 -- `config.h' which is included by sources files. Boolean values will
21 -- either #define or #undef the key with no associated values. String
22 -- values will be defined as quoted strings while any other value,
23 -- particularly numbers, will be defined unquoted.
25 -- The table `define' contains key-value pairs to be written to the file
26 -- `config.mk' which is included by the Makefile. Values are assigned to
27 -- their keys as they are, unquoted.
29 -- The table `export' contains key-value pairs to be written to the file
30 -- `config.sed' which is used by sed to perform search-and-replace on files
31 -- containing @REPLACE_ME@-style keywords. When used with sed, such
32 -- keyworded keys will be replaced by their values as they are, unquoted.
38 This script prepares ]]..project..[[ for building on your system.
39 Usage: ./configure [OPTION]... [VAR=VALUE]...
41 This is NOT an autoconf-generated configure script, though it was written
42 to be familiar by supporting many of the same options.
45 -h, --help display this help and exit
46 --host=HOST cross-compile the program to run on HOST
48 --prefix=DIR base directory to install programs to
49 --bindir=DIR directory to install executables
50 --datadir=DIR directory to install shared data files
51 --mandir=DIR directory to install manual pages
53 --disable-dependency-tracking speed up one-time builds
54 --enable-link-sh decrease the number of direct dependencies
57 --enable-debug include debugging symbols and code paths
58 --enable-double-precision use doubles instead of floats
59 --enable-profile compile in gprof profiling instructions
60 --enable-clock_gettime use clock_gettime() for timing
61 --enable-threads use threads for concurrency
62 --enable-hotloading watch assets for automatic reloading
64 --with-gtk use the gtk2 toolkit (overrides --with-qt4)
65 --with-qt4 use the qt4 gui toolkit
71 -- Setup a temporary file to collect error messages.
74 error_log = "config.log"
79 -- Define some useful functions.
82 -- Return true if a file exists, false otherwise.
83 function file_exists(file)
84 return os.execute("test -f "..file) == 0
87 -- Print an error message and exit with an error.
89 for _,value in ipairs(arg) do
90 print("fatal: "..tostring(value))
92 if file_exists(error_log) then
94 print("Look through the file `config.log' for more information:\n")
95 os.execute("tail "..error_log)
100 -- Execute a command and return its output or nil if the command failed to
102 function backtick_run(command)
103 local fd = io.popen(command.." 2>>"..error_log)
105 local stdout = fd:read("*l")
112 -- Try to execute a command and return true if the command finished
113 -- successfully (with an exit code of zero).
114 function try_run(command)
115 return os.execute(command.." >/dev/null 2>>"..error_log) == 0
118 -- Remove the whitespace surrounding a string.
120 str = str:gsub("^%s+", "")
121 return str:gsub("%s+$", "")
124 -- Trim the string and convert all sequences of whitespace to a single
126 function reduce_whitespace(str)
127 str = str:gsub("%s+", " ")
131 -- Get the CFLAGS from pkg-config for the passed libraries.
132 function pkg_config_cflags(libs)
133 local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
134 local cmd = env.." pkg-config"
135 return backtick_run(cmd.." --cflags "..libs)
138 -- Get the LDFLAGS from pkg-config for the passed libraries.
139 function pkg_config_ldflags(libs)
140 local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
141 local cmd = env.." pkg-config"
142 return (" "..backtick_run(cmd.." --libs "..libs)):gsub("%s%-l%S*", "")
145 -- Get the LIBS flags from pkg-config for the passed libraries.
146 function pkg_config_libs(libs)
147 local env = "PKG_CONFIG_PATH="..libdir.."/pkgconfig:$PKG_CONFIG_PATH"
148 local cmd = env.." pkg-config"
149 return backtick_run(cmd.." --libs-only-l "..libs)
152 -- Add a flag to the CFLAGS and CXXFLAGS variables.
153 function add_cflag(flag)
157 CFLAGS = string.format("%s %s", CFLAGS, flag)
159 if CXXFLAGS == "" then
162 CXXFLAGS = string.format("%s %s", CXXFLAGS, flag)
166 -- Replace a flag in the CFLAGS and CXXFLAGS variables.
167 function set_cflag(flag)
168 local cflag_set, cxxflag_set = false, false
169 CFLAGS = CFLAGS:gsub("%"..flag:sub(1, 2).."%S+", function()
173 CXXFLAGS = CXXFLAGS:gsub("%"..flag:sub(1, 2).."%S+", function()
177 if not cflag_set or not cxxflag_set then add_cflag(flag) end
180 -- Look for a command from a list that can complete successfully (with exit
181 -- code zero) given some arguments. Returns nil if none were successful.
182 function find_command(commands, args)
183 if not args then args = "" end
184 for _,command in ipairs(commands) do
185 if try_run(command.." "..args) then return command end
192 -- Perform a quick sanity check.
195 if not file_exists("configure") or not file_exists("Makefile") then
196 -- This script doesn't support out-of-tree builds.
197 die("You must `cd' to the project root where the Makefile is.")
202 -- Parse the command-line options.
205 -- This script supports many of the options provided by autoconf-generated
206 -- scripts. In particular, all the directory-related options are
207 -- supported, including --prefix, --exec-prefix, and all the --dirDIR
208 -- options for configuring installation paths. If passed, the option
209 -- parsing routine below will place these options in the global namespace
210 -- (i.e. prefix, eprefix, bindir, datadir, etc).
212 -- Feature and package options are also supported. The value of any option
213 -- of the form --enable-OPT= and --with-OPT= can be obtained with the
214 -- functions get_feature and get_package, respectively. Like
215 -- autoconf-generated scripts, passing --disable-OPT or --without-OPT is
216 -- equivalent to passing --enable-OPT=no and --without-OPT=no,
217 -- respectively. Values that are either yes or no are interpreted as
218 -- booleans. Any other values are interpreted as strings.
220 -- Definitions of the form KEY=VALUE are also supported. If passed, the
221 -- option parsing routine below will place these options in the global
224 -- Finally, the options -h, --help, and --host are also supported, the
225 -- former two calling the show_help function and the latter setting the
226 -- host global variable.
234 -- Define the option-parsing rules and handlers.
236 handlers["--help"] = function()
240 handlers["-h"] = handlers["--help"]
242 handlers["--host=(.+)"] = function(triplet)
247 local add_feature = function(feature, value)
248 if value == "yes" or value == "" then value = true end
249 if value == "no" then value = false end
250 features[feature] = value
252 handlers["--enable%-([%w_-]+)=?(.*)"] = add_feature
253 handlers["--disable%-([%w_-]+)"] = function(feature)
254 add_feature(feature, "no")
257 local add_package = function(package, value)
258 if value == "yes" or value == "" then value = true end
259 if value == "no" then value = false end
260 packages[package] = value
262 handlers["--with%-([%w_-]+)=?(.*)"] = add_package
263 handlers["--without%-([%w_-]+)"] = function(package)
264 add_package(package, "no")
267 handlers["--(%l+)dir=(.+)"] = function(dir, path)
268 _G[dir.."dir"] = path
270 handlers["--prefix=(.+)"] = function(path)
273 handlers["--exec-prefix=(.+)"] = function(path)
277 handlers["([%w_]+)=(.*)"] = function(key, value)
282 -- Define the feature and package accessors.
284 function get_feature(feature) return features[feature] end
285 function get_package(package) return packages[package] end
288 -- Now read and parse the command-line options.
290 local function parse_arg(arg)
291 for key,value in pairs(handlers) do
292 local matches = {arg:match(key)}
293 if matches[1] then value(unpack(matches)) return end
295 print("warning: unknown or incomplete argument "..arg)
298 for _,arg in ipairs(arg) do
303 -- Define default values for significant variables.
305 local function define(symbol, value)
306 if not _G[symbol] then _G[symbol] = value end
313 define("WINDRES", "")
315 define("CFLAGS", "-g -O2")
316 define("CXXFLAGS", CFLAGS)
317 define("LDFLAGS", "")
320 define("host", backtick_run("tools/config.guess"))
321 define("alt_host", backtick_run("tools/config.sub "..host))
323 define("prefix", "/usr/local")
324 define("eprefix", prefix)
325 define("bindir", eprefix.."/bin")
326 define("sbindir", eprefix.."/sbin")
327 define("libexecdir", eprefix.."/libexec")
328 define("sysconfdir", prefix.."/etc")
329 define("localstatedir", prefix.."/var")
330 define("libdir", eprefix.."/lib")
331 define("includedir", prefix.."/include")
332 define("datarootdir", prefix.."/share")
333 define("datadir", datarootdir.."/"..tarname)
334 define("infodir", datarootdir.."/info")
335 define("localedir", datarootdir.."/locale")
336 define("mandir", datarootdir.."/man")
337 define("docdir", datarootdir.."/doc/"..tarname)
339 if not features["dependency-tracking"] then
340 features["dependency-tracking"] = true
350 -- Determine special target platforms.
353 -- Win32 has some special cases related to its resource file, src/yoinkrc.
354 if host:match("mingw32") then platform = "win32" end
358 -- Look for a working toolchain.
361 print("Please wait...")
364 tmpname = os.tmpname()..".c"
365 tmpfile, err = io.open(tmpname, "w")
371 printf("Hello world!\n");
377 function extra() if not cross_compile then return "gcc", "cc" end end
380 host.."-gcc", host.."-cc",
381 alt_host.."-gcc", alt_host.."-cc",
382 extra()}, tmpname.." -o "..tmpname..".tmp")
384 os.remove(tmpname..".tmp")
385 if not CC then die("Can't find a working C compiler.") end
387 die("failed to create temporary file: "..err)
391 tmpname = os.tmpname()..".c"
392 tmpfile, err = io.open(tmpname, "w")
398 std::cout << "Hello world!" << std::endl;
404 function extra() if not cross_compile then return "g++", "c++" end end
407 host.."-g++", host.."-c++",
408 alt_host.."-g++", alt_host.."-c++",
409 extra()}, tmpname.." -o "..tmpname..".tmp")
411 os.remove(tmpname..".tmp")
412 if not CXX then die("Can't find a working C++ compiler.") end
414 die("failed to create temporary file: "..err)
419 function extra() if not cross_compile then return "ar" end end
424 extra()}, "--version")
425 if not AR then die("Can't find a working archiver.") end
430 function extra() if not cross_compile then return "ranlib" end end
431 RANLIB = find_command({
435 extra()}, "--version")
436 if not RANLIB then die("Can't find a working library indexer.") end
439 -- Check for WINDRES.
440 if platform == "win32" then
441 function extra() if not cross_compile then return "windres" end end
442 WINDRES = find_command({
445 alt_host.."-windres",
446 extra()}, "--version")
447 if not WINDRES then die("Can't find a working resource compiler.") end
452 -- Configure the features and packages.
455 if get_feature("debug") then
457 add_cflag("-Wall -Wno-uninitialized")
463 if get_feature("extra-warnings") then
464 add_cflag("-Wextra -Wno-unused-parameter")
467 config.USE_CLOCK_GETTIME = get_feature("clock_gettime")
468 config.USE_DOUBLE_PRECISION = get_feature("double-precision")
469 config.USE_HOTLOADING = get_feature("hotloading")
470 config.USE_THREADS = get_feature("threads")
471 config.PROFILING_ENABLED = get_feature("profile") and add_cflag("-pg")
473 if get_feature("link-sh") then
477 if get_package("gtk") then
481 if get_package("qt4") then
487 -- Check for the libraries we need.
491 local dependencies = "sdl gl glu libpng openal vorbisfile lua"
493 if get_package("gtk") then
494 dependencies = dependencies.." gtk+-2.0"
495 elseif get_package("qt4") then
496 dependencies = dependencies.." QtGui"
499 add_cflag(pkg_config_cflags(dependencies))
500 LDFLAGS = LDFLAGS .." "..pkg_config_ldflags(dependencies)
501 LIBS = LIBS .." "..pkg_config_libs(dependencies)
503 if platform == "win32" then
504 LIBS = LIBS.." -lws2_32"
505 exe_extension = ".exe"
513 -- Define the exports and definitions.
516 if platform == "win32" then
517 -- These are used in src/yoink.rc.
518 local vmajor, vminor, vrevis = version:match("^(%d*)%.?(%d*)%.?(%d*)")
519 config.VERSION_MAJOR = tonumber(vmajor) or 0
520 config.VERSION_MINOR = tonumber(vminor) or 0
521 config.VERSION_REVISION = tonumber(vrevis) or 0
524 config.PACKAGE = tarname
525 config.PACKAGE_NAME = project
526 config.PACKAGE_VERSION = version
527 config.PACKAGE_STRING = project.." "..version
528 config.PACKAGE_TARNAME = tarname
529 config.PACKAGE_URL = website
530 config.PACKAGE_BUGREPORT = contact
531 config.YOINK_GITHEAD = backtick_run("git describe master")
532 config.YOINK_DATADIR = datadir
534 define.PACKAGE = project
535 define.TARNAME = tarname.."-"..version
537 define.PLATFORM = platform
541 define.RANLIB = RANLIB
542 define.WINDRES = WINDRES
543 define.CFLAGS = reduce_whitespace(CFLAGS)
544 define.CXXFLAGS = reduce_whitespace(CXXFLAGS)
545 define.LDFLAGS = reduce_whitespace(LDFLAGS)
546 define.LIBS = reduce_whitespace(LIBS)
547 define.prefix = prefix
548 define.bindir = bindir
549 define.datadir = datadir
550 define.mandir = mandir
551 define.EXEEXT = exe_extension
552 define.DEP_TRACKING = get_feature("dependency-tracking")
554 export.datadir = datadir -- Used in doc/yoink.6.in.
555 export.PACKAGE_BUGREPORT = contact -- Used in doc/yoink.6.in.
559 -- All done; output the configuration files.
562 -- Output the program options.
563 output = io.open("config.h", "w")
564 for key,value in pairs(config) do
566 if type(value) == "boolean" then
568 output:write("#define "..key.." 1")
570 output:write("#undef "..key)
572 elseif type(value) == "string" then
573 output:write(string.format("#define %s %q", key, tostring(value)))
575 output:write(string.format("#define %s %s", key, tostring(value)))
581 -- Output the make definitions.
582 output = io.open("config.mk", "w")
583 for key,value in pairs(define) do
585 value = tostring(value)
586 output:write(string.format("%s = %s\n", key, value))
591 -- Output the exported variables.
592 output = io.open("config.sed", "w")
593 for key,value in pairs(export) do
594 key = key:gsub("/", "\\/")
595 value = value:gsub("/", "\\/")
596 output:write(string.format("s/@%s@/%s/g\n", key, value))
603 Configuration complete! You can review your configuration in `config.mk'.
605 To finish the installation, type: