18d5d444f321959dcbe5b60f9834fa85c52a7360
[ric-plt/appmgr.git] / build / make.go.mk
1 #   Copyright (c) 2019 AT&T Intellectual Property.
2 #   Copyright (c) 2019 Nokia.
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15
16
17 #------------------------------------------------------------------------------
18 #
19 #------------------------------------------------------------------------------
20 #ROOT_DIR:=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
21
22 ifndef ROOT_DIR
23 $(error ROOT_DIR NOT DEFINED)
24 endif
25 BUILD_DIR?=$(abspath $(ROOT_DIR)/build)
26 CACHE_DIR?=$(abspath $(BUILD_DIR)/cache)
27
28
29 #------------------------------------------------------------------------------
30 #
31 #-------------------------------------------------------------------- ----------
32 ifndef MAKE_GO_TARGETS
33 MAKE_GO_TARGETS:=1
34
35
36 GOOS=$(shell go env GOOS)
37 GOCMD=go
38 GOBUILD=$(GOCMD) build -a -installsuffix cgo
39 GORUN=$(GOCMD) run -a -installsuffix cgo
40 GOCLEAN=$(GOCMD) clean
41 GOTEST=$(GOCMD) test -v
42 GOGET=$(GOCMD) get
43
44 GOFILES:=$(shell find $(ROOT_DIR) -name '*.go' -not -name '*_test.go')
45 GOALLFILES:=$(shell find $(ROOT_DIR) -name '*.go')
46 GOMODFILES:=go.mod go.sum
47
48 .PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean
49  
50
51 FORCE:
52
53
54 $(CACHE_DIR)/%: $(GOFILES) $(GOMODFILES)
55         @echo "Building:\t$*"
56         GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOBUILD) -o $@ ./$*
57
58
59 $(CACHE_DIR)/%_test: $(GOALLFILES) $(GOMODFILES)
60         @echo "Testing:\t$*"
61         GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOTEST) -coverprofile $(COVEROUT) -c -o $@ ./$*
62         test -e $@ && (eval $(TESTENV) $@ -test.coverprofile $(COVEROUT) || false) || true
63         test -e $@ && (go tool cover -html=$(COVEROUT) -o $(COVERHTML) || false) || true
64
65
66 .SECONDEXPANSION:
67 go-build: XAPP_TARGETS:=
68 go-build: $$(XAPP_TARGETS)
69
70 .SECONDEXPANSION:
71 go-test: XAPP_TARGETS:=
72 go-test: go-clean $$(XAPP_TARGETS)
73
74 go-test-fmt: $(GOFILES)
75         @(RESULT="$$(gofmt -l $^)"; test -z "$${RESULT}" || (echo -e "gofmt failed:\n$${RESULT}" && false) )
76
77 go-fmt: $(GOFILES)
78         gofmt -w -s $^
79
80 go-clean: XAPP_TARGETS:=
81 go-clean:
82         @echo "  >  Cleaning build cache"
83         @-rm -rf $(XAPP_TARGETS)* 2> /dev/null
84         go clean 2> /dev/null
85
86
87 endif
88
89 #------------------------------------------------------------------------------
90 #
91 #-------------------------------------------------------------------- ----------
92
93 $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVEROUT:=$(abspath $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.out)
94 $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVERHTML:=$(abspath $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.html)
95 $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: TESTENV:=$(XAPP_TESTENV)
96
97 go-build: XAPP_TARGETS+=$(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)
98 go-test: XAPP_TARGETS+=$(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
99 go-clean: XAPP_TARGETS+=$(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) $(CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
100