mwah has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a somehow "inverted" Perl problem here.

I have a lot of small applications I wrote over the time that depend on a bunch of common source files (tools). This is stuff where I never bothered to "autoconf" anything and which is expected to run on both win32 and Unix/Linux (for win32, there will be VC6/VC2005 config stuff in the appropriate dirs).

When using an Unix/Linux Makefile template (since aeons), I throwed in at some point a line that determines the applications name which is going to get built. E.g.: If I have an Unix app to build in "../sources/applications/worlddomination":

sources | +---applications | | | +---loveandpeace | | | | | +---unix | | | | | +---win32 | | | +---worlddomination | | | +---unix | | | +---win32 | +---tools--- ...

there will be a (generic) Makefile in the "./worlddomination/unix" directory that emits a:

.SUFFIXES: .cxx .cpp .c .o # APPNAME is the parent directory of the current one APPNAME = ${shell perl -MCwd -e 'print +(split/\//,cwd)[-2]'} EXE = ${APPNAME} ... (the usual stuff follows) ...

and makes an application "worlddomination" (does in most cases work). I'm not a makefile guru and therefore my question: How can this be done *simpler* with one or two shell commands (I feel the Perl invocation of ${shell perl -MCwd -e 'print +(split/\//,cwd)[-2]'} is somehow a big hammer for this purpose).

Regards & Thanks

mwa

Replies are listed 'Best First'.
Re: Getting rid of Perl in Makefiles
by andyford (Curate) on Oct 24, 2007 at 09:31 UTC

    Hopefully this will work inside your Makefile: if I'm in a certain directory I can type

    basename `pwd`
    and I get just the cwd name.

    non-Perl: Andy Ford

      andyford
      I can type basename `pwd` and I get just the cwd name.

      Perfect, this does almost what I need.

      I only had to change the dir level (up one) in the backticks, like

      ... APPNAME = ${shell basename `cd .. ; pwd`} ...

      Fine!

      Regards & Thanks

      mwa

Re: Getting rid of Perl in Makefiles
by Bloodnok (Vicar) on Oct 24, 2007 at 10:31 UTC
    Hi ,

    Andys' solution works for either *NIX or Cygwin (on Wintel), but doesn't work on DOS CLI and hence wouldn't be platform neutral as required :-((

    ... been there and done that in ClearCase on Wintel. Our platform neutral solution was not dissimilar to yours - AFAIR, it was along the lines of ...

    ITEM :sh= perl -MCwd -e "$$cwd = cwd();$$cwd =~ /.*[\\\/](.*)/;$$1"
    Sorry if that's not quite right, but it was a while ago ..

    HTH ,

    At last, a user level that best describes my experience :-))