-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
93 lines (71 loc) · 2.77 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-include Makefile.local # for optional local options
NAME = helloworld
COMPILER ?= crystal
SHARDS ?= shards
SOURCES != find src -name '*.cr'
LIB_SOURCES != find lib -name '*.cr' 2>/dev/null
SPEC_SOURCES != find spec -name '*.cr' 2>/dev/null
CRYSTAL_ENTRY_FILE != cat shard.yml |grep main: |cut -d: -f2|cut -d" " -f2
OUTPUT_FILE != cat shard.yml |grep main: -B1 |head -n1 |awk '{print $$1}'|awk -F: '{print $$1}'
CRYSTAL_ENTRY_PATH := $(shell pwd)/$(CRYSTAL_ENTRY_FILE)
CACHE_DIR != $(COMPILER) env CRYSTAL_CACHE_DIR
CACHE_DIR := $(CACHE_DIR)/$(subst /,-,${shell echo $(CRYSTAL_ENTRY_PATH) |cut -c2-})
FLAGS ?= --progress -Dstrict_multi_assign -Dno_number_autocast -Dpreview_overload_order
RELEASE_FLAGS ?= --no-debug --link-flags=-s --release --progress -Dstrict_multi_assign -Dno_number_autocast -Dpreview_overload_order
# INSTALL:
DESTDIR ?= /usr/local
BINDIR ?= $(DESTDIR)/bin
INSTALL ?= /usr/bin/install
O := bin/$(OUTPUT_FILE)
.PHONY: all
all: build ## build [default]
.PHONY: build
build: $(O) ## Build the application binary
$(O): $(SOURCES) $(LIB_SOURCES) lib bin
$(COMPILER) build $(FLAGS) $(CRYSTAL_ENTRY_FILE) -o $(O)
# 注意, 这些不带 .PHONY 通常都是真实文件名或目录名
lib: ## Run shards install to install dependencies
$(SHARDS) install
.PHONY: spec
spec: $(SPEC_SOURCES) $(SOURCES) $(LIB_SOURCES) lib bin ## Run spec
$(COMPILER) spec $(FLAGS) --order=random --error-on-warnings
.PHONY: format
format: ## Apply source code formatting
$(COMPILER) tool format src spec
.PHONY: install
install: release ## Install the compiler at DESTDIR
$(INSTALL) -d -m 0755 "$(BINDIR)/"
$(INSTALL) -m 0755 "$(O)" "$(BINDIR)/$(NAME)"
.PHONY: uninstall
uninstall: ## Uninstall the compiler from DESTDIR
rm -f "$(BINDIR)/$(NAME)"
.PHONY: check
check: ## Check dependencies, run shards install if necessary
$(SHARDS) check || $(SHARDS) install
.PHONY: clean
clean: ## Delete built binary
rm -f $(O)
.PHONY: cleanall
cleanall: clean # Delete built binary with cache
rm -rf ${CACHE_DIR}
.PHONY: release
release: $(SOURCES) $(LIB_SOURCES) lib bin ## Build release binary
$(COMPILER) build $(RELEASE_FLAGS) $(CRYSTAL_ENTRY_FILE) -o $(O)
bin:
@mkdir -p bin
.PHONY: help
help: ## Show this help
@echo
@printf '\033[34mtargets:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34moptional variables:\033[0m\n'
@grep -hE '^[a-zA-Z_-]+ \?=.*?## .*$$' $(MAKEFILE_LIST) |\
sort |\
awk 'BEGIN {FS = " \\?=.*?## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
@echo
@printf '\033[34mrecipes:\033[0m\n'
@grep -hE '^##.*$$' $(MAKEFILE_LIST) |\
awk 'BEGIN {FS = "## "}; /^## [a-zA-Z_-]/ {printf " \033[36m%s\033[0m\n", $$2}; /^## / {printf " %s\n", $$2}'