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    "proto_metadata_plugin"
59    "protoc_extractor"
60    "versioner"
61)
62
63binaries=()
64for name in "${SOONG_BINARIES[@]}"; do
65    binaries+=("${SOONG_HOST_OUT}/bin/${name}")
66done
67
68libs=()
69if [ "${OS}" = "darwin" ]; then
70    libs+=("${SOONG_HOST_OUT}/lib64/libc++abi_host.dylib")
71fi
72
73# Build binaries and shared libs
74build/soong/soong_ui.bash --make-mode --skip-make "${binaries[@]}" "${libs[@]}"
75
76# Copy binaries and shared libs
77SOONG_DIST="${SOONG_OUT}/dist"
78mkdir -p "${SOONG_DIST}/bin"
79cp "${binaries[@]}" "${SOONG_DIST}/bin"
80cp -R "${SOONG_HOST_OUT}/lib"* "${SOONG_DIST}"
81
82# Copy clang header and share files
83CLANG_DIR="prebuilts/clang/host/${OS}-x86/${LLVM_PREBUILTS_VERSION}"
84CLANG_LIB_DIR="${CLANG_DIR}/lib64/clang/${LLVM_RELEASE_VERSION}"
85CLANG_LIB_DIR_OUT="${SOONG_DIST}/lib64/clang/${LLVM_RELEASE_VERSION}"
86mkdir -p "${CLANG_LIB_DIR_OUT}"
87cp -R "${CLANG_LIB_DIR}/share" "${CLANG_LIB_DIR_OUT}/share"
88cp -R "${CLANG_LIB_DIR}/include" "${CLANG_LIB_DIR_OUT}/include"
89ln -s "lib64/clang/${LLVM_RELEASE_VERSION}/include" "${SOONG_DIST}/clang-headers"
90
91# Normalize library file names.  All library file names must match their soname.
92function extract_soname () {
93    local file="$1"
94
95    case "${OS}" in
96    linux)
97        readelf -d "${file}" | \
98            grep '(SONAME)\s*Library soname: \[.*\]$' -o | \
99            sed 's/(SONAME)\s*Library soname: \[\(.*\)\]$/\1/g'
100        ;;
101    darwin)
102        local install_path="$(otool -D "${file}" | sed -n 2p)"
103        if [ -n "${install_path}" ]; then
104            basename "${install_path}"
105        fi
106        ;;
107    esac
108}
109
110for file in "${SOONG_OUT}/dist/lib"*"/"*; do
111    soname="$(extract_soname "${file}")"
112    if [ -n "${soname}" -a "$(basename "${file}")" != "${soname}" ]; then
113        mv "${file}" "$(dirname "${file}")/${soname}"
114    fi
115done
116
117# Package binaries and shared libs
118(
119    cd "${SOONG_OUT}/dist"
120    zip -qryX build-prebuilts.zip *
121)
122
123if [ -n "${DIST_DIR}" ]; then
124    mkdir -p "${DIST_DIR}" || true
125    cp "${SOONG_OUT}/dist/build-prebuilts.zip" "${DIST_DIR}/"
126fi
127