blob: ac5a2191d9f45bf2129aaa5bf9a91f56e69f0854 [file] [log] [blame]
Juha Hyttinen6e075ce2019-11-06 08:42:34 +02001# 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#------------------------------------------------------------------------------
20ifndef ROOT_DIR
21$(error ROOT_DIR NOT DEFINED)
22endif
23ifndef CACHE_DIR
24$(error CACHE_DIR NOT DEFINED)
25endif
26
27#------------------------------------------------------------------------------
28#
29#------------------------------------------------------------------------------
30
31GO_CACHE_DIR?=$(abspath $(CACHE_DIR)/go)
32
33#------------------------------------------------------------------------------
34#
35#------------------------------------------------------------------------------
36ifndef MAKE_GO_TARGETS
37MAKE_GO_TARGETS:=1
38
39
40.PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean
41
42FORCE:
43
44
45GOOS=$(shell go env GOOS)
46GOCMD=go
47GOBUILD=$(GOCMD) build -a -installsuffix cgo
48GORUN=$(GOCMD) run -a -installsuffix cgo
49GOCLEAN=$(GOCMD) clean
50GOTEST=$(GOCMD) test -v
51GOGET=$(GOCMD) get
52
53GOFILES:=$(shell find $(ROOT_DIR) -name '*.go' -not -name '*_test.go')
54GOALLFILES:=$(shell find $(ROOT_DIR) -name '*.go')
55GOMODFILES:=go.mod go.sum
56
57
58.SECONDEXPANSION:
59$(GO_CACHE_DIR)/%: $(GOFILES) $(GOMODFILES) $$(BUILDDEPS)
60 @echo "Building:\t$*"
61 @eval GO111MODULE=on GOSUMDB=off GO_ENABLED=0 GOOS=linux $(BUILDARGS) $(GOBUILD) -o $@ ./$*
62
63
64.SECONDEXPANSION:
65$(GO_CACHE_DIR)/%_test: $(GOALLFILES) $(GOMODFILES) $$(BUILDDEPS) FORCE
66 @echo "Testing:\t$*"
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.coverprofile $(COVEROUT); else true ; fi
69 @if test -e $@ ; then go tool cover -html=$(COVEROUT) -o $(COVERHTML); else true ; fi
70
71
72.SECONDEXPANSION:
73go-build: GO_TARGETS:=
74go-build: $$(GO_TARGETS)
75
76.SECONDEXPANSION:
77go-test: GO_TARGETS:=
78go-test: $$(GO_TARGETS)
79
80go-test-fmt: $(GOFILES)
81 @(RESULT="$$(gofmt -l $^)"; test -z "$${RESULT}" || (echo -e "gofmt failed:\n$${RESULT}" && false) )
82
83go-fmt: $(GOFILES)
84 gofmt -w -s $^
85
86go-mod-tidy: FORCE
87 GO111MODULE=on GOSUMDB=off go mod tidy
88
89go-mod-download: FORCE
90 GO111MODULE=on GOSUMDB=off go mod download
91
92go-clean: GO_TARGETS:=
93go-clean:
94 @echo " > Cleaning build cache"
95 @-rm -rf $(GO_TARGETS)* 2> /dev/null
96 go clean 2> /dev/null
97
98
99endif
100
101#------------------------------------------------------------------------------
102#
103#-------------------------------------------------------------------- ----------
104
105$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDDEPS:=$(XAPP_BUILDDEPS)
106$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDARGS:=$(XAPP_BUILDARGS)
107
108
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)
114
115go-build: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)
116go-test: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
117go-clean: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test