help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I call "notdir" on a file name that contains spaces?


From: John Graham-Cumming
Subject: Re: How can I call "notdir" on a file name that contains spaces?
Date: Fri, 03 Mar 2006 20:04:24 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040208 Thunderbird/0.5 Mnenhy/0.6.0.104

Eric Hanchrow wrote:
Here's an example makefile that doesn't do what I want.

    name   := I have a space/x

    worse:
            @echo input  is $(name)
            @echo output is $(notdir $(name))

    # I see

    # input  is I have a space/x
    # output is I have a x


    # But I wish I'd have seen
    # input  is I have a space/x
    # output is x

At first, I thought this behavior was a bug, but then I realized that
it's working exactly as specified: notdir treats its argument as a
_whitespace-separated_ list of file names.  That makes it difficult to
deal with file names that themselves contains spaces.  Is there a way
to do it?

This is a classic GNU Make problem. GNU Make assumes that spaces are a list separator (as you point out) and hence you get this problem. The only way around this for your purpose is to transform the file name to something without spaces and then convert it back.

The simplest way to do this is to convert every space in the file name to a ? (since ? won't appear in a real file name we can safely use it) and then convert it back afterwards.

Here are two simple functions that will do that:

    sq = $(subst $(sp),?,$1)
    qs = $(subst ?,$(sp),$1)

    e :=
    sp := $(e) $(e)

sq converts a file name so that all spaces are replaced by ?, qs does the opposite.

So you could rewrite your example

    name   := I have a space/x

    worse:
        @echo input  is $(name)
        @echo output is $(call qs,$(notdir $(call sq,$(name))))

And you would get the following output:

    input is I have a space/x
    output is x

Now a nice side effect of using ? is that it's the single character globbing symbol so a file name transformed using sq can be safely passed into GNU Make's $(wildcard) function to determine if a file exists. Of course if you were to have file names like "I have" and "Idhave" then this globbing doesn't do quite what you want, but it's pretty close.

John.
--
John Graham-Cumming
address@hidden

Home: http://www.jgc.org/
Blog: http://www.jgc.org/blog/

POPFile: http://getpopfile.org/
GNU Make Standard Library: http://gmsl.sf.net/
GNU Make Debugger: http://gmd.sf.net/
Fast, Parallel Builds: http://www.electric-cloud.com/

Sign up for my Spam and Anti-spam Newsletter
at http://www.jgc.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]