Skip to content

Commit c025590

Browse files
committed
[build-script][SR-237] Migrated calculation of COMMON_CMAKE_OPTIONS to Python.
Calculate `COMMON_CMAKE_OPTIONS` in `build-script`, then pass them to `build-script-impl` using newly introduced `--common-cmake-option` argument.
1 parent ebc3e77 commit c025590

File tree

4 files changed

+428
-88
lines changed

4 files changed

+428
-88
lines changed

utils/build-script

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ from swift_build_support import migration # noqa (E402)
4848
import swift_build_support.ninja # noqa (E402)
4949
import swift_build_support.tar # noqa (E402)
5050
import swift_build_support.targets # noqa (E402)
51+
from swift_build_support.cmake import CMake # noqa (E402)
5152

5253

5354
# A strict parser for bools (unlike Python's `bool()`, where
@@ -812,6 +813,33 @@ details of the setups of other systems or automated environments.""")
812813
help="Path to a directory containing headers libicui18n",
813814
metavar="PATH")
814815

816+
parser.add_argument(
817+
"--host-cc",
818+
help="the absolute path to CC, the 'clang' compiler for the host "
819+
"platform. Default is auto detected.",
820+
metavar="PATH")
821+
parser.add_argument(
822+
"--host-cxx",
823+
help="the absolute path to CXX, the 'clang++' compiler for the host "
824+
"platform. Default is auto detected.",
825+
metavar="PATH")
826+
parser.add_argument(
827+
"--distcc",
828+
help="use distcc in pump mode",
829+
action="store_true")
830+
parser.add_argument(
831+
"--enable-asan",
832+
help="enable Address Sanitizer",
833+
action="store_true")
834+
parser.add_argument(
835+
"--enable-ubsan",
836+
help="enable Undefined Behavior Sanitizer",
837+
action="store_true")
838+
parser.add_argument(
839+
"--clang-compiler-version",
840+
help="string that indicates a compiler version for Clang",
841+
metavar="MAJOR.MINOR.PATCH")
842+
815843
parser.add_argument(
816844
"--extra-cmake-options",
817845
help="Pass through extra options to CMake in the form of comma "
@@ -955,11 +983,6 @@ details of the setups of other systems or automated environments.""")
955983

956984
build_script_impl_inferred_args = []
957985

958-
if args.export_compile_commands:
959-
build_script_impl_inferred_args += [
960-
"--export-compile-commands"
961-
]
962-
963986
if not args.test and not args.validation_test and not args.long_test:
964987
build_script_impl_inferred_args += [
965988
"--skip-test-cmark",
@@ -1166,9 +1189,16 @@ details of the setups of other systems or automated environments.""")
11661189

11671190
host_clang = swift_build_support.toolchain.host_clang(
11681191
xcrun_toolchain=args.darwin_xcrun_toolchain)
1169-
if not host_clang:
1192+
if args.host_cc is None and host_clang is not None:
1193+
args.host_cc = str(host_clang.cc)
1194+
if args.host_cxx is None and host_clang is not None:
1195+
args.host_cxx = str(host_clang.cxx)
1196+
is_host_clang_ok = (
1197+
args.host_cc is not None and os.access(args.host_cc, os.X_OK) and
1198+
args.host_cxx is not None and os.access(args.host_cxx, os.X_OK))
1199+
if not is_host_clang_ok:
11701200
print_with_argv0(
1171-
"Can't find clang. Please install clang-3.5 or a later version.")
1201+
"Can't find clang. Please install clang-3.5 or a later version.")
11721202
return 1
11731203

11741204
host_cmake = args.cmake
@@ -1178,14 +1208,38 @@ details of the setups of other systems or automated environments.""")
11781208
if not host_cmake:
11791209
print_with_argv0("Can't find CMake. Please install CMake.")
11801210
return 1
1211+
1212+
# distcc usage.
1213+
host_distcc = None
1214+
host_distcc_pump = None
1215+
if args.distcc:
1216+
host_distcc = swift_build_support.which('distcc')
1217+
# On some platforms, 'pump' may be unrelated to distcc, in which case
1218+
# it's called 'distcc-pump'.
1219+
host_distcc_pump = swift_build_support.which('distcc-pump')
1220+
if host_distcc_pump is None:
1221+
host_distcc_pump = swift_build_support.which('pump')
1222+
1223+
if host_distcc is None or host_distcc_pump is None:
1224+
print_with_argv0(
1225+
"Can't find distcc. Please install distcc")
1226+
1227+
host_distcc = str(host_distcc)
1228+
host_distcc_pump = str(host_distcc_pump)
1229+
1230+
cmake = CMake(args=args,
1231+
host_cc=args.host_cc,
1232+
host_cxx=args.host_cxx,
1233+
host_distcc=host_distcc)
1234+
11811235
build_script_impl_args = [
11821236
"--build-dir", build_dir,
11831237
"--install-prefix", os.path.abspath(args.install_prefix),
11841238
"--host-target", args.host_target,
11851239
"--stdlib-deployment-targets",
11861240
" ".join(args.stdlib_deployment_targets),
1187-
"--host-cc", host_clang.cc,
1188-
"--host-cxx", host_clang.cxx,
1241+
"--host-cc", args.host_cc,
1242+
"--host-cxx", args.host_cxx,
11891243
"--darwin-xcrun-toolchain", args.darwin_xcrun_toolchain,
11901244
"--cmake", host_cmake,
11911245
"--cmark-build-type", args.cmark_build_variant,
@@ -1201,8 +1255,24 @@ details of the setups of other systems or automated environments.""")
12011255
args.swift_analyze_code_coverage).lower(),
12021256
"--cmake-generator", args.cmake_generator,
12031257
"--build-jobs", str(args.build_jobs),
1204-
"--workspace", SWIFT_SOURCE_ROOT
1258+
"--workspace", SWIFT_SOURCE_ROOT,
1259+
"--common-cmake-options=%s" % ' '.join(
1260+
pipes.quote(opt) for opt in cmake.common_options()),
12051261
]
1262+
1263+
if args.distcc:
1264+
build_script_impl_args += [
1265+
"--distcc",
1266+
"--distcc-pump=%s" % host_distcc_pump
1267+
]
1268+
if args.enable_asan:
1269+
build_script_impl_args += ["--enable-asan"]
1270+
if args.enable_ubsan:
1271+
build_script_impl_args += ["--enable-ubsan"]
1272+
if args.clang_compiler_version:
1273+
build_script_impl_args += [
1274+
"--clang-compiler-version", args.clang_compiler_version
1275+
]
12061276
if args.install_symroot:
12071277
build_script_impl_args += [
12081278
"--install-symroot", os.path.abspath(args.install_symroot)

utils/build-script-impl

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ KNOWN_SETTINGS=(
8989
enable-ubsan "" "enable Undefined Behavior Sanitizer"
9090
cmake "" "path to the cmake binary"
9191
distcc "" "use distcc in pump mode"
92+
distcc-pump "" "the path to distcc pump executable. This argument is required if distcc is set."
9293
build-runtime-with-host-compiler "1" "use the host c++ compiler to build everything"
9394
cmake-generator "Unix Makefiles" "kind of build system to generate; see output of 'cmake --help' for choices"
9495
verbose-build "" "print the commands executed during the build"
@@ -218,8 +219,8 @@ KNOWN_SETTINGS=(
218219
android-icu-uc-include "" "Path to a directory containing headers for libicuuc"
219220
android-icu-i18n "" "Path to a directory containing libicui18n.so"
220221
android-icu-i18n-include "" "Path to a directory containing headers libicui18n"
221-
export-compile-commands "" "set to generate JSON compilation databases for each build product"
222222
check-args-only "" "set to check all arguments are known. Exit with status 0 if success, non zero otherwise"
223+
common-cmake-options "" "CMake options used for all targets, including LLVM/Clang"
223224
# TODO: Remove this some time later.
224225
user-config-args "" "**Renamed to --extra-cmake-options**: User-supplied arguments to cmake when used to do configuration."
225226
)
@@ -247,13 +248,6 @@ function to_varname() {
247248
toupper "${1//-/_}"
248249
}
249250

250-
function join_array_with_delimiter() {
251-
# $1 is the delimiter
252-
local IFS="$1"
253-
shift
254-
echo "$*"
255-
}
256-
257251
function set_lldb_build_mode() {
258252
LLDB_BUILD_MODE="CustomSwift-${LLDB_BUILD_TYPE}"
259253
}
@@ -1132,64 +1126,12 @@ if ! [[ "${SKIP_TEST_BENCHMARKS}" ]] &&
11321126
fi
11331127
echo
11341128

1135-
# CMake options used for all targets, including LLVM/Clang
1136-
COMMON_CMAKE_OPTIONS=(
1137-
-G "${CMAKE_GENERATOR}"
1138-
)
1139-
11401129
COMMON_C_FLAGS=""
1141-
SANITIZERS=()
1142-
if [[ "${ENABLE_ASAN}" ]] ; then
1143-
SANITIZERS=(
1144-
"${SANITIZERS[@]}"
1145-
"Address"
1146-
)
1147-
fi
1148-
1149-
if [[ "${ENABLE_UBSAN}" ]] ; then
1150-
SANITIZERS=(
1151-
"${SANITIZERS[@]}"
1152-
"Undefined"
1153-
)
1154-
fi
1155-
1156-
if [[ -n "${SANITIZERS[@]}" ]] ; then
1157-
COMMON_CMAKE_OPTIONS=(
1158-
"${COMMON_CMAKE_OPTIONS[@]}"
1159-
-DLLVM_USE_SANITIZER=$(join_array_with_delimiter ";" "${SANITIZERS[@]}")
1160-
)
1161-
fi
1162-
1163-
if [[ "${EXPORT_COMPILE_COMMANDS}" ]] ; then
1164-
COMMON_CMAKE_OPTIONS=(
1165-
"${COMMON_CMAKE_OPTIONS[@]}"
1166-
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
1167-
)
1168-
fi
11691130

11701131
# Convert to an array.
1132+
eval COMMON_CMAKE_OPTIONS=(${COMMON_CMAKE_OPTIONS})
11711133
eval EXTRA_CMAKE_OPTIONS=(${EXTRA_CMAKE_OPTIONS})
11721134

1173-
if [[ "${DISTCC}" ]] ; then
1174-
# On some platforms, 'pump' may be unrelated to distcc, in which case it's
1175-
# called 'distcc-pump'.
1176-
DISTCC_PUMP="$(which distcc-pump || which pump)"
1177-
COMMON_CMAKE_OPTIONS=(
1178-
"${COMMON_CMAKE_OPTIONS[@]}"
1179-
-DCMAKE_C_COMPILER:PATH="$(which distcc)"
1180-
-DCMAKE_C_COMPILER_ARG1="${HOST_CC}"
1181-
-DCMAKE_CXX_COMPILER:PATH="$(which distcc)"
1182-
-DCMAKE_CXX_COMPILER_ARG1="${HOST_CXX}"
1183-
)
1184-
else
1185-
COMMON_CMAKE_OPTIONS=(
1186-
"${COMMON_CMAKE_OPTIONS[@]}"
1187-
-DCMAKE_C_COMPILER:PATH="${HOST_CC}"
1188-
-DCMAKE_CXX_COMPILER:PATH="${HOST_CXX}"
1189-
)
1190-
fi
1191-
1192-
11931135
CMAKE_JOBS="${BUILD_JOBS}"
11941136
if [[ "${DISTCC}" ]] ; then
11951137
CMAKE_JOBS="$(distcc -j)"
@@ -1215,29 +1157,13 @@ case "${CMAKE_GENERATOR}" in
12151157
BUILD_ARGS="${BUILD_ARGS} -parallelizeTargets"
12161158
BUILD_ARGS="${BUILD_ARGS} -jobs ${CMAKE_JOBS}"
12171159
BUILD_TARGET_FLAG="-target"
1218-
COMMON_CMAKE_OPTIONS=(
1219-
"${COMMON_CMAKE_OPTIONS[@]}"
1220-
-DCMAKE_CONFIGURATION_TYPES="Debug;Release;MinSizeRel;RelWithDebInfo"
1221-
)
12221160

12231161
# FIXME: We currently do not support building compiler-rt with the
12241162
# Xcode generator.
12251163
SKIP_COMPILER_RT=1
12261164
;;
12271165
esac
12281166

1229-
if [[ "${CLANG_COMPILER_VERSION}" ]] ; then
1230-
major_version=$(echo "${CLANG_COMPILER_VERSION}" | sed -e 's/\([0-9]*\).*/\1/')
1231-
minor_version=$(echo "${CLANG_COMPILER_VERSION}" | sed -e 's/[^.]*\.\([0-9]*\).*/\1/')
1232-
patch_version=$(echo "${CLANG_COMPILER_VERSION}" | sed -e 's/[^.]*\.[^.]*\.\([0-9]*\)/\1/')
1233-
COMMON_CMAKE_OPTIONS=(
1234-
"${COMMON_CMAKE_OPTIONS[@]}"
1235-
-DLLVM_VERSION_MAJOR:STRING="${major_version}"
1236-
-DLLVM_VERSION_MINOR:STRING="${minor_version}"
1237-
-DLLVM_VERSION_PATCH:STRING="${patch_version}"
1238-
)
1239-
fi
1240-
12411167
function build_directory() {
12421168
deployment_target=$1
12431169
product=$2

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#
1515
# ----------------------------------------------------------------------------
1616

17+
import re
1718
import platform
19+
from numbers import Number
1820

1921
from . import xcrun
2022
from .which import which
@@ -34,3 +36,103 @@ def host_cmake(xcrun_toolchain):
3436
return cmake
3537
else:
3638
return '/usr/local/bin/cmake'
39+
40+
41+
class CMakeOptions(object):
42+
"""List like object used to define cmake options
43+
"""
44+
45+
def __init__(self):
46+
self._options = []
47+
48+
def define(self, var, value):
49+
"""Utility to define cmake options in this object.
50+
51+
opts.define("FOO", "BAR") # -> -DFOO=BAR
52+
opts.define("FLAG:BOOL", True) # -> -FLAG:BOOL=TRUE
53+
"""
54+
if var.endswith(':BOOL'):
55+
value = self.true_false(value)
56+
if value is None:
57+
value = ""
58+
elif not isinstance(value, (str, Number)):
59+
raise ValueError('define: invalid value: %s' % value)
60+
self._options.append('-D%s=%s' % (var, value))
61+
62+
@staticmethod
63+
def true_false(value):
64+
if hasattr(value, 'lower'):
65+
value = value.lower()
66+
if value in [True, 1, 'true', 'yes', '1']:
67+
return 'TRUE'
68+
if value in [False, 0, 'false', 'no', '0']:
69+
return 'FALSE'
70+
raise ValueError("true_false: invalid value: %s" % value)
71+
72+
def __len__(self):
73+
return self._options.__len__()
74+
75+
def __iter__(self):
76+
return self._options.__iter__()
77+
78+
def __add__(self, other):
79+
ret = CMakeOptions()
80+
ret._options += self._options
81+
ret._options += list(other)
82+
return ret
83+
84+
def __iadd__(self, other):
85+
self._options += list(other)
86+
return self
87+
88+
89+
class CMake(object):
90+
91+
def __init__(self, args, host_cc, host_cxx, host_distcc):
92+
self.args = args
93+
self.host_cc = host_cc
94+
self.host_cxx = host_cxx
95+
self.host_distcc = host_distcc
96+
97+
def common_options(self):
98+
"""Return options used for all products, including LLVM/Clang
99+
"""
100+
args = self.args
101+
options = CMakeOptions()
102+
define = options.define
103+
104+
options += ['-G', args.cmake_generator]
105+
106+
sanitizers = []
107+
if args.enable_asan:
108+
sanitizers.append('Address')
109+
if args.enable_ubsan:
110+
sanitizers.append('Undefined')
111+
if sanitizers:
112+
define("LLVM_USE_SANITIZER", ";".join(sanitizers))
113+
114+
if args.export_compile_commands:
115+
define("CMAKE_EXPORT_COMPILE_COMMANDS", "ON")
116+
117+
if args.distcc:
118+
define("CMAKE_C_COMPILER:PATH", self.host_distcc)
119+
define("CMAKE_C_COMPILER_ARG1", self.host_cc)
120+
define("CMAKE_CXX_COMPILER:PATH", self.host_distcc)
121+
define("CMAKE_CXX_COMPILER_ARG1", self.host_cxx)
122+
else:
123+
define("CMAKE_C_COMPILER:PATH", self.host_cc)
124+
define("CMAKE_CXX_COMPILER:PATH", self.host_cxx)
125+
126+
if args.cmake_generator == 'Xcode':
127+
define("CMAKE_CONFIGURATION_TYPES",
128+
"Debug;Release;MinSizeRel;RelWithDebInfo")
129+
130+
if args.clang_compiler_version:
131+
m = re.match(r'([0-9]*)\.([0-9]*)\.([0-9]*)',
132+
args.clang_compiler_version)
133+
if m is not None:
134+
define("LLVM_VERSION_MAJOR:STRING", m.group(1))
135+
define("LLVM_VERSION_MINOR:STRING", m.group(2))
136+
define("LLVM_VERSION_PATCH:STRING", m.group(3))
137+
138+
return options

0 commit comments

Comments
 (0)