-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
227 lines (190 loc) · 7.8 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
################################################################################
# Makefile
################################################################################
unexport LDFLAGS
unexport CFLAGS
unexport CXXFLAGS
######################################################################
# template to generate the targets
######################################################################
define pandoc_template =
NOTES_TARGETS += $$(BUILD_DIR)/$(1).pdf
$$(BUILD_DIR)/$(1).pdf:
$(PANDOC) -f markdown -t pdf -o $$@ \
--resource-path="$$(NOTES_DIR)/$(1)" \
"$$(NOTES_DIR)/$(1)/README.md"
NOTES_TARGETS += $$(BUILD_DIR)/$(1).epub
$$(BUILD_DIR)/$(1).epub:
$(PANDOC) -f markdown -t epub -o $$@ \
--resource-path="$$(NOTES_DIR)/$(1)" \
"$$(NOTES_DIR)/$(1)/README.md"
NOTES_TARGETS += $$(BUILD_DIR)/$(1).txt
$$(BUILD_DIR)/$(1).txt:
$(PANDOC) -f markdown -t plain -o $$@ \
--resource-path="$$(NOTES_DIR)/$(1)" \
"$$(NOTES_DIR)/$(1)/README.md"
NOTES_TARGETS += $$(BUILD_DIR)/$(1).html
$$(BUILD_DIR)/$(1).html:
$(PANDOC) -f markdown -t html -o $$@ \
--resource-path="$$(NOTES_DIR)/$(1)" \
"$$(NOTES_DIR)/$(1)/README.md"
endef
################################################################################
# GLOBAL Configuration
################################################################################
LOCAL_DIR ?= $(shell pwd)
PRIV_DIR ?= priv
CSRC_DIR ?= c_src
CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)
PROJECT ?= $(notdir $(BASEDIR))
PROJECT := $(strip $(PROJECT))
BUILD_DIR ?= _build/notes
NOTES_DIR ?= notes
NOTES = $(shell ls $(NOTES_DIR) | grep -E "^[0-9]+-")
PANDOC_OPTS = -C
PANDOC = pandoc $(PANDOC_OPTS)
################################################################################
# ERTS Configuration
################################################################################
ifdef ASDF_DIR
ASDF_ERLANG_VERSION ?= $(shell asdf current erlang | awk '{print $$2}')
ERTS_INCLUDE_DIR ?= $(ASDF_DIR)/installs/erlang/$(ASDF_ERLANG_VERSION)/usr/include
ERL_INTERFACE_LIB_DIR ?= $(ASDF_DIR)/installs/erlang/$(ASDF_ERLANG_VERSION)/usr/lib
ERL_INTERFACE_INCLUDE_DIR ?= $(ASDF_DIR)/installs/erlang/$(ASDF_ERLANG_VERSION)/usr/include
else
ERL_BIN_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts/erts-~ts/bin/\", [code:root_dir(), erlang:system_info(version)])." -s init stop)
ERTS_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts/erts-~ts/include/\", [code:root_dir(), erlang:system_info(version)])." -s init stop)
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, lib)])." -s init stop)
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, include)])." -s init stop)
endif
################################################################################
# COZODB Configuration
################################################################################
COZO_REPOSITORY = https://github.com/cozodb/cozo/releases/download
COZO_VERSION ?= 0.7.5
COZO_LIB_CHECKSUM ?= 2d4ac784ce730180b49d0a9ff09a2cada197802379e15a7d2a7cd03f89316702
# Architecture Auto configuration
UNAME_M := $(shell uname -m)
ifeq ($(UNAME_M), x86_64)
COZO_LIB_ARCH = x86_64
else ifeq ($(UNAME_M), aarch64)
COZO_LIB_ARCH = aarch64
else ifeq ($(UNAME_M), arm64)
COZO_LIB_ARCH = aarch64
else ifeq ($(UNAME_M), armv7l)
COZO_LIB_ARCH = armv7
endif
# Operating System Auto Configuration
UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
COZO_LIB_OS = apple-darwin
COZO_LIBC_EXT = dylib
LDFLAGS = -dynamiclib -flat_namespace -undefined suppress
else ifeq ($(UNAME_SYS), Linux)
COZO_LIB_OS = unknown-linux-gnu
COZO_LIBC_EXT = so
LDFLAGS = -shared
endif
# Configure checksum based on the os/arch
ifeq ($(UNAME_SYS)-$(UNAME_M), unknown-linux-gnu-x86_64)
COZO_LIB_CHECKSUM ?= 472921fc7a944fe5bdf040aa154cafdd6b23ce6401b4ad96abb9a41747c84df6
endif
# Cozo Path Building
COZO_LIB_PREFIX = libcozo_c-$(COZO_VERSION)-$(COZO_LIB_ARCH)-$(COZO_LIB_OS)
COZO_LIB_NAME = libcozo_c-$(COZO_VERSION)-$(COZO_LIB_ARCH)-$(COZO_LIB_OS).$(COZO_LIBC_EXT)
COZO_LIB_PACKAGE = $(COZO_LIB_NAME).gz
COZO_LIB_URL = $(COZO_REPOSITORY)/v$(COZO_VERSION)/$(COZO_LIB_PACKAGE)
COZO_HEADER_NAME = cozo_c.h
COZO_HEADER_CHECKSUM = 2cc2d84f626825000294f138983aa8fb374be6ab1c045d0ce1a1748753a37243
COZO_HEADER_URL = $(COZO_REPOSITORY)/v$(COZO_VERSION)/$(COZO_HEADER_NAME)
################################################################################
# COMPILER Configuration
################################################################################
CC_FLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes -I $(ERTS_INCLUDE_DIR) -L $(ERL_INTERFACE_LIB_DIR)
CC_INCLUDE = -I $(ERL_INTERFACE_INCLUDE_DIR) -I $(LOCAL_DIR)/c_src -I $(LOCAL_DIR)/$(PRIV_DIR)
LDLIBS = -L $(LOCAL_DIR)/c_src -L $(LOCAL_DIR)/$(PRIV_DIR)
CC_OPTS ?= $(CC_INCLUDE) $(LDLIBS) -Werror -lei -lcozo_c -fPIC $(CC_FLAGS) $(LDFLAGS)
################################################################################
# Default Targets
################################################################################
TARGETS = $(CSRC_DIR)/cozo_c.h \
$(PRIV_DIR)/libcozo_c.so \
$(PRIV_DIR)/cozo_nif.so
# Used to start rebar3 and erlang
LD_LIBRARY_PATH ?= $(LOCAL_DIR)/$(PRIV_DIR)
DYLD_FALLBACK_LIBRARY_PATH ?= $(LD_LIBRARY_PATH) # for macOS
CFLAG_RUNTIME_LIBRARY_PATH ?= $(LOCAL_DIR)/$(PRIV_DIR)
ENV_BOOTSTRAP ?= LD_LIBRARY_PATH=$(LD_LIBRARY_PATH) CFLAG_RUNTIME_LIBRARY_PATH=$(CFLAG_RUNTIME_LIBRARY_PATH)
################################################################################
# Makefile body
################################################################################
.PHONY += help
help:
@echo "Usage: make [all|deps|compile|test|cover|dialyzer|doc|hex|shell|clean]"
######################################################################
# create the build directory if not present
######################################################################
$(BUILD_DIR):
mkdir -p $@
######################################################################
# generate all templates based on notes directory name
######################################################################
$(foreach note,$(NOTES),$(eval $(call pandoc_template,$(note))))
################################################################################
# Main Targets
################################################################################
$(CSRC_DIR)/cozo_c.h:
wget -qO $@ $(COZO_HEADER_URL)
$(PRIV_DIR):
mkdir $@
$(PRIV_DIR)/$(COZO_LIB_PACKAGE): | $(PRIV_DIR)
wget -qO $@ $(COZO_LIB_URL)
$(PRIV_DIR)/$(COZO_LIB_NAME): $(PRIV_DIR)/$(COZO_LIB_PACKAGE)
cd $(PRIV_DIR) && gunzip --keep $(COZO_LIB_PACKAGE)
$(PRIV_DIR)/libcozo_c.so: $(PRIV_DIR)/$(COZO_LIB_NAME)
cd $(PRIV_DIR) && ln $(COZO_LIB_NAME) libcozo_c.so
$(PRIV_DIR)/cozo_nif.so: $(CSRC_DIR)/cozo_c.h $(PRIV_DIR)/libcozo_c.so
ifeq ($(UNAME_SYS), Darwin)
@# Required for dlopen() to find libcozo_c on macOS.
@# Changes the dynamic shared library install name and rpath recorded in the
@# binary
install_name_tool -id \
$(LOCAL_DIR)/$(PRIV_DIR)/libcozo_c.so \
$(LOCAL_DIR)/$(PRIV_DIR)/libcozo_c.so
endif
$(CC) c_src/cozo_nif.c $(CC_OPTS) -o $(@)
.PHONY += all
all: deps compile test cover dialyzer doc
.PHONY += deps
deps: $(PRIV_DIR)/cozo_nif.so
.PHONY += compile
compile:
$(ENV_BOOTSTRAP) rebar3 compile
.PHONY += test
test:
$(ENV_BOOTSTRAP) RUST_BACKTRACE=1 rebar3 ct
.PHONY += doc
doc:
$(ENV_BOOTSTRAP) rebar3 ex_doc skip_deps=true
.PHONY += notes
notes: $(BUILD_DIR) $(NOTES_TARGETS)
.PHONY += shell
shell:
$(ENV_BOOTSTRAP) rebar3 shell
.PHONY += cover
cover:
$(ENV_BOOTSTRAP) RUST_BACKTRACE=1 rebar3 cover
.PHONY += dialyzer
dialyzer:
$(ENV_BOOTSTRAP) RUST_BACKTRACE=1 rebar3 dialyzer
.PHONY += hex
hex:
$(ENV_BOOTSTRAP) rebar3 hex build
.PHONY += clean
clean: clean-notes
-rm $(TARGETS)
.PHONY += clean-notes
clean-notes:
-rm $(NOTES_TARGETS)
.PHONY: $(.PHONY)