Home | History | Annotate | Download | only in soong
      1 #!/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.
     19 export 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.
     23 function 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.
     52 function 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=2
     57 
     58     local mf_src="${TOP}/build/soong/cmd/microfactory"
     59 
     60     local out_dir="${OUT_DIR-}"
     61     if [ -z "${out_dir}" ]; then
     62         if [ "${OUT_DIR_COMMON_BASE-}" ]; then
     63             out_dir="${OUT_DIR_COMMON_BASE}/$(basename ${TOP})"
     64         else
     65             out_dir="${TOP}/out"
     66         fi
     67     fi
     68 
     69     local mf_bin="${out_dir}/microfactory_$(uname)"
     70     local mf_version_file="${out_dir}/.microfactory_$(uname)_version"
     71     local soong_ui_bin="${out_dir}/soong_ui"
     72     local from_src=1
     73 
     74     if [ -f "${mf_bin}" ] && [ -f "${mf_version_file}" ]; then
     75         if [ "${mf_version}" -eq "$(cat "${mf_version_file}")" ]; then
     76             from_src=0
     77         fi
     78     fi
     79 
     80     local mf_cmd
     81     if [ $from_src -eq 1 ]; then
     82         mf_cmd="${GOROOT}/bin/go run ${mf_src}/microfactory.go"
     83     else
     84         mf_cmd="${mf_bin}"
     85     fi
     86 
     87     ${mf_cmd} -s "${mf_src}" -b "${mf_bin}" \
     88             -pkg-path "android/soong=${TOP}/build/soong" -trimpath "${TOP}/build/soong" \
     89             -o "${soong_ui_bin}" android/soong/cmd/soong_ui
     90 
     91     if [ $from_src -eq 1 ]; then
     92         echo "${mf_version}" >"${mf_version_file}"
     93     fi
     94 
     95     exec "${out_dir}/soong_ui" "$@"
     96 }
     97 
     98 export TOP=$(gettop)
     99 case $(uname) in
    100     Linux)
    101         export GOROOT="${TOP}/prebuilts/go/linux-x86/"
    102         ;;
    103     Darwin)
    104         export GOROOT="${TOP}/prebuilts/go/darwin-x86/"
    105         ;;
    106     *) echo "unknown OS:" $(uname) >&2 && exit 1;;
    107 esac
    108 
    109 run_go "$@"
    110