Is there a better way to source a script, which sets env vars, from within a makefile?
FLAG ?= 0
ifeq ($(FLAG),0)
export FLAG=1
/bin/myshell -c '<source scripts here> ; $(MAKE) $@'
else
...targets...
endif
Is there a better way to source a script, which sets env vars, from within a makefile?
FLAG ?= 0
ifeq ($(FLAG),0)
export FLAG=1
/bin/myshell -c '<source scripts here> ; $(MAKE) $@'
else
...targets...
endif
If your goal is to merely set environment variables for Make, why not keep it in Makefile syntax and use the
include
command?If you have to invoke the shell script, capture the result in a
shell
command:the shell command should run before the targets. However this won't set the environment variables.
If you want to set environment variables in the build, write a separate shell script that sources your environment variables and calls make. Then, in the makefile, have the targets call the new shell script.
For example, if your original makefile has target
a
, then you want to do something like this:Using GNU Make 3.81 I can source a shell script from make using:
build_files.sh "gets" the environment variables exported by source_script.sh.
Note that using:
will not work. Each line is ran in its own subshell.
This works for me. Substitute
env.sh
with the name of the file you want to source. It works by sourcing the file in bash and outputting the modified environment, after formatting it, to a file calledmakeenv
which is then sourced by the makefile.If you need only a few known variables exporting in makefile can be an option, here is an example of what I am using.
-- http://rzr.online.fr/q/gnumake
Another possible way would be to create a sh script, for example
run.sh
, source the required scripts and call make inside the script.To answer the question as asked: you can't.
The basic issue is that a child process can not alter the parent's environment. The shell gets around this by not forking a new process when
source
'ing, but just running those commands in the current incarnation of the shell. That works fine, butmake
is not/bin/sh
(or whatever shell your script is for) and does not understand that language (aside from the bits they have in common).Chris Dodd and Foo Bah have addressed one possible workaround, so I'll suggest another (assuming you are running GNU make): post-process the shell script into make compatible text and include the result:
messy details left as an exercise.