Makefile to process a folder's contents - makefile

I have a folder with some markdown files I want to process using pandoc and output them into another folder for a Jekyll site. I am attempting to use the following Makefile:
MARKDOWN = $(wildcard *.md)
jekyll-build : _posts/$(MARKDOWN)
bundle exec jekyll build
_posts/%.md : source/%.md
pandoc -s -t markdown-citations -o $# $<
However, instead of looking for the contents of the _posts folder, make is looking for any *.md files in the current working directory (where I have an index.md file), then complains it can't find _posts/index.md. From this question I gather it's the expected behavior of pattern rules, so my question is: how do I get make to look for all existing files in the source folder and run the recipe on each one of them?

MARKDOWN = $(wildcard source/*.md)
POSTS := $(patsubst source/%,_posts/%, $(MARKDOWN))
jekyll-build : $(POSTS)
...
_posts/%.md : source/%.md
...

Related

Makefile - pdf generation using LaTeX

I wrote a simple makefile to generate pdf using latex and I succeeded.
However, There are 2 point that irritates me:
when I run make the output PDF is in the same folder where the makefile is written and I want to change that
I want to copy the pdf file generated to another file with another name but I don't know how to do that
Here is a snippet of my code:
# Generating the file
$(FILE).pdf: $(IMAGES) $(PACKAGES) $(COMMON_TEX_SOURCES) $(FILE_TEX_SOURCES) $(FILE_IMAGES)
latexmk -lualatex $(FILE)
##############################################################################
#rule to generate file
file: $(FILE).pdf
Your help is appreciated
To make the target go into a another folder, change the target path:
some/dir/$(FILE).pdf: $(IMAGES) ...
To copy the file from one place to another:
some/other/dir/$(FILE).pdf: some/dir/$(FILE).pdf
cp $< $#

GNU Make - build only out-of-date file in directory

Pretty new to GNU Make. This is a less complex example of something more general I have been trying to get to work.
I have many input files that have similar name format .txt, and I have a shell script that will take the input file and generate an output of the same name but with a different extension .wc. I have written the following Make file.
# name of dependencies
SRC = $(wildcard *.txt)
# get name of targets (substitute .wc for .txt)
TAR = $(SRC:.txt=.wc)
all: $(TAR)
%.wc: %.txt
sh word_count.sh $<
This runs fine, and will generate all the .wc output files. However, if I modify one of the input(dependency) files, they are all rebuilt. So the question is; what is the best way to get GNU Make to only process the modified .txt files in the directory?

Makefile: for each .x file, create a .html file

(Similar question here). For every .x file in the directory I want to run a command to generate an HTML file. The command already works fine, it's the Makefile that I'm having trouble with. Here's what I came up with:
all: $(OBJECTS)
OBJECTS := $(patsubst %.x,%.html,$(wildcard *.x))
%.html: %.x
generate $< > $#
The OBJECTS variable is meant to contain all html files I need to generate. Invoking make states nothing to be done for 'all', and there are no HTML files already in the directory.
Turns out make is sensitive to the order of your variable definitions. The Makefile works when the first two lines are switched!

How to write a Make rule for files of certain type in a directory tree?

Imagine a directory tree (which might be more than one level deep) containing several Markdown files. A PDF version of each file exist in the same directory and must be updated each time the corresponding Markdown file is updated. What rule must be written in a single Makefile in the root directory of this tree to achieve this?
I am looking a for a solution where files can be added or removed from the directory tree without a need for updating the Makefile.
Assumptions:
all markdown files follow a certain pattern in their name; for example they end with a .md postfix.
GNU Make is being used.
You can use $(shell find) to find files recursively. For example:
markdown := $(shell find . -name '*.md')
all: $(patsubst %.md, %.pdf, $(markdown))
%.pdf: %.md
pandoc -o $# $<

Strip prefix of dependency in makefile rule target

I have a makefile which is designed to get some datasets from the web and unzip them into folders (given by the archive names). Unfortunately, the archives on the website are grouped within a folder structure. For example, if I want dataset.tar.gz, which is part of set DIR1, I call wget with: www.example.com/DIR1/dataset.tar.gz.
This is then downloaded to dataset.tar.gz, which I can then extract with tar -xzf. My makefile for this is currently:
URL = www.example.com
FILES = DIR1/something.url DIR2/another.url
FOLDERS = $(notdir $(patsubst %.url, %, $(FILES)) )
%.url: # do nothing
%.tar.gz : %.url
wget -q $(URL)/$<
% : %.tar.gz
tar -xzf $<
all: $(FOLDERS)
Unfortunately, the target for the rule %.tar.gz : %.url does not resolve to (e.g.) something.tar.gz, but to DIR1/something.tar.gz, even though it produces the expected file.
This causes problems when I rerun the makefile, as it does not detect something.tar.gz (as it is looking for DIR1/something.tar.gz), and redownloads the dataset, wasting time and bandwidth.
Is there any way of stripping prefixes/directories from makefile targets, i.e. removing DIR1 from the rule %.tar.gz : %.url, in order that the makefile correctly checks to see if it is satisfied?

Resources