Skip to content

Commit 4926ed5

Browse files
committed
[build-script][SR-237] Migrated calculation of BUILD_ARGS to Python.
Calculate `BUILD_ARGS` in `build-script`, then pass them to `build-script-impl` using existing `--build-args` argument.
1 parent c025590 commit 4926ed5

File tree

5 files changed

+172
-41
lines changed

5 files changed

+172
-41
lines changed

utils/build-script

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,19 @@ details of the setups of other systems or automated environments.""")
849849
type=argparse_shell_split,
850850
default=[])
851851

852+
parser.add_argument(
853+
"--build-args",
854+
help="arguments to the build tool. This would be prepended to the "
855+
"default argument that is '-j8' when CMake generator is "
856+
"\"Ninja\".",
857+
type=argparse_shell_split,
858+
default=[])
859+
860+
parser.add_argument(
861+
"--verbose-build",
862+
help="print the commands executed during the build",
863+
action="store_true")
864+
852865
args = migration.parse_args(parser, sys.argv[1:])
853866

854867
build_script_impl = os.path.join(
@@ -1258,6 +1271,8 @@ details of the setups of other systems or automated environments.""")
12581271
"--workspace", SWIFT_SOURCE_ROOT,
12591272
"--common-cmake-options=%s" % ' '.join(
12601273
pipes.quote(opt) for opt in cmake.common_options()),
1274+
"--build-args=%s" % ' '.join(
1275+
pipes.quote(arg) for arg in cmake.build_args()),
12611276
]
12621277

12631278
if args.distcc:
@@ -1273,6 +1288,8 @@ details of the setups of other systems or automated environments.""")
12731288
build_script_impl_args += [
12741289
"--clang-compiler-version", args.clang_compiler_version
12751290
]
1291+
if args.verbose_build:
1292+
build_script_impl_args += ["--verbose-build"]
12761293
if args.install_symroot:
12771294
build_script_impl_args += [
12781295
"--install-symroot", os.path.abspath(args.install_symroot)

utils/build-script-impl

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,12 @@ if [[ "${SKIP_BUILD_WATCHOS_SIMULATOR}" ]] ; then
774774
SKIP_TEST_WATCHOS_SIMULATOR=1
775775
fi
776776

777+
# FIXME: We currently do not support building compiler-rt with the
778+
# Xcode generator.
779+
if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
780+
SKIP_COMPILER_RT=1
781+
fi
782+
777783
# FIXME: We currently do not support cross-compiling swift with compiler-rt.
778784
if [[ "${CROSS_COMPILE_TOOLS_DEPLOYMENT_TARGETS}" ]]; then
779785
SKIP_COMPILER_RT=1
@@ -1131,39 +1137,12 @@ COMMON_C_FLAGS=""
11311137
# Convert to an array.
11321138
eval COMMON_CMAKE_OPTIONS=(${COMMON_CMAKE_OPTIONS})
11331139
eval EXTRA_CMAKE_OPTIONS=(${EXTRA_CMAKE_OPTIONS})
1140+
eval BUILD_ARGS=(${BUILD_ARGS})
11341141

1135-
CMAKE_JOBS="${BUILD_JOBS}"
1136-
if [[ "${DISTCC}" ]] ; then
1137-
CMAKE_JOBS="$(distcc -j)"
1142+
if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
1143+
BUILD_TARGET_FLAG="-target"
11381144
fi
11391145

1140-
case "${CMAKE_GENERATOR}" in
1141-
Ninja)
1142-
BUILD_ARGS="${BUILD_ARGS} -j${CMAKE_JOBS}"
1143-
if [[ "${VERBOSE_BUILD}" ]] ; then
1144-
BUILD_ARGS="${BUILD_ARGS} -v"
1145-
fi
1146-
;;
1147-
'Unix Makefiles')
1148-
BUILD_ARGS="${BUILD_ARGS} -j${CMAKE_JOBS}"
1149-
if [[ "${VERBOSE_BUILD}" ]] ; then
1150-
BUILD_ARGS="${BUILD_ARGS} VERBOSE=1"
1151-
fi
1152-
;;
1153-
Xcode)
1154-
# -parallelizeTargets is an unsupported flag from the Xcode 3 days,
1155-
# but since we're not using proper Xcode 4 schemes, this is the
1156-
# only way to get target-level parallelism.
1157-
BUILD_ARGS="${BUILD_ARGS} -parallelizeTargets"
1158-
BUILD_ARGS="${BUILD_ARGS} -jobs ${CMAKE_JOBS}"
1159-
BUILD_TARGET_FLAG="-target"
1160-
1161-
# FIXME: We currently do not support building compiler-rt with the
1162-
# Xcode generator.
1163-
SKIP_COMPILER_RT=1
1164-
;;
1165-
esac
1166-
11671146
function build_directory() {
11681147
deployment_target=$1
11691148
product=$2
@@ -2001,7 +1980,7 @@ for deployment_target in "${HOST_TARGET}" "${CROSS_TOOLS_DEPLOYMENT_TARGETS[@]}"
20011980
fi
20021981

20031982
set -x
2004-
${DISTCC_PUMP} "${CMAKE}" --build "${build_dir}" $(cmake_config_opt ${product}) -- ${BUILD_ARGS} ${build_targets[@]}
1983+
${DISTCC_PUMP} "${CMAKE}" --build "${build_dir}" $(cmake_config_opt ${product}) -- "${BUILD_ARGS[@]}" ${build_targets[@]}
20051984
{ set +x; } 2>/dev/null
20061985
fi
20071986

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
#
1515
# ----------------------------------------------------------------------------
1616

17-
import re
18-
import platform
1917
from numbers import Number
18+
import platform
19+
import re
20+
import subprocess
2021

2122
from . import xcrun
2223
from .which import which
@@ -136,3 +137,30 @@ def common_options(self):
136137
define("LLVM_VERSION_PATCH:STRING", m.group(3))
137138

138139
return options
140+
141+
def build_args(self):
142+
"""Return arguments to the build tool used for all products
143+
"""
144+
args = self.args
145+
jobs = args.build_jobs
146+
if args.distcc:
147+
jobs = str(
148+
subprocess.check_output([self.host_distcc, '-j'])).rstrip()
149+
150+
build_args = list(args.build_args)
151+
152+
if args.cmake_generator == 'Ninja':
153+
build_args += ['-j%s' % jobs]
154+
if args.verbose_build:
155+
build_args += ['-v']
156+
157+
elif args.cmake_generator == 'Unix Makefiles':
158+
build_args += ['-j%s' % jobs]
159+
if args.verbose_build:
160+
build_args += ['VERBOSE=1']
161+
162+
elif args.cmake_generator == 'Xcode':
163+
build_args += ['-parallelizeTargets',
164+
'-jobs', str(jobs)]
165+
166+
return build_args
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# mock-distcc - discc mock used from tests ----------------------*- python -*-
3+
#
4+
# This source file is part of the Swift.org open source project
5+
#
6+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
7+
# Licensed under Apache License v2.0 with Runtime Library Exception
8+
#
9+
# See http://swift.org/LICENSE.txt for license information
10+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
12+
from __future__ import print_function
13+
14+
import sys
15+
16+
17+
try:
18+
if sys.argv[1] == '-j':
19+
print('6')
20+
sys.exit(0)
21+
except Exception:
22+
pass
23+
24+
print("Error: invalid option", file=sys.stderr)
25+
sys.exit(1)

utils/swift_build_support/tests/test_cmake.py

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,37 @@ def test_cmake_available_on_this_platform(self):
2929

3030
class CMakeTestCase(unittest.TestCase):
3131

32+
def mock_distcc_path(self):
33+
"""Return a path string of mock distcc executable
34+
"""
35+
return os.path.join(os.path.dirname(__file__),
36+
'mock-distcc')
37+
3238
def default_args(self):
33-
"Return new args object with default values"
39+
"""Return new args object with default values
40+
"""
3441
return Namespace(host_cc="/path/to/clang",
3542
host_cxx="/path/to/clang++",
3643
enable_asan=False,
3744
enable_ubsan=False,
3845
export_compile_commands=False,
3946
distcc=False,
4047
cmake_generator="Ninja",
41-
clang_compiler_version=None)
48+
clang_compiler_version=None,
49+
build_jobs=8,
50+
build_args=[],
51+
verbose_build=False)
4252

4353
def cmake(self, args):
44-
"Return new CMake object initialized with given args"
54+
"""Return new CMake object initialized with given args
55+
"""
56+
host_distcc = None
57+
if args.distcc:
58+
host_distcc = self.mock_distcc_path()
4559
return CMake(args=args,
4660
host_cc=args.host_cc,
4761
host_cxx=args.host_cxx,
48-
host_distcc="/path/to/distcc")
62+
host_distcc=host_distcc)
4963

5064
def test_common_options_defaults(self):
5165
args = self.default_args()
@@ -108,9 +122,9 @@ def test_common_options_distcc(self):
108122
self.assertEqual(
109123
list(cmake.common_options()),
110124
["-G", "Ninja",
111-
"-DCMAKE_C_COMPILER:PATH=/path/to/distcc",
125+
"-DCMAKE_C_COMPILER:PATH=" + self.mock_distcc_path(),
112126
"-DCMAKE_C_COMPILER_ARG1=/path/to/clang",
113-
"-DCMAKE_CXX_COMPILER:PATH=/path/to/distcc",
127+
"-DCMAKE_CXX_COMPILER:PATH=" + self.mock_distcc_path(),
114128
"-DCMAKE_CXX_COMPILER_ARG1=/path/to/clang++"])
115129

116130
def test_common_options_xcode(self):
@@ -152,16 +166,84 @@ def test_common_options_full(self):
152166
["-G", "Xcode",
153167
"-DLLVM_USE_SANITIZER=Address;Undefined",
154168
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
155-
"-DCMAKE_C_COMPILER:PATH=/path/to/distcc",
169+
"-DCMAKE_C_COMPILER:PATH=" + self.mock_distcc_path(),
156170
"-DCMAKE_C_COMPILER_ARG1=/path/to/clang",
157-
"-DCMAKE_CXX_COMPILER:PATH=/path/to/distcc",
171+
"-DCMAKE_CXX_COMPILER:PATH=" + self.mock_distcc_path(),
158172
"-DCMAKE_CXX_COMPILER_ARG1=/path/to/clang++",
159173
"-DCMAKE_CONFIGURATION_TYPES=" +
160174
"Debug;Release;MinSizeRel;RelWithDebInfo",
161175
"-DLLVM_VERSION_MAJOR:STRING=3",
162176
"-DLLVM_VERSION_MINOR:STRING=8",
163177
"-DLLVM_VERSION_PATCH:STRING=0"])
164178

179+
def test_build_args_ninja(self):
180+
args = self.default_args()
181+
cmake = self.cmake(args)
182+
self.assertEqual(
183+
list(cmake.build_args()),
184+
["-j8"])
185+
186+
args.verbose_build = True
187+
cmake = self.cmake(args)
188+
self.assertEqual(
189+
list(cmake.build_args()),
190+
["-j8", "-v"])
191+
192+
def test_build_args_makefile(self):
193+
args = self.default_args()
194+
args.cmake_generator = "Unix Makefiles"
195+
cmake = self.cmake(args)
196+
self.assertEqual(
197+
list(cmake.build_args()),
198+
["-j8"])
199+
200+
args.verbose_build = True
201+
cmake = self.cmake(args)
202+
self.assertEqual(
203+
list(cmake.build_args()),
204+
["-j8", "VERBOSE=1"])
205+
206+
def test_build_args_xcode(self):
207+
args = self.default_args()
208+
args.cmake_generator = "Xcode"
209+
cmake = self.cmake(args)
210+
self.assertEqual(
211+
list(cmake.build_args()),
212+
["-parallelizeTargets", "-jobs", "8"])
213+
214+
# NOTE: Xcode generator DOES NOT take 'verbose-build' into account.
215+
args.verbose_build = True
216+
cmake = self.cmake(args)
217+
self.assertEqual(
218+
list(cmake.build_args()),
219+
["-parallelizeTargets", "-jobs", "8"])
220+
221+
def test_build_args_eclipse_ninja(self):
222+
# NOTE: Eclipse generator DOES NOT take 'build-jobs' into account,
223+
# nor 'verbose-build'.
224+
args = self.default_args()
225+
args.cmake_generator = "Eclipse CDT4 - Ninja"
226+
args.verbose_build = True
227+
cmake = self.cmake(args)
228+
self.assertEqual(
229+
list(cmake.build_args()), [])
230+
231+
def test_build_args_custom_build_args(self):
232+
args = self.default_args()
233+
args.build_args = ["-foo", "bar baz"]
234+
cmake = self.cmake(args)
235+
self.assertEqual(
236+
list(cmake.build_args()),
237+
["-foo", "bar baz", "-j8"])
238+
239+
def test_build_args_distcc(self):
240+
args = self.default_args()
241+
args.distcc = True
242+
cmake = self.cmake(args)
243+
self.assertEqual(
244+
list(cmake.build_args()),
245+
["-j6"])
246+
165247

166248
class CMakeOptionsTestCase(unittest.TestCase):
167249
def test_define(self):

0 commit comments

Comments
 (0)