From 211a59d837e0260456931f4b6a4d030afa07ea0c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 14 Sep 2013 10:17:09 +0200
Subject: Use system Lua if present

Change-Id: I665a603b1afa1e24e1b73618e976113a87be2001

diff --git a/BuildTools/SCons/SConscript.boot b/BuildTools/SCons/SConscript.boot
index 9c78bc0..b0e14ee 100644
--- a/BuildTools/SCons/SConscript.boot
+++ b/BuildTools/SCons/SConscript.boot
@@ -60,6 +60,10 @@ vars.Add(PathVariable("sqlite_includedir", "SQLite headers location", None, Path
 vars.Add(PathVariable("sqlite_libdir", "SQLite library location", None, PathVariable.PathAccept))
 vars.Add("sqlite_libname", "SQLite library name", "libsqlite3" if os.name == "nt" else "sqlite3")
 vars.Add("sqlite_force_bundled", "Force use of the bundled SQLite", None)
+vars.Add(PathVariable("lua_includedir", "Lua headers location", None, PathVariable.PathAccept))
+vars.Add(PathVariable("lua_libdir", "Lua library location", None, PathVariable.PathAccept))
+vars.Add("lua_libname", "Lua library name", "liblua" if os.name == "nt" else "lua")
+vars.Add("lua_force_bundled", "Force use of the bundled Lua", None)
 vars.Add(PathVariable("avahi_includedir", "Avahi headers location", None, PathVariable.PathAccept))
 vars.Add(PathVariable("avahi_libdir", "Avahi library location", None, PathVariable.PathAccept))
 vars.Add(PathVariable("qt", "Qt location", "", PathVariable.PathAccept))
diff --git a/BuildTools/SCons/SConstruct b/BuildTools/SCons/SConstruct
index f3c4e5e..ae58bd9 100644
--- a/BuildTools/SCons/SConstruct
+++ b/BuildTools/SCons/SConstruct
@@ -83,6 +83,12 @@ def CheckPKG(context, name):
 
 def CheckVersion(context, library, version, define, header, value) :
 	context.Message("Checking " + library + " version (>= " + version + ") ...")
+	version = GetVersion(context, define, header)
+	ok = version >= value
+	context.Result(ok)
+	return ok
+
+def GetVersion(context, define, header, extension = ".c") :
 	ret = context.TryRun("""
 #include <%(header)s>
 #include <stdio.h>
@@ -91,10 +97,12 @@ int main(int argc, char* argv[]) {
 	printf("%%d\\n", %(define)s);
 	return 0;
 }
-""" % { "header" : header, "define": define }, ".c")
-	ok = ret[0] and int(ret[1]) >= value
-	context.Result(ok)
-	return ok
+""" % { "header" : header, "define": define }, extension)
+	if ret[0] :
+	  return int(ret[1])
+	else :
+	  return -1
+
 
 conf = Configure(conf_env)
 
@@ -405,7 +413,26 @@ else :
 
 
 # Lua
-env["LUA_BUNDLED"] = 1
+lua_conf_env = conf_env.Clone()
+lua_flags = {}
+if env.get("lua_libdir", None) :
+	lua_flags["LIBPATH"] = [env["lua_libdir"]]
+if env.get("lua_includedir", None) :
+	lua_flags["CPPPATH"] = [env["lua_includedir"]]
+lua_conf_env.MergeFlags(lua_flags)
+conf = Configure(lua_conf_env)
+if not env.get("lua_force_bundled", False) and conf.CheckCXXHeader("lua.hpp") and conf.CheckLib(env["lua_libname"]) :
+	env["HAVE_LUA"] = 1
+	env["LUA_FLAGS"] = { "LIBS": [env["lua_libname"]] }
+	lua_version = GetVersion(conf, "LUA_VERSION_NUM", "lua.h")
+	if lua_version > 0 :
+		env["LUA_FLAGS"]["LUA_VERSION"] = str(lua_version // 100) + "." + str(lua_version % 100)
+	else :
+		print "Warning: Unable to determine Lua version. Not installing Lua libraries."
+	env["LUA_FLAGS"].update(lua_flags)
+else :
+	env["LUA_BUNDLED"] = 1
+conf.Finish()
 
 # Readline
 conf = Configure(conf_env)
-- 
cgit v0.10.2-6-g49f6