this post was submitted on 22 Oct 2023
12 points (87.5% liked)

General Programming Discussion

7810 readers
8 users here now

A general programming discussion community.

Rules:

  1. Be civil.
  2. Please start discussions that spark conversation

Other communities

Systems

Functional Programming

Also related

founded 5 years ago
MODERATORS
 

cross-posted from: https://lemmy.ml/post/6856563

When writing a (GNU) Makefile, there are times when you need a particular target(s) to be run before anything else. That can be for example to check the environment, ensure variables are set or prepare a particular directory layout.

... take advantage of GNU Make's mechanism of includeing and makeing makefiles which is described in details in the manual:

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago

To solve your DRY problem, you may not realize that you can generate target rules from built-in functions eval and foreach and a user-defined new-line macro. Think of it like a preprocessor step.

For example:

# This defines a new-line macro.  It must have two blank lines.
define nl


endef

# Generate two rules for ansible playbooks:
$(eval $(foreach v,1 2,\
.PHONY : ansible.run-playbook$v $(nl)\
\
ansible.run-playbook$v : ensure-variables cleanup-residue | $$(ansible.venv)$(nl)\
ansible.run-playbook$v :;\
	... $(nl)\
))

I winged it a bit for you, but hopefully I got it right, or at least right enough you get what I'm doing with this technique.