robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
1883fe39bd
master
1-- helps managing loading different headers into the LuaJIT ffi. Untested on 2-- windows, will probably need quite a bit of adjustment to run there. 3 4local ffi =require ( 'ffi' ) 5local global_t =require ( 'test.testutil' ) 6 7local argss_to_cmd =global_t . argss_to_cmd 8local repeated_read_cmd =global_t . repeated_read_cmd 9 10--- @alias Compiler {path: string[], type: string} 11 12--- @type Compiler[] 13local ccs ={} 14 15local env_cc =os . getenv ( 'CC' ) 16if env_cc then 17table . insert ( ccs , { path ={ '/usr/bin/env' , env_cc } , type ='gcc' } ) 18end 19 20if ffi . os == 'Windows' then 21table . insert ( ccs , { path ={ 'cl' } , type ='msvc' } ) 22end 23 24table . insert ( ccs , { path ={ '/usr/bin/env' , 'cc' } , type ='gcc' } ) 25table . insert ( ccs , { path ={ '/usr/bin/env' , 'gcc' } , type ='gcc' } ) 26table . insert ( ccs , { path ={ '/usr/bin/env' , 'gcc-4.9' } , type ='gcc' } ) 27table . insert ( ccs , { path ={ '/usr/bin/env' , 'gcc-4.8' } , type ='gcc' } ) 28table . insert ( ccs , { path ={ '/usr/bin/env' , 'gcc-4.7' } , type ='gcc' } ) 29table . insert ( ccs , { path ={ '/usr/bin/env' , 'clang' } , type ='clang' } ) 30table . insert ( ccs , { path ={ '/usr/bin/env' , 'icc' } , type ='gcc' } ) 31 32-- parse Makefile format dependencies into a Lua table 33--- @param deps string 34--- @return string[] 35local function parse_make_deps ( deps ) 36-- remove line breaks and line concatenators 37deps =deps : gsub ( '\n' , '' ): gsub ( '\\' , '' ) 38-- remove the Makefile "target:" element 39deps =deps : gsub ( '.+:' , '' ) 40-- remove redundant spaces 41deps =deps : gsub ( ' +' , ' ' ) 42 43-- split according to token (space in this case) 44local headers ={} --- @type string[] 45for token in deps : gmatch ( '[^%s]+' ) do 46-- headers[token] = true 47headers [ # headers + 1 ] =token 48end 49 50-- resolve path redirections (..) to normalize all paths 51for i , v in ipairs ( headers ) do 52-- double dots (..) 53headers [ i ] =v : gsub ( '/[^/%s]+/%.%.' , '' ) 54-- single dot (.) 55headers [ i ] =v : gsub ( '%./' , '' ) 56end 57 58return headers 59end 60 61--- will produce a string that represents a meta C header file that includes 62--- all the passed in headers. I.e.: 63--- 64--- headerize({"stdio.h", "math.h"}, true) 65--- produces: 66--- #include <stdio.h> 67--- #include <math.h> 68--- 69--- headerize({"vim_defs.h", "memory.h"}, false) 70--- produces: 71--- #include "vim_defs.h" 72--- #include "memory.h" 73--- @param headers string[] 74--- @param global? boolean 75--- @return string 76local function headerize ( headers , global ) 77local fmt =global and '#include <%s>' or '#include "%s"' 78local formatted ={} --- @type string[] 79for _ , hdr in ipairs ( headers ) do 80formatted [ # formatted + 1 ] =string . format ( fmt , hdr ) 81end 82 83return table . concat ( formatted , '\n' ) 84end 85 86--- @class Gcc 87--- @field path string 88--- @field preprocessor_extra_flags string[] 89--- @field get_defines_extra_flags string[] 90--- @field get_declarations_extra_flags string[] 91local Gcc ={ 92preprocessor_extra_flags ={} , 93get_defines_extra_flags ={ '-std=c99' , '-dM' , '-E' } , 94get_declarations_extra_flags ={ '-std=c99' , '-P' , '-E' } , 95} 96 97--- @param name string 98--- @param args string[]? 99--- @param val string? 100function Gcc : define ( name , args , val ) 101local define =string . format ( '-D%s' , name ) 102if args then 103define =string . format ( '%s(%s)' , define , table . concat ( args , ',' )) 104end 105if val then 106define =string . format ( '%s=%s' , define , val ) 107end 108self . preprocessor_extra_flags [ # self . preprocessor_extra_flags + 1 ] =define 109end 110 111function Gcc : undefine ( name ) 112self . preprocessor_extra_flags [ # self . preprocessor_extra_flags + 1 ] ='-U' .. name 113end 114 115function Gcc : init_defines () 116-- preprocessor flags that will hopefully make the compiler produce C 117-- declarations that the LuaJIT ffi understands. 118self : define ( 'aligned' , { 'ARGS' } , '' ) 119self : define ( '__attribute__' , { 'ARGS' } , '' ) 120self : define ( '__asm' , { 'ARGS' } , '' ) 121self : define ( '__asm__' , { 'ARGS' } , '' ) 122self : define ( '__inline__' , nil , '' ) 123self : define ( 'EXTERN' , nil , 'extern' ) 124self : define ( 'INIT' , { '...' } , '' ) 125self : define ( '_GNU_SOURCE' ) 126self : define ( 'UNIT_TESTING' ) 127self : define ( 'UNIT_TESTING_LUA_PREPROCESSING' ) 128-- Needed for FreeBSD 129self : define ( '_Thread_local' , nil , '' ) 130-- Needed for macOS Sierra 131self : define ( '_Nullable' , nil , '' ) 132self : define ( '_Nonnull' , nil , '' ) 133self : undefine ( '__BLOCKS__' ) 134end 135 136--- @param obj? Compiler 137--- @return Gcc 138function Gcc : new ( obj ) 139obj =obj or {} 140setmetatable ( obj , self ) 141self . __index =self 142self : init_defines () 143return obj 144end 145 146--- @param ... string 147function Gcc : add_to_include_path ( ... ) 148local ef =self . preprocessor_extra_flags 149for i =1 , select ( '#' , ... ) do 150local path =select ( i , ... ) 151ef [ # ef + 1 ] ='-I' .. path 152end 153end 154 155function Gcc : add_apple_sysroot ( sysroot ) 156local ef =self . preprocessor_extra_flags 157 158table . insert ( ef , '-isysroot' ) 159table . insert ( ef , sysroot ) 160end 161 162-- returns a list of the headers files upon which this file relies 163--- @param hdr string 164--- @return string[]? 165function Gcc : dependencies ( hdr ) 166local cmd =table . concat ( argss_to_cmd ( self . path , { '-M' , hdr } ), ' ' ) .. ' 2>&1' 167local out =assert ( io . popen ( cmd )) 168local deps =out : read ( '*a' ) 169out : close () 170if deps then 171return parse_make_deps ( deps ) 172end 173end 174 175--- @param defines string 176--- @return string 177function Gcc : filter_standard_defines ( defines ) 178if not self . standard_defines then 179local pseudoheader_fname ='tmp_empty_pseudoheader.h' 180local pseudoheader_file =assert ( io . open ( pseudoheader_fname , 'w' )) 181pseudoheader_file : close () 182local standard_defines =assert ( 183repeated_read_cmd ( 184self . path , 185self . preprocessor_extra_flags , 186self . get_defines_extra_flags , 187{ pseudoheader_fname } 188) 189) 190os . remove ( pseudoheader_fname ) 191self . standard_defines ={} --- @type table<string,true> 192for line in standard_defines : gmatch ( '[^\n]+' ) do 193self . standard_defines [ line ] =true 194end 195end 196 197local ret ={} --- @type string[] 198for line in defines : gmatch ( '[^\n]+' ) do 199if not self . standard_defines [ line ] then 200ret [ # ret + 1 ] =line 201end 202end 203 204return table . concat ( ret , '\n' ) 205end 206 207--- returns a stream representing a preprocessed form of the passed-in headers. 208--- Don't forget to close the stream by calling the close() method on it. 209--- @param previous_defines string 210--- @param ... string 211--- @return string, string 212function Gcc : preprocess ( previous_defines , ... ) 213-- create pseudo-header 214local pseudoheader =headerize ( { ... } , false ) 215local pseudoheader_fname ='tmp_pseudoheader.h' 216local pseudoheader_file =assert ( io . open ( pseudoheader_fname , 'w' )) 217pseudoheader_file : write ( previous_defines ) 218pseudoheader_file : write ( '\n' ) 219pseudoheader_file : write ( pseudoheader ) 220pseudoheader_file : flush () 221pseudoheader_file : close () 222 223local defines =assert ( 224repeated_read_cmd ( 225self . path , 226self . preprocessor_extra_flags , 227self . get_defines_extra_flags , 228{ pseudoheader_fname } 229) 230) 231defines =self : filter_standard_defines ( defines ) 232 233local declarations =assert ( 234repeated_read_cmd ( 235self . path , 236self . preprocessor_extra_flags , 237self . get_declarations_extra_flags , 238{ pseudoheader_fname } 239) 240) 241 242os . remove ( pseudoheader_fname ) 243 244return declarations , defines 245end 246 247-- find the best cc. If os.exec causes problems on windows (like popping up 248-- a console window) we might consider using something like this: 249-- http://scite-ru.googlecode.com/svn/trunk/pack/tools/LuaLib/shell.html#exec 250--- @param compilers Compiler[] 251--- @return Gcc? 252local function find_best_cc ( compilers ) 253for _ , meta in pairs ( compilers ) do 254local version =assert ( io . popen ( tostring ( meta . path ) .. ' -v 2>&1' )) 255version : close () 256if version then 257return Gcc : new ( { path =meta . path } ) 258end 259end 260end 261 262-- find the best cc. If os.exec causes problems on windows (like popping up 263-- a console window) we might consider using something like this: 264-- http://scite-ru.googlecode.com/svn/trunk/pack/tools/LuaLib/shell.html#exec 265local cc =assert ( find_best_cc ( ccs )) 266 267local M ={} 268 269--- @param hdr string 270--- @return string[]? 271function M . includes ( hdr ) 272return cc : dependencies ( hdr ) 273end 274 275--- @param ... string 276--- @return string, string 277function M . preprocess ( ... ) 278return cc : preprocess ( ... ) 279end 280 281--- @param ... string 282function M . add_to_include_path ( ... ) 283return cc : add_to_include_path ( ... ) 284end 285 286--- @param ... string 287function M . add_apple_sysroot ( ... ) 288return cc : add_apple_sysroot ( ... ) 289end 290 291return M