Skip to content

Commit ec9391e

Browse files
committed
Add ability to use a CMake toolchains file for specifying details of the stage1 CC/CXX
This allows us to follow best/usual CMake practice for configuring cross compiling.
1 parent 4d57130 commit ec9391e

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

zorg/buildbot/builders/ClangBuilder.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def getClangCMakeBuildFactory(
162162
# CMake arguments to use for stage2 instead of extra_cmake_args.
163163
extra_stage2_cmake_args=None,
164164

165+
# If set, use a toolchains file for the stage 2 build and add
166+
# these options to it.
167+
stage2_toolchain_options=None,
168+
165169
# Extra repositories
166170
checkout_clang_tools_extra=True,
167171
checkout_compiler_rt=True,
@@ -180,6 +184,7 @@ def getClangCMakeBuildFactory(
180184
submitURL=submitURL, testerName=testerName,
181185
env=env, extra_cmake_args=extra_cmake_args,
182186
extra_stage2_cmake_args=extra_stage2_cmake_args,
187+
stage2_toolchain_options=stage2_toolchain_options,
183188
checkout_clang_tools_extra=checkout_clang_tools_extra,
184189
checkout_lld=checkout_lld,
185190
checkout_compiler_rt=checkout_compiler_rt,
@@ -219,6 +224,10 @@ def _getClangCMakeBuildFactory(
219224
# CMake arguments to use for stage2 instead of extra_cmake_args.
220225
extra_stage2_cmake_args=None,
221226

227+
# If set, use a toolchains file for the stage 2 build and add
228+
# these options to it.
229+
stage2_toolchain_options=None,
230+
222231
# Extra repositories
223232
checkout_clang_tools_extra=True,
224233
checkout_compiler_rt=True,
@@ -427,9 +436,31 @@ def _getClangCMakeBuildFactory(
427436
# Note: Backslash path separators do not work well with cmake and ninja.
428437
# Forward slash path separator works on Windows as well.
429438
stage1_cc = InterpolateToPosixPath(
430-
f"-DCMAKE_C_COMPILER=%(prop:builddir)s/{stage1_install}/bin/{cc}")
439+
f"%(prop:builddir)s/{stage1_install}/bin/{cc}")
431440
stage1_cxx = InterpolateToPosixPath(
432-
f"-DCMAKE_CXX_COMPILER=%(prop:builddir)s/{stage1_install}/bin/{cxx}")
441+
f"%(prop:builddir)s/{stage1_install}/bin/{cxx}")
442+
443+
# If stage2_toolchain_options is set when we'll use a toolchain file
444+
# to specify the compiler being used (the just-built stage1) and add
445+
# any stage2_toolchain_options to it. Otherwise, just set
446+
# -DCMAKE_{C,CXX}_COMPILER.
447+
if stage2_toolchain_options is None:
448+
compiler_args = [
449+
f"-DCMAKE_C_COMPILER={stage1_cc}",
450+
f"-DCMAKE_CXX_COMPILER={stage1_cxx}"
451+
]
452+
else:
453+
toolchain_file = InterpolateToPosixPath(
454+
f"%(prop:builddir)s/{stage2_build}/stage1-toolchain.cmake")
455+
with open(toolchain_file, 'w') as file:
456+
file.write(f"set(CMAKE_C_COMPILER {stage1_cc})\n")
457+
file.write(f"set(CMAKE_CXX_COMPILER {stage1_cxx})\n")
458+
for option in stage2_toolchain_options:
459+
file.write(f"{option}\n")
460+
461+
compiler_args = [
462+
"-DCMAKE_TOOLCHAIN_FILE={toolchain_file}"
463+
]
433464

434465
# If we have a separate stage2 cmake arg list, then ensure we re-apply
435466
# enable_projects and enable_runtimes if necessary.
@@ -445,13 +476,12 @@ def _getClangCMakeBuildFactory(
445476

446477
rel_src_dir = LLVMBuildFactory.pathRelativeTo(f.llvm_srcdir, stage2_build)
447478
cmake_cmd2 = [cmake, "-G", "Ninja", rel_src_dir,
448-
stage1_cc,
449-
stage1_cxx,
450479
f"-DCMAKE_BUILD_TYPE={stage2_config}",
451480
"-DLLVM_ENABLE_ASSERTIONS=True",
452481
f"-DLLVM_LIT_ARGS={lit_args}",
453482
f"-DCMAKE_INSTALL_PREFIX=../{stage2_install}"
454-
] + (extra_stage2_cmake_args or extra_cmake_args)
483+
] + (extra_stage2_cmake_args or extra_cmake_args) \
484+
+ compiler_args
455485

456486
f.addStep(ShellCommand(name='cmake stage 2',
457487
command=cmake_cmd2,

0 commit comments

Comments
 (0)