Skip to content

Commit e46e322

Browse files
committed
[build-script] Build Ninja with CMake
This patch switches the Ninja build from using the configure.py script to building with the just-built CMake. The configure.py in Ninja 1.11.1 still uses Python 2.7, importing the `pipes` module. The pipes module was deprecated in Python 3.11 and removed in 3.13, so folks using newer versions of Python are running into issues with this. The CMake build doesn't have this issue and is also perfectly valid, so we can switch to that.
1 parent 90bf96c commit e46e322

File tree

2 files changed

+17
-62
lines changed

2 files changed

+17
-62
lines changed

utils/swift_build_support/swift_build_support/products/ninja.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
# ----------------------------------------------------------------------------
1616

1717
import os.path
18-
import platform
19-
import sys
2018

2119
from build_swift.build_swift import cache_utils
22-
from build_swift.build_swift.wrappers import xcrun
2320

2421
from . import product
2522
from .. import shell
@@ -55,29 +52,11 @@ def ninja_bin_path(self):
5552
def build(self):
5653
if os.path.exists(self.ninja_bin_path):
5754
return
58-
59-
env = None
60-
if platform.system() == "Darwin":
61-
sysroot = xcrun.sdk_path("macosx")
62-
assert sysroot is not None
63-
env = {
64-
"CXX": shell._quote(self.toolchain.cxx),
65-
"CFLAGS": (
66-
"-isysroot {sysroot}"
67-
).format(sysroot=shell._quote(sysroot)),
68-
"LDFLAGS": (
69-
"-isysroot {sysroot}"
70-
).format(sysroot=shell._quote(sysroot)),
71-
}
72-
elif self.toolchain.cxx:
73-
env = {
74-
"CXX": shell._quote(self.toolchain.cxx),
75-
}
76-
77-
# Ninja can only be built in-tree. Copy the source tree to the build
78-
# directory.
79-
shell.rmtree(self.build_dir)
80-
shell.copytree(self.source_dir, self.build_dir, ignore_pattern=".git")
81-
with shell.pushd(self.build_dir):
82-
shell.call([sys.executable, 'configure.py', '--bootstrap'],
83-
env=env)
55+
shell.call([
56+
self.toolchain.cmake,
57+
"-S", self.source_dir,
58+
"-B", self.build_dir,
59+
"-DCMAKE_BUILD_TYPE=Release",
60+
f"-DCMAKE_C_COMPILER={self.toolchain.cc}",
61+
f"-DCMAKE_CXX_COMPILER={self.toolchain.cxx}"])
62+
shell.call([self.toolchain.cmake, "--build", self.build_dir])

utils/swift_build_support/tests/products/test_ninja.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import unittest
1919
from io import StringIO
2020

21-
from build_swift.build_swift.wrappers import xcrun
22-
2321
from swift_build_support import shell
2422
from swift_build_support.products import Ninja
2523
from swift_build_support.targets import StdlibDeploymentTarget
@@ -90,37 +88,15 @@ def test_build(self):
9088

9189
ninja_build.build()
9290

93-
expect_env = ""
94-
if platform.system() == "Darwin":
95-
expect_env = (
96-
"env "
97-
"'CFLAGS=-isysroot {sysroot}' "
98-
"CXX={cxx} "
99-
"'LDFLAGS=-isysroot {sysroot}' "
100-
).format(
101-
cxx=self.toolchain.cxx,
102-
sysroot=xcrun.sdk_path('macosx')
103-
)
104-
elif self.toolchain.cxx:
105-
expect_env = (
106-
"env "
107-
"CXX={cxx} "
108-
).format(
109-
cxx=self.toolchain.cxx,
110-
)
111-
112-
self.assertEqual(self.stdout.getvalue(), """\
113-
+ rm -rf {build_dir}
114-
+ cp -r {source_dir} {build_dir}
115-
+ pushd {build_dir}
116-
+ {expect_env}{python} configure.py --bootstrap
117-
+ popd
118-
""".format(source_dir=self._platform_quote(
119-
self.workspace.source_dir('ninja')),
120-
build_dir=self._platform_quote(
121-
self.workspace.build_dir('build', 'ninja')),
122-
expect_env=expect_env,
123-
python=self._platform_quote(sys.executable)))
91+
self.assertEqual(self.stdout.getvalue(), f"""\
92+
+ {self.toolchain.cmake} \
93+
-S {self.workspace.source_dir('ninja')} \
94+
-B {self.workspace.build_dir('build', 'ninja')} \
95+
-DCMAKE_BUILD_TYPE=Release \
96+
-DCMAKE_C_COMPILER=/path/to/cc \
97+
-DCMAKE_CXX_COMPILER=/path/to/cxx
98+
+ {self.toolchain.cmake} --build {self.workspace.build_dir('build', 'ninja')}
99+
""")
124100

125101
def _platform_quote(self, path):
126102
if platform.system() == 'Windows':

0 commit comments

Comments
 (0)