| ... | @@ -7417,6 +7417,21 @@ static const char *build_mode_to_str(BuildMode build_mode) { | ... | @@ -7417,6 +7417,21 @@ static const char *build_mode_to_str(BuildMode build_mode) { |
| 7417 | zig_unreachable(); | 7417 | zig_unreachable(); |
| 7418 | } | 7418 | } |
| 7419 | | 7419 | |
| | 7420 | static const char *subsystem_to_str(TargetSubsystem subsystem) { |
| | 7421 | switch (subsystem) { |
| | 7422 | case TargetSubsystemConsole: return "Console"; |
| | 7423 | case TargetSubsystemWindows: return "Windows"; |
| | 7424 | case TargetSubsystemPosix: return "Posix"; |
| | 7425 | case TargetSubsystemNative: return "Native"; |
| | 7426 | case TargetSubsystemEfiApplication: return "EfiApplication"; |
| | 7427 | case TargetSubsystemEfiBootServiceDriver: return "EfiBootServiceDriver"; |
| | 7428 | case TargetSubsystemEfiRom: return "EfiRom"; |
| | 7429 | case TargetSubsystemEfiRuntimeDriver: return "EfiRuntimeDriver"; |
| | 7430 | case TargetSubsystemAuto: zig_unreachable(); |
| | 7431 | } |
| | 7432 | zig_unreachable(); |
| | 7433 | } |
| | 7434 | |
| 7420 | static bool detect_dynamic_link(CodeGen *g) { | 7435 | static bool detect_dynamic_link(CodeGen *g) { |
| 7421 | if (g->is_dynamic) | 7436 | if (g->is_dynamic) |
| 7422 | return true; | 7437 | return true; |
| ... | @@ -7462,6 +7477,23 @@ static bool detect_stack_probing(CodeGen *g) { | ... | @@ -7462,6 +7477,23 @@ static bool detect_stack_probing(CodeGen *g) { |
| 7462 | zig_unreachable(); | 7477 | zig_unreachable(); |
| 7463 | } | 7478 | } |
| 7464 | | 7479 | |
| | 7480 | // Returns TargetSubsystemAuto to mean "no subsystem" |
| | 7481 | TargetSubsystem detect_subsystem(CodeGen *g) { |
| | 7482 | if (g->subsystem != TargetSubsystemAuto) |
| | 7483 | return g->subsystem; |
| | 7484 | if (g->zig_target->os == OsWindows) { |
| | 7485 | if (g->have_dllmain_crt_startup || (g->out_type == OutTypeLib && g->is_dynamic)) |
| | 7486 | return TargetSubsystemAuto; |
| | 7487 | if (g->have_c_main || g->have_pub_main || g->is_test_build) |
| | 7488 | return TargetSubsystemConsole; |
| | 7489 | if (g->have_winmain || g->have_winmain_crt_startup) |
| | 7490 | return TargetSubsystemWindows; |
| | 7491 | } else if (g->zig_target->os == OsUefi) { |
| | 7492 | return TargetSubsystemEfiApplication; |
| | 7493 | } |
| | 7494 | return TargetSubsystemAuto; |
| | 7495 | } |
| | 7496 | |
| 7465 | static bool detect_single_threaded(CodeGen *g) { | 7497 | static bool detect_single_threaded(CodeGen *g) { |
| 7466 | if (g->want_single_threaded) | 7498 | if (g->want_single_threaded) |
| 7467 | return true; | 7499 | return true; |
| ... | @@ -7882,14 +7914,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { | ... | @@ -7882,14 +7914,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7882 | " EfiRuntimeDriver,\n" | 7914 | " EfiRuntimeDriver,\n" |
| 7883 | "};\n\n"); | 7915 | "};\n\n"); |
| 7884 | | 7916 | |
| 7885 | assert(TargetSubsystemConsole == 1); | 7917 | assert(TargetSubsystemConsole == 0); |
| 7886 | assert(TargetSubsystemWindows == 2); | 7918 | assert(TargetSubsystemWindows == 1); |
| 7887 | assert(TargetSubsystemPosix == 3); | 7919 | assert(TargetSubsystemPosix == 2); |
| 7888 | assert(TargetSubsystemNative == 4); | 7920 | assert(TargetSubsystemNative == 3); |
| 7889 | assert(TargetSubsystemEfiApplication == 5); | 7921 | assert(TargetSubsystemEfiApplication == 4); |
| 7890 | assert(TargetSubsystemEfiBootServiceDriver == 6); | 7922 | assert(TargetSubsystemEfiBootServiceDriver == 5); |
| 7891 | assert(TargetSubsystemEfiRom == 7); | 7923 | assert(TargetSubsystemEfiRom == 6); |
| 7892 | assert(TargetSubsystemEfiRuntimeDriver == 8); | 7924 | assert(TargetSubsystemEfiRuntimeDriver == 7); |
| 7893 | } | 7925 | } |
| 7894 | { | 7926 | { |
| 7895 | const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; | 7927 | const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; |
| ... | @@ -7908,29 +7940,10 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { | ... | @@ -7908,29 +7940,10 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7908 | buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); | 7940 | buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); |
| 7909 | | 7941 | |
| 7910 | { | 7942 | { |
| 7911 | static const char* subsystem_strings[] = { | 7943 | TargetSubsystem detected_subsystem = detect_subsystem(g); |
| 7912 | "Console", | 7944 | if (detected_subsystem != TargetSubsystemAuto) { |
| 7913 | "Windows", | 7945 | buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_to_str(detected_subsystem)); |
| 7914 | "Posix", | | |
| 7915 | "Native", | | |
| 7916 | "EfiApplication", | | |
| 7917 | "EfiBootServiceDriver", | | |
| 7918 | "EfiRom", | | |
| 7919 | "EfiRuntimeDriver", | | |
| 7920 | }; | | |
| 7921 | | | |
| 7922 | if (g->zig_target->os != OsWindows || g->zig_target->os != OsUefi || g->have_dllmain_crt_startup || g->out_type == OutTypeLib) { | | |
| 7923 | buf_appendf(contents, "pub const subsystem = null;\n"); | | |
| 7924 | } else if (g->subsystem == TargetSubsystemAuto) { | | |
| 7925 | if (g->have_c_main || g->have_pub_main) { | | |
| 7926 | buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemConsole - 1]); | | |
| 7927 | } else if (g->have_winmain || g->have_winmain_crt_startup) { | | |
| 7928 | buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemWindows - 1]); | | |
| 7929 | } | | |
| 7930 | } else { | | |
| 7931 | buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem - 1]); | | |
| 7932 | } | 7946 | } |
| 7933 | | | |
| 7934 | } | 7947 | } |
| 7935 | | 7948 | |
| 7936 | if (g->is_test_build) { | 7949 | if (g->is_test_build) { |
| ... | @@ -7976,7 +7989,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { | ... | @@ -7976,7 +7989,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { |
| 7976 | cache_bool(&cache_hash, g->have_err_ret_tracing); | 7989 | cache_bool(&cache_hash, g->have_err_ret_tracing); |
| 7977 | cache_bool(&cache_hash, g->libc_link_lib != nullptr); | 7990 | cache_bool(&cache_hash, g->libc_link_lib != nullptr); |
| 7978 | cache_bool(&cache_hash, g->valgrind_support); | 7991 | cache_bool(&cache_hash, g->valgrind_support); |
| 7979 | cache_int(&cache_hash, g->subsystem - 1); | 7992 | cache_int(&cache_hash, detect_subsystem(g)); |
| 7980 | | 7993 | |
| 7981 | Buf digest = BUF_INIT; | 7994 | Buf digest = BUF_INIT; |
| 7982 | buf_resize(&digest, 0); | 7995 | buf_resize(&digest, 0); |
| ... | @@ -8044,10 +8057,6 @@ static void init(CodeGen *g) { | ... | @@ -8044,10 +8057,6 @@ static void init(CodeGen *g) { |
| 8044 | g->is_single_threaded = true; | 8057 | g->is_single_threaded = true; |
| 8045 | } | 8058 | } |
| 8046 | | 8059 | |
| 8047 | if (g->is_test_build) { | | |
| 8048 | g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem; | | |
| 8049 | } | | |
| 8050 | | | |
| 8051 | assert(g->root_out_name); | 8060 | assert(g->root_out_name); |
| 8052 | g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name)); | 8061 | g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name)); |
| 8053 | | 8062 | |
| ... | @@ -9401,7 +9410,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { | ... | @@ -9401,7 +9410,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { |
| 9401 | cache_int(ch, g->zig_target->vendor); | 9410 | cache_int(ch, g->zig_target->vendor); |
| 9402 | cache_int(ch, g->zig_target->os); | 9411 | cache_int(ch, g->zig_target->os); |
| 9403 | cache_int(ch, g->zig_target->abi); | 9412 | cache_int(ch, g->zig_target->abi); |
| 9404 | cache_int(ch, g->subsystem); | 9413 | cache_int(ch, detect_subsystem(g)); |
| 9405 | cache_bool(ch, g->strip_debug_symbols); | 9414 | cache_bool(ch, g->strip_debug_symbols); |
| 9406 | cache_bool(ch, g->is_test_build); | 9415 | cache_bool(ch, g->is_test_build); |
| 9407 | if (g->is_test_build) { | 9416 | if (g->is_test_build) { |