lrsppp
lrsppp

Reputation: 1413

Create one Makefile rule for several files

I would like to create just one Makefile rule instead of creating the same rule for different files of the same type.

Consider this small example where a files declare two Python-Notebooks and each of these Notebooks should be converted into a Python-Script (.py). This can be done with the command jupyter nbconvert --to script FILE. The Makefile I tried to create looks like this but is not working:

files = A.ipynb B.ipynb

all: convert

convert: $(files)   
     jupyter nbconvert --to script $<      # How to define $< as the changed file?

Now, whenever A.ipynb is changed, I would like the command

jupyter nbconvert --to script A.ipynb

to be executed. Can this be done within one simple rule as 'suggested' above? I can write a Makefile accomplishing the task but its longer and scales with the size of files:

all: A.py B.py

A.py: A.ipynb
    jupyter nbconvert --to script $<

B.py: B.ipynb
    jupyter nbconvert --to script $<

Upvotes: 0

Views: 273

Answers (1)

MadScientist
MadScientist

Reputation: 101081

This is what pattern rules are for:

all: A.py B.py

%.py: %.ipynb
        jupyter nbconvert --to script $<

You can also use static pattern rules which some prefer.

Upvotes: 2

Related Questions