117

I want to write a Makefile which would run tests. Test are in a directory './tests' and executable files to be tested are in the directory './bin'.

When I run the tests, they don't see the exec files, as the directory ./bin is not in the $PATH.

When I do something like this:

EXPORT PATH=bin:$PATH
make test

everything works. However I need to change the $PATH in the Makefile.

Simple Makefile content:

test all:
    PATH=bin:${PATH}
    @echo $(PATH)
    x

It prints the path correctly, however it doesn't find the file x.

When I do this manually:

$ export PATH=bin:$PATH
$ x

everything is OK then.

How could I change the $PATH in the Makefile?

cxw
  • 16,685
  • 2
  • 45
  • 81
Szymon Lipiński
  • 27,098
  • 17
  • 75
  • 77
  • Can you not just call the tests from the executable directory like `../test/test_to_run`? Sorry if I have misunderstood the question. – Chris Jan 20 '12 at 12:02
  • I want this file to be visible to the tests normally. I don't want to play with the directories, as I refactoring that would be a nighmare. – Szymon Lipiński Jan 20 '12 at 12:12
  • The only way you can come close to this is to have the makefile write out a shell script containing the variable decls and then have the parent shell source that script with `.`. This is probably impractical however. – Michael Smith Jan 20 '12 at 13:03
  • I believe that http://unix.stackexchange.com/questions/11530/adding-directory-to-path-through-makefile is a **very different** (stupid) question, unlike yours. – imz -- Ivan Zakharyaschev Feb 13 '15 at 11:22

5 Answers5

132

Did you try export directive of Make itself (assuming that you use GNU Make)?

export PATH := bin:$(PATH)

test all:
    x

Also, there is a bug in you example:

test all:
    PATH=bin:${PATH}
    @echo $(PATH)
    x

First, the value being echoed is an expansion of PATH variable performed by Make, not the shell. If it prints the expected value then, I guess, you've set PATH variable somewhere earlier in your Makefile, or in a shell that invoked Make. To prevent such behavior you should escape dollars:

test all:
    PATH=bin:$$PATH
    @echo $$PATH
    x

Second, in any case this won't work because Make executes each line of the recipe in a separate shell. This can be changed by writing the recipe in a single line:

test all:
    export PATH=bin:$$PATH; echo $$PATH; x
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
  • 8
    `$(PATH)` will get set to the value of `PATH` of the shell invoking make. As per [the manual](http://www.gnu.org/software/make/manual/html_node/Environment.html), "Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value." – Emil Sit Jul 04 '13 at 04:43
  • 2
    The export directive worked for me (thanks!), but only after I installed GNU Make 4.3. In version 3.81 (the default on macOS Catalina), the updated `PATH` is correctly reflected in variables (`echo $(PATH)`) and within commands' environments (`env`, `which python`, `bash -c python`), but does not seem to be used when locating the executable for the command: the command `python` still runs the Python executable on the original `PATH`. – doctaphred Jun 22 '20 at 13:16
39

By design make parser executes lines in a separate shell invocations, that's why changing variable (e.g. PATH) in one line, the change may not be applied for the next lines (see this post).

One way to workaround this problem, is to convert multiple commands into a single line (separated by ;), or use One Shell special target (.ONESHELL, as of GNU Make 3.82).

Alternatively you can provide PATH variable at the time when shell is invoked. For example:

PATH  := $(PATH):$(PWD)/bin:/my/other/path
SHELL := env PATH=$(PATH) /bin/bash
kenorb
  • 155,785
  • 88
  • 678
  • 743
27

Path changes appear to be persistent if you set the SHELL variable in your makefile first:

SHELL := /bin/bash
PATH := bin:$(PATH)

test all:
    x

I don't know if this is desired behavior or not.

underspecified
  • 979
  • 1
  • 7
  • 15
  • Yeah, this does indeed do what the OP wanted...but is it a feature or a bug? Even after reading the [make manual's SHELL section](http://www.gnu.org/software/make/manual/make.html#Choosing-the-Shell) I'm not sure. – pje Apr 14 '14 at 21:55
  • 6
    This doesn’t work for me either. `which` and `env` now pick up the new PATH, but directly executing a binary is still not found in the modified PATH, only in the original one. – Konrad Rudolph Dec 04 '14 at 15:24
4

What I usually do is supply the path to the executable explicitly:

EXE=./bin/
...
test all:
    $(EXE)x

I also use this technique to run non-native binaries under an emulator like QEMU if I'm cross compiling:

EXE = qemu-mips ./bin/

If make is using the sh shell, this should work:

test all:
    PATH=bin:$PATH x
Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
  • Yep, cool soulution, however I've got my tests written in perl and I need to call the exe from the perl script, not directly from makefile. I've got to rethink the whole testing of this stuff :) – Szymon Lipiński Jan 20 '12 at 12:58
  • Gotcha. How about setting PATH on the command line itself? See the edit above. – Richard Pennington Jan 20 '12 at 13:00
-3

To set the PATH variable, within the Makefile only, use something like:

PATH := $(PATH):/my/dir

test:
@echo my new PATH = $(PATH)
kenorb
  • 155,785
  • 88
  • 678
  • 743
karnhick
  • 255
  • 1
  • 2
  • 8