Chris
Chris

Reputation: 31216

Make: Set shell and source a file at the same time?

Suppose I would like to set a shell in make:

SHELL:=/usr/bin/env bash

Next, suppose I have some runcom/bash file I would like to source as well. This file optionally activates a virtual environment:

if [ -d venv ]; then source venv/bin/activate fi;

However, if I write:

SHELL:=/usr/bin/env bash && source runcom/bash

This fails. However, if I deposited the venv logic into the local ~/.bashrc and write:

SHELL:=/usr/bin/env bash -l

I can get the exact functionality I need.


But, I have to deposit something that should remain downstream from the local user into the user's upstream environment -- I'd rather not.

Is there a way to get the make shell to source a file at the declaration step in the make start-up process?

Upvotes: 0

Views: 205

Answers (1)

MadScientist
MadScientist

Reputation: 100856

This can't work:

SHELL:=/usr/bin/env bash && source runcom/bash

Because SHELL tells make how to invoke the shell; if you make the contents of SHELL be a shell script then make has to invoke the shell to interpret how to invoke the shell, which means it has to invoke the shell to invoke the shell to interpret how to invoke the shell, etc.

So, SHELL must be either a simple command or, at most, a simple set of arguments that can converted into an argv list and passed to exec(2).

So, this is really a shell question not a make question: how can you get the shell to source arbitrary stuff when it starts without changing ~/.profile or whatever?

Luckily, this is possible; see the bash man page:

   BASH_ENV
          If  this parameter is set when bash is executing a shell script,
          its value is interpreted as a filename  containing  commands  to
          initialize the shell, as in ~/.bashrc.  The value of BASH_ENV is
          subjected to  parameter  expansion,  command  substitution,  and
          arithmetic  expansion  before  being  interpreted as a filename.
          PATH is not used to search for the resultant filename.

   ENV    Similar to BASH_ENV; used when the shell  is  invoked  in  posix
          mode.

So, in your makefile you can use something like:

SHELL := /bin/bash
export BASH_ENV := runcom/bash

and that should be sufficient.

Upvotes: 2

Related Questions