@@ -75,6 +75,15 @@ run() {
7575 exit 0
7676 fi
7777
78+ # platform mode: Only to build target platform for cross-compilation
79+ if $( has_flag --platform -p) ; then
80+ # Extract GOOS and GOARCH from command-line arguments
81+ GOOS=" ${ARGS[1]} "
82+ GOARCH=" ${ARGS[2]} "
83+ go_build_with_goos_goarch " $GOOS " " $GOARCH "
84+ exit 0
85+ fi
86+
7887 # Run only tests
7988 if has_flag --test -t; then
8089 go_test
@@ -146,6 +155,25 @@ go_build() {
146155 fi
147156}
148157
158+ go_build_with_goos_goarch () {
159+ GOOS=" ${1} "
160+ GOARCH=" ${2} "
161+
162+ if [ -z " ${GOOS} " ] || [ -z " ${GOARCH} " ]; then
163+ echo " ❌ Missing GOOS or GOARCH. Please provide both GOOS and GOARCH as arguments."
164+ exit 1
165+ fi
166+
167+ echo " 🚧 Compile for GOOS=${GOOS} GOARCH=${GOARCH} "
168+
169+ # Env var exported by hack/build-flags.sh
170+ GOOS=" ${GOOS} " GOARCH=" ${GOARCH} " go build -mod=vendor -ldflags " ${KN_BUILD_LD_FLAGS:- } " -o kn ./cmd/...
171+
172+ if $( file kn | grep -q -i " Windows" ) ; then
173+ mv kn kn.exe
174+ fi
175+ }
176+
149177go_test () {
150178 local test_output
151179 test_output=" $( mktemp /tmp/kn-client-test-output.XXXXXX) "
@@ -336,6 +364,7 @@ Usage: $(basename "${BASH_SOURCE[0]}") [... options ...]
336364with the following options:
337365
338366-f --fast Only compile (without dep update, formatting, testing, doc gen)
367+ -p --platform Specify the target platform for cross-compilation (e.g., linux amd64, darwin amd64)"
339368-t --test Run tests when used with --fast or --watch
340369-c --codegen Runs formatting, doc gen and update without compiling/testing
341370-w --watch Watch for source changes and recompile in fast mode
@@ -353,12 +382,13 @@ ln -s $(basedir)/hack/build.sh /usr/local/bin/kn_build.sh
353382Examples:
354383
355384* Update deps, format, license check,
356- gen docs, compile, test: ........... build.sh
357- * Compile only: ...................... build.sh --fast
358- * Run only tests: .................... build.sh --test
359- * Compile with tests: ................ build.sh -f -t
360- * Automatic recompilation: ........... build.sh --watch
361- * Build cross platform binaries: ..... build.sh --all
385+ gen docs, compile, test: ........................ build.sh
386+ * Compile only: ................................... build.sh --fast
387+ * Build cross platform binaries for a platform: ... build.sh -p linux amd64
388+ * Run only tests: ................................. build.sh --test
389+ * Compile with tests: ............................. build.sh -f -t
390+ * Automatic recompilation: ........................ build.sh --watch
391+ * Build cross platform binaries: .................. build.sh --all
362392EOT
363393}
364394
0 commit comments