How can I add dir to $ PATH in a Makefile?

I want to write a Makefile that will run the tests. The test is located in the "./tests" directory and the executable files to be tested are in the "./bin" directory.

When I run the tests, they do not see the exec files since the directory. / bin is not in $ PATH.

When I do something like this:

EXPORT PATH=bin:$PATH make test 

everything is working. However, I need to change $ PATH in the Makefile.

Simple Makefile Content:

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

It prints the path correctly, however it does not find the x file.

When I do this manually:

 $ export PATH=bin:$PATH $ x 

everything is fine.

How can I change $ PATH in a Makefile?

+52
linux makefile
Jan 20 '12 at 11:59
source share
5 answers

Have you tried the export directive yourself (assuming you are using GNU Make)?

 export PATH := bin:$(PATH) test all: x 

Also, in your example there is an error:

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

First, the value echo ed is an extension of the PATH variable executed by Make, not the shell. If it prints the expected value, then I think you set the PATH variable somewhere earlier in the Makefile or in the shell that Make calls. To prevent this behavior, you should avoid the dollar:

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

Secondly, in any case, this will not work, because Make executes each line of the recipe in a separate shell . This can be changed by writing the recipe in one line:

 test all: export PATH=bin:$$PATH; echo $$PATH; x 
+63
Jan 20 '12 at 13:35
source share

The path changes seem permanent if you first set the SHELL variable to your file:

 SHELL := /bin/bash PATH := bin:$(PATH) test all: x 

I do not know if this is desirable behavior or not.

+18
Nov 20 '12 at 7:14
source share

When the shell is invoked by make , it probably still uses the old PATH. Therefore, it is best to specify the PATH variable at the time the shell is called. For example:

 PATH := $(PATH):$(PWD)/bin:/my/other/path SHELL := env PATH=$(PATH) /bin/bash 
+7
Mar 25 '16 at 19:40
source share

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

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

I also use this method to run non-interacting binaries under an emulator like QEMU, if I cross-compile:

 EXE = qemu-mips ./bin/ 

If make uses the sh shell, this should work:

 test all: PATH=bin:$PATH x 
+2
Jan 20 2018-12-12T00:
source share

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

 PATH := $(PATH):/my/dir test: @echo my new PATH = $(PATH) 
-2
Jan 20 2018-12-12T00:
source share



All Articles