Take advantage of eval in Makefile

This commit is contained in:
Yann Esposito (Yogsototh) 2023-04-20 00:26:51 +02:00
parent f8b5487e65
commit b524e83144
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646

View file

@ -8,7 +8,30 @@ CACHE_DIR ?= .cache
# we don't want to publish files in drafts # we don't want to publish files in drafts
NO_DRAFT := -not -path '$(SRC_DIR)/drafts/*' NO_DRAFT := -not -path '$(SRC_DIR)/drafts/*'
# we don't copy source files # we don't copy source files
NO_SRC_FILE := ! -name '*.org' NO_SRC_FILE := ! -name '*.org' ! -name '*.css'
define adv_rule
SRC_$(1) := $$(shell find $$(SRC_DIR) -type f $(2))
DST_$(1) := $$(patsubst $$(SRC_DIR)/%,$$(DST_DIR)/%,$$(SRC_$(1)))
$$(DST_DIR)/%$(4): $$(SRC_DIR)/%$(4) $$(ENV_VARS)
@mkdir -p "$$(dir $$@)"
$(3)
.PHONY: $(1)
$(1): $$(DST_$(1))
ALL += $(1)
endef
define rule
SRC_$(1) := $$(shell find $$(SRC_DIR) -type f $(2))
DST_$(1) := $$(patsubst $$(SRC_DIR)/%,$$(DST_DIR)/%,$$(SRC_$(1)))
$$(DST_DIR)/%.$(1): $$(SRC_DIR)/%.$(1) $$(ENV_VARS)
@mkdir -p "$$(dir $$@)"
$(3)
.PHONY: $(1)
$(1): $$(DST_$(1))
ALL += $(1)
endef
# Prevent path/nix bugs # Prevent path/nix bugs
ENV_VARS := ./engine/envvars.sh ENV_VARS := ./engine/envvars.sh
@ -21,23 +44,11 @@ envvars: $(ENV_VARS)
ALL += envvars ALL += envvars
# ASSETS # ASSETS
SRC_RAW_FILES := $(shell find $(SRC_DIR) -type f $(NO_DRAFT) $(NO_SRC_FILE)) $(eval $(call adv_rule,assets,$$(NO_DRAFT) $$(NO_SRC_FILE),cp "$$<" "$$@",))
DST_RAW_FILES := $(patsubst $(SRC_DIR)/%,$(DST_DIR)/%,$(SRC_RAW_FILES))
$(DST_DIR)/%: $(SRC_DIR)/% $(ENV_VARS)
@mkdir -p "$(dir $@)"
cp "$<" "$@"
.PHONY: assets
assets: $(DST_RAW_FILES)
ALL += assets
# CSS # CSS
SRC_CSS_FILES := $(shell find $(SRC_DIR) -type f -name '*.css') # $(info $(call rule,css,-name '*.css',minify "$$<" > "$$@"))
DST_CSS_FILES := $(patsubst $(SRC_DIR)/%,$(DST_DIR)/%,$(SRC_RAW_FILES)) $(eval $(call rule,css,-name '*.css',minify "$$<" > "$$@"))
$(DST_DIR)/%.css: $(SRC_DIR)/%.css $(ENV_VARS)
@mkdir -p "$(dir $@)"
minify "$<" > "$@"
css: $(DST_CSS_FILES)
ALL += css
# ORG -> HTML # ORG -> HTML
EXT ?= .org EXT ?= .org
@ -135,11 +146,11 @@ ALL += $(GMI_INDEX)
gmi-index: $(GMI_INDEX) gmi-index: $(GMI_INDEX)
# RSS # RSS
GEM_ATOM := $(DST_DIR)/gem-atom.xml GMI_ATOM := $(DST_DIR)/gem-atom.xml
MK_GEMINI_ATOM := engine/mk-gemini-atom.sh MK_GEMINI_ATOM := engine/mk-gemini-atom.sh
$(GEM_ATOM): $(DST_GMI_FILES) $(MK_GEMINI_ATOM) $(GEM_ATOM): $(DST_GMI_FILES) $(MK_GEMINI_ATOM)
$(MK_GEMINI_ATOM) $(MK_GEMINI_ATOM)
ALL += $(GEM_ATOM) ALL += $(GMI_ATOM)
.PHONY: gmi-atom .PHONY: gmi-atom
gmi-atom: $(GMI_ATOM) gmi-atom: $(GMI_ATOM)
@ -147,29 +158,27 @@ gmi-atom: $(GMI_ATOM)
gemini: $(DST_GMI_FILES) $(GMI_INDEX) $(GEM_ATOM) gemini: $(DST_GMI_FILES) $(GMI_INDEX) $(GEM_ATOM)
# Images # Images
SRC_IMG_FILES ?= $(shell find $(SRC_DIR) -type f -name "*.jpg" -or -name "*.jpeg" -or -name "*.gif" -or -name "*.png")
DST_IMG_FILES ?= $(patsubst $(SRC_DIR)/%,$(DST_DIR)/%, $(SRC_IMG_FILES))
OPTIM_IMG := engine/optim-img.sh OPTIM_IMG := engine/optim-img.sh
$(DST_DIR)/%.jpg: $(SRC_DIR)/%.jpg $(OPTIM_IMG) define img
@mkdir -p $(dir $@) SRC_IMG_$(1) ?= $$(shell find $$(SRC_DIR) -type f -name "*.$(1)")
$(OPTIM_IMG) "$<" "$@" DST_IMG_$(1) ?= $$(patsubst $$(SRC_DIR)/%,$$(DST_DIR)/%,$$(SRC_IMG_$(1)))
$$(DST_DIR)/%.$(1): $$(SRC_DIR)/%.$(1) $$(OPTIM_IMG)
@mkdir -p $$(dir $$@)
$$(OPTIM_IMG) "$$<" "$$@"
.PHONY: $(1)
$(1): $$(DST_IMG_$(1))
ALL += $(1)
endef
$(DST_DIR)/%.jpg: $(SRC_DIR)/%.jpeg $(OPTIM_IMG) $(info $(call img,jpg))
@mkdir -p $(dir $@) $(eval $(call img,jpg))
$(OPTIM_IMG) "$<" "$@" $(eval $(call img,jpeg))
$(eval $(call img,gif))
$(DST_DIR)/%.gif: $(SRC_DIR)/%.gif $(OPTIM_IMG) $(eval $(call img,png))
@mkdir -p $(dir $@)
$(OPTIM_IMG) "$<" "$@"
$(DST_DIR)/%.png: $(SRC_DIR)/%.png $(OPTIM_IMG)
@mkdir -p $(dir $@)
$(OPTIM_IMG) "$<" "$@"
.PHONY: img .PHONY: img
img: $(DST_IMG_FILES) img: jpg jpeg gif png
ALL += $(DST_IMG_FILES)
# DEPLOY # DEPLOY
.PHONY: site .PHONY: site