1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2019 Nokia.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 #------------------------------------------------------------------------------
19 #------------------------------------------------------------------------------
21 $(error ROOT_DIR NOT DEFINED)
24 $(error CACHE_DIR NOT DEFINED)
27 #------------------------------------------------------------------------------
29 #------------------------------------------------------------------------------
31 GO_CACHE_DIR?=$(abspath $(CACHE_DIR)/go)
33 #------------------------------------------------------------------------------
35 #------------------------------------------------------------------------------
36 ifndef MAKE_GO_TARGETS
40 .PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean
45 GOOS=$(shell go env GOOS)
47 GOBUILD=$(GOCMD) build -a -installsuffix cgo
48 GORUN=$(GOCMD) run -a -installsuffix cgo
49 GOCLEAN=$(GOCMD) clean
50 GOTEST=$(GOCMD) test -v
53 GOFILES:=$(shell find $(ROOT_DIR) -name '*.go' -not -name '*_test.go')
54 GOALLFILES:=$(shell find $(ROOT_DIR) -name '*.go')
55 GOMODFILES:=go.mod go.sum
59 $(GO_CACHE_DIR)/%: $(GOFILES) $(GOMODFILES) $$(BUILDDEPS)
61 @eval GO111MODULE=on GOSUMDB=off GO_ENABLED=0 GOOS=linux $(BUILDARGS) $(GOBUILD) -o $@ ./$*
65 $(GO_CACHE_DIR)/%_test: $(GOALLFILES) $(GOMODFILES) $$(BUILDDEPS) FORCE
67 @eval GO111MODULE=on GOSUMDB=off GO_ENABLED=0 GOOS=linux $(BUILDARGS) $(GOTEST) -coverprofile $(COVEROUT) -c -o $@ ./$*
68 @if test -e $@ ; then eval $(TESTENV) $@ -test.v -test.coverprofile $(COVEROUT); else true ; fi
69 @if test -e $@ ; then go tool cover -html=$(COVEROUT) -o $(COVERHTML); else true ; fi
73 go-build: GO_TARGETS:=
74 go-build: $$(GO_TARGETS)
78 go-test: $$(GO_TARGETS)
80 go-test-fmt: $(GOFILES)
81 @(RESULT="$$(gofmt -l $^)"; test -z "$${RESULT}" || (echo -e "gofmt failed:\n$${RESULT}" && false) )
87 GO111MODULE=on GOSUMDB=off go mod tidy
89 go-mod-download: FORCE
90 GO111MODULE=on GOSUMDB=off go mod download
92 go-clean: GO_TARGETS:=
94 @echo " > Cleaning build cache"
95 @-rm -rf $(GO_TARGETS)* 2> /dev/null
101 #------------------------------------------------------------------------------
103 #-------------------------------------------------------------------- ----------
105 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDDEPS:=$(XAPP_BUILDDEPS)
106 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDARGS:=$(XAPP_BUILDARGS)
109 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: BUILDDEPS:=$(XAPP_BUILDDEPS)
110 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: BUILDARGS:=$(XAPP_BUILDARGS)
111 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVEROUT:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.out)
112 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVERHTML:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.html)
113 $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: TESTENV:=$(XAPP_TESTENV)
115 go-build: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)
116 go-test: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
117 go-clean: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test