Skip to content

Commit

Permalink
Improve and cleanup Makefile (#185)
Browse files Browse the repository at this point in the history
Signed-off-by: Max Lambrecht <maxlambrecht@gmail.com>
  • Loading branch information
Max Lambrecht authored Jun 1, 2023
1 parent 8bf6c21 commit bb5d985
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 127 deletions.
203 changes: 76 additions & 127 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
DIR := $(CURDIR)
include versions.mk

.DEFAULT_GOAL = help
cyan := $(shell which tput > /dev/null && tput setaf 6 2>/dev/null || echo "")
reset := $(shell which tput > /dev/null && tput sgr0 2>/dev/null || echo "")
bold := $(shell which tput > /dev/null && tput bold 2>/dev/null || echo "")

# Vars
DIR := $(CURDIR)
.DEFAULT_GOAL = help
E := @
ifeq ($(V),1)
E =
endif

cyan := $(shell which tput > /dev/null && tput setaf 6 2>/dev/null || echo "")
reset := $(shell which tput > /dev/null && tput sgr0 2>/dev/null || echo "")
bold := $(shell which tput > /dev/null && tput bold 2>/dev/null || echo "")

.PHONY: default

default: build

all: build test

############################################################################
# OS/ARCH detection
############################################################################
os1 := $(shell uname -s)
os2 :=
ifeq ($(os1),Darwin)
Expand All @@ -46,144 +39,110 @@ else
$(error unsupported ARCH: $(arch1))
endif

############################################################################
# Vars
############################################################################

# Define build directories and URLs
build_dir := $(DIR)/.build/$(os1)-$(arch1)

go_version_full := $(shell cat .go-version)
go_version := $(subst .0,,$(go_version_full))
go_dir := $(build_dir)/go/$(go_version)
go_dir := $(build_dir)/go/$(GO_VERSION)
server_sqlc_config_file := $(DIR)/pkg/server/db/sqlc.yaml
sqlc_dir := $(build_dir)/sqlc/$(SQLC_VERSION)
sqlc_bin := $(sqlc_dir)/sqlc

ifeq ($(os1),windows)
go_bin_dir := $(go_dir)/go/bin
go_url := https://storage.googleapis.com/golang/go$(go_version).$(os1)-$(arch2).zip
go_url := https://storage.googleapis.com/golang/go$(GO_VERSION).$(os1)-$(arch2).zip
exe := .exe
else
go_bin_dir := $(go_dir)/bin
go_url := https://storage.googleapis.com/golang/go$(go_version).$(os1)-$(arch2).tar.gz
go_url := https://storage.googleapis.com/golang/go$(GO_VERSION).$(os1)-$(arch2).tar.gz
exe :=
endif

go_path := PATH="$(go_bin_dir):$(PATH)"

oapi_codegen_version := 1.12.4
oapi_codegen_dir := $(build_dir)/protoc/$(protoc_version)

server_sqlc_config_file := $(DIR)/pkg/server/db/sqlc.yaml

sqlc_dir := $(build_dir)/sqlc/$(sqlc_version)
sqlc_bin := $(sqlc_dir)/sqlc
sqlc_version := 1.18.0
# SQLC download URL
ifeq ($(os1),windows)
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(sqlc_version)/sqlc_$(sqlc_version)_windows_amd64.zip
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(SQLC_VERSION)/sqlc_$(SQLC_VERSION)_windows_amd64.zip
else ifeq ($(os1),darwin)
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(sqlc_version)/sqlc_$(sqlc_version)_darwin_$(arch2).zip
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(SQLC_VERSION)/sqlc_$(SQLC_VERSION)_darwin_$(arch2).zip
else
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(sqlc_version)/sqlc_$(sqlc_version)_linux_amd64.zip
sqlc_url := https://github.com/kyleconroy/sqlc/releases/download/v$(SQLC_VERSION)/sqlc_$(SQLC_VERSION)_linux_amd64.zip
endif

# Define Go path
go_path := PATH="$(go_bin_dir):$(PATH)"

# Define master template for compiling Go binaries
define binary_rule
.PHONY: $1
$1: | go-check bin/
@echo "Building $1..."
$(E)$(go_path) go build -o $1 $2
endef

# Dynamically generate targets for each binary using the binary_rule template
$(eval $(call binary_rule,bin/galadriel-harvester,cmd/harvester/main.go))
$(eval $(call binary_rule,bin/galadriel-server,cmd/server/main.go))

# Build directories
bin/:
@mkdir -p $@

# Go check and installation if necessary
go-check:
ifeq (go$(go_version), $(shell $(go_path) go version 2>/dev/null | cut -f3 -d' '))
ifeq (go$(GO_VERSION), $(shell $(go_path) go version 2>/dev/null | cut -f3 -d' '))
else
@echo "Installing go $(go_version)..."
@echo "Installing go $(GO_VERSION)..."
$(E)rm -rf $(dir $(go_dir))
$(E)mkdir -p $(go_dir)
$(E)curl -sSfL $(go_url) | tar xz -C $(go_dir) --strip-components=1
endif

## Checks installed go version and prints the path it is installed.
# Prints Go binary installation path
go-bin-path: go-check
@echo "$(go_bin_dir):${PATH}"

# Install necessary toolchains
install-toolchain: install-sqlc | go-check

# Install SQLC
install-sqlc: $(sqlc_bin)

$(sqlc_bin):
@echo "Installing sqlc $(sqlc_version)..."
@echo "Installing sqlc $(SQLC_VERSION)..."
$(E)rm -rf $(dir $(sqlc_dir))
$(E)mkdir -p $(sqlc_dir)
$(E)echo $(sqlc_url); curl -sSfL $(sqlc_url) -o $(build_dir)/tmp.zip; unzip -q -d $(sqlc_dir) $(build_dir)/tmp.zip; rm $(build_dir)/tmp.zip


# The following vars are used in rule construction
comma := ,
null :=
space := $(null)

.PHONY: build

## Compiles all Galadriel binaries.
# Rules for building binaries and running tests
default: build
all: build test
build: bin/galadriel-harvester bin/galadriel-server

# This is the master template for compiling Go binaries
define binary_rule
.PHONY: $1
$1: | go-check bin/
@echo "Building $1..."
$(E)$(go_path) go build -o $1 $2
endef

# This dynamically generates targets for each binary using
# the binary_rule template above
$(eval $(call binary_rule,bin/galadriel-harvester,cmd/harvester/main.go))
$(eval $(call binary_rule,bin/galadriel-server,cmd/server/main.go))

bin/:
@mkdir -p $@

CONTAINER_OPTIONS := docker podman
CONTAINER_EXEC := $(shell command -v $(CONTAINER_OPTIONS) 2> /dev/null)

server-run: build
./bin/galadriel-server run

## Runs the go unit tests.
test: test-unit

test-unit:
go test -cover ./...

## Runs unit tests with race detection.
race-test:
go test -cover -race ./...

## Generates the test coverage for the code with the Go tool.
coverage:
$(E)mkdir -p out/coverage
go test -v -coverprofile ./out/coverage/coverage.out ./... && \
go tool cover -html=./out/coverage/coverage.out -o ./out/coverage/index.html
clean:
rm -rf $(build_dir)
rm -f bin/galadriel-harvester
rm -f bin/galadriel-server
rm -rf out/coverage
.PHONY: clean

## Builds docker image for Galadriel Server.
docker-build-server:
docker build . --target galadriel-server --tag galadriel-server:latest

## Builds docker image for Galadriel Harvester.
docker-build-harvester:
docker build . --target galadriel-harvester --tag galadriel-harvester:latest

## Builds all docker images.
docker-build: docker-build-server docker-build-harvester

#------------------------------------------------------------------------
# Document file
#------------------------------------------------------------------------

# VARIABLES
NAME := Galadriel
VERSION := 0.1.0
AUTHOR := HPE

# COLORS
GREEN := $(shell tput -Txterm setaf 2)
RESET := $(shell tput -Txterm sgr0)

TARGET_MAX_CHAR_NUM := 30
# Generate SQL and API code
generate-sql-code: install-sqlc $(server_sqlc_config_file)
@echo "Generating server SQL code..."
$(sqlc_bin) generate --file $(server_sqlc_config_file)
generate-api-code: $(SPEC_FILES)
@echo "Generating API code..."
go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v$(OAPI_CODEGEN_VERSION)
cd ./pkg/common/api; $(GOPATH)/bin/oapi-codegen -config schemas.cfg.yaml schemas.yaml
cd ./pkg/server/api/admin; $(GOPATH)/bin/oapi-codegen -config admin.cfg.yaml admin.yaml
cd ./pkg/server/api/harvester; $(GOPATH)/bin/oapi-codegen -config harvester.cfg.yaml harvester.yaml
cd ./pkg/harvester/api/admin; $(GOPATH)/bin/oapi-codegen -config admin.cfg.yaml admin.yaml

## Shows help.
## Shows help.
# Help rule
help:
@echo "$(bold)Usage:$(reset) make $(cyan)<target>$(reset)"
@echo
Expand All @@ -193,25 +152,15 @@ help:
@echo "$(bold)Test:$(reset)"
@echo " $(cyan)test$(reset) - run unit tests"
@echo
@echo "$(bold)Build and test:$(reset)"
@echo " $(cyan)all$(reset) - build all Galadriel binaries, and run unit tests"
@echo "$(bold)Toolchain:$(reset)"
@echo " $(cyan)install-toolchain$(reset) - install required build tools"
@echo " $(cyan)go-bin-path$(reset) - print path of installed go binary"
@echo
@echo "$(bold)Code generation:$(reset)"
@echo " $(cyan)generate-sql-code$(reset) - generate SQL code using sqlc"
@echo " $(cyan)generate-api-code$(reset) - generate API code using oapi-codegen"

.PHONY: generate-sql-code generate-api-code

# Run sqlc to generate SQL code for the server
generate-sql-code: install-sqlc $(server_sqlc_config_file)
@echo "Generating server SQL code..."
$(sqlc_bin) generate --file $(server_sqlc_config_file)

# Specify the input specification files as prerequisites for generate-api-code
generate-api-code: $(SPEC_FILES)
@echo "Generating API code..."
go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v$(oapi_codegen_version)
cd ./pkg/common/api; $(GOPATH)/bin/oapi-codegen -config schemas.cfg.yaml schemas.yaml
cd ./pkg/server/api/admin; $(GOPATH)/bin/oapi-codegen -config admin.cfg.yaml admin.yaml
cd ./pkg/server/api/harvester; $(GOPATH)/bin/oapi-codegen -config harvester.cfg.yaml harvester.yaml
cd ./pkg/harvester/api/admin; $(GOPATH)/bin/oapi-codegen -config admin.cfg.yaml admin.yaml
@echo "$(bold)Code Generation:$(reset)"
@echo " $(cyan)generate-sql-code$(reset) - generate sql code using sqlc"
@echo " $(cyan)generate-api-code$(reset) - generate api code using oapi-codegen"
@echo
@echo "$(bold)Cleanup:$(reset)"
@echo " $(cyan)clean$(reset) - clean build artifacts"
@echo
.PHONY: help
3 changes: 3 additions & 0 deletions versions.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GO_VERSION := $(shell cat .go-version)
SQLC_VERSION := 1.18.0
OAPI_CODEGEN_VERSION := 1.12.4

0 comments on commit bb5d985

Please sign in to comment.