blob: 724d9c54e88953b814b721450a33cbbe50c7f2d9 [file] [log] [blame]
Dan Willemsen0043c0e2016-09-18 20:27:41 -07001#!/bin/bash -eu
2#
3# Copyright 2017 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
17# To track how long we took to startup. %N isn't supported on Darwin, but
18# that's detected in the Go code, and skip calculating the startup time.
19export TRACE_BEGIN_SOONG=$(date +%s%N)
20
21# Function to find top of the source tree (if $TOP isn't set) by walking up the
22# tree.
23function gettop
24{
25 local TOPFILE=build/soong/root.bp
26 if [ -z "${TOP-}" -a -f "${TOP-}/${TOPFILE}" ] ; then
27 # The following circumlocution ensures we remove symlinks from TOP.
28 (cd $TOP; PWD= /bin/pwd)
29 else
30 if [ -f $TOPFILE ] ; then
31 # The following circumlocution (repeated below as well) ensures
32 # that we record the true directory name and not one that is
33 # faked up with symlink names.
34 PWD= /bin/pwd
35 else
36 local HERE=$PWD
37 T=
38 while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
39 \cd ..
40 T=`PWD= /bin/pwd -P`
41 done
42 \cd $HERE
43 if [ -f "$T/$TOPFILE" ]; then
44 echo $T
45 fi
46 fi
47 fi
48}
49
50# Bootstrap microfactory from source if necessary and use it to build the
51# soong_ui binary, then run soong_ui.
52function run_go
53{
54 # Increment when microfactory changes enough that it cannot rebuild itself.
55 # For example, if we use a new command line argument that doesn't work on older versions.
56 local mf_version=1
57
58 local mf_src="${TOP}/build/soong/cmd/microfactory"
59
60 local out_dir="${OUT_DIR:-${TOP}/out}"
61 local mf_bin="${out_dir}/microfactory_$(uname)"
62 local mf_version_file="${out_dir}/.microfactory_$(uname)_version"
63 local soong_ui_bin="${out_dir}/soong_ui"
64 local from_src=1
65
66 if [ -f "${mf_bin}" ] && [ -f "${mf_version_file}" ]; then
67 if [ "${mf_version}" -eq "$(cat "${mf_version_file}")" ]; then
68 from_src=0
69 fi
70 fi
71
72 local mf_cmd
73 if [ $from_src -eq 1 ]; then
74 mf_cmd="${GOROOT}/bin/go run ${mf_src}/microfactory.go"
75 else
76 mf_cmd="${mf_bin}"
77 fi
78
79 ${mf_cmd} -s "${mf_src}" -b "${mf_bin}" \
80 -pkg-path "android/soong=${TOP}/build/soong" -trimpath "${TOP}/build/soong" \
81 -o "${soong_ui_bin}" android/soong/cmd/soong_ui
82
83 if [ $from_src -eq 1 ]; then
84 echo "${mf_version}" >"${mf_version_file}"
85 fi
86
87 exec "${out_dir}/soong_ui" "$@"
88}
89
90export TOP=$(gettop)
91case $(uname) in
92 Linux)
93 export GOROOT="${TOP}/prebuilts/go/linux-x86/"
94 ;;
95 Darwin)
96 export GOROOT="${TOP}/prebuilts/go/darwin-x86/"
97 ;;
98 *) echo "unknown OS:" $(uname) >&2 && exit 1;;
99esac
100
101run_go "$@"