I am processing some files and want to at one point create two categories depending on the filename so I can compare the two. Is this possible in a makefile?
%.output1: %.input ifneq (,$(findstring filename1,$(echo $<))) mv $<.output1 [email protected] endif %.output2: %.input ifneq (,$(findstring filename2,$(echo $<))) mv $<.output2 [email protected] endif %.output_final: %.output1 %.output2 do_something
I think there is two things wrong with this code:
You have tab-indented the
ifneq line so make doesn't consider it a make directive and is considering it a shell command and attempting to pass it to the shell to execute (hence the shell error you removed in your recent edit).
Use spaces (or no indentation) on that line to have make process it correctly. That being said having done that you cannot use
$< in the comparison as it will not be set at that point.
$(echo) is also not a make function. You have mixed/confused processing times. You cannot combine make and shell operations that way. (Not that you need
echo there to begin with.)
If you want the comparison to happen at shell time then do not use make constructs and instead use shell constructs:
%.output1: %.input if [ filename1 = '$<' ]; then mv $<.output1 [email protected] fi
Though that is also incorrect as
$< is
%.input and
%.output1 for whatever stem matched the
%. That rule should probably look more like this (though I'm having trouble understanding what you are even trying to have this rule do so I may have gotten this wrong).
%.output1: %.input # If the stem that matched the '%' is equal to 'filename1' if [ filename1 = '$*' ]; then # Then copy the prerequisite/input file to the output file name. cp $< [email protected] fi
I'm not sure I understand your second question point. The
% in a single rule will always match the same thing but between rules it can differ.
This
%.output_final: %.output1 %.output2 target will map the target file
foo.output_final to the prerequisite files
foo.output1 and
foo.output2. But will also map any other
*.output_final file to appropriately matching prerequisite files.