diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-12-03 17:26:26 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-12-03 17:26:26 -0500 |
| commit | 38791ac616069963fd808ec724161b93cbc564c1 (patch) | |
| tree | 1f4c254a76a3c5d016574c1e124a392973383964 /src/codegen.cpp | |
| parent | fd7c7be33c95fd1bd77010378b407fc9fb4933e2 (diff) | |
| parent | 521744bb91b486bfab7d9a6bd74dd658da95ea18 (diff) | |
| download | zig-38791ac616069963fd808ec724161b93cbc564c1.tar.gz zig-38791ac616069963fd808ec724161b93cbc564c1.zip | |
Merge branch 'Vexu-build-start'
closes #3810
closes #3793
closes #3798
Diffstat (limited to 'src/codegen.cpp')
| -rw-r--r-- | src/codegen.cpp | 63 |
1 files changed, 21 insertions, 42 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index dc29e8f4b1..324a32485e 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8229,9 +8229,9 @@ TargetSubsystem detect_subsystem(CodeGen *g) { 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) + if (g->have_c_main || g->is_test_build || g->have_winmain_crt_startup) return TargetSubsystemConsole; - if (g->have_winmain || g->have_winmain_crt_startup) + if (g->have_winmain) return TargetSubsystemWindows; } else if (g->zig_target->os == OsUefi) { return TargetSubsystemEfiApplication; @@ -8375,6 +8375,22 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { const char *endian_str = g->is_big_endian ? "Endian.Big" : "Endian.Little"; buf_appendf(contents, "pub const endian = %s;\n", endian_str); } + const char *out_type = nullptr; + switch (g->out_type) { + case OutTypeExe: + out_type = "Exe"; + break; + case OutTypeLib: + out_type = "Lib"; + break; + case OutTypeObj: + case OutTypeUnknown: // This happens when running the `zig builtin` command. + out_type = "Obj"; + break; + } + buf_appendf(contents, "pub const output_mode = OutputMode.%s;\n", out_type); + const char *link_type = g->is_dynamic ? "Dynamic" : "Static"; + buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", link_type); buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build)); buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded)); buf_appendf(contents, "pub const os = Os.%s;\n", cur_os); @@ -8441,6 +8457,8 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_buf(&cache_hash, compiler_id); cache_int(&cache_hash, g->build_mode); cache_bool(&cache_hash, g->strip_debug_symbols); + cache_int(&cache_hash, g->out_type); + cache_bool(&cache_hash, g->is_dynamic); cache_bool(&cache_hash, g->is_test_build); cache_bool(&cache_hash, g->is_single_threaded); cache_int(&cache_hash, g->zig_target->is_native); @@ -9148,38 +9166,6 @@ static Buf *get_resolved_root_src_path(CodeGen *g) { return resolved_path; } -static bool want_startup_code(CodeGen *g) { - // Test builds get handled separately. - if (g->is_test_build) - return false; - - // WASM freestanding can still have an entry point but other freestanding targets do not. - if (g->zig_target->os == OsFreestanding && !target_is_wasm(g->zig_target)) - return false; - - // Declaring certain export functions means skipping the start code - if (g->have_c_main || g->have_winmain || g->have_winmain_crt_startup) - return false; - - // If there is a pub main in the root source file, that means we need start code. - if (g->have_pub_main) { - return true; - } else { - if (g->zig_target->os == OsUefi) - return false; - } - - if (g->out_type == OutTypeExe) { - // For build-exe, we might add start code even though there is no pub main, so that the - // programmer gets the "no pub main" compile error. However if linking libc and there is - // a C source file, that might have main(). - return g->c_source_files.length == 0 || g->libc_link_lib == nullptr; - } - - // For objects and libraries, and we don't have pub main, no start code. - return false; -} - static void gen_root_source(CodeGen *g) { Buf *resolved_path = get_resolved_root_src_path(g); if (resolved_path == nullptr) @@ -9241,21 +9227,14 @@ static void gen_root_source(CodeGen *g) { assert(g->panic_fn != nullptr); } - if (!g->error_during_imports) { semantic_analyze(g); } report_errors_and_maybe_exit(g); - if (want_startup_code(g)) { + if (!g->is_test_build) { g->start_import = add_special_code(g, create_start_pkg(g, g->root_package), "start.zig"); } - if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup && - g->out_type == OutTypeLib && g->is_dynamic) - { - g->start_import = add_special_code(g, create_start_pkg(g, g->root_package), "start_lib.zig"); - } - if (!g->error_during_imports) { semantic_analyze(g); } |
