authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 11:56:36-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-29 16:55:02-04:00
log1ab0ac3ea2a187648b9d13de7f3e0bd1c7c4bf4a
treebd1b8a2c5a0a2c05901def632dcec6bf6a14de10
parentb461e600e2cb4d2f5a1f8b15003d7b7a5e397482
signaturelock-open Commit is signed but in an unrecognized format.

cleanups for windows subsystem in builtin.zig


6 files changed, 55 insertions(+), 43 deletions(-)

src/all_types.hpp+1-1
......@@ -1857,7 +1857,7 @@ struct CodeGen {
18571857 BuildMode build_mode;
18581858 OutType out_type;
18591859 const ZigTarget *zig_target;
1860 TargetSubsystem subsystem;
1860 TargetSubsystem subsystem; // careful using this directly; see detect_subsystem
18611861 ValgrindSupport valgrind_support;
18621862 bool strip_debug_symbols;
18631863 bool is_test_build;
src/analyze.cpp-3
......@@ -2722,12 +2722,10 @@ void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, Buf *symbol_name, GlobalLi
27222722 if (ccc) {
27232723 if (buf_eql_str(symbol_name, "main") && g->libc_link_lib != nullptr) {
27242724 g->have_c_main = true;
2725 g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem;
27262725 } else if (buf_eql_str(symbol_name, "WinMain") &&
27272726 g->zig_target->os == OsWindows)
27282727 {
27292728 g->have_winmain = true;
2730 g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemWindows : g->subsystem;
27312729 } else if (buf_eql_str(symbol_name, "WinMainCRTStartup") &&
27322730 g->zig_target->os == OsWindows)
27332731 {
......@@ -3966,7 +3964,6 @@ ZigType *add_source_file(CodeGen *g, ZigPackage *package, Buf *resolved_path, Bu
39663964 if (is_pub) {
39673965 if (buf_eql_str(proto_name, "main")) {
39683966 g->have_pub_main = true;
3969 g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem;
39703967 } else if (buf_eql_str(proto_name, "panic")) {
39713968 g->have_pub_panic = true;
39723969 }
src/codegen.cpp+45-36
......@@ -7417,6 +7417,21 @@ static const char *build_mode_to_str(BuildMode build_mode) {
74177417 zig_unreachable();
74187418}
74197419
7420static 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
74207435static bool detect_dynamic_link(CodeGen *g) {
74217436 if (g->is_dynamic)
74227437 return true;
......@@ -7462,6 +7477,23 @@ static bool detect_stack_probing(CodeGen *g) {
74627477 zig_unreachable();
74637478}
74647479
7480// Returns TargetSubsystemAuto to mean "no subsystem"
7481TargetSubsystem 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
74657497static bool detect_single_threaded(CodeGen *g) {
74667498 if (g->want_single_threaded)
74677499 return true;
......@@ -7882,14 +7914,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
78827914 " EfiRuntimeDriver,\n"
78837915 "};\n\n");
78847916
7885 assert(TargetSubsystemConsole == 1);
7886 assert(TargetSubsystemWindows == 2);
7887 assert(TargetSubsystemPosix == 3);
7888 assert(TargetSubsystemNative == 4);
7889 assert(TargetSubsystemEfiApplication == 5);
7890 assert(TargetSubsystemEfiBootServiceDriver == 6);
7891 assert(TargetSubsystemEfiRom == 7);
7892 assert(TargetSubsystemEfiRuntimeDriver == 8);
7917 assert(TargetSubsystemConsole == 0);
7918 assert(TargetSubsystemWindows == 1);
7919 assert(TargetSubsystemPosix == 2);
7920 assert(TargetSubsystemNative == 3);
7921 assert(TargetSubsystemEfiApplication == 4);
7922 assert(TargetSubsystemEfiBootServiceDriver == 5);
7923 assert(TargetSubsystemEfiRom == 6);
7924 assert(TargetSubsystemEfiRuntimeDriver == 7);
78937925 }
78947926 {
78957927 const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little";
......@@ -7908,29 +7940,10 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
79087940 buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic));
79097941
79107942 {
7911 static const char* subsystem_strings[] = {
7912 "Console",
7913 "Windows",
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]);
7943 TargetSubsystem detected_subsystem = detect_subsystem(g);
7944 if (detected_subsystem != TargetSubsystemAuto) {
7945 buf_appendf(contents, "pub const subsystem = SubSystem.%s;\n", subsystem_to_str(detected_subsystem));
79327946 }
7933
79347947 }
79357948
79367949 if (g->is_test_build) {
......@@ -7976,7 +7989,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
79767989 cache_bool(&cache_hash, g->have_err_ret_tracing);
79777990 cache_bool(&cache_hash, g->libc_link_lib != nullptr);
79787991 cache_bool(&cache_hash, g->valgrind_support);
7979 cache_int(&cache_hash, g->subsystem - 1);
7992 cache_int(&cache_hash, detect_subsystem(g));
79807993
79817994 Buf digest = BUF_INIT;
79827995 buf_resize(&digest, 0);
......@@ -8044,10 +8057,6 @@ static void init(CodeGen *g) {
80448057 g->is_single_threaded = true;
80458058 }
80468059
8047 if (g->is_test_build) {
8048 g->subsystem = g->subsystem == TargetSubsystemAuto ? TargetSubsystemConsole : g->subsystem;
8049 }
8050
80518060 assert(g->root_out_name);
80528061 g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));
80538062
......@@ -9401,7 +9410,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
94019410 cache_int(ch, g->zig_target->vendor);
94029411 cache_int(ch, g->zig_target->os);
94039412 cache_int(ch, g->zig_target->abi);
9404 cache_int(ch, g->subsystem);
9413 cache_int(ch, detect_subsystem(g));
94059414 cache_bool(ch, g->strip_debug_symbols);
94069415 cache_bool(ch, g->is_test_build);
94079416 if (g->is_test_build) {
src/codegen.hpp+2
......@@ -56,4 +56,6 @@ void codegen_translate_c(CodeGen *g, Buf *full_path, FILE *out_file, bool use_us
5656
5757Buf *codegen_generate_builtin_source(CodeGen *g);
5858
59TargetSubsystem detect_subsystem(CodeGen *g);
60
5961#endif
src/link.cpp+2-2
......@@ -1225,7 +1225,7 @@ static void add_mingw_link_args(LinkJob *lj, bool is_library) {
12251225 lj->args.append(get_libc_file(g->libc, "libmingwex.a"));
12261226 lj->args.append(get_libc_file(g->libc, "libmsvcrt.a"));
12271227
1228 if (g->subsystem == TargetSubsystemWindows) {
1228 if (detect_subsystem(g) == TargetSubsystemWindows) {
12291229 lj->args.append(get_libc_file(g->libc, "libgdi32.a"));
12301230 lj->args.append(get_libc_file(g->libc, "libcomdlg32.a"));
12311231 }
......@@ -1307,7 +1307,7 @@ static void construct_linker_job_coff(LinkJob *lj) {
13071307 lj->args.append((const char *)buf_ptr(g->link_objects.at(i)));
13081308 }
13091309
1310 switch (g->subsystem) {
1310 switch (detect_subsystem(g)) {
13111311 case TargetSubsystemAuto:
13121312 if (g->zig_target->os == OsUefi) {
13131313 add_uefi_link_args(lj);
src/target.hpp+5-1
......@@ -62,7 +62,6 @@ enum SubArchList {
6262};
6363
6464enum TargetSubsystem {
65 TargetSubsystemAuto, // Zig should infer the subsystem
6665 TargetSubsystemConsole,
6766 TargetSubsystemWindows,
6867 TargetSubsystemPosix,
......@@ -71,6 +70,11 @@ enum TargetSubsystem {
7170 TargetSubsystemEfiBootServiceDriver,
7271 TargetSubsystemEfiRom,
7372 TargetSubsystemEfiRuntimeDriver,
73
74 // This means Zig should infer the subsystem.
75 // It's last so that the indexes of other items can line up
76 // with the enum in builtin.zig.
77 TargetSubsystemAuto
7478};
7579
7680struct ZigTarget {