github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/makelib/inc.mk (about)

     1  # PUBLIC VARIABLES
     2  
     3  # Directory containing an mk file.
     4  MK_SRCDIR := # will be set later in file
     5  # Basename of an mk file.
     6  MK_FILENAME := # will be set later in file
     7  # Path of an mk file.
     8  MK_PATH := # will be set later in file
     9  # Directory containing a toplevel Makefile.
    10  MK_TOPLEVEL_SRCDIR :=
    11  
    12  # PUBLIC FUNCTIONS
    13  
    14  # Includes one file. Sets MK_SRCDIR, MK_FILENAME and MK_PATH variables
    15  # for the scope of included files and restores them then to old
    16  # values. The passed include file must be relative to current
    17  # MK_SRCDIR.
    18  # Example: $(call inc-one,foo/foo.mk)
    19  # Inside foo/foo.mk, MK_SRCDIR will be "inc" (without quotes),
    20  # MK_FILENAME will be "foo.mk" (also without quotes) and MK_PATH -
    21  # "foo/foo.mk" (without quotes, of course).
    22  define inc-one
    23  $(eval $(call _UTIL_MK_INC_ONE_,$1))
    24  endef
    25  # Includes many files. See inc-one docs.
    26  # Example $(call inc-many,foo/foo.mk bar/bar.mk baz/baz.mk)
    27  define inc-many
    28  $(eval $(foreach f,$1,$(call inc-one,$f)))
    29  endef
    30  # Gets directory part of filename, strips trailing slash.
    31  # Example: DIR := $(call to-dir,foo/bar/baz.mk)
    32  define to-dir
    33  $(call _UTIL_MK_TO_DIR_,$1)
    34  endef
    35  
    36  
    37  # PRIVATE DETAILS
    38  
    39  $(if $(_UTIL_MK_INCLUDED_),$(error $(_UTIL_MK_) can be included only once))
    40  # Guard variable used for checking against including this file
    41  # multiple times.
    42  _UTIL_MK_INCLUDED_ := 1
    43  
    44  # Gets a directory containing this file. Path is relative to current
    45  # working directory (which is often a directory where toplevel
    46  # Makefile sits). Should be called before including anything in file.
    47  # Example: MK_SRCDIR := $(call _UTIL_MK_GET_THIS_DIR_)
    48  define _UTIL_MK_GET_THIS_DIR_
    49  $(call _UTIL_MK_TO_DIR_,$(call _UTIL_MK_MAKEFILE_))
    50  endef
    51  # Gets a filename of this file. Should be called before including
    52  # anything in file.
    53  # Example: MK_FILENAME := $(call _UTIL_MK_GET_THIS_FILENAME_)
    54  define _UTIL_MK_GET_THIS_FILENAME_
    55  $(notdir $(call _UTIL_MK_MAKEFILE_))
    56  endef
    57  # Gets a path of this file. Should be called before including anything
    58  # in file.
    59  # Example: MK_PATH := $(call _UTIL_MK_GET_THIS_PATH_)
    60  define _UTIL_MK_GET_THIS_PATH_
    61  $(call _UTIL_MK_MAKEFILE_)
    62  endef
    63  
    64  # Gets the name of this file. It's important that nothing in this file
    65  # gets included before this line.
    66  _UTIL_MK_ := $(lastword $(MAKEFILE_LIST))
    67  # Gets a name of the file. Filters out name of this file from the list.
    68  # Example: FILE := $(call _UTIL_MK_MAKEFILE)
    69  define _UTIL_MK_MAKEFILE_
    70  $(lastword $(filter-out $(_UTIL_MK_),$(MAKEFILE_LIST)))
    71  endef
    72  # See docs of to-dir.
    73  define _UTIL_MK_TO_DIR_
    74  $(patsubst %/,%,$(dir $1))
    75  endef
    76  
    77  # Stacks for saving MK_SRCDIR variables.
    78  _UTIL_MK_SRCDIR_STACK_ :=
    79  _UTIL_MK_SRCDIR_DO_POP_ :=
    80  
    81  # Stacks for saving MK_FILENAME variables.
    82  _UTIL_MK_FILENAME_STACK_ :=
    83  _UTIL_MK_FILENAME_DO_POP_ :=
    84  
    85  # Stacks for saving MK_PATH variables.
    86  _UTIL_MK_PATH_STACK_ :=
    87  _UTIL_MK_PATH_DO_POP_ :=
    88  
    89  # Bails out if variable of passed name is undefined.
    90  # Example: $(call _UTIL_MK_ENSURE_DEFINED_,FOO_VAR)
    91  define _UTIL_MK_ENSURE_DEFINED_
    92  $(eval $(if $(filter undefined,$(origin $1)),$(error $1 variable is not defined)))
    93  endef
    94  
    95  # Pushes value onto given stack. Stack must be defined.
    96  # Example: $(call _UTIL_MK_PUSH_,MY_STACK,$(SOME_VALUE))
    97  define _UTIL_MK_PUSH_
    98  $(eval $(call _UTIL_MK_ENSURE_DEFINED_,$1)) \
    99  $(eval $1 := $(strip $2 $($1)))
   100  endef
   101  
   102  # Pops value from given stack. Stack must be defined.
   103  # Example: $(call _UTIL_MK_POP_,MY_STACK)
   104  define _UTIL_MK_POP_
   105  $(eval $(call _UTIL_MK_ENSURE_DEFINED_,$1)) \
   106  $(eval $1 := $(wordlist 2,$(words $($1)),$($1)))
   107  endef
   108  
   109  # Gets value from top of stack. Stack must be defined.
   110  # Example: VALUE := $(call _UTIL_MK_TOP,MY_STACK)
   111  define _UTIL_MK_TOP_
   112  $(eval $(call _UTIL_MK_ENSURE_DEFINED_,$1)) \
   113  $(firstword $($1))
   114  endef
   115  
   116  # Potentially saves a value if it is not empty. Requires two stacks to
   117  # do that - one for maybe storing the value and one for saying whether
   118  # the value was saved. Both stacks must be defined.
   119  # Example: $(call _UTIL_MK_SAVE_VALUE,IS_VALUE_SAVED_STACK,VALUE_STACK,VALUE)
   120  define _UTIL_MK_SAVE_VALUE_
   121  $(eval _UTIL_MK_TMP_DO_POP_STACK_NAME_ := $1) \
   122  $(eval _UTIL_MK_TMP_VALUE_STACK_NAME_ := $2) \
   123  $(eval _UTIL_MK_TMP_VALUE_NAME_ := $3) \
   124   \
   125  $(eval $(if $(strip $($(_UTIL_MK_TMP_VALUE_NAME_))), \
   126          $(eval $(call _UTIL_MK_PUSH_,$(_UTIL_MK_TMP_DO_POP_STACK_NAME_),1)) \
   127          $(eval $(call _UTIL_MK_PUSH_,$(_UTIL_MK_TMP_VALUE_STACK_NAME_),$($(_UTIL_MK_TMP_VALUE_NAME_)))), \
   128   \
   129          $(eval $(call _UTIL_MK_PUSH_,$(_UTIL_MK_TMP_DO_POP_STACK_NAME_),0)))) \
   130   \
   131  $(eval _UTIL_MK_TMP_DO_POP_STACK_NAME_ :=) \
   132  $(eval _UTIL_MK_TMP_VALUE_STACK_NAME_ :=) \
   133  $(eval _UTIL_MK_TMP_VALUE_NAME_ :=)
   134  endef
   135  
   136  # Potentially restores a value if it was not empty when saving it. If
   137  # it was, then the variable is set to empty string. See
   138  # _UTIL_MK_SAVE_VALUE_ docs for details.
   139  # Example: $(call _UTIL_MK_LOAD_VALUE,IS_VALUE_SAVED_STACK,VALUE_STACK,VALUE)
   140  define _UTIL_MK_LOAD_VALUE_
   141  $(eval _UTIL_MK_TMP_DO_POP_STACK_NAME_ := $1) \
   142  $(eval _UTIL_MK_TMP_VALUE_STACK_NAME_ := $2) \
   143  $(eval _UTIL_MK_TMP_VALUE_NAME_ := $3) \
   144  $(eval $(if $(filter 0,$(call _UTIL_MK_TOP_,$(_UTIL_MK_TMP_DO_POP_STACK_NAME_))), \
   145          $(eval $(_UTIL_MK_TMP_VALUE_NAME_) :=), \
   146   \
   147          $(eval $(_UTIL_MK_TMP_VALUE_NAME_) := $(call _UTIL_MK_TOP_,$(_UTIL_MK_TMP_VALUE_STACK_NAME_))) \
   148          $(eval $(call _UTIL_MK_POP_,$(_UTIL_MK_TMP_VALUE_STACK_NAME_))))) \
   149  $(eval $(call _UTIL_MK_POP_,$(_UTIL_MK_TMP_DO_POP_STACK_NAME_))) \
   150  $(eval _UTIL_MK_TMP_DO_POP_STACK_NAME_ :=) \
   151  $(eval _UTIL_MK_TMP_VALUE_STACK_NAME_ :=) \
   152  $(eval _UTIL_MK_TMP_VALUE_NAME_ :=)
   153  endef
   154  
   155  # Expands include file path with MK_SRCDIR (or . if it is
   156  # empty). Produces spurious leading/trailing whitespace, so
   157  # _UTIL_MK_EXPAND_INC_FILE_ should be used instead.
   158  # Example: INC_FILE := $(strip $(call _UTIL_MK_EXPAND_INC_FILE_UNSTRIPPED_,bar.mk))
   159  define _UTIL_MK_EXPAND_INC_FILE_UNSTRIPPED_
   160  $(eval $(if $(MK_SRCDIR), \
   161          $(eval _UTIL_MK_TMP_SRCDIR_ := $(MK_SRCDIR)), \
   162   \
   163          $(eval _UTIL_MK_TMP_SRCDIR_ := .))) \
   164  $(eval _UTIL_MK_EXPANDED_INC_FILE_ := $(_UTIL_MK_TMP_SRCDIR_)/$1) \
   165  $(strip $(_UTIL_MK_EXPANDED_INC_FILE_)) \
   166  $(eval _UTIL_MK_TMP_SRCDIR_ :=) \
   167  $(eval _UTIL_MK_EXPANDED_INC_FILE_ :=)
   168  endef
   169  
   170  # Same as _UTIL_MK_EXPAND_INC_FILE_UNSTRIPPED_, but without
   171  # unnecessary whitespace.
   172  # Example: INC_FILE := $(call _UTIL_MK_EXPAND_INC_FILE_,bar.mk)
   173  define _UTIL_MK_EXPAND_INC_FILE_
   174  $(strip $(call _UTIL_MK_EXPAND_INC_FILE_UNSTRIPPED_,$1))
   175  endef
   176  
   177  define _UTIL_MK_CANONICALIZE_
   178  $(strip \
   179  	$(eval _UTIL_MK_TMP_PATH_ := $(abspath $1)) \
   180  	$(eval _UTIL_MK_TMP_PATH_ := $(patsubst $(MK_TOPLEVEL_ABS_SRCDIR)/%,%,$(_UTIL_MK_TMP_PATH_))) \
   181  	$(eval _UTIL_MK_TMP_PATH_ := $(MK_TOPLEVEL_SRCDIR)/$(_UTIL_MK_TMP_PATH_)) \
   182  	$(_UTIL_MK_TMP_PATH_) \
   183  	$(eval _UTIL_MK_TMP_PATH_ :=))
   184  endef
   185  
   186  # See docs of inc-one.
   187  define _UTIL_MK_INC_ONE_
   188  $(eval _UTIL_MK_INC_FILE_ := $(strip $1)) \
   189  $(eval $(if $(filter-out 1,$(words $(_UTIL_MK_INC_FILE_))),$(error Expected one file to include, got '$(_UTIL_MK_INC_FILE_)'))) \
   190   \
   191  $(eval _UTIL_MK_INC_FILE_ := $(call _UTIL_MK_EXPAND_INC_FILE_,$(_UTIL_MK_INC_FILE_))) \
   192  $(eval _UTIL_MK_INC_FILE_ := $(call _UTIL_MK_CANONICALIZE_,$(_UTIL_MK_INC_FILE_)))
   193  $(eval $(call _UTIL_MK_SAVE_VALUE_,_UTIL_MK_SRCDIR_DO_POP_,_UTIL_MK_SRCDIR_STACK_,MK_SRCDIR)) \
   194  $(eval $(call _UTIL_MK_SAVE_VALUE_,_UTIL_MK_FILENAME_DO_POP_,_UTIL_MK_FILENAME_STACK_,MK_FILENAME)) \
   195  $(eval $(call _UTIL_MK_SAVE_VALUE_,_UTIL_MK_PATH_DO_POP_,_UTIL_MK_PATH_STACK_,MK_PATH)) \
   196   \
   197  $(eval MK_SRCDIR := $(call _UTIL_MK_TO_DIR_,$(_UTIL_MK_INC_FILE_))) \
   198  $(eval MK_FILENAME := $(notdir $(_UTIL_MK_INC_FILE_))) \
   199  $(eval MK_PATH := $(_UTIL_MK_INC_FILE_)) \
   200  $(eval include $(_UTIL_MK_INC_FILE_)) \
   201   \
   202  $(eval $(call _UTIL_MK_LOAD_VALUE_,_UTIL_MK_SRCDIR_DO_POP_,_UTIL_MK_SRCDIR_STACK_,MK_SRCDIR)) \
   203  $(eval $(call _UTIL_MK_LOAD_VALUE_,_UTIL_MK_FILENAME_DO_POP_,_UTIL_MK_FILENAME_STACK_,MK_FILENAME)) \
   204  $(eval $(call _UTIL_MK_LOAD_VALUE_,_UTIL_MK_PATH_DO_POP_,_UTIL_MK_PATH_STACK_,MK_PATH))
   205  endef
   206  
   207  # Initial setup of exported variables.
   208  MK_SRCDIR := $(call _UTIL_MK_GET_THIS_DIR_)
   209  MK_FILENAME := $(call _UTIL_MK_GET_THIS_FILENAME_)
   210  MK_PATH := $(call _UTIL_MK_GET_THIS_PATH_)
   211  MK_TOPLEVEL_SRCDIR := $(MK_SRCDIR)
   212  MK_TOPLEVEL_ABS_SRCDIR := $(abspath $(MK_TOPLEVEL_SRCDIR))