Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 4 | "bytes" |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 5 | "strings" |
| 6 | "testing" |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 7 | "unicode" |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 8 | |
| 9 | bpparser "github.com/google/blueprint/parser" |
| 10 | ) |
| 11 | |
| 12 | var valueTestCases = []struct { |
| 13 | blueprint string |
| 14 | expected string |
| 15 | }{ |
| 16 | { |
| 17 | blueprint: `test = false`, |
| 18 | expected: `false`, |
| 19 | }, |
| 20 | { |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 21 | blueprint: `test = "string"`, |
| 22 | expected: `string`, |
| 23 | }, |
| 24 | { |
| 25 | blueprint: `test = ["a", "b"]`, |
| 26 | expected: `\ |
Colin Cross | b3245e9 | 2015-06-30 16:27:57 -0700 | [diff] [blame] | 27 | a \ |
| 28 | b |
| 29 | `, |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 30 | }, |
| 31 | } |
| 32 | |
| 33 | func TestValueToString(t *testing.T) { |
| 34 | for _, testCase := range valueTestCases { |
| 35 | blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil) |
| 36 | if len(errs) > 0 { |
| 37 | t.Errorf("Failed to read blueprint: %q", errs) |
| 38 | } |
| 39 | |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 40 | str, err := valueToString(blueprint.Defs[0].(*bpparser.Assignment).Value) |
| 41 | if err != nil { |
| 42 | t.Error(err.Error()) |
| 43 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 44 | expect(t, testCase.blueprint, testCase.expected, str) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | var moduleTestCases = []struct { |
| 49 | blueprint string |
| 50 | androidmk string |
| 51 | }{ |
| 52 | // Target-only |
| 53 | { |
| 54 | blueprint: `cc_library_shared { name: "test", }`, |
| 55 | androidmk: `include $(CLEAR_VARS) |
| 56 | LOCAL_MODULE := test |
| 57 | include $(BUILD_SHARED_LIBRARY)`, |
| 58 | }, |
| 59 | // Host-only |
| 60 | { |
| 61 | blueprint: `cc_library_host_shared { name: "test", }`, |
| 62 | androidmk: `include $(CLEAR_VARS) |
| 63 | LOCAL_MODULE := test |
| 64 | include $(BUILD_HOST_SHARED_LIBRARY)`, |
| 65 | }, |
| 66 | // Target and Host |
| 67 | { |
| 68 | blueprint: `cc_library_shared { name: "test", host_supported: true, }`, |
| 69 | androidmk: `include $(CLEAR_VARS) |
| 70 | LOCAL_MODULE := test |
| 71 | include $(BUILD_SHARED_LIBRARY) |
| 72 | |
| 73 | include $(CLEAR_VARS) |
| 74 | LOCAL_MODULE := test |
| 75 | include $(BUILD_HOST_SHARED_LIBRARY)`, |
| 76 | }, |
Dan Willemsen | 49f5045 | 2015-06-24 14:56:00 -0700 | [diff] [blame] | 77 | // Static and Shared |
| 78 | { |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 79 | blueprint: `cc_library { name: "test", srcs: ["a"], }`, |
Dan Willemsen | 49f5045 | 2015-06-24 14:56:00 -0700 | [diff] [blame] | 80 | androidmk: `include $(CLEAR_VARS) |
| 81 | LOCAL_MODULE := test |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 82 | LOCAL_WHOLE_STATIC_LIBRARIES := \ |
| 83 | test |
Dan Willemsen | 49f5045 | 2015-06-24 14:56:00 -0700 | [diff] [blame] | 84 | include $(BUILD_SHARED_LIBRARY) |
| 85 | |
| 86 | include $(CLEAR_VARS) |
| 87 | LOCAL_MODULE := test |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 88 | LOCAL_SRC_FILES := \ |
| 89 | a |
Dan Willemsen | 49f5045 | 2015-06-24 14:56:00 -0700 | [diff] [blame] | 90 | include $(BUILD_STATIC_LIBRARY)`, |
| 91 | }, |
Dan Willemsen | 3a4045d | 2015-06-24 15:37:17 -0700 | [diff] [blame] | 92 | // Static and Shared / Target and Host |
| 93 | { |
| 94 | blueprint: `cc_library { name: "test", host_supported: true, }`, |
| 95 | androidmk: `include $(CLEAR_VARS) |
| 96 | LOCAL_MODULE := test |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 97 | LOCAL_WHOLE_STATIC_LIBRARIES := \ |
| 98 | test |
Dan Willemsen | 3a4045d | 2015-06-24 15:37:17 -0700 | [diff] [blame] | 99 | include $(BUILD_SHARED_LIBRARY) |
| 100 | |
| 101 | include $(CLEAR_VARS) |
| 102 | LOCAL_MODULE := test |
| 103 | include $(BUILD_STATIC_LIBRARY) |
| 104 | |
| 105 | include $(CLEAR_VARS) |
| 106 | LOCAL_MODULE := test |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 107 | LOCAL_WHOLE_STATIC_LIBRARIES := \ |
| 108 | test |
Dan Willemsen | 3a4045d | 2015-06-24 15:37:17 -0700 | [diff] [blame] | 109 | include $(BUILD_HOST_SHARED_LIBRARY) |
| 110 | |
| 111 | include $(CLEAR_VARS) |
| 112 | LOCAL_MODULE := test |
| 113 | include $(BUILD_HOST_STATIC_LIBRARY)`, |
| 114 | }, |
Dan Willemsen | 2701212 | 2015-06-26 17:40:54 -0700 | [diff] [blame] | 115 | // Static and Shared properties |
| 116 | { |
| 117 | blueprint: `cc_library { |
| 118 | name: "test", |
| 119 | srcs: ["a"], |
| 120 | static: { srcs: ["c"], static_libs: ["l"], }, |
| 121 | shared: { srcs: ["b"], }, |
| 122 | multilib: { lib32: { shared: { cflags: ["f"], }, }, }, |
| 123 | }`, |
| 124 | androidmk: `include $(CLEAR_VARS) |
| 125 | LOCAL_MODULE := test |
| 126 | LOCAL_SRC_FILES := \ |
| 127 | a \ |
| 128 | b |
| 129 | LOCAL_CFLAGS_32 := \ |
| 130 | f |
| 131 | include $(BUILD_SHARED_LIBRARY) |
| 132 | |
| 133 | include $(CLEAR_VARS) |
| 134 | LOCAL_MODULE := test |
| 135 | LOCAL_SRC_FILES := \ |
| 136 | a \ |
| 137 | c |
| 138 | LOCAL_STATIC_LIBRARIES := \ |
| 139 | l |
| 140 | include $(BUILD_STATIC_LIBRARY)`, |
| 141 | }, |
Dan Willemsen | 2617316 | 2015-07-06 13:36:50 -0700 | [diff] [blame^] | 142 | // Static and Shared properties, use whole static lib, but add extra shared srcs |
| 143 | { |
| 144 | blueprint: `cc_library { |
| 145 | name: "test", |
| 146 | srcs: ["a"], |
| 147 | shared: { srcs: ["b"], }, |
| 148 | }`, |
| 149 | androidmk: `include $(CLEAR_VARS) |
| 150 | LOCAL_MODULE := test |
| 151 | LOCAL_WHOLE_STATIC_LIBRARIES := \ |
| 152 | test |
| 153 | LOCAL_SRC_FILES := \ |
| 154 | b |
| 155 | include $(BUILD_SHARED_LIBRARY) |
| 156 | |
| 157 | include $(CLEAR_VARS) |
| 158 | LOCAL_MODULE := test |
| 159 | LOCAL_SRC_FILES := \ |
| 160 | a |
| 161 | include $(BUILD_STATIC_LIBRARY)`, |
| 162 | }, |
Colin Cross | b1a66c0 | 2015-06-29 16:24:57 -0700 | [diff] [blame] | 163 | // Manual translation |
| 164 | { |
| 165 | blueprint: `/* Android.mk:start |
| 166 | # Manual translation |
| 167 | Android.mk:end */ |
| 168 | cc_library { name: "test", host_supported: true, }`, |
| 169 | androidmk: `# Manual translation`, |
| 170 | }, |
| 171 | // Ignored translation |
| 172 | { |
| 173 | blueprint: `/* Android.mk:ignore */ |
| 174 | cc_library { name: "test", host_supported: true, }`, |
| 175 | androidmk: ``, |
| 176 | }, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | func TestModules(t *testing.T) { |
| 180 | for _, testCase := range moduleTestCases { |
| 181 | blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil) |
| 182 | if len(errs) > 0 { |
| 183 | t.Errorf("Failed to read blueprint: %q", errs) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 184 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 185 | |
| 186 | buf := &bytes.Buffer{} |
| 187 | writer := &androidMkWriter{ |
| 188 | blueprint: blueprint, |
| 189 | path: "", |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 190 | Writer: buf, |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | module := blueprint.Defs[0].(*bpparser.Module) |
Colin Cross | b093124 | 2015-06-29 14:18:27 -0700 | [diff] [blame] | 194 | err := writer.handleModule(module) |
| 195 | if err != nil { |
| 196 | t.Errorf("Unexpected error %s", err.Error()) |
| 197 | } |
Dan Willemsen | ead184a | 2015-06-23 22:47:38 -0700 | [diff] [blame] | 198 | |
| 199 | expect(t, testCase.blueprint, testCase.androidmk, buf.String()) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Trim left whitespace, and any trailing newlines. Leave inner blank lines and |
| 204 | // right whitespace so that we can still check line continuations are correct |
| 205 | func trim(str string) string { |
| 206 | var list []string |
| 207 | for _, s := range strings.Split(str, "\n") { |
| 208 | list = append(list, strings.TrimLeftFunc(s, unicode.IsSpace)) |
| 209 | } |
| 210 | return strings.TrimRight(strings.Join(list, "\n"), "\n") |
| 211 | } |
| 212 | |
| 213 | func expect(t *testing.T, testCase string, expected string, out string) { |
| 214 | expected = trim(expected) |
| 215 | out = trim(out) |
| 216 | if expected != out { |
| 217 | sep := " " |
| 218 | if strings.Index(expected, "\n") != -1 || strings.Index(out, "\n") != -1 { |
| 219 | sep = "\n" |
| 220 | } |
| 221 | |
| 222 | t.Errorf("test case: %s", testCase) |
| 223 | t.Errorf("unexpected difference:") |
| 224 | t.Errorf(" expected:%s%s", sep, expected) |
| 225 | t.Errorf(" got:%s%s", sep, out) |
Dan Willemsen | f33877b | 2015-06-23 23:34:49 -0700 | [diff] [blame] | 226 | } |
| 227 | } |