Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: makefile project engineering design and build design #2161

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ pkg/cloud/dashboard/Cargo.lock
docs/site/node_modules
docs/site/package-lock.json

.vscode

# tools is responsible for collecting sealer third party package binary tools
tools/
#coverage.out
tmp/
263 changes: 147 additions & 116 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,140 +1,171 @@
# 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.

# ==============================================================================
# define the default goal
#

.DEFAULT_GOAL := help

Dirs=$(shell ls)
GIT_TAG := $(shell git describe --exact-match --tags --abbrev=0 2> /dev/null || echo untagged)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
.PHONY: all
all: tidy gen add-copyright format lint cover build

# ==============================================================================
# Build set

ROOT_PACKAGE=github.com/sealerio/sealer
VERSION_PACKAGE=github.com/sealerio/sealer/pkg/version

BUILD_SCRIPTS := scripts/build.sh

# ==============================================================================
# Includes

include scripts/make-rules/common.mk # make sure include common.mk at the first include line
include scripts/make-rules/golang.mk
include scripts/make-rules/image.mk
include scripts/make-rules/copyright.mk
include scripts/make-rules/gen.mk
include scripts/make-rules/dependencies.mk
include scripts/make-rules/tools.mk

# ==============================================================================
# Usage

define USAGE_OPTIONS

Options:

DEBUG Whether or not to generate debug symbols. Default is 0.

BINS Binaries to build. Default is all binaries under cmd.
This option is available when using: make {build}(.multiarch)
Example: make build BINS="sealer sealctl"

PLATFORMS Platform to build for. Default is linux_arm64 and linux_amd64.
This option is available when using: make {build}.multiarch
Example: make build.multiarch PLATFORMS="linux_arm64 linux_amd64"

V Set to 1 enable verbose build. Default is 0.
endef
export USAGE_OPTIONS

# ==============================================================================
# Targets

## build: Build binaries by default
.PHONY: build
build:
@$(MAKE) go.build

TOOLS_DIR := hack/build.sh
## tidy: tidy go.mod
.PHONY: tidy
tidy:
@$(GO) mod tidy

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifneq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
## vendor: vendor go.mod
.PHONY: vendor
vendor:
@$(GO) mod vendor

## fmt: Run go fmt against code.
.PHONY: fmt
fmt:
go fmt ./...
@$(GO) fmt ./...

## vet: Run go vet against code.
.PHONY: vet
vet:
go vet ./...
@$(GO) vet ./...

## lint: Run go lint against code.
## lint: Check syntax and styling of go sources.
.PHONY: lint
lint:
golangci-lint run -v ./...
@$(MAKE) go.lint

## style: code style: fmt,vet,lint
## style: code style -> fmt,vet,lint
.PHONY: style
style: fmt vet lint

## build: Build binaries by default
build: clean
@echo "build sealer and seautil bin"
$(TOOLS_DIR)
## linux: Build the all with a build script
.PHONY: linux
linux:
@$(MAKE) go.linux-a

## linux: Build binaries for Linux
linux: clean
@echo "Building sealer and seautil binaries for Linux (amd64)"
GOOS=$(GOOS) GOARCH=$(GOARCH) $(TOOLS_DIR) $(GIT_TAG)
## linux.%: Build binaries for Linux (make linux.amd64 OR make linux.arm64)
linux.%:
@$(MAKE) go.linux.$*

## linux-amd64: Build binaries for Linux (amd64)
linux-amd64: clean
@echo "Building sealer and seautil binaries for Linux (amd64)"
GOOS=linux GOARCH=amd64 $(TOOLS_DIR) $(GIT_TAG)
## format: Gofmt (reformat) package sources (exclude vendor dir if existed).
.PHONY: format
format:
@$(MAKE) go.format

## linux-arm64: Build binaries for Linux (arm64)
linux-arm64: clean
@echo "Building sealer and seautil binaries for Linux (arm64)"
GOOS=linux GOARCH=arm64 $(TOOLS_DIR) $(GIT_TAG)
## test: Run unit test.
.PHONY: test
test:
@$(MAKE) go.test

## build-in-docker: sealer should be compiled in linux platform, otherwise there will be GraphDriver problem.
build-in-docker:
docker run --rm -v ${PWD}:/usr/src/sealer -w /usr/src/sealer registry.cn-qingdao.aliyuncs.com/sealer-io/sealer-build:v1 make linux
## cover: Run unit test and get test coverage.
.PHONY: cover
cover:
@$(MAKE) go.test.cover

## updates: Check for updates to go.mod dependencies
.PHONY: updates
@$(MAKE) go.updates

## imports: task to automatically handle import packages in Go files using goimports tool
.PHONY: imports
imports:
@$(MAKE) go.imports

## clean: Remove all files that are created by building.
.PHONY: clean
clean:
@echo "===========> Cleaning all build output"
@-rm -rf _output

## install-addlicense: check license if not exist install addlicense tools
install-addlicense:
ifeq (, $(shell which addlicense))
@{ \
set -e ;\
LICENSE_TMP_DIR=$$(mktemp -d) ;\
cd $$LICENSE_TMP_DIR ;\
go mod init tmp ;\
go get -v github.com/google/addlicense ;\
rm -rf $$LICENSE_TMP_DIR ;\
}
ADDLICENSE_BIN=$(GOBIN)/addlicense
else
ADDLICENSE_BIN=$(shell which addlicense)
endif

filelicense: SHELL:=/bin/bash
## filelicense: add license
filelicense:
for file in ${Dirs} ; do \
if [[ $$file != '_output' && $$file != 'docs' && $$file != 'vendor' && $$file != 'logger' && $$file != 'applications' ]]; then \
$(ADDLICENSE_BIN) -y $(shell date +"%Y") -c "Alibaba Group Holding Ltd." -f hack/LICENSE_TEMPLATE ./$$file ; \
fi \
done


## install-gosec: check license if not exist install addlicense tools
install-gosec:
ifeq (, $(shell which gosec))
@{ \
set -e ;\
curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(GOBIN) v2.2.0 ;\
}
GOSEC_BIN=$(GOBIN)/gosec
else
GOSEC_BIN=$(shell which gosec)
endif

gosec: install-gosec
$(GOSEC_BIN) ./...


install-deepcopy-gen:
ifeq (, $(shell which deepcopy-gen))
@{ \
set -e ;\
LICENSE_TMP_DIR=$$(mktemp -d) ;\
cd $$LICENSE_TMP_DIR ;\
go mod init tmp ;\
go get -v k8s.io/code-generator/cmd/deepcopy-gen ;\
rm -rf $$LICENSE_TMP_DIR ;\
}
DEEPCOPY_BIN=$(GOBIN)/deepcopy-gen
else
DEEPCOPY_BIN=$(shell which deepcopy-gen)
endif

HEAD_FILE := hack/boilerplate.go.txt
INPUT_DIR := github.com/sealerio/sealer/types/api
deepcopy:install-deepcopy-gen
$(DEEPCOPY_BIN) \
--input-dirs="$(INPUT_DIR)/v1" \
-O zz_generated.deepcopy \
--go-header-file "$(HEAD_FILE)" \
--output-base "${GOPATH}/src"
$(DEEPCOPY_BIN) \
--input-dirs="$(INPUT_DIR)/v2" \
-O zz_generated.deepcopy \
--go-header-file "$(HEAD_FILE)" \
--output-base "${GOPATH}/src"

## help: Display help information
@$(MAKE) go.clean

## tools: Install dependent tools.
.PHONY: tools
tools:
@$(MAKE) tools.install

## build-in-docker: sealer should be compiled in linux platform, otherwise there will be GraphDriver problem.
build-in-docker:
@docker run --rm -v ${PWD}:/usr/src/sealer -w /usr/src/sealer registry.cn-qingdao.aliyuncs.com/sealer-io/sealer-build:v1 make linux

## gen: Generate all necessary files.
.PHONY: gen
gen:
@$(MAKE) gen.run

## verify-copyright: Verify the license headers for all files.
.PHONY: verify-copyright
verify-copyright:
@$(MAKE) copyright.verify

## add-copyright: Add copyright ensure source code files have license headers.
.PHONY: add-copyright
add-copyright:
@$(MAKE) copyright.add

## help: Show this help info.
.PHONY: help
help: Makefile
@echo ""
@echo "Usage:" "\n"
@echo " make [target]" "\n"
@echo "Targets:" "\n" ""
@awk -F ':|##' '/^[^\.%\t][^\t]*:.*##/{printf " \033[36m%-20s\033[0m %s\n", $$1, $$NF}' $(MAKEFILE_LIST) | sort
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'
$(call makehelp)

## all-help: Show all help details info.
.PHONY: help-all
help-all: go.help copyright.help tools.help image.help help
$(call makeallhelp)
10 changes: 5 additions & 5 deletions build/buildimage/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ import (
osi "github.com/sealerio/sealer/utils/os"
)

// TODO: update the variable name
// TODO: update the variable name.
var (
copyToManifests = "manifests"
copyToChart = "charts"
copyToImageList = "imageList"
copyToApplication = "application"
)

type parseContainerImageStringSliceFunc func(srcPath string) ([]string, error)
type parseContainerImageListFunc func(srcPath string) ([]*v12.ContainerImage, error)
type (
parseContainerImageStringSliceFunc func(srcPath string) ([]string, error)
parseContainerImageListFunc func(srcPath string) ([]*v12.ContainerImage, error)
)

var parseContainerImageStringSliceFuncMap = map[string]func(srcPath string) ([]string, error){
copyToManifests: parseYamlImages,
Expand Down Expand Up @@ -274,7 +276,6 @@ func parseApplicationKubeImages(kubePath string) ([]string, error) {
}

ima, err := imageSearcher.ListImages(path)

if err != nil {
return err
}
Expand Down Expand Up @@ -351,7 +352,6 @@ func parseYamlImages(srcPath string) ([]string, error) {
}

ima, err := imageSearcher.ListImages(path)

if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions build/kubefile/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
// Deprecated
Cmd = "cmd"

// the following commands are the intenal implementations for kube commands
// the following commands are the intenal implementations for kube commands.
Add = "add"
Arg = "arg"
Copy = "copy"
Expand All @@ -59,7 +59,7 @@ var (
LabelKubeCSIPrefix = fmt.Sprintf("%s-", LabelSupportedKubeCSIAlpha)
)

// SupportedCommands is list of all Kubefile commands
// SupportedCommands is list of all Kubefile commands.
var SupportedCommands = map[string]struct{}{
Add: {},
Arg: {},
Expand Down
2 changes: 1 addition & 1 deletion cmd/sealer/cmd/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/pkg/imageengine/buildah"
"github.com/sealerio/sealer/pkg/rootfs"
"github.com/sealerio/sealer/pkg/version"
v1 "github.com/sealerio/sealer/types/api/v1"
osi "github.com/sealerio/sealer/utils/os"
"github.com/sealerio/sealer/utils/strings"
"github.com/sealerio/sealer/utils/yaml"
"github.com/sealerio/sealer/version"

"github.com/containerd/containerd/platforms"
"github.com/containers/buildah/define"
Expand Down
2 changes: 1 addition & 1 deletion cmd/sealer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/sealerio/sealer/cmd/sealer/cmd/image"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/logger"
"github.com/sealerio/sealer/version"
"github.com/sealerio/sealer/pkg/version"
)

type rootOpts struct {
Expand Down
Loading