Skip to content

Commit bc51350

Browse files
authored
Fix CMake build to explicitly pick up ar and ranlib from toolchain. (#6754)
CMake under various conditions will attempt to use `llvm-ar` and `llvm-ranlib` when it spots that we're building using `clang`, and while that works fine on Linux, it doesn't work so well on Darwin, leaving us with a link failure with a perplexing error message. The rest of the Swift build is resilient to this problem already, but the `bootstrap` script here doesn't pass the relevant options to CMake, so it's possible to end up with the build failing when trying to link `lib/libCYaml.a` after the build has mangled the static library with `llvm-ranlib`. rdar://113028933
1 parent 4a07547 commit bc51350

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

Utilities/bootstrap

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ def add_build_args(parser):
103103
'--ninja-path',
104104
metavar='PATH',
105105
help='path to the ninja binary to use for building with CMake')
106+
parser.add_argument(
107+
'--ar-path',
108+
metavar='PATH',
109+
help='path to the ar binary to use for building with CMake')
110+
parser.add_argument(
111+
'--ranlib-path',
112+
metavar='PATH',
113+
help='path to the ranlib binary to use for building with CMake')
106114
parser.add_argument(
107115
"--dispatch-build-dir",
108116
help="path to Dispatch build directory")
@@ -208,9 +216,11 @@ def parse_build_args(args):
208216
args.build_dirs["llbuild"] = os.path.abspath(args.llbuild_build_dir)
209217

210218
args.swiftc_path = get_swiftc_path(args)
211-
args.clang_path = get_clang_path(args)
212-
args.cmake_path = get_cmake_path(args)
213-
args.ninja_path = get_ninja_path(args)
219+
args.clang_path = get_tool_path(args, "clang")
220+
args.cmake_path = get_tool_path(args, "cmake")
221+
args.ninja_path = get_tool_path(args, "ninja")
222+
args.ar_path = get_tool_path(args, "ar")
223+
args.ranlib_path = get_tool_path(args, "ranlib")
214224
if args.cross_compile_hosts:
215225
if re.match("macosx-", args.cross_compile_hosts):
216226
# Use XCBuild target directory when building for multiple arches.
@@ -253,44 +263,19 @@ def get_swiftc_path(args):
253263
return swiftc_path
254264
error("unable to find swiftc at %s" % swiftc_path)
255265

256-
def get_clang_path(args):
257-
"""Returns the path to the Clang compiler."""
258-
if args.clang_path:
259-
return os.path.abspath(args.clang_path)
260-
elif platform.system() == 'Darwin':
261-
return call_output(
262-
["xcrun", "--find", "clang"],
263-
stderr=subprocess.PIPE,
264-
verbose=args.verbose
265-
)
266-
else:
267-
return call_output(["which", "clang"], verbose=args.verbose)
268-
269-
def get_cmake_path(args):
270-
"""Returns the path to CMake."""
271-
if args.cmake_path:
272-
return os.path.abspath(args.cmake_path)
273-
elif platform.system() == 'Darwin':
274-
return call_output(
275-
["xcrun", "--find", "cmake"],
276-
stderr=subprocess.PIPE,
277-
verbose=args.verbose
278-
)
279-
else:
280-
return call_output(["which", "cmake"], verbose=args.verbose)
281-
282-
def get_ninja_path(args):
283-
"""Returns the path to Ninja."""
284-
if args.ninja_path:
285-
return os.path.abspath(args.ninja_path)
266+
def get_tool_path(args, tool):
267+
"""Returns the path to the specified tool."""
268+
path = getattr(args, tool + "_path", None)
269+
if path is not None:
270+
return os.path.abspath(path)
286271
elif platform.system() == 'Darwin':
287272
return call_output(
288-
["xcrun", "--find", "ninja"],
273+
["xcrun", "--find", tool],
289274
stderr=subprocess.PIPE,
290275
verbose=args.verbose
291276
)
292277
else:
293-
return call_output(["which", "ninja"], verbose=args.verbose)
278+
return call_output(["which", tool], verbose=args.verbose)
294279

295280
def get_build_target(args, cross_compile=False):
296281
"""Returns the target-triple of the current machine or for cross-compilation."""
@@ -504,6 +489,8 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
504489
"-DCMAKE_Swift_FLAGS='%s'" % swift_flags,
505490
"-DCMAKE_Swift_COMPILER:=%s" % (args.swiftc_path),
506491
"-DCMAKE_C_COMPILER:=%s" % (args.clang_path),
492+
"-DCMAKE_AR:PATH=%s" % (args.ar_path),
493+
"-DCMAKE_RANLIB:PATH=%s" % (args.ranlib_path),
507494
] + cmake_args + [source_path]
508495

509496
if args.verbose:
@@ -537,6 +524,8 @@ def build_llbuild(args):
537524
flags = [
538525
"-DCMAKE_C_COMPILER:=%s" % (args.clang_path),
539526
"-DCMAKE_CXX_COMPILER:=%s" % (args.clang_path),
527+
"-DCMAKE_AR:PATH=%s" % (args.ar_path),
528+
"-DCMAKE_RANLIB:PATH=%s" % (args.ranlib_path),
540529
"-DLLBUILD_SUPPORT_BINDINGS:=Swift",
541530
]
542531
cmake_env = []

0 commit comments

Comments
 (0)