authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-01 23:14:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-08-03 09:52:14-07:00
loge565ff305ae208b15058a462d348fde515b3e950
tree31fd4344027677cf9c854d610c1b6ee9eeae4c44
parent9e50f960875601dbaaf7245cedb7d9e429678aeb

CLI: revert -l behavior

chicken out and make -l match the status quo behavior, where it looks for dynamic libraries and then falls back to static libraries. library resolution is still done in the CLI now though, and these options are added: -search_static_first Search for static libs in all library search paths, then dynamic libs. -search_dylibs_only Only search for dynamic libs. -search_static_only Only search for static libs. this matches the already existing options below: -search_paths_first For each library search path, check for dynamic lib then static lib before proceeding to next path. -search_dylibs_first Search for dynamic libs in all library search So, it is still possible to get the strict behavior by passing `-search_dylibs_only` or `-search_static_only`. This commit also makes -dynamic and -static affect the preferred link mode and search strategy.

2 files changed, 102 insertions(+), 58 deletions(-)

cmake/Findllvm.cmake+57-21
......@@ -52,8 +52,6 @@ if(ZIG_USE_LLVM_CONFIG)
5252 set(STATIC_OR_SHARED_LINK "--link-shared")
5353 elseif (ZIG_STATIC_LLVM)
5454 set(STATIC_OR_SHARED_LINK "--link-static")
55 else()
56 set(STATIC_OR_SHARED_LINK "")
5755 endif()
5856
5957 execute_process(
......@@ -105,31 +103,69 @@ if(ZIG_USE_LLVM_CONFIG)
105103 break()
106104 endwhile()
107105
106 if(ZIG_SHARED_LLVM OR ZIG_STATIC_LLVM)
107 execute_process(
108 COMMAND ${LLVM_CONFIG_EXE} --libfiles ${STATIC_OR_SHARED_LINK}
109 OUTPUT_VARIABLE LLVM_LIBRARIES_SPACES
110 OUTPUT_STRIP_TRAILING_WHITESPACE)
111 string(REPLACE " " ";" LLVM_LIBRARIES "${LLVM_LIBRARIES_SPACES}")
108112
109 execute_process(
110 COMMAND ${LLVM_CONFIG_EXE} --shared-mode ${STATIC_OR_SHARED_LINK}
111 OUTPUT_VARIABLE LLVM_LINK_MODE
112 OUTPUT_STRIP_TRAILING_WHITESPACE)
113 execute_process(
114 COMMAND ${LLVM_CONFIG_EXE} --libdir ${STATIC_OR_SHARED_LINK}
115 OUTPUT_VARIABLE LLVM_LIBDIRS_SPACES
116 OUTPUT_STRIP_TRAILING_WHITESPACE)
117 string(REPLACE " " ";" LLVM_LIBDIRS "${LLVM_LIBDIRS_SPACES}")
113118
114 execute_process(
115 COMMAND ${LLVM_CONFIG_EXE} --libfiles ${STATIC_OR_SHARED_LINK}
116 OUTPUT_VARIABLE LLVM_LIBRARIES_SPACES
117 OUTPUT_STRIP_TRAILING_WHITESPACE)
118 string(REPLACE " " ";" LLVM_LIBRARIES "${LLVM_LIBRARIES_SPACES}")
119 execute_process(
120 COMMAND ${LLVM_CONFIG_EXE} --system-libs ${STATIC_OR_SHARED_LINK}
121 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS_SPACES
122 OUTPUT_STRIP_TRAILING_WHITESPACE)
123 string(REPLACE " " ";" LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS_SPACES}")
119124
120 execute_process(
121 COMMAND ${LLVM_CONFIG_EXE} --libdir ${STATIC_OR_SHARED_LINK}
122 OUTPUT_VARIABLE LLVM_LIBDIRS_SPACES
125 execute_process(
126 COMMAND ${LLVM_CONFIG_EXE} --shared-mode ${STATIC_OR_SHARED_LINK}
127 OUTPUT_VARIABLE LLVM_LINK_MODE
123128 OUTPUT_STRIP_TRAILING_WHITESPACE)
124 string(REPLACE " " ";" LLVM_LIBDIRS "${LLVM_LIBDIRS_SPACES}")
129 else()
130 execute_process(
131 COMMAND ${LLVM_CONFIG_EXE} --libs
132 OUTPUT_VARIABLE LLVM_LIBRARIES_SPACES
133 OUTPUT_STRIP_TRAILING_WHITESPACE)
134 string(REPLACE " " ";" LLVM_LIBRARIES "${LLVM_LIBRARIES_SPACES}")
125135
126 execute_process(
127 COMMAND ${LLVM_CONFIG_EXE} --system-libs ${STATIC_OR_SHARED_LINK}
128 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS_SPACES
136 execute_process(
137 COMMAND ${LLVM_CONFIG_EXE} --libdir
138 OUTPUT_VARIABLE LLVM_LIBDIRS_SPACES
139 OUTPUT_STRIP_TRAILING_WHITESPACE)
140 string(REPLACE " " ";" LLVM_LIBDIRS "${LLVM_LIBDIRS_SPACES}")
141
142 execute_process(
143 COMMAND ${LLVM_CONFIG_EXE} --system-libs
144 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS_SPACES
145 OUTPUT_STRIP_TRAILING_WHITESPACE)
146 string(REPLACE " " ";" LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS_SPACES}")
147
148 execute_process(
149 COMMAND ${LLVM_CONFIG_EXE} --shared-mode
150 OUTPUT_VARIABLE LLVM_LINK_MODE
129151 OUTPUT_STRIP_TRAILING_WHITESPACE)
130 string(REPLACE " " ";" LLVM_SYSTEM_LIBS "${LLVM_SYSTEM_LIBS_SPACES}")
152 endif()
153
154 if (${LLVM_LINK_MODE} STREQUAL "shared")
155 # We always ask for the system libs corresponding to static linking,
156 # since on some distros LLD is only available as a static library
157 # and we need these libraries to link it successfully
158 execute_process(
159 COMMAND ${LLVM_CONFIG_EXE} --system-libs --link-static
160 OUTPUT_VARIABLE LLVM_STATIC_SYSTEM_LIBS_SPACES
161 ERROR_QUIET # Some installations have no static libs, we just ignore the failure
162 OUTPUT_STRIP_TRAILING_WHITESPACE)
163 string(REPLACE " " ";" LLVM_STATIC_SYSTEM_LIBS "${LLVM_STATIC_SYSTEM_LIBS_SPACES}")
131164
132 set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
165 set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS} ${LLVM_STATIC_SYSTEM_LIBS})
166 else()
167 set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
168 endif()
133169
134170 execute_process(
135171 COMMAND ${LLVM_CONFIG_EXE} --includedir
......@@ -337,4 +373,4 @@ endif()
337373include(FindPackageHandleStandardArgs)
338374find_package_handle_standard_args(llvm DEFAULT_MSG LLVM_LIBRARIES LLVM_INCLUDE_DIRS)
339375
340mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_LIBDIRS LLVM_LINK_MODE)
376mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_LIBDIRS)
src/main.zig+45-37
......@@ -482,6 +482,10 @@ const usage_build_generic =
482482 \\ lib then static lib before proceeding to next path.
483483 \\ -search_dylibs_first Search for dynamic libs in all library search
484484 \\ paths, then static libs.
485 \\ -search_static_first Search for static libs in all library search
486 \\ paths, then dynamic libs.
487 \\ -search_dylibs_only Only search for dynamic libs.
488 \\ -search_static_only Only search for static libs.
485489 \\ -T[script], --script [script] Use a custom linker script
486490 \\ --version-script [path] Provide a version .map file
487491 \\ --dynamic-linker [path] Set the dynamic interpreter path (usually ld.so)
......@@ -878,8 +882,8 @@ fn buildOutputType(
878882 var hash_style: link.HashStyle = .both;
879883 var entitlements: ?[]const u8 = null;
880884 var pagezero_size: ?u64 = null;
881 var lib_search_strategy: ?SystemLib.SearchStrategy = null;
882 var lib_preferred_mode: ?std.builtin.LinkMode = null;
885 var lib_search_strategy: SystemLib.SearchStrategy = .paths_first;
886 var lib_preferred_mode: std.builtin.LinkMode = .Dynamic;
883887 var headerpad_size: ?u32 = null;
884888 var headerpad_max_install_names: bool = false;
885889 var dead_strip_dylibs: bool = false;
......@@ -1111,6 +1115,15 @@ fn buildOutputType(
11111115 } else if (mem.eql(u8, arg, "-search_dylibs_first")) {
11121116 lib_search_strategy = .mode_first;
11131117 lib_preferred_mode = .Dynamic;
1118 } else if (mem.eql(u8, arg, "-search_static_first")) {
1119 lib_search_strategy = .mode_first;
1120 lib_preferred_mode = .Static;
1121 } else if (mem.eql(u8, arg, "-search_dylibs_only")) {
1122 lib_search_strategy = .no_fallback;
1123 lib_preferred_mode = .Dynamic;
1124 } else if (mem.eql(u8, arg, "-search_static_only")) {
1125 lib_search_strategy = .no_fallback;
1126 lib_preferred_mode = .Static;
11141127 } else if (mem.eql(u8, arg, "-headerpad")) {
11151128 const next_arg = args_iter.nextOrFatal();
11161129 headerpad_size = std.fmt.parseUnsigned(u32, eatIntPrefix(next_arg, 16), 16) catch |err| {
......@@ -1136,8 +1149,8 @@ fn buildOutputType(
11361149 // -l always dynamic links. For static libraries,
11371150 // users are expected to use positional arguments
11381151 // which are always unambiguous.
1139 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1140 .search_strategy = lib_search_strategy orelse .no_fallback,
1152 .preferred_mode = lib_preferred_mode,
1153 .search_strategy = lib_search_strategy,
11411154 });
11421155 } else if (mem.eql(u8, arg, "--needed-library") or
11431156 mem.eql(u8, arg, "-needed-l") or
......@@ -1147,15 +1160,15 @@ fn buildOutputType(
11471160 try system_libs.put(next_arg, .{
11481161 .needed = true,
11491162 .weak = false,
1150 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1151 .search_strategy = lib_search_strategy orelse .no_fallback,
1163 .preferred_mode = lib_preferred_mode,
1164 .search_strategy = lib_search_strategy,
11521165 });
11531166 } else if (mem.eql(u8, arg, "-weak_library") or mem.eql(u8, arg, "-weak-l")) {
11541167 try system_libs.put(args_iter.nextOrFatal(), .{
11551168 .needed = false,
11561169 .weak = true,
1157 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1158 .search_strategy = lib_search_strategy orelse .no_fallback,
1170 .preferred_mode = lib_preferred_mode,
1171 .search_strategy = lib_search_strategy,
11591172 });
11601173 } else if (mem.eql(u8, arg, "-D")) {
11611174 try clang_argv.append(arg);
......@@ -1388,8 +1401,12 @@ fn buildOutputType(
13881401 emit_implib_arg_provided = true;
13891402 } else if (mem.eql(u8, arg, "-dynamic")) {
13901403 link_mode = .Dynamic;
1404 lib_preferred_mode = .Dynamic;
1405 lib_search_strategy = .mode_first;
13911406 } else if (mem.eql(u8, arg, "-static")) {
13921407 link_mode = .Static;
1408 lib_preferred_mode = .Static;
1409 lib_search_strategy = .no_fallback;
13931410 } else if (mem.eql(u8, arg, "-fdll-export-fns")) {
13941411 dll_export_fns = true;
13951412 } else if (mem.eql(u8, arg, "-fno-dll-export-fns")) {
......@@ -1541,22 +1558,22 @@ fn buildOutputType(
15411558 // -l always dynamic links. For static libraries,
15421559 // users are expected to use positional arguments
15431560 // which are always unambiguous.
1544 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1545 .search_strategy = lib_search_strategy orelse .no_fallback,
1561 .preferred_mode = lib_preferred_mode,
1562 .search_strategy = lib_search_strategy,
15461563 });
15471564 } else if (mem.startsWith(u8, arg, "-needed-l")) {
15481565 try system_libs.put(arg["-needed-l".len..], .{
15491566 .needed = true,
15501567 .weak = false,
1551 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1552 .search_strategy = lib_search_strategy orelse .no_fallback,
1568 .preferred_mode = lib_preferred_mode,
1569 .search_strategy = lib_search_strategy,
15531570 });
15541571 } else if (mem.startsWith(u8, arg, "-weak-l")) {
15551572 try system_libs.put(arg["-weak-l".len..], .{
15561573 .needed = false,
15571574 .weak = true,
1558 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1559 .search_strategy = lib_search_strategy orelse .no_fallback,
1575 .preferred_mode = lib_preferred_mode,
1576 .search_strategy = lib_search_strategy,
15601577 });
15611578 } else if (mem.startsWith(u8, arg, "-D")) {
15621579 try clang_argv.append(arg);
......@@ -1632,7 +1649,6 @@ fn buildOutputType(
16321649 var emit_llvm = false;
16331650 var needed = false;
16341651 var must_link = false;
1635 var force_static_libs = false;
16361652 var file_ext: ?Compilation.FileExt = null;
16371653 while (it.has_next) {
16381654 it.next() catch |err| {
......@@ -1702,22 +1718,12 @@ fn buildOutputType(
17021718 .must_link = must_link,
17031719 .loption = true,
17041720 });
1705 } else if (force_static_libs) {
1706 try system_libs.put(it.only_arg, .{
1707 .needed = false,
1708 .weak = false,
1709 .preferred_mode = .Static,
1710 .search_strategy = .no_fallback,
1711 });
17121721 } else {
1713 // C compilers are traditionally expected to look
1714 // first for dynamic libraries and then fall back
1715 // to static libraries.
17161722 try system_libs.put(it.only_arg, .{
17171723 .needed = needed,
17181724 .weak = false,
1719 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1720 .search_strategy = lib_search_strategy orelse .paths_first,
1725 .preferred_mode = lib_preferred_mode,
1726 .search_strategy = lib_search_strategy,
17211727 });
17221728 }
17231729 },
......@@ -1814,13 +1820,15 @@ fn buildOutputType(
18141820 mem.eql(u8, linker_arg, "-dy") or
18151821 mem.eql(u8, linker_arg, "-call_shared"))
18161822 {
1817 force_static_libs = false;
1823 lib_search_strategy = .no_fallback;
1824 lib_preferred_mode = .Dynamic;
18181825 } else if (mem.eql(u8, linker_arg, "-Bstatic") or
18191826 mem.eql(u8, linker_arg, "-dn") or
18201827 mem.eql(u8, linker_arg, "-non_shared") or
18211828 mem.eql(u8, linker_arg, "-static"))
18221829 {
1823 force_static_libs = true;
1830 lib_search_strategy = .no_fallback;
1831 lib_preferred_mode = .Static;
18241832 } else if (mem.eql(u8, linker_arg, "-search_paths_first")) {
18251833 lib_search_strategy = .paths_first;
18261834 lib_preferred_mode = .Dynamic;
......@@ -1939,8 +1947,8 @@ fn buildOutputType(
19391947 .weak_library => try system_libs.put(it.only_arg, .{
19401948 .needed = false,
19411949 .weak = true,
1942 .preferred_mode = lib_preferred_mode orelse .Dynamic,
1943 .search_strategy = lib_search_strategy orelse .paths_first,
1950 .preferred_mode = lib_preferred_mode,
1951 .search_strategy = lib_search_strategy,
19441952 }),
19451953 .weak_framework => try frameworks.put(gpa, it.only_arg, .{ .weak = true }),
19461954 .headerpad_max_install_names => headerpad_max_install_names = true,
......@@ -2240,22 +2248,22 @@ fn buildOutputType(
22402248 try system_libs.put(linker_args_it.nextOrFatal(), .{
22412249 .weak = false,
22422250 .needed = true,
2243 .preferred_mode = lib_preferred_mode orelse .Dynamic,
2244 .search_strategy = lib_search_strategy orelse .paths_first,
2251 .preferred_mode = lib_preferred_mode,
2252 .search_strategy = lib_search_strategy,
22452253 });
22462254 } else if (mem.startsWith(u8, arg, "-weak-l")) {
22472255 try system_libs.put(arg["-weak-l".len..], .{
22482256 .weak = true,
22492257 .needed = false,
2250 .preferred_mode = lib_preferred_mode orelse .Dynamic,
2251 .search_strategy = lib_search_strategy orelse .paths_first,
2258 .preferred_mode = lib_preferred_mode,
2259 .search_strategy = lib_search_strategy,
22522260 });
22532261 } else if (mem.eql(u8, arg, "-weak_library")) {
22542262 try system_libs.put(linker_args_it.nextOrFatal(), .{
22552263 .weak = true,
22562264 .needed = false,
2257 .preferred_mode = lib_preferred_mode orelse .Dynamic,
2258 .search_strategy = lib_search_strategy orelse .paths_first,
2265 .preferred_mode = lib_preferred_mode,
2266 .search_strategy = lib_search_strategy,
22592267 });
22602268 } else if (mem.eql(u8, arg, "-compatibility_version")) {
22612269 const compat_version = linker_args_it.nextOrFatal();