1

I'm new to Makefile and just trying to change a file suffix. I want to convert files from one format to another eg .mov to .mp4

In bash I would do something like

out1 = "${input/mov/mp4}"

(is there a way to just use inline Bash syntax in Make?)

anyway it doesn't work. So I found these docs on Text functions https://www.gnu.org/software/make/manual/html_node/Text-Functions.html

$(var:suffix=replacement)

so trying to translate that into something I can use...

out1 := "${input/mov/mp4}"   ##bash syntax doesnt work
out2 := $(input:suffix=mp4)

check:
    @echo " input: ${input}"
    @echo "  out1: ${out1}"
    @echo "  out2: ${out2}"

but it doesn't work, the suffix is untouched

$ input=tutorial-v1.mov make check
 input: tutorial-v1.mov
  out1:
  out2: tutorial-v1.mov

I've seen other scripts using sed, but just for changing a suffix I just need to get the incantation syntax right.

second question, i dont s'pose there's a nicer way to pass params to a makefile script than have to define named env vars. eg just make cmd param1 param2 like any other shell script for $1 ?

2
  • I think you misunderstood the syntax; for your example it would be $(input:.mov=.mp4). See for example GNU make: 6.3.1 Substitution References Commented Oct 17, 2020 at 2:33
  • hmm that part you are right. but now i can't put the assignment block inside a make command, only once at the top of the file. so it's basically pointless if i need different suffixes :/
    – dcsan
    Commented Oct 17, 2020 at 4:57

2 Answers 2

1

My suggestion is to take both PARAM1 and PARAM2 as arguments and then use them in your target:

check:
    @echo "  input: ${INPUT}"
    @echo " param1: ${PARAM1}"
    @echo " param2: ${PARAM2}"
    @echo "    out: $(INPUT:$(PARAM1)=$(PARAM2))"

Here is an example of its usage:

INPUT=file.mkv PARAM1=.mkv PARAM2=.avi make check
  input: file.mkv
 param1: .mkv
 param2: .avi
    out: file.avi

Another option using file name functions

check:
    @echo " input: ${INPUT}"
    @echo "suffix: ${SUFFIX}"
    @echo "   out: $(addsuffix $(SUFFIX), $(basename $(INPUT)))"

Test it like this: INPUT=file.avi SUFFIX=.mp4 make check, it should produce the following:

 input: file.avi
suffix: .mp4
   out: file.mp4
0
out1 = $(patsubst %.mov,%.mp4,$(input))

Beware: like most stuff in makefiles, it doesn't work if you have spaces in filenames.

If the input is a space-separated list of filenames, so is the output. Each filename is individually converted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .