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    "versioner"
43)
44
45VALID_SOONG_TESTS=(
46    "header-checker-unittests"
47)
48
49BUILD_TARGETS=()
50
51while [ $# -gt 0 ]; do
52    case $1 in
53        -*) # Display help.
54            usage
55            exit 0
56            ;;
57        *) # Add specified build targets into BUILD_TARGETS
58            BUILD_TARGETS+=("$1")
59            ;;
60    esac
61    shift
62done
63
64set -ex
65
66source "$(dirname "$0")/envsetup.sh"
67
68if [ "$(uname)" != "Linux" ]; then
69    echo "error: Unsupported uname: $(uname)"
70    exit 1
71fi
72
73# Targets to be built
74SOONG_BINARIES=()
75SOONG_TESTS=()
76
77# Check if all specified targets are valid
78for name in "${BUILD_TARGETS[@]}"; do
79    if in_array "${name}" "${VALID_SOONG_BINARIES[@]}"; then
80        SOONG_BINARIES+=("${name}")
81    elif in_array "${name}" "${VALID_SOONG_TESTS[@]}"; then
82        SOONG_TESTS+=("${name}")
83    else
84        echo "build_target ${name} is not one of the supported targets:" \
85             "${VALID_SOONG_BINARIES[@]}" "${VALID_SOONG_TESTS[@]}"
86        exit 1
87    fi
88done
89
90if [ "${#BUILD_TARGETS[@]}" -eq 0 ]; then
91    # Build everything by default.
92    SOONG_BINARIES=("${VALID_SOONG_BINARIES[@]}")
93    SOONG_TESTS=("${VALID_SOONG_TESTS[@]}")
94fi
95
96if [ -z "${OUT_DIR}" ]; then
97    echo "error: Must set OUT_DIR"
98    exit 1
99fi
100
101TOP=$(pwd)
102
103# Setup Soong configuration
104SOONG_OUT="${OUT_DIR}/soong"
105SOONG_HOST_OUT="${OUT_DIR}/soong/host/linux-x86"
106rm -rf "${SOONG_OUT}"
107mkdir -p "${SOONG_OUT}"
108cat > "${SOONG_OUT}/soong.variables" << __EOF__
109{
110    "Allow_missing_dependencies": true,
111    "HostArch":"x86_64"
112}
113__EOF__
114
115# Allow unknown warning options since this may lag behind platform's compiler
116# version.
117export ALLOW_UNKNOWN_WARNING_OPTION=true
118
119binaries=()
120for name in "${SOONG_BINARIES[@]}"; do
121    binaries+=("${SOONG_HOST_OUT}/bin/${name}")
122done
123
124# Build binaries and shared libs
125build/soong/soong_ui.bash --make-mode --skip-config --soong-only \
126  "${binaries[@]}" "${SOONG_TESTS[@]}"
127
128# Copy binaries and shared libs
129SOONG_DIST="${SOONG_OUT}/dist"
130mkdir -p "${SOONG_DIST}/bin"
131if [ -n "${binaries}" ]; then
132    cp "${binaries[@]}" "${SOONG_DIST}/bin"
133fi
134cp -R "${SOONG_HOST_OUT}/lib64" "${SOONG_DIST}"
135# create symlink lib -> lib64 as toolchain libraries have a RUNPATH pointing to
136# $ORIGIN/../lib instead of lib64
137ln -s "lib64" "${SOONG_DIST}/lib"
138
139# Copy clang header and share files
140CLANG_DIR="prebuilts/clang/host/linux-x86/${LLVM_PREBUILTS_VERSION}"
141CLANG_LIB_DIR="${CLANG_DIR}/lib/clang/${LLVM_RELEASE_VERSION}"
142CLANG_LIB_DIR_OUT="${SOONG_DIST}/lib/clang/${LLVM_RELEASE_VERSION}"
143mkdir -p "${CLANG_LIB_DIR_OUT}"
144cp -R "${CLANG_LIB_DIR}/share" "${CLANG_LIB_DIR_OUT}/share"
145cp -R "${CLANG_LIB_DIR}/include" "${CLANG_LIB_DIR_OUT}/include"
146ln -s "lib/clang/${LLVM_RELEASE_VERSION}/include" "${SOONG_DIST}/clang-headers"
147
148# Normalize library file names.  All library file names must match their soname.
149function extract_soname () {
150    local file="$1"
151    readelf -d "${file}" | \
152        grep '(SONAME)\s*Library soname: \[.*\]$' -o | \
153        sed 's/(SONAME)\s*Library soname: \[\(.*\)\]$/\1/g'
154}
155
156for file in "${SOONG_OUT}/dist/lib"*"/"*; do
157    soname="$(extract_soname "${file}")"
158    if [ -n "${soname}" -a "$(basename "${file}")" != "${soname}" ]; then
159        mv "${file}" "$(dirname "${file}")/${soname}"
160    fi
161done
162
163# Package binaries and shared libs
164if [ -z "${DIST_DIR}" ]; then
165    echo "DIST_DIR is empty. Skip zipping binaries."
166else
167    pushd "${SOONG_OUT}/dist"
168    zip -qryX build-prebuilts.zip *
169    popd
170    mkdir -p "${DIST_DIR}" || true
171    cp "${SOONG_OUT}/dist/build-prebuilts.zip" "${DIST_DIR}/"
172fi
173