### BUILD ###
caravel:
	./oasis_setup -build

### CONFIGURE ###
configure: config.log

config.log: oasis_setup
	./oasis_setup -configure

oasis_setup: setup.ml
	ocamlc.opt setup.ml -o oasis_setup
	rm setup.cm*

### PREPARE ###
prepare: gen-options setup.ml

OPTIONS_GEN_NATIVE := _build/tools/options/options.native
gen-options: ${OPTIONS_GEN_NATIVE} config.log
	find browser -name "*Descr.ml" -a ! -path '*_build' -prune -print0 \
	  | while read -d "$$(printf '\000')" ml_file; do \
	      base_name=$${ml_file%Descr.ml}; \
	      cmxs_file=$${base_name}Descr.cmxs; \
	      out_file=$${base_name}.ml; \
				ocamlbuild $${cmxs_file}; \
	      ${OPTIONS_GEN_NATIVE} _build/"$${cmxs_file}" "$${out_file}" "$${out_file}i"; \
	    done

${OPTIONS_GEN_NATIVE}: tools/options/options.ml config.log
	ocamlbuild -no-links tools/options/options.native

### OASIS ###
oasis: setup.ml

TRIG := | Native ->
PAT := byte (native acc_nopath)
REPL := native acc_nopath
setup.ml: _oasis
	oasis setup
	sed -e '/${TRIG}/ { N; s/${PAT}/${REPL}/}' setup.ml > setup_0.ml
	mv setup_0.ml setup.ml
	sed -e '/include/ d' _tags > tags_0
	mv tags_0 _tags

### CLEAN ###
clean:
	if [ -x oasis_setup ]; then \
	  ./oasis_setup -clean -quiet; \
	else \
	  rm -rf _build; \
	fi

### DISTCLEAN ###
distclean: clean
	if [ -x oasis_setup ]; then \
		./oasis_setup -distclean -quiet; \
		rm oasis_setup; \
	fi

### FULLCLEAN ###
fullclean: distclean fullclean-options
	rm -rf myocamlbuild.ml setup.ml _tags *.ba[0-9]*

fullclean-options:
	find browser -name "*Descr.ml" -print0 \
	  | while read -d "$$(printf '\000')" ml_file; do \
	      options_file=$${ml_file%Descr.ml}.ml; \
	      if [ -e $${options_file} ]; then rm $${ml_file%Descr.ml}.ml; fi; \
	    done

.PHONY: browser setup.ml
