From fb5dc2892171b4f9f14460844b67163b5aa4d269 Mon Sep 17 00:00:00 2001 From: emekoi Date: Thu, 9 May 2019 18:01:12 -0500 Subject: [PATCH 1/5] added subsystem to builtin.zig --- src/codegen.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/codegen.cpp b/src/codegen.cpp index 7d9e03baf..46dc0df0e 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7869,6 +7869,30 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { //assert(EndianBig == 0); //assert(EndianLittle == 1); } + { + buf_appendf(contents, + "pub const SubSystem = enum {\n" + " Auto,\n" + " Console,\n" + " Windows,\n" + " Posix,\n" + " Native,\n" + " EfiApplication,\n" + " EfiBootServiceDriver,\n" + " EfiRom,\n" + " EfiRuntimeDriver,\n" + "};\n\n"); + + assert(TargetSubsystemAuto == 0); + assert(TargetSubsystemConsole == 1); + assert(TargetSubsystemWindows == 2); + assert(TargetSubsystemPosix == 3); + assert(TargetSubsystemNative == 4); + assert(TargetSubsystemEfiApplication == 5); + assert(TargetSubsystemEfiBootServiceDriver == 6); + assert(TargetSubsystemEfiRom == 7); + assert(TargetSubsystemEfiRuntimeDriver == 8); + } { const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; buf_appendf(contents, "pub const endian = %s;\n", endian_str); @@ -7885,6 +7909,21 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g))); buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); + { + static const char* subsystem_strings[] = { + "Auto", + "Console", + "Windows", + "Posix", + "Native", + "EfiApplication", + "EfiBootServiceDriver", + "EfiRom", + "EfiRuntimeDriver", + }; + buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem]); + } + if (g->is_test_build) { buf_appendf(contents, "const TestFn = struct {\n" @@ -7928,6 +7967,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_bool(&cache_hash, g->have_err_ret_tracing); cache_bool(&cache_hash, g->libc_link_lib != nullptr); cache_bool(&cache_hash, g->valgrind_support); + cache_int(&cache_hash, g->subsystem); Buf digest = BUF_INIT; buf_resize(&digest, 0); From 6bc5b07e3e24871fc81ddd5a3780523bf95cf8d5 Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 10 May 2019 09:48:32 -0500 Subject: [PATCH 2/5] try to resolve TargetSubSystemAuto to actual subsystem --- src/codegen.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 46dc0df0e..b401c9d97 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7872,7 +7872,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { { buf_appendf(contents, "pub const SubSystem = enum {\n" - " Auto,\n" " Console,\n" " Windows,\n" " Posix,\n" @@ -7883,7 +7882,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { " EfiRuntimeDriver,\n" "};\n\n"); - assert(TargetSubsystemAuto == 0); assert(TargetSubsystemConsole == 1); assert(TargetSubsystemWindows == 2); assert(TargetSubsystemPosix == 3); @@ -7911,7 +7909,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { { static const char* subsystem_strings[] = { - "Auto", "Console", "Windows", "Posix", @@ -7921,7 +7918,19 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { "EfiRom", "EfiRuntimeDriver", }; - buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem]); + + if (g->subsystem == TargetSubsystemAuto) { + if (g->have_c_main || g->have_pub_main) { + buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemConsole - 1]); + } else if (g->have_winmain || g->have_winmain_crt_startup) { + buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemWindows - 1]); + } else if (g->have_dllmain_crt_startup || g->out_type == OutTypeLib) { + buf_appendf(contents, "pub const subsystem = null;\n"); + } + } else { + buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem - 1]); + } + } if (g->is_test_build) { @@ -7967,7 +7976,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_bool(&cache_hash, g->have_err_ret_tracing); cache_bool(&cache_hash, g->libc_link_lib != nullptr); cache_bool(&cache_hash, g->valgrind_support); - cache_int(&cache_hash, g->subsystem); + cache_int(&cache_hash, g->subsystem - 1); Buf digest = BUF_INIT; buf_resize(&digest, 0); From b461e600e2cb4d2f5a1f8b15003d7b7a5e397482 Mon Sep 17 00:00:00 2001 From: emekoi Date: Fri, 10 May 2019 09:57:02 -0500 Subject: [PATCH 3/5] set subsystem to null if not on windows or uefi --- src/codegen.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index b401c9d97..ef3851006 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7919,13 +7919,13 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { "EfiRuntimeDriver", }; - if (g->subsystem == TargetSubsystemAuto) { + if (g->zig_target->os != OsWindows || g->zig_target->os != OsUefi || g->have_dllmain_crt_startup || g->out_type == OutTypeLib) { + buf_appendf(contents, "pub const subsystem = null;\n"); + } else if (g->subsystem == TargetSubsystemAuto) { if (g->have_c_main || g->have_pub_main) { buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemConsole - 1]); } else if (g->have_winmain || g->have_winmain_crt_startup) { buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemWindows - 1]); - } else if (g->have_dllmain_crt_startup || g->out_type == OutTypeLib) { - buf_appendf(contents, "pub const subsystem = null;\n"); } } else { buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem - 1]); From 1ab0ac3ea2a187648b9d13de7f3e0bd1c7c4bf4a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 29 May 2019 11:56:36 -0400 Subject: [PATCH 4/5] cleanups for windows subsystem in builtin.zig --- src/all_types.hpp | 2 +- src/analyze.cpp | 3 -- src/codegen.cpp | 81 ++++++++++++++++++++++++++--------------------- src/codegen.hpp | 2 ++ src/link.cpp | 4 +-- src/target.hpp | 6 +++- 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index 91676d0c3..4bb76ff64 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -1857,7 +1857,7 @@ struct CodeGen { BuildMode build_mode; OutType out_type; const ZigTarget *zig_target; - TargetSubsystem subsystem; + TargetSubsystem subsystem; // careful using this directly; see detect_subsystem ValgrindSupport valgrind_support; bool strip_debug_symbols; bool is_test_build; diff --git a/src/analyze.cpp b/src/analyze.cpp index 182bcc93a..3769ae47c 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2722,12 +2722,10 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLi if (ccc) { if (buf_eql_str(symbol_name, "main") && g->libc_link_lib != nullptr) { g->have_c_main = true; - g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem; } else if (buf_eql_str(symbol_name, "WinMain") && g->zig_target->os == OsWindows) { g->have_winmain = true; - g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemWindows : g->subsystem; } else if (buf_eql_str(symbol_name, "WinMainCRTStartup") && g->zig_target->os == OsWindows) { @@ -3966,7 +3964,6 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu if (is_pub) { if (buf_eql_str(proto_name, "main")) { g->have_pub_main = true; - g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem; } else if (buf_eql_str(proto_name, "panic")) { g->have_pub_panic = true; } diff --git a/src/codegen.cpp b/src/codegen.cpp index ef3851006..57f895ded 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7417,6 +7417,21 @@ static const char *build_mode_to_str(BuildMode build_mode) { zig_unreachable(); } +static const char *subsystem_to_str(TargetSubsystem subsystem) { + switch (subsystem) { + case TargetSubsystemConsole: return "Console"; + case TargetSubsystemWindows: return "Windows"; + case TargetSubsystemPosix: return "Posix"; + case TargetSubsystemNative: return "Native"; + case TargetSubsystemEfiApplication: return "EfiApplication"; + case TargetSubsystemEfiBootServiceDriver: return "EfiBootServiceDriver"; + case TargetSubsystemEfiRom: return "EfiRom"; + case TargetSubsystemEfiRuntimeDriver: return "EfiRuntimeDriver"; + case TargetSubsystemAuto: zig_unreachable(); + } + zig_unreachable(); +} + static bool detect_dynamic_link(CodeGen *g) { if (g->is_dynamic) return true; @@ -7462,6 +7477,23 @@ static bool detect_stack_probing(CodeGen *g) { zig_unreachable(); } +// Returns TargetSubsystemAuto to mean "no subsystem" +TargetSubsystem detect_subsystem(CodeGen *g) { + if (g->subsystem != TargetSubsystemAuto) + return g->subsystem; + if (g->zig_target->os == OsWindows) { + if (g->have_dllmain_crt_startup || (g->out_type == OutTypeLib && g->is_dynamic)) + return TargetSubsystemAuto; + if (g->have_c_main || g->have_pub_main || g->is_test_build) + return TargetSubsystemConsole; + if (g->have_winmain || g->have_winmain_crt_startup) + return TargetSubsystemWindows; + } else if (g->zig_target->os == OsUefi) { + return TargetSubsystemEfiApplication; + } + return TargetSubsystemAuto; +} + static bool detect_single_threaded(CodeGen *g) { if (g->want_single_threaded) return true; @@ -7882,14 +7914,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { " EfiRuntimeDriver,\n" "};\n\n"); - assert(TargetSubsystemConsole == 1); - assert(TargetSubsystemWindows == 2); - assert(TargetSubsystemPosix == 3); - assert(TargetSubsystemNative == 4); - assert(TargetSubsystemEfiApplication == 5); - assert(TargetSubsystemEfiBootServiceDriver == 6); - assert(TargetSubsystemEfiRom == 7); - assert(TargetSubsystemEfiRuntimeDriver == 8); + assert(TargetSubsystemConsole == 0); + assert(TargetSubsystemWindows == 1); + assert(TargetSubsystemPosix == 2); + assert(TargetSubsystemNative == 3); + assert(TargetSubsystemEfiApplication == 4); + assert(TargetSubsystemEfiBootServiceDriver == 5); + assert(TargetSubsystemEfiRom == 6); + assert(TargetSubsystemEfiRuntimeDriver == 7); } { const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; @@ -7908,29 +7940,10 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); { - static const char* subsystem_strings[] = { - "Console", - "Windows", - "Posix", - "Native", - "EfiApplication", - "EfiBootServiceDriver", - "EfiRom", - "EfiRuntimeDriver", - }; - - if (g->zig_target->os != OsWindows || g->zig_target->os != OsUefi || g->have_dllmain_crt_startup || g->out_type == OutTypeLib) { - buf_appendf(contents, "pub const subsystem = null;\n"); - } else if (g->subsystem == TargetSubsystemAuto) { - if (g->have_c_main || g->have_pub_main) { - buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemConsole - 1]); - } else if (g->have_winmain || g->have_winmain_crt_startup) { - buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[TargetSubsystemWindows - 1]); - } - } else { - buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_strings[g->subsystem - 1]); + TargetSubsystem detected_subsystem = detect_subsystem(g); + if (detected_subsystem != TargetSubsystemAuto) { + buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_to_str(detected_subsystem)); } - } if (g->is_test_build) { @@ -7976,7 +7989,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_bool(&cache_hash, g->have_err_ret_tracing); cache_bool(&cache_hash, g->libc_link_lib != nullptr); cache_bool(&cache_hash, g->valgrind_support); - cache_int(&cache_hash, g->subsystem - 1); + cache_int(&cache_hash, detect_subsystem(g)); Buf digest = BUF_INIT; buf_resize(&digest, 0); @@ -8044,10 +8057,6 @@ static void init(CodeGen *g) { g->is_single_threaded = true; } - if (g->is_test_build) { - g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem; - } - assert(g->root_out_name); g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name)); @@ -9401,7 +9410,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_int(ch, g->zig_target->vendor); cache_int(ch, g->zig_target->os); cache_int(ch, g->zig_target->abi); - cache_int(ch, g->subsystem); + cache_int(ch, detect_subsystem(g)); cache_bool(ch, g->strip_debug_symbols); cache_bool(ch, g->is_test_build); if (g->is_test_build) { diff --git a/src/codegen.hpp b/src/codegen.hpp index 47c0097e4..9a340d720 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -56,4 +56,6 @@ void codegen_translate_c(CodeGen *g, Buf *full_path, FILE *out_file, bool use_us Buf *codegen_generate_builtin_source(CodeGen *g); +TargetSubsystem detect_subsystem(CodeGen *g); + #endif diff --git a/src/link.cpp b/src/link.cpp index 4c3b3d476..f94b1219f 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1225,7 +1225,7 @@ static void add_mingw_link_args(LinkJob *lj, bool is_library) { lj->args.append(get_libc_file(g->libc, "libmingwex.a")); lj->args.append(get_libc_file(g->libc, "libmsvcrt.a")); - if (g->subsystem == TargetSubsystemWindows) { + if (detect_subsystem(g) == TargetSubsystemWindows) { lj->args.append(get_libc_file(g->libc, "libgdi32.a")); lj->args.append(get_libc_file(g->libc, "libcomdlg32.a")); } @@ -1307,7 +1307,7 @@ static void construct_linker_job_coff(LinkJob *lj) { lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); } - switch (g->subsystem) { + switch (detect_subsystem(g)) { case TargetSubsystemAuto: if (g->zig_target->os == OsUefi) { add_uefi_link_args(lj); diff --git a/src/target.hpp b/src/target.hpp index f3cde902a..7fa99bcda 100644 --- a/src/target.hpp +++ b/src/target.hpp @@ -62,7 +62,6 @@ enum SubArchList { }; enum TargetSubsystem { - TargetSubsystemAuto, // Zig should infer the subsystem TargetSubsystemConsole, TargetSubsystemWindows, TargetSubsystemPosix, @@ -71,6 +70,11 @@ enum TargetSubsystem { TargetSubsystemEfiBootServiceDriver, TargetSubsystemEfiRom, TargetSubsystemEfiRuntimeDriver, + + // This means Zig should infer the subsystem. + // It's last so that the indexes of other items can line up + // with the enum in builtin.zig. + TargetSubsystemAuto }; struct ZigTarget { From 3819654c3931b762b0d3dac3b79d58eb791a646d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 29 May 2019 12:08:21 -0400 Subject: [PATCH 5/5] codegen: initialize subsystem --- src/codegen.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/codegen.cpp b/src/codegen.cpp index 57f895ded..fc9cdfc9b 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -100,6 +100,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget codegen_add_time_event(g, "Initialize"); + g->subsystem = TargetSubsystemAuto; g->libc = libc; g->zig_target = target; g->cache_dir = cache_dir;