|
| 1 | +# This source file is part of the Swift.org open source project |
| 2 | +# |
| 3 | +# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 4 | +# Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +# |
| 6 | +# See https://swift.org/LICENSE.txt for license information |
| 7 | +# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | + |
| 9 | + |
| 10 | +from __future__ import absolute_import |
| 11 | +from __future__ import print_function |
| 12 | +from __future__ import unicode_literals |
| 13 | + |
| 14 | +import os.path |
| 15 | + |
| 16 | + |
| 17 | +__all__ = [ |
| 18 | + "BUILD_SCRIPT_IMPL_PATH", |
| 19 | + "BUILD_SCRIPT_PATH", |
| 20 | + "BUILD_SWIFT_PATH", |
| 21 | + "MODULE_PATH", |
| 22 | + "MULTIROOT_DATA_FILE_PATH", |
| 23 | + "PROJECT_PATH", |
| 24 | + "RESOURCES_PATH", |
| 25 | + "SWIFT_BUILD_ROOT", |
| 26 | + "SWIFT_REPO_NAME", |
| 27 | + "SWIFT_SOURCE_ROOT", |
| 28 | + "UTILS_PATH", |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +# -------------------------------------------------------------------------------------- |
| 33 | +# Project Paths |
| 34 | + |
| 35 | + |
| 36 | +MODULE_PATH = os.path.abspath(os.path.dirname(__file__)) |
| 37 | + |
| 38 | +BUILD_SWIFT_PATH = os.path.dirname(MODULE_PATH) |
| 39 | + |
| 40 | +UTILS_PATH = os.path.dirname(BUILD_SWIFT_PATH) |
| 41 | + |
| 42 | +PROJECT_PATH = os.path.dirname(UTILS_PATH) |
| 43 | + |
| 44 | + |
| 45 | +BUILD_SCRIPT_PATH = os.path.join(UTILS_PATH, "build-script") |
| 46 | + |
| 47 | +BUILD_SCRIPT_IMPL_PATH = os.path.join(UTILS_PATH, "build-script-impl") |
| 48 | + |
| 49 | + |
| 50 | +# -------------------------------------------------------------------------------------- |
| 51 | +# Resources |
| 52 | + |
| 53 | + |
| 54 | +RESOURCES_PATH = os.path.join(BUILD_SWIFT_PATH, "resources") |
| 55 | + |
| 56 | + |
| 57 | +# The path to the Xcode workspace to use for a unified build of multiple SwiftPM |
| 58 | +# projects. |
| 59 | +MULTIROOT_DATA_FILE_PATH = os.path.join( |
| 60 | + RESOURCES_PATH, "SwiftPM-Unified-Build.xcworkspace" |
| 61 | +) |
| 62 | + |
| 63 | + |
| 64 | +# -------------------------------------------------------------------------------------- |
| 65 | +# Helpers |
| 66 | + |
| 67 | + |
| 68 | +def _is_llvm_checkout(llvm_path): |
| 69 | + """Returns true if the given llvm_path is a valid LLVM checkout, false otherwise. |
| 70 | +
|
| 71 | + NOTE: This is a very naive validation, checking only for the existence of a few |
| 72 | + known files. |
| 73 | + """ |
| 74 | + |
| 75 | + if not os.path.exists(os.path.join(llvm_path, "tools")): |
| 76 | + return False |
| 77 | + |
| 78 | + if not os.path.exists(os.path.join(llvm_path, "CMakeLists.txt")): |
| 79 | + return False |
| 80 | + |
| 81 | + return True |
| 82 | + |
| 83 | + |
| 84 | +def _is_swift_checkout(swift_path): |
| 85 | + """Returns true if the given swift_path is a valid Swift checkout, false otherwise. |
| 86 | +
|
| 87 | + NOTE: This is a very naive validation, checking only for the existence of a few |
| 88 | + known files. |
| 89 | + """ |
| 90 | + |
| 91 | + if not os.path.exists(os.path.join(swift_path, "utils")): |
| 92 | + return False |
| 93 | + |
| 94 | + if not os.path.exists(os.path.join(swift_path, "CMakeLists.txt")): |
| 95 | + return False |
| 96 | + |
| 97 | + return True |
| 98 | + |
| 99 | + |
| 100 | +def _get_swift_source_root(swift_path, env=None): |
| 101 | + """Returns the Swift source root or None if one cannot be determined. |
| 102 | +
|
| 103 | + Users are able to manually override the source root by setting the SWIFT_SOURCE_ROOT |
| 104 | + environment variable. If that cannot be found then this function will check the |
| 105 | + directory structure to infer if we are building as a standalone Swift build or if we |
| 106 | + are building in the unified LLVM. |
| 107 | +
|
| 108 | + Building standalone means Swift will be checked out as a peer of LLVM and the |
| 109 | + enclosing directory is the source root. |
| 110 | +
|
| 111 | + source-root/ |
| 112 | + |- llvm/ |
| 113 | + |- swift/ |
| 114 | + | ... |
| 115 | +
|
| 116 | + However the unified case means Swift will be checked out in the llvm/tools |
| 117 | + directory, which means the directory containing LLVM is the source root. |
| 118 | +
|
| 119 | + source-root/ |
| 120 | + |- llvm/ |
| 121 | + | |- tools/ |
| 122 | + | | |- swift/ |
| 123 | + | | | ... |
| 124 | + | | ... |
| 125 | + | ... |
| 126 | +
|
| 127 | + In the case that this function is called with an invalid Swift checkout it returns |
| 128 | + None as well. |
| 129 | +
|
| 130 | + FIXME: What about the new llvm-project monorepo? |
| 131 | + """ |
| 132 | + |
| 133 | + env = env or {} |
| 134 | + |
| 135 | + # Check the environment first. |
| 136 | + if "SWIFT_SOURCE_ROOT" in env: |
| 137 | + return env["SWIFT_SOURCE_ROOT"] |
| 138 | + |
| 139 | + # Assert we are in a valid Swift checkout. |
| 140 | + if not _is_swift_checkout(swift_path): |
| 141 | + return None |
| 142 | + |
| 143 | + source_root = os.path.dirname(swift_path) |
| 144 | + |
| 145 | + # Check if Swift is checked out as part of a unified build. |
| 146 | + if os.path.basename(source_root) != "tools": |
| 147 | + return source_root |
| 148 | + |
| 149 | + llvm_path = os.path.dirname(source_root) |
| 150 | + if not _is_llvm_checkout(llvm_path): |
| 151 | + return source_root |
| 152 | + |
| 153 | + # Return the directory containing LLVM. |
| 154 | + return os.path.dirname(llvm_path) |
| 155 | + |
| 156 | + |
| 157 | +def _get_swift_build_root(source_root, env=None): |
| 158 | + """Returns the Swift build root. |
| 159 | +
|
| 160 | + Users are able to manually override the build root by setting the SWIFT_BUILD_ROOT |
| 161 | + environment variable. If that cannot be found then this function returns the path |
| 162 | + to a directory named "build" in the given source root. |
| 163 | + """ |
| 164 | + |
| 165 | + env = env or {} |
| 166 | + |
| 167 | + if "SWIFT_BUILD_ROOT" in env: |
| 168 | + return env["SWIFT_BUILD_ROOT"] |
| 169 | + |
| 170 | + return os.path.join(source_root, "build") |
| 171 | + |
| 172 | + |
| 173 | +def _get_swift_repo_name(swift_path, env=None): |
| 174 | + """Returns the Swift repo name or None if it cannot be determined. |
| 175 | +
|
| 176 | + Users are able to manually override the repo name by setting the SWIFT_REPO_NAME |
| 177 | + environment variable. If that cannot be found then this function returns the name |
| 178 | + of the given swift path or None if it is not a valid Swift checkout. |
| 179 | + """ |
| 180 | + |
| 181 | + env = env or {} |
| 182 | + |
| 183 | + if "SWIFT_REPO_NAME" in env: |
| 184 | + return env["SWIFT_REPO_NAME"] |
| 185 | + |
| 186 | + if not _is_swift_checkout(swift_path): |
| 187 | + return None |
| 188 | + |
| 189 | + return os.path.basename(swift_path) |
| 190 | + |
| 191 | + |
| 192 | +# -------------------------------------------------------------------------------------- |
| 193 | +# Swift Source and Build Roots |
| 194 | + |
| 195 | + |
| 196 | +# Set SWIFT_SOURCE_ROOT in your environment to control where the sources are found. |
| 197 | +SWIFT_SOURCE_ROOT = _get_swift_source_root(PROJECT_PATH, env=os.environ) |
| 198 | + |
| 199 | +# Set SWIFT_BUILD_ROOT to a directory that will contain a subdirectory for each build |
| 200 | +# configuration |
| 201 | +SWIFT_BUILD_ROOT = _get_swift_build_root(SWIFT_SOURCE_ROOT, env=os.environ) |
| 202 | + |
| 203 | +# Set SWIFT_REPO_NAME in your environment to control the name of the swift directory |
| 204 | +# name that is used. |
| 205 | +SWIFT_REPO_NAME = _get_swift_repo_name(PROJECT_PATH, env=os.environ) |
0 commit comments