Skip to content

Instantly share code, notes, and snippets.

@doitian
Last active December 9, 2022 02:31
Show Gist options
  • Select an option

  • Save doitian/4978329 to your computer and use it in GitHub Desktop.

Select an option

Save doitian/4978329 to your computer and use it in GitHub Desktop.
Forward Makefile to subdirectory `build`
# List targets defined in this file
TARGETS_SELF := cmake
# Exclude targets defined in this file
TARGETS_OTHER := $(filter-out $(TARGETS_SELF), $(MAKECMDGOALS))
# Call all targets using `Makefile` in build directory in one `make` command. It
# can depend on targets defined in this file, e.g., depending on a target to
# create the Makefile.
#
# If no targets are specified, use the dummy `all` target
$(or $(lastword $(TARGETS_OTHER)),all):
$(MAKE) -C build $(TARGETS_OTHER)
.PHONY: $(TARGETS_OTHER) all
# Do nothing for all targets but last. Also quiet the message "Noting to be done on xxx"
$(filter-out $(lastword $(TARGETS_OTHER)), $(TARGETS_OTHER)):
@cd .
cmake:
cd build && cmake ../src
.PHONY: cmake
@doitian
Copy link
Copy Markdown
Author

doitian commented Feb 18, 2013

Say your Makefile is in build, but you want to call make in top directory without specifying -C

@mavam
Copy link
Copy Markdown

mavam commented Apr 8, 2015

Very useful, thanks! Unfortunately it relies on GNU make features. Do you happen to know a version that achieves the same thing with BSD make?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment