Hash :
92b12812
Author :
Date :
2013-11-10T00:38:37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
--
-- This software is provided 'as-is', without any express or implied
-- warranty. In no event will the authors be held liable for any damages
-- arising from the use of this software.
--
-- Permission is granted to anyone to use this software for any purpose,
-- including commercial applications, and to alter it and redistribute it
-- freely.
--
-- Meta-build system using premake created and maintained by
-- Benjamin Henning <b.henning@digipen.edu>
--[[
sdl_check_compile.lua
This file provides various utility functions which allow the meta-build
system to perform more complex dependency checking than premake initially
allows. This is done using the (currently) GCC toolchain to build generated
C files which try to import certain headers, link to certain functions, link
to certain libraries, or a combination of the above. It supports providing a
custom source to try and build, link, and/or run per the implementation's
choice, so the possibilities are nearly endless with that this system is
capable of, though it could always do with more flexibility.
]]
local cxx = "gcc"
local cxx_flags = ""
local cxx_io_flags = "-o premakecheck.o -c premakecheck.c 2> /dev/null"
local cxx_includes = { }
local link = "gcc"
local link_flags = ""
local link_io_flags = "-o premakecheck.out premakecheck.o"
local link_end = " 2> /dev/null"
local run = "./premakecheck.out"
local run_flags = ""
local run_io_flags = " > ./premakecheck.stdout"
local checked_printf = false
local has_printf = false
-- Set the application used to compile the generated files.
function set_cxx(compiler)
cxx = compiler
end
-- Set custom flags for the compiler.
function set_cxx_flags(flags)
cxx_flags = flags
end
-- Include a search directory for libraries.
local function include_library_dir(dir)
link_flags = link_flags .. "-L" .. dir .. " "
end
-- Include a library to be linked durnig the link step.
local function link_library(lib)
link_flags = link_flags .. "-l" .. lib .. " "
end
-- Reset the link flags.
local function reset_link_flags()
link_flags = ""
end
-- Creates the build command line to be executed.
local function build_compile_line()
return cxx .. " " .. cxx_flags .. " " .. cxx_io_flags
end
-- Creates the link command line to be executed.
local function build_link_line()
return link .. " " .. link_io_flags .. " " .. link_flags .. link_end
end
-- Create the run line to be executed.
local function build_run_line()
return run .. " " .. run_flags .. " " .. run_io_flags
end
-- Builds a list of preprocessor include directives for all the include files
-- successfully found so far by these functions, so as to perform automatic
-- feature checking for the clientside code.
local function build_includes()
local includes = ""
for _,v in ipairs(cxx_includes) do
includes = includes .. '#include "' .. v .. '"\n'
end
return includes
end
-- Cleanup the generated build environment.
local function cleanup_build()
os.remove("./premakecheck.c")
os.remove("./premakecheck.o")
os.remove("./premakecheck.out")
os.remove("./premakecheck.stdout")
end
-- Check if a source builds, links, and or/runs, where running depends on
-- linking and linking depends on building. The return from this function is
-- a triple, where the first is a boolean value indicating if it successfully
-- was built, the second is a boolean value indicating if it successfully
-- linked, and the third represents nil if it was not run or run correctly, or
-- the output from the program executed (may be empty for no output).
local function check_build_source(source, link, run)
local file = fileopen("./premakecheck.c", "wt")
file:write(source)
file:close()
local result = os.execute(build_compile_line())
if not link then
cleanup_build()
if result == 0 then
return true, false, nil -- compile, no link, no run
end
return false, false, nil -- no compile, no link, no run
end
-- try linking, too
if result ~= 0 then
-- can't link if it doesn't compile
cleanup_build()
return false, false, nil -- no compile, no link, no run
end
result = os.execute(build_link_line())
if not run or result ~= 0 then -- have to link to run
cleanup_build()
return true, result == 0, nil -- compile, maybe link, no run
end
result = os.execute(build_run_line())
local output = readfile("./premakecheck.stdout", "rt")
cleanup_build()
return true, true, output -- compile, link, ran
end
-- Given C source code, determine whether the source code will compile in the
-- present environment. Returns true if the source was successfully compiled, or
-- false if otherwise.
function check_cxx_source_compiles(source)
local r1, _, __ = check_build_source(source, false, false)
return r1
end
-- Given C source code, determine whether the source code can be built into a
-- working executable. That is, it will check if the code both compiles and
-- links. Returns true if the code was successfully built (compiled and linked),
-- or false if otherwise.
function check_cxx_source_builds(source)
local r1, r2, _ = check_build_source(source, true, false)
return r1 and r2
end
-- Given C source code, attempt to compile, link, and execute the source code.
-- This function will return two values. The first is a boolean indicating
-- whether the source code was successfully run (meaning it was compiled, built,
-- and ran successfully), and the second value returned is the actual output
-- from running the application, or nil if it did not run correctly or was not
-- built. The output may be an empty string if the code does not print anything
-- to stdout.
function check_cxx_source_runs(source)
local r1, r2, r3 = check_build_source(source, true, true)
return r1 and r2 and (r3 ~= nil), r3
end
-- Given a header file, check whether the header file is visible to the compiler
-- in the given environment. Returns a boolean indicating thus. If a header file
-- is found in either of these functions, it will be added to a list of headers
-- that can be used in subsequent dependency checks.
function check_include_file(inc)
return check_include_files(inc)
end
-- Given a variable list of header files, check whether all of the includes are
-- visible in the given environment. Every file must be included in order for
-- this function to return true.
function check_include_files(...)
local source = ""
for _, v in ipairs{...} do
source = source .. '#include "' .. v .. '"\n'
end
local result = check_cxx_source_compiles(source)
if result then
for _, v in ipairs{...} do
table.insert(cxx_includes, v)
end
end
return result
end
-- Given a directory, determine whether the directory contains any header files.
-- Unfortunately it does assume the extension is .h, but this can be altered in
-- future versions of this software. The function returns true if the directory
-- (or any of its subdirectories) contain .h files, or false if otherwise (such
-- as if the directory does not exist).
function check_include_directory(incDir)
incDir = incDir:gsub("\\", "/"):gsub("//", "/")
if incDir:sub(#incDir, #incDir) ~= "/" then
incDir = incDir .. "/"
end
return #os.matchfiles(incDir .. "**.h") > 0
end
-- Given a variable list of directories, iteratively check if each one contains
-- header files, per the functionality of check_include_directory. This function
-- returns true if and only if every listed directory or its subdirectories
-- contain .h files.
function check_include_directories(...)
for _, v in ipairs{...} do
if not check_include_directory(v) then
return false
end
end
return true
end
-- Given a function name, attempt to determine whether the function can be found
-- within all of the known include files. Known include files are derived from
-- the check_include_file(s) functions.
function check_function_exists(func)
local source = build_includes()
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tvoid *check = (void *) ' .. func .. ';\n'
source = source .. '\treturn 0;\n'
return check_cxx_source_builds(source .. '}')
end
-- Given a library, a function that must exist within the library, and an
-- include file prototyping the function, this function determines whether those
-- three variables are able to build a working executable. That is, if a
-- function can be properly linked to using a given library, then the library
-- can be assumed to exist. Returns true if and only if the function was
-- correctly linked to.
function check_library_exists(lib, func, inc)
local source = build_includes()
if inc ~= nil then
source = source .. '#include "' .. inc .. '"\n'
end
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tvoid *check = (void *) ' .. func .. ';\n'
source = source .. '\treturn 0;\n'
if lib ~= nil then
link_library(lib)
end
local result = check_cxx_source_builds(source .. '}')
reset_link_flags()
return result
end
-- This is a merge variable list version of the check_library_exists function.
-- The thing to note with this function is that it will return true for the
-- first library found to correctly link to the function. This function is used
-- to determine whether the function is found in a list of libraries, not if it
-- is found in every one of the libraries.
function check_library_exists_multiple(func, inc, ...)
for _,v in ipairs{...} do
if check_library_exists(v, func, inc) then
return true
end
end
return false
end
-- This is a wrapper for the check_library_exists function that will also
-- attempt to locate the library in question, in case it's not in a path the
-- compiler is already aware of. This function has the same return consequences
-- as check_library_exists.
function check_library_exists_lookup(lib, func, inc)
local dir = os.findlib(lib)
if dir == nil then
return false
end
include_library_dir(dir)
return check_library_exists(lib, func, inc)
end
-- Given a valid C type name, this function generates a program that will print
-- the size of the type using the sizeof operator to the console, then parse the
-- size to indicate the byte size of the type on this platform. The resulting
-- executable is dependent on stdio and the printf function, which it safely
-- checks for behind the scenes. If these dependencies are not found for
-- whatever reason, this function returns 0, otherwise it returns a proper
-- numerical value representing the size of the specified type.
function check_type_size(typename)
if not checked_printf then
checked_printf = true
has_printf = check_include_file("stdio.h") and check_function_exists("printf")
if not has_printf then
print("Warning: cannot check the size of a type without stdio and printf.")
end
end
if not has_printf then
return 0
end
local source = '#include "stdio.h"\n'
source = source .. 'int main(int argc, char **argv) {\n'
source = source .. '\tprintf("%d", sizeof(' .. typename .. '));\n'
source = source .. '\treturn 0;\n'
local success, result = check_cxx_source_runs(source .. '}');
if not success then
print("Warning: could not get the size of type: " .. typename)
return 0
end
return tonumber(result)
end