• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 for macOS: ${MACOS_SOONG_BINARIES[*]}"
21    echo "    Supported build targets for Linux: ${LINUX_SOONG_BINARIES[*]}"
22}
23
24valid_build_target () {
25    for i in "${VALID_SOONG_BINARIES[@]}"; do
26        if [ "$i" = "$1" ]; then
27            return 0
28        fi
29    done
30    return 1
31}
32
33SOONG_BINARIES=()
34
35while [ $# -gt 0 ]; do
36    case $1 in
37        -*) # Display help.
38            usage
39            exit 0
40            ;;
41        *) # Add specified build targets into SOONG_BINARIES
42            SOONG_BINARIES+=("$1")
43            ;;
44    esac
45    shift
46done
47
48set -ex
49
50source "$(dirname "$0")/envsetup.sh"
51
52UNAME="$(uname)"
53case "${UNAME}" in
54Linux)
55    OS='linux'
56    ;;
57Darwin)
58    OS='darwin'
59    ;;
60*)
61    echo "error: Unknown uname: ${UNAME}"
62    exit 1
63    ;;
64esac
65
66LINUX_SOONG_BINARIES=(
67    "bindgen"
68    "cxx_extractor"
69    "header-abi-linker"
70    "header-abi-dumper"
71    "header-abi-diff"
72    "proto_metadata_plugin"
73    "protoc_extractor"
74    "versioner"
75)
76
77MACOS_SOONG_BINARIES=(
78    "versioner"
79)
80
81# Targets to be built
82if [ "${OS}" = "darwin" ]; then
83    VALID_SOONG_BINARIES=("${MACOS_SOONG_BINARIES[@]}")
84else
85    VALID_SOONG_BINARIES=("${LINUX_SOONG_BINARIES[@]}")
86fi
87
88if [ "${#SOONG_BINARIES[@]}" -eq 0 ]; then
89    # SOONG_BINARIES is empty, so there must be no commandline argument, thus we
90    # build everything.
91    SOONG_BINARIES=("${VALID_SOONG_BINARIES[@]}")
92fi
93
94# Check if all specified targets are valid
95for name in "${SOONG_BINARIES[@]}"; do
96  if ! valid_build_target "${name}"; then
97    echo "build_target ${name} is not one of the supported targets: ${VALID_SOONG_BINARIES[*]}"
98    exit 1
99  fi
100done
101
102if [ -z "${OUT_DIR}" ]; then
103    echo "error: Must set OUT_DIR"
104    exit 1
105fi
106
107TOP=$(pwd)
108
109# Setup Soong configuration
110SOONG_OUT="${OUT_DIR}/soong"
111SOONG_HOST_OUT="${OUT_DIR}/soong/host/${OS}-x86"
112rm -rf "${SOONG_OUT}"
113mkdir -p "${SOONG_OUT}"
114cat > "${SOONG_OUT}/soong.variables" << __EOF__
115{
116    "Allow_missing_dependencies": true,
117    "HostArch":"x86_64"
118}
119__EOF__
120
121# Allow unknown warning options since this may lag behind platform's compiler
122# version.
123export ALLOW_UNKNOWN_WARNING_OPTION=true
124
125binaries=()
126for name in "${SOONG_BINARIES[@]}"; do
127    binaries+=("${SOONG_HOST_OUT}/bin/${name}")
128done
129
130libs=()
131if [ "${OS}" = "darwin" ]; then
132    libs+=("${SOONG_HOST_OUT}/lib64/libc++abi_host.dylib")
133fi
134
135# Build binaries and shared libs
136build/soong/soong_ui.bash --make-mode --skip-config --soong-only "${binaries[@]}" "${libs[@]}"
137
138# Copy binaries and shared libs
139SOONG_DIST="${SOONG_OUT}/dist"
140mkdir -p "${SOONG_DIST}/bin"
141cp "${binaries[@]}" "${SOONG_DIST}/bin"
142cp -R "${SOONG_HOST_OUT}/lib64" "${SOONG_DIST}"
143# create symlink lib -> lib64 as toolchain libraries have a RUNPATH pointing to
144# $ORIGIN/../lib instead of lib64
145ln -s "lib64" "${SOONG_DIST}/lib"
146
147# Copy clang header and share files
148CLANG_DIR="prebuilts/clang/host/${OS}-x86/${LLVM_PREBUILTS_VERSION}"
149CLANG_LIB_DIR="${CLANG_DIR}/lib/clang/${LLVM_RELEASE_VERSION}"
150CLANG_LIB_DIR_OUT="${SOONG_DIST}/lib/clang/${LLVM_RELEASE_VERSION}"
151mkdir -p "${CLANG_LIB_DIR_OUT}"
152cp -R "${CLANG_LIB_DIR}/share" "${CLANG_LIB_DIR_OUT}/share"
153cp -R "${CLANG_LIB_DIR}/include" "${CLANG_LIB_DIR_OUT}/include"
154ln -s "lib/clang/${LLVM_RELEASE_VERSION}/include" "${SOONG_DIST}/clang-headers"
155
156# Normalize library file names.  All library file names must match their soname.
157function extract_soname () {
158    local file="$1"
159
160    case "${OS}" in
161    linux)
162        readelf -d "${file}" | \
163            grep '(SONAME)\s*Library soname: \[.*\]$' -o | \
164            sed 's/(SONAME)\s*Library soname: \[\(.*\)\]$/\1/g'
165        ;;
166    darwin)
167        local install_path="$(otool -D "${file}" | sed -n 2p)"
168        if [ -n "${install_path}" ]; then
169            basename "${install_path}"
170        fi
171        ;;
172    esac
173}
174
175for file in "${SOONG_OUT}/dist/lib"*"/"*; do
176    soname="$(extract_soname "${file}")"
177    if [ -n "${soname}" -a "$(basename "${file}")" != "${soname}" ]; then
178        mv "${file}" "$(dirname "${file}")/${soname}"
179    fi
180done
181
182# Package binaries and shared libs
183(
184    cd "${SOONG_OUT}/dist"
185    zip -qryX build-prebuilts.zip *
186)
187
188if [ -n "${DIST_DIR}" ]; then
189    mkdir -p "${DIST_DIR}" || true
190    cp "${SOONG_OUT}/dist/build-prebuilts.zip" "${DIST_DIR}/"
191fi
192