| 1 | pub const Env = enum { |
| 2 | /// zig1 features |
| 3 | /// - `-ofmt=c` only |
| 4 | /// - `-OReleaseFast` or `-OReleaseSmall` only |
| 5 | /// - no `@setRuntimeSafety(true)` |
| 6 | bootstrap, |
| 7 | |
| 8 | /// zig2 features |
| 9 | core, |
| 10 | |
| 11 | /// stage3 features |
| 12 | full, |
| 13 | |
| 14 | /// - `zig cc` |
| 15 | /// - `zig c++` |
| 16 | /// - `zig translate-c` |
| 17 | c_source, |
| 18 | |
| 19 | /// - `zig ast-check` |
| 20 | /// - `zig changelist` |
| 21 | /// - `zig dump-zir` |
| 22 | ast_gen, |
| 23 | |
| 24 | /// - ast_gen |
| 25 | /// - `zig build-* -fno-emit-bin` |
| 26 | sema, |
| 27 | |
| 28 | /// - sema |
| 29 | /// - `zig build-* -fincremental -fno-llvm -fno-lld -target aarch64-linux --listen=-` |
| 30 | @"aarch64-linux", |
| 31 | |
| 32 | /// - `zig build-* -ofmt=c` |
| 33 | cbe, |
| 34 | |
| 35 | /// - sema |
| 36 | /// - `zig build-* -fincremental -fno-llvm -fno-lld -target powerpc(64)(le)-linux --listen=-` |
| 37 | @"powerpc-linux", |
| 38 | |
| 39 | /// - sema |
| 40 | /// - `zig build-* -fno-llvm -fno-lld -target riscv64-linux` |
| 41 | @"riscv64-linux", |
| 42 | |
| 43 | /// - sema |
| 44 | /// - `zig build-* -fno-llvm -fno-lld -target spirv(32/64)-* --listen=-` |
| 45 | spirv, |
| 46 | |
| 47 | /// - sema |
| 48 | /// - `zig build-* -fno-llvm -fno-lld -target wasm32-* --listen=-` |
| 49 | wasm, |
| 50 | |
| 51 | /// - sema |
| 52 | /// - `zig build-* -fincremental -fno-llvm -fno-lld -target x86_64-linux --listen=-` |
| 53 | @"x86_64-linux", |
| 54 | |
| 55 | /// - sema |
| 56 | /// - `zig build-* -fincremental -fno-llvm -fno-lld -target x86_64-windows --listen=-` |
| 57 | @"x86_64-windows", |
| 58 | |
| 59 | /// - sema |
| 60 | /// - `zig build-* -fincremental -fno-llvm -fno-lld -target loongarch(32/64)-linux --listen=-` |
| 61 | @"loongarch-linux", |
| 62 | |
| 63 | /// - sema |
| 64 | /// - `zig build-* -fno-llvm -fno-lld -target spork8-* --listen=-` |
| 65 | spork8, |
| 66 | |
| 67 | pub inline fn supports(comptime dev_env: Env, comptime feature: Feature) bool { |
| 68 | return switch (dev_env) { |
| 69 | .full => true, |
| 70 | .bootstrap => switch (feature) { |
| 71 | .build_exe_command, |
| 72 | .build_obj_command, |
| 73 | .ast_gen, |
| 74 | .sema, |
| 75 | .c_backend, |
| 76 | .c_linker, |
| 77 | => true, |
| 78 | else => false, |
| 79 | }, |
| 80 | .core => switch (feature) { |
| 81 | .build_exe_command, |
| 82 | .build_lib_command, |
| 83 | .build_obj_command, |
| 84 | .test_command, |
| 85 | .run_command, |
| 86 | .ar_command, |
| 87 | .clang_command, |
| 88 | .stdio_listen, |
| 89 | .build_import_lib, |
| 90 | .make_executable, |
| 91 | .make_writable, |
| 92 | .incremental, |
| 93 | .ast_gen, |
| 94 | .sema, |
| 95 | .legalize, |
| 96 | .c_compiler, |
| 97 | .llvm_backend, |
| 98 | .c_backend, |
| 99 | .wasm_backend, |
| 100 | .arm_backend, |
| 101 | .x86_64_backend, |
| 102 | .aarch64_backend, |
| 103 | .x86_backend, |
| 104 | .powerpc_backend, |
| 105 | .riscv64_backend, |
| 106 | .sparc64_backend, |
| 107 | .spirv_backend, |
| 108 | .loongarch_backend, |
| 109 | .spork8_backend, |
| 110 | .lld_linker, |
| 111 | .coff_linker, |
| 112 | .coff2_linker, |
| 113 | .elf_linker, |
| 114 | .elf2_linker, |
| 115 | .macho_linker, |
| 116 | .c_linker, |
| 117 | .wasm_linker, |
| 118 | .spirv_linker, |
| 119 | .plan9_linker, |
| 120 | .spork8_linker, |
| 121 | .jit_command, |
| 122 | => true, |
| 123 | .cc_command, |
| 124 | .translate_c_command, |
| 125 | .fmt_command, |
| 126 | .init_command, |
| 127 | .targets_command, |
| 128 | .version_command, |
| 129 | .env_command, |
| 130 | .zen_command, |
| 131 | .help_command, |
| 132 | .ast_check_command, |
| 133 | .detect_cpu_command, |
| 134 | .changelist_command, |
| 135 | .dump_zir_command, |
| 136 | .llvm_ints_command, |
| 137 | .docs_emit, |
| 138 | // Avoid dragging networking into zig2.c because it adds dependencies on some |
| 139 | // linker symbols that are annoying to satisfy while bootstrapping. |
| 140 | .network_listen, |
| 141 | .win32_resource, |
| 142 | => false, |
| 143 | }, |
| 144 | .c_source => switch (feature) { |
| 145 | .clang_command, |
| 146 | .cc_command, |
| 147 | .translate_c_command, |
| 148 | .c_compiler, |
| 149 | => true, |
| 150 | else => false, |
| 151 | }, |
| 152 | .ast_gen => switch (feature) { |
| 153 | .ast_check_command, |
| 154 | .changelist_command, |
| 155 | .dump_zir_command, |
| 156 | .make_executable, |
| 157 | .make_writable, |
| 158 | .incremental, |
| 159 | .ast_gen, |
| 160 | => true, |
| 161 | else => false, |
| 162 | }, |
| 163 | .sema => switch (feature) { |
| 164 | .build_exe_command, |
| 165 | .build_lib_command, |
| 166 | .build_obj_command, |
| 167 | .test_command, |
| 168 | .run_command, |
| 169 | .sema, |
| 170 | => true, |
| 171 | else => Env.ast_gen.supports(feature), |
| 172 | }, |
| 173 | .@"aarch64-linux" => switch (feature) { |
| 174 | .stdio_listen, |
| 175 | .incremental, |
| 176 | .aarch64_backend, |
| 177 | .elf_linker, |
| 178 | .elf2_linker, |
| 179 | => true, |
| 180 | else => Env.sema.supports(feature), |
| 181 | }, |
| 182 | .cbe => switch (feature) { |
| 183 | .legalize, |
| 184 | .c_backend, |
| 185 | .c_linker, |
| 186 | => true, |
| 187 | else => Env.sema.supports(feature), |
| 188 | }, |
| 189 | .@"powerpc-linux" => switch (feature) { |
| 190 | .stdio_listen, |
| 191 | .incremental, |
| 192 | .x86_64_backend, |
| 193 | .elf_linker, |
| 194 | => true, |
| 195 | else => Env.sema.supports(feature), |
| 196 | }, |
| 197 | .@"riscv64-linux" => switch (feature) { |
| 198 | .riscv64_backend, |
| 199 | .elf_linker, |
| 200 | => true, |
| 201 | else => Env.sema.supports(feature), |
| 202 | }, |
| 203 | .spirv => switch (feature) { |
| 204 | .spirv_backend, |
| 205 | .spirv_linker, |
| 206 | .legalize, |
| 207 | => true, |
| 208 | else => Env.sema.supports(feature), |
| 209 | }, |
| 210 | .wasm => switch (feature) { |
| 211 | .incremental, |
| 212 | .legalize, |
| 213 | .stdio_listen, |
| 214 | .wasm_backend, |
| 215 | .wasm_linker, |
| 216 | => true, |
| 217 | else => Env.sema.supports(feature), |
| 218 | }, |
| 219 | .@"x86_64-linux" => switch (feature) { |
| 220 | .stdio_listen, |
| 221 | .incremental, |
| 222 | .legalize, |
| 223 | .x86_64_backend, |
| 224 | .elf_linker, |
| 225 | .elf2_linker, |
| 226 | => true, |
| 227 | else => Env.sema.supports(feature), |
| 228 | }, |
| 229 | .@"x86_64-windows" => switch (feature) { |
| 230 | .build_command, |
| 231 | .stdio_listen, |
| 232 | .incremental, |
| 233 | .legalize, |
| 234 | .x86_64_backend, |
| 235 | .coff2_linker, |
| 236 | => true, |
| 237 | else => Env.sema.supports(feature), |
| 238 | }, |
| 239 | .@"loongarch-linux" => switch (feature) { |
| 240 | .stdio_listen, |
| 241 | .incremental, |
| 242 | .legalize, |
| 243 | .loongarch_backend, |
| 244 | .elf2_linker, |
| 245 | => true, |
| 246 | else => Env.sema.supports(feature), |
| 247 | }, |
| 248 | .spork8 => switch (feature) { |
| 249 | .stdio_listen, |
| 250 | .incremental, |
| 251 | .legalize, |
| 252 | .spork8_backend, |
| 253 | .spork8_linker, |
| 254 | => true, |
| 255 | else => Env.sema.supports(feature), |
| 256 | }, |
| 257 | }; |
| 258 | } |
| 259 | |
| 260 | pub inline fn supportsAny(comptime dev_env: Env, comptime features: []const Feature) bool { |
| 261 | inline for (features) |feature| if (dev_env.supports(feature)) return true; |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | pub inline fn supportsAll(comptime dev_env: Env, comptime features: []const Feature) bool { |
| 266 | inline for (features) |feature| if (!dev_env.supports(feature)) return false; |
| 267 | return true; |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | pub const Feature = enum { |
| 272 | build_exe_command, |
| 273 | build_lib_command, |
| 274 | build_obj_command, |
| 275 | test_command, |
| 276 | run_command, |
| 277 | ar_command, |
| 278 | clang_command, |
| 279 | cc_command, |
| 280 | translate_c_command, |
| 281 | fmt_command, |
| 282 | jit_command, |
| 283 | init_command, |
| 284 | targets_command, |
| 285 | version_command, |
| 286 | env_command, |
| 287 | zen_command, |
| 288 | help_command, |
| 289 | ast_check_command, |
| 290 | detect_cpu_command, |
| 291 | changelist_command, |
| 292 | dump_zir_command, |
| 293 | llvm_ints_command, |
| 294 | |
| 295 | docs_emit, |
| 296 | stdio_listen, |
| 297 | network_listen, |
| 298 | build_import_lib, |
| 299 | win32_resource, |
| 300 | make_executable, |
| 301 | make_writable, |
| 302 | incremental, |
| 303 | ast_gen, |
| 304 | sema, |
| 305 | legalize, |
| 306 | |
| 307 | c_compiler, |
| 308 | |
| 309 | llvm_backend, |
| 310 | c_backend, |
| 311 | wasm_backend, |
| 312 | arm_backend, |
| 313 | x86_64_backend, |
| 314 | aarch64_backend, |
| 315 | x86_backend, |
| 316 | powerpc_backend, |
| 317 | riscv64_backend, |
| 318 | sparc64_backend, |
| 319 | spirv_backend, |
| 320 | loongarch_backend, |
| 321 | spork8_backend, |
| 322 | |
| 323 | lld_linker, |
| 324 | coff_linker, |
| 325 | coff2_linker, |
| 326 | elf_linker, |
| 327 | elf2_linker, |
| 328 | macho_linker, |
| 329 | c_linker, |
| 330 | wasm_linker, |
| 331 | spirv_linker, |
| 332 | plan9_linker, |
| 333 | spork8_linker, |
| 334 | }; |
| 335 | |
| 336 | /// Makes the code following the call to this function unreachable if `feature` is disabled. |
| 337 | pub fn check(comptime feature: Feature) if (env.supports(feature)) void else noreturn { |
| 338 | if (env.supports(feature)) return; |
| 339 | @panic("development environment " ++ @tagName(env) ++ " does not support feature " ++ @tagName(feature)); |
| 340 | } |
| 341 | |
| 342 | /// Makes the code following the call to this function unreachable if all of `features` are disabled. |
| 343 | pub fn checkAny(comptime features: []const Feature) if (env.supportsAny(features)) void else noreturn { |
| 344 | if (env.supportsAny(features)) return; |
| 345 | comptime var feature_tags: []const u8 = ""; |
| 346 | inline for (features[0 .. features.len - 1]) |feature| feature_tags = feature_tags ++ @tagName(feature) ++ ", "; |
| 347 | feature_tags = feature_tags ++ "or " ++ @tagName(features[features.len - 1]); |
| 348 | @panic("development environment " ++ @tagName(env) ++ " does not support feature " ++ feature_tags); |
| 349 | } |
| 350 | |
| 351 | /// Makes the code following the call to this function unreachable if any of `features` are disabled. |
| 352 | pub fn checkAll(comptime features: []const Feature) if (env.supportsAll(features)) void else noreturn { |
| 353 | if (env.supportsAll(features)) return; |
| 354 | inline for (features) |feature| if (!env.supports(feature)) |
| 355 | @panic("development environment " ++ @tagName(env) ++ " does not support feature " ++ @tagName(feature)); |
| 356 | } |
| 357 | |
| 358 | const build_options = @import("build_options"); |
| 359 | |
| 360 | pub const env: Env = if (@hasDecl(build_options, "dev")) |
| 361 | @field(Env, @tagName(build_options.dev)) |
| 362 | else if (@hasDecl(build_options, "only_c") and build_options.only_c) |
| 363 | .bootstrap |
| 364 | else if (@hasDecl(build_options, "only_core_functionality") and build_options.only_core_functionality) |
| 365 | .core |
| 366 | else |
| 367 | .full; |