Louis Dionne | 77b46fb | 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}")" |
| 13 | function usage() { |
| 14 | cat <<EOF |
| 15 | Usage: |
| 16 | ${PROGNAME} [-h|--help] --llvm-root <DIR> --build-dir <DIR> --install-dir <DIR> --symbols-dir <DIR> --sdk <SDK> --architectures <architectures...> --version <X.Y.Z> --cache <PATH> |
| 17 | |
| 18 | --llvm-root Full path to the root of the LLVM monorepo. Only the libcxx |
| 19 | and libcxxabi directories are required. |
| 20 | |
| 21 | --build-dir Directory to use for building. This will contain intermediate |
| 22 | build products. |
| 23 | |
| 24 | --install-dir Directory to install the library to. |
| 25 | |
| 26 | --symbols-dir Directory to install the .dSYM bundle to. |
| 27 | |
| 28 | --sdk SDK used for building the library. This represents the target |
| 29 | platform that the library will run on. You can get a list of |
| 30 | SDKs with \`xcodebuild -showsdks\`. |
| 31 | |
| 32 | --architectures A whitespace separated list of architectures to build for. |
| 33 | The library will be built for each architecture independently, |
| 34 | and a universal binary containing all architectures will be |
| 35 | created from that. |
| 36 | |
| 37 | --version The version of the library to encode in the dylib. |
| 38 | |
| 39 | --cache The CMake cache to use to control how the library gets built. |
| 40 | EOF |
| 41 | } |
| 42 | |
| 43 | while [[ $# -gt 0 ]]; do |
| 44 | case ${1} in |
| 45 | -h|--help) |
| 46 | usage |
| 47 | exit 0 |
| 48 | ;; |
| 49 | --llvm-root) |
| 50 | llvm_root="${2}" |
| 51 | shift; shift |
| 52 | ;; |
| 53 | --build-dir) |
| 54 | build_dir="${2}" |
| 55 | shift; shift |
| 56 | ;; |
| 57 | --symbols-dir) |
| 58 | symbols_dir="${2}" |
| 59 | shift; shift |
| 60 | ;; |
| 61 | --install-dir) |
| 62 | install_dir="${2}" |
| 63 | shift; shift |
| 64 | ;; |
| 65 | --sdk) |
| 66 | sdk="${2}" |
| 67 | shift; shift |
| 68 | ;; |
| 69 | --architectures) |
| 70 | architectures="" |
| 71 | shift |
| 72 | while [[ $# -gt 0 ]]; do |
| 73 | if [[ "${1}" == "-"* ]]; then |
| 74 | break |
| 75 | fi |
| 76 | architectures+=" ${1}" |
| 77 | shift |
| 78 | done |
| 79 | ;; |
| 80 | --version) |
| 81 | version="${2}" |
| 82 | shift; shift |
| 83 | ;; |
| 84 | --cache) |
| 85 | cache="${2}" |
| 86 | shift; shift |
| 87 | ;; |
| 88 | *) |
| 89 | echo "Unknown argument '${1}'" |
| 90 | exit 1 |
| 91 | ;; |
| 92 | esac |
| 93 | done |
| 94 | |
| 95 | for arg in llvm_root build_dir symbols_dir install_dir sdk architectures version cache; do |
| 96 | if [ -z ${!arg+x} ]; then |
| 97 | echo "Missing required argument '--${arg//_/-}'" |
| 98 | exit 1 |
| 99 | elif [ "${!arg}" == "" ]; then |
| 100 | echo "Argument to --${arg//_/-} must not be empty" |
| 101 | exit 1 |
| 102 | fi |
| 103 | done |
| 104 | |
| 105 | function step() { |
| 106 | separator="$(printf "%0.s-" $(seq 1 ${#1}))" |
| 107 | echo |
| 108 | echo "${separator}" |
| 109 | echo "${1}" |
| 110 | echo "${separator}" |
| 111 | } |
| 112 | |
| 113 | install_name_dir="/usr/lib" |
| 114 | headers_prefix="${install_dir}" |
| 115 | |
| 116 | for arch in ${architectures}; do |
| 117 | step "Building libc++abi.dylib for architecture ${arch}" |
| 118 | mkdir -p "${build_dir}/${arch}" |
| 119 | (cd "${build_dir}/${arch}" && |
| 120 | xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxxabi" \ |
| 121 | -GNinja \ |
| 122 | -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \ |
| 123 | -C "${cache}" \ |
| 124 | -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \ |
| 125 | -DCMAKE_INSTALL_NAME_DIR="${install_name_dir}" \ |
| 126 | -DCMAKE_OSX_ARCHITECTURES="${arch}" \ |
| 127 | -DLIBCXXABI_LIBRARY_VERSION="${version}" \ |
| 128 | -DLIBCXXABI_LIBCXX_INCLUDES="$(xcrun --sdk "${sdk}" c++ -print-resource-dir)/../../../include/c++/v1" |
| 129 | ) |
| 130 | |
| 131 | xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxxabi -- -v |
| 132 | done |
| 133 | |
| 134 | all_dylibs=$(for arch in ${architectures}; do |
| 135 | echo "${build_dir}/${arch}-install/lib/libc++abi.dylib" |
| 136 | done) |
| 137 | |
| 138 | all_archives=$(for arch in ${architectures}; do |
| 139 | echo "${build_dir}/${arch}-install/lib/libc++abi.a" |
| 140 | done) |
| 141 | |
| 142 | step "Creating a universal dylib from the dylibs for each architecture at ${install_dir}/usr/lib" |
| 143 | xcrun --sdk "${sdk}" lipo -create ${all_dylibs} -output "${build_dir}/libc++abi.dylib" |
| 144 | |
| 145 | step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib" |
| 146 | mkdir -p "${install_dir}/usr/lib" |
| 147 | cp "${build_dir}/libc++abi.dylib" "${install_dir}/usr/lib/libc++abi.dylib" |
| 148 | xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/libc++abi.dylib" |
| 149 | |
| 150 | step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}" |
| 151 | xcrun --sdk "${sdk}" dsymutil "${build_dir}/libc++abi.dylib" -o "${symbols_dir}/libc++abi.dylib.dSYM" |
| 152 | cp "${build_dir}/libc++abi.dylib" "${symbols_dir}/libc++abi.dylib" |
| 153 | |
| 154 | step "Creating a universal static archive from the static archives for each architecture" |
| 155 | mkdir -p "${install_dir}/usr/local/lib/libcxx" |
| 156 | xcrun --sdk "${sdk}" libtool -static ${all_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a" |
| 157 | |
| 158 | # |
| 159 | # Install the headers by copying the headers from the source directory into |
| 160 | # the install directory. |
| 161 | # TODO: In the future, we should install the headers through CMake. |
| 162 | # |
| 163 | step "Installing the libc++abi headers to ${headers_prefix}/usr/include" |
| 164 | mkdir -p "${headers_prefix}/usr/include" |
| 165 | ditto "${llvm_root}/libcxxabi/include" "${headers_prefix}/usr/include" |
| 166 | if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root |
| 167 | chown -R root:wheel "${headers_prefix}/usr/include" |
| 168 | fi |
| 169 | |
| 170 | step "Installing the libc++abi license" |
| 171 | mkdir -p "${headers_prefix}/usr/local/OpenSourceLicenses" |
| 172 | cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxxabi.txt" |