How to source a script in a Makefile?

2019-01-13 17:30发布

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

13条回答
再贱就再见
2楼-- · 2019-01-13 17:36

If your goal is to merely set environment variables for Make, why not keep it in Makefile syntax and use the include command?

include other_makefile

If you have to invoke the shell script, capture the result in a shell command:

JUST_DO_IT=$(shell source_script)

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:

# mysetenv.sh
#!/bin/bash
. <script to source>
export FLAG=1
make "$@" 

# Makefile
ifeq($(FLAG),0)
export FLAG=1
a: 
    ./mysetenv.sh a
else
a:
    .. do it
endif
查看更多
唯我独甜
3楼-- · 2019-01-13 17:40

Using GNU Make 3.81 I can source a shell script from make using:

rule:
<tab>source source_script.sh && build_files.sh

build_files.sh "gets" the environment variables exported by source_script.sh.

Note that using:

rule:
<tab>source source_script.sh
<tab>build_files.sh

will not work. Each line is ran in its own subshell.

查看更多
Summer. ? 凉城
4楼-- · 2019-01-13 17:46

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 called makeenv which is then sourced by the makefile.

IGNORE := $(shell bash -c "source env.sh; env | sed 's/=/:=/' | sed 's/^/export /' > makeenv")                         
include makeenv   
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-13 17:47

If you need only a few known variables exporting in makefile can be an option, here is an example of what I am using.

$ grep ID /etc/os-release 

ID=ubuntu
ID_LIKE=debian


$ cat Makefile

default: help rule/setup/lsb

source?=.

help:
        -${MAKE} --version | head -n1

rule/setup/%:
        echo ID=${@F}

rule/setup/lsb: /etc/os-release
        ${source} $< && export ID && ${MAKE} rule/setup/$${ID}


$ make

make --version | head -n1
GNU Make 3.81
. /etc/os-release && export ID && make rule/setup/${ID}
make[1]: Entering directory `/tmp'
echo ID=ubuntu
ID=ubuntu

-- http://rzr.online.fr/q/gnumake

查看更多
孤傲高冷的网名
6楼-- · 2019-01-13 17:48

Another possible way would be to create a sh script, for example run.sh, source the required scripts and call make inside the script.

#!/bin/sh
source script1
source script2 and so on

make 
查看更多
女痞
7楼-- · 2019-01-13 17:49

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, but make 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:

shell-variable-setter.make: shell-varaible-setter.sh
    postprocess.py @^

# ...
else
include shell-variable-setter.make
endif

messy details left as an exercise.

查看更多
登录 后发表回答