bahmanm,
@bahmanm@lemmy.ml avatar

Can you provide what you mean by check the environment, and why you’d need to do that before anything else?

One recent example is a makefile (in a subproject), w/ a dozen of targets to provision machines and run Ansible playbooks. Almost all the targets need at least a few variables to be set. Additionally, I needed any fresh invocation to clean the “build” directory before starting the work.

At first, I tried capturing those variables w/ a bunch of ifeqs, shells and defines. However, I wasn’t satisfied w/ the results for a couple of reasons:

  1. Subjectively speaking, it didn’t turn out as nice and easy-to-read as I wanted it to.
  2. I had to replicate my (admittedly simple) clean target as a shell command at the top of the file.

Then I tried capturing that in a target using bmakelib.error-if-blank and bmakelib.default-if-blank as below.


<span style="color:#323232;">##############
</span><span style="color:#323232;">
</span><span style="color:#323232;">.PHONY : ensure-variables
</span><span style="color:#323232;">
</span><span style="color:#323232;">ensure-variables : bmakelib.error-if-blank( VAR1 VAR2 )
</span><span style="color:#323232;">ensure-variables : bmakelib.default-if-blank( VAR3,foo )
</span><span style="color:#323232;">
</span><span style="color:#323232;">##############
</span><span style="color:#323232;">
</span><span style="color:#323232;">.PHONY : ansible.run-playbook1
</span><span style="color:#323232;">
</span><span style="color:#323232;">ansible.run-playbook1 : ensure-variables cleanup-residue | $(ansible.venv)
</span><span style="color:#323232;">ansible.run-playbook1 : 
</span><span style="color:#323232;">	...
</span><span style="color:#323232;">
</span><span style="color:#323232;">##############
</span><span style="color:#323232;">
</span><span style="color:#323232;">.PHONY : ansible.run-playbook2
</span><span style="color:#323232;">
</span><span style="color:#323232;">ansible.run-playbook2 : ensure-variables cleanup-residue | $(ansible.venv)
</span><span style="color:#323232;">ansible.run-playbook2 : 
</span><span style="color:#323232;">	...
</span><span style="color:#323232;">
</span><span style="color:#323232;">##############
</span>

But this was not DRY as I had to repeat myself.

That’s why I thought there may be a better way of doing this which led me to the manual and then the method I describe in the post.


running specific targets or rules unconditionally can lead to trouble later as your Makefile grows up

That is true! My concern is that when the number of targets which don’t need that initialisation grows I may have to rethink my approach.

I’ll keep this thread posted of how this pans out as the makefile scales.


Even though I’ve been writing GNU Makefiles for decades, I still am learning new stuff constantly, so if someone has better, different ways, I’m certainly up for studying them.

Love the attitude! I’m on the same boat. I could have just kept doing what I already knew but I thought a bit of manual reading is going to be well worth it.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming@lemmy.ml
  • everett
  • DreamBathrooms
  • Durango
  • cubers
  • modclub
  • magazineikmin
  • khanakhh
  • thenastyranch
  • rosin
  • Youngstown
  • slotface
  • InstantRegret
  • kavyap
  • ngwrru68w68
  • megavids
  • ethstaker
  • Leos
  • normalnudes
  • osvaldo12
  • GTA5RPClips
  • mdbf
  • tacticalgear
  • anitta
  • provamag3
  • tester
  • cisconetworking
  • JUstTest
  • lostlight
  • All magazines