< prev index next >

make/common/TextFileProcessing.gmk

Print this page




  41         $$($1_INCLUDES_COMMAND_LINE) < '$$<' > '$$@.includes.tmp' ; \
  42         $$($1_REPLACEMENTS_COMMAND_LINE) < '$$@.includes.tmp' > '$$@.replacements.tmp' ; \
  43         $(RM) '$$@.includes.tmp' ; \
  44         $(MV) '$$@.replacements.tmp' '$$@'
  45 
  46   $1 += $(strip $3)/$(strip $4)
  47 endef
  48 
  49 # Setup make rules for processing one or more text files, in which specified
  50 # markers are replaced with a given text, or with the contents of a given file.
  51 #
  52 # Parameter 1 is the name of the rule. This name is used as variable prefix,
  53 # and the targets generated are listed in a variable by that name.
  54 #
  55 # Remaining parameters are named arguments. These include:
  56 #   SOURCE_DIRS one or more directory roots to search for files to process
  57 #   SOURCE_FILES complete paths to one or more files to process
  58 #   OUTPUT_DIR the directory where we store the processed files.
  59 #   OUTPUT_FILE the name of the resulting file. Only allowed if processing a
  60 #       single file.
  61 #   SOURCE_BASE_DIR a common root to all SOURCE_DIRS or SOURCE_FILES.
  62 #       If specified, files will keep the path relative to the base in the
  63 #       OUTPUT_DIR. Otherwise, the hierarchy will be flattened into the OUTPUT_DIR.
  64 #   INCLUDE_FILES only include files matching these patterns (used only with
  65 #       SOURCE_DIRS)
  66 #   EXCLUDE_FILES exclude files matching these patterns (used only with
  67 #       SOURCE_DIRS)
  68 #   INCLUDES replace the occurances of a pattern with the contents of a file;
  69 #       one or more such include pattern, using the syntax:
  70 #       PLACEHOLDER => FILE_TO_INCLUDE ; ...
  71 #       Each PLACEHOLDER must be on a single, otherwise empty line (whitespace
  72 #       padding is allowed).
  73 #   REPLACEMENTS one or more text replacement patterns, using the syntax:
  74 #       PATTERN => REPLACEMENT_TEXT ; ...
  75 #
  76 #   If both INCLUDES or REPLACEMENTS are present, then the includes will be
  77 #   processed first, and replacements will be done on the included fragments as well.
  78 #   If neither is present, the files will just be copied without modifications.
  79 #
  80 SetupTextFileProcessing = $(NamedParamsMacroTemplate)
  81 define SetupTextFileProcessingBody
  82 
  83   ifneq ($$($1_SOURCE_FILES),)
  84     ifneq ($$($1_SOURCE_DIRS),)
  85       $$(error Cannot use both SOURCE_FILES and SOURCE_DIRS (in $1))
  86     endif



  87     ifneq ($$($1_EXCLUDE_FILES)$$($1_INCLUDE_FILES),)
  88       $$(error Cannot INCLUDE/EXCLUDE_FILES with SOURCE_FILES (in $1))
  89     endif
  90   else
  91     ifeq ($$($1_SOURCE_DIRS),)
  92       $$(error Must specify either SOURCE_FILES or SOURCE_DIRS (in $1))
  93     endif
  94     # Find all files in the source trees. Sort to remove duplicates.
  95     $$(foreach src, $$($1_SOURCE_DIRS), $$(if $$(wildcard $$(src)), , \
  96         $$(error SOURCE_DIRS contains missing directory $$(src) (in $1))))
  97     ifneq ($$($1_SOURCE_BASE_DIR),)
  98       $$(foreach src, $$($1_SOURCE_DIRS), \
  99           $$(if $$(findstring $$($1_SOURCE_BASE_DIR), $$(src)), , \
 100           $$(error SOURCE_DIRS contains directory $$(src) outside \
 101               SOURCE_BASE_DIR $$($1_SOURCE_BASE_DIR) (in $1))))
 102     endif
 103     $1_SOURCE_FILES := $$(sort $$(call FindFiles,$$($1_SOURCE_DIRS)))
 104     $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
 105     $1_INCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
 106     $1_SOURCE_FILES := $$(filter-out $$($1_EXCLUDE_FILES),$$($1_SOURCE_FILES))


 135     else
 136       $1_SEP := /
 137     endif
 138 
 139     # If we have a trailing "=>" (i.e. last rule replaces with empty, and is not
 140     # terminated by a ;), add a trailing ; to minimize the number of corner
 141     # cases in the hairy subst expression..
 142     ifeq ($$(lastword $$($1_REPLACEMENTS)), =>)
 143       $1_REPLACEMENTS += ;
 144     endif
 145 
 146     # If we have a trailing ";", add a dummy replacement, since there is no easy
 147     # way to delete the last word in make.
 148     ifeq ($$(lastword $$($1_REPLACEMENTS)), ;)
 149       $1_REPLACEMENTS += DUMMY_REPLACEMENT => DUMMY_REPLACEMENT
 150     endif
 151 
 152     # Convert the REPLACEMENTS syntax ( A => B ; C => D ; ...) to a sed command
 153     # line (-e "s/A/B/g" -e "s/C/D/g" ...), basically by replacing '=>' with '/'
 154     # and ';' with '/g" -e "s/', and adjusting for edge cases.
 155     # '&' has special meaning in sed so needs to be escaped.
 156     $1_REPLACEMENTS_COMMAND_LINE := $(SED) -e 's$$($1_SEP)$$(subst $$(SPACE);$$(SPACE),$$($1_SEP)g' \
 157         -e 's$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE),$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE);$$(SPACE),$$($1_SEP)$$($1_SEP)g' \
 158         -e 's$$($1_SEP),$$(subst &,\&,$$(strip $$($1_REPLACEMENTS))))))$$($1_SEP)g'
 159   else
 160     # We don't have any replacements, just pipe the file through cat.
 161     $1_REPLACEMENTS_COMMAND_LINE := $(CAT)
 162   endif
 163 
 164   ifneq ($$($1_INCLUDES),)
 165     # We have a include request, prepare it for the recipe.
 166     # Convert an INCLUDE like this PATTERN_1 => file1 ; PATTERN_2 => file2 ;
 167     # into an awk script fragment like this:
 168     # {
 169     #   if (matches("PATTERN_1")) { include("file1") } else
 170     #   if (matches("PATTERN_2")) { include("file2") } else
 171     #   print
 172     # }
 173 
 174     $1_INCLUDES_HEADER_AWK := \
 175         function matches(pattern) { return ($$$$0 ~ "^[ \t]*" pattern "[ \t]*$$$$") } \
 176         function include(filename) { while ((getline < filename) == 1) print ; close(filename) }
 177     $1_INCLUDES_PARTIAL_AWK := $$(subst $$(SPACE);,,$$(subst $$(SPACE)=>$$(SPACE),"$$(RIGHT_PAREN)$$(RIGHT_PAREN) \
 178         { include$$(LEFT_PAREN)",$$(subst $$(SPACE);$$(SPACE),"$$(RIGHT_PAREN) } \




  41         $$($1_INCLUDES_COMMAND_LINE) < '$$<' > '$$@.includes.tmp' ; \
  42         $$($1_REPLACEMENTS_COMMAND_LINE) < '$$@.includes.tmp' > '$$@.replacements.tmp' ; \
  43         $(RM) '$$@.includes.tmp' ; \
  44         $(MV) '$$@.replacements.tmp' '$$@'
  45 
  46   $1 += $(strip $3)/$(strip $4)
  47 endef
  48 
  49 # Setup make rules for processing one or more text files, in which specified
  50 # markers are replaced with a given text, or with the contents of a given file.
  51 #
  52 # Parameter 1 is the name of the rule. This name is used as variable prefix,
  53 # and the targets generated are listed in a variable by that name.
  54 #
  55 # Remaining parameters are named arguments. These include:
  56 #   SOURCE_DIRS one or more directory roots to search for files to process
  57 #   SOURCE_FILES complete paths to one or more files to process
  58 #   OUTPUT_DIR the directory where we store the processed files.
  59 #   OUTPUT_FILE the name of the resulting file. Only allowed if processing a
  60 #       single file.
  61 #   SOURCE_BASE_DIR a common root to all SOURCE_DIRS.
  62 #       If specified, files will keep the path relative to the base in the
  63 #       OUTPUT_DIR. Otherwise, the hierarchy will be flattened into the OUTPUT_DIR.
  64 #   INCLUDE_FILES only include files matching these patterns (used only with
  65 #       SOURCE_DIRS)
  66 #   EXCLUDE_FILES exclude files matching these patterns (used only with
  67 #       SOURCE_DIRS)
  68 #   INCLUDES replace the occurances of a pattern with the contents of a file;
  69 #       one or more such include pattern, using the syntax:
  70 #       PLACEHOLDER => FILE_TO_INCLUDE ; ...
  71 #       Each PLACEHOLDER must be on a single, otherwise empty line (whitespace
  72 #       padding is allowed).
  73 #   REPLACEMENTS one or more text replacement patterns, using the syntax:
  74 #       PATTERN => REPLACEMENT_TEXT ; ...
  75 #
  76 #   If both INCLUDES or REPLACEMENTS are present, then the includes will be
  77 #   processed first, and replacements will be done on the included fragments as well.
  78 #   If neither is present, the files will just be copied without modifications.
  79 #
  80 SetupTextFileProcessing = $(NamedParamsMacroTemplate)
  81 define SetupTextFileProcessingBody
  82 
  83   ifneq ($$($1_SOURCE_FILES),)
  84     ifneq ($$($1_SOURCE_DIRS),)
  85       $$(error Cannot use both SOURCE_FILES and SOURCE_DIRS (in $1))
  86     endif
  87     ifneq ($$($1_SOURCE_BASE_DIR),)
  88       $$(error Cannot use SOURCE_BASE_DIR without SOURCE_DIRS (in $1))
  89     endif
  90     ifneq ($$($1_EXCLUDE_FILES)$$($1_INCLUDE_FILES),)
  91       $$(error Cannot INCLUDE/EXCLUDE_FILES with SOURCE_FILES (in $1))
  92     endif
  93   else
  94     ifeq ($$($1_SOURCE_DIRS),)
  95       $$(error Must specify either SOURCE_FILES or SOURCE_DIRS (in $1))
  96     endif
  97     # Find all files in the source trees. Sort to remove duplicates.
  98     $$(foreach src, $$($1_SOURCE_DIRS), $$(if $$(wildcard $$(src)), , \
  99         $$(error SOURCE_DIRS contains missing directory $$(src) (in $1))))
 100     ifneq ($$($1_SOURCE_BASE_DIR),)
 101       $$(foreach src, $$($1_SOURCE_DIRS), \
 102           $$(if $$(findstring $$($1_SOURCE_BASE_DIR), $$(src)), , \
 103           $$(error SOURCE_DIRS contains directory $$(src) outside \
 104               SOURCE_BASE_DIR $$($1_SOURCE_BASE_DIR) (in $1))))
 105     endif
 106     $1_SOURCE_FILES := $$(sort $$(call FindFiles,$$($1_SOURCE_DIRS)))
 107     $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
 108     $1_INCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
 109     $1_SOURCE_FILES := $$(filter-out $$($1_EXCLUDE_FILES),$$($1_SOURCE_FILES))


 138     else
 139       $1_SEP := /
 140     endif
 141 
 142     # If we have a trailing "=>" (i.e. last rule replaces with empty, and is not
 143     # terminated by a ;), add a trailing ; to minimize the number of corner
 144     # cases in the hairy subst expression..
 145     ifeq ($$(lastword $$($1_REPLACEMENTS)), =>)
 146       $1_REPLACEMENTS += ;
 147     endif
 148 
 149     # If we have a trailing ";", add a dummy replacement, since there is no easy
 150     # way to delete the last word in make.
 151     ifeq ($$(lastword $$($1_REPLACEMENTS)), ;)
 152       $1_REPLACEMENTS += DUMMY_REPLACEMENT => DUMMY_REPLACEMENT
 153     endif
 154 
 155     # Convert the REPLACEMENTS syntax ( A => B ; C => D ; ...) to a sed command
 156     # line (-e "s/A/B/g" -e "s/C/D/g" ...), basically by replacing '=>' with '/'
 157     # and ';' with '/g" -e "s/', and adjusting for edge cases.

 158     $1_REPLACEMENTS_COMMAND_LINE := $(SED) -e 's$$($1_SEP)$$(subst $$(SPACE);$$(SPACE),$$($1_SEP)g' \
 159         -e 's$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE),$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE);$$(SPACE),$$($1_SEP)$$($1_SEP)g' \
 160         -e 's$$($1_SEP),$$(strip $$($1_REPLACEMENTS)))))$$($1_SEP)g'
 161   else
 162     # We don't have any replacements, just pipe the file through cat.
 163     $1_REPLACEMENTS_COMMAND_LINE := $(CAT)
 164   endif
 165 
 166   ifneq ($$($1_INCLUDES),)
 167     # We have a include request, prepare it for the recipe.
 168     # Convert an INCLUDE like this PATTERN_1 => file1 ; PATTERN_2 => file2 ;
 169     # into an awk script fragment like this:
 170     # {
 171     #   if (matches("PATTERN_1")) { include("file1") } else
 172     #   if (matches("PATTERN_2")) { include("file2") } else
 173     #   print
 174     # }
 175 
 176     $1_INCLUDES_HEADER_AWK := \
 177         function matches(pattern) { return ($$$$0 ~ "^[ \t]*" pattern "[ \t]*$$$$") } \
 178         function include(filename) { while ((getline < filename) == 1) print ; close(filename) }
 179     $1_INCLUDES_PARTIAL_AWK := $$(subst $$(SPACE);,,$$(subst $$(SPACE)=>$$(SPACE),"$$(RIGHT_PAREN)$$(RIGHT_PAREN) \
 180         { include$$(LEFT_PAREN)",$$(subst $$(SPACE);$$(SPACE),"$$(RIGHT_PAREN) } \


< prev index next >