blob: d550016a4c61ca888ca6e1502d83dbbb52237af8 [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 Dionnef0928ed2020-04-23 13:53:14 -040023--llvm-root <DIR> Full path to the root of the LLVM monorepo. Only the libcxx
24 and libcxxabi directories are required.
Louis Dionne544ea1b2020-04-08 15:26:31 -040025
Louis Dionnef0928ed2020-04-23 13:53:14 -040026--build-dir <DIR> Full path to the directory to use for building. This will
27 contain intermediate build products.
Louis Dionne544ea1b2020-04-08 15:26:31 -040028
Louis Dionnef0928ed2020-04-23 13:53:14 -040029--install-dir <DIR> Full path to the directory to install the library to.
Louis Dionne544ea1b2020-04-08 15:26:31 -040030
Louis Dionnef0928ed2020-04-23 13:53:14 -040031--symbols-dir <DIR> Full 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.
43
44--cache <PATH> The CMake cache to use to control how the library gets built.
Louis Dionne544ea1b2020-04-08 15:26:31 -040045EOF
46}
47
48while [[ $# -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 Dionnef0928ed2020-04-23 13:53:14 -040075 architectures="${2}"
76 shift; shift
Louis Dionne544ea1b2020-04-08 15:26:31 -040077 ;;
78 --version)
79 version="${2}"
80 shift; shift
81 ;;
82 --cache)
83 cache="${2}"
84 shift; shift
85 ;;
86 *)
Louis Dionnef0928ed2020-04-23 13:53:14 -040087 error "Unknown argument '${1}'"
Louis Dionne544ea1b2020-04-08 15:26:31 -040088 ;;
89 esac
90done
91
92for arg in llvm_root build_dir symbols_dir install_dir sdk architectures version cache; do
93 if [ -z ${!arg+x} ]; then
Louis Dionnef0928ed2020-04-23 13:53:14 -040094 error "Missing required argument '--${arg//_/-}'"
Louis Dionne544ea1b2020-04-08 15:26:31 -040095 elif [ "${!arg}" == "" ]; then
Louis Dionnef0928ed2020-04-23 13:53:14 -040096 error "Argument to --${arg//_/-} must not be empty"
Louis Dionne544ea1b2020-04-08 15:26:31 -040097 fi
98done
99
100function step() {
101 separator="$(printf "%0.s-" $(seq 1 ${#1}))"
102 echo
103 echo "${separator}"
104 echo "${1}"
105 echo "${separator}"
106}
107
108install_name_dir="/usr/lib"
109headers_prefix="${install_dir}"
110
111for arch in ${architectures}; do
112 step "Building libc++abi.dylib for architecture ${arch}"
113 mkdir -p "${build_dir}/${arch}"
114 (cd "${build_dir}/${arch}" &&
115 xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxxabi" \
116 -GNinja \
117 -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \
118 -C "${cache}" \
119 -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \
120 -DCMAKE_INSTALL_NAME_DIR="${install_name_dir}" \
121 -DCMAKE_OSX_ARCHITECTURES="${arch}" \
122 -DLIBCXXABI_LIBRARY_VERSION="${version}" \
Louis Dionne58e691e2020-04-27 11:18:24 -0400123 -DLIBCXXABI_LIBCXX_PATH="${llvm_root}/libcxx"
Louis Dionne544ea1b2020-04-08 15:26:31 -0400124 )
125
126 xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxxabi -- -v
127done
128
129all_dylibs=$(for arch in ${architectures}; do
130 echo "${build_dir}/${arch}-install/lib/libc++abi.dylib"
131done)
132
133all_archives=$(for arch in ${architectures}; do
134 echo "${build_dir}/${arch}-install/lib/libc++abi.a"
135done)
136
137step "Creating a universal dylib from the dylibs for each architecture at ${install_dir}/usr/lib"
138xcrun --sdk "${sdk}" lipo -create ${all_dylibs} -output "${build_dir}/libc++abi.dylib"
139
140step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib"
141mkdir -p "${install_dir}/usr/lib"
142cp "${build_dir}/libc++abi.dylib" "${install_dir}/usr/lib/libc++abi.dylib"
143xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/libc++abi.dylib"
144
145step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}"
146xcrun --sdk "${sdk}" dsymutil "${build_dir}/libc++abi.dylib" -o "${symbols_dir}/libc++abi.dylib.dSYM"
147cp "${build_dir}/libc++abi.dylib" "${symbols_dir}/libc++abi.dylib"
148
149step "Creating a universal static archive from the static archives for each architecture"
150mkdir -p "${install_dir}/usr/local/lib/libcxx"
151xcrun --sdk "${sdk}" libtool -static ${all_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a"
152
153#
154# Install the headers by copying the headers from the source directory into
155# the install directory.
156# TODO: In the future, we should install the headers through CMake.
157#
158step "Installing the libc++abi headers to ${headers_prefix}/usr/include"
159mkdir -p "${headers_prefix}/usr/include"
160ditto "${llvm_root}/libcxxabi/include" "${headers_prefix}/usr/include"
161if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root
162 chown -R root:wheel "${headers_prefix}/usr/include"
163fi
164
165step "Installing the libc++abi license"
166mkdir -p "${headers_prefix}/usr/local/OpenSourceLicenses"
167cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxxabi.txt"