--- /dev/null
+--
+-- Yoink Build System
+-- A collection of Lua functions for performing checks.
+--
+-- Copyright (c) 2011, Charles McGarvey
+-- Distributable under the terms and conditions of the 2-clause BSD
+-- license; see the file COPYING for a complete text of the license.
+--
+
+local M = {}
+
+
+local util = require "utility"
+local print_check = util.printer("checking for %s... ")
+
+-- TODO: Should have check for toolset triplet, compiling arbitrary code,
+-- headers, libraries.
+
+
+local CC = ""
+local CXX = ""
+
+
+local compile
+do
+ local cache = {}
+ compile = function(compiler, ext, flags, code)
+ if type(compiler) ~= "table" then compiler = {compiler} end
+ local key = util.concat(compiler, "&") .. ext .. code
+ if cache[key] then return cache[key], "cached value" end
+ local tmpdir = util.make_tempdir("config.XXXXXX")
+ if tmpdir
+ then
+ local fd, err = io.open(tmpdir .. "/test." .. ext, "w")
+ if not fd then die("failed to create temporary file", fd) end
+ fd:write(code)
+ fd:close()
+ --util.print_log("code to be compiled:\n" .. code)
+ compiler = util.find_command(compiler, "test." .. ext, tmpdir)
+ os.execute("rm -rf " .. tmpdir)
+ else
+ die("failed to create temporary directory", tmpdir)
+ end
+ cache[key] = compiler
+ if compiler == nil then return nil, "test program could not be compiled" end
+ return compiler
+ end
+end
+
+
+do
+ local cache = {}
+ local function cc(...)
+ local arg = util.copy(arg, "v", tostring)
+ local key = util.concat(arg)
+ if cache[key] then return cache[key], "cached value" end
+ --print_check("C compiler")
+ local cc, err = compile(arg, "c", "", "int main() { return 0; }")
+ --if cc then util.print(cc) else util.print("no") end
+ cache[key] = cc
+ --util.print_info("cccheck:", key, cc)
+ if cc == nil then return nil, err end
+ return cc
+ end
+ M.cc = cc
+
+ local function native_cc(triplet)
+ local ccenv = os.getenv("CC")
+ if ccenv
+ then
+ local result = cc(ccenv)
+ if result then return result end
+ end
+ if type(triplet) ~= "string" then triplet = util.exec("config.guess") end
+ return cc(triplet .. "-gcc", "cc", "gcc")
+ end
+ M.native_cc = native_cc
+
+ local function cross_cc(triplet)
+ return cc(triplet .. "-gcc")
+ end
+ M.cross_cc = cross_cc
+end
+
+do
+ local cache = {}
+ local function cxx(...)
+ local arg = util.copy(arg, "v", tostring)
+ local key = util.concat(arg)
+ if cache[key] then return cache[key], "cached value" end
+ --print_check("C++ compiler")
+ local cxx, err = compile(arg, "c++", "", "int main() { return 0; }")
+ --if cxx then util.print(cxx) else util.print("no") end
+ cache[key] = cxx
+ CXX = cxx
+ --util.print_info("cxxcheck:", key, cxx)
+ if cxx == nil then return nil, err end
+ return cxx
+ end
+ M.cxx = cxx
+
+ local function native_cxx(triplet)
+ if type(triplet) ~= "string" then triplet = util.exec("config.guess") end
+ return cxx(os.getenv("CXX"), "g++", "c++")
+ end
+ M.native_cxx = native_cxx
+
+ local function cross_cxx(triplet)
+ return cxx(triplet .. "-g++")
+ end
+ M.cross_cxx = cross_cxx
+end
+
+do
+ local cache = {}
+ local function lib(...)
+ --local key = util.concat(arg, ", ")
+ --if cache[key] then return cache[key] end
+ --print_check(key)
+ --local code = ""
+ --local result = false
+ --for _,header in ipairs(arg)
+ --do
+ --code = string.format("%s#include <%s>\n", code, header)
+ --end
+ --code = code .. "int main() { return 0; }\n"
+ --if compile({CC}, "c", "", code) or compile({CC}, "c++", "", code)
+ --then
+ --result = true
+ --util.print("yes")
+ --else
+ --util.print("no")
+ --end
+ --cache[key] = result
+ --return key
+ return true
+ end
+ M.lib = lib
+end
+
+do
+ local cache = {}
+ local function header(...)
+ local CC = M.native_cc()
+ local key = util.concat(arg, ", ")
+ if cache[key] then return cache[key] end
+ --print_check(key)
+ local code = ""
+ local result = false
+ for _,header in util.npairs(arg)
+ do
+ code = string.format("%s#include <%s>\n", code, header)
+ end
+ code = code .. "int main() { return 0; }\n"
+ if compile({CC}, "c", "", code) or compile({CC}, "c++", "", code)
+ then
+ result = true
+ --util.print("yes")
+ else
+ --util.print("no")
+ end
+ cache[key] = result
+ return key
+ end
+ M.header = header
+end
+
+
+return M
+