2020-05-02 17:43:26 +01:00
const std = @import ( " std " ) ;
2021-12-30 03:35:16 +00:00
const builtin = @import ( " builtin " ) ;
2022-07-15 17:06:18 +01:00
const shared = @import ( " src/shared.zig " ) ;
2020-05-25 22:12:08 +01:00
2023-06-18 22:29:24 +01:00
const zls_version = std . SemanticVersion { . major = 0 , . minor = 11 , . patch = 0 } ;
2022-09-02 19:10:05 +01:00
2020-05-07 16:29:40 +01:00
pub fn build ( b : * std . build . Builder ) ! void {
2023-01-06 18:59:58 +00:00
comptime {
const current_zig = builtin . zig_version ;
2023-06-18 22:29:24 +01:00
const min_zig = std . SemanticVersion . parse ( " 0.11.0-dev.3696+8d0a8c285 " ) catch unreachable ; // std.builtin.Version -> std.SemanticVersion
2023-01-06 18:59:58 +00:00
if ( current_zig . order ( min_zig ) = = . lt ) {
@compileError ( std . fmt . comptimePrint ( " Your Zig version v{} does not meet the minimum build requirement of v{} " , . { current_zig , min_zig } ) ) ;
}
}
2023-02-03 22:06:57 +00:00
2020-04-24 23:19:03 +01:00
const target = b . standardTargetOptions ( . { } ) ;
2023-02-03 22:06:57 +00:00
const optimize = b . standardOptimizeOption ( . { } ) ;
const exe = b . addExecutable ( . {
. name = " zls " ,
. root_source_file = . { . path = " src/main.zig " } ,
. target = target ,
. optimize = optimize ,
} ) ;
2020-04-24 23:19:03 +01:00
2021-08-28 16:22:41 +01:00
const exe_options = b . addOptions ( ) ;
exe . addOptions ( " build_options " , exe_options ) ;
2023-02-20 21:40:05 +00:00
const pie = b . option ( bool , " pie " , " Build a Position Independent Executable " ) ;
2022-09-21 19:11:04 +01:00
const enable_tracy = b . option ( bool , " enable_tracy " , " Whether tracy should be enabled. " ) orelse false ;
const coverage = b . option ( bool , " generate_coverage " , " Generate coverage data with kcov " ) orelse false ;
2022-10-31 05:51:51 +00:00
const coverage_output_dir = b . option ( [ ] const u8 , " coverage_output_dir " , " Output directory for coverage data " ) orelse b . pathJoin ( & . { b . install_prefix , " kcov " } ) ;
2023-03-20 19:30:51 +00:00
const test_filter = b . option ( [ ] const u8 , " test-filter " , " Skip tests that do not match filter " ) ;
2022-09-21 19:11:04 +01:00
2023-03-03 06:26:15 +00:00
exe_options . addOption ( shared . ZigVersion , " data_version " , b . option ( shared . ZigVersion , " data_version " , " The Zig version your compiler is. " ) orelse . master ) ;
exe_options . addOption ( std . log . Level , " log_level " , b . option ( std . log . Level , " log_level " , " The Log Level to be used. " ) orelse . info ) ;
exe_options . addOption ( bool , " enable_tracy " , enable_tracy ) ;
2023-05-09 23:38:09 +01:00
exe_options . addOption ( bool , " enable_tracy_allocation " , b . option ( bool , " enable_tracy_allocation " , " Enable using TracyAllocator to monitor allocations. " ) orelse enable_tracy ) ;
exe_options . addOption ( bool , " enable_tracy_callstack " , b . option ( bool , " enable_tracy_callstack " , " Enable callstack graphs. " ) orelse enable_tracy ) ;
2023-03-03 06:26:15 +00:00
exe_options . addOption ( bool , " enable_failing_allocator " , b . option ( bool , " enable_failing_allocator " , " Whether to use a randomly failing allocator. " ) orelse false ) ;
exe_options . addOption ( u32 , " enable_failing_allocator_likelihood " , b . option ( u32 , " enable_failing_allocator_likelihood " , " The chance that an allocation will fail is `1/likelihood` " ) orelse 256 ) ;
2023-06-04 07:42:56 +01:00
exe_options . addOption ( bool , " use_gpa " , b . option ( bool , " use_gpa " , " Good for debugging " ) orelse ( optimize = = . Debug ) ) ;
2022-06-06 04:50:17 +01:00
2022-09-01 14:42:15 +01:00
const version = v : {
2022-09-02 19:10:05 +01:00
const version_string = b . fmt ( " {d}.{d}.{d} " , . { zls_version . major , zls_version . minor , zls_version . patch } ) ;
2023-03-20 19:30:51 +00:00
const build_root_path = b . build_root . path orelse " . " ;
2022-09-02 19:10:05 +01:00
2022-09-02 21:21:55 +01:00
var code : u8 = undefined ;
const git_describe_untrimmed = b . execAllowFail ( & [ _ ] [ ] const u8 {
2023-02-14 08:07:25 +00:00
" git " , " -C " , build_root_path , " describe " , " --match " , " *.*.* " , " --tags " ,
2022-09-02 21:21:55 +01:00
} , & code , . Ignore ) catch break : v version_string ;
2022-09-01 14:42:15 +01:00
2022-09-02 19:10:05 +01:00
const git_describe = std . mem . trim ( u8 , git_describe_untrimmed , " \n \r " ) ;
switch ( std . mem . count ( u8 , git_describe , " - " ) ) {
0 = > {
// Tagged release version (e.g. 0.10.0).
std . debug . assert ( std . mem . eql ( u8 , git_describe , version_string ) ) ; // tagged release must match version string
break : v version_string ;
} ,
2 = > {
// Untagged development build (e.g. 0.10.0-dev.216+34ce200).
var it = std . mem . split ( u8 , git_describe , " - " ) ;
const tagged_ancestor = it . first ( ) ;
const commit_height = it . next ( ) . ? ;
const commit_id = it . next ( ) . ? ;
2023-06-18 22:29:24 +01:00
const ancestor_ver = try std . SemanticVersion . parse ( tagged_ancestor ) ;
2022-09-02 19:10:05 +01:00
std . debug . assert ( zls_version . order ( ancestor_ver ) = = . gt ) ; // zls version must be greater than its previous version
std . debug . assert ( std . mem . startsWith ( u8 , commit_id , " g " ) ) ; // commit hash is prefixed with a 'g'
break : v b . fmt ( " {s}-dev.{s}+{s} " , . { version_string , commit_height , commit_id [ 1 . . ] } ) ;
} ,
else = > {
std . debug . print ( " Unexpected 'git describe' output: '{s}' \n " , . { git_describe } ) ;
std . process . exit ( 1 ) ;
} ,
}
2022-09-01 14:42:15 +01:00
} ;
2022-09-02 19:10:05 +01:00
2023-03-20 19:30:51 +00:00
exe_options . addOption ( [ ] const u8 , " version " , version ) ;
2022-09-01 14:42:15 +01:00
2023-03-03 06:26:15 +00:00
const known_folders_module = b . dependency ( " known_folders " , . { } ) . module ( " known-folders " ) ;
2023-02-05 07:15:42 +00:00
exe . addModule ( " known-folders " , known_folders_module ) ;
2020-05-25 01:22:39 +01:00
2023-03-03 06:26:15 +00:00
const tres_module = b . dependency ( " tres " , . { } ) . module ( " tres " ) ;
2023-02-05 07:15:42 +00:00
exe . addModule ( " tres " , tres_module ) ;
2022-12-27 06:47:57 +00:00
2023-03-03 06:26:15 +00:00
const diffz_module = b . dependency ( " diffz " , . { } ) . module ( " diffz " ) ;
2023-02-11 19:21:10 +00:00
exe . addModule ( " diffz " , diffz_module ) ;
2023-06-04 07:42:56 +01:00
const binned_allocator_module = b . dependency ( " binned_allocator " , . { } ) . module ( " binned_allocator " ) ;
exe . addModule ( " binned_allocator " , binned_allocator_module ) ;
2022-06-06 04:50:17 +01:00
if ( enable_tracy ) {
2023-05-09 23:38:09 +01:00
const client_cpp = " src/tracy/public/TracyClient.cpp " ;
2022-06-06 04:50:17 +01:00
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
const tracy_c_flags : [ ] const [ ] const u8 = if ( target . isWindows ( ) and target . getAbi ( ) = = . gnu )
& [ _ ] [ ] const u8 { " -DTRACY_ENABLE=1 " , " -fno-sanitize=undefined " , " -D_WIN32_WINNT=0x601 " }
else
& [ _ ] [ ] const u8 { " -DTRACY_ENABLE=1 " , " -fno-sanitize=undefined " } ;
exe . addIncludePath ( " src/tracy " ) ;
exe . addCSourceFile ( client_cpp , tracy_c_flags ) ;
2023-03-20 19:30:51 +00:00
exe . linkLibCpp ( ) ;
2022-06-06 04:50:17 +01:00
exe . linkLibC ( ) ;
if ( target . isWindows ( ) ) {
exe . linkSystemLibrary ( " dbghelp " ) ;
exe . linkSystemLibrary ( " ws2_32 " ) ;
}
}
2023-02-20 21:40:05 +00:00
exe . pie = pie ;
2023-04-11 21:21:47 +01:00
b . installArtifact ( exe ) ;
2020-04-24 23:19:03 +01:00
2023-03-20 19:30:51 +00:00
const build_options_module = exe_options . createModule ( ) ;
const zls_module = b . addModule ( " zls " , . {
. source_file = . { . path = " src/zls.zig " } ,
. dependencies = & . {
. { . name = " known-folders " , . module = known_folders_module } ,
. { . name = " tres " , . module = tres_module } ,
. { . name = " diffz " , . module = diffz_module } ,
2023-06-04 07:42:56 +01:00
. { . name = " binned_allocator " , . module = binned_allocator_module } ,
2023-03-20 19:30:51 +00:00
. { . name = " build_options " , . module = build_options_module } ,
} ,
} ) ;
2023-02-03 22:06:57 +00:00
const gen_exe = b . addExecutable ( . {
. name = " zls_gen " ,
. root_source_file = . { . path = " src/config_gen/config_gen.zig " } ,
} ) ;
2023-02-05 07:15:42 +00:00
gen_exe . addModule ( " tres " , tres_module ) ;
2022-12-11 11:10:48 +00:00
2023-04-11 21:21:47 +01:00
const gen_cmd = b . addRunArtifact ( gen_exe ) ;
2022-12-11 11:10:48 +00:00
gen_cmd . addArgs ( & . {
2023-03-20 19:30:51 +00:00
b . pathFromRoot ( " src/Config.zig " ) ,
b . pathFromRoot ( " schema.json " ) ,
b . pathFromRoot ( " README.md " ) ,
b . pathFromRoot ( " src/data " ) ,
2022-12-11 11:10:48 +00:00
} ) ;
2023-01-19 06:46:42 +00:00
if ( b . args ) | args | gen_cmd . addArgs ( args ) ;
2022-12-30 23:45:31 +00:00
2022-12-11 11:10:48 +00:00
const gen_step = b . step ( " gen " , " Regenerate config files " ) ;
gen_step . dependOn ( & gen_cmd . step ) ;
2020-05-27 19:58:35 +01:00
const test_step = b . step ( " test " , " Run all the tests " ) ;
2021-10-01 01:46:10 +01:00
test_step . dependOn ( b . getInstallStep ( ) ) ;
2020-05-27 19:58:35 +01:00
2023-02-03 22:06:57 +00:00
var tests = b . addTest ( . {
. root_source_file = . { . path = " tests/tests.zig " } ,
. target = target ,
. optimize = . Debug ,
2023-04-11 21:21:47 +01:00
. filter = test_filter ,
2023-02-03 22:06:57 +00:00
} ) ;
2023-02-05 07:15:42 +00:00
tests . addModule ( " zls " , zls_module ) ;
tests . addModule ( " tres " , tres_module ) ;
2023-02-11 19:21:10 +00:00
tests . addModule ( " diffz " , diffz_module ) ;
2023-06-04 07:42:56 +01:00
tests . addModule ( " binned_allocator " , binned_allocator_module ) ;
2023-03-20 19:30:51 +00:00
test_step . dependOn ( & b . addRunArtifact ( tests ) . step ) ;
2023-01-20 16:04:59 +00:00
2023-02-03 22:19:40 +00:00
var src_tests = b . addTest ( . {
. root_source_file = . { . path = " src/zls.zig " } ,
. target = target ,
. optimize = . Debug ,
2023-04-11 21:21:47 +01:00
. filter = test_filter ,
2023-02-03 22:19:40 +00:00
} ) ;
2023-03-20 19:30:51 +00:00
test_step . dependOn ( & b . addRunArtifact ( src_tests ) . step ) ;
if ( coverage ) {
const src_dir = b . pathFromRoot ( " src " ) ;
const include_pattern = b . fmt ( " --include-pattern={s} " , . { src_dir } ) ;
const args = & [ _ ] ? [ ] const u8 { " kcov " , include_pattern , coverage_output_dir , null } ;
tests . setExecCmd ( args ) ;
src_tests . setExecCmd ( args ) ;
// TODO merge coverage reports
}
2020-04-24 23:19:03 +01:00
}