Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive wildcards in GNU make?

It's been a while since I've used make, so bear with me...

I've got a directory, flac, containing .FLAC files. I've got a corresponding directory, mp3 containing MP3 files. If a FLAC file is newer than the corresponding MP3 file (or the corresponding MP3 file doesn't exist), then I want to run a bunch of commands to convert the FLAC file to an MP3 file, and copy the tags across.

The kicker: I need to search the flac directory recursively, and create corresponding subdirectories in the mp3 directory. The directories and files can have spaces in the names, and are named in UTF-8.

And I want to use make to drive this.

like image 871
Roger Lipscombe Avatar asked Mar 20 '10 13:03

Roger Lipscombe


People also ask

How do you use a wildcard in makefile?

If you want to do wildcard expansion in such places, you need to use the wildcard function, like this: $(wildcard pattern ...) This string, used anywhere in a makefile, is replaced by a space-separated list of names of existing files that match one of the given file name patterns.

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

How do you make a wild card?

In short, the three teams with the best record in each conference outside of the teams that won their division are given the Wild Card spots. This can get a little complicated if teams have the same record and need tiebreakers to decide the final one or two spots.


1 Answers

I would try something along these lines

FLAC_FILES = $(shell find flac/ -type f -name '*.flac') MP3_FILES = $(patsubst flac/%.flac, mp3/%.mp3, $(FLAC_FILES))  .PHONY: all all: $(MP3_FILES)  mp3/%.mp3: flac/%.flac     @mkdir -p "$(@D)"     @echo convert "$<" to "$@" 

A couple of quick notes for make beginners:

  • The @ in front of the commands prevents make from printing the command before actually running it.
  • $(@D) is the directory part of the target file name ($@)
  • Make sure that the lines with shell commands in them start with a tab, not with spaces.

Even if this should handle all UTF-8 characters and stuff, it will fail at spaces in file or directory names, as make uses spaces to separate stuff in the makefiles and I am not aware of a way to work around that. So that leaves you with just a shell script, I am afraid :-/

like image 77
ndim Avatar answered Oct 06 '22 05:10

ndim