scc
scc

Reputation: 10716

Makefile process several files

I want to parse several json files, get their contents, prepend/append some text and save the new string in another file. Currently I have this which works for a single file:

#set variables for shared libs
SHAREDFILE=$(SERVERPATH)/xx/mod/supervisor/cfg/prod/config.json
JSONCONF = `cat "$(SHAREDFILE)"`

share:
    @echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
    @echo "parseConfig($(JSONCONF));" > $(CLIENTPATH)/public/js/shared/config.json

Since from what i understand variables can only be set outside rules how would i go about doing this for a list of files?

Update: the files to be copied can be in separate directories but they should all be copied to the same target directory.

Upvotes: 0

Views: 102

Answers (1)

Beta
Beta

Reputation: 99172

Based on a guess about your list of files:

SHAREDFILES = foo.json bar.json baz.json

TARGETS = $(addprefix $(CLIENTPATH)/public/js/shared/, $(SHAREDFILES))

share: $(TARGETS)

$(TARGETS) : $(CLIENTPATH)/public/js/shared/%.json : $(SERVERPATH)/xx/mod/supervisor/cfg/prod/%.json
    @echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
    @echo "parseConfig(`cat $<`);" > $@

EDIT:
If the source files are in several directories, but the targets all go into the same directory... There are several ways to do it. This is probably the best:

SHAREDFILES = some/path/foo.json another/path/bar.json yet/another/path/baz.json

TARGETDIR = $(CLIENTPATH)/public/js/shared
TARGETS = $(addprefix $(TARGETDIR)/, $(notdir $(SHAREDFILES)))

vpath %.json $(dir $(SHAREDFILES))

share: $(TARGETS)

$(TARGETS) : $(TARGETDIR)/%.json : %.json
    @echo "Sharing node-client code in ${PROJECT}.- ENV $(ENV)..."
    @echo "parseConfig(`cat $<`);" > $@

Upvotes: 2

Related Questions