sbsv2/raptor/lib/flm/flmtools.mk
changeset 0 044383f39525
child 3 e1eecf4d390d
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # Tools for use in FLMs - common macros and variables
       
    16 #
       
    17 
       
    18 ifeq ($(FLMTOOLS.MK),)
       
    19 FLMTOOLS.MK:=included
       
    20 
       
    21 CHAR_COMMA:=,
       
    22 CHAR_SEMIC:=;
       
    23 CHAR_COLON:=:
       
    24 CHAR_BLANK:=
       
    25 #the BLANK BLANK trick saves us from clever text editrors that remove spaces at the end of a line
       
    26 CHAR_SPACE:=$(BLANK) $(BLANK)
       
    27 CHAR_DQUOTE:="
       
    28 CHAR_QUOTE:='
       
    29 CHAR_LBRACKET:=(
       
    30 CHAR_RBRACKET:=)
       
    31 CHAR_DOLLAR:=$
       
    32 
       
    33 #'" # This comment makes syntax highlighting work again. Leave it here please!
       
    34 
       
    35 # A macro to ensure that the shell does not 
       
    36 # interpret brackets in a command
       
    37 define shEscapeBrackets
       
    38 $(subst $(CHAR_LBRACKET),\$(CHAR_LBRACKET),$(subst $(CHAR_RBRACKET),\$(CHAR_RBRACKET),$(1)))
       
    39 endef
       
    40 
       
    41 # A macro to protect quotes from shell expansion
       
    42 define shEscapeQuotes
       
    43 $(subst $(CHAR_QUOTE),\$(CHAR_QUOTE),$(subst $(CHAR_DQUOTE),\$(CHAR_DQUOTE),$(1)))
       
    44 endef
       
    45 
       
    46 # Protect against shell expansion
       
    47 define shEscape
       
    48 $(call shEscapeBrackets,$(call shEscapeQuotes,$(1)))
       
    49 endef
       
    50 
       
    51 # A macro to escape spaces for use in rule targets or dependencies
       
    52 define ruleEscape
       
    53 $(subst $(CHAR_SPACE),\$(CHAR_SPACE),$(1))
       
    54 endef
       
    55 
       
    56 # A macro for turning a list into a concatenated string
       
    57 # 1st parameter is the separator to use, second is the list
       
    58 # e.g. $(call concat,:,/sbin /usr/sbin /usr/local/sbin /bin /usr/bin /usr/local/bin)
       
    59 # would create the string
       
    60 #   /sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin
       
    61 # It has to be recursive to manage to create a string from a list
       
    62 define concat
       
    63 $(if $(word 2,$(2)),$(firstword $(2))$(1)$(call concat,$(1),$(wordlist 2,$(words $(2)),$(2))),$(2))
       
    64 endef
       
    65 
       
    66 # A macro for converting a string to lowercase
       
    67 lowercase_TABLE:=A,a B,b C,c D,d E,e F,f G,g H,h I,i J,j K,k L,l M,m N,n O,o P,p Q,q R,r S,s T,t U,u V,v W,w X,x Y,y Z,z
       
    68 
       
    69 define lowercase_internal
       
    70 $(if $1,$$(subst $(firstword $1),$(call lowercase_internal,$(wordlist 2,$(words $1),$1),$2)),$2)
       
    71 endef
       
    72 
       
    73 define lowercase
       
    74 $(eval lowercase_RESULT:=$(call lowercase_internal,$(lowercase_TABLE),$1))$(lowercase_RESULT)
       
    75 endef
       
    76 
       
    77 # A macro for converting a string to uppercase
       
    78 uppercase_TABLE:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
       
    79 
       
    80 define uppercase_internal
       
    81 $(if $1,$$(subst $(firstword $1),$(call uppercase_internal,$(wordlist 2,$(words $1),$1),$2)),$2)
       
    82 endef
       
    83 
       
    84 define uppercase
       
    85 $(eval uppercase_RESULT:=$(call uppercase_internal,$(uppercase_TABLE),$1))$(uppercase_RESULT)
       
    86 endef
       
    87 
       
    88 # A macro for removing duplicate tokens from a list 
       
    89 # whilst retaining the list's order
       
    90 define uniq
       
    91 $(if $(1),$(info $(words $(1)) $(1))\
       
    92 $(firstword $(1))$(call uniq,$(filter-out $(firstword $(1)),$(wordlist 2,$(words $(1)),$(1))))\
       
    93 ,)
       
    94 endef
       
    95 
       
    96 # A macro for enclosing all list elements in some kind of quote or bracket
       
    97 define enclose
       
    98 $(foreach ITEM,$(2),$(1)$(ITEM)$(1))
       
    99 endef
       
   100 
       
   101 # A macro enclosing all list elements in double quotes and removing escapes
       
   102 define dblquote
       
   103 $(subst \,,$(call enclose,$(CHAR_DQUOTE),$(1)))
       
   104 endef
       
   105 
       
   106 # A macro enclosing a single item in double quotes, so that spaces can be quoted
       
   107 define dblquoteitem
       
   108 $(if $(1),$(CHAR_DQUOTE)$(1)$(CHAR_DQUOTE),)
       
   109 endef
       
   110 
       
   111 # A macro to add a prefix to a list of items, while putting quotes around each
       
   112 # item so prefixed, to allow spaces in the prefix.
       
   113 define addquotedprefix
       
   114 $(addprefix $(CHAR_DQUOTE)$(1),$(addsuffix $(CHAR_DQUOTE),$(2)))
       
   115 endef
       
   116 
       
   117 # A macro enclosing all list elements in single quotes and prepending a string
       
   118 # basically to turn a list like:
       
   119 #    LINUX TOOLS EXP=_declspec(export) NODEBUG
       
   120 # into:
       
   121 #    -D'LINUX' -D'TOOLS' -D'EXP=_declspec(export)' -DNODEBUG
       
   122 define makemacrodef
       
   123 $(strip $(patsubst %,$1'%',$2))
       
   124 endef
       
   125 
       
   126 # Make a (filename) string safe for use as a variable name
       
   127 define sanitise
       
   128 $(subst /,_,$(subst :,_,$(1)))
       
   129 endef
       
   130 
       
   131 # A variation on several macros implemented directly in FLMs, this takes a filename and a list
       
   132 # of "words" and pumps groups of 10 words at a time into the file.  Its main use is the creation
       
   133 # of command/response files for tools that need to avoid blowing command line length limits
       
   134 # when referencing long lists of absolutely pathed files.
       
   135 define groupin10infile
       
   136 	$(if $2,@echo -e $(foreach L,$(wordlist 1,10,$2),"$(L)\\n") >>$1,)
       
   137 	$(if $2,$(call groupin10infile,$1,$(wordlist 11,$(words $2),$2)),@true)
       
   138 endef
       
   139 
       
   140 ## DEBUGGING Macros ######################################
       
   141 
       
   142 # A macro to help with debugging FLMs by printing out variables specified in the FLMDEBUG variable
       
   143 define flmdebug
       
   144 $(if $(FLMDEBUG),@echo -e "<flmdebug>\\n $(foreach VAR,$(FLMDEBUG),$(VAR):=$($(VAR))\\n)</flmdebug>",)
       
   145 endef
       
   146 
       
   147 define flmdebug2
       
   148 flmdebug2_OUT:=$(if $(FLMDEBUG),$(shell echo -e "<flmdebug>\\n $(foreach VAR,$(FLMDEBUG),$(VAR):=$($(VAR))\\n)</flmdebug>" 1>&2),)
       
   149 endef
       
   150 
       
   151 ## Path handling tools ###################################
       
   152 
       
   153 ifeq ($(OSTYPE),cygwin)
       
   154 
       
   155 SPACESLASH:=$(CHAR_SPACE)/
       
   156 SPACESLASH2:=$(CHAR_SPACE)\/
       
   157 SPACEDSLASH:=$(CHAR_SPACE)//
       
   158 # How lists of directories are separated on this OS:
       
   159 DIRSEP:=$(CHAR_SEMIC)
       
   160 
       
   161 define slashprotect
       
   162 $(subst $(SPACESLASH),$(SPACESLASH2),$(1))
       
   163 endef
       
   164 
       
   165 define pathprep
       
   166 $(subst $(SPACESLASH),$(SPACEDSLASH),$(1))
       
   167 endef
       
   168 
       
   169 else
       
   170 
       
   171 # How lists of directories are separated on this OS:
       
   172 DIRSEP:=$(CHAR_COLON)
       
   173 
       
   174 define slashprotect
       
   175 $(1)
       
   176 endef
       
   177 
       
   178 define pathprep
       
   179 $(1)
       
   180 endef
       
   181 
       
   182 endif
       
   183 
       
   184 # Path separator (always / in make)
       
   185 PATHSEP=/
       
   186 
       
   187 ## Source to object file mapping #########################
       
   188 
       
   189 ## Converting a list of source files to a list of object files without altering their
       
   190 ## relative order.  Also deals with multiple file types mapping to the same object type
       
   191 ## e.g. .CPP and .cpp and .c++ all map to .o
       
   192 define allsuffixsubst_internal
       
   193 $(if $1,$$(patsubst %$(firstword $1),%$2,$(call allsuffixsubst_internal,$(wordlist 2,$(words $1),$1),$2,$3)),$3)
       
   194 endef
       
   195 
       
   196 # $1 - the list of suffixes to replace
       
   197 # $2 - the suffix to replace them with
       
   198 # $3 - the list of strings to perform the replacement on
       
   199 define allsuffixsubst
       
   200 $(eval allsuffixsubst_RESULT:=$(call allsuffixsubst_internal,$1,$2,$3))$(allsuffixsubst_RESULT)
       
   201 endef
       
   202 
       
   203 ## extractfilesoftype ##
       
   204 # $(1) is the list of types to extract e.g. cpp cxx CPP
       
   205 # $(2) is the list of files to select from
       
   206 define extractfilesoftype
       
   207 $(foreach EXT,$(1),$(filter %$(EXT),$(2)))
       
   208 endef
       
   209 
       
   210 ## extractandmap ##
       
   211 # $(1) is the list of types e.g. cpp cxx CPP
       
   212 # $(2) is the extension to map to e.g. .o or .ARMV5.lst oe _.cpp
       
   213 # $(3) is the list of files to select from
       
   214 # This functon turns a list like 'fred.cpp bob.c++' into 'fred.o bob.o'
       
   215 define extractandmap
       
   216 $(foreach EXT,$(1),$(patsubst %$(EXT),%$(2),$(filter %$(EXT),$(3))))
       
   217 endef
       
   218 
       
   219 ## relocatefiles ##
       
   220 # $(1) directory to relocate them in
       
   221 # $(2) list of files to relocate
       
   222 define relocatefiles
       
   223 $(patsubst %,$(1)/%,$(notdir $(2)))
       
   224 endef
       
   225 
       
   226 
       
   227 ## Get stack handling code (for calls to FLMS which don't destroy variables)
       
   228 include $(FLMHOME)/stack.mk
       
   229 
       
   230 ## Allow flm rules to be tagged and enable FORCESUCCESS feature etc
       
   231 include $(FLMHOME)/taggedrules.mk
       
   232 
       
   233 ## Get boolean tools e.g. that implement the equals macro.
       
   234 include $(FLMHOME)/booleanlogic.mk
       
   235 
       
   236 ## Macros for writing recipes without needing to know eval
       
   237 #
       
   238 # an ordinary recipe, for example,
       
   239 #
       
   240 # $(call raptor_recipe,name,$(TARGET),$(PREREQUISITES),$(COMMAND))
       
   241 #
       
   242 define raptor_recipe
       
   243 $(eval $2: $3
       
   244 	$(call startrule,$1) $4 $(call endrule,$1)
       
   245 )
       
   246 endef
       
   247 #
       
   248 # a phony recipe (double colon rule). Making a separate macro with an uglier
       
   249 # name will hopefully discourage people from using this unless they need to,
       
   250 # for example,
       
   251 #
       
   252 # $(call raptor_phony_recipe,name,ALL,,$(COMMAND))
       
   253 #
       
   254 define raptor_phony_recipe
       
   255 $(eval $2:: $3
       
   256 	$(call startrule,$1) $4 $(call endrule,$1)
       
   257 )
       
   258 endef
       
   259 
       
   260 ################################################################################
       
   261 ## Test code to allow this makefile fragment to be tested in a standalone manner
       
   262 ##
       
   263 ## example:
       
   264 ##   FLMHOME=./ make -f flmtools.mk  STANDALONE_TEST=1 
       
   265 ##
       
   266 
       
   267 ifneq ($(STANDALONE_TEST),)
       
   268 
       
   269 test::
       
   270 	@echo "macros"
       
   271 	test  "$(call concat,:,CON CAT EN ATED)" == "CON:CAT:EN:ATED" 
       
   272 	test  "$(call enclose,$(CHAR_QUOTE),THESE WORDS ARE QUOTED)" == "'THESE' 'WORDS' 'ARE' 'QUOTED'" 
       
   273 	echo $(SHELL)
       
   274 	test  '$(call dblquote,THESE WORDS ARE DOUBLEQUOTED)' == '"THESE" "WORDS" "ARE" "DOUBLEQUOTED"'
       
   275 	echo  $(call dqnp,/c/fred /d/alice /blah/blah)
       
   276 	@echo ""
       
   277 
       
   278 
       
   279 test::
       
   280 	@echo lowercase macro
       
   281 	test '$(call lowercase,ABCDEFGHIJKLMNOPQRSTUVWXYZ AA BB CC)' == 'abcdefghijklmnopqrstuvwxyz aa bb cc'
       
   282 
       
   283 define lowercase_internal
       
   284 $(if $1,$$(subst $(firstword $1),$(call lowercase_internal,$(wordlist 2,$(words $1),$1),$2)),$2)
       
   285 endef
       
   286 
       
   287 define lowercase
       
   288 $(eval lowercase_RESULT:=$(call lowercase_internal,$(lowercase_TABLE),$1))$(lowercase_RESULT)
       
   289 endef
       
   290 
       
   291 	test '$(call pathprep,--apcs /inter)' == '--apcs //inter'
       
   292 	
       
   293 test::
       
   294 	@echo "pathprep macro"
       
   295 	test '$(call pathprep,--apcs /inter)' == '--apcs //inter'
       
   296 	test '$(call pathprep,blah theone/or/theother)' == 'blah theone/or/theother'
       
   297 
       
   298 endif
       
   299 endif
       
   300