Skip to content

Commit c7001f9

Browse files
committed
[build-script] Provide infrastructure for CMakeProduct subclasses to opt into using the just built swift toolchain.
1 parent 51b08b6 commit c7001f9

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ def __iadd__(self, other):
9090

9191
class CMake(object):
9292

93-
def __init__(self, args, toolchain):
93+
def __init__(self, args, toolchain, prefer_just_built_toolchain=False):
94+
"""If prefer_just_built_toolchain is set to True, we set the clang, clang++,
95+
and Swift compilers from the installed toolchain.
96+
"""
9497
self.args = args
9598
self.toolchain = toolchain
99+
self.prefer_just_built_toolchain = prefer_just_built_toolchain
96100

97-
def common_options(self):
101+
def common_options(self, product=None):
98102
"""Return options used for all products, including LLVM/Clang
99103
"""
100104
args = self.args
@@ -135,8 +139,18 @@ def common_options(self):
135139
if args.cmake_cxx_launcher:
136140
define("CMAKE_CXX_COMPILER_LAUNCHER:PATH", args.cmake_cxx_launcher)
137141

138-
define("CMAKE_C_COMPILER:PATH", toolchain.cc)
139-
define("CMAKE_CXX_COMPILER:PATH", toolchain.cxx)
142+
if self.prefer_just_built_toolchain and product:
143+
toolchain_path = product.install_toolchain_path(args.host_target)
144+
define("CMAKE_C_COMPILER:PATH", os.path.join(toolchain_path,
145+
'bin', 'clang'))
146+
define("CMAKE_CXX_COMPILER:PATH", os.path.join(toolchain_path,
147+
'bin', 'clang++'))
148+
define("CMAKE_Swift_COMPILER:PATH", os.path.join(toolchain_path,
149+
'bin', 'swiftc'))
150+
else:
151+
define("CMAKE_C_COMPILER:PATH", toolchain.cc)
152+
define("CMAKE_CXX_COMPILER:PATH", toolchain.cxx)
153+
define("CMAKE_Swift_COMPILER:PATH", toolchain.swiftc)
140154
define("CMAKE_LIBTOOL:PATH", toolchain.libtool)
141155
define("CMAKE_AR:PATH", toolchain.ar)
142156

utils/swift_build_support/swift_build_support/products/cmake_product.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919

2020
class CMakeProduct(product.Product):
21-
def build_with_cmake(self, build_targets, build_type, build_args):
21+
def build_with_cmake(self, build_targets, build_type, build_args,
22+
prefer_just_built_toolchain=False):
2223
assert self.toolchain.cmake is not None
2324
cmake_build = []
24-
_cmake = cmake.CMake(self.args, self.toolchain)
25+
_cmake = cmake.CMake(self.args, self.toolchain,
26+
prefer_just_built_toolchain)
2527

2628
if self.toolchain.distcc_pump:
2729
cmake_build.append(self.toolchain.distcc_pump)
@@ -52,7 +54,7 @@ def build_with_cmake(self, build_targets, build_type, build_args):
5254

5355
with shell.pushd(self.build_dir):
5456
shell.call([self.toolchain.cmake] + list(self.cmake_options) +
55-
list(_cmake.common_options()) +
57+
list(_cmake.common_options(self)) +
5658
self.args.extra_cmake_options + [self.source_dir],
5759
env=env)
5860

utils/swift_build_support/tests/test_cmake.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def default_args(self):
4747
"""
4848
return Namespace(host_cc="/path/to/clang",
4949
host_cxx="/path/to/clang++",
50+
host_swiftc="/path/to/swiftc",
5051
host_libtool="/path/to/libtool",
5152
host_ar="/path/to/ar",
5253
enable_asan=False,
@@ -81,6 +82,7 @@ def cmake(self, args):
8182
toolchain = host_toolchain()
8283
toolchain.cc = args.host_cc
8384
toolchain.cxx = args.host_cxx
85+
toolchain.swiftc = args.host_swiftc
8486
toolchain.libtool = args.host_libtool
8587
toolchain.ar = args.host_ar
8688
if args.distcc:
@@ -98,6 +100,7 @@ def test_common_options_defaults(self):
98100
["-G", "Ninja",
99101
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
100102
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
103+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
101104
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
102105
"-DCMAKE_AR:PATH=/path/to/ar",
103106
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -112,6 +115,7 @@ def test_common_options_asan(self):
112115
"-DLLVM_USE_SANITIZER=Address",
113116
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
114117
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
118+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
115119
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
116120
"-DCMAKE_AR:PATH=/path/to/ar",
117121
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -126,6 +130,7 @@ def test_common_options_ubsan(self):
126130
"-DLLVM_USE_SANITIZER=Undefined",
127131
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
128132
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
133+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
129134
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
130135
"-DCMAKE_AR:PATH=/path/to/ar",
131136
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -140,6 +145,7 @@ def test_common_options_tsan(self):
140145
"-DLLVM_USE_SANITIZER=Thread",
141146
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
142147
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
148+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
143149
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
144150
"-DCMAKE_AR:PATH=/path/to/ar",
145151
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -155,6 +161,7 @@ def test_common_options_asan_ubsan(self):
155161
"-DLLVM_USE_SANITIZER=Address;Undefined",
156162
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
157163
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
164+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
158165
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
159166
"-DCMAKE_AR:PATH=/path/to/ar",
160167
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -170,6 +177,7 @@ def test_common_options_ubsan_tsan(self):
170177
"-DLLVM_USE_SANITIZER=Undefined;Thread",
171178
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
172179
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
180+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
173181
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
174182
"-DCMAKE_AR:PATH=/path/to/ar",
175183
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -186,6 +194,7 @@ def test_common_options_asan_ubsan_tsan(self):
186194
"-DLLVM_USE_SANITIZER=Address;Undefined;Thread",
187195
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
188196
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
197+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
189198
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
190199
"-DCMAKE_AR:PATH=/path/to/ar",
191200
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -200,6 +209,7 @@ def test_common_options_lsan(self):
200209
"-DLLVM_USE_SANITIZER=Leaks",
201210
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
202211
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
212+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
203213
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
204214
"-DCMAKE_AR:PATH=/path/to/ar",
205215
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -214,6 +224,7 @@ def test_common_options_coverage_sanitizer(self):
214224
"-DLLVM_USE_SANITIZE_COVERAGE=ON",
215225
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
216226
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
227+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
217228
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
218229
"-DCMAKE_AR:PATH=/path/to/ar",
219230
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -228,6 +239,7 @@ def test_common_options_export_compile_commands(self):
228239
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
229240
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
230241
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
242+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
231243
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
232244
"-DCMAKE_AR:PATH=/path/to/ar",
233245
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -243,6 +255,7 @@ def test_common_options_distcc(self):
243255
"-DCMAKE_CXX_COMPILER_LAUNCHER:PATH=" + self.mock_distcc_path(),
244256
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
245257
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
258+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
246259
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
247260
"-DCMAKE_AR:PATH=/path/to/ar",
248261
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -258,6 +271,7 @@ def test_common_options_sccache(self):
258271
"-DCMAKE_CXX_COMPILER_LAUNCHER:PATH=" + self.mock_sccache_path(),
259272
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
260273
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
274+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
261275
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
262276
"-DCMAKE_AR:PATH=/path/to/ar",
263277
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -276,6 +290,7 @@ def test_common_options_launcher(self):
276290
"-DCMAKE_CXX_COMPILER_LAUNCHER:PATH=" + cmake_cxx_launcher,
277291
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
278292
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
293+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
279294
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
280295
"-DCMAKE_AR:PATH=/path/to/ar",
281296
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -289,6 +304,7 @@ def test_common_options_xcode(self):
289304
["-G", "Xcode",
290305
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
291306
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
307+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
292308
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
293309
"-DCMAKE_AR:PATH=/path/to/ar",
294310
"-DCMAKE_CONFIGURATION_TYPES=" +
@@ -303,6 +319,7 @@ def test_common_options_clang_compiler_version(self):
303319
["-G", "Ninja",
304320
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
305321
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
322+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
306323
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
307324
"-DCMAKE_AR:PATH=/path/to/ar",
308325
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -316,6 +333,7 @@ def test_common_options_clang_user_visible_version(self):
316333
["-G", "Ninja",
317334
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
318335
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
336+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
319337
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
320338
"-DCMAKE_AR:PATH=/path/to/ar",
321339
"-DLLVM_VERSION_MAJOR:STRING=9",
@@ -335,6 +353,7 @@ def test_common_options_build_ninja(self):
335353
["-G", "Ninja",
336354
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
337355
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
356+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
338357
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
339358
"-DCMAKE_AR:PATH=/path/to/ar",
340359
"-DCMAKE_MAKE_PROGRAM=" + self.which_ninja(args)])
@@ -359,6 +378,7 @@ def test_common_options_full(self):
359378
"-DCMAKE_CXX_COMPILER_LAUNCHER:PATH=" + self.mock_distcc_path(),
360379
"-DCMAKE_C_COMPILER:PATH=/path/to/clang",
361380
"-DCMAKE_CXX_COMPILER:PATH=/path/to/clang++",
381+
"-DCMAKE_Swift_COMPILER:PATH=/path/to/swiftc",
362382
"-DCMAKE_LIBTOOL:PATH=/path/to/libtool",
363383
"-DCMAKE_AR:PATH=/path/to/ar",
364384
"-DCMAKE_CONFIGURATION_TYPES=" +

0 commit comments

Comments
 (0)