__gmsl 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. # ----------------------------------------------------------------------------
  2. #
  3. # GNU Make Standard Library (GMSL)
  4. #
  5. # A library of functions to be used with GNU Make's $(call) that
  6. # provides functionality not available in standard GNU Make.
  7. #
  8. # Copyright (c) 2005-2018 John Graham-Cumming
  9. #
  10. # This file is part of GMSL
  11. #
  12. # Redistribution and use in source and binary forms, with or without
  13. # modification, are permitted provided that the following conditions
  14. # are met:
  15. #
  16. # Redistributions of source code must retain the above copyright
  17. # notice, this list of conditions and the following disclaimer.
  18. #
  19. # Redistributions in binary form must reproduce the above copyright
  20. # notice, this list of conditions and the following disclaimer in the
  21. # documentation and/or other materials provided with the distribution.
  22. #
  23. # Neither the name of the John Graham-Cumming nor the names of its
  24. # contributors may be used to endorse or promote products derived from
  25. # this software without specific prior written permission.
  26. #
  27. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. # POSSIBILITY OF SUCH DAMAGE.
  39. #
  40. # ----------------------------------------------------------------------------
  41. # This is the GNU Make Standard Library version number as a list with
  42. # three items: major, minor, revision
  43. gmsl_version := 1 1 8
  44. __gmsl_name := GNU Make Standard Library
  45. # Used to output warnings and error from the library, it's possible to
  46. # disable any warnings or errors by overriding these definitions
  47. # manually or by setting GMSL_NO_WARNINGS or GMSL_NO_ERRORS
  48. ifdef GMSL_NO_WARNINGS
  49. __gmsl_warning :=
  50. else
  51. __gmsl_warning = $(if $1,$(warning $(__gmsl_name): $1))
  52. endif
  53. ifdef GMSL_NO_ERRORS
  54. __gmsl_error :=
  55. else
  56. __gmsl_error = $(if $1,$(error $(__gmsl_name): $1))
  57. endif
  58. # If GMSL_TRACE is enabled then calls to the library functions are
  59. # traced to stdout using warning messages with their arguments
  60. ifdef GMSL_TRACE
  61. __gmsl_tr1 = $(warning $0('$1'))
  62. __gmsl_tr2 = $(warning $0('$1','$2'))
  63. __gmsl_tr3 = $(warning $0('$1','$2','$3'))
  64. else
  65. __gmsl_tr1 :=
  66. __gmsl_tr2 :=
  67. __gmsl_tr3 :=
  68. endif
  69. # See if spaces are valid in variable names (this was the case until
  70. # GNU Make 3.82)
  71. ifeq ($(MAKE_VERSION),3.82)
  72. __gmsl_spaced_vars := $(false)
  73. else
  74. __gmsl_spaced_vars := $(true)
  75. endif
  76. # Figure out whether we have $(eval) or not (GNU Make 3.80 and above)
  77. # if we do not then output a warning message, if we do then some
  78. # functions will be enabled.
  79. __gmsl_have_eval := $(false)
  80. __gmsl_ignore := $(eval __gmsl_have_eval := $(true))
  81. # If this is being run with Electric Cloud's emake then warn that
  82. # their $(eval) support is incomplete in 1.x, 2.x, 3.x, 4.x and 5.0,
  83. # 5.1, 5.2 and 5.3
  84. ifdef ECLOUD_BUILD_ID
  85. __gmsl_emake_major := $(word 1,$(subst ., ,$(EMAKE_VERSION)))
  86. __gmsl_emake_minor := $(word 2,$(subst ., ,$(EMAKE_VERSION)))
  87. ifneq ("$(findstring $(__gmsl_emake_major),1 2 3 4)$(findstring $(__gmsl_emake_major)$(__gmsl_emake_minor),50 51 52 53)","")
  88. $(warning You are using a version of Electric Cloud's emake which has incomplete $$(eval) support)
  89. __gmsl_have_eval := $(false)
  90. endif
  91. endif
  92. # See if we have $(lastword) (GNU Make 3.81 and above)
  93. __gmsl_have_lastword := $(lastword $(false) $(true))
  94. # See if we have native or and and (GNU Make 3.81 and above)
  95. __gmsl_have_or := $(if $(filter-out undefined, \
  96. $(origin or)),$(call or,$(true),$(false)))
  97. __gmsl_have_and := $(if $(filter-out undefined, \
  98. $(origin and)),$(call and,$(true),$(true)))
  99. ifneq ($(__gmsl_have_eval),$(true))
  100. $(call __gmsl_warning,Your make version $(MAKE_VERSION) does not support $$$$(eval): some functions disabled)
  101. endif
  102. __gmsl_dollar := $$
  103. __gmsl_hash := \#
  104. # ----------------------------------------------------------------------------
  105. # ----------------------------------------------------------------------------
  106. # Function: gmsl_compatible
  107. # Arguments: List containing the desired library version number (maj min rev)
  108. # Returns: $(true) if this version of the library is compatible
  109. # with the requested version number, otherwise $(false)
  110. # ----------------------------------------------------------------------------
  111. gmsl_compatible = $(strip \
  112. $(if $(call gt,$(word 1,$1),$(word 1,$(gmsl_version))), \
  113. $(false), \
  114. $(if $(call lt,$(word 1,$1),$(word 1,$(gmsl_version))), \
  115. $(true), \
  116. $(if $(call gt,$(word 2,$1),$(word 2,$(gmsl_version))), \
  117. $(false), \
  118. $(if $(call lt,$(word 2,$1),$(word 2,$(gmsl_version))), \
  119. $(true), \
  120. $(call lte,$(word 3,$1),$(word 3,$(gmsl_version))))))))
  121. # ###########################################################################
  122. # LOGICAL OPERATORS
  123. # ###########################################################################
  124. # not is defined in gmsl
  125. # ----------------------------------------------------------------------------
  126. # Function: and
  127. # Arguments: Two boolean values
  128. # Returns: Returns $(true) if both of the booleans are true
  129. # ----------------------------------------------------------------------------
  130. ifneq ($(__gmsl_have_and),$(true))
  131. and = $(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(false))
  132. endif
  133. # ----------------------------------------------------------------------------
  134. # Function: or
  135. # Arguments: Two boolean values
  136. # Returns: Returns $(true) if either of the booleans is true
  137. # ----------------------------------------------------------------------------
  138. ifneq ($(__gmsl_have_or),$(true))
  139. or = $(__gmsl_tr2)$(if $1$2,$(true),$(false))
  140. endif
  141. # ----------------------------------------------------------------------------
  142. # Function: xor
  143. # Arguments: Two boolean values
  144. # Returns: Returns $(true) if exactly one of the booleans is true
  145. # ----------------------------------------------------------------------------
  146. xor = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(if $2,$(true),$(false)))
  147. # ----------------------------------------------------------------------------
  148. # Function: nand
  149. # Arguments: Two boolean values
  150. # Returns: Returns value of 'not and'
  151. # ----------------------------------------------------------------------------
  152. nand = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(true))
  153. # ----------------------------------------------------------------------------
  154. # Function: nor
  155. # Arguments: Two boolean values
  156. # Returns: Returns value of 'not or'
  157. # ----------------------------------------------------------------------------
  158. nor = $(__gmsl_tr2)$(if $1$2,$(false),$(true))
  159. # ----------------------------------------------------------------------------
  160. # Function: xnor
  161. # Arguments: Two boolean values
  162. # Returns: Returns value of 'not xor'
  163. # ----------------------------------------------------------------------------
  164. xnor =$(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(if $2,$(false),$(true)))
  165. # ###########################################################################
  166. # LIST MANIPULATION FUNCTIONS
  167. # ###########################################################################
  168. # ----------------------------------------------------------------------------
  169. # Function: first (same as LISP's car, or head)
  170. # Arguments: 1: A list
  171. # Returns: Returns the first element of a list
  172. # ----------------------------------------------------------------------------
  173. first = $(__gmsl_tr1)$(firstword $1)
  174. # ----------------------------------------------------------------------------
  175. # Function: last
  176. # Arguments: 1: A list
  177. # Returns: Returns the last element of a list
  178. # ----------------------------------------------------------------------------
  179. ifeq ($(__gmsl_have_lastword),$(true))
  180. last = $(__gmsl_tr1)$(lastword $1)
  181. else
  182. last = $(__gmsl_tr1)$(if $1,$(word $(words $1),$1))
  183. endif
  184. # ----------------------------------------------------------------------------
  185. # Function: rest (same as LISP's cdr, or tail)
  186. # Arguments: 1: A list
  187. # Returns: Returns the list with the first element removed
  188. # ----------------------------------------------------------------------------
  189. rest = $(__gmsl_tr1)$(wordlist 2,$(words $1),$1)
  190. # ----------------------------------------------------------------------------
  191. # Function: chop
  192. # Arguments: 1: A list
  193. # Returns: Returns the list with the last element removed
  194. # ----------------------------------------------------------------------------
  195. chop = $(__gmsl_tr1)$(wordlist 2,$(words $1),x $1)
  196. # ----------------------------------------------------------------------------
  197. # Function: map
  198. # Arguments: 1: Name of function to $(call) for each element of list
  199. # 2: List to iterate over calling the function in 1
  200. # Returns: The list after calling the function on each element
  201. # ----------------------------------------------------------------------------
  202. map = $(__gmsl_tr2)$(strip $(foreach a,$2,$(call $1,$a)))
  203. # ----------------------------------------------------------------------------
  204. # Function: pairmap
  205. # Arguments: 1: Name of function to $(call) for each pair of elements
  206. # 2: List to iterate over calling the function in 1
  207. # 3: Second list to iterate over calling the function in 1
  208. # Returns: The list after calling the function on each pair of elements
  209. # ----------------------------------------------------------------------------
  210. pairmap = $(strip $(__gmsl_tr3)\
  211. $(if $2$3,$(call $1,$(call first,$2),$(call first,$3)) \
  212. $(call pairmap,$1,$(call rest,$2),$(call rest,$3))))
  213. # ----------------------------------------------------------------------------
  214. # Function: leq
  215. # Arguments: 1: A list to compare against...
  216. # 2: ...this list
  217. # Returns: Returns $(true) if the two lists are identical
  218. # ----------------------------------------------------------------------------
  219. leq = $(__gmsl_tr2)$(strip $(if $(call seq,$(words $1),$(words $2)), \
  220. $(call __gmsl_list_equal,$1,$2),$(false)))
  221. __gmsl_list_equal = $(if $(strip $1), \
  222. $(if $(call seq,$(call first,$1),$(call first,$2)), \
  223. $(call __gmsl_list_equal, \
  224. $(call rest,$1), \
  225. $(call rest,$2)), \
  226. $(false)), \
  227. $(true))
  228. # ----------------------------------------------------------------------------
  229. # Function: lne
  230. # Arguments: 1: A list to compare against...
  231. # 2: ...this list
  232. # Returns: Returns $(true) if the two lists are different
  233. # ----------------------------------------------------------------------------
  234. lne = $(__gmsl_tr2)$(call not,$(call leq,$1,$2))
  235. # ----------------------------------------------------------------------------
  236. # Function: reverse
  237. # Arguments: 1: A list to reverse
  238. # Returns: The list with its elements in reverse order
  239. # ----------------------------------------------------------------------------
  240. reverse =$(__gmsl_tr1)$(strip $(if $1,$(call reverse,$(call rest,$1)) \
  241. $(call first,$1)))
  242. # ----------------------------------------------------------------------------
  243. # Function: uniq
  244. # Arguments: 1: A list from which to remove repeated elements
  245. # Returns: The list with duplicate elements removed without reordering
  246. # ----------------------------------------------------------------------------
  247. uniq = $(strip $(__gmsl_tr1) $(if $1,$(firstword $1) \
  248. $(call uniq,$(filter-out $(firstword $1),$1))))
  249. # ----------------------------------------------------------------------------
  250. # Function: length
  251. # Arguments: 1: A list
  252. # Returns: The number of elements in the list
  253. # ----------------------------------------------------------------------------
  254. length = $(__gmsl_tr1)$(words $1)
  255. # ###########################################################################
  256. # STRING MANIPULATION FUNCTIONS
  257. # ###########################################################################
  258. # Helper function that translates any GNU Make 'true' value (i.e. a
  259. # non-empty string) to our $(true)
  260. __gmsl_make_bool = $(if $(strip $1),$(true),$(false))
  261. # ----------------------------------------------------------------------------
  262. # Function: seq
  263. # Arguments: 1: A string to compare against...
  264. # 2: ...this string
  265. # Returns: Returns $(true) if the two strings are identical
  266. # ----------------------------------------------------------------------------
  267. seq = $(__gmsl_tr2)$(if $(subst x$1,,x$2)$(subst x$2,,x$1),$(false),$(true))
  268. # ----------------------------------------------------------------------------
  269. # Function: sne
  270. # Arguments: 1: A string to compare against...
  271. # 2: ...this string
  272. # Returns: Returns $(true) if the two strings are not the same
  273. # ----------------------------------------------------------------------------
  274. sne = $(__gmsl_tr2)$(call not,$(call seq,$1,$2))
  275. # ----------------------------------------------------------------------------
  276. # Function: split
  277. # Arguments: 1: The character to split on
  278. # 2: A string to split
  279. # Returns: Splits a string into a list separated by spaces at the split
  280. # character in the first argument
  281. # ----------------------------------------------------------------------------
  282. split = $(__gmsl_tr2)$(strip $(subst $1, ,$2))
  283. # ----------------------------------------------------------------------------
  284. # Function: merge
  285. # Arguments: 1: The character to put between fields
  286. # 2: A list to merge into a string
  287. # Returns: Merges a list into a single string, list elements are separated
  288. # by the character in the first argument
  289. # ----------------------------------------------------------------------------
  290. merge = $(__gmsl_tr2)$(strip $(if $2, \
  291. $(if $(call seq,1,$(words $2)), \
  292. $2,$(call first,$2)$1$(call merge,$1,$(call rest,$2)))))
  293. ifdef __gmsl_have_eval
  294. # ----------------------------------------------------------------------------
  295. # Function: tr
  296. # Arguments: 1: The list of characters to translate from
  297. # 2: The list of characters to translate to
  298. # 3: The text to translate
  299. # Returns: Returns the text after translating characters
  300. # ----------------------------------------------------------------------------
  301. tr = $(strip $(__gmsl_tr3)$(call assert_no_dollar,$0,$1$2$3) \
  302. $(eval __gmsl_t := $3) \
  303. $(foreach c, \
  304. $(join $(addsuffix :,$1),$2), \
  305. $(eval __gmsl_t := \
  306. $(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)), \
  307. $(__gmsl_t))))$(__gmsl_t))
  308. # Common character classes for use with the tr function. Each of
  309. # these is actually a variable declaration and must be wrapped with
  310. # $() or ${} to be used.
  311. [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 #
  312. [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 #
  313. [0-9] := 0 1 2 3 4 5 6 7 8 9 #
  314. [A-F] := A B C D E F #
  315. # ----------------------------------------------------------------------------
  316. # Function: uc
  317. # Arguments: 1: Text to upper case
  318. # Returns: Returns the text in upper case
  319. # ----------------------------------------------------------------------------
  320. uc = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(call tr,$([a-z]),$([A-Z]),$1)
  321. # ----------------------------------------------------------------------------
  322. # Function: lc
  323. # Arguments: 1: Text to lower case
  324. # Returns: Returns the text in lower case
  325. # ----------------------------------------------------------------------------
  326. lc = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(call tr,$([A-Z]),$([a-z]),$1)
  327. # ----------------------------------------------------------------------------
  328. # Function: strlen
  329. # Arguments: 1: A string
  330. # Returns: Returns the length of the string
  331. # ----------------------------------------------------------------------------
  332. # This results in __gmsl_tab containing a tab
  333. __gmsl_tab := #
  334. __gmsl_characters := 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
  335. __gmsl_characters += 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
  336. __gmsl_characters += 0 1 2 3 4 5 6 7 8 9
  337. __gmsl_characters += ` ~ ! @ \# $$ % ^ & * ( ) - _ = +
  338. __gmsl_characters += { } [ ] \ : ; ' " < > , . / ? |
  339. # This results in __gmsl_space containing just a space
  340. __gmsl_space :=
  341. __gmsl_space +=
  342. strlen = $(__gmsl_tr1)$(call assert_no_dollar,$0,$1)$(strip $(eval __temp := $(subst $(__gmsl_space),x,$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,x,$(__temp))))$(eval __temp := $(subst x,x ,$(__temp)))$(words $(__temp)))
  343. # This results in __gmsl_newline containing just a newline
  344. define __gmsl_newline
  345. endef
  346. # ----------------------------------------------------------------------------
  347. # Function: substr
  348. # Arguments: 1: A string
  349. # 2: Start position (first character is 1)
  350. # 3: End position (inclusive)
  351. # Returns: A substring.
  352. # Note: The string in $1 must not contain a §
  353. # ----------------------------------------------------------------------------
  354. substr = $(if $2,$(__gmsl_tr3)$(call assert_no_dollar,$0,$1$2$3)$(strip $(eval __temp := $$(subst $$(__gmsl_space),§ ,$$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,$$a$$(__gmsl_space),$(__temp))))$(eval __temp := $(wordlist $2,$3,$(__temp))))$(subst §,$(__gmsl_space),$(subst $(__gmsl_space),,$(__temp))))
  355. endif # __gmsl_have_eval
  356. # ###########################################################################
  357. # SET MANIPULATION FUNCTIONS
  358. # ###########################################################################
  359. # Sets are represented by sorted, deduplicated lists. To create a set
  360. # from a list use set_create, or start with the empty_set and
  361. # set_insert individual elements
  362. # This is the empty set
  363. empty_set :=
  364. # ----------------------------------------------------------------------------
  365. # Function: set_create
  366. # Arguments: 1: A list of set elements
  367. # Returns: Returns the newly created set
  368. # ----------------------------------------------------------------------------
  369. set_create = $(__gmsl_tr1)$(sort $1)
  370. # ----------------------------------------------------------------------------
  371. # Function: set_insert
  372. # Arguments: 1: A single element to add to a set
  373. # 2: A set
  374. # Returns: Returns the set with the element added
  375. # ----------------------------------------------------------------------------
  376. set_insert = $(__gmsl_tr2)$(sort $1 $2)
  377. # ----------------------------------------------------------------------------
  378. # Function: set_remove
  379. # Arguments: 1: A single element to remove from a set
  380. # 2: A set
  381. # Returns: Returns the set with the element removed
  382. # ----------------------------------------------------------------------------
  383. set_remove = $(__gmsl_tr2)$(filter-out $1,$2)
  384. # ----------------------------------------------------------------------------
  385. # Function: set_is_member, set_is_not_member
  386. # Arguments: 1: A single element
  387. # 2: A set
  388. # Returns: (set_is_member) Returns $(true) if the element is in the set
  389. # (set_is_not_member) Returns $(false) if the element is in the set
  390. # ----------------------------------------------------------------------------
  391. set_is_member = $(__gmsl_tr2)$(if $(filter $1,$2),$(true),$(false))
  392. set_is_not_member = $(__gmsl_tr2)$(if $(filter $1,$2),$(false),$(true))
  393. # ----------------------------------------------------------------------------
  394. # Function: set_union
  395. # Arguments: 1: A set
  396. # 2: Another set
  397. # Returns: Returns the union of the two sets
  398. # ----------------------------------------------------------------------------
  399. set_union = $(__gmsl_tr2)$(sort $1 $2)
  400. # ----------------------------------------------------------------------------
  401. # Function: set_intersection
  402. # Arguments: 1: A set
  403. # 2: Another set
  404. # Returns: Returns the intersection of the two sets
  405. # ----------------------------------------------------------------------------
  406. set_intersection = $(__gmsl_tr2)$(filter $1,$2)
  407. # ----------------------------------------------------------------------------
  408. # Function: set_is_subset
  409. # Arguments: 1: A set
  410. # 2: Another set
  411. # Returns: Returns $(true) if the first set is a subset of the second
  412. # ----------------------------------------------------------------------------
  413. set_is_subset = $(__gmsl_tr2)$(call set_equal,$(call set_intersection,$1,$2),$1)
  414. # ----------------------------------------------------------------------------
  415. # Function: set_equal
  416. # Arguments: 1: A set
  417. # 2: Another set
  418. # Returns: Returns $(true) if the two sets are identical
  419. # ----------------------------------------------------------------------------
  420. set_equal = $(__gmsl_tr2)$(call seq,$1,$2)
  421. # ###########################################################################
  422. # ARITHMETIC LIBRARY
  423. # ###########################################################################
  424. # Integers a represented by lists with the equivalent number of x's.
  425. # For example the number 4 is x x x x.
  426. # ----------------------------------------------------------------------------
  427. # Function: int_decode
  428. # Arguments: 1: A number of x's representation
  429. # Returns: Returns the integer for human consumption that is represented
  430. # by the string of x's
  431. # ----------------------------------------------------------------------------
  432. int_decode = $(__gmsl_tr1)$(if $1,$(if $(call seq,$(word 1,$1),x),$(words $1),$1),0)
  433. # ----------------------------------------------------------------------------
  434. # Function: int_encode
  435. # Arguments: 1: A number in human-readable integer form
  436. # Returns: Returns the integer encoded as a string of x's
  437. # ----------------------------------------------------------------------------
  438. __int_encode = $(if $1,$(if $(call seq,$(words $(wordlist 1,$1,$2)),$1),$(wordlist 1,$1,$2),$(call __int_encode,$1,$(if $2,$2 $2,x))))
  439. __strip_leading_zero = $(if $1,$(if $(call seq,$(patsubst 0%,%,$1),$1),$1,$(call __strip_leading_zero,$(patsubst 0%,%,$1))),0)
  440. int_encode = $(__gmsl_tr1)$(call __int_encode,$(call __strip_leading_zero,$1))
  441. # The arithmetic library functions come in two forms: one form of each
  442. # function takes integers as arguments and the other form takes the
  443. # encoded form (x's created by a call to int_encode). For example,
  444. # there are two plus functions:
  445. #
  446. # plus Called with integer arguments and returns an integer
  447. # int_plus Called with encoded arguments and returns an encoded result
  448. #
  449. # plus will be slower than int_plus because its arguments and result
  450. # have to be translated between the x's format and integers. If doing
  451. # a complex calculation use the int_* forms with a single encoding of
  452. # inputs and single decoding of the output. For simple calculations
  453. # the direct forms can be used.
  454. # Helper function used to wrap an int_* function into a function that
  455. # takes a pair of integers, perhaps a function and returns an integer
  456. # result
  457. __gmsl_int_wrap = $(call int_decode,$(call $1,$(call int_encode,$2),$(call int_encode,$3)))
  458. __gmsl_int_wrap1 = $(call int_decode,$(call $1,$(call int_encode,$2)))
  459. __gmsl_int_wrap2 = $(call $1,$(call int_encode,$2),$(call int_encode,$3))
  460. # ----------------------------------------------------------------------------
  461. # Function: int_plus
  462. # Arguments: 1: A number in x's representation
  463. # 2: Another number in x's represntation
  464. # Returns: Returns the sum of the two numbers in x's representation
  465. # ----------------------------------------------------------------------------
  466. int_plus = $(strip $(__gmsl_tr2)$1 $2)
  467. # ----------------------------------------------------------------------------
  468. # Function: plus (wrapped version of int_plus)
  469. # Arguments: 1: An integer
  470. # 2: Another integer
  471. # Returns: Returns the sum of the two integers
  472. # ----------------------------------------------------------------------------
  473. plus = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_plus,$1,$2)
  474. # ----------------------------------------------------------------------------
  475. # Function: int_subtract
  476. # Arguments: 1: A number in x's representation
  477. # 2: Another number in x's represntation
  478. # Returns: Returns the difference of the two numbers in x's representation,
  479. # or outputs an error on a numeric underflow
  480. # ----------------------------------------------------------------------------
  481. int_subtract = $(strip $(__gmsl_tr2)$(if $(call int_gte,$1,$2), \
  482. $(filter-out xx,$(join $1,$2)), \
  483. $(call __gmsl_warning,Subtraction underflow)))
  484. # ----------------------------------------------------------------------------
  485. # Function: subtract (wrapped version of int_subtract)
  486. # Arguments: 1: An integer
  487. # 2: Another integer
  488. # Returns: Returns the difference of the two integers,
  489. # or outputs an error on a numeric underflow
  490. # ----------------------------------------------------------------------------
  491. subtract = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_subtract,$1,$2)
  492. # ----------------------------------------------------------------------------
  493. # Function: int_multiply
  494. # Arguments: 1: A number in x's representation
  495. # 2: Another number in x's represntation
  496. # Returns: Returns the product of the two numbers in x's representation
  497. # ----------------------------------------------------------------------------
  498. int_multiply = $(strip $(__gmsl_tr2)$(foreach a,$1,$2))
  499. # ----------------------------------------------------------------------------
  500. # Function: multiply (wrapped version of int_multiply)
  501. # Arguments: 1: An integer
  502. # 2: Another integer
  503. # Returns: Returns the product of the two integers
  504. # ----------------------------------------------------------------------------
  505. multiply = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_multiply,$1,$2)
  506. # ----------------------------------------------------------------------------
  507. # Function: int_divide
  508. # Arguments: 1: A number in x's representation
  509. # 2: Another number in x's represntation
  510. # Returns: Returns the result of integer division of argument 1 divided
  511. # by argument 2 in x's representation
  512. # ----------------------------------------------------------------------------
  513. int_divide = $(__gmsl_tr2)$(strip $(if $1,$(if $2, \
  514. $(subst M,x,$(filter-out x,$(subst $2,M,$1))), \
  515. $(call __gmsl_error,Division by zero))))
  516. # ----------------------------------------------------------------------------
  517. # Function: divide (wrapped version of int_divide)
  518. # Arguments: 1: An integer
  519. # 2: Another integer
  520. # Returns: Returns the integer division of the first argument by the second
  521. # ----------------------------------------------------------------------------
  522. divide = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_divide,$1,$2)
  523. # ----------------------------------------------------------------------------
  524. # Function: int_modulo
  525. # Arguments: 1: A number in x's representation
  526. # 2: Another number in x's represntation
  527. # Returns: Returns the remainder of integer division of argument 1 divided
  528. # by argument 2 in x's representation
  529. # ----------------------------------------------------------------------------
  530. int_modulo = $(__gmsl_tr2)$(strip $(if $1,$(if $2, \
  531. $(filter-out M,$(subst $2,M,$1)), \
  532. $(call __gmsl_error,Division by zero))))
  533. # ----------------------------------------------------------------------------
  534. # Function: modulo (wrapped version of int_modulo)
  535. # Arguments: 1: An integer
  536. # 2: Another integer
  537. # Returns: Returns the remainder of integer division of the first argument
  538. # by the second
  539. # ----------------------------------------------------------------------------
  540. modulo = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_modulo,$1,$2)
  541. # ----------------------------------------------------------------------------
  542. # Function: int_max, int_min
  543. # Arguments: 1: A number in x's representation
  544. # 2: Another number in x's represntation
  545. # Returns: Returns the maximum or minimum of its arguments in x's
  546. # representation
  547. # ----------------------------------------------------------------------------
  548. int_max = $(__gmsl_tr2)$(subst xx,x,$(join $1,$2))
  549. int_min = $(__gmsl_tr2)$(subst xx,x,$(filter xx,$(join $1,$2)))
  550. # ----------------------------------------------------------------------------
  551. # Function: max, min
  552. # Arguments: 1: An integer
  553. # 2: Another integer
  554. # Returns: Returns the maximum or minimum of its integer arguments
  555. # ----------------------------------------------------------------------------
  556. max = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_max,$1,$2)
  557. min = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_min,$1,$2)
  558. # ----------------------------------------------------------------------------
  559. # Function: int_gt, int_gte, int_lt, int_lte, int_eq, int_ne
  560. # Arguments: Two x's representation numbers to be compared
  561. # Returns: $(true) or $(false)
  562. #
  563. # int_gt First argument greater than second argument
  564. # int_gte First argument greater than or equal to second argument
  565. # int_lt First argument less than second argument
  566. # int_lte First argument less than or equal to second argument
  567. # int_eq First argument is numerically equal to the second argument
  568. # int_ne First argument is not numerically equal to the second argument
  569. # ----------------------------------------------------------------------------
  570. int_gt = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  571. $(filter-out $(words $2), \
  572. $(words $(call int_max,$1,$2))))
  573. int_gte = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  574. $(call int_gt,$1,$2)$(call int_eq,$1,$2))
  575. int_lt = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  576. $(filter-out $(words $1), \
  577. $(words $(call int_max,$1,$2))))
  578. int_lte = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  579. $(call int_lt,$1,$2)$(call int_eq,$1,$2))
  580. int_eq = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  581. $(filter $(words $1),$(words $2)))
  582. int_ne = $(__gmsl_tr2)$(call __gmsl_make_bool, \
  583. $(filter-out $(words $1),$(words $2)))
  584. # ----------------------------------------------------------------------------
  585. # Function: gt, gte, lt, lte, eq, ne
  586. # Arguments: Two integers to be compared
  587. # Returns: $(true) or $(false)
  588. #
  589. # gt First argument greater than second argument
  590. # gte First argument greater than or equal to second argument
  591. # lt First argument less than second argument
  592. # lte First argument less than or equal to second argument
  593. # eq First argument is numerically equal to the second argument
  594. # ne First argument is not numerically equal to the second argument
  595. # ----------------------------------------------------------------------------
  596. gt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gt,$1,$2)
  597. gte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gte,$1,$2)
  598. lt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lt,$1,$2)
  599. lte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lte,$1,$2)
  600. eq = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_eq,$1,$2)
  601. ne = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_ne,$1,$2)
  602. # increment adds 1 to its argument, decrement subtracts 1. Note that
  603. # decrement does not range check and hence will not underflow, but
  604. # will incorrectly say that 0 - 1 = 0
  605. # ----------------------------------------------------------------------------
  606. # Function: int_inc
  607. # Arguments: 1: A number in x's representation
  608. # Returns: The number incremented by 1 in x's representation
  609. # ----------------------------------------------------------------------------
  610. int_inc = $(strip $(__gmsl_tr1)$1 x)
  611. # ----------------------------------------------------------------------------
  612. # Function: inc
  613. # Arguments: 1: An integer
  614. # Returns: The argument incremented by 1
  615. # ----------------------------------------------------------------------------
  616. inc = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_inc,$1)
  617. # ----------------------------------------------------------------------------
  618. # Function: int_dec
  619. # Arguments: 1: A number in x's representation
  620. # Returns: The number decremented by 1 in x's representation
  621. # ----------------------------------------------------------------------------
  622. int_dec = $(__gmsl_tr1)$(strip \
  623. $(if $(call sne,0,$(words $1)), \
  624. $(wordlist 2,$(words $1),$1)))
  625. # ----------------------------------------------------------------------------
  626. # Function: dec
  627. # Arguments: 1: An integer
  628. # Returns: The argument decremented by 1
  629. # ----------------------------------------------------------------------------
  630. dec = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_dec,$1)
  631. # double doubles its argument, and halve halves it
  632. # ----------------------------------------------------------------------------
  633. # Function: int_double
  634. # Arguments: 1: A number in x's representation
  635. # Returns: The number doubled (i.e. * 2) and returned in x's representation
  636. # ----------------------------------------------------------------------------
  637. int_double = $(strip $(__gmsl_tr1)$1 $1)
  638. # ----------------------------------------------------------------------------
  639. # Function: double
  640. # Arguments: 1: An integer
  641. # Returns: The integer times 2
  642. # ----------------------------------------------------------------------------
  643. double = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_double,$1)
  644. # ----------------------------------------------------------------------------
  645. # Function: int_halve
  646. # Arguments: 1: A number in x's representation
  647. # Returns: The number halved (i.e. / 2) and returned in x's representation
  648. # ----------------------------------------------------------------------------
  649. int_halve = $(__gmsl_tr1)$(strip $(subst xx,x,$(filter-out xy x y, \
  650. $(join $1,$(foreach a,$1,y x)))))
  651. # ----------------------------------------------------------------------------
  652. # Function: halve
  653. # Arguments: 1: An integer
  654. # Returns: The integer divided by 2
  655. # ----------------------------------------------------------------------------
  656. halve = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_halve,$1)
  657. # ----------------------------------------------------------------------------
  658. # Function: sequence
  659. # Arguments: 1: An integer
  660. # 2: An integer
  661. # Returns: The sequence [arg1, arg2] of integers if arg1 < arg2 or
  662. # [arg2, arg1] if arg2 > arg1. If arg1 == arg1 return [arg1]
  663. # ----------------------------------------------------------------------------
  664. sequence = $(__gmsl_tr2)$(strip $(if $(call lte,$1,$2), \
  665. $(call __gmsl_sequence_up,$1,$2), \
  666. $(call __gmsl_sequence_dn,$2,$1)))
  667. __gmsl_sequence_up = $(if $(call seq,$1,$2),$1,$1 $(call __gmsl_sequence_up,$(call inc,$1),$2))
  668. __gmsl_sequence_dn = $(if $(call seq,$1,$2),$1,$2 $(call __gmsl_sequence_dn,$1,$(call dec,$2)))
  669. # ----------------------------------------------------------------------------
  670. # Function: dec2hex, dec2bin, dec2oct
  671. # Arguments: 1: An integer
  672. # Returns: The decimal argument converted to hexadecimal, binary or
  673. # octal
  674. # ----------------------------------------------------------------------------
  675. __gmsl_digit = $(subst 15,f,$(subst 14,e,$(subst 13,d,$(subst 12,c,$(subst 11,b,$(subst 10,a,$1))))))
  676. dec2hex = $(call __gmsl_dec2base,$(call int_encode,$1),$(call int_encode,16))
  677. dec2bin = $(call __gmsl_dec2base,$(call int_encode,$1),$(call int_encode,2))
  678. dec2oct = $(call __gmsl_dec2base,$(call int_encode,$1),$(call int_encode,8))
  679. __gmsl_base_divide = $(subst $2,X ,$1)
  680. __gmsl_q = $(strip $(filter X,$1))
  681. __gmsl_r = $(words $(filter x,$1))
  682. __gmsl_dec2base = $(eval __gmsl_temp := $(call __gmsl_base_divide,$1,$2))$(call __gmsl_dec2base_,$(call __gmsl_q,$(__gmsl_temp)),$(call __gmsl_r,$(__gmsl_temp)),$2)
  683. __gmsl_dec2base_ = $(if $1,$(call __gmsl_dec2base,$(subst X,x,$1),$3))$(call __gmsl_digit,$2)
  684. ifdef __gmsl_have_eval
  685. # ###########################################################################
  686. # ASSOCIATIVE ARRAYS
  687. # ###########################################################################
  688. # Magic string that is very unlikely to appear in a key or value
  689. __gmsl_aa_magic := faf192c8efbc25c27992c5bc5add390393d583c6
  690. # ----------------------------------------------------------------------------
  691. # Function: set
  692. # Arguments: 1: Name of associative array
  693. # 2: The key value to associate
  694. # 3: The value associated with the key
  695. # Returns: Nothing
  696. # ----------------------------------------------------------------------------
  697. set = $(__gmsl_tr3)$(call assert_no_space,$0,$1$2)$(call assert_no_dollar,$0,$1$2$3)$(eval __gmsl_aa_$1_$(__gmsl_aa_magic)_$2_gmsl_aa_$1 := $3)
  698. # Only used internally by memoize function
  699. __gmsl_set = $(call set,$1,$2,$3)$3
  700. # ----------------------------------------------------------------------------
  701. # Function: get
  702. # Arguments: 1: Name of associative array
  703. # 2: The key to retrieve
  704. # Returns: The value stored in the array for that key
  705. # ----------------------------------------------------------------------------
  706. get = $(strip $(__gmsl_tr2)$(call assert_no_space,$0,$1$2)$(call assert_no_dollar,$0,$1$2)$(__gmsl_aa_$1_$(__gmsl_aa_magic)_$2_gmsl_aa_$1))
  707. # ----------------------------------------------------------------------------
  708. # Function: keys
  709. # Arguments: 1: Name of associative array
  710. # Returns: Returns a list of all defined keys in the array
  711. # ----------------------------------------------------------------------------
  712. keys = $(__gmsl_tr1)$(call assert_no_space,$0,$1)$(call assert_no_dollar,$0,$1)$(sort $(patsubst __gmsl_aa_$1_$(__gmsl_aa_magic)_%_gmsl_aa_$1,%, \
  713. $(filter __gmsl_aa_$1_$(__gmsl_aa_magic)_%_gmsl_aa_$1,$(.VARIABLES))))
  714. # ----------------------------------------------------------------------------
  715. # Function: defined
  716. # Arguments: 1: Name of associative array
  717. # 2: The key to test
  718. # Returns: Returns true if the key is defined (i.e. not empty)
  719. # ----------------------------------------------------------------------------
  720. defined = $(__gmsl_tr2)$(call assert_no_space,$0,$1$2)$(call assert_no_dollar,$0,$1$2)$(call sne,$(call get,$1,$2),)
  721. endif # __gmsl_have_eval
  722. ifdef __gmsl_have_eval
  723. # ###########################################################################
  724. # NAMED STACKS
  725. # ###########################################################################
  726. # ----------------------------------------------------------------------------
  727. # Function: push
  728. # Arguments: 1: Name of stack
  729. # 2: Value to push onto the top of the stack (must not contain
  730. # a space)
  731. # Returns: None
  732. # ----------------------------------------------------------------------------
  733. push = $(__gmsl_tr2)$(call assert_no_space,$0,$1$2)$(call assert_no_dollar,$0,$1$2)$(eval __gmsl_stack_$1 := $2 $(if $(filter-out undefined,\
  734. $(origin __gmsl_stack_$1)),$(__gmsl_stack_$1)))
  735. # ----------------------------------------------------------------------------
  736. # Function: pop
  737. # Arguments: 1: Name of stack
  738. # Returns: Top element from the stack after removing it
  739. # ----------------------------------------------------------------------------
  740. pop = $(__gmsl_tr1)$(call assert_no_space,$0,$1)$(call assert_no_dollar,$0,$1)$(strip $(if $(filter-out undefined,$(origin __gmsl_stack_$1)), \
  741. $(call first,$(__gmsl_stack_$1)) \
  742. $(eval __gmsl_stack_$1 := $(call rest,$(__gmsl_stack_$1)))))
  743. # ----------------------------------------------------------------------------
  744. # Function: peek
  745. # Arguments: 1: Name of stack
  746. # Returns: Top element from the stack without removing it
  747. # ----------------------------------------------------------------------------
  748. peek = $(__gmsl_tr1)$(call assert_no_space,$0,$1)$(call assert_no_dollar,$0,$1)$(call first,$(__gmsl_stack_$1))
  749. # ----------------------------------------------------------------------------
  750. # Function: depth
  751. # Arguments: 1: Name of stack
  752. # Returns: Number of items on the stack
  753. # ----------------------------------------------------------------------------
  754. depth = $(__gmsl_tr1)$(call assert_no_space,$0,$1)$(call assert_no_dollar,$0,$1)$(words $(__gmsl_stack_$1))
  755. endif # __gmsl_have_eval
  756. ifdef __gmsl_have_eval
  757. # ###########################################################################
  758. # STRING CACHE
  759. # ###########################################################################
  760. # ----------------------------------------------------------------------------
  761. # Function: memoize
  762. # Arguments: 1. Name of the function to be called if the string
  763. # has not been previously seen
  764. # 2. A string
  765. # Returns: Returns the result of a memo function (which the user must
  766. # define) on the passed in string and remembers the result.
  767. #
  768. # Example: Set memo = $(shell echo "$1" | md5sum) to make a cache
  769. # of MD5 hashes of strings. $(call memoize,memo,foo bar baz)
  770. # ----------------------------------------------------------------------------
  771. __gmsl_memoize = $(subst $(__gmsl_space),§,$1)cc2af1bb7c4482f2ba75e338b963d3e7$(subst $(__gmsl_space),§,$2)
  772. memoize = $(__gmsl_tr2)$(strip $(if $(call defined,__gmsl_m,$(__gmsl_memoize)),\
  773. $(call get,__gmsl_m,$(__gmsl_memoize)), \
  774. $(call __gmsl_set,__gmsl_m,$(__gmsl_memoize),$(call $1,$2))))
  775. endif # __gmsl_have_eval
  776. # ###########################################################################
  777. # DEBUGGING FACILITIES
  778. # ###########################################################################
  779. # ----------------------------------------------------------------------------
  780. # Target: gmsl-print-%
  781. # Arguments: The % should be replaced by the name of a variable that you
  782. # wish to print out.
  783. # Action: Echos the name of the variable that matches the % and its value.
  784. # For example, 'make gmsl-print-SHELL' will output the value of
  785. # the SHELL variable
  786. # ----------------------------------------------------------------------------
  787. gmsl-print-%: ; @echo $* = $($*)
  788. # ----------------------------------------------------------------------------
  789. # Function: assert
  790. # Arguments: 1: A boolean that must be true or the assertion will fail
  791. # 2: The message to print with the assertion
  792. # Returns: None
  793. # ----------------------------------------------------------------------------
  794. assert = $(if $2,$(if $1,,$(call __gmsl_error,Assertion failure: $2)))
  795. # ----------------------------------------------------------------------------
  796. # Function: assert_exists
  797. # Arguments: 1: Name of file that must exist, if it is missing an assertion
  798. # will be generated
  799. # Returns: None
  800. # ----------------------------------------------------------------------------
  801. assert_exists = $(if $0,$(call assert,$(wildcard $1),file '$1' missing))
  802. # ----------------------------------------------------------------------------
  803. # Function: assert_no_dollar
  804. # Arguments: 1: Name of a function being executd
  805. # 2: Arguments to check
  806. # Returns: None
  807. # ----------------------------------------------------------------------------
  808. assert_no_dollar = $(call __gmsl_tr2)$(call assert,$(call not,$(findstring $(__gmsl_dollar),$2)),$1 called with a dollar sign in argument)
  809. # ----------------------------------------------------------------------------
  810. # Function: assert_no_space
  811. # Arguments: 1: Name of a function being executd
  812. # 2: Arguments to check
  813. # Returns: None
  814. # ----------------------------------------------------------------------------
  815. ifeq ($(__gmsl_spaced_vars),$(false))
  816. assert_no_space = $(call assert,$(call not,$(findstring $(__gmsl_aa_magic),$(subst $(__gmsl_space),$(__gmsl_aa_magic),$2))),$1 called with a space in argument)
  817. else
  818. assert_no_space =
  819. endif