From 1ca80aa06737323838ee3f0db3413e2a0386a2ef Mon Sep 17 00:00:00 2001 From: OpenShift Cherrypick Robot Date: Mon, 13 May 2024 18:42:28 +0200 Subject: [PATCH] [release-v1.12] Only to build target platform for cross-compilation by providing goos and goarch. (#376) * add go build with goos and goarch * add a example at help message --------- Co-authored-by: Kaustubh Pande --- Makefile | 9 +++++++-- hack/build.sh | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index d819704536..fd47b79de3 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -CGO_ENABLED=0 -GOOS=linux +CGO_ENABLED=1 +GOOS ?= +GOARCH ?= 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 TEST= TEST_IMAGE_TAG ?= latest @@ -28,6 +29,10 @@ build: ./hack/build.sh -f .PHONY: build +build-with-platform: + ./hack/build.sh -p $(GOOS) $(GOARCH) +.PHONY: build-with-platform + build-cross: ./hack/build.sh -x .PHONY: build-cross diff --git a/hack/build.sh b/hack/build.sh index 6c2bead889..56c24b76d3 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -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]}" + go_build_with_goos_goarch "$GOOS" "$GOARCH" + exit 0 + fi + # Run only tests if has_flag --test -t; then go_test @@ -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 -mod=vendor -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)" @@ -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 @@ -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 }