-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
299 lines (234 loc) · 8.86 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
CATALA ?= catala
CLERK ?= clerk
BUILD = _build
# Tests use a separate build directory because they may need different flags
# (e.g. no --trace) and the objects would otherwise overwrite the main ones
BUILD_TESTS = _build/clerk_tests
VERSION = 0.10.0
#############################################
# Compiler and build tools commands and flags
#############################################
CATALA_INCLUDE := base_mensuelle_allocations_familiales:smic:prologue_france:prestations_familiales:allocations_familiales:aides_logement:droit_successions
CATALA_FLAGS ?= --trace
CLERK_FLAGS ?= --exe=$(CATALA)
CLERK_BUILD = $(CLERK) build --build-dir=$(BUILD) $(if $(CATALA_FLAGS),--catala-opts='$(CATALA_FLAGS)',) $(CLERK_FLAGS)
CLERK_TEST = $(CLERK) test $(CLERK_FLAGS) --build-dir=$(BUILD_TESTS)
OPAM = opam --cli=2.1
OCAMLC = ocamlfind ocamlc -package catala.runtime_ocaml
OCAMLOPT = ocamlfind ocamlopt -package catala.runtime_ocaml
JSOO_FLAGS = --pretty --source-map-inline --no-extern-fs
CATALA_DEPENDS = $(CATALA) depends -I $(CATALA_INCLUDE) --prefix=$(BUILD)
OCAML_FLAGS = -g
#######################
# List of build targets
#######################
NODOC ?=
DOC_SUFFIXES := $(if $(NODOC),,.tex .html)
LIB_SUFFIXES := $(DOC_SUFFIXES) .py .cma .cmxs .cmxa _schema.json
TARGET_LIBS = \
allocations_familiales/Allocations_familiales \
aides_logement/Aides_logement
TARGET_DOCS = \
polish_taxes/polish_taxes \
tutorial_en/tutorial_en \
tutoriel_fr/tutoriel_fr \
us_tax_code/us_tax_code
TARGETS := \
$(foreach lib,$(TARGET_LIBS),$(addprefix $(lib),$(LIB_SUFFIXES))) \
$(foreach lib,$(TARGET_DOCS),$(addprefix $(lib),$(DOC_SUFFIXES)))
FRENCH_LAW_TARGETS := \
french_law_python.tar.gz \
french-law_npm.tar.gz
all: \
$(addprefix $(BUILD)/,$(TARGETS)) \
$(addprefix $(BUILD)/,$(FRENCH_LAW_TARGETS)) \
catala-examples.install
$(BUILD):
mkdir -p $@
###############################
# Catala rules: literate output
###############################
# Note: this generates the literate output only for a single file at the moment,
# excluding modules (and this rule is only run for the listed targets). We could
# run `catala latex $(catala depends $^)` to include all dependencies.
$(BUILD)/%.tex: %.catala_??
$(CATALA) latex $^ $(CATALA_FLAGS) -o $@ --wrap
$(BUILD)/%.html: %.catala_??
$(CATALA) html $^ $(CATALA_FLAGS) -o $@ --wrap
##########################
# Rules: OCaml compilation
##########################
$(BUILD)/%.cmi: %.catala_??
$(CLERK_BUILD) $@
$(BUILD)/%.cmo: %.catala_??
$(CLERK_BUILD) $@
$(BUILD)/%.cmx: %.catala_??
$(CLERK_BUILD) $@
$(BUILD)/%.cmxs: %.catala_??
$(CLERK_BUILD) $@
$(BUILD)/%.exe: %.catala_??
$(CLERK_BUILD) $@
$(BUILD)/%.cma: $(BUILD)/%.cmi
$(OCAMLC) $(OCAML_FLAGS) \
$(shell $(CATALA_DEPENDS) --extension=cmo $(BUILD)/$*.catala_??) \
-intf-suffix .ml \
-a -o $@
$(BUILD)/%.cmxa: $(BUILD)/%.cmi
$(OCAMLOPT) $(OCAML_FLAGS) \
$(shell $(CATALA_DEPENDS) --extension=cmx $(BUILD)/$*.catala_??) \
-intf-suffix .ml \
-a -o $@
###############################################
# Rules: api_web and compilation of JS packages
###############################################
$(BUILD)/%_api_web.ml: %.catala_?? | $(BUILD)/%.cmo
$(CATALA) api_web -I $(CATALA_INCLUDE) $^ $(CATALA_FLAGS) -o $@
$(BUILD)/%-web.bc:
$(if $^,,$(error Building a js bundle ($@) requires expliciting the expected contents))
$(eval MLDEPS = $(shell $(CATALA_DEPENDS) --extension=cmo $^))
$(eval API = $(patsubst %,$(BUILD)/%_api_web.ml,$(basename $^)))
$(CLERK_BUILD) $(MLDEPS)
$(MAKE) $(API)
ocamlfind ocamlc -linkpkg \
-package catala.runtime_jsoo,js_of_ocaml-ppx \
$(OCAML_FLAGS) \
$(addprefix -I $(BUILD)/,$(subst :, , $(CATALA_INCLUDE))) \
$(MLDEPS) \
$(API) \
-o $@
$(BUILD)/%.js: $(BUILD)/%-web.bc
js_of_ocaml compile $(JSOO_FLAGS) \
$$(ocamlfind query -format "%+(jsoo_runtime)" -r catala.runtime_jsoo) \
$^ -o $@
$(BUILD)/french-law-web.bc: \
allocations_familiales/Allocations_familiales.catala_fr \
aides_logement/Aides_logement.catala_fr
$(BUILD)/%_schema.json: %.catala_??
$(if $(SCOPE),,$(error Building a json schema ($@) requires an explicit scope))
$(CATALA) json_schema -I $(CATALA_INCLUDE) $^ -o $@ --scope=$(SCOPE)
$(BUILD)/aides_logement/Aides_logement_schema.json: \
SCOPE=CalculetteAidesAuLogementGardeAlternée
$(BUILD)/allocations_familiales/Allocations_familiales_schema.json: \
SCOPE=InterfaceAllocationsFamiliales
###################################
# Rules for node package generation
###################################
define package_json
{
"name": "@catala-lang/$1",
"version": "$(VERSION)",
"description": "Examples of law texts compiled from catala code",
"main": "$1.js",
"repository": "github:CatalaLang/catala-examples",
"keywords": [ "catala", "law" ],
"author": "Denis Merigoux <[email protected]>",
"license": "Apache-2.0",
"bugs": { "url": "https://github.com/CatalaLang/catala/issues" },
"homepage": "https://github.com/CatalaLang/catala-examples"
}
endef
$(BUILD)/%_npm.tar.gz: $(BUILD)/%.js
@mkdir -p $(BUILD)/npm/$(*F)
cp $^ $(BUILD)/npm/$(*F)
$(file >$(BUILD)/$(*F)_npm.json,$(call package_json,$(*F)))
mv $(BUILD)/$(*F)_npm.json $(BUILD)/npm/$(*F)/package.json
tar czf $@ -C $(BUILD)/npm $(*F)
################################################
# Rule for python, and python project generation
################################################
$(BUILD)/%.py: %.catala_??
$(CLERK_BUILD) $@
define pyproject_toml
[project]
name = "$1"
description = "Examples of law texts compiled from catala code"
version = "$(VERSION)"
dependencies = [ "catala-runtime" ]
[project.urls]
"Homepage" = "https://github.com/CatalaLang/catala-examples/"
"Bug Tracker" = "https://github.com/CatalaLang/catala/issues"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["$1"]
endef
$(BUILD)/%_python.tar.gz:
$(if $^,,$(error Building a python package ($@) requires expliciting the expected contents))
$(eval PYDEPS = $(shell $(CATALA_DEPENDS) --extension=py $^))
@mkdir -p $(BUILD)/python/$(*F)/$(*F)
$(CLERK_BUILD) $(PYDEPS)
cp $(PYDEPS) $(BUILD)/python/$(*F)/$(*F)
echo '__all__ = [$(foreach f,$(PYDEPS),"$(notdir $(basename $f))",)]' \
>$(BUILD)/python/$(*F)/$(*F)/__init__.py
touch $(BUILD)/python/$(*F)/$(*F)/py.typed
$(file >$(BUILD)/$(*F).toml,$(call pyproject_toml,$(*F)))
mv $(BUILD)/$(*F).toml $(BUILD)/python/$(*F)/pyproject.toml
tar czf $@ -C $(BUILD)/python $(*F)
$(BUILD)/french_law_python.tar.gz: $(TARGET_LIBS:=.catala_??) | $(BUILD)
##############
# Installation
##############
INSTALL_lib = $(filter %.cma %.cmxa %.js %_schema.json, $(TARGETS))
INSTALL_libexec = $(filter %.cmxs, $(TARGETS))
INSTALL_doc = $(filter %.txt %.md %.tex %.html, $(TARGETS))
# Install specification file as expected by opam
catala-examples.install: $(TARGET_LIBS:=.catala_??)
@{ \
echo "lib: ["; \
echo " \"META\""; \
$(foreach lib,$(TARGET_LIBS),\
$(foreach ext,.a .ml .cma .cmxa _schema.json,\
echo " \"$(BUILD)/$(lib)$(ext)\" {\"$(lib)$(ext)\"}"; )\
$(foreach f,$(shell catala depends -I $(CATALA_INCLUDE) --extension= $(lib).catala_??),\
echo " \"$(BUILD)/$f.mli\" {\"$(dir $(lib))$(notdir $f.mli)\"}"; \
echo " \"$(BUILD)/$f.cmi\" {\"$(dir $(lib))$(notdir $f.cmi)\"}"; )\
) \
$(foreach f,$(FRENCH_LAW_TARGETS),echo " \"$(BUILD)/$f\" {\"$f\"}"; )\
echo "]"; \
echo "libexec: ["; \
$(foreach f,$(INSTALL_libexec),echo " \"$(BUILD)/$f\" {\"$f\"}";) \
echo "]"; \
echo "doc: ["; \
echo " \"LICENSE.txt\""; \
echo " \"README.md\""; \
$(foreach f,$(INSTALL_doc),echo " \"$(BUILD)/$f\" {\"$f\"}";) \
echo "]"; \
} >$@
INSTALL_PREFIX ?= $(realpath $(shell ocamlfind query catala)/../..)
local-install: all
$(if $(INSTALL_PREFIX),,$(error Could not determine INSTALL_PREFIX for installation))
opam-installer --prefix=$(INSTALL_PREFIX)
# Files to be installed are listed in the catala-examples.install file
install: all
@if [ x$$($(OPAM) --version) = "x2.1.5" ]; then \
$(OPAM) install . --working-dir; \
else \
$(OPAM) install . --working-dir --assume-built; \
fi
clean: .FORCE
rm -rf catala-examples.install _build
################################
# Running legislation unit tests
################################
binary-tests: \
$(BUILD)/allocations_familiales/tests/tests_allocations_familiales.exe \
$(BUILD)/aides_logement/tests/tests_calculette_globale.exe
$(^:= && ) echo "$(CURDIR): all binary tests passed"
test: .FORCE binary-tests
$(CLERK_TEST)
TEST_FLAGS_LIST = "" --lcalc,-O
testsuite: .FORCE binary-tests
@for F in $(TEST_FLAGS_LIST); do \
echo >&2; \
[ -z "$$F" ] || echo ">> RE-RUNNING TESTS WITH FLAGS: $$F" >&2; \
echo $(CLERK_TEST) --test-flags="$$F"; \
$(CLERK_TEST) --test-flags="$$F" || exit 1; \
done
pass_all_tests: testsuite
reset_all_tests:
$(CLERK_TEST) --reset
.FORCE:
.PHONY: all pass_all_tests reset_all_tests
# Disables make removing intermediate files
.SECONDARY: