blob: 54d08b3dee92323e58803e869fa9cf0d4ad8d9d2 [file] [log] [blame]
Louis Dionne544ea1b2020-04-08 15:26:31 -04001#!/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
10set -e
11
12PROGNAME="$(basename "${0}")"
Louis Dionnef0928ed2020-04-23 13:53:14 -040013
14function error() { printf "error: %s\n" "$*"; exit 1; }
15
Louis Dionne544ea1b2020-04-08 15:26:31 -040016function usage() {
17cat <<EOF
18Usage:
Louis Dionnef0928ed2020-04-23 13:53:14 -040019${PROGNAME} [options]
Louis Dionne544ea1b2020-04-08 15:26:31 -040020
Louis Dionnef0928ed2020-04-23 13:53:14 -040021[-h|--help] Display this help and exit.
Louis Dionne544ea1b2020-04-08 15:26:31 -040022
Louis Dionne44f35c32020-10-13 12:27:22 -040023--llvm-root <DIR> Path to the root of the LLVM monorepo. Only the libcxx
Louis Dionnef0928ed2020-04-23 13:53:14 -040024 and libcxxabi directories are required.
Louis Dionne544ea1b2020-04-08 15:26:31 -040025
Louis Dionne44f35c32020-10-13 12:27:22 -040026--build-dir <DIR> Path to the directory to use for building. This will
Louis Dionnef0928ed2020-04-23 13:53:14 -040027 contain intermediate build products.
Louis Dionne544ea1b2020-04-08 15:26:31 -040028
Louis Dionne44f35c32020-10-13 12:27:22 -040029--install-dir <DIR> Path to the directory to install the library to.
Louis Dionne544ea1b2020-04-08 15:26:31 -040030
Louis Dionne44f35c32020-10-13 12:27:22 -040031--symbols-dir <DIR> Path to the directory to install the .dSYM bundle to.
Louis Dionne544ea1b2020-04-08 15:26:31 -040032
Louis Dionnef0928ed2020-04-23 13:53:14 -040033--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 Dionne544ea1b2020-04-08 15:26:31 -040036
Louis Dionnef0928ed2020-04-23 13:53:14 -040037--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 Dionne544ea1b2020-04-08 15:26:31 -040041
Louis Dionnef0928ed2020-04-23 13:53:14 -040042--version X[.Y[.Z]] The version of the library to encode in the dylib.
Louis Dionne544ea1b2020-04-08 15:26:31 -040043EOF
44}
45
46while [[ $# -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 Dionnef0928ed2020-04-23 13:53:14 -040073 architectures="${2}"
74 shift; shift
Louis Dionne544ea1b2020-04-08 15:26:31 -040075 ;;
76 --version)
77 version="${2}"
78 shift; shift
79 ;;
Louis Dionne544ea1b2020-04-08 15:26:31 -040080 *)
Louis Dionnef0928ed2020-04-23 13:53:14 -040081 error "Unknown argument '${1}'"
Louis Dionne544ea1b2020-04-08 15:26:31 -040082 ;;
83 esac
84done
85
Louis Dionne3aba7802021-06-03 18:26:31 -040086for arg in llvm_root build_dir symbols_dir install_dir sdk architectures version; do
Louis Dionne544ea1b2020-04-08 15:26:31 -040087 if [ -z ${!arg+x} ]; then
Louis Dionnef0928ed2020-04-23 13:53:14 -040088 error "Missing required argument '--${arg//_/-}'"
Louis Dionne544ea1b2020-04-08 15:26:31 -040089 elif [ "${!arg}" == "" ]; then
Louis Dionnef0928ed2020-04-23 13:53:14 -040090 error "Argument to --${arg//_/-} must not be empty"
Louis Dionne544ea1b2020-04-08 15:26:31 -040091 fi
92done
93
Louis Dionne44f35c32020-10-13 12:27:22 -040094# Allow using relative paths
Louis Dionne72935562020-10-16 12:57:30 -040095function realpath() {
96 if [[ $1 = /* ]]; then echo "$1"; else echo "$(pwd)/${1#./}"; fi
97}
Louis Dionne3aba7802021-06-03 18:26:31 -040098for arg in llvm_root build_dir symbols_dir install_dir; do
Louis Dionne44f35c32020-10-13 12:27:22 -040099 path="$(realpath "${!arg}")"
100 eval "${arg}=\"${path}\""
101done
102
Louis Dionne544ea1b2020-04-08 15:26:31 -0400103function step() {
104 separator="$(printf "%0.s-" $(seq 1 ${#1}))"
105 echo
106 echo "${separator}"
107 echo "${1}"
108 echo "${separator}"
109}
110
111for arch in ${architectures}; do
Louis Dionnec1c05692020-06-04 10:32:16 -0400112 step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}"
Louis Dionne544ea1b2020-04-08 15:26:31 -0400113 mkdir -p "${build_dir}/${arch}"
114 (cd "${build_dir}/${arch}" &&
Louis Dionnefb82b632021-10-07 16:07:59 -0400115 xcrun --sdk "${sdk}" cmake "${llvm_root}/runtimes" \
Louis Dionne544ea1b2020-04-08 15:26:31 -0400116 -GNinja \
117 -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \
Louis Dionnefb82b632021-10-07 16:07:59 -0400118 -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
Louis Dionne3aba7802021-06-03 18:26:31 -0400119 -C "${llvm_root}/libcxx/cmake/caches/Apple.cmake" \
Louis Dionne544ea1b2020-04-08 15:26:31 -0400120 -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \
Louis Dionne3aba7802021-06-03 18:26:31 -0400121 -DCMAKE_INSTALL_NAME_DIR="/usr/lib" \
Louis Dionne544ea1b2020-04-08 15:26:31 -0400122 -DCMAKE_OSX_ARCHITECTURES="${arch}" \
Louis Dionnec1c05692020-06-04 10:32:16 -0400123 -DLIBCXXABI_LIBRARY_VERSION="${version}" \
Louis Dionneaabc8e42021-09-29 14:56:42 -0400124 -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
Louis Dionne23c7e4c2021-10-12 17:45:45 -0400125 -DLIBCXX_TEST_CONFIG="apple-libc++-shared.cfg.in" \
126 -DLIBCXXABI_TEST_CONFIG="apple-libc++abi-shared.cfg.in"
Louis Dionne544ea1b2020-04-08 15:26:31 -0400127 )
128
Louis Dionnec1c05692020-06-04 10:32:16 -0400129 xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxx install-cxxabi -- -v
Louis Dionne544ea1b2020-04-08 15:26:31 -0400130done
131
Louis Dionnec1c05692020-06-04 10:32:16 -0400132function universal_dylib() {
133 dylib=${1}
Louis Dionne544ea1b2020-04-08 15:26:31 -0400134
Louis Dionnec1c05692020-06-04 10:32:16 -0400135 inputs=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/${dylib}"; done)
136
137 step "Creating a universal dylib ${dylib} from the dylibs for all architectures"
138 xcrun --sdk "${sdk}" lipo -create ${inputs} -output "${build_dir}/${dylib}"
139
140 step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib"
141 mkdir -p "${install_dir}/usr/lib"
142 cp "${build_dir}/${dylib}" "${install_dir}/usr/lib/${dylib}"
143 xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib}"
144
145 step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}"
146 xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib}" -o "${symbols_dir}/${dylib}.dSYM"
147 cp "${build_dir}/${dylib}" "${symbols_dir}/${dylib}"
148}
149
Louis Dionne3aba7802021-06-03 18:26:31 -0400150universal_dylib libc++.1.dylib
Louis Dionnec1c05692020-06-04 10:32:16 -0400151universal_dylib libc++abi.dylib
Louis Dionne3aba7802021-06-03 18:26:31 -0400152(cd "${install_dir}/usr/lib" && ln -s "libc++.1.dylib" libc++.dylib)
Louis Dionne544ea1b2020-04-08 15:26:31 -0400153
Louis Dionne544ea1b2020-04-08 15:26:31 -0400154# 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.
Louis Dionne3aba7802021-06-03 18:26:31 -0400156step "Installing the libc++ and libc++abi headers to ${install_dir}/usr/include"
Louis Dionne544ea1b2020-04-08 15:26:31 -0400157any_arch=$(echo ${architectures} | cut -d ' ' -f 1)
Louis Dionne3aba7802021-06-03 18:26:31 -0400158mkdir -p "${install_dir}/usr/include"
159ditto "${build_dir}/${any_arch}-install/include" "${install_dir}/usr/include"
160ditto "${llvm_root}/libcxxabi/include" "${install_dir}/usr/include" # TODO: libcxxabi should install its headers in CMake
Louis Dionnec1c05692020-06-04 10:32:16 -0400161if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root
Louis Dionne3aba7802021-06-03 18:26:31 -0400162 chown -R root:wheel "${install_dir}/usr/include"
Louis Dionnec1c05692020-06-04 10:32:16 -0400163fi
164
165step "Installing the libc++ and libc++abi licenses"
Louis Dionne3aba7802021-06-03 18:26:31 -0400166mkdir -p "${install_dir}/usr/local/OpenSourceLicenses"
167cp "${llvm_root}/libcxx/LICENSE.TXT" "${install_dir}/usr/local/OpenSourceLicenses/libcxx.txt"
168cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${install_dir}/usr/local/OpenSourceLicenses/libcxxabi.txt"
Louis Dionnec1c05692020-06-04 10:32:16 -0400169
Louis Dionne3aba7802021-06-03 18:26:31 -0400170# Also install universal static archives for libc++ and libc++abi
171libcxx_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++.a"; done)
Louis Dionnec1c05692020-06-04 10:32:16 -0400172libcxxabi_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++abi.a"; done)
Louis Dionne3aba7802021-06-03 18:26:31 -0400173step "Creating universal static archives for libc++ and libc++abi from the static archives for each architecture"
Louis Dionnec1c05692020-06-04 10:32:16 -0400174mkdir -p "${install_dir}/usr/local/lib/libcxx"
Louis Dionne3aba7802021-06-03 18:26:31 -0400175xcrun --sdk "${sdk}" libtool -static ${libcxx_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++-static.a"
Louis Dionnec1c05692020-06-04 10:32:16 -0400176xcrun --sdk "${sdk}" libtool -static ${libcxxabi_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a"