Skip to content

Commit 36898fc

Browse files
committed
[build-script][SR-237] Migrate Ninja build to Python
Migrated impl args: --build-ninja --darwin-deployment-version-{osx,ios,tvos,watchos} Removed impl args: --build-ninja Added impl args: --ninja-bin
1 parent b1109c6 commit 36898fc

File tree

10 files changed

+321
-61
lines changed

10 files changed

+321
-61
lines changed

utils/build-script

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), 'swift_build_support'))
4343
from swift_build_support.toolchain import host_toolchain # noqa (E402)
4444
import swift_build_support.debug # noqa (E402)
4545
from swift_build_support import migration # noqa (E402)
46+
from swift_build_support import products # noqa (E402)
4647
from swift_build_support import shell # noqa (E402)
4748
import swift_build_support.tar # noqa (E402)
4849
import swift_build_support.targets # noqa (E402)
@@ -392,6 +393,10 @@ details of the setups of other systems or automated environments.""")
392393
help="build libdispatch",
393394
action="store_true",
394395
dest="build_libdispatch")
396+
projects_group.add_argument(
397+
"--build-ninja",
398+
help="build the Ninja tool",
399+
action="store_true")
395400

396401
extra_actions_group = parser.add_argument_group(
397402
title="Extra actions to perform before or in addition to building")
@@ -937,6 +942,27 @@ details of the setups of other systems or automated environments.""")
937942
type=argparse_clang_compiler_version,
938943
metavar="MAJOR.MINOR.PATCH")
939944

945+
parser.add_argument(
946+
"--darwin-deployment-version-osx",
947+
help="minimum deployment target version for OS X",
948+
metavar="MAJOR.MINOR",
949+
default="10.9")
950+
parser.add_argument(
951+
"--darwin-deployment-version-ios",
952+
help="minimum deployment target version for iOS",
953+
metavar="MAJOR.MINOR",
954+
default="7.0")
955+
parser.add_argument(
956+
"--darwin-deployment-version-tvos",
957+
help="minimum deployment target version for tvOS",
958+
metavar="MAJOR.MINOR",
959+
default="9.0")
960+
parser.add_argument(
961+
"--darwin-deployment-version-watchos",
962+
help="minimum deployment target version for watchOS",
963+
metavar="MAJOR.MINOR",
964+
default="2.0")
965+
940966
parser.add_argument(
941967
"--extra-cmake-options",
942968
help="Pass through extra options to CMake in the form of comma "
@@ -1097,6 +1123,11 @@ details of the setups of other systems or automated environments.""")
10971123
if args.cmake_generator is None:
10981124
args.cmake_generator = "Ninja"
10991125

1126+
ninja_required = (
1127+
args.cmake_generator == 'Ninja' or args.build_foundation)
1128+
if ninja_required and toolchain.ninja is None:
1129+
args.build_ninja = True
1130+
11001131
# SwiftPM and XCTest have a dependency on Foundation.
11011132
# On OS X, Foundation is built automatically using xcodebuild.
11021133
# On Linux, we must ensure that it is built manually.
@@ -1199,10 +1230,42 @@ details of the setups of other systems or automated environments.""")
11991230
source_root=SWIFT_SOURCE_ROOT,
12001231
build_root=os.path.join(SWIFT_BUILD_ROOT, args.build_subdir))
12011232

1202-
# Clean build_dir if requested.
1233+
if args.build_ninja:
1234+
if not os.path.exists(workspace.source_dir("ninja")):
1235+
print_with_argv0("Can't find source directory for ninja "
1236+
"(tried %s)" % (workspace.source_dir("ninja")))
1237+
return 1
1238+
1239+
# Unset environment variables that might affect how tools behave.
1240+
for v in [
1241+
'MAKEFLAGS',
1242+
'SDKROOT',
1243+
'MACOSX_DEPLOYMENT_TARGET',
1244+
'IPHONEOS_DEPLOYMENT_TARGET',
1245+
'TVOS_DEPLOYMENT_TARGET',
1246+
'WATCHOS_DEPLOYMENT_TARGET']:
1247+
os.environ.pop(v, None)
1248+
1249+
if args.show_sdks:
1250+
swift_build_support.debug.print_xcodebuild_versions()
1251+
1252+
# Clean build direcotry if requested.
12031253
if args.clean:
12041254
shell.rmtree(workspace.build_root)
12051255

1256+
# Create build directory.
1257+
shell.makedirs(workspace.build_root)
1258+
1259+
# Build ninja if required.
1260+
if args.build_ninja:
1261+
ninja_build = products.Ninja(
1262+
args=args,
1263+
toolchain=toolchain,
1264+
source_dir=workspace.source_dir("ninja"),
1265+
build_dir=workspace.build_dir("build", "ninja"))
1266+
ninja_build.do_build()
1267+
toolchain.ninja = ninja_build.ninja_bin_path
1268+
12061269
cmake = CMake(args=args,
12071270
toolchain=toolchain)
12081271

@@ -1216,6 +1279,14 @@ details of the setups of other systems or automated environments.""")
12161279
"--host-cc", toolchain.cc,
12171280
"--host-cxx", toolchain.cxx,
12181281
"--darwin-xcrun-toolchain", args.darwin_xcrun_toolchain,
1282+
"--darwin-deployment-version-osx=%s" % (
1283+
args.darwin_deployment_version_osx),
1284+
"--darwin-deployment-version-ios=%s" % (
1285+
args.darwin_deployment_version_ios),
1286+
"--darwin-deployment-version-tvos=%s" % (
1287+
args.darwin_deployment_version_tvos),
1288+
"--darwin-deployment-version-watchos=%s" % (
1289+
args.darwin_deployment_version_watchos),
12191290
"--cmake", toolchain.cmake,
12201291
"--cmark-build-type", args.cmark_build_variant,
12211292
"--llvm-build-type", args.llvm_build_variant,
@@ -1237,6 +1308,8 @@ details of the setups of other systems or automated environments.""")
12371308
pipes.quote(arg) for arg in cmake.build_args()),
12381309
]
12391310

1311+
if toolchain.ninja:
1312+
build_script_impl_args += ["--ninja-bin=%s" % toolchain.ninja]
12401313
if args.distcc:
12411314
build_script_impl_args += [
12421315
"--distcc",
@@ -1256,8 +1329,6 @@ details of the setups of other systems or automated environments.""")
12561329
build_script_impl_args += [
12571330
"--install-symroot", os.path.abspath(args.install_symroot)
12581331
]
1259-
if args.cmake_generator == 'Ninja' and toolchain.ninja is None:
1260-
build_script_impl_args += ["--build-ninja"]
12611332

12621333
if args.skip_build:
12631334
build_script_impl_args += ["--skip-build-cmark",
@@ -1381,19 +1452,6 @@ details of the setups of other systems or automated environments.""")
13811452

13821453
build_script_impl_args += args.build_script_impl_args
13831454

1384-
# Unset environment variables that might affect how tools behave.
1385-
for v in [
1386-
'MAKEFLAGS',
1387-
'SDKROOT',
1388-
'MACOSX_DEPLOYMENT_TARGET',
1389-
'IPHONEOS_DEPLOYMENT_TARGET',
1390-
'TVOS_DEPLOYMENT_TARGET',
1391-
'WATCHOS_DEPLOYMENT_TARGET']:
1392-
os.environ.pop(v, None)
1393-
1394-
if args.show_sdks:
1395-
swift_build_support.debug.print_xcodebuild_versions()
1396-
13971455
check_call([build_script_impl] + build_script_impl_args,
13981456
disable_sleep=True)
13991457

utils/build-script-impl

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ KNOWN_SETTINGS=(
6060
host-cc "" "the path to CC, the 'clang' compiler for the host platform. **This argument is required**"
6161
host-cxx "" "the path to CXX, the 'clang++' compiler for the host platform. **This argument is required**"
6262
darwin-xcrun-toolchain "default" "the name of the toolchain to use on Darwin"
63-
build-ninja "" "build the Ninja tool"
63+
ninja-bin "" "the path to Ninja tool"
6464
cmark-build-type "Debug" "the CMake build variant for CommonMark (Debug, RelWithDebInfo, Release, MinSizeRel). Defaults to Debug."
6565
lldb-extra-cmake-args "" "extra command line args to pass to lldb cmake"
6666
lldb-extra-xcodebuild-args "" "extra command line args to pass to lldb xcodebuild"
@@ -1258,43 +1258,6 @@ function set_swiftpm_bootstrap_command() {
12581258
fi
12591259
}
12601260

1261-
mkdir -p "${BUILD_DIR}"
1262-
1263-
#
1264-
# Build Ninja
1265-
#
1266-
if [[ "${BUILD_NINJA}" ]] ; then
1267-
build_dir=$(build_directory build ninja)
1268-
if [ ! -f "${build_dir}/ninja" ] ; then
1269-
if [ ! -d "${NINJA_SOURCE_DIR}" ] ; then
1270-
echo "Can't find source directory for ninja (tried ${NINJA_SOURCE_DIR})"
1271-
exit 1
1272-
fi
1273-
1274-
# Ninja can only be built in-tree. Copy the source tree to the build
1275-
# directory.
1276-
set -x
1277-
rm -rf "${build_dir}"
1278-
cp -r "${NINJA_SOURCE_DIR}" "${build_dir}"
1279-
if [[ $(uname -s) == "Darwin" ]]; then
1280-
(cd "${build_dir}" && \
1281-
env CXX=$(xcrun --sdk macosx -find clang++) \
1282-
CFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path) -mmacosx-version-min=${DARWIN_DEPLOYMENT_VERSION_OSX}" \
1283-
LDFLAGS="-mmacosx-version-min=${DARWIN_DEPLOYMENT_VERSION_OSX}" \
1284-
python ./configure.py --bootstrap)
1285-
{ set +x; } 2>/dev/null
1286-
else
1287-
(cd "${build_dir}" && python ./configure.py --bootstrap)
1288-
{ set +x; } 2>/dev/null
1289-
fi
1290-
fi
1291-
NINJA_BIN="${build_dir}/ninja"
1292-
COMMON_CMAKE_OPTIONS=(
1293-
"${COMMON_CMAKE_OPTIONS[@]}"
1294-
-DCMAKE_MAKE_PROGRAM=${NINJA_BIN}
1295-
)
1296-
fi
1297-
12981261
#
12991262
# Configure and build each product
13001263
#
@@ -1809,7 +1772,6 @@ for deployment_target in "${HOST_TARGET}" "${CROSS_COMPILE_TOOLS_DEPLOYMENT_TARG
18091772
SWIFT_BIN="$(build_directory_bin ${deployment_target} swift)/swift"
18101773
SWIFT_BUILD_PATH="$(build_directory ${deployment_target} swift)"
18111774
LLVM_BIN="$(build_directory_bin ${deployment_target} llvm)"
1812-
NINJA_BIN="ninja"
18131775

18141776
# Staging: require opt-in for building with dispatch
18151777
if [[ ! "${SKIP_BUILD_LIBDISPATCH}" ]] ; then
@@ -1821,11 +1783,6 @@ for deployment_target in "${HOST_TARGET}" "${CROSS_COMPILE_TOOLS_DEPLOYMENT_TARG
18211783
SWIFT_USE_LINKER="-fuse-ld=gold"
18221784
fi
18231785

1824-
if [[ "${BUILD_NINJA}" ]]; then
1825-
NINJA_BUILD_DIR=$(build_directory build ninja)
1826-
NINJA_BIN="${NINJA_BUILD_DIR}/ninja"
1827-
fi
1828-
18291786
set -x
18301787
pushd "${FOUNDATION_SOURCE_DIR}"
18311788
SWIFTC="${SWIFTC_BIN}" CLANG="${LLVM_BIN}"/clang SWIFT="${SWIFT_BIN}" \

utils/swift_build_support/swift_build_support/cmake.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ def common_options(self):
113113
define("LLVM_VERSION_MINOR:STRING", minor)
114114
define("LLVM_VERSION_PATCH:STRING", patch)
115115

116+
if args.build_ninja and args.cmake_generator == 'Ninja':
117+
define('CMAKE_MAKE_PROGRAM', toolchain.ninja)
118+
116119
return options
117120

118121
def build_args(self):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# swift_build_support/products/__init__.py ----------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
from .ninja import Ninja
14+
15+
__all__ = [
16+
'Ninja',
17+
]
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# swift_build_support/producsts/ninja.py ------------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
"""
13+
Ninja build
14+
"""
15+
# ----------------------------------------------------------------------------
16+
17+
import os.path
18+
import platform
19+
import sys
20+
21+
from .. import shell
22+
from .. import cache_util
23+
24+
25+
class Ninja(object):
26+
27+
def __init__(self, args, toolchain, source_dir, build_dir):
28+
self.args = args
29+
self.toolchain = toolchain
30+
self.source_dir = source_dir
31+
self.build_dir = build_dir
32+
33+
@cache_util.reify
34+
def ninja_bin_path(self):
35+
return os.path.join(self.build_dir, 'ninja')
36+
37+
def do_build(self):
38+
if os.path.exists(self.ninja_bin_path):
39+
return
40+
41+
env = None
42+
if platform.system() == "Darwin":
43+
from .. import xcrun
44+
sysroot = xcrun.sdk_path("macosx")
45+
osx_version_min = self.args.darwin_deployment_version_osx
46+
assert sysroot is not None
47+
env = [
48+
("CXX", self.toolchain.cxx),
49+
("CFLAGS", (
50+
"-isysroot {sysroot} -mmacosx-version-min={osx_version}"
51+
).format(sysroot=sysroot, osx_version=osx_version_min)),
52+
("LDFLAGS", (
53+
"-mmacosx-version-min={osx_version}"
54+
).format(osx_version=osx_version_min)),
55+
]
56+
57+
# Ninja can only be built in-tree. Copy the source tree to the build
58+
# directory.
59+
shell.rmtree(self.build_dir)
60+
shell.copytree(self.source_dir, self.build_dir)
61+
with shell.pushd(self.build_dir):
62+
shell.call([sys.executable, 'configure.py', '--bootstrap'],
63+
env=env)

utils/swift_build_support/swift_build_support/xcrun.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,18 @@ def find(tool, sdk=None, toolchain=None):
4343
return str(out.rstrip().decode())
4444
except subprocess.CalledProcessError:
4545
return None
46+
47+
48+
@cache_util.cached
49+
def sdk_path(sdk):
50+
"""
51+
Return the path string for given SDK, according to `xcrun --show-sdk-path`.
52+
53+
If `xcrun --show-sdk-path` cannot find the SDK, return None.
54+
"""
55+
command = ['xcrun', '--sdk', sdk, '--show-sdk-path']
56+
try:
57+
out = subprocess.check_output(command)
58+
return str(out.rstrip().decode())
59+
except subprocess.CalledProcessError:
60+
return None
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# tests/products/__init__.py ------------------------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
# ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)