this post was submitted on 22 Oct 2023
12 points (87.5% liked)
General Programming Discussion
7806 readers
3 users here now
A general programming discussion community.
Rules:
- Be civil.
- Please start discussions that spark conversation
Other communities
Systems
Functional Programming
Also related
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I've written hundreds (thousands?) of GNU Makefiles over the past 30 years and never had a need to unconditionally run particular targets before all others. GNU Make utility is a rule-based language. I'd suggest what you're attempting to do is enforce an imperative programming language model onto a rule-based programming language model, which you're going to run into trouble trying to code in a different language model than the tool's native model.
Can you provide what you mean by check the environment, and why you'd need to do that before anything else?
For example, in the past I've want to determine if and which particular command was installed, so I have near the top of my
Makefile
:Using the
:=
operator with$(shell ...)
is a way to run a command while GNU Make is initially parsing your Makefile. Normally, using:=
assignment operator is antithetical to a rule-based language, so you want to limit its use as much as possible, but unusual exceptions can exist.I'm also unclear what you mean by "ensure variables are set". What kind of variables?
The above snippet shows how you can check if a makefile variable is set when the
Makefile
is first parsed, if not, declare an error and exit. (The same approach works for environment variables too.)Preparing a particular layout ahead of time is not the best approach. I'd suggest a given layout is nothing more than dependencies that should be declared as such.
Also, running specific targets or rules unconditionally can lead to trouble later as your Makefile grows up. You may eventually have additional targets that say provide information about the build's state or run checks or tests. You wouldn't want those targets necessarily to go off and build an entire tree of directories for you or take other unnecessary actions.
If you want to ensure certain directories are present, add those as dependencies for those targets with the
|
character. For example: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.
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
ifeq
s,shell
s anddefine
s. However, I wasn't satisfied w/ the results for a couple of reasons: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
andbmakelib.default-if-blank
as below.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.
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.
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.
You may like an approach I came up with some time ago.
In my
include
d file that's common among myMakefile
s:At the top of a
Makefile
that I want to ensure certain variables are set before it runs:I usually do these checks in sub-Makefiles to ensure someone didn't break the top level Makefile by not passing down a required macro.
To solve your DRY problem, you may not realize that you can generate target rules from built-in functions
eval
andforeach
and a user-defined new-line macro. Think of it like a preprocessor step.For example:
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.