Help with some makefile hackery

Patrick Mahan mahan at mahan.org
Fri Jun 25 12:57:31 UTC 2010


Dag-Erling,

I am using option 3 below.  I have the following in my
shell:

amd64-kernel.sh
#!/bin/sh
trap "exit 1" 1 2 3 15

export SRCROOT=`pwd -P`
export MAKEOBJDIRPREFIX=$SRCROOT/../amd64/obj
export SRCCONF=$SRCROOT/src.conf  # Use our private copy
export SECKNOB="-DPRIVATE"
KERNCONF=TCONF

make NO_CLEAN=yes NO_PROFILE=yes NO_MODULES=yes KERNCONF=$KERNCONF buildkernel || 
exit 1
...


In the top-level makefile I have the following label:

src-kernel: src-kernel-tools
	cd src; ./amd64-kernel.sh 2>&1 | tee build_amd64_kernel.log

If there is a build failure with the kernel, it can be seen in the
file 'build_amd64_kernel.log'.  However, the top-level make file just
continues on to the next label as if no error occurs.

The reason we are using shell scripts is because of the environment
variables that need to be defined and some other house-keeping
stuff that I did not include in the above example.  Also, I wanted
scripts so the developer could just build individual "groups"
without resorting to building everything.

Thanks,

Patrick

Dag-Erling Smørgrav wrote:
> Patrick Mahan <mahan at mahan.org> writes:
>> My issue is that if there is a build failure at any point, the
>> status does not seem to be propagated upward.  For example, if
>> the kernel fails to build due to incorrect code, the script
>> <machine>-kernel64.sh stops (verifable by examining the logfile),
>> however, the make will continue to the next target, src-world,
>> and continue building.  How do I propagate the status up to the
>> top-level make?
> 
> Your shell script needs to exit with a non-zero status if it fails.
> There are several ways to do this.  For instance, if your shell script
> looks like this:
> 
>   #!/bin/sh
>   make TARGET=amd64 kernel-toolchain
> 
> you can:
> 
>  - prefix "make" with "exec": sh will exec make instead of running it in
>    a subshell, and the exit status will be whatever make returns.
> 
>  - add the following line at the bottom:
> 
>      exit $?
> 
>    which causes the shell script to exit with the same exit status as
>    the last command it ran, in this case make.
> 
>  - append "|| exit 1" to the the "make" command line; this will cause
>    the script to exit immediately with status 1 if make fails, otherwise
>    it will continue (in case you want to do something else if make
>    succeeds)
> 
>  - wrap the make command line in an if statement, which allows you do
>    additional work depending on the outcome, and end the failure case
>    with "exit 1" or similar
> 
>  - insert the following line at any point between the shebang and the
>    make command line:
> 
>      set -e
> 
>    this will cause sh to terminate the script immediately with a
>    non-zero exit status if any command after the "set" line fails.
> 
> However, I don't see the point of using shell scripts in the first
> place...
> 
> DES



More information about the freebsd-hackers mailing list