gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > [makefile] changing spaces in macro in underscore

#172595 - vuurrobin - Thu Feb 18, 2010 7:19 pm

hello,

in a makefile I have, I have a macro that may contain spaces and I want to replace those spaces with underscores. I already tried the following:

Code:
$(subst  ,_,$(strip $(NAME))$(strip $(EXTENTION)))

$(subst \ ,_,$(strip $(NAME))$(strip $(EXTENTION)))

$(subst " ",_,$(strip $(NAME))$(strip $(EXTENTION)))

$(subst ' ',_,$(strip $(NAME))$(strip $(EXTENTION)))


but none of them works. I also tried it with foreach, but no luck.
google is also of little help.


does anybody know how to do this?
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#172599 - zelbo - Fri Feb 19, 2010 1:24 am

Can't you just load it in a text editor use the built in search and replace? if you don't want to risk changing anything else in the file, what i would do is copy that macro into a temp file, do the s&r there, then copy it back in.

#172601 - vuurrobin - Fri Feb 19, 2010 2:19 am

the value isn't hardcoded into the file, so I can't change it manually, and using a texteditor while compiling the program isn't really an option either.


its not really that big of a deal, but it would be nice to have and it seems something that should be possible in a makefile.
_________________
my blog:
http://vuurrobin.100webcustomers.com/

#172605 - zelbo - Fri Feb 19, 2010 3:32 am

Over my head. I should have known it wouldn't be that simple.

#172609 - Cearn - Fri Feb 19, 2010 1:56 pm

One way to do it is to use sed:
Code:
`echo $(NAME) | sed -e 's/[ ]/_/g'`

You only need to use the backquotes if you intend to create variables from $(NAME). In rule-command, you can leave them out.

There is a way to make it work using subst, but it's kinda convoluted (see the manual, section 6.2 and seach for "nullstring" to see how you can get a space-string).

#172610 - vuurrobin - Fri Feb 19, 2010 3:03 pm

thanks!

I used the subst way because then it wouldn't be dependend of sed and the unix pipe thingy which may or may not work on different environments (?). the subst way was easy enough and will work with pretty much all forms of make.

this is what it ended up to be:

Code:
NULLSTRING   :=   
SPACE      :=   $(NULLSTRING) #don't remove this, or you will mess the space var up.

OUTPUT   :=   $(subst $(SPACE),_,$(strip $(NAME))$(strip $(EXTENTION)))

_________________
my blog:
http://vuurrobin.100webcustomers.com/