Skip to content

Commit

Permalink
feat: implement multiple solutions for building
Browse files Browse the repository at this point in the history
Signed-off-by: Xinwei Xiong(cubxxw) <[email protected]>
  • Loading branch information
cubxxw committed May 6, 2023
1 parent c0fc0c4 commit aea8446
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
16 changes: 16 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/usr/bin/env bash

# Copyright © 2022 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# ==============================================================================

## Build script

# Build helper functions. These functions help to set up, save, and load the following variables:
Expand Down
2 changes: 0 additions & 2 deletions scripts/make-rules/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ endif

# platforms: The OS must be linux when building docker images
PLATFORMS ?= linux_amd64 linux_arm64
# TODO(sealer?): The OS can be linux/windows/darwin when building binaries? if only support linux
# PLATFORMS ?= darwin_amd64 windows_amd64 linux_amd64 linux_arm64

# only support linux
GOOS=linux
Expand Down
32 changes: 18 additions & 14 deletions scripts/make-rules/golang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ ifeq ($(DEBUG), 1)
GO_BUILD_FLAGS += -gcflags "all=-N -l"
GO_LDFLAGS=
endif
GO_BUILD_FLAGS += -tags "containers_image_openpgp netgo exclude_graphdriver_devicemapper static osusergo exclude_graphdriver_btrfs" -trimpath -ldflags "$(GO_LDFLAGS)"
# Fixed container dependency issues
# Link -> https://github.com/containers/image/pull/271/files
# For btrfs, we are currently only using overlays, so we don't need to include btrfs, otherwise we need to fix some lib issues
GO_BUILD_FLAGS += -tags "containers_image_openpgp exclude_graphdriver_devicemapper exclude_graphdriver_btrfs"

# ifeq ($(GOOS),windows)
# GO_OUT_EXT := .exe
Expand Down Expand Up @@ -103,7 +106,7 @@ go.bin.%:
$(eval COMMAND := $(word 2,$(subst ., ,$*)))
$(eval PLATFORM := $(word 1,$(subst ., ,$*)))
@echo "===========> Verifying binary $(COMMAND) $(VERSION) for $(PLATFORM)"
@if [ ! -f $(BIN_DIR)/$(PLATFORM)/$(COMMAND) ]; then echo $(MAKE) go.build PLATFORM=$(PLATFORM); fi
@if [ ! -f $(BIN_DIR)/$(COMMAND)/$(PLATFORM)/$(COMMAND) ]; then echo $(MAKE) go.build PLATFORM=$(PLATFORM); fi

## go.build.%: Build binary for specific platform
.PHONY: go.build.%
Expand All @@ -114,22 +117,23 @@ go.build.%:
$(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM))))
@echo "COMMAND=$(COMMAND)"
@echo "PLATFORM=$(PLATFORM)"
@echo "OS=$(OS)"
@echo "ARCH=$(ARCH)"
@echo "BINS=$(BINS)"
@echo "BIN_DIR=$(BIN_DIR)"
@echo "===========> Building binary $(COMMAND) $(VERSION) for $(OS) $(ARCH)"
@mkdir -p $(OUTPUT_DIR)/bin/$(OS)/$(ARCH)

# @CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(OUTPUT_DIR)/bin/$(OS)/$(ARCH)/$(COMMAND)$(GO_OUT_EXT) $(ROOT_PACKAGE)/cmd/$(COMMAND)
@echo "===========> Building binary $(COMMAND) $(VERSION) for $(OS)_$(ARCH)"
@mkdir -p $(BIN_DIR)/$(COMMAND)/$(PLATFORM)
@if [ "$(COMMAND)" == "sealer" ] || [ "$(COMMAND)" == "seautil" ]; then \
CGO_ENABLED=1; \
CC=x86_64-linux-gnu-gcc; \
if [ "$(ARCH)" == "arm64" ]; then \
CC=aarch64-linux-gnu-gcc; \
fi; \
CGO_ENABLED=$$CGO_ENABLED CC=$$CC GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(BIN_DIR)/$(COMMAND)/$(PLATFORM) -mod vendor $(ROOT_PACKAGE)/cmd/$(COMMAND); \
else \
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GO_BUILD_FLAGS) -o $(BIN_DIR)/$(COMMAND)/$(PLATFORM) -mod vendor $(ROOT_PACKAGE)/cmd/$(COMMAND); \
fi

## go.build: Build binaries
.PHONY: go.build
go.build: go.build.verify $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS)))
@echo "COMMAND=$(COMMAND)"
@echo "PLATFORM=$(PLATFORM)"
@echo "OS=$(OS)"
@echo "ARCH=$(ARCH)"
@echo "===========> Building binary $(BINS) $(VERSION) for $(PLATFORM)"

## go.build.multiarch: Build multi-arch binaries
Expand All @@ -153,7 +157,7 @@ go.lint: tools.verify.golangci-lint
## go.test: Run unit test
.PHONY: go.test
go.test:
@$(GO) test ./...
@$(GO) test $(GO_BUILD_FLAGS) ./...

## go.test.junit-report: Run unit test
.PHONY: go.test.junit-report
Expand Down

0 comments on commit aea8446

Please sign in to comment.