|
| 1 | +# swift_build_support/products/product.py -----------------------*- python -*- |
| 2 | +# |
| 3 | +# This source file is part of the Swift.org open source project |
| 4 | +# |
| 5 | +# Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +# |
| 8 | +# See https://swift.org/LICENSE.txt for license information |
| 9 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +# |
| 11 | +# ---------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import os |
| 14 | + |
| 15 | +from . import product |
| 16 | +from .. import cmake |
| 17 | +from .. import shell |
| 18 | + |
| 19 | + |
| 20 | +class CMakeProduct(product.Product): |
| 21 | + def build_with_cmake(self, build_targets, build_type, build_args): |
| 22 | + assert self.toolchain.cmake is not None |
| 23 | + cmake_build = [] |
| 24 | + _cmake = cmake.CMake(self.args, self.toolchain) |
| 25 | + |
| 26 | + if self.toolchain.distcc_pump: |
| 27 | + cmake_build.append(self.toolchain.distcc_pump) |
| 28 | + cmake_build.extend([self.toolchain.cmake, "--build"]) |
| 29 | + |
| 30 | + generator_output_path = "" |
| 31 | + if self.args.cmake_generator == "Ninja": |
| 32 | + generator_output_path = os.path.join(self.build_dir, "build.ninja") |
| 33 | + |
| 34 | + cmake_cache_path = os.path.join(self.build_dir, "CMakeCache.txt") |
| 35 | + if self.args.reconfigure or not os.path.isfile(cmake_cache_path) or \ |
| 36 | + (generator_output_path and not os.path.isfile(generator_output_path)): |
| 37 | + if not os.path.exists(self.build_dir): |
| 38 | + os.makedirs(self.build_dir) |
| 39 | + |
| 40 | + # Use `cmake-file-api` in case it is available. |
| 41 | + query_dir = os.path.join(self.build_dir, ".cmake", "api", "v1", "query") |
| 42 | + if not os.path.exists(query_dir): |
| 43 | + os.makedirs(query_dir) |
| 44 | + open(os.path.join(query_dir, "codemodel-v2"), 'a').close() |
| 45 | + open(os.path.join(query_dir, "cache-v2"), 'a').close() |
| 46 | + |
| 47 | + env = None |
| 48 | + if self.toolchain.distcc: |
| 49 | + env = { |
| 50 | + "DISTCC_HOSTS": "localhost,lzo,cpp" |
| 51 | + } |
| 52 | + |
| 53 | + with shell.pushd(self.build_dir): |
| 54 | + shell.call([self.toolchain.cmake] + list(self.cmake_options) + |
| 55 | + list(_cmake.common_options()) + |
| 56 | + self.args.extra_cmake_options + [self.source_dir], |
| 57 | + env=env) |
| 58 | + |
| 59 | + if not self.args.skip_build or self.product_name() == "llvm": |
| 60 | + if self.args.cmake_generator == "Xcode": |
| 61 | + # Xcode generator uses "ALL_BUILD" instead of "all". |
| 62 | + # Also, xcodebuild uses -target instead of bare names. |
| 63 | + build_targets = build_targets.copy() |
| 64 | + build_targets = [val for target in build_targets |
| 65 | + for val in ["-target", |
| 66 | + target if target != "all" |
| 67 | + else "ALL_BUILD"]] |
| 68 | + |
| 69 | + # Xcode can't restart itself if it turns out we need to reconfigure. |
| 70 | + # Do an advance build to handle that. |
| 71 | + shell.call(cmake_build + [self.build_dir, build_type]) |
| 72 | + |
| 73 | + shell.call(cmake_build + [self.build_dir, "--config", build_type, "--"] |
| 74 | + + build_args + build_targets) |
| 75 | + |
| 76 | + def test_with_cmake(self, executable_target, results_targets, |
| 77 | + build_type, build_args): |
| 78 | + assert self.toolchain.cmake is not None |
| 79 | + cmake_build = [] |
| 80 | + |
| 81 | + if self.toolchain.distcc_pump: |
| 82 | + cmake_build.append(self.toolchain.distcc_pump) |
| 83 | + cmake_args = [self.toolchain.cmake, "--build", self.build_dir, |
| 84 | + "--config", build_type, "--"] |
| 85 | + cmake_build.extend(cmake_args + build_args) |
| 86 | + |
| 87 | + def target_flag(target): |
| 88 | + if self.args.cmake_generator == "Xcode": |
| 89 | + return ["-target", target] |
| 90 | + return [target] |
| 91 | + |
| 92 | + if executable_target: |
| 93 | + shell.call(cmake_build + target_flag(executable_target)) |
| 94 | + |
| 95 | + for target in results_targets: |
| 96 | + if target: |
| 97 | + test_target = target |
| 98 | + print("--- %s ---" % target) |
| 99 | + if test_target.startswith("check-swift") and self.args.test_paths: |
| 100 | + test_target = test_target + "-custom" |
| 101 | + |
| 102 | + shell.call(cmake_build + target_flag(test_target)) |
| 103 | + |
| 104 | + print("--- %s finished ---" % target) |
| 105 | + |
| 106 | + def install_with_cmake(self, install_targets, install_destdir): |
| 107 | + assert self.toolchain.cmake is not None |
| 108 | + cmake_build = [] |
| 109 | + |
| 110 | + if self.toolchain.distcc_pump: |
| 111 | + cmake_build.append(self.toolchain.distcc_pump) |
| 112 | + cmake_args = [self.toolchain.cmake, "--build", self.build_dir, "--"] |
| 113 | + cmake_build.extend(cmake_args + install_targets) |
| 114 | + |
| 115 | + environment = {'DESTDIR': install_destdir} |
| 116 | + shell.call(cmake_build, env=environment) |
0 commit comments