authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-02 14:26:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-07-02 14:26:54-04:00
log704444a6e329ee3bb7a99f8b2cc6cebde18b93cd
tree1b19a3dd940a68584141ca2df98d7aff9261789d
parent7d4a0cfed0fe03e3642a9b1518b8fb5b4a302f50
signaturelock-open Commit is signed but in an unrecognized format.

improved logic on whether to include start files


1 files changed, 33 insertions(+), 5 deletions(-)

src/codegen.cpp+33-5
...@@ -8606,6 +8606,38 @@ static Buf *get_resolved_root_src_path(CodeGen *g) {...@@ -8606,6 +8606,38 @@ static Buf *get_resolved_root_src_path(CodeGen *g) {
8606 return resolved_path;8606 return resolved_path;
8607}8607}
86088608
8609static bool want_startup_code(CodeGen *g) {
8610 // Test builds get handled separately.
8611 if (g->is_test_build)
8612 return false;
8613
8614 // start code does not handle UEFI target
8615 if (g->zig_target->os == OsUefi)
8616 return false;
8617
8618 // WASM freestanding can still have an entry point but other freestanding targets do not.
8619 if (g->zig_target->os == OsFreestanding && !target_is_wasm(g->zig_target))
8620 return false;
8621
8622 // Declaring certain export functions means skipping the start code
8623 if (g->have_c_main || g->have_winmain || g->have_winmain_crt_startup)
8624 return false;
8625
8626 // If there is a pub main in the root source file, that means we need start code.
8627 if (g->have_pub_main)
8628 return true;
8629
8630 if (g->out_type == OutTypeExe) {
8631 // For build-exe, we might add start code even though there is no pub main, so that the
8632 // programmer gets the "no pub main" compile error. However if linking libc and there is
8633 // a C source file, that might have main().
8634 return g->c_source_files.length == 0 || g->libc_link_lib == nullptr;
8635 }
8636
8637 // For objects and libraries, and we don't have pub main, no start code.
8638 return false;
8639}
8640
8609static void gen_root_source(CodeGen *g) {8641static void gen_root_source(CodeGen *g) {
8610 Buf *resolved_path = get_resolved_root_src_path(g);8642 Buf *resolved_path = get_resolved_root_src_path(g);
8611 if (resolved_path == nullptr)8643 if (resolved_path == nullptr)
...@@ -8646,11 +8678,7 @@ static void gen_root_source(CodeGen *g) {...@@ -8646,11 +8678,7 @@ static void gen_root_source(CodeGen *g) {
8646 }8678 }
8647 report_errors_and_maybe_exit(g);8679 report_errors_and_maybe_exit(g);
86488680
8649 if (!g->is_test_build && (g->zig_target->os != OsFreestanding || target_is_wasm(g->zig_target)) &&8681 if (want_startup_code(g)) {
8650 g->zig_target->os != OsUefi &&
8651 !g->have_c_main && !g->have_winmain && !g->have_winmain_crt_startup &&
8652 ((g->have_pub_main && g->out_type == OutTypeObj) || g->out_type == OutTypeExe))
8653 {
8654 g->start_import = add_special_code(g, create_start_pkg(g, g->root_package), "start.zig");8682 g->start_import = add_special_code(g, create_start_pkg(g, g->root_package), "start.zig");
8655 }8683 }
8656 if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup &&8684 if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup &&