Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | #===----------------------------------------------------------------------===## |
| 3 | # |
| 4 | # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | # See https://llvm.org/LICENSE.txt for license information. |
| 6 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | # |
| 8 | #===----------------------------------------------------------------------===## |
| 9 | |
| 10 | set -e |
| 11 | |
| 12 | PROGNAME="$(basename "${0}")" |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 13 | |
| 14 | function error() { printf "error: %s\n" "$*"; exit 1; } |
| 15 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 16 | function usage() { |
| 17 | cat <<EOF |
| 18 | Usage: |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 19 | ${PROGNAME} [options] |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 20 | |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 21 | [-h|--help] Display this help and exit. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 22 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 23 | --llvm-root <DIR> Path to the root of the LLVM monorepo. Only the libcxx |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 24 | and libcxxabi directories are required. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 25 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 26 | --build-dir <DIR> Path to the directory to use for building. This will |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 27 | contain intermediate build products. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 28 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 29 | --install-dir <DIR> Path to the directory to install the library to. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 30 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 31 | --symbols-dir <DIR> Path to the directory to install the .dSYM bundle to. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 32 | |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 33 | --sdk <SDK> SDK used for building the library. This represents |
| 34 | the target platform that the library will run on. |
| 35 | You can get a list of SDKs with \`xcodebuild -showsdks\`. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 36 | |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 37 | --architectures "<arch>..." A whitespace separated list of architectures to build for. |
| 38 | The library will be built for each architecture independently, |
| 39 | and a universal binary containing all architectures will be |
| 40 | created from that. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 41 | |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 42 | --version X[.Y[.Z]] The version of the library to encode in the dylib. |
| 43 | |
| 44 | --cache <PATH> The CMake cache to use to control how the library gets built. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 45 | EOF |
| 46 | } |
| 47 | |
| 48 | while [[ $# -gt 0 ]]; do |
| 49 | case ${1} in |
| 50 | -h|--help) |
| 51 | usage |
| 52 | exit 0 |
| 53 | ;; |
| 54 | --llvm-root) |
| 55 | llvm_root="${2}" |
| 56 | shift; shift |
| 57 | ;; |
| 58 | --build-dir) |
| 59 | build_dir="${2}" |
| 60 | shift; shift |
| 61 | ;; |
| 62 | --symbols-dir) |
| 63 | symbols_dir="${2}" |
| 64 | shift; shift |
| 65 | ;; |
| 66 | --install-dir) |
| 67 | install_dir="${2}" |
| 68 | shift; shift |
| 69 | ;; |
| 70 | --sdk) |
| 71 | sdk="${2}" |
| 72 | shift; shift |
| 73 | ;; |
| 74 | --architectures) |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 75 | architectures="${2}" |
| 76 | shift; shift |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 77 | ;; |
| 78 | --version) |
| 79 | version="${2}" |
| 80 | shift; shift |
| 81 | ;; |
| 82 | --cache) |
| 83 | cache="${2}" |
| 84 | shift; shift |
| 85 | ;; |
| 86 | *) |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 87 | error "Unknown argument '${1}'" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 88 | ;; |
| 89 | esac |
| 90 | done |
| 91 | |
| 92 | for arg in llvm_root build_dir symbols_dir install_dir sdk architectures version cache; do |
| 93 | if [ -z ${!arg+x} ]; then |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 94 | error "Missing required argument '--${arg//_/-}'" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 95 | elif [ "${!arg}" == "" ]; then |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 96 | error "Argument to --${arg//_/-} must not be empty" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 97 | fi |
| 98 | done |
| 99 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 100 | # Allow using relative paths |
| 101 | for arg in llvm_root build_dir symbols_dir install_dir cache; do |
| 102 | path="$(realpath "${!arg}")" |
| 103 | eval "${arg}=\"${path}\"" |
| 104 | done |
| 105 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 106 | function step() { |
| 107 | separator="$(printf "%0.s-" $(seq 1 ${#1}))" |
| 108 | echo |
| 109 | echo "${separator}" |
| 110 | echo "${1}" |
| 111 | echo "${separator}" |
| 112 | } |
| 113 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 114 | install_name_dir="/usr/lib" |
| 115 | dylib_name="libc++.1.dylib" |
| 116 | make_symlink="yes" |
| 117 | headers_prefix="${install_dir}" |
| 118 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 119 | for arch in ${architectures}; do |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 120 | step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 121 | mkdir -p "${build_dir}/${arch}" |
| 122 | (cd "${build_dir}/${arch}" && |
Louis Dionne | dab02ef | 2020-06-18 14:04:01 -0400 | [diff] [blame] | 123 | xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxx/utils/ci/runtimes" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 124 | -GNinja \ |
| 125 | -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \ |
Louis Dionne | 792b90a | 2020-06-04 10:18:50 -0400 | [diff] [blame] | 126 | -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 127 | -C "${cache}" \ |
| 128 | -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \ |
| 129 | -DCMAKE_INSTALL_NAME_DIR="${install_name_dir}" \ |
| 130 | -DCMAKE_OSX_ARCHITECTURES="${arch}" \ |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 131 | -DLIBCXXABI_LIBRARY_VERSION="${version}" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 132 | -DLIBCXX_INCLUDE_BENCHMARKS=OFF \ |
| 133 | -DLIBCXX_INCLUDE_TESTS=OFF |
| 134 | ) |
| 135 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 136 | xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxx install-cxxabi -- -v |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 137 | done |
| 138 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 139 | function universal_dylib() { |
| 140 | dylib=${1} |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 141 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 142 | inputs=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/${dylib}"; done) |
| 143 | |
| 144 | step "Creating a universal dylib ${dylib} from the dylibs for all architectures" |
| 145 | xcrun --sdk "${sdk}" lipo -create ${inputs} -output "${build_dir}/${dylib}" |
| 146 | |
| 147 | step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib" |
| 148 | mkdir -p "${install_dir}/usr/lib" |
| 149 | cp "${build_dir}/${dylib}" "${install_dir}/usr/lib/${dylib}" |
| 150 | xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib}" |
| 151 | |
| 152 | step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}" |
| 153 | xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib}" -o "${symbols_dir}/${dylib}.dSYM" |
| 154 | cp "${build_dir}/${dylib}" "${symbols_dir}/${dylib}" |
| 155 | } |
| 156 | |
| 157 | universal_dylib ${dylib_name} |
| 158 | universal_dylib libc++abi.dylib |
| 159 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 160 | if [[ "${make_symlink}" == "yes" ]]; then |
| 161 | (cd "${install_dir}/usr/lib" && ln -s "${dylib_name}" libc++.dylib) |
| 162 | fi |
| 163 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 164 | # Install the headers by copying the headers from one of the built architectures |
| 165 | # into the install directory. Headers from all architectures should be the same. |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 166 | step "Installing the libc++ and libc++abi headers to ${headers_prefix}/usr/include" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 167 | any_arch=$(echo ${architectures} | cut -d ' ' -f 1) |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 168 | mkdir -p "${headers_prefix}/usr/include" |
| 169 | ditto "${build_dir}/${any_arch}-install/include" "${headers_prefix}/usr/include" |
| 170 | ditto "${llvm_root}/libcxxabi/include" "${headers_prefix}/usr/include" # TODO: libcxxabi should install its headers in CMake |
| 171 | if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root |
| 172 | chown -R root:wheel "${headers_prefix}/usr/include" |
| 173 | fi |
| 174 | |
| 175 | step "Installing the libc++ and libc++abi licenses" |
| 176 | mkdir -p "${headers_prefix}/usr/local/OpenSourceLicenses" |
| 177 | cp "${llvm_root}/libcxx/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxx.txt" |
| 178 | cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxxabi.txt" |
| 179 | |
| 180 | # Also install a static archive for libc++abi |
| 181 | libcxxabi_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++abi.a"; done) |
| 182 | step "Creating a universal libc++abi static archive from the static archives for each architecture" |
| 183 | mkdir -p "${install_dir}/usr/local/lib/libcxx" |
| 184 | xcrun --sdk "${sdk}" libtool -static ${libcxxabi_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a" |