Skip to content

Commit 912017e

Browse files
committed
Merge pull request #2295 from rintaro/build-script-common-cmake-options
2 parents f8b97c5 + d0cbe06 commit 912017e

File tree

5 files changed

+599
-119
lines changed

5 files changed

+599
-119
lines changed

utils/build-script

Lines changed: 109 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import argparse
1515
import multiprocessing
1616
import os
1717
import platform
18+
import re
1819
import shutil
1920
import sys
2021
import pipes
@@ -48,6 +49,7 @@ from swift_build_support import migration # noqa (E402)
4849
import swift_build_support.ninja # noqa (E402)
4950
import swift_build_support.tar # noqa (E402)
5051
import swift_build_support.targets # noqa (E402)
52+
from swift_build_support.cmake import CMake # noqa (E402)
5153

5254

5355
# A strict parser for bools (unlike Python's `bool()`, where
@@ -75,6 +77,16 @@ def argparse_shell_split(string):
7577
return list(lex)
7678

7779

80+
# Parse version string and split into a tuple of strings (major, minor, patch)
81+
# Support only "MAJOR.MINOR.PATCH" format.
82+
def argparse_clang_compiler_version(string):
83+
m = re.match(r'([0-9]*)\.([0-9]*)\.([0-9]*)', string)
84+
if m is not None:
85+
return m.group(1, 2, 3)
86+
raise argparse.ArgumentTypeError(
87+
"%r is invalid version value. must be 'MAJOR.MINOR.PATCH'" % string)
88+
89+
7890
# Main entry point for the preset mode.
7991
def main_preset():
8092
parser = argparse.ArgumentParser(
@@ -812,6 +824,34 @@ details of the setups of other systems or automated environments.""")
812824
help="Path to a directory containing headers libicui18n",
813825
metavar="PATH")
814826

827+
parser.add_argument(
828+
"--host-cc",
829+
help="the absolute path to CC, the 'clang' compiler for the host "
830+
"platform. Default is auto detected.",
831+
metavar="PATH")
832+
parser.add_argument(
833+
"--host-cxx",
834+
help="the absolute path to CXX, the 'clang++' compiler for the host "
835+
"platform. Default is auto detected.",
836+
metavar="PATH")
837+
parser.add_argument(
838+
"--distcc",
839+
help="use distcc in pump mode",
840+
action="store_true")
841+
parser.add_argument(
842+
"--enable-asan",
843+
help="enable Address Sanitizer",
844+
action="store_true")
845+
parser.add_argument(
846+
"--enable-ubsan",
847+
help="enable Undefined Behavior Sanitizer",
848+
action="store_true")
849+
parser.add_argument(
850+
"--clang-compiler-version",
851+
help="string that indicates a compiler version for Clang",
852+
type=argparse_clang_compiler_version,
853+
metavar="MAJOR.MINOR.PATCH")
854+
815855
parser.add_argument(
816856
"--extra-cmake-options",
817857
help="Pass through extra options to CMake in the form of comma "
@@ -821,6 +861,19 @@ details of the setups of other systems or automated environments.""")
821861
type=argparse_shell_split,
822862
default=[])
823863

864+
parser.add_argument(
865+
"--build-args",
866+
help="arguments to the build tool. This would be prepended to the "
867+
"default argument that is '-j8' when CMake generator is "
868+
"\"Ninja\".",
869+
type=argparse_shell_split,
870+
default=[])
871+
872+
parser.add_argument(
873+
"--verbose-build",
874+
help="print the commands executed during the build",
875+
action="store_true")
876+
824877
args = migration.parse_args(parser, sys.argv[1:])
825878

826879
build_script_impl = os.path.join(
@@ -955,11 +1008,6 @@ details of the setups of other systems or automated environments.""")
9551008

9561009
build_script_impl_inferred_args = []
9571010

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

11671215
host_clang = swift_build_support.toolchain.host_clang(
11681216
xcrun_toolchain=args.darwin_xcrun_toolchain)
1169-
if not host_clang:
1217+
if args.host_cc is None and host_clang is not None:
1218+
args.host_cc = str(host_clang.cc)
1219+
if args.host_cxx is None and host_clang is not None:
1220+
args.host_cxx = str(host_clang.cxx)
1221+
is_host_clang_ok = (
1222+
args.host_cc is not None and os.access(args.host_cc, os.X_OK) and
1223+
args.host_cxx is not None and os.access(args.host_cxx, os.X_OK))
1224+
if not is_host_clang_ok:
11701225
print_with_argv0(
1171-
"Can't find clang. Please install clang-3.5 or a later version.")
1226+
"Can't find clang. Please install clang-3.5 or a later version.")
11721227
return 1
11731228

11741229
host_cmake = args.cmake
@@ -1178,14 +1233,38 @@ details of the setups of other systems or automated environments.""")
11781233
if not host_cmake:
11791234
print_with_argv0("Can't find CMake. Please install CMake.")
11801235
return 1
1236+
1237+
# distcc usage.
1238+
host_distcc = None
1239+
host_distcc_pump = None
1240+
if args.distcc:
1241+
host_distcc = swift_build_support.which('distcc')
1242+
# On some platforms, 'pump' may be unrelated to distcc, in which case
1243+
# it's called 'distcc-pump'.
1244+
host_distcc_pump = swift_build_support.which('distcc-pump')
1245+
if host_distcc_pump is None:
1246+
host_distcc_pump = swift_build_support.which('pump')
1247+
1248+
if host_distcc is None or host_distcc_pump is None:
1249+
print_with_argv0(
1250+
"Can't find distcc. Please install distcc")
1251+
1252+
host_distcc = str(host_distcc)
1253+
host_distcc_pump = str(host_distcc_pump)
1254+
1255+
cmake = CMake(args=args,
1256+
host_cc=args.host_cc,
1257+
host_cxx=args.host_cxx,
1258+
host_distcc=host_distcc)
1259+
11811260
build_script_impl_args = [
11821261
"--build-dir", build_dir,
11831262
"--install-prefix", os.path.abspath(args.install_prefix),
11841263
"--host-target", args.host_target,
11851264
"--stdlib-deployment-targets",
11861265
" ".join(args.stdlib_deployment_targets),
1187-
"--host-cc", host_clang.cc,
1188-
"--host-cxx", host_clang.cxx,
1266+
"--host-cc", args.host_cc,
1267+
"--host-cxx", args.host_cxx,
11891268
"--darwin-xcrun-toolchain", args.darwin_xcrun_toolchain,
11901269
"--cmake", host_cmake,
11911270
"--cmark-build-type", args.cmark_build_variant,
@@ -1201,8 +1280,28 @@ details of the setups of other systems or automated environments.""")
12011280
args.swift_analyze_code_coverage).lower(),
12021281
"--cmake-generator", args.cmake_generator,
12031282
"--build-jobs", str(args.build_jobs),
1204-
"--workspace", SWIFT_SOURCE_ROOT
1283+
"--workspace", SWIFT_SOURCE_ROOT,
1284+
"--common-cmake-options=%s" % ' '.join(
1285+
pipes.quote(opt) for opt in cmake.common_options()),
1286+
"--build-args=%s" % ' '.join(
1287+
pipes.quote(arg) for arg in cmake.build_args()),
12051288
]
1289+
1290+
if args.distcc:
1291+
build_script_impl_args += [
1292+
"--distcc",
1293+
"--distcc-pump=%s" % host_distcc_pump
1294+
]
1295+
if args.enable_asan:
1296+
build_script_impl_args += ["--enable-asan"]
1297+
if args.enable_ubsan:
1298+
build_script_impl_args += ["--enable-ubsan"]
1299+
if args.clang_compiler_version:
1300+
build_script_impl_args += [
1301+
"--clang-compiler-version=%s.%s.%s" % args.clang_compiler_version
1302+
]
1303+
if args.verbose_build:
1304+
build_script_impl_args += ["--verbose-build"]
12061305
if args.install_symroot:
12071306
build_script_impl_args += [
12081307
"--install-symroot", os.path.abspath(args.install_symroot)

utils/build-script-impl

Lines changed: 13 additions & 108 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
}
@@ -780,6 +774,12 @@ if [[ "${SKIP_BUILD_WATCHOS_SIMULATOR}" ]] ; then
780774
SKIP_TEST_WATCHOS_SIMULATOR=1
781775
fi
782776

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+
783783
# FIXME: We currently do not support cross-compiling swift with compiler-rt.
784784
if [[ "${CROSS_COMPILE_TOOLS_DEPLOYMENT_TARGETS}" ]]; then
785785
SKIP_COMPILER_RT=1
@@ -1132,110 +1132,15 @@ if ! [[ "${SKIP_TEST_BENCHMARKS}" ]] &&
11321132
fi
11331133
echo
11341134

1135-
# CMake options used for all targets, including LLVM/Clang
1136-
COMMON_CMAKE_OPTIONS=(
1137-
-G "${CMAKE_GENERATOR}"
1138-
)
1139-
11401135
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
11691136

11701137
# Convert to an array.
1138+
eval COMMON_CMAKE_OPTIONS=(${COMMON_CMAKE_OPTIONS})
11711139
eval EXTRA_CMAKE_OPTIONS=(${EXTRA_CMAKE_OPTIONS})
1140+
eval BUILD_ARGS=(${BUILD_ARGS})
11721141

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-
1193-
CMAKE_JOBS="${BUILD_JOBS}"
1194-
if [[ "${DISTCC}" ]] ; then
1195-
CMAKE_JOBS="$(distcc -j)"
1196-
fi
1197-
1198-
case "${CMAKE_GENERATOR}" in
1199-
Ninja)
1200-
BUILD_ARGS="${BUILD_ARGS} -j${CMAKE_JOBS}"
1201-
if [[ "${VERBOSE_BUILD}" ]] ; then
1202-
BUILD_ARGS="${BUILD_ARGS} -v"
1203-
fi
1204-
;;
1205-
'Unix Makefiles')
1206-
BUILD_ARGS="${BUILD_ARGS} -j${CMAKE_JOBS}"
1207-
if [[ "${VERBOSE_BUILD}" ]] ; then
1208-
BUILD_ARGS="${BUILD_ARGS} VERBOSE=1"
1209-
fi
1210-
;;
1211-
Xcode)
1212-
# -parallelizeTargets is an unsupported flag from the Xcode 3 days,
1213-
# but since we're not using proper Xcode 4 schemes, this is the
1214-
# only way to get target-level parallelism.
1215-
BUILD_ARGS="${BUILD_ARGS} -parallelizeTargets"
1216-
BUILD_ARGS="${BUILD_ARGS} -jobs ${CMAKE_JOBS}"
1217-
BUILD_TARGET_FLAG="-target"
1218-
COMMON_CMAKE_OPTIONS=(
1219-
"${COMMON_CMAKE_OPTIONS[@]}"
1220-
-DCMAKE_CONFIGURATION_TYPES="Debug;Release;MinSizeRel;RelWithDebInfo"
1221-
)
1222-
1223-
# FIXME: We currently do not support building compiler-rt with the
1224-
# Xcode generator.
1225-
SKIP_COMPILER_RT=1
1226-
;;
1227-
esac
1228-
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-
)
1142+
if [[ "${CMAKE_GENERATOR}" == "Xcode" ]]; then
1143+
BUILD_TARGET_FLAG="-target"
12391144
fi
12401145

12411146
function build_directory() {
@@ -2065,7 +1970,7 @@ for deployment_target in "${HOST_TARGET}" "${CROSS_TOOLS_DEPLOYMENT_TARGETS[@]}"
20651970
fi
20661971

20671972
set -x
2068-
${DISTCC_PUMP} "${CMAKE}" --build "${build_dir}" $(cmake_config_opt ${product}) -- ${BUILD_ARGS} ${build_targets[@]}
1973+
${DISTCC_PUMP} "${CMAKE}" --build "${build_dir}" $(cmake_config_opt ${product}) -- "${BUILD_ARGS[@]}" ${build_targets[@]}
20691974
{ set +x; } 2>/dev/null
20701975
fi
20711976

0 commit comments

Comments
 (0)