Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target platform flag to build script #1981

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ run() {
exit 0
fi

# platform mode: Only to build target platform for cross-compilation
if has_flag --platform -p; then
# Extract GOOS and GOARCH from command-line arguments
GOOS="${ARGS[1]}"
GOARCH="${ARGS[2]}"
Comment on lines +81 to +82
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure those will always be 1 and 2 args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's being used downstream, os I think we can defer to consuming script to provide all args as expected.

go_build_with_goos_goarch "$GOOS" "$GOARCH"
exit 0
fi

# Run only tests
if has_flag --test -t; then
go_test
Expand Down Expand Up @@ -146,6 +155,25 @@ go_build() {
fi
}

go_build_with_goos_goarch() {
GOOS="${1:-}"
GOARCH="${2:-}"

if [ -z "${GOOS}" ] || [ -z "${GOARCH}" ]; then
echo "❌ Missing GOOS or GOARCH. Please provide both GOOS and GOARCH as arguments."
exit 1
fi

echo "🚧 Compile for GOOS=${GOOS} GOARCH=${GOARCH}"

# Env var exported by hack/build-flags.sh
GOOS="${GOOS}" GOARCH="${GOARCH}" go build -ldflags "${KN_BUILD_LD_FLAGS:-}" -o kn ./cmd/...

if $(file kn | grep -q -i "Windows"); then
mv kn kn.exe
fi
}

go_test() {
local test_output
test_output="$(mktemp /tmp/kn-client-test-output.XXXXXX)"
Expand Down Expand Up @@ -336,6 +364,7 @@ Usage: $(basename "${BASH_SOURCE[0]}") [... options ...]
with the following options:

-f --fast Only compile (without dep update, formatting, testing, doc gen)
-p --platform Specify the target platform for cross-compilation (e.g., linux amd64, darwin amd64)"
-t --test Run tests when used with --fast or --watch
-c --codegen Runs formatting, doc gen and update without compiling/testing
-w --watch Watch for source changes and recompile in fast mode
Expand All @@ -353,12 +382,13 @@ ln -s $(basedir)/hack/build.sh /usr/local/bin/kn_build.sh
Examples:

* Update deps, format, license check,
gen docs, compile, test: ........... build.sh
* Compile only: ...................... build.sh --fast
* Run only tests: .................... build.sh --test
* Compile with tests: ................ build.sh -f -t
* Automatic recompilation: ........... build.sh --watch
* Build cross platform binaries: ..... build.sh --all
gen docs, compile, test: ........................ build.sh
* Compile only: ................................... build.sh --fast
* Build cross platform binaries for a platform: ... build.sh -p linux amd64
* Run only tests: ................................. build.sh --test
* Compile with tests: ............................. build.sh -f -t
* Automatic recompilation: ........................ build.sh --watch
* Build cross platform binaries: .................. build.sh --all
EOT
}

Expand Down
Loading