blob: 286d9b16ebb2cd99ca5bd744fc9084b3b6ac2110 [file] [log] [blame]
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +00001# 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#------------------------------------------------------------------------------
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000020ifndef ROOT_DIR
21$(error ROOT_DIR NOT DEFINED)
22endif
Abukar Mohamed059775c2019-05-22 14:48:10 +000023ifndef CACHE_DIR
24$(error CACHE_DIR NOT DEFINED)
25endif
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000026
27#------------------------------------------------------------------------------
28#
Abukar Mohamed059775c2019-05-22 14:48:10 +000029#------------------------------------------------------------------------------
30
31GO_CACHE_DIR?=$(abspath $(CACHE_DIR)/go)
32
33#------------------------------------------------------------------------------
34#
35#------------------------------------------------------------------------------
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000036ifndef MAKE_GO_TARGETS
37MAKE_GO_TARGETS:=1
38
39
Abukar Mohamed059775c2019-05-22 14:48:10 +000040.PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean
41
42FORCE:
43
44
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000045GOOS=$(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
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000057
Abukar Mohamed059775c2019-05-22 14:48:10 +000058.SECONDEXPANSION:
59$(GO_CACHE_DIR)/%: $(GOFILES) $(GOMODFILES) $$(BUILDDEPS)
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000060 @echo "Building:\t$*"
61 GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOBUILD) -o $@ ./$*
62
63
Abukar Mohamed059775c2019-05-22 14:48:10 +000064.SECONDEXPANSION:
65$(GO_CACHE_DIR)/%_test: $(GOALLFILES) $(GOMODFILES) $$(BUILDDEPS) FORCE
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000066 @echo "Testing:\t$*"
67 GO111MODULE=on GO_ENABLED=0 GOOS=linux $(GOTEST) -coverprofile $(COVEROUT) -c -o $@ ./$*
68 test -e $@ && (eval $(TESTENV) $@ -test.coverprofile $(COVEROUT) || false) || true
69 test -e $@ && (go tool cover -html=$(COVEROUT) -o $(COVERHTML) || false) || true
70
71
72.SECONDEXPANSION:
Abukar Mohamed059775c2019-05-22 14:48:10 +000073go-build: GO_TARGETS:=
74go-build: $$(GO_TARGETS)
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000075
76.SECONDEXPANSION:
Abukar Mohamed059775c2019-05-22 14:48:10 +000077go-test: GO_TARGETS:=
78go-test: $$(GO_TARGETS)
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000079
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
Abukar Mohamed059775c2019-05-22 14:48:10 +000086go-mod-tidy: FORCE
87 GO111MODULE=on go mod tidy
88
89go-mod-download: FORCE
90 GO111MODULE=on go mod download
91
92go-clean: GO_TARGETS:=
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000093go-clean:
94 @echo " > Cleaning build cache"
Abukar Mohamed059775c2019-05-22 14:48:10 +000095 @-rm -rf $(GO_TARGETS)* 2> /dev/null
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +000096 go clean 2> /dev/null
97
98
99endif
100
101#------------------------------------------------------------------------------
102#
103#-------------------------------------------------------------------- ----------
104
Abukar Mohamed059775c2019-05-22 14:48:10 +0000105$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME): BUILDDEPS:=$(XAPP_BUILDDEPS)
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +0000106
Abukar Mohamed059775c2019-05-22 14:48:10 +0000107
108$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: BUILDDEPS:=$(XAPP_BUILDDEPS)
109$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVEROUT:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.out)
110$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: COVERHTML:=$(abspath $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_cover.html)
111$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test: TESTENV:=$(XAPP_TESTENV)
112
113go-build: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)
114go-test: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
115go-clean: GO_TARGETS+=$(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME) $(GO_CACHE_DIR)/$(XAPP_ROOT)/$(XAPP_NAME)_test
Abukar Mohamed97e8d9a2019-05-02 06:11:43 +0000116