How can I use wildcards to remove repetition in my makefile?

145 Views Asked by At

I've got a makefile for Advent of Code that I've decided to do in chicken scheme.

However, it's starting to grow, and I'd like to remove the repetition.

Here's a section of what I've got so far, but how do I make it so it can automatically find all days instead of 2 entries per day and an ever increasing main target?

all: advent2015

# advent years - this is ever increasing with the *.so targets
advent2015: $(ADVENTS_SRC)2015/advent2015.scm \
 aoc2015day01.so aoc2015day02.so
    $(CSC) $(INCLUDE_FLAGS) $< -d3 -O2 -compile-syntax -o $@

# individual days - how do i write a single rule that works for all days?
aoc2015day01.so: $(ADVENTS_SRC)2015/aoc2015day01.scm aoc-files.so
    $(CSC) $(INCLUDE_FLAGS) $(LIBFLAGS) $< -j aoc2015day01 -emit-types-file aoc2015day01.types -o $@
    $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) aoc2015day01.import.scm

aoc2015day01.import.so: aoc2015day01.so
    $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) aoc2015day01.import.scm

# day02...
aoc2015day02.so: $(ADVENTS_SRC)2015/aoc2015day02.scm aoc-files.so
    $(CSC) $(INCLUDE_FLAGS) $(LIBFLAGS) $< -j aoc2015day02 -emit-types-file aoc2015day02.types -o $@
    $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) aoc2015day02.import.scm

aoc2015day02.import.so: aoc2015day02.so
    $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) aoc2015day02.import.scm

I tried a small change to the advent2015 target by specifying a wildcard in the so files it is using, but make completely skipped generating the targets.

2

There are 2 best solutions below

3
MadScientist On BEST ANSWER

No problem:

DAYS := 01 02 03 04 <...> 24 25

all: advent2015

advent2015: $(ADVENTS_SRC)2015/advent2015.scm \
     $(foreach D,$(DAYS),aoc2015day$(D).so)
        $(CSC) $(INCLUDE_FLAGS) $< -d3 -O2 -compile-syntax -o $@

%.so: $(ADVENTS_SRC)2015/%.scm aoc-files.so
        $(CSC) $(INCLUDE_FLAGS) $(LIBFLAGS) $< -j $* -emit-types-file $*.types -o $@
        $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) $*.import.scm

%.import.so: %.so
        $(CSC) $(INCLUDE_FLAGS) $(IMPORTFLAGS) $*.import.scm

I'm not sure these commands are actually correct but they're what you provided above, so you can fix them if not.

1
sjamaan On

You may find the csm egg useful. I know it means getting rid of your makefile, but it should do the trick much more easily.

Alternatively, beaker provides ways of building based on .egg files, like those used by chicken-install.