2
\$\begingroup\$

This is my first shot at writing a makefile for a document generation system. Please identify any more ways to refine it. Some pointers on how it works:

  • The INPUT files initially are templates to be edited and are copied to the "src" directory upon first execution only.
  • The code at the beginning tests for an environment variable equal to the directory path containing the input templates.
  • The desired files will be in the "output" directory
  • The file formats used include mustache templates for asciidoctor (.mst.adoc), asciidoctor (.adoc), HTML, and PDF. One adoc file uses asciidoctor-reveal.js, an HTML slide generator.
# Check that given variables are set and all have non-empty values,
# die with an error otherwise.
#
# Params:
#   1. Variable name(s) to test.
#   2. (optional) Error message to print.
check_defined = \
    $(strip $(foreach 1,$1, \
        $(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
    $(if $(value $1),, \
      $(error Undefined $1$(if $2, ($2))))
#TEST ENVIRONMENT VARIABLES
$(call check_defined, TEACH_PROJ)

#DIRECTORIES:
PRJ=$(TEACH_PROJ)
OUT=../output/
BLD=../build/
ADC=asciidoctor
MST=mustache

#OUTPUT
HTM_NOT=$(OUT)notes.html
HTM_PRS=$(OUT)presentation.html
HTM_VOC=$(OUT)vocabulary.html
PDF_PRS=$(OUT)presentation.pdf
#INTERMEDIATE
ADC_PRS=$(BLD)presentation.adoc
ADC_VOC=$(BLD)vocabulary.adoc
#INPUT
YML_VOC=vocabulary.yml
FIL_GEM=Gemfile
MST_PRS=presentation.mst.adoc
MST_VOC=vocabulary.mst.adoc
#RECIPES
RCP_COPY=cp -an $(PRJ)$@
RCP_MKD=mkdir -p $@
RCP_ADC=$(ADC) $< -o $@
RCP_RJS=bundle exec $(ADC)-revealjs -o $@ $<
RCP_PDF=$(RCP_ADC) -r $(ADC)-pdf -b pdf -a pdf-stylesdir=$(PRJ) -a pdf-style=compact
RCP_MST=$(MST) $(YML_VOC) $< > $@

all: $(HTM_PRS) $(HTM_NOT) $(PDF_PRS) $(HTM_VOC)
$(HTM_PRS):  $(ADC_PRS) $(FIL_GEM)|$(OUT); $(RCP_RJS)
$(PDF_PRS): $(ADC_PRS)|$(OUT); $(RCP_PDF)

TARGETS=$(HTM_NOT) $(HTM_VOC)
$(HTM_NOT): $(ADC_PRS)|$(OUT)
$(HTM_VOC): $(ADC_VOC)|$(OUT)
$(TARGETS): ;$(RCP_ADC)

#build:
TARGETS=$(ADC_PRS) $(ADC_VOC)
$(ADC_PRS): $(MST_PRS) $(YML_VOC)|$(BLD)
$(ADC_VOC): $(MST_VOC) $(YML_VOC)|$(BLD)
$(TARGETS): ;$(RCP_MST)
#input
$(YML_VOC) $(MST_VOC) $(MST_PRS) $(FIL_GEM): ; $(RCP_COPY) .
$(OUT) $(BLD): ; $(RCP_MKD)
clean: ; rm -rf $(BLD) $(OUT)

```
\$\endgroup\$
  • \$\begingroup\$ Please note that YML and YAML have been around for about the same time, but are not the same. And the recommended file extension for YAML documents has been .yaml since 2006. It is unclear if you intended to use YAML_VOC=vocabulary.yaml or that you really use YML and is that tag yaml incorrect. \$\endgroup\$ – Anthon May 4 at 16:31
  • \$\begingroup\$ Thanks @Anthon that's good to know and I made the change. \$\endgroup\$ – KevinRethwisch May 5 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.