Two more cheap gmake tricks: creating directories and printing variables

Here are another couple rule patterns that let “make” do its thing and you do yours.
Some targets (such as “install”) depend on target directories existing. Often you see:

install : $(things) ; mkdir -p $(DESTDIR); cp $^ $(DESTDIR)

A more make-ish way of expressing this is:

install : $(DESTDIR)/.. $(things) ; cp $(things) $(DESTDIR)/
%/.. : ;@mkdir -p $(@D)

The pattern rule means you don’t have to litter your makefile with prophylactic “mkdir -p” commands.

Another handy trick is:

%. : ;@echo '$($*)'

This is a handy way to check the value of any makefile variable, given all the nested and conditional expressions that contribute to its value. For example:

$ make CFLAGS.
Advertisements

About mischasan

I've had the privilege to work in a field where abstract thinking has concrete value. That applies at the macro level --- optimizing actions on terabyte database --- or the micro level --- fast parallel string searches in memory. You can find my documents on production-system radix sort (NOT just for academics!) and some neat little tricks for developers, on my blog https://mischasan.wordpress.com My e-mail sig (since 1976): Engineers think equations approximate reality. Physicists think reality approximates the equations. Mathematicians never make the connection.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

6 Responses to Two more cheap gmake tricks: creating directories and printing variables

  1. Mr Person says:

    Please don’t do this. It isn’t portable across makes and adds reliance on GNU products ;(

    • mischasan says:

      I sense a fairly hefty tangent looming. Sitting where I do, using Linux and FreeBSD and Windows, SQLServer and Postgres and what-have-you, with a mix of propietary, LGPL and GPL packages across them all, “reliance on GNU products” sounds not so much an argument as a partisan cry. You may have noticed I refer to “gmake tricks”, rather than “make”, as if gmake were the only make in town. Useful BSDmake-isms aren’t portable either. I appreciate how BSD keeps the Linux community honest, and keeps the Unix world from becoming a monoculture.

      How about if I make sure that my example commands also never use the word “make”, so it’s unambiguous? Otherwise, you’re just asking me to shut up and not post anything about something you don’t like. Deal?

      • eeh says:

        How about if I make sure that my example commands also never use the word “make”, so it’s unambiguous?

        I like this a lot. Sorry for the noise 🙂

  2. Mr Person says:

    ” to check the value of any makefile variable ”

    in BSD make you just use “make -V variable_name”

  3. mischasan says:

    Nice. Time for me to check the BSDMakefiles on my FreeBSD system 🙂 Have you posted any bsdmake tips and tricks somewhere?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s