Skip to content

Instantly share code, notes, and snippets.

@amn
Created October 24, 2024 14:19
Show Gist options
  • Save amn/50207bca57f22c126d2cb1b512797008 to your computer and use it in GitHub Desktop.
Save amn/50207bca57f22c126d2cb1b512797008 to your computer and use it in GitHub Desktop.
A decent "resolve like using VPATH" [GNU] Make expression
vpath-resolve = $(foreach path,$(1),$(or $(firstword $(wildcard $(path) $(addsuffix /$(path),$(patsubst %/,%,$(VPATH))))),$(error `$(path)` not found in any of `VPATH`.)))
@amn
Copy link
Author

amn commented Oct 24, 2024

The vpath-resolve function expressed with the above [GNU] Make file, is written to aid in situations where one wants to resolve a path (or list of paths) as if Make itself were looking for the file as a prerequisite. I.e. if you have a rule for a target foo that has bar for prerequisite:

foo: bar

...in a recipe for foo (omitted from the above for simplicity), the path of the prerequisite corresponding to bar in the rule above, available with $< or $^, would actually be the path of some "bar" found according to "Searching Directories for Prerequisites" -- it wouldn't necessarily be "bar" relative to current working directory, but rather whichever "bar" was first found among paths specified with VPATH.

The vpath-resolve function offered with the gist attempts to replicate the prerequisite searching method results, meaning $(call vpath-resolve,bar) should produce a path as if Make were searching for it as a prerequisite, as described by the linked documentation section.

The reason I have added the function definition as a gist, is because I, for reasons I can't really understand, have come to expect the functionality on numerous occasions myself, so perhaps I am not the only one in need of it?

#opaqueabstractions #transparentabstractions

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