]> Dogcows Code - chaz/yoink/blob - build/check.lua
import stlplus 3.7
[chaz/yoink] / build / check.lua
1 --
2 -- Yoink Build System
3 -- A collection of Lua functions for performing checks.
4 --
5 -- Copyright (c) 2011, Charles McGarvey
6 -- Distributable under the terms and conditions of the 2-clause BSD
7 -- license; see the file COPYING for a complete text of the license.
8 --
9
10 local M = {}
11
12
13 local util = require "utility"
14 local print_check = util.printer("checking for %s... ")
15
16 -- TODO: Should have check for toolset triplet, compiling arbitrary code,
17 -- headers, libraries.
18
19
20 local CC = ""
21 local CXX = ""
22
23
24 local compile
25 do
26 local cache = {}
27 compile = function(compiler, ext, flags, code)
28 if type(compiler) ~= "table" then compiler = {compiler} end
29 local key = util.concat(compiler, "&") .. ext .. code
30 if cache[key] then return cache[key], "cached value" end
31 local tmpdir = util.make_tempdir("config.XXXXXX")
32 if tmpdir
33 then
34 local fd, err = io.open(tmpdir .. "/test." .. ext, "w")
35 if not fd then die("failed to create temporary file", fd) end
36 fd:write(code)
37 fd:close()
38 --util.print_log("code to be compiled:\n" .. code)
39 compiler = util.find_command(compiler, "test." .. ext, tmpdir)
40 os.execute("rm -rf " .. tmpdir)
41 else
42 die("failed to create temporary directory", tmpdir)
43 end
44 cache[key] = compiler
45 if compiler == nil then return nil, "test program could not be compiled" end
46 return compiler
47 end
48 end
49
50
51 do
52 local cache = {}
53 local function cc(...)
54 local arg = util.copy(arg, "v", tostring)
55 local key = util.concat(arg)
56 if cache[key] then return cache[key], "cached value" end
57 --print_check("C compiler")
58 local cc, err = compile(arg, "c", "", "int main() { return 0; }")
59 --if cc then util.print(cc) else util.print("no") end
60 cache[key] = cc
61 --util.print_info("cccheck:", key, cc)
62 if cc == nil then return nil, err end
63 return cc
64 end
65 M.cc = cc
66
67 local function native_cc(triplet)
68 local ccenv = os.getenv("CC")
69 if ccenv
70 then
71 local result = cc(ccenv)
72 if result then return result end
73 end
74 if type(triplet) ~= "string" then triplet = util.exec("config.guess") end
75 return cc(triplet .. "-gcc", "cc", "gcc")
76 end
77 M.native_cc = native_cc
78
79 local function cross_cc(triplet)
80 return cc(triplet .. "-gcc")
81 end
82 M.cross_cc = cross_cc
83 end
84
85 do
86 local cache = {}
87 local function cxx(...)
88 local arg = util.copy(arg, "v", tostring)
89 local key = util.concat(arg)
90 if cache[key] then return cache[key], "cached value" end
91 --print_check("C++ compiler")
92 local cxx, err = compile(arg, "c++", "", "int main() { return 0; }")
93 --if cxx then util.print(cxx) else util.print("no") end
94 cache[key] = cxx
95 CXX = cxx
96 --util.print_info("cxxcheck:", key, cxx)
97 if cxx == nil then return nil, err end
98 return cxx
99 end
100 M.cxx = cxx
101
102 local function native_cxx(triplet)
103 if type(triplet) ~= "string" then triplet = util.exec("config.guess") end
104 return cxx(os.getenv("CXX"), "g++", "c++")
105 end
106 M.native_cxx = native_cxx
107
108 local function cross_cxx(triplet)
109 return cxx(triplet .. "-g++")
110 end
111 M.cross_cxx = cross_cxx
112 end
113
114 do
115 local cache = {}
116 local function lib(...)
117 --local key = util.concat(arg, ", ")
118 --if cache[key] then return cache[key] end
119 --print_check(key)
120 --local code = ""
121 --local result = false
122 --for _,header in ipairs(arg)
123 --do
124 --code = string.format("%s#include <%s>\n", code, header)
125 --end
126 --code = code .. "int main() { return 0; }\n"
127 --if compile({CC}, "c", "", code) or compile({CC}, "c++", "", code)
128 --then
129 --result = true
130 --util.print("yes")
131 --else
132 --util.print("no")
133 --end
134 --cache[key] = result
135 --return key
136 return true
137 end
138 M.lib = lib
139 end
140
141 do
142 local cache = {}
143 local function header(...)
144 local CC = M.native_cc()
145 local key = util.concat(arg, ", ")
146 if cache[key] then return cache[key] end
147 --print_check(key)
148 local code = ""
149 local result = false
150 for _,header in util.npairs(arg)
151 do
152 code = string.format("%s#include <%s>\n", code, header)
153 end
154 code = code .. "int main() { return 0; }\n"
155 if compile({CC}, "c", "", code) or compile({CC}, "c++", "", code)
156 then
157 result = true
158 --util.print("yes")
159 else
160 --util.print("no")
161 end
162 cache[key] = result
163 return key
164 end
165 M.header = header
166 end
167
168
169 return M
170
This page took 0.039381 seconds and 4 git commands to generate.