| ... | @@ -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 | } |
| 8608 | | 8608 | |
| | 8609 | static 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 | |
| 8609 | static void gen_root_source(CodeGen *g) { | 8641 | static 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); |
| 8648 | | 8680 | |
| 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 && |