Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I could add dir to $PATH in Makefile?

Tags:

linux

makefile

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?

like image 536
Szymon Lipiński Avatar asked Jan 20 '12 11:01

Szymon Lipiński


People also ask

How do I pass a PATH in makefile?

You can use the -C flag to specify the path to your makefile. This way you can execute it from a different directory. The -f flag has a different use. With that flag you can execute a makefile with a name other than makefile .

How do I add something to my PATH environment?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


1 Answers

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 
like image 72
Eldar Abusalimov Avatar answered Oct 08 '22 10:10

Eldar Abusalimov