| author | |
| committer | |
| log | 5d2edac12dc9eec626977a5bf9b0630504b28c15 |
| tree | 07df535ed5e89f616d064ca0facf8941d59681b7 |
| parent | 85d0f0d45bf1529db8965b8176f8021d1ca27534 |
| signature |
`--static` is no longer an option. Instead, Zig makes things as static
as possible by default. `-dynamic` can be used to choose a dynamic
library rather than a static one.
`--enable-pic` is a new option. Usually it will be enabled
automatically, but in the case of build-exe with no dynamic libraries
on Linux or freestanding, Zig chooses off by default.
closes #1703
closes #18289 files changed, 212 insertions(+), 137 deletions(-)
src/all_types.hpp+10-2| ... | @@ -1616,6 +1616,12 @@ enum ValgrindSupport { | ... | @@ -1616,6 +1616,12 @@ enum ValgrindSupport { |
| 1616 | ValgrindSupportEnabled, | 1616 | ValgrindSupportEnabled, |
| 1617 | }; | 1617 | }; |
| 1618 | 1618 | ||
| 1619 | enum WantPIC { | ||
| 1620 | WantPICAuto, | ||
| 1621 | WantPICDisabled, | ||
| 1622 | WantPICEnabled, | ||
| 1623 | }; | ||
| 1624 | |||
| 1619 | struct CFile { | 1625 | struct CFile { |
| 1620 | ZigList<const char *> args; | 1626 | ZigList<const char *> args; |
| 1621 | const char *source_path; | 1627 | const char *source_path; |
| ... | @@ -1791,6 +1797,8 @@ struct CodeGen { | ... | @@ -1791,6 +1797,8 @@ struct CodeGen { |
| 1791 | bool have_dllmain_crt_startup; | 1797 | bool have_dllmain_crt_startup; |
| 1792 | bool have_pub_panic; | 1798 | bool have_pub_panic; |
| 1793 | bool have_err_ret_tracing; | 1799 | bool have_err_ret_tracing; |
| 1800 | bool have_pic; | ||
| 1801 | bool have_dynamic_link; // this is whether the final thing will be dynamically linked. see also is_dynamic | ||
| 1794 | bool c_want_stdint; | 1802 | bool c_want_stdint; |
| 1795 | bool c_want_stdbool; | 1803 | bool c_want_stdbool; |
| 1796 | bool verbose_tokenize; | 1804 | bool verbose_tokenize; |
| ... | @@ -1834,13 +1842,13 @@ struct CodeGen { | ... | @@ -1834,13 +1842,13 @@ struct CodeGen { |
| 1834 | const ZigTarget *zig_target; | 1842 | const ZigTarget *zig_target; |
| 1835 | TargetSubsystem subsystem; | 1843 | TargetSubsystem subsystem; |
| 1836 | ValgrindSupport valgrind_support; | 1844 | ValgrindSupport valgrind_support; |
| 1837 | bool is_static; | 1845 | WantPIC want_pic; |
| 1846 | bool is_dynamic; // shared library rather than static library. dynamic musl rather than static musl. | ||
| 1838 | bool strip_debug_symbols; | 1847 | bool strip_debug_symbols; |
| 1839 | bool is_test_build; | 1848 | bool is_test_build; |
| 1840 | bool is_single_threaded; | 1849 | bool is_single_threaded; |
| 1841 | bool linker_rdynamic; | 1850 | bool linker_rdynamic; |
| 1842 | bool each_lib_rpath; | 1851 | bool each_lib_rpath; |
| 1843 | bool disable_pic; | ||
| 1844 | bool is_dummy_so; | 1852 | bool is_dummy_so; |
| 1845 | bool disable_gen_h; | 1853 | bool disable_gen_h; |
| 1846 | 1854 |
src/codegen.cpp+67-29| ... | @@ -182,13 +182,13 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget | ... | @@ -182,13 +182,13 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget |
| 182 | } else { | 182 | } else { |
| 183 | g->each_lib_rpath = true; | 183 | g->each_lib_rpath = true; |
| 184 | 184 | ||
| 185 | if (target_is_darwin(g->zig_target)) { | 185 | if (target_os_is_darwin(g->zig_target->os)) { |
| 186 | init_darwin_native(g); | 186 | init_darwin_native(g); |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | if (target_requires_libc(g->zig_target)) { | 191 | if (target_os_requires_libc(g->zig_target->os)) { |
| 192 | g->libc_link_lib = create_link_lib(buf_create_from_str("c")); | 192 | g->libc_link_lib = create_link_lib(buf_create_from_str("c")); |
| 193 | g->link_libs_list.append(g->libc_link_lib); | 193 | g->link_libs_list.append(g->libc_link_lib); |
| 194 | } | 194 | } |
| ... | @@ -3370,7 +3370,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default | ... | @@ -3370,7 +3370,7 @@ static LLVMValueRef gen_valgrind_client_request(CodeGen *g, LLVMValueRef default |
| 3370 | bool asm_has_side_effects = true; | 3370 | bool asm_has_side_effects = true; |
| 3371 | bool asm_is_alignstack = false; | 3371 | bool asm_is_alignstack = false; |
| 3372 | if (g->zig_target->arch == ZigLLVM_x86_64) { | 3372 | if (g->zig_target->arch == ZigLLVM_x86_64) { |
| 3373 | if (g->zig_target->os == OsLinux || target_is_darwin(g->zig_target) || g->zig_target->os == OsSolaris || | 3373 | if (g->zig_target->os == OsLinux || target_os_is_darwin(g->zig_target->os) || g->zig_target->os == OsSolaris || |
| 3374 | (g->zig_target->os == OsWindows && g->zig_target->abi != ZigLLVM_MSVC)) | 3374 | (g->zig_target->os == OsWindows && g->zig_target->abi != ZigLLVM_MSVC)) |
| 3375 | { | 3375 | { |
| 3376 | if (g->cur_fn->valgrind_client_request_array == nullptr) { | 3376 | if (g->cur_fn->valgrind_client_request_array == nullptr) { |
| ... | @@ -7283,7 +7283,44 @@ static const char *build_mode_to_str(BuildMode build_mode) { | ... | @@ -7283,7 +7283,44 @@ static const char *build_mode_to_str(BuildMode build_mode) { |
| 7283 | zig_unreachable(); | 7283 | zig_unreachable(); |
| 7284 | } | 7284 | } |
| 7285 | 7285 | ||
| 7286 | static bool detect_dynamic_link(CodeGen *g) { | ||
| 7287 | if (g->is_dynamic) | ||
| 7288 | return true; | ||
| 7289 | if (g->zig_target->os == OsFreestanding) | ||
| 7290 | return false; | ||
| 7291 | if (target_requires_pic(g->zig_target)) | ||
| 7292 | return true; | ||
| 7293 | if (g->out_type == OutTypeExe) { | ||
| 7294 | // If there are no dynamic libraries then we can disable PIC | ||
| 7295 | for (size_t i = 0; i < g->link_libs_list.length; i += 1) { | ||
| 7296 | LinkLib *link_lib = g->link_libs_list.at(i); | ||
| 7297 | if (target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) | ||
| 7298 | continue; | ||
| 7299 | return true; | ||
| 7300 | } | ||
| 7301 | return false; | ||
| 7302 | } | ||
| 7303 | return true; | ||
| 7304 | } | ||
| 7305 | |||
| 7306 | static bool detect_pic(CodeGen *g) { | ||
| 7307 | if (target_requires_pic(g->zig_target)) | ||
| 7308 | return true; | ||
| 7309 | switch (g->want_pic) { | ||
| 7310 | case WantPICDisabled: | ||
| 7311 | return false; | ||
| 7312 | case WantPICEnabled: | ||
| 7313 | return true; | ||
| 7314 | case WantPICAuto: | ||
| 7315 | return g->have_dynamic_link; | ||
| 7316 | } | ||
| 7317 | zig_unreachable(); | ||
| 7318 | } | ||
| 7319 | |||
| 7286 | Buf *codegen_generate_builtin_source(CodeGen *g) { | 7320 | Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7321 | g->have_dynamic_link = detect_dynamic_link(g); | ||
| 7322 | g->have_pic = detect_pic(g); | ||
| 7323 | |||
| 7287 | Buf *contents = buf_alloc(); | 7324 | Buf *contents = buf_alloc(); |
| 7288 | 7325 | ||
| 7289 | // NOTE: when editing this file, you may need to make modifications to the | 7326 | // NOTE: when editing this file, you may need to make modifications to the |
| ... | @@ -7683,6 +7720,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { | ... | @@ -7683,6 +7720,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7683 | buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr)); | 7720 | buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr)); |
| 7684 | buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing)); | 7721 | buf_appendf(contents, "pub const have_error_return_tracing = %s;\n", bool_to_str(g->have_err_ret_tracing)); |
| 7685 | buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g))); | 7722 | buf_appendf(contents, "pub const valgrind_support = %s;\n", bool_to_str(want_valgrind_support(g))); |
| 7723 | buf_appendf(contents, "pub const position_independent_code = %s;\n", bool_to_str(g->have_pic)); | ||
| 7686 | 7724 | ||
| 7687 | buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n"); | 7725 | buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n"); |
| 7688 | 7726 | ||
| ... | @@ -7777,6 +7815,9 @@ static void init(CodeGen *g) { | ... | @@ -7777,6 +7815,9 @@ static void init(CodeGen *g) { |
| 7777 | if (g->module) | 7815 | if (g->module) |
| 7778 | return; | 7816 | return; |
| 7779 | 7817 | ||
| 7818 | g->have_dynamic_link = detect_dynamic_link(g); | ||
| 7819 | g->have_pic = detect_pic(g); | ||
| 7820 | |||
| 7780 | if (g->is_test_build) { | 7821 | if (g->is_test_build) { |
| 7781 | g->subsystem = TargetSubsystemConsole; | 7822 | g->subsystem = TargetSubsystemConsole; |
| 7782 | } | 7823 | } |
| ... | @@ -7807,10 +7848,7 @@ static void init(CodeGen *g) { | ... | @@ -7807,10 +7848,7 @@ static void init(CodeGen *g) { |
| 7807 | bool is_optimized = g->build_mode != BuildModeDebug; | 7848 | bool is_optimized = g->build_mode != BuildModeDebug; |
| 7808 | LLVMCodeGenOptLevel opt_level = is_optimized ? LLVMCodeGenLevelAggressive : LLVMCodeGenLevelNone; | 7849 | LLVMCodeGenOptLevel opt_level = is_optimized ? LLVMCodeGenLevelAggressive : LLVMCodeGenLevelNone; |
| 7809 | 7850 | ||
| 7810 | if (g->out_type == OutTypeExe && g->is_static) { | 7851 | LLVMRelocMode reloc_mode = g->have_pic ? LLVMRelocPIC: LLVMRelocStatic; |
| 7811 | g->disable_pic = true; | ||
| 7812 | } | ||
| 7813 | LLVMRelocMode reloc_mode = g->disable_pic ? LLVMRelocStatic : LLVMRelocPIC; | ||
| 7814 | 7852 | ||
| 7815 | const char *target_specific_cpu_args; | 7853 | const char *target_specific_cpu_args; |
| 7816 | const char *target_specific_features; | 7854 | const char *target_specific_features; |
| ... | @@ -7892,8 +7930,13 @@ static void init(CodeGen *g) { | ... | @@ -7892,8 +7930,13 @@ static void init(CodeGen *g) { |
| 7892 | } | 7930 | } |
| 7893 | 7931 | ||
| 7894 | static void detect_dynamic_linker(CodeGen *g) { | 7932 | static void detect_dynamic_linker(CodeGen *g) { |
| 7895 | if (g->dynamic_linker_path != nullptr || g->is_static) | 7933 | if (g->dynamic_linker_path != nullptr) |
| 7896 | return; | 7934 | return; |
| 7935 | if (!g->have_dynamic_link) | ||
| 7936 | return; | ||
| 7937 | if (g->out_type == OutTypeObj || (g->out_type == OutTypeLib && !g->is_dynamic)) | ||
| 7938 | return; | ||
| 7939 | |||
| 7897 | const char *standard_ld_path = target_dynamic_linker(g->zig_target); | 7940 | const char *standard_ld_path = target_dynamic_linker(g->zig_target); |
| 7898 | if (standard_ld_path == nullptr) | 7941 | if (standard_ld_path == nullptr) |
| 7899 | return; | 7942 | return; |
| ... | @@ -7944,13 +7987,6 @@ static void detect_libc(CodeGen *g) { | ... | @@ -7944,13 +7987,6 @@ static void detect_libc(CodeGen *g) { |
| 7944 | if (g->libc != nullptr || g->libc_link_lib == nullptr) | 7987 | if (g->libc != nullptr || g->libc_link_lib == nullptr) |
| 7945 | return; | 7988 | return; |
| 7946 | 7989 | ||
| 7947 | if (g->zig_target->os == OsLinux && target_abi_is_gnu(g->zig_target->abi) && | ||
| 7948 | g->is_static && g->out_type == OutTypeExe) | ||
| 7949 | { | ||
| 7950 | fprintf(stderr, "glibc does not support static linking\n"); | ||
| 7951 | exit(1); | ||
| 7952 | } | ||
| 7953 | |||
| 7954 | if (target_can_build_libc(g->zig_target)) { | 7990 | if (target_can_build_libc(g->zig_target)) { |
| 7955 | const char *generic_name = target_libc_generic_name(g->zig_target); | 7991 | const char *generic_name = target_libc_generic_name(g->zig_target); |
| 7956 | 7992 | ||
| ... | @@ -8010,17 +8046,16 @@ static void detect_libc(CodeGen *g) { | ... | @@ -8010,17 +8046,16 @@ static void detect_libc(CodeGen *g) { |
| 8010 | if (want_sys_dir) { | 8046 | if (want_sys_dir) { |
| 8011 | g->libc_include_dir_list[1] = &g->libc->sys_include_dir; | 8047 | g->libc_include_dir_list[1] = &g->libc->sys_include_dir; |
| 8012 | } | 8048 | } |
| 8013 | } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) && | 8049 | } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) && |
| 8014 | !target_is_darwin(g->zig_target)) | 8050 | !target_os_is_darwin(g->zig_target->os)) |
| 8015 | { | 8051 | { |
| 8052 | Buf triple_buf = BUF_INIT; | ||
| 8053 | get_target_triple(&triple_buf, g->zig_target); | ||
| 8016 | fprintf(stderr, | 8054 | fprintf(stderr, |
| 8017 | "Zig is unable to provide a libc for the chosen target '%s-%s-%s'.\n" | 8055 | "Zig is unable to provide a libc for the chosen target '%s'.\n" |
| 8018 | "The target is non-native, so Zig also cannot use the native libc installation.\n" | 8056 | "The target is non-native, so Zig also cannot use the native libc installation.\n" |
| 8019 | "Choose a target which has a libc available, or provide a libc installation text file.\n" | 8057 | "Choose a target which has a libc available, or provide a libc installation text file.\n" |
| 8020 | "See `zig libc --help` for more details.\n", | 8058 | "See `zig libc --help` for more details.\n", buf_ptr(&triple_buf)); |
| 8021 | target_arch_name(g->zig_target->arch), | ||
| 8022 | target_os_name(g->zig_target->os), | ||
| 8023 | target_abi_name(g->zig_target->abi)); | ||
| 8024 | exit(1); | 8059 | exit(1); |
| 8025 | } | 8060 | } |
| 8026 | } | 8061 | } |
| ... | @@ -8203,7 +8238,7 @@ static void gen_root_source(CodeGen *g) { | ... | @@ -8203,7 +8238,7 @@ static void gen_root_source(CodeGen *g) { |
| 8203 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); | 8238 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig"); |
| 8204 | } | 8239 | } |
| 8205 | if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup && | 8240 | if (g->zig_target->os == OsWindows && !g->have_dllmain_crt_startup && |
| 8206 | g->out_type == OutTypeLib && !g->is_static) | 8241 | g->out_type == OutTypeLib && g->is_dynamic) |
| 8207 | { | 8242 | { |
| 8208 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); | 8243 | g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap_lib.zig"); |
| 8209 | } | 8244 | } |
| ... | @@ -8297,7 +8332,8 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose | ... | @@ -8297,7 +8332,8 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose |
| 8297 | cache_int(cache_hash, g->zig_target->abi); | 8332 | cache_int(cache_hash, g->zig_target->abi); |
| 8298 | cache_bool(cache_hash, g->strip_debug_symbols); | 8333 | cache_bool(cache_hash, g->strip_debug_symbols); |
| 8299 | cache_int(cache_hash, g->build_mode); | 8334 | cache_int(cache_hash, g->build_mode); |
| 8300 | cache_bool(cache_hash, g->disable_pic); | 8335 | cache_bool(cache_hash, g->have_pic); |
| 8336 | cache_bool(cache_hash, want_valgrind_support(g)); | ||
| 8301 | for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) { | 8337 | for (size_t arg_i = 0; arg_i < g->clang_argv_len; arg_i += 1) { |
| 8302 | cache_str(cache_hash, g->clang_argv[arg_i]); | 8338 | cache_str(cache_hash, g->clang_argv[arg_i]); |
| 8303 | } | 8339 | } |
| ... | @@ -8451,7 +8487,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) { | ... | @@ -8451,7 +8487,7 @@ static void gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) { |
| 8451 | args.append("-c"); | 8487 | args.append("-c"); |
| 8452 | args.append(buf_ptr(c_source_file)); | 8488 | args.append(buf_ptr(c_source_file)); |
| 8453 | 8489 | ||
| 8454 | if (target_supports_fpic(g->zig_target) && !g->disable_pic) { | 8490 | if (target_supports_fpic(g->zig_target) && g->have_pic) { |
| 8455 | args.append("-fPIC"); | 8491 | args.append("-fPIC"); |
| 8456 | } | 8492 | } |
| 8457 | 8493 | ||
| ... | @@ -9064,7 +9100,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { | ... | @@ -9064,7 +9100,6 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { |
| 9064 | cache_int(ch, g->zig_target->os); | 9100 | cache_int(ch, g->zig_target->os); |
| 9065 | cache_int(ch, g->zig_target->abi); | 9101 | cache_int(ch, g->zig_target->abi); |
| 9066 | cache_int(ch, g->subsystem); | 9102 | cache_int(ch, g->subsystem); |
| 9067 | cache_bool(ch, g->is_static); | ||
| 9068 | cache_bool(ch, g->strip_debug_symbols); | 9103 | cache_bool(ch, g->strip_debug_symbols); |
| 9069 | cache_bool(ch, g->is_test_build); | 9104 | cache_bool(ch, g->is_test_build); |
| 9070 | if (g->is_test_build) { | 9105 | if (g->is_test_build) { |
| ... | @@ -9074,9 +9109,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { | ... | @@ -9074,9 +9109,10 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { |
| 9074 | cache_bool(ch, g->is_single_threaded); | 9109 | cache_bool(ch, g->is_single_threaded); |
| 9075 | cache_bool(ch, g->linker_rdynamic); | 9110 | cache_bool(ch, g->linker_rdynamic); |
| 9076 | cache_bool(ch, g->each_lib_rpath); | 9111 | cache_bool(ch, g->each_lib_rpath); |
| 9077 | cache_bool(ch, g->disable_pic); | ||
| 9078 | cache_bool(ch, g->disable_gen_h); | 9112 | cache_bool(ch, g->disable_gen_h); |
| 9079 | cache_bool(ch, g->valgrind_support); | 9113 | cache_bool(ch, want_valgrind_support(g)); |
| 9114 | cache_bool(ch, g->have_pic); | ||
| 9115 | cache_bool(ch, g->have_dynamic_link); | ||
| 9080 | cache_bool(ch, g->is_dummy_so); | 9116 | cache_bool(ch, g->is_dummy_so); |
| 9081 | cache_buf_opt(ch, g->mmacosx_version_min); | 9117 | cache_buf_opt(ch, g->mmacosx_version_min); |
| 9082 | cache_buf_opt(ch, g->mios_version_min); | 9118 | cache_buf_opt(ch, g->mios_version_min); |
| ... | @@ -9150,7 +9186,7 @@ static void resolve_out_paths(CodeGen *g) { | ... | @@ -9150,7 +9186,7 @@ static void resolve_out_paths(CodeGen *g) { |
| 9150 | buf_resize(out_basename, 0); | 9186 | buf_resize(out_basename, 0); |
| 9151 | buf_append_str(out_basename, target_lib_file_prefix(g->zig_target)); | 9187 | buf_append_str(out_basename, target_lib_file_prefix(g->zig_target)); |
| 9152 | buf_append_buf(out_basename, g->root_out_name); | 9188 | buf_append_buf(out_basename, g->root_out_name); |
| 9153 | buf_append_str(out_basename, target_lib_file_ext(g->zig_target, g->is_static, | 9189 | buf_append_str(out_basename, target_lib_file_ext(g->zig_target, !g->is_dynamic, |
| 9154 | g->version_major, g->version_minor, g->version_patch)); | 9190 | g->version_major, g->version_minor, g->version_patch)); |
| 9155 | break; | 9191 | break; |
| 9156 | } | 9192 | } |
| ... | @@ -9182,6 +9218,8 @@ void codegen_build_and_link(CodeGen *g) { | ... | @@ -9182,6 +9218,8 @@ void codegen_build_and_link(CodeGen *g) { |
| 9182 | g->output_dir = buf_create_from_str("."); | 9218 | g->output_dir = buf_create_from_str("."); |
| 9183 | } | 9219 | } |
| 9184 | 9220 | ||
| 9221 | g->have_dynamic_link = detect_dynamic_link(g); | ||
| 9222 | g->have_pic = detect_pic(g); | ||
| 9185 | detect_libc(g); | 9223 | detect_libc(g); |
| 9186 | detect_dynamic_linker(g); | 9224 | detect_dynamic_linker(g); |
| 9187 | 9225 |
src/ir.cpp+12-3| ... | @@ -15543,9 +15543,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ | ... | @@ -15543,9 +15543,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ |
| 15543 | } | 15543 | } |
| 15544 | 15544 | ||
| 15545 | static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, AstNode *source_node) { | 15545 | static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, AstNode *source_node) { |
| 15546 | if (buf_eql_str(lib_name, "c") && ira->codegen->libc_link_lib == nullptr && | 15546 | bool is_libc = target_is_libc_lib_name(ira->codegen->zig_target, buf_ptr(lib_name)); |
| 15547 | !ira->codegen->reported_bad_link_libc_error) | 15547 | if (is_libc && ira->codegen->libc_link_lib == nullptr && !ira->codegen->reported_bad_link_libc_error) { |
| 15548 | { | ||
| 15549 | ir_add_error_node(ira, source_node, | 15548 | ir_add_error_node(ira, source_node, |
| 15550 | buf_sprintf("dependency on library c must be explicitly specified in the build command")); | 15549 | buf_sprintf("dependency on library c must be explicitly specified in the build command")); |
| 15551 | ira->codegen->reported_bad_link_libc_error = true; | 15550 | ira->codegen->reported_bad_link_libc_error = true; |
| ... | @@ -15558,6 +15557,16 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, | ... | @@ -15558,6 +15557,16 @@ static void add_link_lib_symbol(IrAnalyze *ira, Buf *lib_name, Buf *symbol_name, |
| 15558 | return; | 15557 | return; |
| 15559 | } | 15558 | } |
| 15560 | } | 15559 | } |
| 15560 | |||
| 15561 | if (!is_libc && !ira->codegen->have_pic && !ira->codegen->reported_bad_link_libc_error) { | ||
| 15562 | ErrorMsg *msg = ir_add_error_node(ira, source_node, | ||
| 15563 | buf_sprintf("dependency on dynamic library '%s' requires enabling Position Independent Code", | ||
| 15564 | buf_ptr(lib_name))); | ||
| 15565 | add_error_note(ira->codegen, msg, source_node, | ||
| 15566 | buf_sprintf("fixed by `--library %s` or `--enable-pic`", buf_ptr(lib_name))); | ||
| 15567 | ira->codegen->reported_bad_link_libc_error = true; | ||
| 15568 | } | ||
| 15569 | |||
| 15561 | for (size_t i = 0; i < ira->codegen->forbidden_libs.length; i += 1) { | 15570 | for (size_t i = 0; i < ira->codegen->forbidden_libs.length; i += 1) { |
| 15562 | Buf *forbidden_lib_name = ira->codegen->forbidden_libs.at(i); | 15571 | Buf *forbidden_lib_name = ira->codegen->forbidden_libs.at(i); |
| 15563 | if (buf_eql_buf(lib_name, forbidden_lib_name)) { | 15572 | if (buf_eql_buf(lib_name, forbidden_lib_name)) { |
src/libc_installation.cpp+1-1| ... | @@ -102,7 +102,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget | ... | @@ -102,7 +102,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | if (buf_len(&libc->crt_dir) == 0) { | 104 | if (buf_len(&libc->crt_dir) == 0) { |
| 105 | if (!target_is_darwin(target)) { | 105 | if (!target_os_is_darwin(target->os)) { |
| 106 | if (verbose) { | 106 | if (verbose) { |
| 107 | fprintf(stderr, "crt_dir may not be empty for %s\n", target_os_name(target->os)); | 107 | fprintf(stderr, "crt_dir may not be empty for %s\n", target_os_name(target->os)); |
| 108 | } | 108 | } |
src/link.cpp+25-53| ... | @@ -35,7 +35,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou | ... | @@ -35,7 +35,7 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou |
| 35 | child_gen->llvm_argv = parent_gen->llvm_argv; | 35 | child_gen->llvm_argv = parent_gen->llvm_argv; |
| 36 | 36 | ||
| 37 | codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); | 37 | codegen_set_strip(child_gen, parent_gen->strip_debug_symbols); |
| 38 | child_gen->disable_pic = parent_gen->disable_pic; | 38 | child_gen->want_pic = parent_gen->have_pic ? WantPICEnabled : WantPICDisabled; |
| 39 | child_gen->valgrind_support = ValgrindSupportDisabled; | 39 | child_gen->valgrind_support = ValgrindSupportDisabled; |
| 40 | 40 | ||
| 41 | codegen_set_errmsg_color(child_gen, parent_gen->err_color); | 41 | codegen_set_errmsg_color(child_gen, parent_gen->err_color); |
| ... | @@ -48,15 +48,6 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou | ... | @@ -48,15 +48,6 @@ static CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, Ou |
| 48 | return child_gen; | 48 | return child_gen; |
| 49 | } | 49 | } |
| 50 | 50 | ||
| 51 | |||
| 52 | static bool target_is_glibc(CodeGen *g) { | ||
| 53 | return g->zig_target->os == OsLinux && target_abi_is_gnu(g->zig_target->abi); | ||
| 54 | } | ||
| 55 | |||
| 56 | static bool target_is_musl(CodeGen *g) { | ||
| 57 | return g->zig_target->os == OsLinux && target_abi_is_musl(g->zig_target->abi); | ||
| 58 | } | ||
| 59 | |||
| 60 | static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFile *c_file) { | 51 | static const char *build_libc_object(CodeGen *parent_gen, const char *name, CFile *c_file) { |
| 61 | CodeGen *child_gen = create_child_codegen(parent_gen, nullptr, OutTypeObj, nullptr); | 52 | CodeGen *child_gen = create_child_codegen(parent_gen, nullptr, OutTypeObj, nullptr); |
| 62 | codegen_set_out_name(child_gen, buf_create_from_str(name)); | 53 | codegen_set_out_name(child_gen, buf_create_from_str(name)); |
| ... | @@ -97,7 +88,7 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo | ... | @@ -97,7 +88,7 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo |
| 97 | CodeGen *child_gen = create_child_codegen(parent, glibc_dummy_root_src, OutTypeLib, nullptr); | 88 | CodeGen *child_gen = create_child_codegen(parent, glibc_dummy_root_src, OutTypeLib, nullptr); |
| 98 | codegen_set_out_name(child_gen, buf_create_from_str(name)); | 89 | codegen_set_out_name(child_gen, buf_create_from_str(name)); |
| 99 | codegen_set_lib_version(child_gen, major_version, 0, 0); | 90 | codegen_set_lib_version(child_gen, major_version, 0, 0); |
| 100 | child_gen->is_static = false; | 91 | child_gen->is_dynamic = true; |
| 101 | child_gen->is_dummy_so = true; | 92 | child_gen->is_dummy_so = true; |
| 102 | codegen_build_and_link(child_gen); | 93 | codegen_build_and_link(child_gen); |
| 103 | return buf_ptr(&child_gen->output_file_path); | 94 | return buf_ptr(&child_gen->output_file_path); |
| ... | @@ -106,7 +97,6 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo | ... | @@ -106,7 +97,6 @@ static const char *build_dummy_so(CodeGen *parent, const char *name, size_t majo |
| 106 | static const char *build_libunwind(CodeGen *parent) { | 97 | static const char *build_libunwind(CodeGen *parent) { |
| 107 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); | 98 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); |
| 108 | codegen_set_out_name(child_gen, buf_create_from_str("unwind")); | 99 | codegen_set_out_name(child_gen, buf_create_from_str("unwind")); |
| 109 | child_gen->is_static = true; | ||
| 110 | LinkLib *new_link_lib = codegen_add_link_lib(child_gen, buf_create_from_str("c")); | 100 | LinkLib *new_link_lib = codegen_add_link_lib(child_gen, buf_create_from_str("c")); |
| 111 | new_link_lib->provided_explicitly = false; | 101 | new_link_lib->provided_explicitly = false; |
| 112 | enum SrcKind { | 102 | enum SrcKind { |
| ... | @@ -490,7 +480,6 @@ static bool is_musl_arch_name(const char *name) { | ... | @@ -490,7 +480,6 @@ static bool is_musl_arch_name(const char *name) { |
| 490 | static const char *build_musl(CodeGen *parent) { | 480 | static const char *build_musl(CodeGen *parent) { |
| 491 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); | 481 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); |
| 492 | codegen_set_out_name(child_gen, buf_create_from_str("c")); | 482 | codegen_set_out_name(child_gen, buf_create_from_str("c")); |
| 493 | child_gen->is_static = true; | ||
| 494 | 483 | ||
| 495 | // When there is a src/<arch>/foo.* then it should substitute for src/foo.* | 484 | // When there is a src/<arch>/foo.* then it should substitute for src/foo.* |
| 496 | // Even a .s file can substitute for a .c file. | 485 | // Even a .s file can substitute for a .c file. |
| ... | @@ -608,7 +597,7 @@ static const char *build_musl(CodeGen *parent) { | ... | @@ -608,7 +597,7 @@ static const char *build_musl(CodeGen *parent) { |
| 608 | 597 | ||
| 609 | 598 | ||
| 610 | static const char *get_libc_crt_file(CodeGen *parent, const char *file) { | 599 | static const char *get_libc_crt_file(CodeGen *parent, const char *file) { |
| 611 | if (parent->libc == nullptr && target_is_glibc(parent)) { | 600 | if (parent->libc == nullptr && target_is_glibc(parent->zig_target)) { |
| 612 | if (strcmp(file, "crti.o") == 0) { | 601 | if (strcmp(file, "crti.o") == 0) { |
| 613 | CFile *c_file = allocate<CFile>(1); | 602 | CFile *c_file = allocate<CFile>(1); |
| 614 | c_file->source_path = glibc_start_asm_path(parent, "crti.S"); | 603 | c_file->source_path = glibc_start_asm_path(parent, "crti.S"); |
| ... | @@ -677,7 +666,6 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) { | ... | @@ -677,7 +666,6 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) { |
| 677 | } else if (strcmp(file, "libc_nonshared.a") == 0) { | 666 | } else if (strcmp(file, "libc_nonshared.a") == 0) { |
| 678 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); | 667 | CodeGen *child_gen = create_child_codegen(parent, nullptr, OutTypeLib, nullptr); |
| 679 | codegen_set_out_name(child_gen, buf_create_from_str("c_nonshared")); | 668 | codegen_set_out_name(child_gen, buf_create_from_str("c_nonshared")); |
| 680 | child_gen->is_static = true; | ||
| 681 | { | 669 | { |
| 682 | CFile *c_file = allocate<CFile>(1); | 670 | CFile *c_file = allocate<CFile>(1); |
| 683 | c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "elf-init.c"); | 671 | c_file->source_path = path_from_libc(parent, "glibc" OS_SEP "csu" OS_SEP "elf-init.c"); |
| ... | @@ -755,7 +743,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) { | ... | @@ -755,7 +743,7 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file) { |
| 755 | } else { | 743 | } else { |
| 756 | zig_unreachable(); | 744 | zig_unreachable(); |
| 757 | } | 745 | } |
| 758 | } else if (parent->libc == nullptr && target_is_musl(parent)) { | 746 | } else if (parent->libc == nullptr && target_is_musl(parent->zig_target)) { |
| 759 | if (strcmp(file, "crti.o") == 0) { | 747 | if (strcmp(file, "crti.o") == 0) { |
| 760 | return build_asm_object(parent, "crti", musl_start_asm_path(parent, "crti.s")); | 748 | return build_asm_object(parent, "crti", musl_start_asm_path(parent, "crti.s")); |
| 761 | } else if (strcmp(file, "crtn.o") == 0) { | 749 | } else if (strcmp(file, "crtn.o") == 0) { |
| ... | @@ -799,7 +787,6 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) | ... | @@ -799,7 +787,6 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path) |
| 799 | 787 | ||
| 800 | CodeGen *child_gen = create_child_codegen(parent_gen, full_path, child_out_type, | 788 | CodeGen *child_gen = create_child_codegen(parent_gen, full_path, child_out_type, |
| 801 | parent_gen->libc); | 789 | parent_gen->libc); |
| 802 | child_gen->is_static = true; | ||
| 803 | codegen_set_out_name(child_gen, buf_create_from_str(aname)); | 790 | codegen_set_out_name(child_gen, buf_create_from_str(aname)); |
| 804 | 791 | ||
| 805 | // This is so that compiler_rt and builtin libraries know whether they | 792 | // This is so that compiler_rt and builtin libraries know whether they |
| ... | @@ -924,9 +911,9 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -924,9 +911,9 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 924 | lj->args.append(getLDMOption(g->zig_target)); | 911 | lj->args.append(getLDMOption(g->zig_target)); |
| 925 | 912 | ||
| 926 | bool is_lib = g->out_type == OutTypeLib; | 913 | bool is_lib = g->out_type == OutTypeLib; |
| 927 | bool is_dyn_lib = !g->is_static && is_lib; | 914 | bool is_dyn_lib = g->is_dynamic && is_lib; |
| 928 | Buf *soname = nullptr; | 915 | Buf *soname = nullptr; |
| 929 | if (g->is_static) { | 916 | if (!g->have_dynamic_link) { |
| 930 | if (g->zig_target->arch == ZigLLVM_arm || g->zig_target->arch == ZigLLVM_armeb || | 917 | if (g->zig_target->arch == ZigLLVM_arm || g->zig_target->arch == ZigLLVM_armeb || |
| 931 | g->zig_target->arch == ZigLLVM_thumb || g->zig_target->arch == ZigLLVM_thumbeb) | 918 | g->zig_target->arch == ZigLLVM_thumb || g->zig_target->arch == ZigLLVM_thumbeb) |
| 932 | { | 919 | { |
| ... | @@ -948,7 +935,7 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -948,7 +935,7 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 948 | const char *crt1o; | 935 | const char *crt1o; |
| 949 | if (g->zig_target->os == OsNetBSD) { | 936 | if (g->zig_target->os == OsNetBSD) { |
| 950 | crt1o = "crt0.o"; | 937 | crt1o = "crt0.o"; |
| 951 | } else if (g->is_static) { | 938 | } else if (!g->have_dynamic_link) { |
| 952 | crt1o = "crt1.o"; | 939 | crt1o = "crt1.o"; |
| 953 | } else { | 940 | } else { |
| 954 | crt1o = "Scrt1.o"; | 941 | crt1o = "Scrt1.o"; |
| ... | @@ -994,12 +981,11 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -994,12 +981,11 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 994 | lj->args.append(buf_ptr(&g->libc->crt_dir)); | 981 | lj->args.append(buf_ptr(&g->libc->crt_dir)); |
| 995 | } | 982 | } |
| 996 | 983 | ||
| 997 | if (!g->is_static) { | 984 | if (g->have_dynamic_link && (is_dyn_lib || g->out_type == OutTypeExe)) { |
| 998 | assert(g->dynamic_linker_path != nullptr); | 985 | assert(g->dynamic_linker_path != nullptr); |
| 999 | lj->args.append("-dynamic-linker"); | 986 | lj->args.append("-dynamic-linker"); |
| 1000 | lj->args.append(buf_ptr(g->dynamic_linker_path)); | 987 | lj->args.append(buf_ptr(g->dynamic_linker_path)); |
| 1001 | } | 988 | } |
| 1002 | |||
| 1003 | } | 989 | } |
| 1004 | 990 | ||
| 1005 | if (is_dyn_lib) { | 991 | if (is_dyn_lib) { |
| ... | @@ -1012,16 +998,14 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -1012,16 +998,14 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 1012 | lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); | 998 | lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); |
| 1013 | } | 999 | } |
| 1014 | 1000 | ||
| 1015 | if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) { | 1001 | if (!g->is_dummy_so && (g->out_type == OutTypeExe || is_dyn_lib)) { |
| 1016 | if (g->libc_link_lib == nullptr && !g->is_dummy_so) { | 1002 | if (g->libc_link_lib == nullptr) { |
| 1017 | Buf *builtin_a_path = build_a(g, "builtin"); | 1003 | Buf *builtin_a_path = build_a(g, "builtin"); |
| 1018 | lj->args.append(buf_ptr(builtin_a_path)); | 1004 | lj->args.append(buf_ptr(builtin_a_path)); |
| 1019 | } | 1005 | } |
| 1020 | 1006 | ||
| 1021 | if (!g->is_dummy_so) { | 1007 | Buf *compiler_rt_o_path = build_compiler_rt(g); |
| 1022 | Buf *compiler_rt_o_path = build_compiler_rt(g); | 1008 | lj->args.append(buf_ptr(compiler_rt_o_path)); |
| 1023 | lj->args.append(buf_ptr(compiler_rt_o_path)); | ||
| 1024 | } | ||
| 1025 | } | 1009 | } |
| 1026 | 1010 | ||
| 1027 | for (size_t i = 0; i < g->link_libs_list.length; i += 1) { | 1011 | for (size_t i = 0; i < g->link_libs_list.length; i += 1) { |
| ... | @@ -1030,17 +1014,9 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -1030,17 +1014,9 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 1030 | // libc is linked specially | 1014 | // libc is linked specially |
| 1031 | continue; | 1015 | continue; |
| 1032 | } | 1016 | } |
| 1033 | if (g->libc == nullptr && (target_is_glibc(g) || target_is_musl(g))) { | 1017 | if (g->libc == nullptr && target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) { |
| 1034 | // these libraries are always linked below when targeting glibc | 1018 | // these libraries are always linked below when targeting glibc |
| 1035 | if (buf_eql_str(link_lib->name, "m")) { | 1019 | continue; |
| 1036 | continue; | ||
| 1037 | } else if (buf_eql_str(link_lib->name, "pthread")) { | ||
| 1038 | continue; | ||
| 1039 | } else if (buf_eql_str(link_lib->name, "dl")) { | ||
| 1040 | continue; | ||
| 1041 | } else if (buf_eql_str(link_lib->name, "rt")) { | ||
| 1042 | continue; | ||
| 1043 | } | ||
| 1044 | } | 1020 | } |
| 1045 | Buf *arg; | 1021 | Buf *arg; |
| 1046 | if (buf_starts_with_str(link_lib->name, "/") || buf_ends_with_str(link_lib->name, ".a") || | 1022 | if (buf_starts_with_str(link_lib->name, "/") || buf_ends_with_str(link_lib->name, ".a") || |
| ... | @@ -1057,7 +1033,7 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -1057,7 +1033,7 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 1057 | // libc dep | 1033 | // libc dep |
| 1058 | if (g->libc_link_lib != nullptr) { | 1034 | if (g->libc_link_lib != nullptr) { |
| 1059 | if (g->libc != nullptr) { | 1035 | if (g->libc != nullptr) { |
| 1060 | if (g->is_static) { | 1036 | if (!g->have_dynamic_link) { |
| 1061 | lj->args.append("--start-group"); | 1037 | lj->args.append("--start-group"); |
| 1062 | lj->args.append("-lgcc"); | 1038 | lj->args.append("-lgcc"); |
| 1063 | lj->args.append("-lgcc_eh"); | 1039 | lj->args.append("-lgcc_eh"); |
| ... | @@ -1076,7 +1052,7 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -1076,7 +1052,7 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 1076 | lj->args.append("-lgcc_s"); | 1052 | lj->args.append("-lgcc_s"); |
| 1077 | lj->args.append("--no-as-needed"); | 1053 | lj->args.append("--no-as-needed"); |
| 1078 | } | 1054 | } |
| 1079 | } else if (target_is_glibc(g)) { | 1055 | } else if (target_is_glibc(g->zig_target)) { |
| 1080 | lj->args.append(build_libunwind(g)); | 1056 | lj->args.append(build_libunwind(g)); |
| 1081 | lj->args.append(build_dummy_so(g, "c", 6)); | 1057 | lj->args.append(build_dummy_so(g, "c", 6)); |
| 1082 | lj->args.append(build_dummy_so(g, "m", 6)); | 1058 | lj->args.append(build_dummy_so(g, "m", 6)); |
| ... | @@ -1084,7 +1060,7 @@ static void construct_linker_job_elf(LinkJob *lj) { | ... | @@ -1084,7 +1060,7 @@ static void construct_linker_job_elf(LinkJob *lj) { |
| 1084 | lj->args.append(build_dummy_so(g, "dl", 2)); | 1060 | lj->args.append(build_dummy_so(g, "dl", 2)); |
| 1085 | lj->args.append(build_dummy_so(g, "rt", 1)); | 1061 | lj->args.append(build_dummy_so(g, "rt", 1)); |
| 1086 | lj->args.append(get_libc_crt_file(g, "libc_nonshared.a")); | 1062 | lj->args.append(get_libc_crt_file(g, "libc_nonshared.a")); |
| 1087 | } else if (target_is_musl(g)) { | 1063 | } else if (target_is_musl(g->zig_target)) { |
| 1088 | lj->args.append(build_libunwind(g)); | 1064 | lj->args.append(build_libunwind(g)); |
| 1089 | lj->args.append(build_musl(g)); | 1065 | lj->args.append(build_musl(g)); |
| 1090 | } else { | 1066 | } else { |
| ... | @@ -1158,10 +1134,10 @@ static void add_nt_link_args(LinkJob *lj, bool is_library) { | ... | @@ -1158,10 +1134,10 @@ static void add_nt_link_args(LinkJob *lj, bool is_library) { |
| 1158 | CodeGen *g = lj->codegen; | 1134 | CodeGen *g = lj->codegen; |
| 1159 | 1135 | ||
| 1160 | if (lj->link_in_crt) { | 1136 | if (lj->link_in_crt) { |
| 1161 | const char *lib_str = g->is_static ? "lib" : ""; | 1137 | const char *lib_str = g->is_dynamic ? "" : "lib"; |
| 1162 | const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; | 1138 | const char *d_str = (g->build_mode == BuildModeDebug) ? "d" : ""; |
| 1163 | 1139 | ||
| 1164 | if (g->is_static) { | 1140 | if (!g->is_dynamic) { |
| 1165 | Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); | 1141 | Buf *cmt_lib_name = buf_sprintf("libcmt%s.lib", d_str); |
| 1166 | lj->args.append(buf_ptr(cmt_lib_name)); | 1142 | lj->args.append(buf_ptr(cmt_lib_name)); |
| 1167 | } else { | 1143 | } else { |
| ... | @@ -1265,7 +1241,7 @@ static void construct_linker_job_coff(LinkJob *lj) { | ... | @@ -1265,7 +1241,7 @@ static void construct_linker_job_coff(LinkJob *lj) { |
| 1265 | lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); | 1241 | lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(&g->libc->crt_dir)))); |
| 1266 | } | 1242 | } |
| 1267 | 1243 | ||
| 1268 | if (is_library && !g->is_static) { | 1244 | if (is_library && g->is_dynamic) { |
| 1269 | lj->args.append("-DLL"); | 1245 | lj->args.append("-DLL"); |
| 1270 | } | 1246 | } |
| 1271 | 1247 | ||
| ... | @@ -1278,7 +1254,7 @@ static void construct_linker_job_coff(LinkJob *lj) { | ... | @@ -1278,7 +1254,7 @@ static void construct_linker_job_coff(LinkJob *lj) { |
| 1278 | lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); | 1254 | lj->args.append((const char *)buf_ptr(g->link_objects.at(i))); |
| 1279 | } | 1255 | } |
| 1280 | 1256 | ||
| 1281 | if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) { | 1257 | if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) { |
| 1282 | if (g->libc_link_lib == nullptr && !g->is_dummy_so) { | 1258 | if (g->libc_link_lib == nullptr && !g->is_dummy_so) { |
| 1283 | Buf *builtin_a_path = build_a(g, "builtin"); | 1259 | Buf *builtin_a_path = build_a(g, "builtin"); |
| 1284 | lj->args.append(buf_ptr(builtin_a_path)); | 1260 | lj->args.append(buf_ptr(builtin_a_path)); |
| ... | @@ -1457,8 +1433,8 @@ static void construct_linker_job_macho(LinkJob *lj) { | ... | @@ -1457,8 +1433,8 @@ static void construct_linker_job_macho(LinkJob *lj) { |
| 1457 | } | 1433 | } |
| 1458 | 1434 | ||
| 1459 | bool is_lib = g->out_type == OutTypeLib; | 1435 | bool is_lib = g->out_type == OutTypeLib; |
| 1460 | bool is_dyn_lib = !g->is_static && is_lib; | 1436 | bool is_dyn_lib = g->is_dynamic && is_lib; |
| 1461 | if (g->is_static) { | 1437 | if (!g->is_dynamic) { |
| 1462 | lj->args.append("-static"); | 1438 | lj->args.append("-static"); |
| 1463 | } else { | 1439 | } else { |
| 1464 | lj->args.append("-dynamic"); | 1440 | lj->args.append("-dynamic"); |
| ... | @@ -1509,11 +1485,7 @@ static void construct_linker_job_macho(LinkJob *lj) { | ... | @@ -1509,11 +1485,7 @@ static void construct_linker_job_macho(LinkJob *lj) { |
| 1509 | 1485 | ||
| 1510 | 1486 | ||
| 1511 | if (g->out_type == OutTypeExe) { | 1487 | if (g->out_type == OutTypeExe) { |
| 1512 | if (g->is_static) { | 1488 | lj->args.append("-pie"); |
| 1513 | lj->args.append("-no_pie"); | ||
| 1514 | } else { | ||
| 1515 | lj->args.append("-pie"); | ||
| 1516 | } | ||
| 1517 | } | 1489 | } |
| 1518 | 1490 | ||
| 1519 | lj->args.append("-o"); | 1491 | lj->args.append("-o"); |
| ... | @@ -1629,7 +1601,7 @@ void codegen_link(CodeGen *g) { | ... | @@ -1629,7 +1601,7 @@ void codegen_link(CodeGen *g) { |
| 1629 | lj.args.append("-r"); | 1601 | lj.args.append("-r"); |
| 1630 | } | 1602 | } |
| 1631 | 1603 | ||
| 1632 | if (g->out_type == OutTypeLib && g->is_static) { | 1604 | if (g->out_type == OutTypeLib && !g->is_dynamic) { |
| 1633 | ZigList<const char *> file_names = {}; | 1605 | ZigList<const char *> file_names = {}; |
| 1634 | for (size_t i = 0; i < g->link_objects.length; i += 1) { | 1606 | for (size_t i = 0; i < g->link_objects.length; i += 1) { |
| 1635 | file_names.append((const char *)buf_ptr(g->link_objects.at(i))); | 1607 | file_names.append((const char *)buf_ptr(g->link_objects.at(i))); |
src/main.cpp+28-19| ... | @@ -52,7 +52,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { | ... | @@ -52,7 +52,8 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { |
| 52 | " --cache [auto|off|on] build in cache, print output path to stdout\n" | 52 | " --cache [auto|off|on] build in cache, print output path to stdout\n" |
| 53 | " --color [auto|off|on] enable or disable colored error messages\n" | 53 | " --color [auto|off|on] enable or disable colored error messages\n" |
| 54 | " --disable-gen-h do not generate a C header file (.h)\n" | 54 | " --disable-gen-h do not generate a C header file (.h)\n" |
| 55 | " --disable-pic disable Position Independent Code for libraries\n" | 55 | " --disable-pic disable Position Independent Code\n" |
| 56 | " --enable-pic enable Position Independent Code\n" | ||
| 56 | " --disable-valgrind omit valgrind client requests in debug builds\n" | 57 | " --disable-valgrind omit valgrind client requests in debug builds\n" |
| 57 | " --enable-valgrind include valgrind client requests release builds\n" | 58 | " --enable-valgrind include valgrind client requests release builds\n" |
| 58 | " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n" | 59 | " --emit [asm|bin|llvm-ir] emit a specific file format as compilation output\n" |
| ... | @@ -67,7 +68,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { | ... | @@ -67,7 +68,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { |
| 67 | " --release-safe build with optimizations on and safety on\n" | 68 | " --release-safe build with optimizations on and safety on\n" |
| 68 | " --release-small build with size optimizations on and safety off\n" | 69 | " --release-small build with size optimizations on and safety off\n" |
| 69 | " --single-threaded source may assume it is only used single-threaded\n" | 70 | " --single-threaded source may assume it is only used single-threaded\n" |
| 70 | " --static output will be statically linked\n" | 71 | " -dynamic create a shared library (.so; .dll; .dylib)\n" |
| 71 | " --strip exclude debug symbols\n" | 72 | " --strip exclude debug symbols\n" |
| 72 | " -target [name] <arch><sub>-<os>-<abi> see the targets command\n" | 73 | " -target [name] <arch><sub>-<os>-<abi> see the targets command\n" |
| 73 | " --verbose-tokenize enable compiler debug output for tokenization\n" | 74 | " --verbose-tokenize enable compiler debug output for tokenization\n" |
| ... | @@ -397,7 +398,7 @@ int main(int argc, char **argv) { | ... | @@ -397,7 +398,7 @@ int main(int argc, char **argv) { |
| 397 | const char *in_file = nullptr; | 398 | const char *in_file = nullptr; |
| 398 | Buf *output_dir = nullptr; | 399 | Buf *output_dir = nullptr; |
| 399 | bool strip = false; | 400 | bool strip = false; |
| 400 | bool is_static = false; | 401 | bool is_dynamic = false; |
| 401 | OutType out_type = OutTypeUnknown; | 402 | OutType out_type = OutTypeUnknown; |
| 402 | const char *out_name = nullptr; | 403 | const char *out_name = nullptr; |
| 403 | bool verbose_tokenize = false; | 404 | bool verbose_tokenize = false; |
| ... | @@ -432,7 +433,6 @@ int main(int argc, char **argv) { | ... | @@ -432,7 +433,6 @@ int main(int argc, char **argv) { |
| 432 | size_t ver_minor = 0; | 433 | size_t ver_minor = 0; |
| 433 | size_t ver_patch = 0; | 434 | size_t ver_patch = 0; |
| 434 | bool timing_info = false; | 435 | bool timing_info = false; |
| 435 | bool disable_pic = false; | ||
| 436 | const char *cache_dir = nullptr; | 436 | const char *cache_dir = nullptr; |
| 437 | CliPkg *cur_pkg = allocate<CliPkg>(1); | 437 | CliPkg *cur_pkg = allocate<CliPkg>(1); |
| 438 | BuildMode build_mode = BuildModeDebug; | 438 | BuildMode build_mode = BuildModeDebug; |
| ... | @@ -445,6 +445,7 @@ int main(int argc, char **argv) { | ... | @@ -445,6 +445,7 @@ int main(int argc, char **argv) { |
| 445 | Buf *override_std_dir = nullptr; | 445 | Buf *override_std_dir = nullptr; |
| 446 | Buf *main_pkg_path = nullptr; | 446 | Buf *main_pkg_path = nullptr; |
| 447 | ValgrindSupport valgrind_support = ValgrindSupportAuto; | 447 | ValgrindSupport valgrind_support = ValgrindSupportAuto; |
| 448 | WantPIC want_pic = WantPICAuto; | ||
| 448 | 449 | ||
| 449 | ZigList<const char *> llvm_argv = {0}; | 450 | ZigList<const char *> llvm_argv = {0}; |
| 450 | llvm_argv.append("zig (LLVM option parsing)"); | 451 | llvm_argv.append("zig (LLVM option parsing)"); |
| ... | @@ -620,8 +621,8 @@ int main(int argc, char **argv) { | ... | @@ -620,8 +621,8 @@ int main(int argc, char **argv) { |
| 620 | } | 621 | } |
| 621 | } else if (strcmp(arg, "--strip") == 0) { | 622 | } else if (strcmp(arg, "--strip") == 0) { |
| 622 | strip = true; | 623 | strip = true; |
| 623 | } else if (strcmp(arg, "--static") == 0) { | 624 | } else if (strcmp(arg, "-dynamic") == 0) { |
| 624 | is_static = true; | 625 | is_dynamic = true; |
| 625 | } else if (strcmp(arg, "--verbose-tokenize") == 0) { | 626 | } else if (strcmp(arg, "--verbose-tokenize") == 0) { |
| 626 | verbose_tokenize = true; | 627 | verbose_tokenize = true; |
| 627 | } else if (strcmp(arg, "--verbose-ast") == 0) { | 628 | } else if (strcmp(arg, "--verbose-ast") == 0) { |
| ... | @@ -642,12 +643,14 @@ int main(int argc, char **argv) { | ... | @@ -642,12 +643,14 @@ int main(int argc, char **argv) { |
| 642 | each_lib_rpath = true; | 643 | each_lib_rpath = true; |
| 643 | } else if (strcmp(arg, "-ftime-report") == 0) { | 644 | } else if (strcmp(arg, "-ftime-report") == 0) { |
| 644 | timing_info = true; | 645 | timing_info = true; |
| 645 | } else if (strcmp(arg, "--disable-pic") == 0) { | ||
| 646 | disable_pic = true; | ||
| 647 | } else if (strcmp(arg, "--enable-valgrind") == 0) { | 646 | } else if (strcmp(arg, "--enable-valgrind") == 0) { |
| 648 | valgrind_support = ValgrindSupportEnabled; | 647 | valgrind_support = ValgrindSupportEnabled; |
| 649 | } else if (strcmp(arg, "--disable-valgrind") == 0) { | 648 | } else if (strcmp(arg, "--disable-valgrind") == 0) { |
| 650 | valgrind_support = ValgrindSupportDisabled; | 649 | valgrind_support = ValgrindSupportDisabled; |
| 650 | } else if (strcmp(arg, "--enable-pic") == 0) { | ||
| 651 | want_pic = WantPICEnabled; | ||
| 652 | } else if (strcmp(arg, "--disable-pic") == 0) { | ||
| 653 | want_pic = WantPICDisabled; | ||
| 651 | } else if (strcmp(arg, "--system-linker-hack") == 0) { | 654 | } else if (strcmp(arg, "--system-linker-hack") == 0) { |
| 652 | system_linker_hack = true; | 655 | system_linker_hack = true; |
| 653 | } else if (strcmp(arg, "--single-threaded") == 0) { | 656 | } else if (strcmp(arg, "--single-threaded") == 0) { |
| ... | @@ -904,12 +907,24 @@ int main(int argc, char **argv) { | ... | @@ -904,12 +907,24 @@ int main(int argc, char **argv) { |
| 904 | } | 907 | } |
| 905 | 908 | ||
| 906 | if (output_dir != nullptr && enable_cache == CacheOptOn) { | 909 | if (output_dir != nullptr && enable_cache == CacheOptOn) { |
| 907 | fprintf(stderr, "The --output-dir argument is incompatible with --cache on.\n"); | 910 | fprintf(stderr, "`--output-dir` is incompatible with --cache on.\n"); |
| 911 | return print_error_usage(arg0); | ||
| 912 | } | ||
| 913 | |||
| 914 | if (target_requires_pic(&target) && want_pic == WantPICDisabled) { | ||
| 915 | Buf triple_buf = BUF_INIT; | ||
| 916 | get_target_triple(&triple_buf, &target); | ||
| 917 | fprintf(stderr, "`--disable-pic` is incompatible with target '%s'\n", buf_ptr(&triple_buf)); | ||
| 908 | return print_error_usage(arg0); | 918 | return print_error_usage(arg0); |
| 909 | } | 919 | } |
| 910 | 920 | ||
| 911 | if (emit_file_type != EmitFileTypeBinary && in_file == nullptr) { | 921 | if (emit_file_type != EmitFileTypeBinary && in_file == nullptr) { |
| 912 | fprintf(stderr, "A root source file is required when using --emit asm or --emit llvm-ir"); | 922 | fprintf(stderr, "A root source file is required when using `--emit asm` or `--emit llvm-ir`\n"); |
| 923 | return print_error_usage(arg0); | ||
| 924 | } | ||
| 925 | |||
| 926 | if (out_type != OutTypeLib && is_dynamic) { | ||
| 927 | fprintf(stderr, "`-dynamic` may only be specified with `build-lib`.\n"); | ||
| 913 | return print_error_usage(arg0); | 928 | return print_error_usage(arg0); |
| 914 | } | 929 | } |
| 915 | 930 | ||
| ... | @@ -936,6 +951,7 @@ int main(int argc, char **argv) { | ... | @@ -936,6 +951,7 @@ int main(int argc, char **argv) { |
| 936 | CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, | 951 | CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, |
| 937 | out_type, build_mode, get_zig_lib_dir(), override_std_dir, nullptr, nullptr); | 952 | out_type, build_mode, get_zig_lib_dir(), override_std_dir, nullptr, nullptr); |
| 938 | g->valgrind_support = valgrind_support; | 953 | g->valgrind_support = valgrind_support; |
| 954 | g->want_pic = want_pic; | ||
| 939 | g->is_single_threaded = is_single_threaded; | 955 | g->is_single_threaded = is_single_threaded; |
| 940 | Buf *builtin_source = codegen_generate_builtin_source(g); | 956 | Buf *builtin_source = codegen_generate_builtin_source(g); |
| 941 | if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) { | 957 | if (fwrite(buf_ptr(builtin_source), 1, buf_len(builtin_source), stdout) != buf_len(builtin_source)) { |
| ... | @@ -1027,16 +1043,9 @@ int main(int argc, char **argv) { | ... | @@ -1027,16 +1043,9 @@ int main(int argc, char **argv) { |
| 1027 | get_zig_lib_dir(), override_std_dir, libc, cache_dir_buf); | 1043 | get_zig_lib_dir(), override_std_dir, libc, cache_dir_buf); |
| 1028 | if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); | 1044 | if (llvm_argv.length >= 2) codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2); |
| 1029 | g->valgrind_support = valgrind_support; | 1045 | g->valgrind_support = valgrind_support; |
| 1046 | g->want_pic = want_pic; | ||
| 1030 | g->subsystem = subsystem; | 1047 | g->subsystem = subsystem; |
| 1031 | 1048 | ||
| 1032 | if (disable_pic) { | ||
| 1033 | if (out_type != OutTypeLib || !is_static) { | ||
| 1034 | fprintf(stderr, "--disable-pic only applies to static libraries"); | ||
| 1035 | return EXIT_FAILURE; | ||
| 1036 | } | ||
| 1037 | g->disable_pic = true; | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | g->enable_time_report = timing_info; | 1049 | g->enable_time_report = timing_info; |
| 1041 | codegen_set_out_name(g, buf_out_name); | 1050 | codegen_set_out_name(g, buf_out_name); |
| 1042 | codegen_set_lib_version(g, ver_major, ver_minor, ver_patch); | 1051 | codegen_set_lib_version(g, ver_major, ver_minor, ver_patch); |
| ... | @@ -1049,7 +1058,7 @@ int main(int argc, char **argv) { | ... | @@ -1049,7 +1058,7 @@ int main(int argc, char **argv) { |
| 1049 | codegen_set_clang_argv(g, clang_argv.items, clang_argv.length); | 1058 | codegen_set_clang_argv(g, clang_argv.items, clang_argv.length); |
| 1050 | 1059 | ||
| 1051 | codegen_set_strip(g, strip); | 1060 | codegen_set_strip(g, strip); |
| 1052 | g->is_static = is_static; | 1061 | g->is_dynamic = is_dynamic; |
| 1053 | g->dynamic_linker_path = dynamic_linker; | 1062 | g->dynamic_linker_path = dynamic_linker; |
| 1054 | g->verbose_tokenize = verbose_tokenize; | 1063 | g->verbose_tokenize = verbose_tokenize; |
| 1055 | g->verbose_ast = verbose_ast; | 1064 | g->verbose_ast = verbose_ast; |
src/target.cpp+46-7| ... | @@ -693,8 +693,8 @@ void get_target_triple(Buf *triple, const ZigTarget *target) { | ... | @@ -693,8 +693,8 @@ void get_target_triple(Buf *triple, const ZigTarget *target) { |
| 693 | ZigLLVMGetEnvironmentTypeName(target->abi)); | 693 | ZigLLVMGetEnvironmentTypeName(target->abi)); |
| 694 | } | 694 | } |
| 695 | 695 | ||
| 696 | bool target_is_darwin(const ZigTarget *target) { | 696 | bool target_os_is_darwin(Os os) { |
| 697 | switch (target->os) { | 697 | switch (os) { |
| 698 | case OsMacOSX: | 698 | case OsMacOSX: |
| 699 | case OsIOS: | 699 | case OsIOS: |
| 700 | case OsWatchOS: | 700 | case OsWatchOS: |
| ... | @@ -708,7 +708,7 @@ bool target_is_darwin(const ZigTarget *target) { | ... | @@ -708,7 +708,7 @@ bool target_is_darwin(const ZigTarget *target) { |
| 708 | ZigLLVM_ObjectFormatType target_object_format(const ZigTarget *target) { | 708 | ZigLLVM_ObjectFormatType target_object_format(const ZigTarget *target) { |
| 709 | if (target->os == OsUefi || target->os == OsWindows) { | 709 | if (target->os == OsUefi || target->os == OsWindows) { |
| 710 | return ZigLLVM_COFF; | 710 | return ZigLLVM_COFF; |
| 711 | } else if (target_is_darwin(target)) { | 711 | } else if (target_os_is_darwin(target->os)) { |
| 712 | return ZigLLVM_MachO; | 712 | return ZigLLVM_MachO; |
| 713 | } | 713 | } |
| 714 | if (target->arch == ZigLLVM_wasm32 || | 714 | if (target->arch == ZigLLVM_wasm32 || |
| ... | @@ -942,7 +942,7 @@ const char *target_lib_file_ext(const ZigTarget *target, bool is_static, | ... | @@ -942,7 +942,7 @@ const char *target_lib_file_ext(const ZigTarget *target, bool is_static, |
| 942 | } else { | 942 | } else { |
| 943 | if (is_static) { | 943 | if (is_static) { |
| 944 | return ".a"; | 944 | return ".a"; |
| 945 | } else if (target_is_darwin(target)) { | 945 | } else if (target_os_is_darwin(target->os)) { |
| 946 | return buf_ptr(buf_sprintf(".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".dylib", | 946 | return buf_ptr(buf_sprintf(".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".dylib", |
| 947 | version_major, version_minor, version_patch)); | 947 | version_major, version_minor, version_patch)); |
| 948 | } else { | 948 | } else { |
| ... | @@ -1279,7 +1279,7 @@ bool target_has_valgrind_support(const ZigTarget *target) { | ... | @@ -1279,7 +1279,7 @@ bool target_has_valgrind_support(const ZigTarget *target) { |
| 1279 | case ZigLLVM_UnknownArch: | 1279 | case ZigLLVM_UnknownArch: |
| 1280 | zig_unreachable(); | 1280 | zig_unreachable(); |
| 1281 | case ZigLLVM_x86_64: | 1281 | case ZigLLVM_x86_64: |
| 1282 | return (target->os == OsLinux || target_is_darwin(target) || target->os == OsSolaris || | 1282 | return (target->os == OsLinux || target_os_is_darwin(target->os) || target->os == OsSolaris || |
| 1283 | (target->os == OsWindows && target->abi != ZigLLVM_MSVC)); | 1283 | (target->os == OsWindows && target->abi != ZigLLVM_MSVC)); |
| 1284 | default: | 1284 | default: |
| 1285 | return false; | 1285 | return false; |
| ... | @@ -1287,11 +1287,11 @@ bool target_has_valgrind_support(const ZigTarget *target) { | ... | @@ -1287,11 +1287,11 @@ bool target_has_valgrind_support(const ZigTarget *target) { |
| 1287 | zig_unreachable(); | 1287 | zig_unreachable(); |
| 1288 | } | 1288 | } |
| 1289 | 1289 | ||
| 1290 | bool target_requires_libc(const ZigTarget *target) { | 1290 | bool target_os_requires_libc(Os os) { |
| 1291 | // On Darwin, we always link libSystem which contains libc. | 1291 | // On Darwin, we always link libSystem which contains libc. |
| 1292 | // Similarly on FreeBSD and NetBSD we always link system libc | 1292 | // Similarly on FreeBSD and NetBSD we always link system libc |
| 1293 | // since this is the stable syscall interface. | 1293 | // since this is the stable syscall interface. |
| 1294 | return (target_is_darwin(target) || target->os == OsFreeBSD || target->os == OsNetBSD); | 1294 | return (target_os_is_darwin(os) || os == OsFreeBSD || os == OsNetBSD); |
| 1295 | } | 1295 | } |
| 1296 | 1296 | ||
| 1297 | bool target_supports_fpic(const ZigTarget *target) { | 1297 | bool target_supports_fpic(const ZigTarget *target) { |
| ... | @@ -1300,6 +1300,19 @@ bool target_supports_fpic(const ZigTarget *target) { | ... | @@ -1300,6 +1300,19 @@ bool target_supports_fpic(const ZigTarget *target) { |
| 1300 | return target->os != OsWindows; | 1300 | return target->os != OsWindows; |
| 1301 | } | 1301 | } |
| 1302 | 1302 | ||
| 1303 | bool target_requires_pic(const ZigTarget *target) { | ||
| 1304 | // This function returns whether non-pic code is completely invalid on the given target. | ||
| 1305 | return target->os == OsWindows || target_os_requires_libc(target->os) || target_is_glibc(target); | ||
| 1306 | } | ||
| 1307 | |||
| 1308 | bool target_is_glibc(const ZigTarget *target) { | ||
| 1309 | return target->os == OsLinux && target_abi_is_gnu(target->abi); | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | bool target_is_musl(const ZigTarget *target) { | ||
| 1313 | return target->os == OsLinux && target_abi_is_musl(target->abi); | ||
| 1314 | } | ||
| 1315 | |||
| 1303 | ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { | 1316 | ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { |
| 1304 | switch (os) { | 1317 | switch (os) { |
| 1305 | case OsFreestanding: | 1318 | case OsFreestanding: |
| ... | @@ -1458,3 +1471,29 @@ const char *target_libc_generic_name(const ZigTarget *target) { | ... | @@ -1458,3 +1471,29 @@ const char *target_libc_generic_name(const ZigTarget *target) { |
| 1458 | } | 1471 | } |
| 1459 | zig_unreachable(); | 1472 | zig_unreachable(); |
| 1460 | } | 1473 | } |
| 1474 | |||
| 1475 | bool target_is_libc_lib_name(const ZigTarget *target, const char *name) { | ||
| 1476 | if (strcmp(name, "c") == 0) | ||
| 1477 | return true; | ||
| 1478 | |||
| 1479 | if (target_abi_is_gnu(target->abi) || target_abi_is_musl(target->abi)) { | ||
| 1480 | if (strcmp(name, "m") == 0) | ||
| 1481 | return true; | ||
| 1482 | if (strcmp(name, "rt") == 0) | ||
| 1483 | return true; | ||
| 1484 | if (strcmp(name, "pthread") == 0) | ||
| 1485 | return true; | ||
| 1486 | if (strcmp(name, "crypt") == 0) | ||
| 1487 | return true; | ||
| 1488 | if (strcmp(name, "util") == 0) | ||
| 1489 | return true; | ||
| 1490 | if (strcmp(name, "xnet") == 0) | ||
| 1491 | return true; | ||
| 1492 | if (strcmp(name, "resolv") == 0) | ||
| 1493 | return true; | ||
| 1494 | if (strcmp(name, "dl") == 0) | ||
| 1495 | return true; | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | return false; | ||
| 1499 | } |
src/target.hpp+6-2| ... | @@ -155,13 +155,17 @@ ZigLLVM_OSType get_llvm_os_type(Os os_type); | ... | @@ -155,13 +155,17 @@ ZigLLVM_OSType get_llvm_os_type(Os os_type); |
| 155 | bool target_is_arm(const ZigTarget *target); | 155 | bool target_is_arm(const ZigTarget *target); |
| 156 | bool target_allows_addr_zero(const ZigTarget *target); | 156 | bool target_allows_addr_zero(const ZigTarget *target); |
| 157 | bool target_has_valgrind_support(const ZigTarget *target); | 157 | bool target_has_valgrind_support(const ZigTarget *target); |
| 158 | bool target_is_darwin(const ZigTarget *target); | 158 | bool target_os_is_darwin(Os os); |
| 159 | bool target_requires_libc(const ZigTarget *target); | 159 | bool target_os_requires_libc(Os os); |
| 160 | bool target_can_build_libc(const ZigTarget *target); | 160 | bool target_can_build_libc(const ZigTarget *target); |
| 161 | const char *target_libc_generic_name(const ZigTarget *target); | 161 | const char *target_libc_generic_name(const ZigTarget *target); |
| 162 | bool target_is_libc_lib_name(const ZigTarget *target, const char *name); | ||
| 162 | bool target_supports_fpic(const ZigTarget *target); | 163 | bool target_supports_fpic(const ZigTarget *target); |
| 164 | bool target_requires_pic(const ZigTarget *target); | ||
| 163 | bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi); | 165 | bool target_abi_is_gnu(ZigLLVM_EnvironmentType abi); |
| 164 | bool target_abi_is_musl(ZigLLVM_EnvironmentType abi); | 166 | bool target_abi_is_musl(ZigLLVM_EnvironmentType abi); |
| 167 | bool target_is_glibc(const ZigTarget *target); | ||
| 168 | bool target_is_musl(const ZigTarget *target); | ||
| 165 | 169 | ||
| 166 | uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch); | 170 | uint32_t target_arch_pointer_bit_width(ZigLLVM_ArchType arch); |
| 167 | 171 |
std/build.zig+17-21| ... | @@ -157,10 +157,6 @@ pub const Builder = struct { | ... | @@ -157,10 +157,6 @@ pub const Builder = struct { |
| 157 | return LibExeObjStep.createExecutable(self, name, root_src, false); | 157 | return LibExeObjStep.createExecutable(self, name, root_src, false); |
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | pub fn addStaticExecutable(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { | ||
| 161 | return LibExeObjStep.createExecutable(self, name, root_src, true); | ||
| 162 | } | ||
| 163 | |||
| 164 | pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { | 160 | pub fn addObject(self: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { |
| 165 | return LibExeObjStep.createObject(self, name, root_src); | 161 | return LibExeObjStep.createObject(self, name, root_src); |
| 166 | } | 162 | } |
| ... | @@ -920,7 +916,7 @@ pub const LibExeObjStep = struct { | ... | @@ -920,7 +916,7 @@ pub const LibExeObjStep = struct { |
| 920 | target: Target, | 916 | target: Target, |
| 921 | linker_script: ?[]const u8, | 917 | linker_script: ?[]const u8, |
| 922 | out_filename: []const u8, | 918 | out_filename: []const u8, |
| 923 | static: bool, | 919 | is_dynamic: bool, |
| 924 | version: Version, | 920 | version: Version, |
| 925 | build_mode: builtin.Mode, | 921 | build_mode: builtin.Mode, |
| 926 | kind: Kind, | 922 | kind: Kind, |
| ... | @@ -975,13 +971,13 @@ pub const LibExeObjStep = struct { | ... | @@ -975,13 +971,13 @@ pub const LibExeObjStep = struct { |
| 975 | 971 | ||
| 976 | pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep { | 972 | pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep { |
| 977 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; | 973 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; |
| 978 | self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver); | 974 | self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, ver); |
| 979 | return self; | 975 | return self; |
| 980 | } | 976 | } |
| 981 | 977 | ||
| 982 | pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { | 978 | pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { |
| 983 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; | 979 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; |
| 984 | self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0)); | 980 | self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, builder.version(0, 0, 0)); |
| 985 | return self; | 981 | return self; |
| 986 | } | 982 | } |
| 987 | 983 | ||
| ... | @@ -991,9 +987,9 @@ pub const LibExeObjStep = struct { | ... | @@ -991,9 +987,9 @@ pub const LibExeObjStep = struct { |
| 991 | return self; | 987 | return self; |
| 992 | } | 988 | } |
| 993 | 989 | ||
| 994 | pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8, static: bool) *LibExeObjStep { | 990 | pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8, is_dynamic: bool) *LibExeObjStep { |
| 995 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; | 991 | const self = builder.allocator.create(LibExeObjStep) catch unreachable; |
| 996 | self.* = initExtraArgs(builder, name, root_src, Kind.Exe, static, builder.version(0, 0, 0)); | 992 | self.* = initExtraArgs(builder, name, root_src, Kind.Exe, is_dynamic, builder.version(0, 0, 0)); |
| 997 | return self; | 993 | return self; |
| 998 | } | 994 | } |
| 999 | 995 | ||
| ... | @@ -1003,14 +999,14 @@ pub const LibExeObjStep = struct { | ... | @@ -1003,14 +999,14 @@ pub const LibExeObjStep = struct { |
| 1003 | return self; | 999 | return self; |
| 1004 | } | 1000 | } |
| 1005 | 1001 | ||
| 1006 | fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, static: bool, ver: Version) LibExeObjStep { | 1002 | fn initExtraArgs(builder: *Builder, name: []const u8, root_src: ?[]const u8, kind: Kind, is_dynamic: bool, ver: Version) LibExeObjStep { |
| 1007 | var self = LibExeObjStep{ | 1003 | var self = LibExeObjStep{ |
| 1008 | .strip = false, | 1004 | .strip = false, |
| 1009 | .builder = builder, | 1005 | .builder = builder, |
| 1010 | .verbose_link = false, | 1006 | .verbose_link = false, |
| 1011 | .verbose_cc = false, | 1007 | .verbose_cc = false, |
| 1012 | .build_mode = builtin.Mode.Debug, | 1008 | .build_mode = builtin.Mode.Debug, |
| 1013 | .static = static, | 1009 | .is_dynamic = is_dynamic, |
| 1014 | .kind = kind, | 1010 | .kind = kind, |
| 1015 | .root_src = root_src, | 1011 | .root_src = root_src, |
| 1016 | .name = name, | 1012 | .name = name, |
| ... | @@ -1057,7 +1053,7 @@ pub const LibExeObjStep = struct { | ... | @@ -1057,7 +1053,7 @@ pub const LibExeObjStep = struct { |
| 1057 | self.out_filename = self.builder.fmt("test{}", self.target.exeFileExt()); | 1053 | self.out_filename = self.builder.fmt("test{}", self.target.exeFileExt()); |
| 1058 | }, | 1054 | }, |
| 1059 | Kind.Lib => { | 1055 | Kind.Lib => { |
| 1060 | if (self.static) { | 1056 | if (!self.is_dynamic) { |
| 1061 | switch (self.target.getOs()) { | 1057 | switch (self.target.getOs()) { |
| 1062 | builtin.Os.windows => { | 1058 | builtin.Os.windows => { |
| 1063 | self.out_filename = self.builder.fmt("{}.lib", self.name); | 1059 | self.out_filename = self.builder.fmt("{}.lib", self.name); |
| ... | @@ -1147,7 +1143,7 @@ pub const LibExeObjStep = struct { | ... | @@ -1147,7 +1143,7 @@ pub const LibExeObjStep = struct { |
| 1147 | } | 1143 | } |
| 1148 | 1144 | ||
| 1149 | pub fn isDynamicLibrary(self: *LibExeObjStep) bool { | 1145 | pub fn isDynamicLibrary(self: *LibExeObjStep) bool { |
| 1150 | return self.kind == Kind.Lib and !self.static; | 1146 | return self.kind == Kind.Lib and self.is_dynamic; |
| 1151 | } | 1147 | } |
| 1152 | 1148 | ||
| 1153 | pub fn linkSystemLibrary(self: *LibExeObjStep, name: []const u8) void { | 1149 | pub fn linkSystemLibrary(self: *LibExeObjStep, name: []const u8) void { |
| ... | @@ -1333,7 +1329,7 @@ pub const LibExeObjStep = struct { | ... | @@ -1333,7 +1329,7 @@ pub const LibExeObjStep = struct { |
| 1333 | try zig_args.append(other.getOutputPath()); | 1329 | try zig_args.append(other.getOutputPath()); |
| 1334 | }, | 1330 | }, |
| 1335 | LibExeObjStep.Kind.Lib => { | 1331 | LibExeObjStep.Kind.Lib => { |
| 1336 | if (other.static or self.target.isWindows()) { | 1332 | if (!other.is_dynamic or self.target.isWindows()) { |
| 1337 | try zig_args.append("--object"); | 1333 | try zig_args.append("--object"); |
| 1338 | try zig_args.append(other.getOutputLibPath()); | 1334 | try zig_args.append(other.getOutputLibPath()); |
| 1339 | } else { | 1335 | } else { |
| ... | @@ -1413,7 +1409,7 @@ pub const LibExeObjStep = struct { | ... | @@ -1413,7 +1409,7 @@ pub const LibExeObjStep = struct { |
| 1413 | zig_args.append("--name") catch unreachable; | 1409 | zig_args.append("--name") catch unreachable; |
| 1414 | zig_args.append(self.name) catch unreachable; | 1410 | zig_args.append(self.name) catch unreachable; |
| 1415 | 1411 | ||
| 1416 | if (self.kind == Kind.Lib and !self.static) { | 1412 | if (self.kind == Kind.Lib and self.is_dynamic) { |
| 1417 | zig_args.append("--ver-major") catch unreachable; | 1413 | zig_args.append("--ver-major") catch unreachable; |
| 1418 | zig_args.append(builder.fmt("{}", self.version.major)) catch unreachable; | 1414 | zig_args.append(builder.fmt("{}", self.version.major)) catch unreachable; |
| 1419 | 1415 | ||
| ... | @@ -1423,8 +1419,8 @@ pub const LibExeObjStep = struct { | ... | @@ -1423,8 +1419,8 @@ pub const LibExeObjStep = struct { |
| 1423 | zig_args.append("--ver-patch") catch unreachable; | 1419 | zig_args.append("--ver-patch") catch unreachable; |
| 1424 | zig_args.append(builder.fmt("{}", self.version.patch)) catch unreachable; | 1420 | zig_args.append(builder.fmt("{}", self.version.patch)) catch unreachable; |
| 1425 | } | 1421 | } |
| 1426 | if (self.static) { | 1422 | if (self.is_dynamic) { |
| 1427 | zig_args.append("--static") catch unreachable; | 1423 | try zig_args.append("-dynamic"); |
| 1428 | } | 1424 | } |
| 1429 | 1425 | ||
| 1430 | switch (self.target) { | 1426 | switch (self.target) { |
| ... | @@ -1531,7 +1527,7 @@ pub const LibExeObjStep = struct { | ... | @@ -1531,7 +1527,7 @@ pub const LibExeObjStep = struct { |
| 1531 | self.output_dir = os.path.dirname(output_path).?; | 1527 | self.output_dir = os.path.dirname(output_path).?; |
| 1532 | } | 1528 | } |
| 1533 | 1529 | ||
| 1534 | if (self.kind == Kind.Lib and !self.static and self.target.wantSharedLibSymLinks()) { | 1530 | if (self.kind == Kind.Lib and self.is_dynamic and self.target.wantSharedLibSymLinks()) { |
| 1535 | try doAtomicSymLinks(builder.allocator, self.getOutputPath(), self.major_only_filename, self.name_only_filename); | 1531 | try doAtomicSymLinks(builder.allocator, self.getOutputPath(), self.major_only_filename, self.name_only_filename); |
| 1536 | } | 1532 | } |
| 1537 | } | 1533 | } |
| ... | @@ -1677,7 +1673,7 @@ const InstallArtifactStep = struct { | ... | @@ -1677,7 +1673,7 @@ const InstallArtifactStep = struct { |
| 1677 | }; | 1673 | }; |
| 1678 | self.step.dependOn(&artifact.step); | 1674 | self.step.dependOn(&artifact.step); |
| 1679 | builder.pushInstalledFile(self.dest_file); | 1675 | builder.pushInstalledFile(self.dest_file); |
| 1680 | if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) { | 1676 | if (self.artifact.kind == LibExeObjStep.Kind.Lib and self.artifact.is_dynamic) { |
| 1681 | builder.pushInstalledFile(os.path.join( | 1677 | builder.pushInstalledFile(os.path.join( |
| 1682 | builder.allocator, | 1678 | builder.allocator, |
| 1683 | [][]const u8{ builder.lib_dir, artifact.major_only_filename }, | 1679 | [][]const u8{ builder.lib_dir, artifact.major_only_filename }, |
| ... | @@ -1700,11 +1696,11 @@ const InstallArtifactStep = struct { | ... | @@ -1700,11 +1696,11 @@ const InstallArtifactStep = struct { |
| 1700 | LibExeObjStep.Kind.Obj => unreachable, | 1696 | LibExeObjStep.Kind.Obj => unreachable, |
| 1701 | LibExeObjStep.Kind.Test => unreachable, | 1697 | LibExeObjStep.Kind.Test => unreachable, |
| 1702 | LibExeObjStep.Kind.Exe => u32(0o755), | 1698 | LibExeObjStep.Kind.Exe => u32(0o755), |
| 1703 | LibExeObjStep.Kind.Lib => if (self.artifact.static) u32(0o666) else u32(0o755), | 1699 | LibExeObjStep.Kind.Lib => if (!self.artifact.is_dynamic) u32(0o666) else u32(0o755), |
| 1704 | }, | 1700 | }, |
| 1705 | }; | 1701 | }; |
| 1706 | try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); | 1702 | try builder.copyFileMode(self.artifact.getOutputPath(), self.dest_file, mode); |
| 1707 | if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) { | 1703 | if (self.artifact.isDynamicLibrary()) { |
| 1708 | try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename); | 1704 | try doAtomicSymLinks(builder.allocator, self.dest_file, self.artifact.major_only_filename, self.artifact.name_only_filename); |
| 1709 | } | 1705 | } |
| 1710 | } | 1706 | } |