Skip to content

Commit f7655ae

Browse files
authored
Merge pull request #79973 from etcwilde/ewilde/build-script-cmake
[build-script] CMake bootstrapping updates
2 parents bebc332 + 90bf96c commit f7655ae

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

utils/build-script

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,17 @@ def main_normal():
695695
if args.cmake is not None:
696696
toolchain.cmake = args.cmake
697697

698-
cmake = CMake(args=args, toolchain=toolchain)
699-
# Check the CMake version is sufficient on Linux and build from source
700-
# if not.
701-
cmake_path = cmake.check_cmake_version(SWIFT_SOURCE_ROOT, SWIFT_BUILD_ROOT)
702-
if cmake_path is not None:
703-
toolchain.cmake = cmake_path
704-
args.cmake = cmake_path
705-
706698
# Preprocess the arguments to apply defaults.
707699
apply_default_arguments(toolchain, args)
708700

701+
# Create the build script invocation.
702+
invocation = build_script_invocation.BuildScriptInvocation(toolchain, args)
703+
704+
cmake = CMake(args=args, toolchain=toolchain)
705+
toolchain.cmake = cmake.get_cmake_path(source_root=SWIFT_SOURCE_ROOT,
706+
build_root=SWIFT_BUILD_ROOT)
707+
args.cmake = toolchain.cmake
708+
709709
# Validate the arguments.
710710
validate_arguments(toolchain, args)
711711

@@ -716,9 +716,6 @@ def main_normal():
716716
# Capture the output because we don't want to see the stats.
717717
shell.capture([toolchain.sccache, "--show-stats"])
718718

719-
# Create the build script invocation.
720-
invocation = build_script_invocation.BuildScriptInvocation(toolchain, args)
721-
722719
# Sanitize the runtime environment.
723720
initialize_runtime_environment()
724721

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -267,39 +267,53 @@ def build_cmake(self, source_root, build_root):
267267
if not os.path.isdir(cmake_build_dir):
268268
os.makedirs(cmake_build_dir)
269269

270-
cwd = os.getcwd()
271-
os.chdir(cmake_build_dir)
272-
build_jobs = self.args.build_jobs or multiprocessing.cpu_count()
273-
shell.call_without_sleeping([cmake_bootstrap, '--no-qt-gui',
274-
'--parallel=%s' % build_jobs, '--',
275-
'-DCMAKE_USE_OPENSSL=OFF'], echo=True)
276-
shell.call_without_sleeping(['make', '-j%s' % build_jobs],
277-
echo=True)
270+
print("--- Bootstrap Local CMake ---", flush=True)
271+
from swift_build_support.swift_build_support.utils \
272+
import log_time_in_scope
273+
with log_time_in_scope("Bootstrap Local CMake"):
274+
cwd = os.getcwd()
275+
os.chdir(cmake_build_dir)
276+
build_jobs = self.args.build_jobs or multiprocessing.cpu_count()
277+
shell.call_without_sleeping([cmake_bootstrap, '--no-qt-gui',
278+
'--parallel=%s' % build_jobs, '--',
279+
'-DCMAKE_USE_OPENSSL=OFF'], echo=True)
280+
shell.call_without_sleeping(['make', '-j%s' % build_jobs],
281+
echo=True)
278282
os.chdir(cwd)
279283
return os.path.join(cmake_build_dir, 'bin', 'cmake')
280284

281-
# For Linux and FreeBSD only, determine the version of the installed
282-
# CMake compared to the source and build the source if necessary.
283-
# Returns the path to the cmake binary.
284-
def check_cmake_version(self, source_root, build_root):
285-
if not platform.system() in ["Linux", "FreeBSD"]:
286-
return
285+
# Get the path to CMake to use for the build
286+
# This function will not build CMake for Apple platforms.
287+
# For other platforms, this builds CMake if a new enough version is not
288+
# available.
289+
def get_cmake_path(self, source_root, build_root):
290+
if platform.system() == 'Darwin':
291+
return self.toolchain.cmake
287292

288293
cmake_source_dir = os.path.join(source_root, 'cmake')
289-
# If the source is not checked out then don't attempt to build cmake.
290294
if not os.path.isdir(cmake_source_dir):
291-
return
292-
293-
cmake_binary = 'cmake'
294-
try:
295-
if self.args.cmake is not None:
296-
cmake_binary = self.args.cmake
297-
except AttributeError:
298-
cmake_binary = 'cmake'
299-
300-
installed_ver = self.installed_cmake_version(cmake_binary)
301-
if installed_ver >= self.cmake_source_version(cmake_source_dir):
302-
return
303-
else:
304-
# Build CMake from source and return the path to the executable.
305-
return self.build_cmake(source_root, build_root)
295+
return self.toolchain.cmake
296+
297+
cmake_required_version = self.cmake_source_version(cmake_source_dir)
298+
299+
# If we have already built a CMake, see if that is new enough. If it is,
300+
# we don't need to build it again. This is a good indication that the
301+
# system either doesn't have a CMake installed or it wasn't new enough
302+
# so prefer our built CMake first.
303+
cmake_built_path = os.path.join(build_root,
304+
f'cmake-{self.args.host_target}',
305+
'bin', 'cmake')
306+
if os.path.isfile(cmake_built_path):
307+
cmake_built_version = self.installed_cmake_version(cmake_built_path)
308+
if cmake_built_version >= cmake_required_version:
309+
return cmake_built_path
310+
311+
# If we already have a new enough CMake installed on the system, use it
312+
if self.toolchain.cmake is not None:
313+
cmake_installed_version = self.installed_cmake_version(self.toolchain.cmake)
314+
if cmake_installed_version >= cmake_required_version:
315+
return self.toolchain.cmake
316+
317+
# The pre-installed CMake isn't new enough. Build one from our sources
318+
# and return the path to that.
319+
return self.build_cmake(source_root, build_root)

0 commit comments

Comments
 (0)