diff options
Diffstat (limited to 'test/unit/preprocess.lua')
-rw-r--r-- | test/unit/preprocess.lua | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/test/unit/preprocess.lua b/test/unit/preprocess.lua index e356695c14..6294114e1e 100644 --- a/test/unit/preprocess.lua +++ b/test/unit/preprocess.lua @@ -1,7 +1,7 @@ -- helps managing loading different headers into the LuaJIT ffi. Untested on -- windows, will probably need quite a bit of adjustment to run there. -local ffi = require("ffi") +local ffi = require('ffi') local global_helpers = require('test.helpers') local argss_to_cmd = global_helpers.argss_to_cmd @@ -12,37 +12,37 @@ local repeated_read_cmd = global_helpers.repeated_read_cmd --- @type Compiler[] local ccs = {} -local env_cc = os.getenv("CC") +local env_cc = os.getenv('CC') if env_cc then - table.insert(ccs, {path = {"/usr/bin/env", env_cc}, type = "gcc"}) + table.insert(ccs, { path = { '/usr/bin/env', env_cc }, type = 'gcc' }) end -if ffi.os == "Windows" then - table.insert(ccs, {path = {"cl"}, type = "msvc"}) +if ffi.os == 'Windows' then + table.insert(ccs, { path = { 'cl' }, type = 'msvc' }) end -table.insert(ccs, {path = {"/usr/bin/env", "cc"}, type = "gcc"}) -table.insert(ccs, {path = {"/usr/bin/env", "gcc"}, type = "gcc"}) -table.insert(ccs, {path = {"/usr/bin/env", "gcc-4.9"}, type = "gcc"}) -table.insert(ccs, {path = {"/usr/bin/env", "gcc-4.8"}, type = "gcc"}) -table.insert(ccs, {path = {"/usr/bin/env", "gcc-4.7"}, type = "gcc"}) -table.insert(ccs, {path = {"/usr/bin/env", "clang"}, type = "clang"}) -table.insert(ccs, {path = {"/usr/bin/env", "icc"}, type = "gcc"}) +table.insert(ccs, { path = { '/usr/bin/env', 'cc' }, type = 'gcc' }) +table.insert(ccs, { path = { '/usr/bin/env', 'gcc' }, type = 'gcc' }) +table.insert(ccs, { path = { '/usr/bin/env', 'gcc-4.9' }, type = 'gcc' }) +table.insert(ccs, { path = { '/usr/bin/env', 'gcc-4.8' }, type = 'gcc' }) +table.insert(ccs, { path = { '/usr/bin/env', 'gcc-4.7' }, type = 'gcc' }) +table.insert(ccs, { path = { '/usr/bin/env', 'clang' }, type = 'clang' }) +table.insert(ccs, { path = { '/usr/bin/env', 'icc' }, type = 'gcc' }) -- parse Makefile format dependencies into a Lua table --- @param deps string --- @return string[] local function parse_make_deps(deps) -- remove line breaks and line concatenators - deps = deps:gsub("\n", ""):gsub("\\", "") + deps = deps:gsub('\n', ''):gsub('\\', '') -- remove the Makefile "target:" element - deps = deps:gsub(".+:", "") + deps = deps:gsub('.+:', '') -- remove redundant spaces - deps = deps:gsub(" +", " ") + deps = deps:gsub(' +', ' ') -- split according to token (space in this case) local headers = {} --- @type string[] - for token in deps:gmatch("[^%s]+") do + for token in deps:gmatch('[^%s]+') do -- headers[token] = true headers[#headers + 1] = token end @@ -50,9 +50,9 @@ local function parse_make_deps(deps) -- resolve path redirections (..) to normalize all paths for i, v in ipairs(headers) do -- double dots (..) - headers[i] = v:gsub("/[^/%s]+/%.%.", "") + headers[i] = v:gsub('/[^/%s]+/%.%.', '') -- single dot (.) - headers[i] = v:gsub("%./", "") + headers[i] = v:gsub('%./', '') end return headers @@ -80,7 +80,7 @@ local function headerize(headers, global) formatted[#formatted + 1] = string.format(fmt, hdr) end - return table.concat(formatted, "\n") + return table.concat(formatted, '\n') end --- @class Gcc @@ -90,8 +90,8 @@ end --- @field get_declarations_extra_flags string[] local Gcc = { preprocessor_extra_flags = {}, - get_defines_extra_flags = {'-std=c99', '-dM', '-E'}, - get_declarations_extra_flags = {'-std=c99', '-P', '-E'}, + get_defines_extra_flags = { '-std=c99', '-dM', '-E' }, + get_declarations_extra_flags = { '-std=c99', '-P', '-E' }, } --- @param name string @@ -115,13 +115,13 @@ end function Gcc:init_defines() -- preprocessor flags that will hopefully make the compiler produce C -- declarations that the LuaJIT ffi understands. - self:define('aligned', {'ARGS'}, '') - self:define('__attribute__', {'ARGS'}, '') - self:define('__asm', {'ARGS'}, '') - self:define('__asm__', {'ARGS'}, '') + self:define('aligned', { 'ARGS' }, '') + self:define('__attribute__', { 'ARGS' }, '') + self:define('__asm', { 'ARGS' }, '') + self:define('__asm__', { 'ARGS' }, '') self:define('__inline__', nil, '') self:define('EXTERN', nil, 'extern') - self:define('INIT', {'...'}, '') + self:define('INIT', { '...' }, '') self:define('_GNU_SOURCE') self:define('INCLUDE_GENERATED_DECLARATIONS') self:define('UNIT_TESTING') @@ -158,9 +158,9 @@ end --- @return string[]? function Gcc:dependencies(hdr) --- @type string - local cmd = argss_to_cmd(self.path, {'-M', hdr}) .. ' 2>&1' + local cmd = argss_to_cmd(self.path, { '-M', hdr }) .. ' 2>&1' local out = assert(io.popen(cmd)) - local deps = out:read("*a") + local deps = out:read('*a') out:close() if deps then return parse_make_deps(deps) @@ -174,10 +174,14 @@ function Gcc:filter_standard_defines(defines) local pseudoheader_fname = 'tmp_empty_pseudoheader.h' local pseudoheader_file = assert(io.open(pseudoheader_fname, 'w')) pseudoheader_file:close() - local standard_defines = assert(repeated_read_cmd(self.path, - self.preprocessor_extra_flags, - self.get_defines_extra_flags, - {pseudoheader_fname})) + local standard_defines = assert( + repeated_read_cmd( + self.path, + self.preprocessor_extra_flags, + self.get_defines_extra_flags, + { pseudoheader_fname } + ) + ) os.remove(pseudoheader_fname) self.standard_defines = {} --- @type table<string,true> for line in standard_defines:gmatch('[^\n]+') do @@ -192,7 +196,7 @@ function Gcc:filter_standard_defines(defines) end end - return table.concat(ret, "\n") + return table.concat(ret, '\n') end --- returns a stream representing a preprocessed form of the passed-in headers. @@ -202,24 +206,33 @@ end --- @return string, string function Gcc:preprocess(previous_defines, ...) -- create pseudo-header - local pseudoheader = headerize({...}, false) + local pseudoheader = headerize({ ... }, false) local pseudoheader_fname = 'tmp_pseudoheader.h' local pseudoheader_file = assert(io.open(pseudoheader_fname, 'w')) pseudoheader_file:write(previous_defines) - pseudoheader_file:write("\n") + pseudoheader_file:write('\n') pseudoheader_file:write(pseudoheader) pseudoheader_file:flush() pseudoheader_file:close() - local defines = assert(repeated_read_cmd(self.path, self.preprocessor_extra_flags, - self.get_defines_extra_flags, - {pseudoheader_fname})) + local defines = assert( + repeated_read_cmd( + self.path, + self.preprocessor_extra_flags, + self.get_defines_extra_flags, + { pseudoheader_fname } + ) + ) defines = self:filter_standard_defines(defines) - local declarations = assert(repeated_read_cmd(self.path, - self.preprocessor_extra_flags, - self.get_declarations_extra_flags, - {pseudoheader_fname})) + local declarations = assert( + repeated_read_cmd( + self.path, + self.preprocessor_extra_flags, + self.get_declarations_extra_flags, + { pseudoheader_fname } + ) + ) os.remove(pseudoheader_fname) @@ -233,10 +246,10 @@ end --- @return Gcc? local function find_best_cc(compilers) for _, meta in pairs(compilers) do - local version = assert(io.popen(tostring(meta.path) .. " -v 2>&1")) + local version = assert(io.popen(tostring(meta.path) .. ' -v 2>&1')) version:close() if version then - return Gcc:new({path = meta.path}) + return Gcc:new({ path = meta.path }) end end end |