help-make
[Top][All Lists]
Advanced

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

Re: How can I "source" a file into makefile?


From: Paul D. Smith
Subject: Re: How can I "source" a file into makefile?
Date: Tue, 18 Apr 2006 18:43:03 -0400

%% "PATTON, BILLY \(SBCSI\)" <address@hidden> writes:

  pb> I have 2 files that do an identical function
  pb> cshrc and kshrc

  pb> These set PATH,MANPATH,INFO_PATH and a few other environment
  pb> variables.

  pb> I've tried
  pb> . kshrc
  pb> and
  pb> include kshrc

  pb> But neither work.

". kshrc" is a shell feature.  Why would that work in a makefile?

"include <filename>" parses <filename> as a makefile; why would it work
to include a shell file here?

  pb> Is the only way to do this to do it before executing make?

Yes, pretty much.


Actually, if you write your shell script very carefully and so that it
doesn't do anything but set simple variables to single-word values, you
_CAN_ include a shell script in a makefile, because shell and make
syntax overlap slightly.

So, a shell script like this:

    PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
    export PATH

is valid as both a shell script and a makefile.  You can also use
variable references if you use the ${} form:

    PATH=${PREFIX}/bin:${PREFIX}/sbin:/usr/local/bin
    export PATH

But, that's about all that you can do.


There is one other trick you can use, which is becoming more common:
write a script to print the variable setting commands on stdout; like
this:

    $ cat setvars
    #!/bin/sh
    PREFIX=/usr/local
    echo "PATH=$PREFIX/bin:$PREFIX/sbin:/usr/local/bin; "
    echo "export PATH; "

Then instead of ". setvars" in your shell scripts you run:

    eval `setvars`

You then can write your "setvars" script to generate different formats
of output if you give an argument.  By default it could generate Bourne
shell; you can run "setvars -c" to have it generate C shell (then in a
.cshrc file you'd use:

    eval `setvars -c`

) And, you can use something like "setvars -m" to generate makefile
syntax; then you can do something like:

    $(shell setvars -m > /tmp/somefile)
    include /tmp/somefile

(although obviously you'll have to find a way to pick a unique temporary
filename to avoid collisions).

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <address@hidden>          Find some GNU make tips at:
 http://www.gnu.org                      http://make.paulandlesley.org
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist




reply via email to

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