#! /bin/sh # Copyright 2017 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License. You may obtain a copy # of the License at: # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. set -e readonly PROGNAME="${0##*/}" readonly SRCDIR="$(cd "$(dirname "${0}")" && pwd -P)" # Settings that end up in the Makefile. CARGO=cargo CLEANFILES=target FEATURES= GOPATH="$(pwd)/.gopath" GOROOT= IS_BMAKE= IS_GNUMAKE= PREFIX= DISTCLEANFILES="Makefile go.sum ${GOPATH}" # List of variables exposed to the Makefile. MK_VARS="CARGO CLEANFILES DISTCLEANFILES FEATURES GOPATH GOROOT PREFIX" # List of variables replaced in the Makefile. MK_SUBSTS="IS_BMAKE IS_GNUMAKE" err() { echo "${PROGNAME}: E: ${@}" 1>&2 exit 1 } info() { echo "${PROGNAME}: I: ${@}" 1>&2 } find_prog() { local prog="${1}"; shift local oldifs="${IFS}" IFS=: set -- ${PATH} IFS="${oldifs}" while [ ${#} -gt 0 ]; do if [ -x "${1}/${prog}" ]; then echo "${1}/${prog}" return 0 else shift fi done return 1 } find_progs() { while [ ${#} -gt 0 ]; do if find_prog "${1}"; then return 0 else shift fi done return 1 } setup_cargo() { local user_override="${1}"; shift if [ -n "${user_override}" ]; then [ -e "${user_override}" ] || err "cargo not found in" \ "${user_override}; bogus argument to --cargo?" CARGO="${user_override}" else local cargo="$(find_progs cargo)" [ -n "${cargo}" ] || err "Cannot find cargo in path; pass" \ "--cargo=/path/to/cargo to configure" CARGO="${cargo}" fi info "Using Cargo from: ${CARGO}" } # Installs git hooks into the git directory provided in git_dir. setup_git() { local git_dir="${1}"; shift cd "${git_dir}/hooks" for hook in ../../admin/pre-commit; do info "Installing git hook ${hook##*/}" ln -s -f "${hook}" . done cd - >/dev/null 2>&1 } setup_go() { local user_override="${1}"; shift if [ -n "${user_override}" ]; then [ "${user_override}" = none ] || GOROOT="${user_override}" else local go="$(find_progs go)" [ -n "${go}" ] && GOROOT="$(dirname "$(dirname "${go}")")" fi if [ -z "${GOROOT}" ]; then info "Go not found; cannot run integration tests" else info "Using Go with GOROOT: ${GOROOT}" GOPATH="${GOPATH}" "${GOROOT}/bin/go" mod download GOPATH="${GOPATH}" "${GOROOT}/bin/go" install golang.org/x/lint/golint fi } setup_make() { IS_BMAKE=# IS_GNUMAKE=# if make --version >/dev/null 2>&1; then info "make is GNU Make" IS_GNUMAKE= else info "make is bmake" IS_BMAKE= fi } setup_prefix() { local prefix="${1:-/usr/local}"; shift info "Installation prefix is ${prefix}" PREFIX="${prefix}" } setup_vscode() { { echo '// AUTOMATICALLY GENERATED!!!' echo '// EDIT settings.json.in INSTEAD' sed \ -e "s,__GOPATH__,${GOPATH},g" \ -e "s,__GOROOT__,${GOROOT},g" \ -e "s,__TOOLS_GOPATH__,$(pwd)/.gopath-tools,g" \ .vscode/settings.json.in } >.vscode/settings.json } generate_makefile() { local src="${1}"; shift local dest="${1}"; shift info "Generating ${dest}" echo "# AUTOMATICALLY GENERATED; DO NOT EDIT!" >"${dest}.tmp" for var in ${MK_VARS}; do local value eval "value=\"\$${var}\"" echo "${var} = ${value}" >>"${dest}.tmp" done local substs= for var in ${MK_SUBSTS}; do local value eval "value=\"\$${var}\"" substs="${substs} -e s,@${var}@,${value},g" done sed ${substs} "${src}" >>"${dest}.tmp" mv "${dest}.tmp" "${dest}" } main() { cd "${SRCDIR}" local cargo= local goroot= local prefix= for arg in "${@}"; do case "${arg}" in --cargo=*) cargo="${arg#*=}" ;; --features=*) FEATURES="${arg#*=}" ;; --goroot=*) goroot="${arg#*=}" ;; --prefix=*) prefix="${arg#*=}" ;; *) err "Unknown argument ${arg}" ;; esac done setup_cargo "${cargo}" [ -d .git ] && setup_git .git setup_go "${goroot}" setup_make setup_prefix "${prefix}" setup_vscode generate_makefile Makefile.in Makefile } main "${@}"