github.com/matrixorigin/matrixone@v1.2.0/Makefile (about)

     1  # Copyright 2021 - 2022 Matrix Origin
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  #
    16  # Examples
    17  #
    18  # By default, make builds the mo-service
    19  #
    20  # make
    21  #
    22  # To re-build MO -
    23  #
    24  #	make clean
    25  #	make build
    26  #
    27  # To re-build MO in debug mode also with race detector enabled -
    28  #
    29  # make clean
    30  # make debug
    31  #
    32  # To run static checks -
    33  #
    34  # make install-static-check-tools
    35  # make static-check
    36  #
    37  # To construct a directory named vendor in the main module’s root directory that contains copies of all packages needed to support builds and tests of packages in the main module.
    38  # make vendor
    39  #
    40  
    41  # where am I
    42  ROOT_DIR = $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
    43  BIN_NAME := mo-service
    44  UNAME_S := $(shell uname -s)
    45  GOPATH := $(shell go env GOPATH)
    46  GO_VERSION=$(shell go version)
    47  BRANCH_NAME=$(shell git rev-parse --abbrev-ref HEAD)
    48  LAST_COMMIT_ID=$(shell git rev-parse --short HEAD)
    49  BUILD_TIME=$(shell date +%s)
    50  MO_VERSION=$(shell git describe --always --contains $(shell git rev-parse HEAD))
    51  GO_MODULE=$(shell go list -m)
    52  MUSL_DIR=$(ROOT_DIR)/musl
    53  MUSL_CC=$(MUSL_DIR)/bin/musl-gcc
    54  MUSL_VERSION:=1.2.5
    55  
    56  # cross compilation has been disabled for now
    57  ifneq ($(GOARCH)$(TARGET_ARCH)$(GOOS)$(TARGET_OS),)
    58  $(error cross compilation has been disabled)
    59  endif
    60  
    61  ###############################################################################
    62  # default target
    63  ###############################################################################
    64  
    65  all: build
    66  
    67  ###############################################################################
    68  # build vendor directory
    69  ###############################################################################
    70  
    71  .PHONY: vendor-build
    72  vendor-build:
    73  	$(info [go mod vendor])
    74  	@go mod vendor
    75  
    76  ###############################################################################
    77  # code generation
    78  ###############################################################################
    79  
    80  .PHONY: config
    81  config:
    82  	$(info [Create build config])
    83  	@go mod tidy
    84  
    85  .PHONY: generate-pb
    86  generate-pb:
    87  	$(ROOT_DIR)/proto/gen.sh
    88  
    89  # Generate protobuf files
    90  .PHONY: pb
    91  pb: vendor-build generate-pb fmt
    92  	$(info all protos are generated)
    93  
    94  ###############################################################################
    95  # build mo-service
    96  ###############################################################################
    97  
    98  RACE_OPT :=
    99  DEBUG_OPT :=
   100  CGO_DEBUG_OPT :=
   101  CGO_OPTS=CGO_CFLAGS="-I$(ROOT_DIR)/cgo " CGO_LDFLAGS="-L$(ROOT_DIR)/cgo -lm -lmo"
   102  GOLDFLAGS=-ldflags="-X '$(GO_MODULE)/pkg/version.GoVersion=$(GO_VERSION)' -X '$(GO_MODULE)/pkg/version.BranchName=$(BRANCH_NAME)' -X '$(GO_MODULE)/pkg/version.CommitID=$(LAST_COMMIT_ID)' -X '$(GO_MODULE)/pkg/version.BuildTime=$(BUILD_TIME)' -X '$(GO_MODULE)/pkg/version.Version=$(MO_VERSION)'"
   103  TAGS :=
   104  
   105  .PHONY: cgo
   106  cgo:
   107  	@(cd cgo; ${MAKE} ${CGO_DEBUG_OPT})
   108  
   109  # build mo-service binary
   110  .PHONY: build
   111  build: config cgo
   112  	$(info [Build binary])
   113  	$(CGO_OPTS) go build $(TAGS) $(RACE_OPT) $(GOLDFLAGS) $(DEBUG_OPT) -o $(BIN_NAME) ./cmd/mo-service
   114  
   115  .PHONY: musl-install
   116  musl-install:
   117  ifeq ("$(UNAME_S)","Linux")
   118   ifeq ("$(wildcard $(MUSL_CC))","")
   119  	@rm -rf /tmp/musl-$(MUSL_VERSION) musl-$(MUSL_VERSION).tar.gz
   120  	@curl -SfL "https://musl.libc.org/releases/musl-$(MUSL_VERSION).tar.gz" -o /tmp/musl-$(MUSL_VERSION).tar.gz
   121  	@tar -zxf /tmp/musl-$(MUSL_VERSION).tar.gz -C $(ROOT_DIR)
   122  	@cd musl-$(MUSL_VERSION) && ./configure --prefix=$(MUSL_DIR) --syslibdir=$(MUSL_DIR)/syslib && $(MAKE) && $(MAKE) install
   123  	@rm -rf musl-$(MUSL_VERSION) /tmp/musl-$(MUSL_VERSION).tar.gz
   124   endif
   125  endif
   126  
   127  .PHONY: musl-cgo
   128  musl-cgo: musl-install
   129  	@(cd $(ROOT_DIR)/cgo; CC=$(MUSL_CC) ${MAKE} ${CGO_DEBUG_OPT})
   130  
   131  .PHONY: musl
   132  musl: override CGO_OPTS += CC=$(MUSL_CC)
   133  musl: override GOLDFLAGS:=-ldflags="--linkmode 'external' --extldflags '-static' -X '$(GO_MODULE)/pkg/version.GoVersion=$(GO_VERSION)' -X '$(GO_MODULE)/pkg/version.BranchName=$(BRANCH_NAME)' -X '$(GO_MODULE)/pkg/version.CommitID=$(LAST_COMMIT_ID)' -X '$(GO_MODULE)/pkg/version.BuildTime=$(BUILD_TIME)' -X '$(GO_MODULE)/pkg/version.Version=$(MO_VERSION)'"
   134  musl: override TAGS := -tags musl
   135  musl: musl-install musl-cgo config
   136  musl:
   137  	$(info [Build binary(musl)])
   138  	$(CGO_OPTS) go build $(TAGS) $(RACE_OPT) $(GOLDFLAGS) $(DEBUG_OPT) -o $(BIN_NAME) ./cmd/mo-service
   139  
   140  # build mo-debug tool
   141  .PHONY: mo-debug
   142  mo-debug: config cgo
   143  	$(info [Build mo-debug tool])
   144  	$(CGO_OPTS) go build -o mo-debug ./cmd/mo-debug
   145  
   146  # build mo-service binary for debugging with go's race detector enabled
   147  # produced executable is 10x slower and consumes much more memory
   148  .PHONY: debug
   149  debug: override BUILD_NAME := debug-binary
   150  debug: override RACE_OPT := -race
   151  debug: override DEBUG_OPT := -gcflags=all="-N -l"
   152  debug: override CGO_DEBUG_OPT := debug
   153  debug: build
   154  
   155  ###############################################################################
   156  # run unit tests
   157  ###############################################################################
   158  # Excluding frontend test cases temporarily
   159  # Argument SKIP_TEST to skip a specific go test
   160  .PHONY: ut
   161  ut: config cgo
   162  	$(info [Unit testing])
   163  ifeq ($(UNAME_S),Darwin)
   164  	@cd optools && ./run_ut.sh UT $(SKIP_TEST)
   165  else
   166  	@cd optools && timeout 60m ./run_ut.sh UT $(SKIP_TEST)
   167  endif
   168  
   169  ###############################################################################
   170  # bvt and unit test
   171  ###############################################################################
   172  UT_PARALLEL ?= 1
   173  ENABLE_UT ?= "false"
   174  GOPROXY ?= "https://proxy.golang.com.cn,direct"
   175  LAUNCH ?= "launch"
   176  
   177  .PHONY: ci
   178  ci:
   179  	@rm -rf $(ROOT_DIR)/tester-log
   180  	@docker image prune -f
   181  	@docker build -f optools/bvt_ut/Dockerfile . -t matrixorigin/matrixone:local-ci --build-arg GOPROXY=$(GOPROXY)
   182  	@docker run --name tester -it \
   183  			-e LAUNCH=$(LAUNCH) \
   184  			-e UT_PARALLEL=$(UT_PARALLEL) \
   185  			-e ENABLE_UT=$(ENABLE_UT)\
   186   			--rm -v $(ROOT_DIR)/tester-log:/matrixone-test/tester-log matrixorigin/matrixone:local-ci
   187  
   188  .PHONY: ci-clean
   189  ci-clean:
   190  	@docker rmi matrixorigin/matrixone:local-ci
   191  	@docker image prune -f
   192  
   193  
   194  ###############################################################################
   195  # docker compose bvt test
   196  ###############################################################################
   197  
   198  COMPOSE_LAUNCH := "launch-multi-cn"
   199  
   200  .PHONY: compose
   201  compose:
   202  	@cd optools/compose_bvt && ./compose_bvt.sh $(ROOT_DIR) $(COMPOSE_LAUNCH)
   203  
   204  .PHONY: compose-clean
   205  compose-clean:
   206  	@docker compose -f etc/launch-tae-compose/compose.yaml --profile $(COMPOSE_LAUNCH) down --remove-orphans
   207  	@docker volume rm launch-tae-compose_minio_storage
   208  	@docker image prune -f
   209  
   210  ###############################################################################
   211  # clean
   212  ###############################################################################
   213  
   214  .PHONY: clean
   215  clean:
   216  	$(info [Clean up])
   217  	$(info Clean go test cache)
   218  	@go clean -testcache
   219  	rm -f $(BIN_NAME)
   220  	rm -rf $(ROOT_DIR)/vendor
   221  	rm -rf $(MUSL_DIR)
   222  	rm -rf /tmp/musl-$(MUSL_VERSION).tar.gz
   223  	$(MAKE) -C cgo clean
   224  
   225  ###############################################################################
   226  # static checks
   227  ###############################################################################
   228  
   229  .PHONY: fmt
   230  fmt:
   231  	gofmt -l -s -w .
   232  
   233  .PHONY: install-static-check-tools
   234  install-static-check-tools:
   235  	@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | bash -s -- -b $(GOPATH)/bin v1.55.2
   236  	@go install github.com/matrixorigin/linter/cmd/molint@latest
   237  	@go install github.com/apache/skywalking-eyes/cmd/license-eye@v0.4.0
   238  
   239  .PHONY: static-check
   240  static-check: config cgo err-check
   241  	$(CGO_OPTS) go vet -vettool=`which molint` ./...
   242  	$(CGO_OPTS) license-eye -c .licenserc.yml header check
   243  	$(CGO_OPTS) license-eye -c .licenserc.yml dep check
   244  	$(CGO_OPTS) golangci-lint run -c .golangci.yml ./...
   245  
   246  fmtErrs := $(shell grep -onr 'fmt.Errorf' pkg/ --exclude-dir=.git --exclude-dir=vendor \
   247  				--exclude=*.pb.go --exclude=system_vars.go --exclude=Makefile)
   248  errNews := $(shell grep -onr 'errors.New' pkg/ --exclude-dir=.git --exclude-dir=vendor \
   249  				--exclude=*.pb.go --exclude=system_vars.go --exclude=Makefile)
   250  
   251  .PHONY: err-check
   252  err-check:
   253  ifneq ("$(strip $(fmtErrs))$(strip $(errNews))", "")
   254   ifneq ("$(strip $(fmtErrs))", "")
   255  		$(warning 'fmt.Errorf()' is found.)
   256  		$(warning One of 'fmt.Errorf()' is called at: $(shell printf "%s\n" $(fmtErrs) | head -1))
   257   endif
   258   ifneq ("$(strip $(errNews))", "")
   259  		$(warning 'errors.New()' is found.)
   260  		$(warning One of 'errors.New()' is called at: $(shell printf "%s\n" $(errNews) | head -1))
   261   endif
   262  
   263  	$(error Use moerr instead.)
   264  else
   265  	$(info Neither 'fmt.Errorf()' nor 'errors.New()' is found)
   266  endif