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}")" |
| 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 | install_name_dir="/usr/lib" |
| 106 | dylib_name="libc++.1.dylib" |
| 107 | make_symlink="yes" |
| 108 | |
| 109 | function step() { |
| 110 | separator="$(printf "%0.s-" $(seq 1 ${#1}))" |
| 111 | echo |
| 112 | echo "${separator}" |
| 113 | echo "${1}" |
| 114 | echo "${separator}" |
| 115 | } |
| 116 | |
| 117 | for arch in ${architectures}; do |
| 118 | step "Building libc++.dylib for architecture ${arch}" |
| 119 | mkdir -p "${build_dir}/${arch}" |
| 120 | (cd "${build_dir}/${arch}" && |
| 121 | xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxx" \ |
| 122 | -GNinja \ |
| 123 | -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \ |
| 124 | -C "${cache}" \ |
| 125 | -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \ |
| 126 | -DCMAKE_INSTALL_NAME_DIR="${install_name_dir}" \ |
| 127 | -DCMAKE_OSX_ARCHITECTURES="${arch}" \ |
| 128 | -DLIBCXX_INCLUDE_BENCHMARKS=OFF \ |
| 129 | -DLIBCXX_INCLUDE_TESTS=OFF |
| 130 | ) |
| 131 | |
| 132 | xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxx -- -v |
| 133 | done |
| 134 | |
| 135 | step "Creating a universal dylib from the dylibs for all architectures" |
| 136 | input_dylibs=$(for arch in ${architectures}; do |
| 137 | echo "${build_dir}/${arch}-install/lib/${dylib_name}" |
| 138 | done) |
| 139 | xcrun --sdk "${sdk}" lipo -create ${input_dylibs} -output "${build_dir}/${dylib_name}" |
| 140 | |
| 141 | step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib" |
| 142 | mkdir -p "${install_dir}/usr/lib" |
| 143 | cp "${build_dir}/${dylib_name}" "${install_dir}/usr/lib/${dylib_name}" |
| 144 | xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib_name}" |
| 145 | if [[ "${make_symlink}" == "yes" ]]; then |
| 146 | (cd "${install_dir}/usr/lib" && ln -s "${dylib_name}" libc++.dylib) |
| 147 | fi |
| 148 | |
| 149 | step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}" |
| 150 | xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib_name}" -o "${symbols_dir}/${dylib_name}.dSYM" |
| 151 | cp "${build_dir}/${dylib_name}" "${symbols_dir}/${dylib_name}" |
| 152 | |
| 153 | # |
| 154 | # Install the headers by copying the headers from one of the built architectures |
| 155 | # into the install directory. Headers from all architectures should be the same. |
| 156 | # |
| 157 | any_arch=$(echo ${architectures} | cut -d ' ' -f 1) |
| 158 | mkdir -p "${install_dir}/usr/include" |
| 159 | ditto "${build_dir}/${any_arch}-install/include" "${install_dir}/usr/include" |