SS891
SS891

Reputation: 39

Replacing a string with underscore character in GNU Make

We are facing a slightly weird problem in GNU Make.

In one of the part Makefile, we try to modify a string in order to get right filename. So "dummy_1_.pl" is to converted to "dummy_1.pl".

We tried to use following way :-

MY_STRING := dummy_1_.pl
UNDPL := _.pl
DPL := .pl

$(subst $(UNDPL), $(DPL), $(MY_STRING) )

Surprisingly it doesn't work. We can replace ".", "pl", ".pl" etc all this way. However just "" or "." or "_.pl" etc replacement strings starting with an underscore doesn't seem to work.

Is underscore a special character in Make. Are we missing something basic here... We are at GNU Make 3.81

Any help/thought is highly appreciated. Thanks in advance!.


EDIT :- The problem was posted in short for focused discussion. It seems, the details are necessary. This applies to a pattern rule as below. There's a OUT_V_FILES target that contains *blah_cpu.v *foo_gpu.v etc various target files. (The special string "cpu", "gpu" etc are part of a list.) We want to derive blah.pl, foo.pl respectively as input file for the rule.

DEVICES := cpu gpu memc dram

MY_STRING := $$(foreach dev,$(DEVICES),$$(subst $$(dev),$(NOTHING),$$(notdir %.pl)))
NOTHING :=
UDOT := _.
DOT := .

$(OUT_V_FILES) : %.v : $(subst $(UDOT),$(DOT),S(MY_STRING)) Makefile
      #Body of rule+++++++++

Upvotes: 0

Views: 1177

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6387

In your updated example, it looks like you have some issues when setting MY_STRING. If you correct it to have the proper filename(s), you get:

MY_STRING := $$(foreach dev,$(DEVICES),$$(subst $$(dev),$(NOTHING),$$(notdir %.pl)))
$(info 1. MY_STRING=$(MY_STRING))
MY_STRING := dummy_1_.pl
$(info 2. MY_STRING=$(MY_STRING))

NOTHING :=
UDOT := _.
DOT := .

MY_NEW_STRING=$(subst $(UDOT),$(DOT),$(MY_STRING))
$(info MY_NEW_STRING=$(MY_NEW_STRING))

gives

1. MY_STRING=$(foreach dev,cpu gpu memc,$(subst $(dev),,$(notdir %.pl)))
2. MY_STRING=dummy_1_.pl
MY_NEW_STRING=dummy_1.pl

Upvotes: 1

G.M.
G.M.

Reputation: 12879

There are two problems here. 1) you never assign the result of the $(subst ... invocation and 2) you have to be aware that whitespace is significant within the context of string manipulation functions. With that in mind your makefile code should be something like...

MY_STRING := $(subst $(UNDPL),$(DPL),$(MY_STRING))

Upvotes: 1

Related Questions