make parameter to change directory before it does anything - makefile

Are there any command line parameters for make to change to a directory before it does anything?
I have a case where I have to run make from a different directory than the Makefile. But since some source header include paths are relative to the Makefile directory, it cannot find those.

'make -C ' does the trick. It tells make to change directory before reading any makefiles as commented by Etan above.

Related

How to prevent make from regenerating .gitignore?

I'm just learning to use make and I wanted to use my Makefile to generate a simple .gitignore file. I've tried to write various versions of the file but none seem to work the way I'd expect. The main problem I have is that the .gitignore file is remade every time I run make even though nothing has changed.
Here's what I've tried:
".gitignore" :
touch .gitignore
echo 'node_modules' >> .gitignore
This re-appends node_modules to the .gitignore every time I run make. Without the quotes around .gitignore it fails with the error: make: *** No targets. Stop.. I tried adding a bogus dependency that doesn't change (i.e. ".gitignore" : Makefile) but the same rebuilding occurs. I've tried making .gitignore depend on itself but it just tells me that the circular dependency was dropped and I get the same behavior. I've tried with GNU make versions 3.81 and 4.3.
I eventually want to generate an entire repository with make and I don't want the timestamp on the file to change between runs because that causes problems with git.
Quotes are not special characters in a makefile (generally speakig).
A rule like this:
".gitignore" :
tells make that your target is named, literally, ".gitignore" (including quotes). Since there is no file named that way in the directory, make runs your rule to generate it.
Change your rule to remove the quotes:
all: .gitignore
.gitignore:
...
and it should work as you expect. The reason you can't just use .gitignore by itself is that make treats targets that start with a period specially: such a target cannot be the default goal in a makefile.

Why is Snakemake not seeing symbol link files?

I have a rule whose output files are symbolic link files. Even though the link files are being made, Snakemake exits with a MissingOutputException and lists the output files as being missing. If instead of making a symlink with "ln -s" I copy the files with "cp -p" it works. I tried increasing the --latency-wait but it made no difference.
Sounds like you are using relative path for source file when symlinking. Use absolute path.
Snakemake sees broken symlinks as missing output.

How to run Makefile from any directory?

I have created a makefile which is fully automatic. This means, I do not need to change makefile to run different programs.
Now what I want to do is put that makefile somewhere and call that makefile from directory where my program is. But condition is that makefile should run as if I have putted that makefile in directory where my program is created and NOT at place where makefile actually is.
You can run a Makefile from another location as if it was in the current directory with:
make -f /path/to/your/makefile
You can make an alias or function for it if you want.

Calling existing make command in cmake file

I have a large project with multiple subdirectories. In the parent directory, I have a CMakeLists.txt file which calls functions defined in other cmake files in the same parent directory. I have a custom Makefile in one of the subdirectories that contains some target "run". When I call cmake from the parent directory, I want the "run" target located in the subdirectory makefile to execute. How should I do this ?
I understand that some people have suggested to use add_custom_target and add_custom_command, but I am still confused as to how to apply these commands to accomplish this task.
If you know, which file(s) are produced by Makefile in the subdirectory, and want to depend on these files, use add_custom_command:
add_custom_command(OUTPUT <output-file>
COMMAND make run
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)
This assumes that your CMakeLists.txt have a target, which depends or uses given file.
Otherwise, if you do not care which files are produced by Makefile, use add_custom_target:
add_custom_target(<target_name> COMMAND make run
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/<subdir>
)
In both cases WORKING_DIRECTORY specifies directory which should be current for command executed.
If you want the target (in the second case) to be executed by default, add ALL option before the COMMAND.

Makefile and Files under a directory

I have Makefile under a directory and directory has many .pm ( perl files )
I want to add makefile at this directory level which does perl file syntax checking.
Basically I want to add:
perl -c <filename>
How to get list of files automatically in that directory without hardcoding.
The following worked for me in the GNU makefile (Linux and Windows)
ALL_PM_FILES = $(wildcard *.pm)
Then run a for/foreach loop on them.
You can try the filter command:
PMFILES=$(filter %.pm, $(SRC))
Documentation for filter is hard to find. See here for an example.
This is the normal workaround:
check_pm_syntax:
for file in *.pm; do ${PERL} -c $$file; done
You run 'make check_pm_syntax' and it goes off and runs the shell loop for all the *.pm files it can find. You can simply list check_pm_syntax as a pre-requisite for your all target if you like (but it means you'll always do work when you build all). The only time this causes trouble is if there are no *.pm files in the directory.
Here's a slightly different approach:
.PHONY: check_%.pm
check_%.pm:
perl -c $*.pm
check_all: $(addprefix check_,$(wildcard *.pm))

Resources