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. |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 43 | EOF |
| 44 | } |
| 45 | |
| 46 | while [[ $# -gt 0 ]]; do |
| 47 | case ${1} in |
| 48 | -h|--help) |
| 49 | usage |
| 50 | exit 0 |
| 51 | ;; |
| 52 | --llvm-root) |
| 53 | llvm_root="${2}" |
| 54 | shift; shift |
| 55 | ;; |
| 56 | --build-dir) |
| 57 | build_dir="${2}" |
| 58 | shift; shift |
| 59 | ;; |
| 60 | --symbols-dir) |
| 61 | symbols_dir="${2}" |
| 62 | shift; shift |
| 63 | ;; |
| 64 | --install-dir) |
| 65 | install_dir="${2}" |
| 66 | shift; shift |
| 67 | ;; |
| 68 | --sdk) |
| 69 | sdk="${2}" |
| 70 | shift; shift |
| 71 | ;; |
| 72 | --architectures) |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 73 | architectures="${2}" |
| 74 | shift; shift |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 75 | ;; |
| 76 | --version) |
| 77 | version="${2}" |
| 78 | shift; shift |
| 79 | ;; |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 80 | *) |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 81 | error "Unknown argument '${1}'" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 82 | ;; |
| 83 | esac |
| 84 | done |
| 85 | |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 86 | for arg in llvm_root build_dir symbols_dir install_dir sdk architectures version; do |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 87 | if [ -z ${!arg+x} ]; then |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 88 | error "Missing required argument '--${arg//_/-}'" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 89 | elif [ "${!arg}" == "" ]; then |
Louis Dionne | f0928ed | 2020-04-23 13:53:14 -0400 | [diff] [blame] | 90 | error "Argument to --${arg//_/-} must not be empty" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 91 | fi |
| 92 | done |
| 93 | |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 94 | # Allow using relative paths |
Louis Dionne | 7293556 | 2020-10-16 12:57:30 -0400 | [diff] [blame] | 95 | function realpath() { |
| 96 | if [[ $1 = /* ]]; then echo "$1"; else echo "$(pwd)/${1#./}"; fi |
| 97 | } |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 98 | for arg in llvm_root build_dir symbols_dir install_dir; do |
Louis Dionne | 44f35c3 | 2020-10-13 12:27:22 -0400 | [diff] [blame] | 99 | path="$(realpath "${!arg}")" |
| 100 | eval "${arg}=\"${path}\"" |
| 101 | done |
| 102 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 103 | function step() { |
| 104 | separator="$(printf "%0.s-" $(seq 1 ${#1}))" |
| 105 | echo |
| 106 | echo "${separator}" |
| 107 | echo "${1}" |
| 108 | echo "${separator}" |
| 109 | } |
| 110 | |
| 111 | for arch in ${architectures}; do |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 112 | step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 113 | mkdir -p "${build_dir}/${arch}" |
| 114 | (cd "${build_dir}/${arch}" && |
Louis Dionne | dab02ef | 2020-06-18 14:04:01 -0400 | [diff] [blame] | 115 | xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxx/utils/ci/runtimes" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 116 | -GNinja \ |
| 117 | -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \ |
Louis Dionne | 792b90a | 2020-06-04 10:18:50 -0400 | [diff] [blame] | 118 | -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \ |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 119 | -C "${llvm_root}/libcxx/cmake/caches/Apple.cmake" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 120 | -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \ |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 121 | -DCMAKE_INSTALL_NAME_DIR="/usr/lib" \ |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 122 | -DCMAKE_OSX_ARCHITECTURES="${arch}" \ |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 123 | -DLIBCXXABI_LIBRARY_VERSION="${version}" \ |
Louis Dionne | aabc8e4 | 2021-09-29 14:56:42 -0400 | [diff] [blame] | 124 | -DLIBCXX_INCLUDE_BENCHMARKS=OFF \ |
| 125 | -DLIBCXX_TEST_CONFIG="${llvm_root}/libcxx/test/configs/apple-libc++-shared.cfg.in" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 126 | ) |
| 127 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 128 | 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] | 129 | done |
| 130 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 131 | function universal_dylib() { |
| 132 | dylib=${1} |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 133 | |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 134 | inputs=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/${dylib}"; done) |
| 135 | |
| 136 | step "Creating a universal dylib ${dylib} from the dylibs for all architectures" |
| 137 | xcrun --sdk "${sdk}" lipo -create ${inputs} -output "${build_dir}/${dylib}" |
| 138 | |
| 139 | step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib" |
| 140 | mkdir -p "${install_dir}/usr/lib" |
| 141 | cp "${build_dir}/${dylib}" "${install_dir}/usr/lib/${dylib}" |
| 142 | xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib}" |
| 143 | |
| 144 | step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}" |
| 145 | xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib}" -o "${symbols_dir}/${dylib}.dSYM" |
| 146 | cp "${build_dir}/${dylib}" "${symbols_dir}/${dylib}" |
| 147 | } |
| 148 | |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 149 | universal_dylib libc++.1.dylib |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 150 | universal_dylib libc++abi.dylib |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 151 | (cd "${install_dir}/usr/lib" && ln -s "libc++.1.dylib" libc++.dylib) |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 152 | |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 153 | # Install the headers by copying the headers from one of the built architectures |
| 154 | # into the install directory. Headers from all architectures should be the same. |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 155 | step "Installing the libc++ and libc++abi headers to ${install_dir}/usr/include" |
Louis Dionne | 544ea1b | 2020-04-08 15:26:31 -0400 | [diff] [blame] | 156 | any_arch=$(echo ${architectures} | cut -d ' ' -f 1) |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 157 | mkdir -p "${install_dir}/usr/include" |
| 158 | ditto "${build_dir}/${any_arch}-install/include" "${install_dir}/usr/include" |
| 159 | ditto "${llvm_root}/libcxxabi/include" "${install_dir}/usr/include" # TODO: libcxxabi should install its headers in CMake |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 160 | if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 161 | chown -R root:wheel "${install_dir}/usr/include" |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 162 | fi |
| 163 | |
| 164 | step "Installing the libc++ and libc++abi licenses" |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 165 | mkdir -p "${install_dir}/usr/local/OpenSourceLicenses" |
| 166 | cp "${llvm_root}/libcxx/LICENSE.TXT" "${install_dir}/usr/local/OpenSourceLicenses/libcxx.txt" |
| 167 | cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${install_dir}/usr/local/OpenSourceLicenses/libcxxabi.txt" |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 168 | |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 169 | # Also install universal static archives for libc++ and libc++abi |
| 170 | libcxx_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++.a"; done) |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 171 | libcxxabi_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++abi.a"; done) |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 172 | step "Creating universal static archives for libc++ and libc++abi from the static archives for each architecture" |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 173 | mkdir -p "${install_dir}/usr/local/lib/libcxx" |
Louis Dionne | 3aba780 | 2021-06-03 18:26:31 -0400 | [diff] [blame] | 174 | xcrun --sdk "${sdk}" libtool -static ${libcxx_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++-static.a" |
Louis Dionne | c1c0569 | 2020-06-04 10:32:16 -0400 | [diff] [blame] | 175 | xcrun --sdk "${sdk}" libtool -static ${libcxxabi_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a" |