robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1const std =@import ( "std" ); 2const build =@import ( "../build.zig" ); 3const LazyPath =std . Build . LazyPath ; 4 5pub fn build_nlua0 ( 6b : * std . Build , 7target : std . Build . ResolvedTarget , 8optimize : std . builtin . OptimizeMode , 9use_luajit : bool , 10ziglua : * std . Build . Dependency , 11libluv : ?* std . Build . Step . Compile , 12system_integration_options : build . SystemIntegrationOptions , 13) ! * std . Build . Step . Compile { 14const options =b . addOptions (); 15options . addOption ( bool , "use_luajit" , use_luajit ); 16 17const nlua0_exe =b . addExecutable (.{ 18. name ="nlua0" , 19. root_module =b . createModule (.{ 20. root_source_file =b . path ( "src/nlua0.zig" ), 21. target =target , 22. optimize =optimize , 23. link_libc =true , 24}), 25. use_llvm =true , 26}); 27const nlua0_mod =nlua0_exe . root_module ; 28 29const exe_unit_tests =b . addTest (.{ 30. root_module =b . createModule (.{ 31. root_source_file =b . path ( "src/nlua0.zig" ), 32. target =target , 33. optimize =optimize , 34. link_libc =true , 35}), 36}); 37 38const embedded_data =b . addModule ( "embedded_data" , .{ 39. root_source_file =b . path ( "runtime/embedded_data.zig" ), 40}); 41 42for ([ 2 ] * std . Build . Module { nlua0_mod , exe_unit_tests . root_module }) |mod |{ 43mod . addImport ( "ziglua" , ziglua . module ( "zlua" )); 44mod . addImport ( "embedded_data" , embedded_data ); 45// addImport already links by itself. but we need headers as well.. 46if ( system_integration_options . lua ) { 47const system_lua_lib =if ( use_luajit ) "luajit" else "lua5.1" ; 48mod . linkSystemLibrary ( system_lua_lib , .{}); 49} else { 50mod . linkLibrary ( ziglua . artifact ( "lua" )); 51} 52if ( libluv ) | luv | { 53mod . linkLibrary ( luv ); 54} else { 55mod . linkSystemLibrary ( "luv" , .{}); 56} 57 58mod . addOptions ( "options" , options ); 59 60mod . addIncludePath ( b . path ( "src" )); 61mod . addIncludePath ( b . path ( "src/includes_fixmelater" )); 62try add_lua_modules ( mod , use_luajit , true ); 63} 64 65// for debugging the nlua0 environment 66// like this: `zig build nlua0 -- script.lua {args}` 67const run_cmd =b . addRunArtifact ( nlua0_exe ); 68if ( b . args ) | args | { 69run_cmd . addArgs ( args ); 70} 71const run_step =b . step ( "nlua0" , "Run nlua0 build tool" ); 72run_step . dependOn ( & run_cmd . step ); 73 74b . installArtifact ( nlua0_exe ); // DEBUG 75 76const run_exe_unit_tests =b . addRunArtifact ( exe_unit_tests ); 77 78const test_step =b . step ( "test_nlua0" , "Run unit tests for nlua0" ); 79test_step . dependOn ( & run_exe_unit_tests . step ); 80 81return nlua0_exe ; 82} 83 84pub fn add_lua_modules ( 85mod : * std . Build . Module , 86use_luajit : bool , 87is_nlua0 : bool , 88) ! void { 89const flags =[ _ ][] const u8 { 90// Standard version used in Lua Makefile 91"-std=gnu99" , 92if ( is_nlua0 ) "-DNVIM_NLUA0" else "" , 93}; 94 95mod . addCSourceFiles (.{ 96. files =& .{ 97"src/mpack/lmpack.c" , 98"src/mpack/mpack_core.c" , 99"src/mpack/object.c" , 100"src/mpack/conv.c" , 101"src/mpack/rpc.c" , 102}, 103. flags =& flags , 104}); 105mod . addCSourceFiles (.{ 106. files =& .{ 107"src/lpeg/lpcap.c" , 108"src/lpeg/lpcode.c" , 109"src/lpeg/lpcset.c" , 110"src/lpeg/lpprint.c" , 111"src/lpeg/lptree.c" , 112"src/lpeg/lpvm.c" , 113}, 114. flags =& flags , 115}); 116 117if ( ! use_luajit ) { 118mod . addCSourceFiles (.{ 119. files =& .{ 120"src/bit.c" , 121}, 122. flags =& flags , 123}); 124} 125} 126 127pub fn build_libluv ( 128b : * std . Build , 129target : std . Build . ResolvedTarget , 130optimize : std . builtin . OptimizeMode , 131lua : ?* std . Build . Step . Compile , 132libuv : * std . Build . Step . Compile , 133use_luajit : bool , 134) ! * std . Build . Step . Compile { 135const upstream =b . lazyDependency ( "luv" , .{}); 136const compat53 =b . lazyDependency ( "lua_compat53" , .{}); 137var root_module =b . createModule (.{ 138. target =target , 139. optimize =optimize , 140}); 141const lib =b . addLibrary (.{ 142. name ="luv" , 143. linkage =. static , 144. root_module =root_module , 145}); 146 147if ( lua ) | lua_lib | { 148root_module . linkLibrary ( lua_lib ); 149} else { 150const system_lua_lib =if ( use_luajit ) "luajit" else "lua5.1" ; 151root_module . linkSystemLibrary ( system_lua_lib , .{}); 152} 153root_module . linkLibrary ( libuv ); 154 155if ( upstream ) | dep | { 156root_module . addIncludePath ( dep . path ( "src" )); 157lib . installHeader ( dep . path ( "src/luv.h" ), "luv/luv.h" ); 158root_module . addCSourceFiles (.{ . root =dep . path ( "src/" ), . files =& .{ 159"luv.c" , 160} }); 161} 162if ( compat53 ) | dep | { 163root_module . addIncludePath ( dep . path ( "c-api" )); 164root_module . addCSourceFiles (.{ . root =dep . path ( "c-api" ), . files =& .{ 165"compat-5.3.c" , 166} }); 167} 168 169return lib ; 170}