I have the following rule in a makefile:
ninja:
git clone git://github.com/martine/ninja.git
pushd ninja
pwd
git checkout release
./configure.py --bootstrap
popd
The idea is to download and build ninja automatically as a project dependency. Notice that the pwd
command is just there to make sure that the directory was pushed. Here's the output it generates:
git clone git://github.com/martine/ninja.git
Cloning into 'ninja'...
remote: Counting objects: 8646, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 8646 (delta 0), reused 0 (delta 0), pack-reused 8642
Receiving objects: 100% (8646/8646), 1.88 MiB | 427.00 KiB/s, done.
Resolving deltas: 100% (6114/6114), done.
Checking connectivity... done.
pushd ninja
~/Desktop/core/ninja ~/Desktop/core
pwd
/Users/fratelli/Desktop/core
git checkout release
error: pathspec 'release' did not match any file(s) known to git.
make: *** [ninja] Error 1
As you can see the directory does get pushed into the stack, but pwd
does not return the correct directory. That's also why the checkout
fails afterwards. Any ideas how to fix this?
To use this makefile, simply cd to the directory and type “ makepp ”. Makepp will attempt to build the first target in the makefile, which is my_program . (If you don't want it to build the first target, then you have to supply a the name of the target you actually want to build on the command line.)
The pushd command is used to save the current directory into a stack and move to a new directory. Furthermore, popd can be used to return back to the previous directory that is on top of the stack. It is very useful when we have to switch between two directories frequently.
pushd and popd work according to the “LIFO” (last in, first out) principle. In this principle, only two operations are allowed: push an item into the stack, and pop an item out of the stack. pushd adds a directory to the top of the stack and popd removes a directory from the top of the stack.
Use cd ./dir && make && pwd inside Makefile . The && was exactly what I needed to change a directory and execute a command there, then drop back to the main folder to finish the build.
Each line in a makefile target recipe is run in its own shell session. This doesn't affect most recipes as they operate in the directory they need to by default. When they don't do that and you need to use cd
or pushd
then you need to write the commands all on the same line or tell make that the lines are continued.
See Splitting Recipe Lines for more details and examples.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With