blob: 1337ca2c271f1c1047d1b4875a44cd60733c9273 [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}")"
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
105install_name_dir="/usr/lib"
106dylib_name="libc++.1.dylib"
107make_symlink="yes"
108
109function step() {
110 separator="$(printf "%0.s-" $(seq 1 ${#1}))"
111 echo
112 echo "${separator}"
113 echo "${1}"
114 echo "${separator}"
115}
116
117for 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
133done
134
135step "Creating a universal dylib from the dylibs for all architectures"
136input_dylibs=$(for arch in ${architectures}; do
137 echo "${build_dir}/${arch}-install/lib/${dylib_name}"
138done)
139xcrun --sdk "${sdk}" lipo -create ${input_dylibs} -output "${build_dir}/${dylib_name}"
140
141step "Installing the (stripped) universal dylib to ${install_dir}/usr/lib"
142mkdir -p "${install_dir}/usr/lib"
143cp "${build_dir}/${dylib_name}" "${install_dir}/usr/lib/${dylib_name}"
144xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib_name}"
145if [[ "${make_symlink}" == "yes" ]]; then
146 (cd "${install_dir}/usr/lib" && ln -s "${dylib_name}" libc++.dylib)
147fi
148
149step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}"
150xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib_name}" -o "${symbols_dir}/${dylib_name}.dSYM"
151cp "${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#
157any_arch=$(echo ${architectures} | cut -d ' ' -f 1)
158mkdir -p "${install_dir}/usr/include"
159ditto "${build_dir}/${any_arch}-install/include" "${install_dir}/usr/include"