blob: 7489fe3982a047c12a8a5d5d85a3531ec499bf58 [file] [log] [blame]
Dan Willemsen0df15172017-05-07 11:23:59 -07001# Copyright 2017 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Set of utility functions to build and run go code with microfactory
16#
17# Inputs:
18# ${TOP}: The top of the android source tree
19# ${OUT_DIR}: The output directory location (defaults to ${TOP}/out)
20# ${OUT_DIR_COMMON_BASE}: Change the default out directory to
21# ${OUT_DIR_COMMON_BASE}/$(basename ${TOP})
22
23# Ensure GOROOT is set to the in-tree version.
24case $(uname) in
25 Linux)
26 export GOROOT="${TOP}/prebuilts/go/linux-x86/"
27 ;;
28 Darwin)
29 export GOROOT="${TOP}/prebuilts/go/darwin-x86/"
30 ;;
31 *) echo "unknown OS:" $(uname) >&2 && exit 1;;
32esac
33
34# Find the output directory
35function getoutdir
36{
37 local out_dir="${OUT_DIR-}"
38 if [ -z "${out_dir}" ]; then
39 if [ "${OUT_DIR_COMMON_BASE-}" ]; then
40 out_dir="${OUT_DIR_COMMON_BASE}/$(basename ${TOP})"
41 else
42 out_dir="${TOP}/out"
43 fi
44 fi
45 echo "${out_dir}"
46}
47
48# Bootstrap microfactory from source if necessary and use it to build the
49# requested binary.
50#
51# Arguments:
52# $1: name of the requested binary
53# $2: package name
54function build_go
55{
56 # Increment when microfactory changes enough that it cannot rebuild itself.
57 # For example, if we use a new command line argument that doesn't work on older versions.
58 local mf_version=2
59
60 local mf_src="${TOP}/build/soong/cmd/microfactory"
61
62 local out_dir=$(getoutdir)
63 local mf_bin="${out_dir}/microfactory_$(uname)"
64 local mf_version_file="${out_dir}/.microfactory_$(uname)_version"
65 local built_bin="${out_dir}/$1"
66 local from_src=1
67
68 if [ -f "${mf_bin}" ] && [ -f "${mf_version_file}" ]; then
69 if [ "${mf_version}" -eq "$(cat "${mf_version_file}")" ]; then
70 from_src=0
71 fi
72 fi
73
74 local mf_cmd
75 if [ $from_src -eq 1 ]; then
76 mf_cmd="${GOROOT}/bin/go run ${mf_src}/microfactory.go"
77 else
78 mf_cmd="${mf_bin}"
79 fi
80
81 ${mf_cmd} -s "${mf_src}" -b "${mf_bin}" \
82 -pkg-path "android/soong=${TOP}/build/soong" -trimpath "${TOP}/build/soong" \
83 -o "${built_bin}" $2
84
85 if [ $from_src -eq 1 ]; then
86 echo "${mf_version}" >"${mf_version_file}"
87 fi
88}