-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Build] Make cmark build a build-script product #37102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
utils/swift_build_support/swift_build_support/products/cmake_product.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# swift_build_support/products/product.py -----------------------*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2021 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See https://swift.org/LICENSE.txt for license information | ||
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# | ||
# ---------------------------------------------------------------------------- | ||
|
||
import os | ||
|
||
from . import product | ||
from .. import cmake | ||
from .. import shell | ||
|
||
|
||
class CMakeProduct(product.Product): | ||
def build_with_cmake(self, build_targets, build_type, build_args): | ||
assert self.toolchain.cmake is not None | ||
cmake_build = [] | ||
_cmake = cmake.CMake(self.args, self.toolchain) | ||
|
||
if self.toolchain.distcc_pump: | ||
cmake_build.append(self.toolchain.distcc_pump) | ||
cmake_build.extend([self.toolchain.cmake, "--build"]) | ||
|
||
generator_output_path = "" | ||
if self.args.cmake_generator == "Ninja": | ||
generator_output_path = os.path.join(self.build_dir, "build.ninja") | ||
|
||
cmake_cache_path = os.path.join(self.build_dir, "CMakeCache.txt") | ||
if self.args.reconfigure or not os.path.isfile(cmake_cache_path) or \ | ||
(generator_output_path and not os.path.isfile(generator_output_path)): | ||
if not os.path.exists(self.build_dir): | ||
os.makedirs(self.build_dir) | ||
|
||
# Use `cmake-file-api` in case it is available. | ||
query_dir = os.path.join(self.build_dir, ".cmake", "api", "v1", "query") | ||
if not os.path.exists(query_dir): | ||
os.makedirs(query_dir) | ||
open(os.path.join(query_dir, "codemodel-v2"), 'a').close() | ||
open(os.path.join(query_dir, "cache-v2"), 'a').close() | ||
|
||
env = None | ||
if self.toolchain.distcc: | ||
env = { | ||
"DISTCC_HOSTS": "localhost,lzo,cpp" | ||
} | ||
|
||
with shell.pushd(self.build_dir): | ||
shell.call([self.toolchain.cmake] + list(self.cmake_options) + | ||
list(_cmake.common_options()) + | ||
self.args.extra_cmake_options + [self.source_dir], | ||
env=env) | ||
|
||
if not self.args.skip_build or self.product_name() == "llvm": | ||
if self.args.cmake_generator == "Xcode": | ||
# Xcode generator uses "ALL_BUILD" instead of "all". | ||
# Also, xcodebuild uses -target instead of bare names. | ||
build_targets = build_targets.copy() | ||
build_targets = [val for target in build_targets | ||
for val in ["-target", | ||
target if target != "all" | ||
else "ALL_BUILD"]] | ||
|
||
# Xcode can't restart itself if it turns out we need to reconfigure. | ||
# Do an advance build to handle that. | ||
shell.call(cmake_build + [self.build_dir, build_type]) | ||
|
||
shell.call(cmake_build + [self.build_dir, "--config", build_type, "--"] | ||
+ build_args + build_targets) | ||
|
||
def test_with_cmake(self, executable_target, results_targets, | ||
build_type, build_args): | ||
assert self.toolchain.cmake is not None | ||
cmake_build = [] | ||
|
||
if self.toolchain.distcc_pump: | ||
cmake_build.append(self.toolchain.distcc_pump) | ||
cmake_args = [self.toolchain.cmake, "--build", self.build_dir, | ||
"--config", build_type, "--"] | ||
cmake_build.extend(cmake_args + build_args) | ||
|
||
def target_flag(target): | ||
if self.args.cmake_generator == "Xcode": | ||
return ["-target", target] | ||
return [target] | ||
|
||
if executable_target: | ||
shell.call(cmake_build + target_flag(executable_target)) | ||
|
||
for target in results_targets: | ||
if target: | ||
test_target = target | ||
print("--- %s ---" % target) | ||
if test_target.startswith("check-swift") and self.args.test_paths: | ||
test_target = test_target + "-custom" | ||
|
||
shell.call(cmake_build + target_flag(test_target)) | ||
|
||
print("--- %s finished ---" % target) | ||
|
||
def install_with_cmake(self, install_targets, install_destdir): | ||
assert self.toolchain.cmake is not None | ||
cmake_build = [] | ||
|
||
if self.toolchain.distcc_pump: | ||
cmake_build.append(self.toolchain.distcc_pump) | ||
cmake_args = [self.toolchain.cmake, "--build", self.build_dir, "--"] | ||
cmake_build.extend(cmake_args + install_targets) | ||
|
||
environment = {'DESTDIR': install_destdir} | ||
shell.call(cmake_build, env=environment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# tests/products/test_ninja.py ----------------------------------*- python -*- | ||
# | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See https://swift.org/LICENSE.txt for license information | ||
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
# ---------------------------------------------------------------------------- | ||
|
||
import argparse | ||
import os | ||
import shutil | ||
import sys | ||
import tempfile | ||
import unittest | ||
try: | ||
# py2 | ||
from StringIO import StringIO | ||
except ImportError: | ||
# py3 | ||
from io import StringIO | ||
|
||
from swift_build_support import cmake | ||
from swift_build_support import shell | ||
from swift_build_support.products import CMark | ||
from swift_build_support.targets import StdlibDeploymentTarget | ||
from swift_build_support.toolchain import host_toolchain | ||
from swift_build_support.workspace import Workspace | ||
|
||
|
||
class CMarkTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
# Setup workspace | ||
tmpdir1 = os.path.realpath(tempfile.mkdtemp()) | ||
tmpdir2 = os.path.realpath(tempfile.mkdtemp()) | ||
os.makedirs(os.path.join(tmpdir1, 'cmark')) | ||
|
||
self.workspace = Workspace(source_root=tmpdir1, | ||
build_root=tmpdir2) | ||
|
||
self.host = StdlibDeploymentTarget.host_target() | ||
|
||
# Setup toolchain | ||
self.toolchain = host_toolchain() | ||
self.toolchain.cc = '/path/to/cc' | ||
self.toolchain.cxx = '/path/to/cxx' | ||
|
||
# Setup args | ||
self.args = argparse.Namespace( | ||
build_cmark=True, | ||
cmake_generator="Ninja", | ||
cmark_build_type="Release", | ||
rebuild=False, | ||
extra_cmake_options=[], | ||
skip_build=False, | ||
darwin_deployment_version_osx="10.9", | ||
cmark_build_variant="Debug", | ||
export_compile_commands=False, | ||
reconfigure=False, | ||
distcc=None, | ||
sccache=None, | ||
cmake_c_launcher=None, | ||
cmake_cxx_launcher=None, | ||
clang_user_visible_version=None, | ||
build_ninja=False, | ||
enable_asan=False, | ||
enable_lsan=False, | ||
enable_sanitize_coverage=False, | ||
enable_tsan=False, | ||
enable_ubsan=False) | ||
|
||
# Setup shell | ||
shell.dry_run = True | ||
self._orig_stdout = sys.stdout | ||
self._orig_stderr = sys.stderr | ||
self.stdout = StringIO() | ||
self.stderr = StringIO() | ||
sys.stdout = self.stdout | ||
sys.stderr = self.stderr | ||
|
||
def tearDown(self): | ||
shutil.rmtree(self.workspace.build_root) | ||
shutil.rmtree(self.workspace.source_root) | ||
sys.stdout = self._orig_stdout | ||
sys.stderr = self._orig_stderr | ||
shell.dry_run = False | ||
self.workspace = None | ||
self.toolchain = None | ||
self.args = None | ||
|
||
def test_build(self): | ||
gottesmm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cmark = CMark( | ||
args=self.args, | ||
toolchain=self.toolchain, | ||
source_dir=self.workspace.source_root, | ||
build_dir=self.workspace.build_root) | ||
|
||
cmark.build(host_target=self.host) | ||
_cmake = cmake.CMake(self.args, self.toolchain) | ||
|
||
self.assertEqual(self.stdout.getvalue(), """\ | ||
+ pushd {build_dir} | ||
+ {cmake} -DCMAKE_BUILD_TYPE:STRING={build_variant} {cmake_args} {source_dir} | ||
+ popd | ||
+ {cmake} --build {build_dir} --config {build_variant} -- all | ||
""".format(build_dir=self.workspace.build_root, | ||
source_dir=self.workspace.source_root, | ||
cmake=self.toolchain.cmake, | ||
cmake_args=' '.join(_cmake.common_options()), | ||
build_variant=self.args.cmark_build_variant)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really the canonical way to do this in python? I guess it works.