blob: cf1e462b322583e50ebf14b4c491033aa129e7af [file] [log] [blame]
Louis Dionne77b46fb2020-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}")"
13function usage() {
14cat <<EOF
15Usage:
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.
40EOF
41}
42
43while [[ $# -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
93done
94
95for 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
103done
104
105function step() {
106 separator="$(printf "%0.s-" $(seq 1 ${#1}))"
107 echo
108 echo "${separator}"
109 echo "${1}"
110 echo "${separator}"
111}
112
113install_name_dir="/usr/lib"
114headers_prefix="${install_dir}"
115
116for 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
132done
133
134all_dylibs=$(for arch in ${architectures}; do
135 echo "${build_dir}/${arch}-install/lib/libc++abi.dylib"
136done)
137
138all_archives=$(for arch in ${architectures}; do
139 echo "${build_dir}/${arch}-install/lib/libc++abi.a"
140done)
141
142step "Creating a universal dylib from the dylibs for each architecture at ${install_dir}/usr/lib"
143xcrun --sdk "${sdk}" lipo -create ${all_dylibs} -output "${build_dir}/libc++abi.dylib"
144
145step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib"
146mkdir -p "${install_dir}/usr/lib"
147cp "${build_dir}/libc++abi.dylib" "${install_dir}/usr/lib/libc++abi.dylib"
148xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/libc++abi.dylib"
149
150step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}"
151xcrun --sdk "${sdk}" dsymutil "${build_dir}/libc++abi.dylib" -o "${symbols_dir}/libc++abi.dylib.dSYM"
152cp "${build_dir}/libc++abi.dylib" "${symbols_dir}/libc++abi.dylib"
153
154step "Creating a universal static archive from the static archives for each architecture"
155mkdir -p "${install_dir}/usr/local/lib/libcxx"
156xcrun --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#
163step "Installing the libc++abi headers to ${headers_prefix}/usr/include"
164mkdir -p "${headers_prefix}/usr/include"
165ditto "${llvm_root}/libcxxabi/include" "${headers_prefix}/usr/include"
166if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root
167 chown -R root:wheel "${headers_prefix}/usr/include"
168fi
169
170step "Installing the libc++abi license"
171mkdir -p "${headers_prefix}/usr/local/OpenSourceLicenses"
172cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxxabi.txt"