Make include in makefiles be relative to the file's location

后端 未结 3 2035
旧巷少年郎
旧巷少年郎 2020-12-31 02:05

Directly related to this question. How can I make the include directive in makefiles behave relatively to the location of the current script?

Assume tha

相关标签:
3条回答
  • 2020-12-31 02:30

    Is there a builtin variable with the current makefile's name?

    Yes, there is, use ${CURDIR}. This is the directory where top-level Makefile is located, so you don't need to strip anything from it.

    http://www.gnu.org/software/make/manual/make.html#Recursion

    0 讨论(0)
  • 2020-12-31 02:36

    I find that relative paths work (GNUMake 3.81), but if they don't for you, try this:

    include $(abspath ../whatever)
    
    0 讨论(0)
  • 2020-12-31 02:38

    You can get the name of the makefile being currently processed from MAKEFILE_LIST builtin variable.

    Given that the current makefile is the last one that has been included (in other words you didn't use another include directive since the beginning of the current script), the path to the script itself would be:

    SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
    

    Now you are able to include a script in the same directory as such (note an absence of slash, it has already been added by $(dir ...)):

    include $(SELF_DIR)another.mk
    

    Note: In GNU Make 3.80 there was no lastword builtin function. In that case you may implement it as follows replacing $(lastword ...) with $(call lastword,...):

    lastword = $(if $(firstword $1),$(word $(words $1),$1))
    
    0 讨论(0)
提交回复
热议问题