TextFileProcessing.gmk revision 1299:8720fa7696cb
1#
2# Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# This code is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 2 only, as
7# published by the Free Software Foundation.  Oracle designates this
8# particular file as subject to the "Classpath" exception as provided
9# by Oracle in the LICENSE file that accompanied this code.
10#
11# This code is distributed in the hope that it will be useful, but WITHOUT
12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14# version 2 for more details (a copy is included in the LICENSE file that
15# accompanied this code).
16#
17# You should have received a copy of the GNU General Public License version
18# 2 along with this work; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20#
21# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22# or visit www.oracle.com if you need additional information or have any
23# questions.
24#
25
26ifeq (,$(_MAKEBASE_GMK))
27  $(error You must include MakeBase.gmk prior to including TextFileProcessing.gmk)
28endif
29
30# Helper function for SetupTextFileProcessing; adds a rule for a single file
31# to be processed.
32# param 1 = The namespace argument, e.g. BUILD_VERSION_FILE
33# param 2 = the source file name (full path)
34# param 3 = the target base directory
35# param 4 = the target file name (possibly with a partial path)
36define SetupSingleTextFileForProcessing
37  $(strip $3)/$(strip $4): $2 $$($1_VARDEPS_FILE)
38	$(ECHO) $(LOG_INFO) "Processing $(strip $4)"
39	$(MKDIR) -p '$$(@D)'
40	$(RM) '$$@' '$$@.includes.tmp' '$$@.replacements.tmp'
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)
47endef
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#
80define SetupTextFileProcessing
81  $(if $(16),$(error Internal makefile error: Too many arguments to SetupTextFileProcessing, please update TextFileProcessing.gmk))
82  $(call EvalDebugWrapper,$(strip $1),$(call SetupTextFileProcessingInner,$(strip $1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15)))
83endef
84
85define SetupTextFileProcessingInner
86  $(foreach i,2 3 4 5 6 7 8 9 10 11 12 13 14 15, $(if $(strip $($i)),$1_$(strip $($i)))$(NEWLINE))
87  $(call LogSetupMacroEntry,SetupTextFileProcessing($1),$2,$3,$4,$5,$6,$7,$8,$9,$(10),$(11),$(12),$(13),$(14),$(15))
88  $(if $(16),$(error Internal makefile error: Too many arguments to SetupTextFileProcessing, please update TextFileProcessing.gmk))
89
90  ifneq ($$($1_SOURCE_FILES),)
91    ifneq ($$($1_SOURCE_DIRS),)
92      $$(error Cannot use both SOURCE_FILES and SOURCE_DIRS (in $1))
93    endif
94    ifneq ($$($1_SOURCE_BASE_DIR),)
95      $$(error Cannot use SOURCE_BASE_DIR without SOURCE_DIRS (in $1))
96    endif
97    ifneq ($$($1_EXCLUDE_FILES)$$($1_INCLUDE_FILES),)
98      $$(error Cannot INCLUDE/EXCLUDE_FILES with SOURCE_FILES (in $1))
99    endif
100  else
101    ifeq ($$($1_SOURCE_DIRS),)
102      $$(error Must specify either SOURCE_FILES or SOURCE_DIRS (in $1))
103    endif
104    # Find all files in the source trees. Sort to remove duplicates.
105    $$(foreach src, $$($1_SOURCE_DIRS), $$(if $$(wildcard $$(src)), , \
106        $$(error SOURCE_DIRS contains missing directory $$(src) (in $1))))
107    ifneq ($$($1_SOURCE_BASE_DIR),)
108      $$(foreach src, $$($1_SOURCE_DIRS), \
109          $$(if $$(findstring $$($1_SOURCE_BASE_DIR), $$(src)), , \
110          $$(error SOURCE_DIRS contains directory $$(src) outside \
111              SOURCE_BASE_DIR $$($1_SOURCE_BASE_DIR) (in $1))))
112    endif
113    $1_SOURCE_FILES := $$(sort $$(call CacheFind,$$($1_SOURCE_DIRS)))
114    $1_EXCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_EXCLUDE_FILES)))
115    $1_INCLUDE_FILES:=$$(foreach i,$$($1_SOURCE_DIRS),$$(addprefix $$i/,$$($1_INCLUDE_FILES)))
116    $1_SOURCE_FILES := $$(filter-out $$($1_EXCLUDE_FILES),$$($1_SOURCE_FILES))
117    ifneq (,$$(strip $$($1_INCLUDE_FILES)))
118      $1_SOURCE_FILES := $$(filter $$($1_INCLUDE_FILES),$$($1_SOURCE_FILES))
119    endif
120    ifeq (,$$($1_SOURCE_FILES))
121      $$(info No sources found for $1 when looking inside the dirs $$($1_SRC))
122    endif
123  endif
124
125  ifneq ($$($1_REPLACEMENTS),)
126    # We have a replacement request, prepare it for the recipe
127    ifneq ($$(findstring /,$$($1_REPLACEMENTS)),)
128      # Cannot use / as separator
129      ifneq ($$(findstring @,$$($1_REPLACEMENTS)),)
130        # Cannot use @ as separator
131        ifneq ($$(findstring |,$$($1_REPLACEMENTS)),)
132          # Cannot use | as separator
133          ifneq ($$(findstring !,$$($1_REPLACEMENTS)),)
134            # Cannot use ! as separator. Give up.
135            $$(error No suitable sed separator can be found for $1. Tested /, @, | and !)
136          else
137            $1_SEP := !
138          endif
139        else
140          $1_SEP := |
141        endif
142      else
143        $1_SEP := @
144      endif
145    else
146      $1_SEP := /
147    endif
148
149    # If we have a trailing "=>" (i.e. last rule replaces with empty, and is not
150    # terminated by a ;), add a trailing ; to minimize the number of corner
151    # cases in the hairy subst expression..
152    ifeq ($$(lastword $$($1_REPLACEMENTS)), =>)
153      $1_REPLACEMENTS += ;
154    endif
155
156    # If we have a trailing ";", add a dummy replacement, since there is no easy
157    # way to delete the last word in make.
158    ifeq ($$(lastword $$($1_REPLACEMENTS)), ;)
159      $1_REPLACEMENTS += DUMMY_REPLACEMENT => DUMMY_REPLACEMENT
160    endif
161
162    # Convert the REPLACEMENTS syntax ( A => B ; C => D ; ...) to a sed command
163    # line (-e "s/A/B/" -e "s/C/D/" ...), basically by replacing '=>' with '/'
164    # and ';' with '/" -e "s/', and adjusting for edge cases.
165    $1_REPLACEMENTS_COMMAND_LINE := $(SED) -e 's$$($1_SEP)$$(subst $$(SPACE);$$(SPACE),$$($1_SEP)' \
166        -e 's$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE),$$($1_SEP),$$(subst $$(SPACE)=>$$(SPACE);$$(SPACE),$$($1_SEP)$$($1_SEP)' \
167        -e 's$$($1_SEP),$$(strip $$($1_REPLACEMENTS)))))$$($1_SEP)'
168  else
169    # We don't have any replacements, just pipe the file through cat.
170    $1_REPLACEMENTS_COMMAND_LINE := $(CAT)
171  endif
172
173  ifneq ($$($1_INCLUDES),)
174    # We have a include request, prepare it for the recipe.
175    # Convert an INCLUDE like this PATTERN_1 => file1 ; PATTERN_2 => file2 ;
176    # into an awk script fragment like this:
177    # {
178    #   if (matches("PATTERN_1")) { include("file1") } else
179    #   if (matches("PATTERN_2")) { include("file2") } else
180    #   print
181    # }
182
183    $1_INCLUDES_HEADER_AWK := \
184        function matches(pattern) { return ($$$$0 ~ "^[ \t]*" pattern "[ \t]*$$$$") } \
185        function include(filename) { while ((getline < filename) == 1) print ; close(filename) }
186    $1_INCLUDES_PARTIAL_AWK := $$(subst $$(SPACE);,,$$(subst $$(SPACE)=>$$(SPACE),"$$(RIGHT_PAREN)$$(RIGHT_PAREN) \
187        { include$$(LEFT_PAREN)",$$(subst $$(SPACE);$$(SPACE),"$$(RIGHT_PAREN) } \
188        else if $$(LEFT_PAREN)matches$$(LEFT_PAREN)",$$(strip $$($1_INCLUDES)))))
189    $1_INCLUDES_COMMAND_LINE := $(NAWK) '$$($1_INCLUDES_HEADER_AWK) \
190        { if (matches("$$($1_INCLUDES_PARTIAL_AWK)") } else print }'
191  else
192    # We don't have any includes, just pipe the file through cat.
193    $1_INCLUDES_COMMAND_LINE := $(CAT)
194  endif
195
196  $1_VARDEPS := $$($1_INCLUDES_COMMAND_LINE) $$($1_REPLACEMENTS_COMMAND_LINE)
197  $1_VARDEPS_FILE := $$(call DependOnVariable, $1_VARDEPS)
198
199  # Reset target list before populating it
200  $1 :=
201
202  ifneq ($$($1_OUTPUT_FILE),)
203    ifneq ($$(words $$($1_SOURCE_FILES)), 1)
204      $$(error Cannot use OUTPUT_FILE for more than one source file (in $1))
205    endif
206
207    # Note that $1 is space sensitive and must disobey whitespace rules
208    $$(eval $$(call SetupSingleTextFileForProcessing,$1, $$($1_SOURCE_FILES), \
209        $$(patsubst %/, %, $$(dir $$($1_OUTPUT_FILE))), $$(notdir $$($1_OUTPUT_FILE))))
210  else
211    ifeq ($$($1_OUTPUT_DIR),)
212      $$(error Neither OUTPUT_FILE nor OUTPUT_DIR was specified (in $1))
213    endif
214
215    # Now call add_native_source for each source file we are going to process.
216    ifeq ($$($1_SOURCE_BASE_DIR),)
217      # With no base dir specified, put all files in target dir, flattening any
218      # hierarchies. Note that $1 is space sensitive and must disobey whitespace
219      # rules.
220      $$(foreach src, $$($1_SOURCE_FILES), \
221          $$(eval $$(call SetupSingleTextFileForProcessing,$1, $$(src), \
222              $$(patsubst %/, %, $$($1_OUTPUT_DIR)), $$(notdir $$(src)))))
223    else
224      # With a base dir, extract the relative portion of the path. Note that $1
225      # is space sensitive and must disobey whitespace rules, and so is the
226      # arguments to patsubst.
227      $$(foreach src, $$($1_SOURCE_FILES), \
228          $$(eval $$(call SetupSingleTextFileForProcessing,$1, $$(src), \
229              $$(patsubst %/, %, $$($1_OUTPUT_DIR)), \
230              $$(patsubst $$($1_SOURCE_BASE_DIR)/%,%,$$(src)))))
231    endif
232  endif
233endef
234