Juha Hyttinen | 6e075ce | 2019-11-06 08:42:34 +0200 | [diff] [blame] | 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 | ifndef ROOT_DIR |
| 21 | $(error ROOT_DIR NOT DEFINED) |
| 22 | endif |
| 23 | ifndef CACHE_DIR |
| 24 | $(error CACHE_DIR NOT DEFINED) |
| 25 | endif |
| 26 | |
| 27 | #------------------------------------------------------------------------------ |
| 28 | # |
| 29 | #------------------------------------------------------------------------------ |
| 30 | |
| 31 | GO_CACHE_DIR?=$(abspath $(CACHE_DIR)/go) |
| 32 | |
| 33 | #------------------------------------------------------------------------------ |
| 34 | # |
| 35 | #------------------------------------------------------------------------------ |
| 36 | ifndef MAKE_GO_TARGETS |
| 37 | MAKE_GO_TARGETS:=1 |
| 38 | |
| 39 | |
| 40 | .PHONY: FORCE go-build go-test go-test-fmt go-fmt go-clean |
| 41 | |
| 42 | FORCE: |
| 43 | |
| 44 | |
| 45 | GOOS=$(shell go env GOOS) |
| 46 | GOCMD=go |
| 47 | GOBUILD=$(GOCMD) build -a -installsuffix cgo |
| 48 | GORUN=$(GOCMD) run -a -installsuffix cgo |
| 49 | GOCLEAN=$(GOCMD) clean |
| 50 | GOTEST=$(GOCMD) test -v |
| 51 | GOGET=$(GOCMD) get |
| 52 | |
| 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 |
| 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: |
| 73 | go-build: GO_TARGETS:= |
| 74 | go-build: $$(GO_TARGETS) |
| 75 | |
| 76 | .SECONDEXPANSION: |
| 77 | go-test: GO_TARGETS:= |
| 78 | go-test: $$(GO_TARGETS) |
| 79 | |
| 80 | go-test-fmt: $(GOFILES) |
| 81 | @(RESULT="$$(gofmt -l $^)"; test -z "$${RESULT}" || (echo -e "gofmt failed:\n$${RESULT}" && false) ) |
| 82 | |
| 83 | go-fmt: $(GOFILES) |
| 84 | gofmt -w -s $^ |
| 85 | |
| 86 | go-mod-tidy: FORCE |
| 87 | GO111MODULE=on GOSUMDB=off go mod tidy |
| 88 | |
| 89 | go-mod-download: FORCE |
| 90 | GO111MODULE=on GOSUMDB=off go mod download |
| 91 | |
| 92 | go-clean: GO_TARGETS:= |
| 93 | go-clean: |
| 94 | @echo " > Cleaning build cache" |
| 95 | @-rm -rf $(GO_TARGETS)* 2> /dev/null |
| 96 | go clean 2> /dev/null |
| 97 | |
| 98 | |
| 99 | endif |
| 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 | |
| 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 |