Search code examples
makefilegnu-makegnu

how to write a loop in gnu make that feed list to execuable


I have a lot of files that with .e extension, and I have a custom program that can process and convert them.

the custom program take input_file_name and output_file_name

./custom [input] output

It needs the [] around the input_file_name.

I used wildcard to get the name of all the .e files

FILES = $(wildcard *.e)

and I made it a phony target

.PHONY convert
convert:
  ./convert [$(FILES)] $(FILES).u

The issue that I have is that $(FILES) pass the whole list at once. For example, a.e b.e c.e. All the file's name are separated by one space.

a.e b.e c.e

I also tried foreach

$(foreach i, $(FILES), $(EXEC) [$(i)] $(basename .u))

It still feed the whole string to the program.

Please help. How to only use gnu make build in function to do this?


Solution

  • Make is a declarative language, not an imperative language. You're describing what you want to be done, but what you should be doing is describing the end-state you want.

    You want your convert target to produce all of the .u files. To do this, we can slightly adapt an example from the GNU make manual:

    U_FILES := $(patsubst %.e,%.u,$(wildcard *.e))
    

    Next, we tell make that running the convert target should produce all of the .u files:

    .PHONY convert
    convert: $(U_FILES)
    

    Next, we teach make how to produce a .u file from a .e file. I like to use a pattern rule for this.

    %.u: %.e
        ./custom [$<] $@
    

    Structuring your Makefile like this gives you some advantages. For example, you can use make -j to run your Makefile in parallel.