--- Makefile	2024-04-18 11:42:42
+++ Makefile	2024-06-06 10:39:46
@@ -51,7 +51,8 @@
 COMPIL_DATE := $(shell date +'%Y-%m-%d')
 COMMIT_DATE := $(shell git show -s --date=short --pretty=format:'%cd')
 COMMIT_ID := $(shell git rev-parse --short HEAD)
-COMMIT_MSG := $(shell git log -1 --pretty="%s%n%n%b" | sed 's/"/\\"/g')
+COMMIT_TITLE := $(shell git log -1 --no-merges --pretty="%s" | sed "s/\"/\\\"/g")
+COMMIT_COMMENT:= $(shell git log -1 --no-merges --pretty="%b" | sed "s/\"/\\\"/g")
 BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
 VERSION := $(shell awk -F\" '/er =/ {print $$2}' lib/version.txt)
 SOURCE := $(shell git remote get-url origin | sed -n 's|^.*m/\([^/]\+/[^/.]\+\)\(.git\)\?|\1|p')
@@ -65,13 +66,37 @@
 	@printf "let commit_date = \"$(COMMIT_DATE)\"\n" >> $@
 	@printf "let compil_date = \"$(COMPIL_DATE)\"\n" >> $@
 	@printf "Generating $@… Done.\n"
-.PHONY: lib/version.ml
 
+# Patch/unpatch files for campl5 >= 8.03
+CAMLP5_VERSION := $(shell camlp5 -version 2>/dev/null || echo 0)
+CAMLP5_MAJOR := $(shell echo $(CAMLP5_VERSION) | cut -d '.' -f 1)
+CAMLP5_MINOR := $(shell echo $(CAMLP5_VERSION) | cut -d '.' -f 2)
+
+patch_files:
+	@if [ "$(CAMLP5_VERSION)" != 0 ] && [ $(CAMLP5_MAJOR) -eq 8 ] && [ $(CAMLP5_MINOR) -ge 3 ]; then \
+	  printf "\nPatching bin/ged2gwb/dune.in and ged2gwb.ml for camlp5 version $(CAMLP5_VERSION) (>= 8.03.00)… Done.\n"; \
+	  perl -pi.bak -e 's|\(preprocess \(action \(run camlp5o pr_o.cmo pa_extend.cmo q_MLast.cmo %\{input-file\}\)\)\)|\(preprocess \(action \(run not-ocamlfind preprocess -package camlp5.extend,camlp5.quotations,camlp5.pr_o -syntax camlp5o %\{input-file\}\)\)\)|' bin/ged2gwb/dune.in; \
+	  perl -0777 -pi.bak -e 's/(; Token\.tok_comm = None)(\n  \})/$$1\n  ; Token.kwds = Hashtbl.create 10$$2/' bin/ged2gwb/ged2gwb.ml; \
+	fi
+
+unpatch_files:
+	@if [ -f bin/ged2gwb/dune.in.bak ] && [ -f bin/ged2gwb/ged2gwb.ml.bak ]; then \
+	  printf "Restoring original patched files… Done.\n"; \
+	  mv bin/ged2gwb/dune.in.bak bin/ged2gwb/dune.in; \
+	  mv bin/ged2gwb/ged2gwb.ml.bak bin/ged2gwb/ged2gwb.ml; \
+	fi
+
+BUILD = dune build -p geneweb --profile $(DUNE_PROFILE)
+UNPATCH = $(MAKE) --no-print-directory unpatch_files
+
 info:
-	@printf "Building \033[1;37mGeneweb $(VERSION)\033[0m with $(OCAMLV).\n\n"
-	@printf "Repository \033[1;37m$(SOURCE)\033[0m. Branch \033[1;37m$(BRANCH)\033[0m.\n\n"
-	@printf "Last commit \033[1;37m$(COMMIT_ID)\033[0m with message “\033[1;37m%s\033[0m”.\n" '$(subst ','\'',$(COMMIT_MSG))'
-	@printf "\n\033[1;37mGenerating configuration files\033[0m\n"
+	@printf "Building \033[1;1mGeneweb $(VERSION)\033[0m with $(OCAMLV).\n\n"
+	@printf "Repository \033[1;1m$(SOURCE)\033[0m. Branch \033[1;1m$(BRANCH)\033[0m. "
+	@printf "Last commit \033[1;1m$(COMMIT_ID)\033[0m message:\n\n"
+	@printf "\033[1;1m  %s\033[0m\n" '$(subst ','\'',$(COMMIT_TITLE))'
+	@printf "  %s\n" '$(subst ','\'',$(COMMIT_COMMENT))' | fmt -w 80
+	@printf "\033[1;1mGenerating configuration files\033[0m\n"
+.PHONY: patch_files unpatch_files info
 
 GENERATED_FILES_DEP = \
 	dune-workspace \
@@ -100,36 +125,43 @@
 	test/dune \
 
 generated: $(GENERATED_FILES_DEP)
+	@printf "Done.\n"
 
-install uninstall fmt build distrib: info $(GENERATED_FILES_DEP)
+fmt build gwd install uninstall: info patch_files generated
 
 fmt: ## Format Ocaml code
 ifneq ($(OS_TYPE),Win)
-	@printf "\n\033[1;37mOcamlformat\033[0m\n"
-	dune build @fmt --auto-promote
+	@printf "\n\033[1;1mOcamlformat\033[0m\n"
+	dune build @fmt --auto-promote ; $(UNPATCH)
 endif
 
 # [BEGIN] Installation / Distribution section
 
 build: ## Build the geneweb package (libraries and binaries)
-build:
-	@printf "\n\033[1;37mBuilding executables\033[0m\n"
-	dune build -p geneweb --profile $(DUNE_PROFILE)
+	@printf "\n\033[1;1mBuilding executables\033[0m\n"
+	@$(BUILD) ; $(UNPATCH)
+	@printf "Done."
 
+gwd: ## Build ondy gwd/gwc executables
+	@printf "\n\033[1;1mBuilding only gwd and gwc executables\033[0m\n"
+	@dune build bin/gwd bin/gwc --profile $(DUNE_PROFILE) ; $(UNPATCH)
+	@printf "Done."
+
 install: ## Install geneweb using dune
-install:
-	dune build @install --profile $(DUNE_PROFILE)
+	dune build @install --profile $(DUNE_PROFILE) ; $(UNPATCH)
 	dune install
 
 uninstall: ## Uninstall geneweb using dune
-uninstall:
-	dune build @install --profile $(DUNE_PROFILE)
+	dune build @install --profile $(DUNE_PROFILE) ; $(UNPATCH)
 	dune uninstall
 
-distrib: build ## Build the project and copy what is necessary for distribution
-distrib:
-	$(RM) -r $(DISTRIB_DIR)
-	@printf "\n\033[1;37mCreating distribution directory\033[0m\n"
+distrib: info ## Build the project and copy what is necessary for distribution
+	@$(MAKE) --no-print-directory patch_files generated
+	@printf "\n\033[1;1mBuilding executables.\n\033[0m"
+	@$(BUILD) || { $(UNPATCH) && exit 1; }
+	@printf "Done."
+	@$(RM) -r $(DISTRIB_DIR)
+	@printf "\n\033[1;1mCreating distribution directory\033[0m\n"
 	mkdir $(DISTRIB_DIR)
 	mkdir -p $(DISTRIB_DIR)/bases
 	cp CHANGES $(DISTRIB_DIR)/CHANGES.txt
@@ -153,7 +185,7 @@
 	mkdir $(DISTRIB_DIR)/gw
 	cp etc/a.gwf $(DISTRIB_DIR)/gw/.
 	echo "-setup_link" > $(DISTRIB_DIR)/gw/gwd.arg
-	@printf "\n\033[1;37m└ Copy binaries in $(DISTRIB_DIR)/gw/\033[0m\n"
+	@printf "\n\033[1;1m└ Copy binaries in $(DISTRIB_DIR)/gw/\033[0m\n"
 	cp $(BUILD_DISTRIB_DIR)connex/connex.exe $(DISTRIB_DIR)/gw/connex$(EXT)
 	cp $(BUILD_DISTRIB_DIR)consang/consang.exe $(DISTRIB_DIR)/gw/consang$(EXT)
 	cp $(BUILD_DISTRIB_DIR)fixbase/gwfixbase.exe $(DISTRIB_DIR)/gw/gwfixbase$(EXT)
@@ -163,11 +195,14 @@
 	cp $(BUILD_DISTRIB_DIR)gwc/gwc.exe $(DISTRIB_DIR)/gw/gwc$(EXT)
 	cp $(BUILD_DISTRIB_DIR)gwd/gwd.exe $(DISTRIB_DIR)/gw/gwd$(EXT)
 	cp $(BUILD_DISTRIB_DIR)gwdiff/gwdiff.exe $(DISTRIB_DIR)/gw/gwdiff$(EXT)
-	if test -f $(BUILD_DISTRIB_DIR)gwrepl/gwrepl.bc ; then cp $(BUILD_DISTRIB_DIR)gwrepl/gwrepl.bc $(DISTRIB_DIR)/gw/gwrepl$(EXT); fi
+	@if test -f $(BUILD_DISTRIB_DIR)gwrepl/gwrepl.bc ; then \
+	  printf "cp %s %s\n" "$(BUILD_DISTRIB_DIR)gwrepl/gwrepl.bc" "$(DISTRIB_DIR)/gw/gwrepl$(EXT)"; \
+	  cp $(BUILD_DISTRIB_DIR)gwrepl/gwrepl.bc $(DISTRIB_DIR)/gw/gwrepl$(EXT); \
+	fi
 	cp $(BUILD_DISTRIB_DIR)gwu/gwu.exe $(DISTRIB_DIR)/gw/gwu$(EXT)
 	cp $(BUILD_DISTRIB_DIR)setup/setup.exe $(DISTRIB_DIR)/gw/gwsetup$(EXT)
 	cp $(BUILD_DISTRIB_DIR)update_nldb/update_nldb.exe $(DISTRIB_DIR)/gw/update_nldb$(EXT)
-	@printf "\n\033[1;37m└ Copy templates in $(DISTRIB_DIR)/gw/\033[0m\n"
+	@printf "\n\033[1;1m└ Copy templates in $(DISTRIB_DIR)/gw/\033[0m\n"
 	cp -R hd/* $(DISTRIB_DIR)/gw/
 	mkdir $(DISTRIB_DIR)/gw/setup
 	cp bin/setup/intro.txt $(DISTRIB_DIR)/gw/setup/
@@ -177,23 +212,27 @@
 	cp bin/setup/lang/*.htm $(DISTRIB_DIR)/gw/setup/lang/
 	cp bin/setup/lang/lexicon.txt $(DISTRIB_DIR)/gw/setup/lang/
 	cp bin/setup/lang/intro.txt $(DISTRIB_DIR)/gw/setup/lang/
-	@printf "\n\033[1;37m└ Copy plugins in $(DISTRIB_DIR)/gw/plugins\033[0m\n"
+	@printf "\n\033[1;1m└ Copy plugins in $(DISTRIB_DIR)/gw/plugins\033[0m\n"
 	mkdir $(DISTRIB_DIR)/gw/plugins
-	for P in $(shell ls plugins); do \
-		if [ -f $(BUILD_DIR)/plugins/$$P/plugin_$$P.cmxs ] ; then \
-			mkdir $(DISTRIB_DIR)/gw/plugins/$$P; \
-			cp $(BUILD_DIR)/plugins/$$P/plugin_$$P.cmxs $(DISTRIB_DIR)/gw/plugins/$$P/; \
-			if [ -d plugins/$$P/assets ] ; then \
-				cp -R $(BUILD_DIR)/plugins/$$P/assets $(DISTRIB_DIR)/gw/plugins/$$P/; \
-			fi; \
-			if [ -f $(BUILD_DIR)/plugins/$$P/META ] ; then \
-				cp $(BUILD_DIR)/plugins/$$P/META $(DISTRIB_DIR)/gw/plugins/$$P/; \
-			fi; \
-		fi; \
+	@for P in $(shell ls plugins); do \
+	  if [ -f $(BUILD_DIR)/plugins/$$P/plugin_$$P.cmxs ] ; then \
+	    mkdir $(DISTRIB_DIR)/gw/plugins/$$P; \
+	    printf "cp %s %s\n" "$(BUILD_DIR)/plugins/$$P/plugin_$$P.cmxs" "$(DISTRIB_DIR)/gw/plugins/$$P/"; \
+	    cp $(BUILD_DIR)/plugins/$$P/plugin_$$P.cmxs $(DISTRIB_DIR)/gw/plugins/$$P/; \
+	    if [ -d plugins/$$P/assets ] ; then \
+	      printf "cp -R %s %s\n" "$(BUILD_DIR)/plugins/$$P/assets" "$(DISTRIB_DIR)/gw/plugins/$$P/"; \
+	      cp -R $(BUILD_DIR)/plugins/$$P/assets $(DISTRIB_DIR)/gw/plugins/$$P/; \
+	    fi; \
+	    if [ -f $(BUILD_DIR)/plugins/$$P/META ] ; then \
+	      printf "cp %s %s\n" "$(BUILD_DIR)/plugins/$$P/META" "$(DISTRIB_DIR)/gw/plugins/$$P/"; \
+	      cp $(BUILD_DIR)/plugins/$$P/META $(DISTRIB_DIR)/gw/plugins/$$P/; \
+	    fi; \
+	  fi; \
 	done
-	@printf "\033[1;37mBuild complete.\033[0m\n"
-	@printf "You can launch Geneweb with “\033[1;37mcd $(DISTRIB_DIR)\033[0m” followed by “\033[1;37mgw/gwd$(EXT)\033[0m”.\n"
-.PHONY: install uninstall distrib
+	@printf "Done.\n\n\033[1;1mDistribution complete.\033[0m\n"
+	@$(UNPATCH)
+	@printf "You can launch Geneweb with “\033[1;1mcd $(DISTRIB_DIR)\033[0m” followed by “\033[1;1mgw/gwd$(EXT)\033[0m”.\n"
+.PHONY: fmt install uninstall distrib
 
 # [END] Installation / Distribution section
 
