Skip to content

Commit

Permalink
Merge pull request #5350 from habitat-sh/fnichol/plan-build-target-se…
Browse files Browse the repository at this point in the history
…tting-and-injection

[plan-build] Set `$pkg_target` at build time for build programs.
  • Loading branch information
fnichol committed Jul 18, 2018
2 parents ef5d8c3 + 7e4aebf commit 0e0157c
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 23 deletions.
9 changes: 8 additions & 1 deletion components/hab/plan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function Invoke-Prepare {
}

$env:SSL_CERT_FILE = "$(Get-HabPackagePath "cacerts")/ssl/certs/cacert.pem"
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
$env:LIB += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:INCLUDE += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/include"
$env:SODIUM_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
Expand All @@ -26,6 +25,14 @@ function Invoke-Prepare {
$env:OPENSSL_LIBS = 'ssleay32:libeay32'
$env:OPENSSL_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:OPENSSL_INCLUDE_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/include"

# Used by the `build.rs` program to set the version of the binaries
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
Write-BuildLine "Setting env:PLAN_VERSION=$env:PLAN_VERSION"

# Used to set the active package target for the binaries at build time
$env:PLAN_PACKAGE_TARGET = "$pkg_target"
Write-BuildLine "Setting env:PLAN_PACKAGE_TARGET=$env:PLAN_PACKAGE_TARGET"
}

function Invoke-Unpack {
Expand Down
4 changes: 4 additions & 0 deletions components/hab/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down
9 changes: 8 additions & 1 deletion components/pkg-export-docker/plan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function Invoke-Prepare {
}

$env:SSL_CERT_FILE = "$(Get-HabPackagePath "cacerts")/ssl/certs/cacert.pem"
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
$env:LIB += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:INCLUDE += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/include"
$env:SODIUM_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
Expand All @@ -27,6 +26,14 @@ function Invoke-Prepare {
$env:OPENSSL_LIBS = 'ssleay32:libeay32'
$env:OPENSSL_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:OPENSSL_INCLUDE_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/include"

# Used by the `build.rs` program to set the version of the binaries
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
Write-BuildLine "Setting env:PLAN_VERSION=$env:PLAN_VERSION"

# Used to set the active package target for the binaries at build time
$env:PLAN_PACKAGE_TARGET = "$pkg_target"
Write-BuildLine "Setting env:PLAN_PACKAGE_TARGET=$env:PLAN_PACKAGE_TARGET"
}

function Invoke-Unpack {
Expand Down
4 changes: 4 additions & 0 deletions components/pkg-export-docker/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down
4 changes: 4 additions & 0 deletions components/pkg-export-helm/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down
4 changes: 4 additions & 0 deletions components/pkg-export-kubernetes/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down
9 changes: 8 additions & 1 deletion components/pkg-export-tar/plan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function Invoke-Prepare {
}

$env:SSL_CERT_FILE = "$(Get-HabPackagePath "cacerts")/ssl/certs/cacert.pem"
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
$env:LIB += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:INCLUDE += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/include"
$env:SODIUM_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
Expand All @@ -26,6 +25,14 @@ function Invoke-Prepare {
$env:OPENSSL_LIBS = 'ssleay32:libeay32'
$env:OPENSSL_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:OPENSSL_INCLUDE_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/include"

# Used by the `build.rs` program to set the version of the binaries
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
Write-BuildLine "Setting env:PLAN_VERSION=$env:PLAN_VERSION"

# Used to set the active package target for the binaries at build time
$env:PLAN_PACKAGE_TARGET = "$pkg_target"
Write-BuildLine "Setting env:PLAN_PACKAGE_TARGET=$env:PLAN_PACKAGE_TARGET"
}

function Invoke-Unpack {
Expand Down
4 changes: 4 additions & 0 deletions components/pkg-export-tar/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down
27 changes: 20 additions & 7 deletions components/plan-build-ps1/bin/hab-plan-build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ if (!(Test-Path Env:\HAB_BLDR_CHANNEL)) {
$script:FALLBACK_CHANNEL = "stable"
# The value of `$env:Path` on initial start of this program
$script:INITIAL_PATH = "$env:Path"
# The target architecture this plan will be built for
$script:pkg_arch = "x86_64"
# The target system (i.e. operating system variant) this plan will be built for
$script:pkg_sys = "windows"
# The full target tuple this plan will be built for
$script:pkg_target = "${pkg_arch}-${pkg_sys}"
$script:pkg_target = "@@pkg_target@@"
# The package's origin (i.e. acme)
$script:pkg_origin = ""
# The package's name (i.e. myapp)
Expand Down Expand Up @@ -1837,8 +1833,6 @@ $pkg_description
* __Maintainer__: $pkg_maintainer
* __Version__: $pkg_version
* __Release__: $pkg_release
* __Architecture__: $pkg_arch
* __System__: $pkg_sys
* __Target__: $pkg_target
* __Upstream URL__: $upstream_url_string
* __License__: $($pkg_license -join ' ')
Expand Down Expand Up @@ -2051,6 +2045,25 @@ function Invoke-DefaultEnd {

# # Main Flow

# If the value of `$pkg_target` is a replacement token, then the program is
# being run out of a raw source tree (which must be supported), otherwise
# `$pkg_target` would have a static value set when the Habitat package for this
# program was built.
if ($pkg_target -eq "@@pkg_target@@") {
if ($env:BUILD_PKG_TARGET) {
# If a build environment variable is set with the desired package target,
# then update the value of `$pkg_target`. This case is used in
# bootstrapping the Habitat packaging system.
$script:pkg_target = "$env:BUILD_PKG_TARGET"
Write-BuildLine "Setting pkg_target='$pkg_target' from `$env:BUILD_PKG_TARGET"
} else {
# Otherwise, set a suitable value for `$pkg_target`. This is prior behavior
# and is backwards compatible and behavior-preserving.
$script:pkg_target = "x86_64-windows"
Write-BuildLine "Setting pkg_target='$pkg_target' as fallback default"
}
}

# Expand the context path to an absolute path
if (-Not (Test-Path "$Context")) {
_Exit-With "Context must be an existing directory" 10
Expand Down
13 changes: 10 additions & 3 deletions components/plan-build-ps1/plan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ $bin = "hab-plan-build.ps1"

function Invoke-Build {
# Embed the release version of the program.
(Get-Content "$PLAN_CONTEXT\bin\${bin}" -Encoding Ascii) -replace
"@VERSION@", "$pkg_version/$pkg_release" |
Out-File "$bin" -Encoding ascii
(Get-Content "$PLAN_CONTEXT\bin\${bin}" -Encoding Ascii) | ForEach-Object {
$_.replace(
"@VERSION@",
"$pkg_version/$pkg_release"
).
replace(
"`$script:pkg_target = `"@@pkg_target@@`"",
"`$script:pkg_target = `"$pkg_target`""
)
} | Out-File "$bin" -Encoding ascii
}

function Invoke-Install {
Expand Down
32 changes: 25 additions & 7 deletions components/plan-build/bin/hab-plan-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,8 @@ INITIAL_PATH="$PATH"
INITIAL_PWD="$(pwd)"
# The compression level to use when compression harts (0..9)
: "${HAB_HART_COMPRESSION_LEVEL:=6}"
# The target architecture this plan will be built for
pkg_arch=$(uname -m | tr '[:upper:]' '[:lower:]')
# The target system (i.e. operating system variant) this plan will be built for
pkg_sys=$(uname -s | tr '[:upper:]' '[:lower:]')
# The full target tuple this plan will be built for
pkg_target="${pkg_arch}-${pkg_sys}"
pkg_target='@@pkg_target@@'
# The package's origin (i.e. acme)
pkg_origin=""
# Each release is a timestamp - `YYYYMMDDhhmmss`
Expand Down Expand Up @@ -2133,8 +2129,6 @@ $pkg_description
* __Maintainer__: $pkg_maintainer
* __Version__: $pkg_version
* __Release__: $pkg_release
* __Architecture__: $pkg_arch
* __System__: $pkg_sys
* __Target__: $pkg_target
* __Upstream URL__: $_upstream_url_string
* __License__: $(printf "%s " "${pkg_license[@]}")
Expand Down Expand Up @@ -2309,6 +2303,30 @@ while getopts "u:" opt; do
esac
done

# If the value of `$pkg_target` is a replacement token, then the program is
# being run out of a raw source tree (which must be supported), otherwise
# `$pkg_target` would have a static value set when the Habitat package for this
# program was built.
if [[ "$pkg_target" == "@@pkg_target@@" ]]; then
if [[ -n "${BUILD_PKG_TARGET:-}" ]]; then
# If a build environment variable is set with the desired package target,
# then update the value of `$pkg_target`. This case is used in
# bootstrapping the Habitat packaging system.
pkg_target="$BUILD_PKG_TARGET"
unset BUILD_PKG_TARGET
build_line "Setting pkg_target='$pkg_target' from \$BUILD_PKG_TARGET"
else
# Otherwise, attempt to detect a suitable value for `$pkg_target` by using
# the `uname` program. This is prior behavior and is backwards compatible
# and behavior-preserving.
_pkg_arch="$(uname -m | tr '[:upper:]' '[:lower:]')"
_pkg_sys="$(uname -s | tr '[:upper:]' '[:lower:]')"
pkg_target="${_pkg_arch}-${_pkg_sys}"
unset _pkg_arch _pkg_sys
build_line "Setting pkg_target='$pkg_target' using \`uname' detection"
fi
fi

# Expand the context path to an absolute path
PLAN_CONTEXT="$(abspath "$PLAN_CONTEXT")"
# Set the initial source root to be the same as the Plan context directory.
Expand Down
5 changes: 3 additions & 2 deletions components/plan-build/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ do_build() {
# release version of the program.
# shellcheck disable=2154
sed \
-e "s,#!/bin/bash$,#!$(pkg_path_for bash)/bin/bash," \
-e "s,^HAB_PLAN_BUILD=.*$,HAB_PLAN_BUILD=$pkg_version/$pkg_release," \
-e "s,#!/bin/bash\$,#!$(pkg_path_for bash)/bin/bash," \
-e "s,^HAB_PLAN_BUILD=0\.0\.1\$,HAB_PLAN_BUILD=$pkg_version/$pkg_release," \
-e "s,^pkg_target='@@pkg_target@@'\$,pkg_target='$pkg_target'," \
-i "$CACHE_PATH/$program"
}

Expand Down
9 changes: 8 additions & 1 deletion components/sup/plan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function Invoke-Prepare {
}

$env:SSL_CERT_FILE = "$(Get-HabPackagePath "cacerts")/ssl/certs/cacert.pem"
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
$env:LIB += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:INCLUDE += ";$HAB_CACHE_SRC_PATH/$pkg_dirname/include"
$env:SODIUM_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
Expand All @@ -28,6 +27,14 @@ function Invoke-Prepare {
$env:OPENSSL_LIB_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/lib"
$env:OPENSSL_INCLUDE_DIR = "$HAB_CACHE_SRC_PATH/$pkg_dirname/include"
$env:LIBZMQ_PREFIX = "$HAB_CACHE_SRC_PATH/$pkg_dirname"

# Used by the `build.rs` program to set the version of the binaries
$env:PLAN_VERSION = "$pkg_version/$pkg_release"
Write-BuildLine "Setting env:PLAN_VERSION=$env:PLAN_VERSION"

# Used to set the active package target for the binaries at build time
$env:PLAN_PACKAGE_TARGET = "$pkg_target"
Write-BuildLine "Setting env:PLAN_PACKAGE_TARGET=$env:PLAN_PACKAGE_TARGET"
}

function Invoke-Unpack {
Expand Down
4 changes: 4 additions & 0 deletions components/sup/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ _common_prepare() {
export PLAN_VERSION="${pkg_version}/${pkg_release}"
build_line "Setting PLAN_VERSION=$PLAN_VERSION"

# Used to set the active package target for the binaries at build time
export PLAN_PACKAGE_TARGET="$pkg_target"
build_line "Setting PLAN_PACKAGE_TARGET=$PLAN_PACKAGE_TARGET"

if [ -z "$HAB_CARGO_TARGET_DIR" ]; then
# Used by Cargo to use a pristine, isolated directory for all compilation
export CARGO_TARGET_DIR="$HAB_CACHE_SRC_PATH/$pkg_dirname"
Expand Down

0 comments on commit 0e0157c

Please sign in to comment.