xref: /aosp_15_r20/development/vndk/tools/header-checker/android/build-prebuilts.sh (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1#!/bin/bash -e
2
3# Copyright 2019 Google Inc. All rights reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17usage() {
18    echo "Usage: $(basename "$0") [build_target]..."
19    echo "    Build all targets if build_target is not specified."
20    echo "    Supported build targets:" \
21         "${VALID_SOONG_BINARIES[@]}" "${VALID_SOONG_TESTS[@]}"
22}
23
24in_array () {
25    value="$1"
26    shift
27    for i in "$@"; do
28        [ "$i" = "${value}" ] && return 0
29    done
30    return 1
31}
32
33VALID_SOONG_BINARIES=(
34    "bindgen"
35    "cxx_extractor"
36    "header-abi-linker"
37    "header-abi-dumper"
38    "header-abi-diff"
39    "ide_query_cc_analyzer"
40    "proto_metadata_plugin"
41    "protoc_extractor"
42)
43
44VALID_SOONG_TESTS=(
45    "header-checker-unittests"
46)
47
48BUILD_TARGETS=()
49
50while [ $# -gt 0 ]; do
51    case $1 in
52        -*) # Display help.
53            usage
54            exit 0
55            ;;
56        *) # Add specified build targets into BUILD_TARGETS
57            BUILD_TARGETS+=("$1")
58            ;;
59    esac
60    shift
61done
62
63set -ex
64
65source "$(dirname "$0")/envsetup.sh"
66
67if [ "$(uname)" != "Linux" ]; then
68    echo "error: Unsupported uname: $(uname)"
69    exit 1
70fi
71
72# Targets to be built
73SOONG_BINARIES=()
74SOONG_TESTS=()
75
76# Check if all specified targets are valid
77for name in "${BUILD_TARGETS[@]}"; do
78    if in_array "${name}" "${VALID_SOONG_BINARIES[@]}"; then
79        SOONG_BINARIES+=("${name}")
80    elif in_array "${name}" "${VALID_SOONG_TESTS[@]}"; then
81        SOONG_TESTS+=("${name}")
82    else
83        echo "build_target ${name} is not one of the supported targets:" \
84             "${VALID_SOONG_BINARIES[@]}" "${VALID_SOONG_TESTS[@]}"
85        exit 1
86    fi
87done
88
89if [ "${#BUILD_TARGETS[@]}" -eq 0 ]; then
90    # Build everything by default.
91    SOONG_BINARIES=("${VALID_SOONG_BINARIES[@]}")
92    SOONG_TESTS=("${VALID_SOONG_TESTS[@]}")
93fi
94
95if [ -z "${OUT_DIR}" ]; then
96    echo "error: Must set OUT_DIR"
97    exit 1
98fi
99
100TOP=$(pwd)
101
102# Setup Soong configuration
103SOONG_OUT="${OUT_DIR}/soong"
104SOONG_HOST_OUT="${OUT_DIR}/soong/host/linux-x86"
105rm -rf "${SOONG_OUT}"
106mkdir -p "${SOONG_OUT}"
107cat > "${SOONG_OUT}/soong.variables" << __EOF__
108{
109    "Allow_missing_dependencies": true,
110    "HostArch":"x86_64"
111}
112__EOF__
113
114# Allow unknown warning options since this may lag behind platform's compiler
115# version.
116export ALLOW_UNKNOWN_WARNING_OPTION=true
117
118binaries=()
119for name in "${SOONG_BINARIES[@]}"; do
120    binaries+=("${SOONG_HOST_OUT}/bin/${name}")
121done
122
123# Build binaries and shared libs
124build/soong/soong_ui.bash --make-mode --skip-config --soong-only \
125  "${binaries[@]}" "${SOONG_TESTS[@]}"
126
127# Copy binaries and shared libs
128SOONG_DIST="${SOONG_OUT}/dist"
129mkdir -p "${SOONG_DIST}/bin"
130if [ -n "${binaries}" ]; then
131    cp "${binaries[@]}" "${SOONG_DIST}/bin"
132fi
133cp -R "${SOONG_HOST_OUT}/lib64" "${SOONG_DIST}"
134# create symlink lib -> lib64 as toolchain libraries have a RUNPATH pointing to
135# $ORIGIN/../lib instead of lib64
136ln -s "lib64" "${SOONG_DIST}/lib"
137
138# Copy clang header and share files
139CLANG_DIR="prebuilts/clang/host/linux-x86/${LLVM_PREBUILTS_VERSION}"
140CLANG_LIB_DIR="${CLANG_DIR}/lib/clang/${LLVM_RELEASE_VERSION}"
141CLANG_LIB_DIR_OUT="${SOONG_DIST}/lib/clang/${LLVM_RELEASE_VERSION}"
142mkdir -p "${CLANG_LIB_DIR_OUT}"
143cp -R "${CLANG_LIB_DIR}/share" "${CLANG_LIB_DIR_OUT}/share"
144cp -R "${CLANG_LIB_DIR}/include" "${CLANG_LIB_DIR_OUT}/include"
145ln -s "lib/clang/${LLVM_RELEASE_VERSION}/include" "${SOONG_DIST}/clang-headers"
146
147# Normalize library file names.  All library file names must match their soname.
148function extract_soname () {
149    local file="$1"
150    readelf -d "${file}" | \
151        grep '(SONAME)\s*Library soname: \[.*\]$' -o | \
152        sed 's/(SONAME)\s*Library soname: \[\(.*\)\]$/\1/g'
153}
154
155for file in "${SOONG_OUT}/dist/lib"*"/"*; do
156    soname="$(extract_soname "${file}")"
157    if [ -n "${soname}" -a "$(basename "${file}")" != "${soname}" ]; then
158        mv "${file}" "$(dirname "${file}")/${soname}"
159    fi
160done
161
162# Package binaries and shared libs
163if [ -z "${DIST_DIR}" ]; then
164    echo "DIST_DIR is empty. Skip zipping binaries."
165else
166    pushd "${SOONG_OUT}/dist"
167    zip -qryX build-prebuilts.zip *
168    popd
169    mkdir -p "${DIST_DIR}" || true
170    cp "${SOONG_OUT}/dist/build-prebuilts.zip" "${DIST_DIR}/"
171fi
172