╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ build.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
901 const exe = b.addExecutable(.{ 902 .name = "zig", 903 // This number should never be raised. If the value is exceeded, then 904 // it is considered a bug in the Zig project that building the 905 // compiler takes more than 8G of memory. 906 .max_rss = 8_000_000_000, 907 .root_module = addCompilerMod(b, options), 908 }); 909 exe.stack_size = stack_size; 910 911 // Must match the condition in CMakeLists.txt. 912 const function_data_sections = switch (options.target.result.cpu.arch) { 913 .arm, 914 .armeb, 915 .thumb, 916 .thumbeb, 917 .hexagon, 918 .powerpc, 919 .powerpcle, 920 .powerpc64, 921 .powerpc64le, 922 => true, 923 else => false, 924 }; 925 926 exe.link_function_sections = function_data_sections; 927 exe.link_data_sections = function_data_sections; 928 929 return exe; 930 } 931 932 const exe_cflags = [_][]const u8{ 933 "-std=c++17", 934 "-D__STDC_CONSTANT_MACROS", 935 "-D__STDC_FORMAT_MACROS", 936 "-D__STDC_LIMIT_MACROS", 937 "-D_GNU_SOURCE", 938 "-fno-exceptions", 939 "-fno-rtti", 940 "-fno-stack-protector", 941 "-fvisibility-inlines-hidden", 942 "-Wno-type-limits", 943 "-Wno-missing-braces", 944 "-Wno-comment", 945 // `exe_cflags` is only used for static linking. 946 "-DLLVM_BUILD_STATIC", 947 "-DCLANG_BUILD_STATIC", 948 }; 949 950 fn addCmakeCfgOptionsToExe( 951 b: *std.Build, 952 cfg: CMakeConfig, 953 exe: *std.Build.Step.Compile, 954 use_zig_libcxx: bool, 955 ) !void { 956 const mod = exe.root_module; 957 const target = &mod.resolved_target.?.result; 958 959 if (target.os.tag.isDarwin()) { 960 // useful for package maintainers 961 exe.headerpad_max_install_names = true; 962 } 963 964 mod.addObjectFile(.{ .cwd_relative = b.pathJoin(&.{ 965 cfg.cmake_binary_dir, 966 "zigcpp", 967 b.fmt("{s}{s}{s}", .{ 968 cfg.cmake_static_library_prefix, 969 "zigcpp", 970 cfg.cmake_static_library_suffix, 971 }), 972 }) }); 973 assert(cfg.lld_include_dir.len != 0); 974 mod.addIncludePath(.{ .cwd_relative = cfg.lld_include_dir }); 975 mod.addIncludePath(.{ .cwd_relative = cfg.llvm_include_dir }); 976 mod.addLibraryPath(.{ .cwd_relative = cfg.llvm_lib_dir }); 977 addCMakeLibraryList(mod, cfg.clang_libraries); 978 addCMakeLibraryList(mod, cfg.lld_libraries); 979 addCMakeLibraryList(mod, cfg.llvm_libraries); 980 981 if (use_zig_libcxx) { 982 mod.link_libcpp = true; 983 } else { 984 // System -lc++ must be used because in this code path we are attempting to link 985 // against system-provided LLVM, Clang, LLD. 986 const need_cpp_includes = true; 987 const static = cfg.llvm_linkage == .static; 988 const lib_suffix = if (static) target.staticLibSuffix()[1..] else target.dynamicLibSuffix()[1..]; 989 switch (target.os.tag) { 990 .linux => { 991 // First we try to link against the detected libcxx name. If that doesn't work, we fall 992 // back to -lc++ and cross our fingers. 993 addCxxKnownPath(b, cfg, exe, b.fmt("lib{s}.{s}", .{ cfg.system_libcxx, lib_suffix }), "", need_cpp_includes) catch |err| switch (err) { 994 error.RequiredLibraryNotFound => { 995 mod.link_libcpp = true; 996 }, 997 else => |e| return e, 998 }; 999 mod.linkSystemLibrary("unwind", .{}); 1000 }, 1001 .driverkit, .ios, .maccatalyst, .macos, .tvos, .visionos, .watchos => { 1002 mod.link_libcpp = true; 1003 }, 1004 .windows => { 1005 if (target.abi != .msvc) mod.link_libcpp = true; 1006 }, 1007 .freebsd => { 1008 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{lib_suffix}), null, need_cpp_includes); 1009 if (static) try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes); 1010 }, 1011 .openbsd => { 1012 // - llvm requires libexecinfo which has conflicting symbols with libc++abi 1013 // - only an issue with .a linking 1014 // - workaround is to link c++abi dynamically 1015 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++.{s}", .{target.dynamicLibSuffix()[1..]}), null, need_cpp_includes); 1016 try addCxxKnownPath(b, cfg, exe, b.fmt("libc++abi.{s}", .{target.dynamicLibSuffix()[1..]}), null, need_cpp_includes); 1017 }, 1018 .netbsd, .dragonfly => { 1019 try addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), null, need_cpp_includes); 1020 if (static) try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes); 1021 }, 1022 .illumos => { 1023 try addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), null, need_cpp_includes); 1024 try addCxxKnownPath(b, cfg, exe, b.fmt("libgcc_eh.{s}", .{lib_suffix}), null, need_cpp_includes); 1025 }, 1026 .haiku => { 1027 try addCxxKnownPath(b, cfg, exe, b.fmt("libstdc++.{s}", .{lib_suffix}), null, need_cpp_includes); 1028 }, 1029 else => {}, 1030 } 1031 } 1032 1033 if (cfg.dia_guids_lib.len != 0) { 1034 mod.addObjectFile(.{ .cwd_relative = cfg.dia_guids_lib }); 1035 } 1036 } 1037 1038 fn addStaticLlvmOptionsToModule(mod: *std.Build.Module, options: struct { 1039 llvm_has_m68k: bool, 1040 llvm_has_csky: bool, 1041 llvm_has_arc: bool, 1042 llvm_has_xtensa: bool, 1043 }) !void { 1044 // Adds the Zig C++ sources which both stage1 and stage2 need. 1045 // 1046 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling 1047 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is 1048 // unavailable when LLVM is compiled in Release mode. 1049 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"}; 1050 mod.addCSourceFiles(.{ 1051 .files = &zig_cpp_sources, 1052 .flags = &zig_cpp_cflags, 1053 }); 1054 1055 const lsl_options: std.Build.Module.LinkSystemLibraryOptions = .{ .use_pkg_config = .no }; 1056 1057 for (clang_libs) |lib_name| { 1058 mod.linkSystemLibrary(lib_name, lsl_options); 1059 } 1060 1061 for (lld_libs) |lib_name| { 1062 mod.linkSystemLibrary(lib_name, lsl_options); 1063 } 1064 1065 for (llvm_libs) |lib_name| { 1066 mod.linkSystemLibrary(lib_name, lsl_options); 1067 } 1068 1069 if (options.llvm_has_m68k) for (llvm_libs_m68k) |lib_name| { 1070 mod.linkSystemLibrary(lib_name, lsl_options); 1071 }; 1072 1073 if (options.llvm_has_csky) for (llvm_libs_csky) |lib_name| { 1074 mod.linkSystemLibrary(lib_name, lsl_options); 1075 }; 1076 1077 if (options.llvm_has_arc) for (llvm_libs_arc) |lib_name| { 1078 mod.linkSystemLibrary(lib_name, lsl_options); 1079 }; 1080 1081 if (options.llvm_has_xtensa) for (llvm_libs_xtensa) |lib_name| { 1082 mod.linkSystemLibrary(lib_name, lsl_options); 1083 }; 1084 1085 mod.linkSystemLibrary("z", lsl_options); 1086 mod.linkSystemLibrary("zstd", lsl_options); 1087 1088 if (mod.resolved_target.?.result.os.tag != .windows or mod.resolved_target.?.result.abi != .msvc) { 1089 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. 1090 mod.linkSystemLibrary("c++", lsl_options); 1091 } 1092 1093 if (mod.resolved_target.?.result.os.tag == .windows) { 1094 mod.linkSystemLibrary("version", lsl_options); 1095 mod.linkSystemLibrary("uuid", lsl_options); 1096 mod.linkSystemLibrary("ole32", lsl_options); 1097 } 1098 } 1099 1100 fn addCxxKnownPath( 1101 b: *std.Build, 1102 ctx: CMakeConfig, 1103 exe: *std.Build.Step.Compile, 1104 objname: []const u8, 1105 errtxt: ?[]const u8, 1106 need_cpp_includes: bool, 1107 ) !void { 1108 if (!std.process.can_spawn) return error.RequiredLibraryNotFound; 1109 1110 const arena = b.graph.arena; 1111 1112 const path_padded = run: { 1113 var args = std.array_list.Managed([]const u8).init(arena); 1114 try args.append(ctx.cxx_compiler); 1115 var it = std.mem.tokenizeAny(u8, ctx.cxx_compiler_arg1, &std.ascii.whitespace); 1116 while (it.next()) |arg| try args.append(arg); 1117 try args.append(b.fmt("-print-file-name={s}", .{objname})); 1118 break :run b.run(args.items); 1119 }; 1120 var tokenizer = mem.tokenizeAny(u8, path_padded, "\r\n"); 1121 const path_unpadded = tokenizer.next().?; 1122 if (mem.eql(u8, path_unpadded, objname)) { 1123 if (errtxt) |msg| { 1124 std.debug.print("{s}", .{msg}); 1125 } else { 1126 std.debug.print("Unable to determine path to {s}\n", .{objname}); 1127 } 1128 return error.RequiredLibraryNotFound; 1129 } 1130 // By default, explicit library paths are not checked for being linker scripts, 1131 // but libc++ may very well be one, so force all inputs to be checked when passing 1132 // an explicit path to libc++. 1133 exe.allow_so_scripts = true; 1134 exe.root_module.addObjectFile(.{ .cwd_relative = path_unpadded }); 1135 1136 // TODO a way to integrate with system c++ include files here 1137 // c++ -E -Wp,-v -xc++ /dev/null 1138 if (need_cpp_includes) { 1139 // I used these temporarily for testing something but we obviously need a 1140 // more general purpose solution here. 1141 //exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0"); 1142 //exe.root_module.addIncludePath("/nix/store/2lr0fc0ak8rwj0k8n3shcyz1hz63wzma-gcc-11.3.0/include/c++/11.3.0/x86_64-unknown-linux-gnu"); 1143 } 1144 } 1145 1146 fn addCMakeLibraryList(mod: *std.Build.Module, list: []const u8) void { 1147 var it = mem.tokenizeScalar(u8, list, ';'); 1148 while (it.next()) |lib| { 1149 if (mem.startsWith(u8, lib, "-l")) { 1150 mod.linkSystemLibrary(lib["-l".len..], .{}); 1151 } else if (mod.resolved_target.?.result.os.tag == .windows and 1152 mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) 1153 { 1154 mod.linkSystemLibrary(lib[0 .. lib.len - ".lib".len], .{}); 1155 } else { 1156 mod.addObjectFile(.{ .cwd_relative = lib }); 1157 } 1158 } 1159 } 1160 1161 const CMakeConfig = struct { 1162 llvm_linkage: std.lang.LinkMode, 1163 cmake_binary_dir: []const u8, 1164 cmake_prefix_path: []const u8, 1165 cmake_static_library_prefix: []const u8, 1166 cmake_static_library_suffix: []const u8, 1167 cxx_compiler: []const u8, 1168 cxx_compiler_arg1: []const u8, 1169 lld_include_dir: []const u8, 1170 lld_libraries: []const u8, 1171 clang_libraries: []const u8, 1172 llvm_lib_dir: []const u8, 1173 llvm_include_dir: []const u8, 1174 llvm_libraries: []const u8, 1175 dia_guids_lib: []const u8, 1176 system_libcxx: []const u8, 1177 }; 1178 1179 const max_config_h_bytes = 1 * 1024 * 1024; 1180 1181 fn findConfigH(b: *std.Build, config_h_path_option: ?[]const u8) ?[]const u8 { 1182 const arena = b.graph.arena; 1183 const io = b.graph.io; 1184 const cwd: Io.Dir = .cwd(); 1185 1186 if (config_h_path_option) |path| { 1187 var config_h_or_err = cwd.openFile(io, path, .{}); 1188 if (config_h_or_err) |*file| { 1189 file.close(io); 1190 return path; 1191 } else |_| { 1192 std.log.err("Could not open provided config.h: \"{s}\"", .{path}); 1193 std.process.exit(1); 1194 } 1195 } 1196 1197 var check_dir = fs.path.dirname(b.graph.zig_exe).?; 1198 while (true) { 1199 var dir = cwd.openDir(io, check_dir, .{}) catch unreachable; 1200 defer dir.close(io); 1201 1202 // Check if config.h is present in dir 1203 var config_h_or_err = dir.openFile(io, "config.h", .{}); 1204 if (config_h_or_err) |*file| { 1205 file.close(io); 1206 return fs.path.join( 1207 arena, 1208 &[_][]const u8{ check_dir, "config.h" }, 1209 ) catch unreachable; 1210 } else |e| switch (e) { 1211 error.FileNotFound => {}, 1212 else => unreachable, 1213 } 1214 1215 // Check if we reached the source root by looking for .git, and bail if so 1216 var git_dir_or_err = dir.openDir(io, ".git", .{}); 1217 if (git_dir_or_err) |*git_dir| { 1218 git_dir.close(io); 1219 return null; 1220 } else |_| {} 1221 1222 // Otherwise, continue search in the parent directory 1223 const new_check_dir = fs.path.dirname(check_dir); 1224 if (new_check_dir == null or mem.eql(u8, new_check_dir.?, check_dir)) { 1225 return null; 1226 } 1227 check_dir = new_check_dir.?; 1228 } 1229 } 1230 1231 fn parseConfigH(b: *std.Build, config_h_text: []const u8) ?CMakeConfig { 1232 var ctx: CMakeConfig = .{ 1233 .llvm_linkage = undefined, 1234 .cmake_binary_dir = undefined, 1235 .cmake_prefix_path = undefined, 1236 .cmake_static_library_prefix = undefined, 1237 .cmake_static_library_suffix = undefined, 1238 .cxx_compiler = undefined, 1239 .cxx_compiler_arg1 = "", 1240 .lld_include_dir = undefined, 1241 .lld_libraries = undefined, 1242 .clang_libraries = undefined, 1243 .llvm_lib_dir = undefined, 1244 .llvm_include_dir = undefined, 1245 .llvm_libraries = undefined, 1246 .dia_guids_lib = undefined, 1247 .system_libcxx = undefined, 1248 }; 1249 1250 const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ 1251 .{ 1252 .prefix = "#define ZIG_CMAKE_BINARY_DIR ", 1253 .field = "cmake_binary_dir", 1254 }, 1255 .{ 1256 .prefix = "#define ZIG_CMAKE_PREFIX_PATH ", 1257 .field = "cmake_prefix_path", 1258 }, 1259 .{ 1260 .prefix = "#define ZIG_CMAKE_STATIC_LIBRARY_PREFIX ", 1261 .field = "cmake_static_library_prefix", 1262 }, 1263 .{ 1264 .prefix = "#define ZIG_CMAKE_STATIC_LIBRARY_SUFFIX ", 1265 .field = "cmake_static_library_suffix", 1266 }, 1267 .{ 1268 .prefix = "#define ZIG_CXX_COMPILER ", 1269 .field = "cxx_compiler", 1270 }, 1271 .{ 1272 .prefix = "#define ZIG_CXX_COMPILER_ARG1 ", 1273 .field = "cxx_compiler_arg1", 1274 }, 1275 .{ 1276 .prefix = "#define ZIG_LLD_INCLUDE_PATH ", 1277 .field = "lld_include_dir", 1278 }, 1279 .{ 1280 .prefix = "#define ZIG_LLD_LIBRARIES ", 1281 .field = "lld_libraries", 1282 }, 1283 .{ 1284 .prefix = "#define ZIG_CLANG_LIBRARIES ", 1285 .field = "clang_libraries", 1286 }, 1287 .{ 1288 .prefix = "#define ZIG_LLVM_LIBRARIES ", 1289 .field = "llvm_libraries", 1290 }, 1291 .{ 1292 .prefix = "#define ZIG_DIA_GUIDS_LIB ", 1293 .field = "dia_guids_lib", 1294 }, 1295 .{ 1296 .prefix = "#define ZIG_LLVM_INCLUDE_PATH ", 1297 .field = "llvm_include_dir", 1298 }, 1299 .{ 1300 .prefix = "#define ZIG_LLVM_LIB_PATH ", 1301 .field = "llvm_lib_dir", 1302 }, 1303 .{ 1304 .prefix = "#define ZIG_SYSTEM_LIBCXX", 1305 .field = "system_libcxx", 1306 }, 1307 // .prefix = ZIG_LLVM_LINK_MODE parsed manually below 1308 }; 1309 1310 var lines_it = mem.tokenizeAny(u8, config_h_text, "\r\n"); 1311 while (lines_it.next()) |line| { 1312 inline for (mappings) |mapping| { 1313 if (mem.startsWith(u8, line, mapping.prefix)) { 1314 var it = mem.splitScalar(u8, line, '"'); 1315 _ = it.first(); // skip the stuff before the quote 1316 const quoted = it.next().?; // the stuff inside the quote 1317 const trimmed = mem.trim(u8, quoted, " "); 1318 @field(ctx, mapping.field) = toNativePathSep(b, trimmed); 1319 } 1320 } 1321 if (mem.startsWith(u8, line, "#define ZIG_LLVM_LINK_MODE ")) { 1322 var it = mem.splitScalar(u8, line, '"'); 1323 _ = it.next().?; // skip the stuff before the quote 1324 const quoted = it.next().?; // the stuff inside the quote 1325 ctx.llvm_linkage = if (mem.eql(u8, quoted, "shared")) .dynamic else .static; 1326 } 1327 } 1328 return ctx; 1329 } 1330 1331 fn toNativePathSep(b: *std.Build, s: []const u8) []u8 { 1332 const arena = b.graph.arena; 1333 const duplicated = arena.dupe(u8, s) catch unreachable; 1334 for (duplicated) |*byte| switch (byte.*) { 1335 '/' => byte.* = fs.path.sep, 1336 else => {}, 1337 }; 1338 return duplicated; 1339 } 1340 1341 const zig_cpp_sources = [_][]const u8{ 1342 // These are planned to stay even when we are self-hosted. 1343 "src/zig_llvm.cpp", 1344 "src/zig_llvm-ar.cpp", 1345 "src/zig_clang_driver.cpp", 1346 "src/zig_clang_cc1_main.cpp", 1347 "src/zig_clang_cc1as_main.cpp", 1348 }; 1349 1350 const clang_libs = [_][]const u8{ 1351 "clangFrontendTool", 1352 "clangCodeGen", 1353 "clangStaticAnalyzerFrontend", 1354 "clangStaticAnalyzerCheckers", 1355 "clangStaticAnalyzerCore", 1356 "clangCrossTU", 1357 "clangFrontend", 1358 "clangDriver", 1359 "clangOptions", 1360 "clangSerialization", 1361 "clangSema", 1362 "clangAnalysisLifetimeSafety", 1363 "clangAnalysis", 1364 "clangASTMatchers", 1365 "clangAST", 1366 "clangParse", 1367 "clangSema", 1368 "clangAPINotes", 1369 "clangBasic", 1370 "clangEdit", 1371 "clangLex", 1372 "clangRewriteFrontend", 1373 "clangRewrite", 1374 "clangIndex", 1375 "clangFormat", 1376 "clangToolingInclusions", 1377 "clangToolingCore", 1378 "clangExtractAPI", 1379 "clangSupport", 1380 "clangInstallAPI", 1381 "clangAST", 1382 }; 1383 const lld_libs = [_][]const u8{ 1384 "lldMinGW", 1385 "lldELF", 1386 "lldCOFF", 1387 "lldWasm", 1388 "lldMachO", 1389 "lldCommon", 1390 }; 1391 // This list can be re-generated with `llvm-config --libfiles` and then 1392 // reformatting using your favorite text editor. Note we do not execute 1393 // `llvm-config` here because we are cross compiling. Also omit LLVMTableGen 1394 // from these libs. 1395 const llvm_libs = [_][]const u8{ 1396 "LLVMWindowsManifest", 1397 "LLVMXRay", 1398 "LLVMLibDriver", 1399 "LLVMDlltoolDriver", 1400 "LLVMTelemetry", 1401 "LLVMTextAPIBinaryReader", 1402 "LLVMCoverage", 1403 "LLVMLineEditor", 1404 "LLVMXCoreDisassembler", 1405 "LLVMXCoreCodeGen", 1406 "LLVMXCoreDesc", 1407 "LLVMXCoreInfo", 1408 "LLVMX86TargetMCA", 1409 "LLVMX86Disassembler", 1410 "LLVMX86AsmParser", 1411 "LLVMX86CodeGen", 1412 "LLVMX86Desc", 1413 "LLVMX86Info", 1414 "LLVMWebAssemblyDisassembler", 1415 "LLVMWebAssemblyAsmParser", 1416 "LLVMWebAssemblyCodeGen", 1417 "LLVMWebAssemblyUtils", 1418 "LLVMWebAssemblyDesc", 1419 "LLVMWebAssemblyInfo", 1420 "LLVMVEDisassembler", 1421 "LLVMVEAsmParser", 1422 "LLVMVECodeGen", 1423 "LLVMVEDesc", 1424 "LLVMVEInfo", 1425 "LLVMSystemZDisassembler", 1426 "LLVMSystemZAsmParser", 1427 "LLVMSystemZCodeGen", 1428 "LLVMSystemZDesc", 1429 "LLVMSystemZInfo", 1430 "LLVMSPIRVCodeGen", 1431 "LLVMSPIRVDesc", 1432 "LLVMSPIRVInfo", 1433 "LLVMSPIRVAnalysis", 1434 "LLVMSparcDisassembler", 1435 "LLVMSparcAsmParser", 1436 "LLVMSparcCodeGen", 1437 "LLVMSparcDesc", 1438 "LLVMSparcInfo", 1439 "LLVMRISCVTargetMCA", 1440 "LLVMRISCVDisassembler", 1441 "LLVMRISCVAsmParser", 1442 "LLVMRISCVCodeGen", 1443 "LLVMRISCVDesc", 1444 "LLVMRISCVInfo", 1445 "LLVMPowerPCDisassembler", 1446 "LLVMPowerPCAsmParser", 1447 "LLVMPowerPCCodeGen", 1448 "LLVMPowerPCDesc", 1449 "LLVMPowerPCInfo", 1450 "LLVMNVPTXCodeGen", 1451 "LLVMNVPTXDesc", 1452 "LLVMNVPTXInfo", 1453 "LLVMMSP430Disassembler", 1454 "LLVMMSP430AsmParser", 1455 "LLVMMSP430CodeGen", 1456 "LLVMMSP430Desc", 1457 "LLVMMSP430Info", 1458 "LLVMMipsDisassembler", 1459 "LLVMMipsAsmParser", 1460 "LLVMMipsCodeGen", 1461 "LLVMMipsDesc", 1462 "LLVMMipsInfo", 1463 "LLVMLoongArchDisassembler", 1464 "LLVMLoongArchAsmParser", 1465 "LLVMLoongArchCodeGen", 1466 "LLVMLoongArchDesc", 1467 "LLVMLoongArchInfo", 1468 "LLVMLanaiDisassembler", 1469 "LLVMLanaiCodeGen", 1470 "LLVMLanaiAsmParser", 1471 "LLVMLanaiDesc", 1472 "LLVMLanaiInfo", 1473 "LLVMHexagonDisassembler", 1474 "LLVMHexagonCodeGen", 1475 "LLVMHexagonAsmParser", 1476 "LLVMHexagonDesc", 1477 "LLVMHexagonInfo", 1478 "LLVMBPFDisassembler", 1479 "LLVMBPFAsmParser", 1480 "LLVMBPFCodeGen", 1481 "LLVMBPFDesc", 1482 "LLVMBPFInfo", 1483 "LLVMAVRDisassembler", 1484 "LLVMAVRAsmParser", 1485 "LLVMAVRCodeGen", 1486 "LLVMAVRDesc", 1487 "LLVMAVRInfo", 1488 "LLVMARMDisassembler", 1489 "LLVMARMAsmParser", 1490 "LLVMARMCodeGen", 1491 "LLVMARMDesc", 1492 "LLVMARMUtils", 1493 "LLVMARMInfo", 1494 "LLVMAMDGPUTargetMCA", 1495 "LLVMAMDGPUDisassembler", 1496 "LLVMAMDGPUAsmParser", 1497 "LLVMAMDGPUCodeGen", 1498 "LLVMAMDGPUDesc", 1499 "LLVMAMDGPUUtils", 1500 "LLVMAMDGPUInfo", 1501 "LLVMAArch64Disassembler", 1502 "LLVMAArch64AsmParser", 1503 "LLVMAArch64CodeGen", 1504 "LLVMAArch64Desc", 1505 "LLVMAArch64Utils", 1506 "LLVMAArch64Info", 1507 "LLVMOrcDebugging", 1508 "LLVMOrcJIT", 1509 "LLVMWindowsDriver", 1510 "LLVMMCJIT", 1511 "LLVMJITLink", 1512 "LLVMInterpreter", 1513 "LLVMExecutionEngine", 1514 "LLVMRuntimeDyld", 1515 "LLVMOrcTargetProcess", 1516 "LLVMOrcShared", 1517 "LLVMDWP", 1518 "LLVMDWARFCFIChecker", 1519 "LLVMDebugInfoLogicalView", 1520 "LLVMOption", 1521 "LLVMObjCopy", 1522 "LLVMMCA", 1523 "LLVMMCDisassembler", 1524 "LLVMDTLTO", 1525 "LLVMLTO", 1526 "LLVMFrontendOpenACC", 1527 "LLVMFrontendDriver", 1528 "LLVMExtensions", 1529 "LLVMPlugins", 1530 "LLVMPasses", 1531 "LLVMHipStdPar", 1532 "LLVMCoroutines", 1533 "LLVMCFGuard", 1534 "LLVMipo", 1535 "LLVMInstrumentation", 1536 "LLVMVectorize", 1537 "LLVMSandboxIR", 1538 "LLVMLinker", 1539 "LLVMFrontendOpenMP", 1540 "LLVMFrontendDirective", 1541 "LLVMFrontendAtomic", 1542 "LLVMFrontendOffloading", 1543 "LLVMObjectYAML", 1544 "LLVMDWARFLinkerParallel", 1545 "LLVMDWARFLinkerClassic", 1546 "LLVMDWARFLinker", 1547 "LLVMGlobalISel", 1548 "LLVMMIRParser", 1549 "LLVMAsmPrinter", 1550 "LLVMSelectionDAG", 1551 "LLVMCodeGen", 1552 "LLVMTarget", 1553 "LLVMObjCARCOpts", 1554 "LLVMCodeGenTypes", 1555 "LLVMCGData", 1556 "LLVMCAS", 1557 "LLVMIRPrinter", 1558 "LLVMInterfaceStub", 1559 "LLVMFileCheck", 1560 "LLVMFuzzMutate", 1561 "LLVMScalarOpts", 1562 "LLVMInstCombine", 1563 "LLVMAggressiveInstCombine", 1564 "LLVMTransformUtils", 1565 "LLVMBitWriter", 1566 "LLVMAnalysis", 1567 "LLVMProfileData", 1568 "LLVMSymbolize", 1569 "LLVMDebugInfoBTF", 1570 "LLVMDebugInfoPDB", 1571 "LLVMDebugInfoMSF", 1572 "LLVMDebugInfoCodeView", 1573 "LLVMDebugInfoGSYM", 1574 "LLVMDebugInfoDWARF", 1575 "LLVMObject", 1576 "LLVMTextAPI", 1577 "LLVMMCParser", 1578 "LLVMIRReader", 1579 "LLVMAsmParser", 1580 "LLVMMC", 1581 "LLVMDebugInfoDWARFLowLevel", 1582 "LLVMBitReader", 1583 "LLVMFrontendHLSL", 1584 "LLVMFuzzerCLI", 1585 "LLVMABI", 1586 "LLVMCore", 1587 "LLVMRemarks", 1588 "LLVMBitstreamReader", 1589 "LLVMBinaryFormat", 1590 "LLVMTargetParser", 1591 "LLVMSupport", 1592 "LLVMDemangle", 1593 }; 1594 const llvm_libs_m68k = [_][]const u8{ 1595 "LLVMM68kDisassembler", 1596 "LLVMM68kAsmParser", 1597 "LLVMM68kCodeGen", 1598 "LLVMM68kDesc", 1599 "LLVMM68kInfo", 1600 }; 1601 const llvm_libs_csky = [_][]const u8{ 1602 "LLVMCSKYDisassembler", 1603 "LLVMCSKYAsmParser", 1604 "LLVMCSKYCodeGen", 1605 "LLVMCSKYDesc", 1606 "LLVMCSKYInfo", 1607 }; 1608 const llvm_libs_arc = [_][]const u8{ 1609 "LLVMARCDisassembler", 1610 "LLVMARCCodeGen", 1611 "LLVMARCDesc", 1612 "LLVMARCInfo", 1613 }; 1614 const llvm_libs_xtensa = [_][]const u8{ 1615 "LLVMXtensaDisassembler", 1616 "LLVMXtensaAsmParser", 1617 "LLVMXtensaCodeGen", 1618 "LLVMXtensaDesc", 1619 "LLVMXtensaInfo", 1620 }; 1621 1622 fn generateLangRef(b: *std.Build) !std.Build.LazyPath { 1623 const io = b.graph.io; 1624 const arena = b.graph.arena; 1625 1626 const doctest_exe = b.addExecutable(.{ 1627 .name = "doctest", 1628 .root_module = b.createModule(.{ 1629 .root_source_file = b.path("tools/doctest.zig"), 1630 .target = b.graph.host, 1631 .optimize = .debug, 1632 }), 1633 }); 1634 1635 const langref_path = try b.root.join(arena, "doc/langref"); 1636 1637 var dir = langref_path.root_dir.handle.openDir(io, langref_path.sub_path, .{ .iterate = true }) catch |err| 1638 std.debug.panic("unable to open directory {f}: {t}", .{ langref_path, err }); 1639 defer dir.close(io); 1640 1641 var wf = b.addWriteFiles(); 1642 b.step("test-docs", "Test code snippets from the docs").dependOn(&wf.step); 1643 1644 var it = dir.iterateAssumeFirstIteration(); 1645 while (it.next(io) catch @panic("failed to read dir")) |entry| { 1646 if (entry.kind != .file) continue; 1647 if (std.mem.startsWith(u8, entry.name, ".")) continue; 1648 if (!std.mem.endsWith(u8, entry.name, ".zig")) continue; 1649 1650 const out_basename = b.fmt("{s}.out", .{std.fs.path.stem(entry.name)}); 1651 const cmd = b.addRunArtifact(doctest_exe); 1652 1653 cmd.addArg("--zig"); 1654 cmd.addFileArg(.zig_exe); 1655 1656 cmd.addArg("--cache-root"); 1657 cmd.addDirectoryArg(.cache_root); 1658 1659 cmd.addArg("--zig-lib-dir"); 1660 cmd.addDirectoryArg(.zig_lib); 1661 1662 cmd.addArg("-i"); 1663 cmd.addFileArg(b.path(b.fmt("doc/langref/{s}", .{entry.name}))); 1664 1665 cmd.addArg("-o"); 1666 _ = wf.addCopyFile(cmd.addOutputFileArg(out_basename), out_basename); 1667 } 1668 1669 const docgen_exe = b.addExecutable(.{ 1670 .name = "docgen", 1671 .root_module = b.createModule(.{ 1672 .root_source_file = b.path("tools/docgen.zig"), 1673 .target = b.graph.host, 1674 .optimize = .debug, 1675 }), 1676 }); 1677 1678 const docgen_cmd = b.addRunArtifact(docgen_exe); 1679 docgen_cmd.addArgs(&.{"--code-dir"}); 1680 docgen_cmd.addDirectoryArg(wf.getDirectory()); 1681 docgen_cmd.addArgs(&.{"--grammar"}); 1682 docgen_cmd.addFileArg(b.path("doc/langref/grammar.peg")); 1683 1684 docgen_cmd.addFileArg(b.path("doc/langref.html.in")); 1685 return docgen_cmd.addOutputFileArg("langref.html"); 1686 } 1687 1688 fn superHtmlCheck(b: *std.Build, html_file: std.Build.LazyPath) *std.Build.Step { 1689 const run_superhtml = b.addSystemCommand(&.{ 1690 "superhtml", "check", 1691 }); 1692 run_superhtml.addFileArg(html_file); 1693 run_superhtml.expectExitCode(0); 1694 return &run_superhtml.step; 1695 } 1696