Skip to content

Commit c6cee35

Browse files
[release-v1.15] Only to build target platform for cross-compilation by providing goos and goarch. (#535)
* add go build with goos and goarch * add a example at help message --------- Co-authored-by: Kaustubh Pande <[email protected]>
1 parent 81c5c9d commit c6cee35

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
CGO_ENABLED=0
18-
GOOS=linux
17+
CGO_ENABLED=1
18+
GOOS ?=
19+
GOARCH ?=
1920
TEST_IMAGES=./test/test_images/helloworld ./vendor/knative.dev/serving/test/test_images/grpc-ping ./vendor/knative.dev/serving/test/test_images/multicontainer/servingcontainer ./vendor/knative.dev/serving/test/test_images/multicontainer/sidecarcontainer
2021
TEST=
2122
TEST_IMAGE_TAG ?= latest
@@ -28,6 +29,10 @@ build:
2829
./hack/build.sh -f
2930
.PHONY: build
3031

32+
build-with-platform:
33+
./hack/build.sh -p $(GOOS) $(GOARCH)
34+
.PHONY: build-with-platform
35+
3136
build-cross:
3237
./hack/build.sh -x
3338
.PHONY: build-cross

hack/build.sh

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
149177
go_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 ...]
336364
with 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
353382
Examples:
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
362392
EOT
363393
}
364394

0 commit comments

Comments
 (0)