How does one allow GNU Make to "include mk/*.mk" without breaking make when there is nothing in the include folder?

Viewed 30

OS: Mac

Make: 4.3

Basically, I have a make-driven environment setup project. This performs, repairs, or procedurally empowers me to perform repetitive CLI tasks using built in terminal auto completion along with verbose command names.

In the top level makefile for this project, I define meta commands that will be useful for the development environment I am in. I also include all *.mk files in a mk/ directory. As a result of this architecture, I can install everything and copy the makefile to the home directory. However, even if I create a mk/ directory at the top level, I must touch a dummy makefile before I can perform a direct copy. That is because include mk/*.mk throws an error when there are no files in the directory.

Ideally, there is no grit and everything is clean. I understand the priority is the same in the source code for make, and so this might be a quirk that is better to have than fix. However, if possible, is there a clean, simple workaround that will allow this system to work without initializing a mk/dummy.mk file?


layout:

├── makefile
├── mk
│   ├── get.mk
│   ├── notes.mk
│   └── setup.mk
└── readme.md

~/dev-env/mk/setup.mk

setup-make:
  mkdir -p ~/mk
  touch ~/mk/add-tgts-here.mk # <= IS THERE A WAY TO GET RID OF THIS
  cat makefile >> ~/makefile # cat in append mode to be safe

~/dev-env/makefile

include mk/*.mk # <= WITHOUT OVER-COMPLICATING THIS

foo:
  echo $@

bar:
  echo $@
2 Answers

Why not just use:

include $(wildcard mk/*.mk)

? wildcard expands to nothing when nothing matches, and include with no arguments is valid (a no-op).

-include mk/*.mk

will include everything currently in the mk directory, without giving an error for any non-existent files.

Related