sdl_check_compile.lua 11 KB

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