1#!/bin/bash -ex
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
17source "$(dirname "$0")/envsetup.sh"
18
19if [ -z "${OUT_DIR}" ]; then
20    echo "error: Must set OUT_DIR"
21    exit 1
22fi
23
24TOP=$(pwd)
25
26UNAME="$(uname)"
27case "${UNAME}" in
28Linux)
29    OS='linux'
30    ;;
31Darwin)
32    OS='darwin'
33    ;;
34*)
35    echo "error: Unknown uname: ${UNAME}"
36    exit 1
37    ;;
38esac
39
40# Setup Soong configuration
41SOONG_OUT="${OUT_DIR}/soong"
42SOONG_HOST_OUT="${OUT_DIR}/soong/host/${OS}-x86"
43rm -rf "${SOONG_OUT}"
44mkdir -p "${SOONG_OUT}"
45cat > "${SOONG_OUT}/soong.variables" << __EOF__
46{
47    "Allow_missing_dependencies": true,
48    "HostArch":"x86_64"
49}
50__EOF__
51
52# Targets to be built
53SOONG_BINARIES=(
54    "cxx_extractor"
55    "header-abi-linker"
56    "header-abi-dumper"
57    "header-abi-diff"
58    "merge-abi-diff"
59    "proto_metadata_plugin"
60    "protoc_extractor"
61    "versioner"
62)
63
64binaries=()
65for name in "${SOONG_BINARIES[@]}"; do
66    binaries+=("${SOONG_HOST_OUT}/bin/${name}")
67done
68
69libs=()
70if [ "${OS}" = "darwin" ]; then
71    libs+=("${SOONG_HOST_OUT}/lib64/libc++abi_host.dylib")
72fi
73
74# Build binaries and shared libs
75build/soong/soong_ui.bash --make-mode --skip-make "${binaries[@]}" "${libs[@]}"
76
77# Copy binaries and shared libs
78SOONG_DIST="${SOONG_OUT}/dist"
79mkdir -p "${SOONG_DIST}/bin"
80cp "${binaries[@]}" "${SOONG_DIST}/bin"
81cp -R "${SOONG_HOST_OUT}/lib"* "${SOONG_DIST}"
82
83# Copy clang header and share files
84CLANG_DIR="prebuilts/clang/host/${OS}-x86/${LLVM_PREBUILTS_VERSION}"
85CLANG_LIB_DIR="${CLANG_DIR}/lib64/clang/${LLVM_RELEASE_VERSION}"
86CLANG_LIB_DIR_OUT="${SOONG_DIST}/lib64/clang/${LLVM_RELEASE_VERSION}"
87mkdir -p "${CLANG_LIB_DIR_OUT}"
88cp -R "${CLANG_LIB_DIR}/share" "${CLANG_LIB_DIR_OUT}/share"
89cp -R "${CLANG_LIB_DIR}/include" "${CLANG_LIB_DIR_OUT}/include"
90ln -s "lib64/clang/${LLVM_RELEASE_VERSION}/include" "${SOONG_DIST}/clang-headers"
91
92# Normalize library file names.  All library file names must match their soname.
93function extract_soname () {
94    local file="$1"
95
96    case "${OS}" in
97    linux)
98        readelf -d "${file}" | \
99            grep '(SONAME)\s*Library soname: \[.*\]$' -o | \
100            sed 's/(SONAME)\s*Library soname: \[\(.*\)\]$/\1/g'
101        ;;
102    darwin)
103        local install_path="$(otool -D "${file}" | sed -n 2p)"
104        if [ -n "${install_path}" ]; then
105            basename "${install_path}"
106        fi
107        ;;
108    esac
109}
110
111for file in "${SOONG_OUT}/dist/lib"*"/"*; do
112    soname="$(extract_soname "${file}")"
113    if [ -n "${soname}" -a "$(basename "${file}")" != "${soname}" ]; then
114        mv "${file}" "$(dirname "${file}")/${soname}"
115    fi
116done
117
118# Package binaries and shared libs
119(
120    cd "${SOONG_OUT}/dist"
121    zip -qryX build-prebuilts.zip *
122)
123
124if [ -n "${DIST_DIR}" ]; then
125    mkdir -p "${DIST_DIR}" || true
126    cp "${SOONG_OUT}/dist/build-prebuilts.zip" "${DIST_DIR}/"
127fi
128