1# 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# ${GOROOT} 19# ${BUILDDIR} 20# ${BLUEPRINTDIR} 21# ${SRCDIR} 22 23# Bootstrap microfactory from source if necessary and use it to build the 24# requested binary. 25# 26# Arguments: 27# $1: name of the requested binary 28# $2: package name 29# ${EXTRA_ARGS}: extra arguments to pass to microfactory (-pkg-path, etc) 30function build_go 31{ 32 # Increment when microfactory changes enough that it cannot rebuild itself. 33 # For example, if we use a new command line argument that doesn't work on older versions. 34 local mf_version=3 35 36 local mf_src="${BLUEPRINTDIR}/microfactory" 37 local mf_bin="${BUILDDIR}/microfactory_$(uname)" 38 local mf_version_file="${BUILDDIR}/.microfactory_$(uname)_version" 39 local built_bin="${BUILDDIR}/$1" 40 local from_src=1 41 42 if [ -f "${mf_bin}" ] && [ -f "${mf_version_file}" ]; then 43 if [ "${mf_version}" -eq "$(cat "${mf_version_file}")" ]; then 44 from_src=0 45 fi 46 fi 47 48 local mf_cmd 49 if [ $from_src -eq 1 ]; then 50 # `go run` requires a single main package, so create one 51 local gen_src_dir="${BUILDDIR}/.microfactory_$(uname)_intermediates/src" 52 mkdir -p "${gen_src_dir}" 53 sed "s/^package microfactory/package main/" "${mf_src}/microfactory.go" >"${gen_src_dir}/microfactory.go" 54 55 mf_cmd="${GOROOT}/bin/go run ${gen_src_dir}/microfactory.go" 56 else 57 mf_cmd="${mf_bin}" 58 fi 59 60 rm -f "${BUILDDIR}/.$1.trace" 61 # GOROOT must be absolute because `go run` changes the local directory 62 GOROOT=$(cd $GOROOT; pwd) ${mf_cmd} -b "${mf_bin}" \ 63 -pkg-path "github.com/google/blueprint=${BLUEPRINTDIR}" \ 64 -trimpath "${SRCDIR}" \ 65 ${EXTRA_ARGS} \ 66 -o "${built_bin}" $2 67 68 if [ $? -eq 0 ] && [ $from_src -eq 1 ]; then 69 echo "${mf_version}" >"${mf_version_file}" 70 fi 71} 72