| 1 | # (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and |
|---|
| 2 | # distribute this software is granted provided this copyright notice appears in |
|---|
| 3 | # all copies. This software is provided "as is" without express or implied |
|---|
| 4 | # warranty, and with no claim as to its suitability for any purpose. |
|---|
| 5 | |
|---|
| 6 | import "class" : is-instance ; |
|---|
| 7 | import errors ; |
|---|
| 8 | |
|---|
| 9 | rule ungrist ( names * ) |
|---|
| 10 | { |
|---|
| 11 | local result ; |
|---|
| 12 | for local name in $(names) |
|---|
| 13 | { |
|---|
| 14 | local stripped = [ MATCH ^<(.*)>$ : $(name) ] ; |
|---|
| 15 | if ! $(stripped) |
|---|
| 16 | { |
|---|
| 17 | errors.error "in ungrist" $(names) : $(name) is not of the form <.*> ; |
|---|
| 18 | } |
|---|
| 19 | result += $(stripped) ; |
|---|
| 20 | } |
|---|
| 21 | return $(result) ; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | # Return the file of the caller of the rule that called caller-file. |
|---|
| 25 | rule caller-file ( ) |
|---|
| 26 | { |
|---|
| 27 | local bt = [ BACKTRACE ] ; |
|---|
| 28 | return $(bt[9]) ; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | # Returns the textual representation of argument. If it is a class |
|---|
| 32 | # instance, class its 'str' method. Otherwise, returns the argument. |
|---|
| 33 | rule str ( value ) |
|---|
| 34 | { |
|---|
| 35 | if [ is-instance $(value) ] |
|---|
| 36 | { |
|---|
| 37 | return [ $(value).str ] ; |
|---|
| 38 | } |
|---|
| 39 | else |
|---|
| 40 | { |
|---|
| 41 | return $(value) ; |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # Tests if 'a' is equal to 'b'. If 'a' is a class instance, |
|---|
| 46 | # calls its 'equal' method. Uses ordinary jam's comparison otherwise. |
|---|
| 47 | rule equal ( a b ) |
|---|
| 48 | { |
|---|
| 49 | if [ is-instance $(a) ] |
|---|
| 50 | { |
|---|
| 51 | return [ $(a).equal $(b) ] ; |
|---|
| 52 | } |
|---|
| 53 | else |
|---|
| 54 | { |
|---|
| 55 | if $(a) = $(b) |
|---|
| 56 | { |
|---|
| 57 | return true ; |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | # Tests if 'a' is less than 'b'. If 'a' is a class instance, |
|---|
| 63 | # calls its 'less' method. Uses ordinary jam's comparison otherwise. |
|---|
| 64 | rule less ( a b ) |
|---|
| 65 | { |
|---|
| 66 | if [ is-instance $(a) ] |
|---|
| 67 | { |
|---|
| 68 | return [ $(a).less $(b) ] ; |
|---|
| 69 | } |
|---|
| 70 | else |
|---|
| 71 | { |
|---|
| 72 | if $(a) < $(b) |
|---|
| 73 | { |
|---|
| 74 | return true ; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | # For all elements of 'list' which do not already have 'suffix', |
|---|
| 80 | # add 'suffix'. |
|---|
| 81 | rule apply-default-suffix ( suffix : list * ) |
|---|
| 82 | { |
|---|
| 83 | local result ; |
|---|
| 84 | for local i in $(list) |
|---|
| 85 | { |
|---|
| 86 | if $(i:S) = $(suffix) |
|---|
| 87 | { |
|---|
| 88 | result += $(i) ; |
|---|
| 89 | } |
|---|
| 90 | else |
|---|
| 91 | { |
|---|
| 92 | result += $(i)$(suffix) ; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | return $(result) ; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | # If 'name' contains a dot, returns the part before the last dot. |
|---|
| 99 | # If 'name' contains no dot, returns it unmodified. |
|---|
| 100 | rule basename ( name ) |
|---|
| 101 | { |
|---|
| 102 | if $(name:S) |
|---|
| 103 | { |
|---|
| 104 | name = $(name:B) ; |
|---|
| 105 | } |
|---|
| 106 | return $(name) ; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | local rule __test__ ( ) |
|---|
| 112 | { |
|---|
| 113 | import assert ; |
|---|
| 114 | import "class" : new ; |
|---|
| 115 | assert.result foo bar : ungrist <foo> <bar> ; |
|---|
| 116 | |
|---|
| 117 | assert.result 123 : str 123 ; |
|---|
| 118 | |
|---|
| 119 | class test-class__ |
|---|
| 120 | { |
|---|
| 121 | rule __init__ ( ) |
|---|
| 122 | { |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | rule str ( ) |
|---|
| 126 | { |
|---|
| 127 | return "str-test-class" ; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | rule less ( a ) |
|---|
| 131 | { |
|---|
| 132 | return "yes, of course!" ; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | rule equal ( a ) |
|---|
| 136 | { |
|---|
| 137 | return "not sure" ; |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | assert.result "str-test-class" : str [ new test-class__ ] ; |
|---|
| 142 | assert.true less 1 2 ; |
|---|
| 143 | assert.false less 2 1 ; |
|---|
| 144 | assert.result "yes, of course!" : less [ new test-class__ ] 1 ; |
|---|
| 145 | assert.true equal 1 1 ; |
|---|
| 146 | assert.false equal 1 2 ; |
|---|
| 147 | assert.result "not sure" : equal [ new test-class__ ] 1 ; |
|---|
| 148 | |
|---|
| 149 | assert.result foo.lib foo.lib : apply-default-suffix .lib : foo.lib foo.lib ; |
|---|
| 150 | |
|---|
| 151 | assert.result foo : basename foo ; |
|---|
| 152 | assert.result foo : basename foo.so ; |
|---|
| 153 | assert.result foo.so : basename foo.so.1 ; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|