Skip to content

Commit

Permalink
[release-v1.12] Only to build target platform for cross-compilation b…
Browse files Browse the repository at this point in the history
…y providing goos and goarch. (#376)

* add go build with goos and goarch

* add a example at help message

---------

Co-authored-by: Kaustubh Pande <kaustubhpande.kp@gmail.com>
  • Loading branch information
openshift-cherrypick-robot and Kaustubh-pande committed May 13, 2024
1 parent b863d32 commit 1ca80aa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
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]}"
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 -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)"
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

0 comments on commit 1ca80aa

Please sign in to comment.