xref: /openbsd-src/gnu/llvm/libcxx/utils/ci/apple-install-libcxx.sh (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
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
10set -e
11
12PROGNAME="$(basename "${0}")"
13
14function error() { printf "error: %s\n" "$*"; exit 1; }
15
16function usage() {
17cat <<EOF
18Usage:
19${PROGNAME} [options]
20
21[-h|--help]                  Display this help and exit.
22
23--llvm-root <DIR>            Full path to the root of the LLVM monorepo. Only the libcxx
24                             and libcxxabi directories are required.
25
26--build-dir <DIR>            Full path to the directory to use for building. This will
27                             contain intermediate build products.
28
29--install-dir <DIR>          Full path to the directory to install the library to.
30
31--symbols-dir <DIR>          Full path to the directory to install the .dSYM bundle to.
32
33--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\`.
36
37--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.
41
42--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.
45EOF
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)
75            architectures="${2}"
76            shift; shift
77            ;;
78        --version)
79            version="${2}"
80            shift; shift
81            ;;
82        --cache)
83            cache="${2}"
84            shift; shift
85            ;;
86        *)
87            error "Unknown argument '${1}'"
88            ;;
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
94        error "Missing required argument '--${arg//_/-}'"
95    elif [ "${!arg}" == "" ]; then
96        error "Argument to --${arg//_/-} must not be empty"
97    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"
109dylib_name="libc++.1.dylib"
110make_symlink="yes"
111headers_prefix="${install_dir}"
112
113for arch in ${architectures}; do
114    step "Building libc++.dylib and libc++abi.dylib for architecture ${arch}"
115    mkdir -p "${build_dir}/${arch}"
116    (cd "${build_dir}/${arch}" &&
117        xcrun --sdk "${sdk}" cmake "${llvm_root}/libcxx/utils/ci/runtimes" \
118            -GNinja \
119            -DCMAKE_MAKE_PROGRAM="$(xcrun --sdk "${sdk}" --find ninja)" \
120            -DLLVM_ENABLE_PROJECTS="libcxx;libcxxabi" \
121            -C "${cache}" \
122            -DCMAKE_INSTALL_PREFIX="${build_dir}/${arch}-install" \
123            -DCMAKE_INSTALL_NAME_DIR="${install_name_dir}" \
124            -DCMAKE_OSX_ARCHITECTURES="${arch}" \
125            -DLIBCXXABI_LIBRARY_VERSION="${version}" \
126            -DLIBCXX_INCLUDE_BENCHMARKS=OFF \
127            -DLIBCXX_INCLUDE_TESTS=OFF
128    )
129
130    xcrun --sdk "${sdk}" cmake --build "${build_dir}/${arch}" --target install-cxx install-cxxabi -- -v
131done
132
133function universal_dylib() {
134    dylib=${1}
135
136    inputs=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/${dylib}"; done)
137
138    step "Creating a universal dylib ${dylib} from the dylibs for all architectures"
139    xcrun --sdk "${sdk}" lipo -create ${inputs} -output "${build_dir}/${dylib}"
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}" "${install_dir}/usr/lib/${dylib}"
144    xcrun --sdk "${sdk}" strip -S "${install_dir}/usr/lib/${dylib}"
145
146    step "Installing the unstripped dylib and the dSYM bundle to ${symbols_dir}"
147    xcrun --sdk "${sdk}" dsymutil "${build_dir}/${dylib}" -o "${symbols_dir}/${dylib}.dSYM"
148    cp "${build_dir}/${dylib}" "${symbols_dir}/${dylib}"
149}
150
151universal_dylib ${dylib_name}
152universal_dylib libc++abi.dylib
153
154if [[ "${make_symlink}" == "yes" ]]; then
155    (cd "${install_dir}/usr/lib" && ln -s "${dylib_name}" libc++.dylib)
156fi
157
158# Install the headers by copying the headers from one of the built architectures
159# into the install directory. Headers from all architectures should be the same.
160step "Installing the libc++ and libc++abi headers to ${headers_prefix}/usr/include"
161any_arch=$(echo ${architectures} | cut -d ' ' -f 1)
162mkdir -p "${headers_prefix}/usr/include"
163ditto "${build_dir}/${any_arch}-install/include" "${headers_prefix}/usr/include"
164ditto "${llvm_root}/libcxxabi/include" "${headers_prefix}/usr/include" # TODO: libcxxabi should install its headers in CMake
165if [[ $EUID -eq 0 ]]; then # Only chown if we're running as root
166    chown -R root:wheel "${headers_prefix}/usr/include"
167fi
168
169step "Installing the libc++ and libc++abi licenses"
170mkdir -p "${headers_prefix}/usr/local/OpenSourceLicenses"
171cp "${llvm_root}/libcxx/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxx.txt"
172cp "${llvm_root}/libcxxabi/LICENSE.TXT" "${headers_prefix}/usr/local/OpenSourceLicenses/libcxxabi.txt"
173
174# Also install a static archive for libc++abi
175libcxxabi_archives=$(for arch in ${architectures}; do echo "${build_dir}/${arch}-install/lib/libc++abi.a"; done)
176step "Creating a universal libc++abi static archive from the static archives for each architecture"
177mkdir -p "${install_dir}/usr/local/lib/libcxx"
178xcrun --sdk "${sdk}" libtool -static ${libcxxabi_archives} -o "${install_dir}/usr/local/lib/libcxx/libc++abi-static.a"
179