DEV Community

Discussion on: How To Make A Makefile

Collapse
 
jmcp profile image
James McPherson

Nice intro article, thankyou.

I rewrote the core Solaris build system (6000+ Makefiles, lead a team of 4 for 3 years to deliver it), so my make knowledge is skewed towards the make which comes with Solaris Studio, but there are lots of similarities with the GNU version.

First comment: you define your c++ compiler as

CXX=clang++ -std=c++11
FLAGS=-Wall -Wextra -Werror -pedantic -c -g

I'd have written this differently:

CXX=clang++
CXXFLAGS= -std=c++11 -Wall -Wextra -Werror -pedantic -c -g

For C code, you'd use CC and CFLAGS, you'd also specify LDFLAGS for the linker, etc etc.

When it comes to your bin/boot target, I'd have written that differently too - to make the subdir dependency explicit:

bin:
    @mkdir -p bin

bin/boot: bin
    @(curlcmd....)
    @chmod 0555 $@

I did a lot of gnarly things with Solaris' make pattern matching rules
(for which I sometimes think I still need therapy). Case in point: Solaris has a closed subrepo for delivering binary blobs to partner companies. In order to remove a lot of extra work, I rewrote a few specific install rules to put those blobs in the right place in one hit:

INS.rename= $(INS.file) ; $(MV) $(@D)/$(<F) $@ ; \
    $(MV) $(@D:$(ROOT)/%=$(CLOSEDROOT)/%)/$(<F) \
    $(@D:$(ROOT)/%=$(CLOSEDROOT)/%)/$(@F)

Line noise making things faster, ftw.

The last comment I'll make is that the % character only matches the same pattern in the target and dependency - it's a rather simplistic regex

$(BUILDDIR)/%.o: $(SRCDIR)/%.c

We had to work around this all the time, and that limitation alone added several months to the project while we figured out the most efficient way of doing so.

Collapse
 
deciduously profile image
Ben Lovy

Ah, thanks so much for the tips! That's one issue with such a flexible tool - a bunch of ways to do something can mean it's hard to find the way to do it best. I can see at a glance why both your suggestions are superior but also see why I got where I did and stopped thinking. I'll keep this in mind in the future.

That's a terrifying recipe. I love it.

I did not know that about % - thanks for pointing out the distinction!