0

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?

1
  • If you are running a stock Python virtual env, the better solution is probably to run venv/bin/python instead of regular python and venv/bin/pip instead of pip. Inside a make recipe, each command runs in a separate subshell, so the effects of source will disappear as soon as you type a newline.
    – tripleee
    Nov 24, 2020 at 17:07

1 Answer 1

2

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.

5
  • I use this method and find that BASH_ENV := runcom/bash without the export also works. However, if I want BASH_ENV to be a target-specific variable, I find that the export is (surprisingly?) required. Furthermore, when declared as private (like this: myTarget: export private BASH_ENV=runcom/bash , it is "inherited by prerequisites of" myTarget, contrary to the documentation. Tested in 3.82 and 4.4. I'm thinking I should report this as an issue on make. @MadScientist ?
    – malcook
    Dec 2, 2023 at 17:32
  • All variables that are part of the environment passed to make, are also automatically added to the exported list of variables. So, IF (and only if) the user invoking make has BASH_ENV in thier environment then you won't need the export in the makefile. I'm not sure about the issues with target-specific variables. Dec 2, 2023 at 18:05
  • Splendid. Yes - I see, and can confirm this. Re: "the issues with target-specific variables" - I think that myTarget: export private BASH_ENV=runcom/bash should, according to current documentation, cause BASH_ENV to "not be inherited by prerequisites of" myTarget, and therefore the shell executing the recipes of myTarget's prerequisites should not see it, nor use it to initialize its environment. Do you agree with me? If so, I think this is a report-worthy issue.
    – malcook
    Dec 3, 2023 at 14:02
  • It's not clear to me exactly what you're doing or exactly what you expect. private doesn't hide a variable completely from a prerequisite target. It simply hides the target-specific value. Any global variable assignment is still available. I tried a quick experiment with what I think you are saying and it worked as expected for me. I'm using the latest release GNU Make 4.4.1. If you need more info please either create a new question or ask on the [email protected] mailing list. Dec 3, 2023 at 14:35
  • Thanks for your time and testing. I've taken it up on [email protected], here: lists.gnu.org/archive/html/help-make/2023-12/msg00000.html
    – malcook
    Dec 4, 2023 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.