• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# Copyright 2021 The Pigweed Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8#     https://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15"""Generate File List Tests."""
16
17import pathlib
18import unittest
19
20from pw_stm32cube_build import find_files
21
22
23class ParseProductStringTest(unittest.TestCase):
24    """parse_product_str tests."""
25    def test_start_with_stm32(self):
26        with self.assertRaises(ValueError):
27            find_files.parse_product_str('f439zit')
28
29    def test_specify_chip(self):
30        with self.assertRaises(ValueError):
31            find_files.parse_product_str('stm32f43')
32
33    def test_stm32f412zx(self):
34        (family, defines, name) = find_files.parse_product_str('stm32f412zx')
35
36        self.assertEqual(family, 'stm32f4xx')
37        self.assertEqual(defines, {'STM32F412xx', 'STM32F412Zx'})
38        self.assertEqual(name, 'stm32f412zx')
39
40    def test_stm32f439xx(self):
41        (family, defines, name) = find_files.parse_product_str('STM32F439xx')
42
43        self.assertEqual(family, 'stm32f4xx')
44        self.assertEqual(defines, {'STM32F439xx'})
45        self.assertEqual(name, 'stm32f439xx')
46
47    def test_stm32f439(self):
48        (family, defines, name) = find_files.parse_product_str('STM32F439')
49
50        self.assertEqual(family, 'stm32f4xx')
51        self.assertEqual(defines, {'STM32F439xx'})
52        self.assertEqual(name, 'stm32f439xx')
53
54    def test_stm32f439xi(self):
55        (family, defines, name) = find_files.parse_product_str('STM32F439xI')
56
57        self.assertEqual(family, 'stm32f4xx')
58        self.assertEqual(defines, {'STM32F439xx', 'STM32F439xI'})
59        self.assertEqual(name, 'stm32f439xi')
60
61    def test_stm32f439zit6u(self):
62        (family, defines,
63         name) = find_files.parse_product_str('stm32f439zit6u')
64
65        self.assertEqual(family, 'stm32f4xx')
66        self.assertEqual(
67            defines,
68            {'STM32F439xx', 'STM32F439Zx', 'STM32F439xI', 'STM32F439ZI'})
69        self.assertEqual(name, 'stm32f439zit6u')
70
71    def test_stm32l552zet(self):
72        (family, defines, name) = find_files.parse_product_str('stm32l552zet')
73
74        self.assertEqual(family, 'stm32l5xx')
75        self.assertEqual(
76            defines,
77            {'STM32L552xx', 'STM32L552Zx', 'STM32L552xE', 'STM32L552ZE'})
78        self.assertEqual(name, 'stm32l552zet')
79
80    def test_stm32l552xc(self):
81        (family, defines, name) = find_files.parse_product_str('stm32l552xc')
82
83        self.assertEqual(family, 'stm32l5xx')
84        self.assertEqual(defines, {'STM32L552xx', 'STM32L552xC'})
85        self.assertEqual(name, 'stm32l552xc')
86
87    def test_stm32wb5m(self):
88        (family, defines, name) = find_files.parse_product_str('stm32wb5m')
89
90        self.assertEqual(family, 'stm32wbxx')
91        self.assertEqual(defines, {'STM32WB5Mxx'})
92        self.assertEqual(name, 'stm32wb5mxx')
93
94
95class SelectDefineTest(unittest.TestCase):
96    """select_define tests."""
97    def test_stm32f412zx_not_found(self):
98        with self.assertRaises(ValueError):
99            find_files.select_define({'STM32F412xx', 'STM32F412Zx'}, "")
100
101    def test_stm32f412zx_found(self):
102        define = find_files.select_define(
103            {'STM32F412xx', 'STM32F412Zx'},
104            "asdf\nfdas\n#if defined(STM32F412Zx)\n")
105        self.assertEqual(define, 'STM32F412Zx')
106
107    def test_stm32f412zx_multiple_found(self):
108        with self.assertRaises(ValueError):
109            find_files.select_define({
110                'STM32F412xx', 'STM32F412Zx'
111            }, "asdf\n#if defined (STM32F412xx)\n#elif defined(STM32F412Zx)\n")
112
113
114class MatchFilenameTest(unittest.TestCase):
115    """match_filename tests."""
116    def test_stm32f412zx(self):
117        # Match should fail if product name is not specific enough
118        self.assertTrue(
119            find_files.match_filename('stm32f412zx', 'stm32f412zx_flash.icf'))
120        self.assertFalse(
121            find_files.match_filename('stm32f412xx', 'stm32f412zx_flash.icf'))
122        self.assertTrue(
123            find_files.match_filename('stm32f412zx', 'startup_stm32f412zx.s'))
124        self.assertFalse(
125            find_files.match_filename('stm32f412xx', 'startup_stm32f429zx.s'))
126
127    def test_stm32f439xx(self):
128        self.assertTrue(
129            find_files.match_filename('stm32f439xx', 'stm32f439xx_flash.icf'))
130        self.assertFalse(
131            find_files.match_filename('stm32f439xx', 'stm32f429xx_flash.icf'))
132        self.assertTrue(
133            find_files.match_filename('stm32f439xx', 'startup_stm32f439xx.s'))
134        self.assertFalse(
135            find_files.match_filename('stm32f439xx', 'startup_stm32f429xx.s'))
136
137    def test_stm32f439xi(self):
138        self.assertTrue(
139            find_files.match_filename('stm32f439xi', 'stm32f439xx_flash.icf'))
140        self.assertFalse(
141            find_files.match_filename('stm32f439xi', 'stm32f429xx_flash.icf'))
142        self.assertTrue(
143            find_files.match_filename('stm32f439xi', 'startup_stm32f439xx.s'))
144        self.assertFalse(
145            find_files.match_filename('stm32f439xi', 'startup_stm32f429xx.s'))
146
147    def test_stm32l552zet(self):
148        self.assertTrue(
149            find_files.match_filename('stm32l552zet', 'STM32L552xE_FLASH.ld'))
150        self.assertTrue(
151            find_files.match_filename('stm32l552zet', 'STM32L552xx_FLASH.ld'))
152        self.assertFalse(
153            find_files.match_filename('stm32l552zet', 'STM32L552xC_FLASH.ld'))
154        self.assertTrue(
155            find_files.match_filename('stm32l552zet', 'stm32l552xe_flash.icf'))
156        self.assertFalse(
157            find_files.match_filename('stm32l552zet', 'stm32l552xc_flash.icf'))
158        self.assertTrue(
159            find_files.match_filename('stm32l552zet', 'startup_stm32l552xx.s'))
160        self.assertFalse(
161            find_files.match_filename('stm32l552zet', 'startup_stm32l562xx.s'))
162
163
164class FindLinkerFilesTest(unittest.TestCase):
165    """find_linker_files tests."""
166    TEST_PATH = pathlib.Path('/test/path')
167
168    def test_stm32f439xx(self):
169        files = [
170            'path/to/stm32f439xx_flash.icf',
171            'other/path/to/stm32f439xx_sram.icf'
172        ]
173        gcc_linker, iar_linker = find_files.find_linker_files(
174            'stm32f439xx', files, self.TEST_PATH)
175
176        self.assertEqual(gcc_linker, None)
177        self.assertEqual(iar_linker, self.TEST_PATH / files[0])
178
179    def test_stm32f439xx_find_ld(self):
180        files = [
181            'path/to/stm32f439xx_flash.icf',
182            'other/path/to/stm32f439xx_sram.icf',
183            'path/to/STM32F439xx_FLASH.ld',
184        ]
185        gcc_linker, iar_linker = find_files.find_linker_files(
186            'stm32f439xx', files, self.TEST_PATH)
187
188        self.assertEqual(gcc_linker, self.TEST_PATH / files[2])
189        self.assertEqual(iar_linker, self.TEST_PATH / files[0])
190
191    def test_stm32f439xc_error_multiple_matching_ld(self):
192        files = [
193            'path/to/stm32f439xx_flash.icf',
194            'other/path/to/stm32f439xx_sram.icf',
195            'other/path/to/STM32F439xI_FLASH.ld',
196            'path/to/STM32F439xx_FLASH.ld',
197        ]
198        with self.assertRaises(ValueError):
199            find_files.find_linker_files('stm32f439xi', files, self.TEST_PATH)
200
201    def test_stm32f439xc_error_multiple_matching_icf(self):
202        files = [
203            'path/to/stm32f439xx_flash.icf',
204            'other/path/to/stm32f439xc_flash.icf',
205        ]
206        with self.assertRaises(ValueError):
207            find_files.find_linker_files('stm32f439xc', files, self.TEST_PATH)
208
209    def test_stm32f439xc_error_none_found(self):
210        files = [
211            'path/to/stm32f439xc_flash.icf',
212            'other/path/to/stm32f439xc_flash.icf',
213        ]
214        with self.assertRaises(ValueError):
215            find_files.find_linker_files('stm32f439xx', files, self.TEST_PATH)
216
217    # ignore secure and nonsecure variants for the M33 boards
218    def test_stm32l552xe_ignore_s_ns(self):
219        files = [
220            'iar/linker/stm32l552xe_flash_ns.icf',
221            'iar/linker/stm32l552xe_flash_s.icf',
222            'iar/linker/stm32l552xe_flash.icf',
223            'gcc/linker/STM32L552xE_FLASH_ns.ld',
224            'gcc/linker/STM32L552xE_FLASH_s.ld',
225            'gcc/linker/STM32L552xE_FLASH.ld',
226        ]
227        gcc_linker, iar_linker = find_files.find_linker_files(
228            'stm32l552xe', files, self.TEST_PATH)
229
230        self.assertEqual(gcc_linker, self.TEST_PATH / files[-1])
231        self.assertEqual(iar_linker, self.TEST_PATH / files[2])
232
233
234class FindStartupFileTest(unittest.TestCase):
235    """find_startup_file tests."""
236    TEST_PATH = pathlib.Path('/test/path')
237
238    def test_stm32f439xx_none_found(self):
239        files = [
240            'path/to/stm32f439xx_flash.icf',
241            'other/path/to/stm32f439xx_sram.icf',
242            'path/iar/startup_stm32f439xx.s',
243        ]
244        with self.assertRaises(ValueError):
245            find_files.find_startup_file('stm32f439xx', files, self.TEST_PATH)
246
247    def test_stm32f439xx(self):
248        files = [
249            'path/to/stm32f439xx_flash.icf',
250            'other/path/to/stm32f439xx_sram.icf',
251            'path/iar/startup_stm32f439xx.s',
252            'path/gcc/startup_stm32f439xx.s',
253        ]
254        startup_file = find_files.find_startup_file('stm32f439xx', files,
255                                                    self.TEST_PATH)
256
257        self.assertEqual(startup_file, self.TEST_PATH / files[3])
258
259    def test_stm32f439xx_multiple_found(self):
260        files = [
261            'path/to/stm32f439xx_flash.icf',
262            'other/path/to/stm32f439xx_sram.icf',
263            'path/gcc/startup_stm32f439xc.s',
264            'path/gcc/startup_stm32f439xx.s',
265        ]
266        with self.assertRaises(ValueError):
267            find_files.find_startup_file('stm32f439xc', files, self.TEST_PATH)
268
269
270class GetSourceAndHeadersTest(unittest.TestCase):
271    """test_sources_and_headers tests."""
272    def test_sources_and_headers(self):
273        files = [
274            'random/header.h',
275            'random/source.c',
276            'cmsis_core/Include/core_cm4.h',
277            'cmsis_device/Include/stm32f4xx.h',
278            'cmsis_device/Include/stm32f439xx.h',
279            'hal_driver/Inc/stm32f4xx_hal_eth.h',
280            'hal_driver/Src/stm32f4xx_hal_adc.c',
281            'hal_driver/Inc/stm32f4xx_hal.h',
282            'hal_driver/Src/stm32f4xx_hal_timebase_tim_template.c',
283            'hal_driver/Src/stm32f4xx_hal_eth.c',
284        ]
285        path = pathlib.Path('/test/path/to/stm32cube')
286        sources, headers = find_files.get_sources_and_headers(files, path)
287        self.assertSetEqual(
288            set([
289                str(path / 'hal_driver/Src/stm32f4xx_hal_adc.c'),
290                str(path / 'hal_driver/Src/stm32f4xx_hal_eth.c'),
291            ]), set(sources))
292        self.assertSetEqual(
293            set([
294                str(path / 'cmsis_core/Include/core_cm4.h'),
295                str(path / 'cmsis_device/Include/stm32f4xx.h'),
296                str(path / 'cmsis_device/Include/stm32f439xx.h'),
297                str(path / 'hal_driver/Inc/stm32f4xx_hal_eth.h'),
298                str(path / 'hal_driver/Inc/stm32f4xx_hal.h'),
299            ]), set(headers))
300
301
302if __name__ == '__main__':
303    unittest.main()
304