Makefile: allow using BUILD_DIR for outputs#536
Merged
Conversation
The Makefile now supports an optional `BUILD_DIR` variable for out-of-tree builds. When not set, it builds in-tree exactly as before. Key mechanisms: `SRCDIR` — automatically derived from the Makefile's own location using `$(abspath $(lastword $(MAKEFILE_LIST)))`, so it always points to the source tree regardless of where `make` is invoked from. `BUILD_DIR` — user-supplied, optional. When set, all build artifacts (`.o`, `.a`, `.so`, binaries) go there instead of the source tree. `_OBJDIR` — internal variable that equals `BUILD_DIR` when set, or `SRCDIR` when not (backward compat). `VPATH := $(SRCDIR)` — lets Make find `.c` source files in the source tree when building from a different directory. `-I$(SRCDIR)` — replaces the old `-I.` so header includes always resolve against the source tree. `.SECONDEXPANSION:` with `| $$(dir $$@)` order-only prerequisites — ensures build subdirectories (`linux/`, `libhfcommon/`, etc.) are created in `BUILD_DIR` before compilation. All output paths (`OBJS`, `BIN`, `HFUZZ_CC_BIN`, `LHFUZZ_ARCH`, `LCOMMON_ARCH`, `LNETDRIVER_ARCH`, etc.) are prefixed with `$(_OBJDIR)/`.
Collaborator
|
Thank you Paul! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi!
We recently got an issue (rust-fuzz/honggfuzz-rs#116) with
honggfuzz-rswhen buildinghonggfuzzfrom a read-only directory. This happens for example when using Nix as a build system.Rust's cargo usually writes all its artifacts in a
targetdirectory but until now myhonggfuzz-rsproject didn't respect that and builthonggfuzz's objects files directly in the source directory.This violates cargo's mechanism and breaks Nix builds.
One easy solution is to copy the whole
honggfuzzproject in thetargetdirectory each time and build from here. I think this is not great ashonggfuzzis a hundred or so MBs and cargo will duplicate it as many times as there will be build configurations.We could avoid copying the
examplesdir or using symlinks/hardlinks but I think the best solution would be have theMakefilebe able to use a separate output directory, similarly to cmake.To illustrate the idea I send you this PR but don't hesitate to reject it, I didn't even write it as I only know the basics of Makefiles. This code was written by Claude Opus 4.6 and I don't fully understand it. It seems to work fine though.
Summary of Changes (written by Claude)
The Makefile now supports an optional
BUILD_DIRvariable for out-of-tree builds. When not set, it builds in-tree exactly as before.Key mechanisms:
SRCDIR— automatically derived from the Makefile's own location using$(abspath $(lastword $(MAKEFILE_LIST))), so it always points to the source tree regardless of wheremakeis invoked from.BUILD_DIR— user-supplied, optional. When set, all build artifacts (.o,.a,.so, binaries) go there instead of the source tree._OBJDIR— internal variable that equalsBUILD_DIRwhen set, orSRCDIRwhen not (backward compat).VPATH := $(SRCDIR)— lets Make find.csource files in the source tree when building from a different directory.-I$(SRCDIR)— replaces the old-I.so header includes always resolve against the source tree..SECONDEXPANSION:with| $$(dir $$@)order-only prerequisites — ensures build subdirectories (linux/,libhfcommon/, etc.) are created inBUILD_DIRbefore compilation.All output paths (
OBJS,BIN,HFUZZ_CC_BIN,LHFUZZ_ARCH,LCOMMON_ARCH,LNETDRIVER_ARCH, etc.) are prefixed with$(_OBJDIR)/.