<commit_before>#
# Usage:
#    make all  # not implemented yet, will build everything
#    make full    # build *.log.js, *.unlog.js, *.token.js
#    make lineof  # build lineof.js
#    make interp  # build interp.js
#
# requires: opam switch 4.02.1; eval `opam config env`

# TODO: test/lambda is not longer supported

###############################################################
# Paths

STDLIB_DIR  := stdlib_ml
TESTS_DIR   := tests
JSREF_DIR   := jsref
JSREF_PATH  := $(TESTS_DIR)/$(JSREF_DIR)
TESTS_ML    := $(wildcard $(TESTS_DIR)/*.ml)
JSREF_ML    := $(wildcard $(TESTS_DIR)/$(JSREF_DIR)/*.ml) 
JSREF_MLI   := $(wildcard $(TESTS_DIR)/$(JSREF_DIR)/*.mli)

# ASSEMBLY_JS must respect dependencies order
ASSEMBLY_JS := \
	Ascii.log.js \
	BinNums.log.js \
	Bool0.log.js \
	Datatypes.log.js \
	Fappli_IEEE_bits.log.js \
	Fappli_IEEE.log.js \
	LibBool.log.js \
	LibReflect.log.js \
	LibOperation.log.js \
	LibList.log.js \
	Heap.log.js \
	Shared.log.js \
	LibString.log.js \
	LibOption.log.js \
	JsNumber.log.js \
	JsSyntax.log.js \
	JsSyntaxAux.log.js \
  Translate_syntax.js \
	List0.log.js \
	JsSyntaxInfos.log.js \
	JsCommon.log.js \
	JsCommonAux.log.js \
	JsPreliminary.log.js \
	JsInit.log.js \
  Prheap.js \
	LibTactics.log.js \
	LibProd.log.js \
	LibFunc.log.js \
	JsInterpreterMonads.log.js \
	JsInterpreter.log.js \
	Specif.log.js
ASSEMBLY_JS := $(STDLIB_DIR)/stdlib.js $(addprefix tests/jsref/,$(ASSEMBLY_JS));


###############################################################
# Global options

.PHONY: all clean .log.js .unlog.js .token.js
   # all gen log unlog 

all: everything

# Do not delete intermediate files.
.SECONDARY:


###############################################################
# Tools

CC          := ocamlc -c
OCAMLDEP    := ocamldep -one-line
DEPSED      := sed -e "s/cmx/cmi/; s/cmx/cmi/g"
OCAMLBUILD := ocamlbuild -j 4 -classic-display -use-ocamlfind -X tests -X $(STDLIB_DIR)

GENERATOR := ./main.byte

LINEOF := ./lineof.byte


###############################################################
# Dependencies

ifeq ($(filter clean%,$(MAKECMDGOALS)),)
-include $(TESTS_ML:.ml=.ml.d)
-include $(JSREF_ML:.ml=.ml.d)
-include $(JSREF_MLI:.mli=.mli.d)
endif


###############################################################
# Rules

##### Compilation of STDLIB

$(STDLIB_DIR)/stdlib.cmi: $(STDLIB_DIR)/stdlib.mli
	$(CC) $<

##### Rule for binaries

%.byte: *.ml _tags
	$(OCAMLBUILD) $@

##### Rule for dependencies

tests/%.mli.d: tests/%.mli
	$(OCAMLDEP) -I $(<D) $< | $(DEPSED) > $@

tests/%.ml.d: tests/%.ml
	$(OCAMLDEP) -I $(<D) $< | $(DEPSED) > $@

##### Rule for cmi

tests/%.cmi: tests/%.ml main.byte stdlib 
	./main.byte -mode cmi -I $(<D) $<

##### Custome cmi rules for compilation of mli files without ml source

$(JSREF_PATH)/Translate_syntax.cmi: $(JSREF_PATH)/Translate_syntax.mli $(JSREF_PATH)/JsSyntax.cmi stdlib
	ocamlc -I $(JSREF_PATH) -I stdlib_ml -open Stdlib $<

$(JSREF_PATH)/Prheap.cmi: $(JSREF_PATH)/Prheap.mli stdlib $(JSREF_PATH)/JsSyntax.cmi
	ocamlc -I $(JSREF_PATH) -I stdlib_ml -open Stdlib $<

##### Rule for log/unlog/token

tests/%.log.js: tests/%.ml main.byte stdlib tests/%.cmi
	./main.byte -mode log -I $(<D) $<

tests/%.unlog.js: tests/%.ml main.byte stdlib tests/%.cmi 
	./main.byte -mode unlog -I $(<D) $<

tests/%.token.js: tests/%.ml main.byte stdlib tests/%.cmi  
	./main.byte -mode token -I $(<D) $<

##### Rule for lineof.js

$(JSREF_PATH)/lineof.js: lineof.byte $(JSREF_ML:.ml=.token.js)
	./lineof.byte -o $@ $(JSREF_ML:.ml=.token.js)

##### Rule for assembly.js

# later add as dependencies the unlog files: $(JSREF_ML:.ml=.unlog.js) 
$(JSREF_PATH)/assembly.js: assembly.byte $(ASSEMBLY_JS)
	./assembly.byte -o $@ $(ASSEMBLY_JS)
# -stdlib $(STDLIB_DIR)/stdlib.js 

# maybe useful

tests/jsref/%.log.js: tests/jsref/%.ml 


#####################################################################
# Short targets

everything: gen assembly lineof

main: main.byte

cmi: $(JSREF_ML:.ml=.cmi) $(JSREF_MLI:.mli=.cmi) 

gen: $(JSREF_ML:.ml=.log.js) $(JSREF_ML:.ml=.unlog.js) $(JSREF_ML:.ml=.token.js)

log: $(TESTS_ML:.ml=.log.js) $(TESTS_ML:.ml=.token.js)

unlog: $(JSREF_ML:.ml=.unlog.js) 

lineof: $(JSREF_PATH)/lineof.js

assembly: $(JSREF_PATH)/assembly.js

stdlib: $(STDLIB_DIR)/stdlib.cmi



#####################################################################
# Clean

DIRTY_EXTS := cmi,token.js,log.js,unlog.js,d,ml.d,mli.d

clean_genjs:
	rm -f $(JSREF_PATH)/lineof.js
	rm -f $(JSREF_PATH)/assembly.js

clean_tests:
	bash -c "rm -f $(TESTS_DIR)/*.{$(DIRTY_EXTS)}"
	bash -c "rm -f $(TESTS_DIR)/$(JSREF_DIR)/*.{$(DIRTY_EXTS)}"

clean_stdlib:
	rm -f $(STDLIB_DIR)/*.cmi

clean: clean_genjs clean_tests clean_stdlib
	rm -rf _build
	rm -f *.native *.byte



#####################################################################
# Extra

debug: main.d.byte

native: _tags
	$(OCAMLBUILD) main.native

##### Shorthand
<commit_msg>Missed junkfiles in `make clean`<commit_after>#
# Usage:
#    make all  # not implemented yet, will build everything
#    make full    # build *.log.js, *.unlog.js, *.token.js
#    make lineof  # build lineof.js
#    make interp  # build interp.js
#
# requires: opam switch 4.02.1; eval `opam config env`

# TODO: test/lambda is not longer supported

###############################################################
# Paths

STDLIB_DIR  := stdlib_ml
TESTS_DIR   := tests
JSREF_DIR   := jsref
JSREF_PATH  := $(TESTS_DIR)/$(JSREF_DIR)
TESTS_ML    := $(wildcard $(TESTS_DIR)/*.ml)
JSREF_ML    := $(wildcard $(TESTS_DIR)/$(JSREF_DIR)/*.ml) 
JSREF_MLI   := $(wildcard $(TESTS_DIR)/$(JSREF_DIR)/*.mli)

# ASSEMBLY_JS must respect dependencies order
ASSEMBLY_JS := \
	Ascii.log.js \
	BinNums.log.js \
	Bool0.log.js \
	Datatypes.log.js \
	Fappli_IEEE_bits.log.js \
	Fappli_IEEE.log.js \
	LibBool.log.js \
	LibReflect.log.js \
	LibOperation.log.js \
	LibList.log.js \
	Heap.log.js \
	Shared.log.js \
	LibString.log.js \
	LibOption.log.js \
	JsNumber.log.js \
	JsSyntax.log.js \
	JsSyntaxAux.log.js \
  Translate_syntax.js \
	List0.log.js \
	JsSyntaxInfos.log.js \
	JsCommon.log.js \
	JsCommonAux.log.js \
	JsPreliminary.log.js \
	JsInit.log.js \
  Prheap.js \
	LibTactics.log.js \
	LibProd.log.js \
	LibFunc.log.js \
	JsInterpreterMonads.log.js \
	JsInterpreter.log.js \
	Specif.log.js
ASSEMBLY_JS := $(STDLIB_DIR)/stdlib.js $(addprefix tests/jsref/,$(ASSEMBLY_JS));


###############################################################
# Global options

.PHONY: all clean .log.js .unlog.js .token.js
   # all gen log unlog 

all: everything

# Do not delete intermediate files.
.SECONDARY:


###############################################################
# Tools

CC          := ocamlc -c
OCAMLDEP    := ocamldep -one-line
DEPSED      := sed -e "s/cmx/cmi/; s/cmx/cmi/g"
OCAMLBUILD := ocamlbuild -j 4 -classic-display -use-ocamlfind -X tests -X $(STDLIB_DIR)

GENERATOR := ./main.byte

LINEOF := ./lineof.byte


###############################################################
# Dependencies

ifeq ($(filter clean%,$(MAKECMDGOALS)),)
-include $(TESTS_ML:.ml=.ml.d)
-include $(JSREF_ML:.ml=.ml.d)
-include $(JSREF_MLI:.mli=.mli.d)
endif


###############################################################
# Rules

##### Compilation of STDLIB

$(STDLIB_DIR)/stdlib.cmi: $(STDLIB_DIR)/stdlib.mli
	$(CC) $<

##### Rule for binaries

%.byte: *.ml _tags
	$(OCAMLBUILD) $@

##### Rule for dependencies

tests/%.mli.d: tests/%.mli
	$(OCAMLDEP) -I $(<D) $< | $(DEPSED) > $@

tests/%.ml.d: tests/%.ml
	$(OCAMLDEP) -I $(<D) $< | $(DEPSED) > $@

##### Rule for cmi

tests/%.cmi: tests/%.ml main.byte stdlib 
	./main.byte -mode cmi -I $(<D) $<

##### Custome cmi rules for compilation of mli files without ml source

$(JSREF_PATH)/Translate_syntax.cmi: $(JSREF_PATH)/Translate_syntax.mli $(JSREF_PATH)/JsSyntax.cmi stdlib
	ocamlc -I $(JSREF_PATH) -I stdlib_ml -open Stdlib $<

$(JSREF_PATH)/Prheap.cmi: $(JSREF_PATH)/Prheap.mli stdlib $(JSREF_PATH)/JsSyntax.cmi
	ocamlc -I $(JSREF_PATH) -I stdlib_ml -open Stdlib $<

##### Rule for log/unlog/token

tests/%.log.js: tests/%.ml main.byte stdlib tests/%.cmi
	./main.byte -mode log -I $(<D) $<

tests/%.unlog.js: tests/%.ml main.byte stdlib tests/%.cmi 
	./main.byte -mode unlog -I $(<D) $<

tests/%.token.js: tests/%.ml main.byte stdlib tests/%.cmi  
	./main.byte -mode token -I $(<D) $<

##### Rule for lineof.js

$(JSREF_PATH)/lineof.js: lineof.byte $(JSREF_ML:.ml=.token.js)
	./lineof.byte -o $@ $(JSREF_ML:.ml=.token.js)

##### Rule for assembly.js

# later add as dependencies the unlog files: $(JSREF_ML:.ml=.unlog.js) 
$(JSREF_PATH)/assembly.js: assembly.byte $(ASSEMBLY_JS)
	./assembly.byte -o $@ $(ASSEMBLY_JS)
# -stdlib $(STDLIB_DIR)/stdlib.js 

# maybe useful

tests/jsref/%.log.js: tests/jsref/%.ml 


#####################################################################
# Short targets

everything: gen assembly lineof

main: main.byte

cmi: $(JSREF_ML:.ml=.cmi) $(JSREF_MLI:.mli=.cmi) 

gen: $(JSREF_ML:.ml=.log.js) $(JSREF_ML:.ml=.unlog.js) $(JSREF_ML:.ml=.token.js)

log: $(TESTS_ML:.ml=.log.js) $(TESTS_ML:.ml=.token.js)

unlog: $(JSREF_ML:.ml=.unlog.js) 

lineof: $(JSREF_PATH)/lineof.js

assembly: $(JSREF_PATH)/assembly.js

stdlib: $(STDLIB_DIR)/stdlib.cmi



#####################################################################
# Clean

DIRTY_EXTS := cmi,token.js,log.js,unlog.js,d,ml.d,mli.d,js.pre

clean_genjs:
	rm -f $(JSREF_PATH)/lineof.js
	rm -f $(JSREF_PATH)/assembly.js

clean_tests:
	bash -c "rm -f $(TESTS_DIR)/*.{$(DIRTY_EXTS)}"
	bash -c "rm -f $(TESTS_DIR)/$(JSREF_DIR)/*.{$(DIRTY_EXTS)}"

clean_stdlib:
	rm -f $(STDLIB_DIR)/*.cmi

clean: clean_genjs clean_tests clean_stdlib
	rm -rf _build
	rm -f *.native *.byte



#####################################################################
# Extra

debug: main.d.byte

native: _tags
	$(OCAMLBUILD) main.native

##### Shorthand

{'fraction_non_alphanumeric': 0.27243133265513736, 'fraction_numerical': 0.0014242115971515768, 'mean_word_length': 3.9352409638554215, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4074649', 'n_tokens_mistral': 4095, 'n_tokens_neox': 3840, 'n_words': 730}