robin/maivi
a tiny cute lua web framework
git clone https://git.robinwobin.dev/robin/maivi
61e6d44
main
1const std =@import ( "std" ); 2 3pub fn build ( b : * std . Build ) ! void { 4const target =b . standardTargetOptions (.{}); 5const optimize =b . standardOptimizeOption (.{}); 6 7// options ==================================================================== 8 9const options =b . addOptions (); 10 11const prefix_runtime_path =b . getInstallPath (. prefix , "share/maivi/runtime" ); 12const prefix_runtime_fallback ="/usr/local/share/maivi/runtime" ; 13const use_runtime =b . option ( bool , "runtime" , "use a runtime directory instead of compiling the lua code" ) orelse false ; 14const runtime_path =b . option ([] const u8 , "runtimepath" , "path where the runtime will be installed by the packager" ) orelse blk : { 15if ( prefix_runtime_path [ 0 ] == '/' ) break : blk prefix_runtime_path ; 16std . log . warn ( "prefix path is not absolute. maivi runtime will fallback to { s } " , .{ prefix_runtime_fallback }); 17break : blk prefix_runtime_fallback ; 18}; 19const opt_md4c =b . option ( bool , "md4c" , "md4c support" ) orelse false ; 20 21const use_llvm =b . option ( bool , "llvm" , "llvm" ); 22 23options . addOption ( bool , "runtime" , use_runtime ); 24options . addOption ([] const u8 , "runtimepath" , runtime_path ); 25options . addOption ( bool , "md4c" , opt_md4c ); 26 27const mod =b . addModule ( "maivi" , .{ 28. root_source_file =b . path ( "src/root.zig" ), 29. target =target , 30. optimize =optimize , 31}); 32 33mod . addOptions ( "options" , options ); 34 35// executables ================================================================ 36 37const exe =b . addExecutable (.{ 38. name ="maivi" , 39. use_llvm =use_llvm , 40. root_module =b . createModule (.{ 41. root_source_file =b . path ( "src/main.zig" ), 42. target =target , 43. optimize =optimize , 44. imports =& .{ 45.{ . name ="maivi" , . module =mod }, 46}, 47}), 48}); 49 50b . installArtifact ( exe ); 51const run_step =b . step ( "run" , "Run" ); 52const run_cmd =b . addRunArtifact ( exe ); 53run_step . dependOn ( & run_cmd . step ); 54run_cmd . step . dependOn ( b . getInstallStep ()); 55if ( b . args ) | args | run_cmd . addArgs ( args ); 56 57const mlua_exe =try build_mlua ( b , b . createModule (.{ 58. root_source_file =b . path ( "src/mlua.zig" ), 59. target =target , 60. optimize =optimize , 61. imports =& .{.{ . name ="maivi" , . module =mod }}, 62})); 63 64// dependencies =============================================================== 65 66const libc =libc : { 67break : libc std . zig . LibCDirs . detect ( 68b . graph . arena , 69b . graph . io , 70b . graph . zig_lib_directory . path orelse break : libc error . NoZigLib , 71& target . result , 72target . query . isNativeAbi (), 73true , 74null , 75& b . graph . environ_map , 76); 77} catch | err | std . debug . panic ( "failed to locate libc: { s } " , .{ @errorName ( err )}); 78 79var include_paths : std . ArrayList ( std . Build . LazyPath ) =try . initCapacity ( b . allocator , libc . libc_include_dir_list . len ); 80for ( libc . libc_include_dir_list ) |include_dir | 81try include_paths . append ( b . allocator , .{ . cwd_relative =include_dir }); 82 83const zlua =b . dependency ( "zlua" , .{ 84. target =target , 85. optimize =optimize , 86. lang =. luajit , 87. additional_system_headers =try include_paths . toOwnedSlice ( b . allocator ), 88}); 89 90mod . addImport ( "zlua" , zlua . module ( "zlua" )); 91exe . root_module . addImport ( "zlua" , zlua . module ( "zlua" )); 92mlua_exe . root_module . addImport ( "zlua" , zlua . module ( "zlua" )); 93 94const inspect_lua =b . dependency ( "inspect_lua" , .{}); 95 96const meow =b . addWriteFiles (); 97_ = meow . addCopyFile ( inspect_lua . path ( "inspect.lua" ), "inspect.lua" ); 98const meow_f =meow . add ( "inspect_lua.zig" , 99\\pub const inspect_module = @embedFile("inspect.lua"); 100); 101mod . addAnonymousImport ( "inspect_lua" , .{ 102. root_source_file =meow_f , 103}); 104 105const fern =b . dependency ( "fern" , .{ 106. target =target , 107. optimize =optimize , 108}); 109 110mod . addImport ( "fern" , fern . module ( "fern" )); 111exe . root_module . addImport ( "fern" , fern . module ( "fern" )); 112mlua_exe . root_module . addImport ( "fern" , fern . module ( "fern" )); 113 114if ( opt_md4c ) { 115const md4c =b . addTranslateC (.{ 116. root_source_file =b . path ( "src/md4c.h" ), 117. target =target , 118. optimize =optimize , 119}); 120md4c . linkSystemLibrary ( "md4c" , .{}); 121md4c . linkSystemLibrary ( "md4c-html" , .{}); 122const md4c_mod =md4c . createModule (); 123mod . addImport ( "md4c" , md4c_mod ); 124} 125 126// runtime ================================================================ 127 128if ( use_runtime ) { 129const runtime =b . addWriteFiles (); 130 131const runtimedir =runtime . addCopyDirectory ( b . path ( "src/lua/runtime" ), "runtime" , .{ 132. include_extensions =& .{ "lua" }, 133}); 134 135b . installDirectory (.{ 136. install_dir =. prefix , 137. install_subdir ="share/maivi/runtime" , 138. source_dir =runtimedir , 139}); 140} 141 142// tests ====================================================================== 143 144const mod_tests =b . addTest (.{ 145. root_module =mod , 146}); 147 148const run_mod_tests =b . addRunArtifact ( mod_tests ); 149 150const exe_tests =b . addTest (.{ 151. root_module =exe . root_module , 152}); 153 154const run_exe_tests =b . addRunArtifact ( exe_tests ); 155 156const test_step =b . step ( "test" , "Run tests" ); 157test_step . dependOn ( & run_mod_tests . step ); 158test_step . dependOn ( & run_exe_tests . step ); 159} 160 161pub fn build_mlua ( b : * std . Build , mod : * std . Build . Module ) ! * std . Build . Step . Compile { 162const mlua_exe =b . addExecutable (.{ . name ="mlua" , . root_module =mod }); 163 164// for debugging the mlua environment 165// like this: `zig build mlua -- script.lua {args}` 166const run_cmd =b . addRunArtifact ( mlua_exe ); 167if ( b . args ) | args | run_cmd . addArgs ( args ); 168const run_step =b . step ( "mlua" , "Run mlua build tool" ); 169run_step . dependOn ( & run_cmd . step ); 170b . installArtifact ( mlua_exe ); 171 172return mlua_exe ; 173}