@@ -20,7 +20,7 @@ set -o pipefail
2020set -o errexit
2121set -o nounset
2222
23- source_dirs=" cmd pkg test tools"
23+ source_dirs=" cmd pkg test lib tools"
2424
2525# Store for later
2626if [ -z " ${1-} " ]; then
@@ -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
@@ -139,13 +148,32 @@ source_lint() {
139148go_build () {
140149 echo " 🚧 Compile"
141150 # Env var exported by hack/build-flags.sh
142- go build -ldflags " ${KN_BUILD_LD_FLAGS:- } " -o kn ./cmd/...
151+ go build -mod=vendor - ldflags " ${KN_BUILD_LD_FLAGS:- } " -o kn ./cmd/...
143152
144153 if file kn | grep -q -i " Windows" ; then
145154 mv kn kn.exe
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) "
@@ -179,7 +207,7 @@ check_license() {
179207 local check_output
180208 check_output=" $( mktemp /tmp/kn-client-licence-check.XXXXXX) "
181209 for ext in " ${extensions_to_check[@]} " ; do
182- find . -name " *.$ext " -a \! -path " ./.*" -a \! -path " ./third_party/*" -print0 |
210+ find . -name " *.$ext " -a \! -path " ./vendor/* " -a \! -path " ./ .*" -a \! -path " ./third_party/*" -print0 |
183211 while IFS= read -r -d ' ' path; do
184212 for rword in " ${required_keywords[@]} " ; do
185213 if ! grep -q " $rword " " $path " ; then
@@ -287,19 +315,19 @@ cross_build() {
287315
288316 export CGO_ENABLED=0
289317 echo " 🐧 kn-linux-amd64"
290- GOOS=linux GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./kn-linux-amd64 ./cmd/... || failed=1
318+ GOOS=linux GOARCH=amd64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-linux-amd64 ./cmd/... || failed=1
291319 echo " 💪 kn-linux-arm64"
292- GOOS=linux GOARCH=arm64 go build -ldflags " ${ld_flags} " -o ./kn-linux-arm64 ./cmd/... || failed=1
320+ GOOS=linux GOARCH=arm64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-linux-arm64 ./cmd/... || failed=1
293321 echo " 🍏 kn-darwin-amd64"
294- GOOS=darwin GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./kn-darwin-amd64 ./cmd/... || failed=1
322+ GOOS=darwin GOARCH=amd64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-darwin-amd64 ./cmd/... || failed=1
295323 echo " 🍎 kn-darwin-arm64"
296- GOOS=darwin GOARCH=arm64 go build -ldflags " ${ld_flags} " -o ./kn-darwin-arm64 ./cmd/... || failed=1
324+ GOOS=darwin GOARCH=arm64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-darwin-arm64 ./cmd/... || failed=1
297325 echo " 🎠 kn-windows-amd64.exe"
298- GOOS=windows GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./kn-windows-amd64.exe ./cmd/... || failed=1
326+ GOOS=windows GOARCH=amd64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-windows-amd64.exe ./cmd/... || failed=1
299327 echo " Z kn-linux-s390x"
300- GOOS=linux GOARCH=s390x go build -ldflags " ${ld_flags} " -o ./kn-linux-s390x ./cmd/... || failed=1
328+ GOOS=linux GOARCH=s390x go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-linux-s390x ./cmd/... || failed=1
301329 echo " P kn-linux-ppc64le"
302- GOOS=linux GOARCH=ppc64le go build -ldflags " ${ld_flags} " -o ./kn-linux-ppc64le ./cmd/... || failed=1
330+ GOOS=linux GOARCH=ppc64le go build -mod=vendor - ldflags " ${ld_flags} " -o ./kn-linux-ppc64le ./cmd/... || failed=1
303331
304332 return ${failed}
305333}
@@ -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
@@ -368,8 +398,7 @@ if has_flag --debug; then
368398fi
369399
370400# Shared funcs from hack repo
371- # shellcheck disable=SC1090
372- source " $( go run knative.dev/hack/cmd/script library.sh) "
401+ source " $( basedir) " /vendor/knative.dev/hack/library.sh
373402
374403# Shared funcs with CI
375404while IFS= read -r -d ' ' file; do
0 commit comments