here's makefile
gitignore-create: touch .gitignore ;\ echo 'node_modules' >> .gitignore ;\ echo 'npm' >> .gitignore ;\ echo '.ds_store' >> .gitignore ;\
if run make gitignore-create
gitignore file current directory.
i'm looking way run makefile command same directory have target specified another. i'd not add cd
command though.
i tried this:
# make wrap package=my-package command=gitignore-create wrap: pushd packages/$(package)/ && make -c ${curdir} $(command) && popd
and creates file in actual current dir.
there's lots wrong this:
wrap: pushd packages/$(package)/ && make -c ${curdir} $(command) && popd
the first thing should never use static command make
when invoking recursive make rules. always use variable $(make)
(or if prefer, ${make}
: they're identical).
second, can't use pushd
, popd
(at least not portably) because bash commands, make runs /bin/sh
shell (unless change setting shell
makefile variable) , on many systems, /bin/sh
not bash.
third, don't need anyway because in unix/linux, right-thinking operating systems, current directory property of current process (part of environment) , changing current directory doesn't change parent process's current directory. and, every recipe line in makefile whole new process (that runs shell). can use cd
directly in recipe line , recipe line finishes , shell exits, changes environment, including current directory, gone: don't have explicitly undo them.
so can write rule above equivalently as:
wrap: cd packages/$(package)/ && $(make) -c ${curdir} $(command)
however, doesn't because -c
option when given make causes make change it's working directory value provided. recipe says, "first change directories packages/$(package)
, run make , tell first thing should before starts commands change directories original directory". identical running make without changing directories @ all.
your explanation not clear think want this:
# variable assignment must come before include statements mymakefile := $(abspath $(lastword $(makefile_list))) ... wrap: cd packages/$(package)/ && $(make) -f $(mymakefile) $(command)
which first remembers path current makefile, creates recipe changes directory packages/$(package)
runs make giving current makefile parse (but not changing directories, don't use -c
!) , command run.