Skip to content

Commit 184d396

Browse files
authored
Fix CMake build to explicitly pick up ar and ranlib from toolchain. (#6729)
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://112478653
1 parent cd3f8f3 commit 184d396

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")
@@ -211,9 +219,11 @@ def parse_build_args(args):
211219
args.build_dirs["llbuild"] = os.path.abspath(args.llbuild_build_dir)
212220

213221
args.swiftc_path = get_swiftc_path(args)
214-
args.clang_path = get_clang_path(args)
215-
args.cmake_path = get_cmake_path(args)
216-
args.ninja_path = get_ninja_path(args)
222+
args.clang_path = get_tool_path(args, "clang")
223+
args.cmake_path = get_tool_path(args, "cmake")
224+
args.ninja_path = get_tool_path(args, "ninja")
225+
args.ar_path = get_tool_path(args, "ar")
226+
args.ranlib_path = get_tool_path(args, "ranlib")
217227
if args.cross_compile_hosts:
218228
if re.match("macosx-", args.cross_compile_hosts):
219229
# Use XCBuild target directory when building for multiple arches.
@@ -256,44 +266,19 @@ def get_swiftc_path(args):
256266
return swiftc_path
257267
error("unable to find swiftc at %s" % swiftc_path)
258268

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

298283
def get_build_target(args, cross_compile=False):
299284
"""Returns the target-triple of the current machine or for cross-compilation."""
@@ -517,6 +502,8 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
517502
"-DCMAKE_Swift_FLAGS='%s'" % swift_flags,
518503
"-DCMAKE_Swift_COMPILER:=%s" % (args.swiftc_path),
519504
"-DCMAKE_C_COMPILER:=%s" % (args.clang_path),
505+
"-DCMAKE_AR:PATH=%s" % (args.ar_path),
506+
"-DCMAKE_RANLIB:PATH=%s" % (args.ranlib_path),
520507
] + cmake_args + [source_path]
521508

522509
if args.verbose:
@@ -550,6 +537,8 @@ def build_llbuild(args):
550537
flags = [
551538
"-DCMAKE_C_COMPILER:=%s" % (args.clang_path),
552539
"-DCMAKE_CXX_COMPILER:=%s" % (args.clang_path),
540+
"-DCMAKE_AR:PATH=%s" % (args.ar_path),
541+
"-DCMAKE_RANLIB:PATH=%s" % (args.ranlib_path),
553542
"-DLLBUILD_SUPPORT_BINDINGS:=Swift",
554543
]
555544
cmake_env = []

0 commit comments

Comments
 (0)