srctools/cmaker/src/utils.mk
changeset 1 be27ed110b50
child 598 0a541d1f13c3
equal deleted inserted replaced
0:044383f39525 1:be27ed110b50
       
     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 "Symbian Foundation License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 # cmaker utils, stack functions (push,pop,popout,peek,length).
       
    16 # print function to echo data on several lines.
       
    17 #
       
    18 
       
    19 ichar := 
       
    20 pchar := 
       
    21 
       
    22 comma := ,
       
    23 space :=
       
    24 space +=
       
    25 squot := '\''
       
    26 
       
    27 [A-Z] := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z #
       
    28 [a-z] := a b c d e f g h i j k l m n o p q r s t u v w x y z #
       
    29 [0-9] := 0 1 2 3 4 5 6 7 8 9 #
       
    30 
       
    31 charset := $([A-Z])$([a-z])$([0-9])! " \# $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
       
    32 
       
    33 # substr(startIndex, endIndex,text)
       
    34 substr = \
       
    35   $(strip $(eval __i_str := $(subst $(space),$(ichar),$3)) \
       
    36   $(foreach c,$(charset) $(ichar),$(eval __i_str := $(subst $c,$c ,$(__i_str)))) \
       
    37   )$(subst $(ichar), ,$(subst $(space),,$(wordlist $1,$2,$(__i_str))))
       
    38 
       
    39 
       
    40 tr = \
       
    41   $(strip $(eval __i_tr := $3)    \
       
    42   $(foreach c,                    \
       
    43     $(join $(addsuffix :,$1),$2), \
       
    44     $(eval __i_tr := $(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$(__i_tr))))$(__i_tr))
       
    45 
       
    46 lcase       = $(call tr,$([A-Z]),$([a-z]),$1)
       
    47 ucase       = $(call tr,$([a-z]),$([A-Z]),$1)
       
    48 
       
    49 
       
    50 # Remove Cygwin from PATH if EXCLCYGWIN is true
       
    51 PATH        := $(shell $(PERL) -e 'print(q—$(EXCLCYGWIN)— ? join(";",grep(!/\\cygwin\\/i,split(/;+/,$$ENV{PATH}))) : $$ENV{PATH})')
       
    52 
       
    53 # remove_trail(dir)
       
    54 # remove the trailing backslash
       
    55 define remove_trail
       
    56 $(patsubst %/,%,$(patsubst %\,%,$1))
       
    57 endef
       
    58 
       
    59 MAKEFILEDIR = $(dir $(call peek,MAKEFILE_STACK))
       
    60 
       
    61 #Global variable for all export directories
       
    62 EXPORTDIRECTORIES ?= 
       
    63 
       
    64 #println(line)
       
    65 # function to print one line of data inside other defines
       
    66 # param line the data line
       
    67 define println
       
    68 	@echo $1
       
    69 
       
    70 endef
       
    71 
       
    72 define pop2
       
    73   $(word 1,$(1)) $(word 2,$(1))
       
    74 endef
       
    75 
       
    76 # tail(n, list)
       
    77 #
       
    78 # param n is the starting word
       
    79 # param list is the input data list
       
    80 # returns the tail from list starting from word n
       
    81 define tail
       
    82   $(wordlist $1,$(words $2),$2)
       
    83 endef
       
    84 
       
    85 # Adds the directory to the global export directories 
       
    86 # list if it is not already existing in it
       
    87 define add_export_dir
       
    88   $(if $(filter $1,$(EXPORTDIRECTORIES)),,\
       
    89     $(eval EXPORTDIRECTORIES+=$1)\
       
    90     $(eval $1 : ; @$(PERL) -e 'use File::Path; mkpath(q($$@))'))
       
    91 endef
       
    92 
       
    93 # addeval(list)
       
    94 #
       
    95 # Adds the items from the list as dependencies 
       
    96 # List format from to from to ..
       
    97 # param list is the input data list
       
    98 define addeval
       
    99   $(if $(strip $1),\
       
   100     $(if $(PHONY_ACT),$(eval .PHONY : $(word 2,$1)))\
       
   101     $(eval $(word 1,$1) : $(call getdir, $(dir $(word 2,$1))))\
       
   102     $(eval $(word 2,$1) : $(word 1,$1) ; $$(FUNCTION))\
       
   103     $(eval $2 :: $(word 2,$1))\
       
   104     $(call add_export_dir,$(call getdir,$(dir $(word 2,$1))))\
       
   105     $(call addeval, $(call tail,3,$1),$2))
       
   106 endef
       
   107 
       
   108 # getdir(dir)
       
   109 # returns a directory entry in lower case without trailing slash
       
   110 define getdir
       
   111 $(call remove_trail,$(strip $(call lcase,$1)))
       
   112 endef
       
   113 #$(call remove_trail,
       
   114 
       
   115 # expand a single source target definition
       
   116 # param source(s), the source file or files (wildcard)
       
   117 # param target target file or folder
       
   118 # E.g. 1. src/*.h /epoc32/config/
       
   119 #      2. src/aa.h /epoc32/config/bb.h
       
   120 define expand_wilds
       
   121   $(if $(notdir $2),$(if $(filter-out 0 1,$(words $(wildcard $1))),$(error a file target must have a single source file, in $2,$1)) )\
       
   122   $(if $(notdir $2),$(eval TARGET=$$2),$(eval TARGET=$$2$$(notdir $$(source))))\
       
   123   $(foreach source,$(wildcard $1),$(source)  $(TARGET)) 
       
   124 endef
       
   125 
       
   126 # expand_all_wilds(inputlist, concatlist)
       
   127 #
       
   128 # expands all wildcard entries from the input list and appends them 
       
   129 # to the concatlist
       
   130 # param intputlist is the input data list (from/*.* to from to ..)
       
   131 # param concatlist is the list where the expanded data is appended
       
   132 # returns a full concatenated list
       
   133 define expand_all_wilds
       
   134   $(if $(strip $1),\
       
   135     $(call expand_all_wilds,\
       
   136       $(call tail,3,$1),\
       
   137       $2 $(call expand_wilds,$(word 1,$1),$(word 2,$1))\
       
   138     ),$2\
       
   139   )
       
   140 endef
       
   141 
       
   142 # addfiles(inputlist, dependant_target)
       
   143 #
       
   144 # adds the export targets and dependancies 
       
   145 # param intputlist is the input data list (from/*.* to from to ..)
       
   146 # param dependant_target is the target which will have a dependency to these dynamic targets
       
   147 define addfiles
       
   148   $(call addeval,$(subst /,\,$(call expand_all_wilds,$1,)),$2)
       
   149 endef
       
   150 
       
   151 
       
   152 # push(list, value)
       
   153 #
       
   154 # adds the value to the given list
       
   155 # param intput list where the data is appended
       
   156 # param value that is added
       
   157 define push
       
   158   $(if $(findstring $2,$($1)),$(error ERROR: Item $2 already exists in the list!!!))\
       
   159   $(eval $1 += $2)
       
   160 endef
       
   161 
       
   162 # pop(list)
       
   163 #
       
   164 # pops out the value from the given list
       
   165 # param list where the data is popped out
       
   166 # returns the value from the list
       
   167 define pop
       
   168 $(strip $(eval value=$(lastword $($1)))\
       
   169 $(eval $1:=$(filter-out $(value),$($1)))\
       
   170 $(value))
       
   171 endef
       
   172 
       
   173 # popout(list)
       
   174 #
       
   175 # pops out the last item from the given list
       
   176 # param list where the data is popped out
       
   177 # returns nothing
       
   178 define popout
       
   179 $(eval value=$(lastword $($1)))\
       
   180 $(eval $1:=$(filter-out $(value),$($1)))
       
   181 endef
       
   182 
       
   183 # peek(list)
       
   184 #
       
   185 # peek the last element from the list without popping it out
       
   186 # param list which is peaked
       
   187 # returns the value of the last element of the list
       
   188 define peek
       
   189 $(lastword $($1))
       
   190 endef
       
   191 
       
   192 # length(list)
       
   193 #
       
   194 # returns the lenght of the stack
       
   195 # param list which is length is queried
       
   196 # returns the lenght as integer value 
       
   197 define length
       
   198 $(words $($1))
       
   199 endef