xref: /aosp_15_r20/external/pigweed/pw_stm32cube_build/py/tests/find_files_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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
26    def test_start_with_stm32(self):
27        with self.assertRaises(ValueError):
28            find_files.parse_product_str('f439zit')
29
30    def test_specify_chip(self):
31        with self.assertRaises(ValueError):
32            find_files.parse_product_str('stm32f43')
33
34    def test_stm32f412zx(self):
35        (family, defines, name) = find_files.parse_product_str('stm32f412zx')
36
37        self.assertEqual(family, 'stm32f4xx')
38        self.assertEqual(defines, {'STM32F412xx', 'STM32F412Zx'})
39        self.assertEqual(name, 'stm32f412zx')
40
41    def test_stm32f439xx(self):
42        (family, defines, name) = find_files.parse_product_str('STM32F439xx')
43
44        self.assertEqual(family, 'stm32f4xx')
45        self.assertEqual(defines, {'STM32F439xx'})
46        self.assertEqual(name, 'stm32f439xx')
47
48    def test_stm32f439(self):
49        (family, defines, name) = find_files.parse_product_str('STM32F439')
50
51        self.assertEqual(family, 'stm32f4xx')
52        self.assertEqual(defines, {'STM32F439xx'})
53        self.assertEqual(name, 'stm32f439xx')
54
55    def test_stm32f439xi(self):
56        (family, defines, name) = find_files.parse_product_str('STM32F439xI')
57
58        self.assertEqual(family, 'stm32f4xx')
59        self.assertEqual(defines, {'STM32F439xx', 'STM32F439xI'})
60        self.assertEqual(name, 'stm32f439xi')
61
62    def test_stm32f439zit6u(self):
63        (family, defines, name) = find_files.parse_product_str('stm32f439zit6u')
64
65        self.assertEqual(family, 'stm32f4xx')
66        self.assertEqual(
67            defines,
68            {'STM32F439xx', 'STM32F439Zx', 'STM32F439xI', 'STM32F439ZI'},
69        )
70        self.assertEqual(name, 'stm32f439zit6u')
71
72    def test_stm32l552zet(self):
73        (family, defines, name) = find_files.parse_product_str('stm32l552zet')
74
75        self.assertEqual(family, 'stm32l5xx')
76        self.assertEqual(
77            defines,
78            {'STM32L552xx', 'STM32L552Zx', 'STM32L552xE', 'STM32L552ZE'},
79        )
80        self.assertEqual(name, 'stm32l552zet')
81
82    def test_stm32l552xc(self):
83        (family, defines, name) = find_files.parse_product_str('stm32l552xc')
84
85        self.assertEqual(family, 'stm32l5xx')
86        self.assertEqual(defines, {'STM32L552xx', 'STM32L552xC'})
87        self.assertEqual(name, 'stm32l552xc')
88
89    def test_stm32wb5m(self):
90        (family, defines, name) = find_files.parse_product_str('stm32wb5m')
91
92        self.assertEqual(family, 'stm32wbxx')
93        self.assertEqual(defines, {'STM32WB5Mxx'})
94        self.assertEqual(name, 'stm32wb5mxx')
95
96
97class SelectDefineTest(unittest.TestCase):
98    """select_define tests."""
99
100    def test_stm32f412zx_not_found(self):
101        with self.assertRaises(ValueError):
102            find_files.select_define({'STM32F412xx', 'STM32F412Zx'}, "")
103
104    def test_stm32f412zx_found(self):
105        define = find_files.select_define(
106            {'STM32F412xx', 'STM32F412Zx'},
107            "asdf\nfdas\n#if defined(STM32F412Zx)\n",
108        )
109        self.assertEqual(define, 'STM32F412Zx')
110
111    def test_stm32f412zx_multiple_found(self):
112        with self.assertRaises(ValueError):
113            find_files.select_define(
114                {'STM32F412xx', 'STM32F412Zx'},
115                "asdf\n#if defined (STM32F412xx)\n#elif defined(STM32F412Zx)\n",
116            )
117
118
119class MatchFilenameTest(unittest.TestCase):
120    """match_filename tests."""
121
122    def test_stm32f412zx(self):
123        # Match should fail if product name is not specific enough
124        self.assertTrue(
125            find_files.match_filename('stm32f412zx', 'stm32f412zx_flash.icf')
126        )
127        self.assertFalse(
128            find_files.match_filename('stm32f412xx', 'stm32f412zx_flash.icf')
129        )
130        self.assertTrue(
131            find_files.match_filename('stm32f412zx', 'startup_stm32f412zx.s')
132        )
133        self.assertFalse(
134            find_files.match_filename('stm32f412xx', 'startup_stm32f429zx.s')
135        )
136
137    def test_stm32f439xx(self):
138        self.assertTrue(
139            find_files.match_filename('stm32f439xx', 'stm32f439xx_flash.icf')
140        )
141        self.assertFalse(
142            find_files.match_filename('stm32f439xx', 'stm32f429xx_flash.icf')
143        )
144        self.assertTrue(
145            find_files.match_filename('stm32f439xx', 'startup_stm32f439xx.s')
146        )
147        self.assertFalse(
148            find_files.match_filename('stm32f439xx', 'startup_stm32f429xx.s')
149        )
150
151    def test_stm32f439xi(self):
152        self.assertTrue(
153            find_files.match_filename('stm32f439xi', 'stm32f439xx_flash.icf')
154        )
155        self.assertFalse(
156            find_files.match_filename('stm32f439xi', 'stm32f429xx_flash.icf')
157        )
158        self.assertTrue(
159            find_files.match_filename('stm32f439xi', 'startup_stm32f439xx.s')
160        )
161        self.assertFalse(
162            find_files.match_filename('stm32f439xi', 'startup_stm32f429xx.s')
163        )
164
165    def test_stm32l552zet(self):
166        self.assertTrue(
167            find_files.match_filename('stm32l552zet', 'STM32L552xE_FLASH.ld')
168        )
169        self.assertTrue(
170            find_files.match_filename('stm32l552zet', 'STM32L552xx_FLASH.ld')
171        )
172        self.assertFalse(
173            find_files.match_filename('stm32l552zet', 'STM32L552xC_FLASH.ld')
174        )
175        self.assertTrue(
176            find_files.match_filename('stm32l552zet', 'stm32l552xe_flash.icf')
177        )
178        self.assertFalse(
179            find_files.match_filename('stm32l552zet', 'stm32l552xc_flash.icf')
180        )
181        self.assertTrue(
182            find_files.match_filename('stm32l552zet', 'startup_stm32l552xx.s')
183        )
184        self.assertFalse(
185            find_files.match_filename('stm32l552zet', 'startup_stm32l562xx.s')
186        )
187
188
189class FindLinkerFilesTest(unittest.TestCase):
190    """find_linker_files tests."""
191
192    TEST_PATH = pathlib.Path('/test/path')
193
194    def test_stm32f439xx(self):
195        files = [
196            'path/to/stm32f439xx_flash.icf',
197            'other/path/to/stm32f439xx_sram.icf',
198        ]
199        gcc_linker, iar_linker = find_files.find_linker_files(
200            'stm32f439xx', files, self.TEST_PATH
201        )
202
203        self.assertEqual(gcc_linker, None)
204        self.assertEqual(iar_linker, self.TEST_PATH / files[0])
205
206    def test_stm32f439xx_find_ld(self):
207        files = [
208            'path/to/stm32f439xx_flash.icf',
209            'other/path/to/stm32f439xx_sram.icf',
210            'path/to/STM32F439xx_FLASH.ld',
211        ]
212        gcc_linker, iar_linker = find_files.find_linker_files(
213            'stm32f439xx', files, self.TEST_PATH
214        )
215
216        self.assertEqual(gcc_linker, self.TEST_PATH / files[2])
217        self.assertEqual(iar_linker, self.TEST_PATH / files[0])
218
219    def test_stm32f439xc_error_multiple_matching_ld(self):
220        files = [
221            'path/to/stm32f439xx_flash.icf',
222            'other/path/to/stm32f439xx_sram.icf',
223            'other/path/to/STM32F439xI_FLASH.ld',
224            'path/to/STM32F439xx_FLASH.ld',
225        ]
226        with self.assertRaises(ValueError):
227            find_files.find_linker_files('stm32f439xi', files, self.TEST_PATH)
228
229    def test_stm32f439xc_error_multiple_matching_icf(self):
230        files = [
231            'path/to/stm32f439xx_flash.icf',
232            'other/path/to/stm32f439xc_flash.icf',
233        ]
234        with self.assertRaises(ValueError):
235            find_files.find_linker_files('stm32f439xc', files, self.TEST_PATH)
236
237    def test_stm32f439xc_error_none_found(self):
238        files = [
239            'path/to/stm32f439xc_flash.icf',
240            'other/path/to/stm32f439xc_flash.icf',
241        ]
242        with self.assertRaises(ValueError):
243            find_files.find_linker_files('stm32f439xx', files, self.TEST_PATH)
244
245    # ignore secure and nonsecure variants for the M33 boards
246    def test_stm32l552xe_ignore_s_ns(self):
247        files = [
248            'iar/linker/stm32l552xe_flash_ns.icf',
249            'iar/linker/stm32l552xe_flash_s.icf',
250            'iar/linker/stm32l552xe_flash.icf',
251            'gcc/linker/STM32L552xE_FLASH_ns.ld',
252            'gcc/linker/STM32L552xE_FLASH_s.ld',
253            'gcc/linker/STM32L552xE_FLASH.ld',
254        ]
255        gcc_linker, iar_linker = find_files.find_linker_files(
256            'stm32l552xe', files, self.TEST_PATH
257        )
258
259        self.assertEqual(gcc_linker, self.TEST_PATH / files[-1])
260        self.assertEqual(iar_linker, self.TEST_PATH / files[2])
261
262
263class FindStartupFileTest(unittest.TestCase):
264    """find_startup_file tests."""
265
266    TEST_PATH = pathlib.Path('/test/path')
267
268    def test_stm32f439xx_none_found(self):
269        files = [
270            'path/to/stm32f439xx_flash.icf',
271            'other/path/to/stm32f439xx_sram.icf',
272            'path/iar/startup_stm32f439xx.s',
273        ]
274        with self.assertRaises(ValueError):
275            find_files.find_startup_file('stm32f439xx', files, self.TEST_PATH)
276
277    def test_stm32f439xx(self):
278        files = [
279            'path/to/stm32f439xx_flash.icf',
280            'other/path/to/stm32f439xx_sram.icf',
281            'path/iar/startup_stm32f439xx.s',
282            'path/gcc/startup_stm32f439xx.s',
283        ]
284        startup_file = find_files.find_startup_file(
285            'stm32f439xx', files, self.TEST_PATH
286        )
287
288        self.assertEqual(startup_file, self.TEST_PATH / files[3])
289
290    def test_stm32f439xx_multiple_found(self):
291        files = [
292            'path/to/stm32f439xx_flash.icf',
293            'other/path/to/stm32f439xx_sram.icf',
294            'path/gcc/startup_stm32f439xc.s',
295            'path/gcc/startup_stm32f439xx.s',
296        ]
297        with self.assertRaises(ValueError):
298            find_files.find_startup_file('stm32f439xc', files, self.TEST_PATH)
299
300
301class GetSourceAndHeadersTest(unittest.TestCase):
302    """test_sources_and_headers tests."""
303
304    def test_sources_and_headers(self):
305        files = [
306            'random/header.h',
307            'random/source.c',
308            'cmsis_core/Include/core_cm4.h',
309            'cmsis_device/Include/stm32f4xx.h',
310            'cmsis_device/Include/stm32f439xx.h',
311            'hal_driver/Inc/stm32f4xx_hal_eth.h',
312            'hal_driver/Src/stm32f4xx_hal_adc.c',
313            'hal_driver/Inc/stm32f4xx_hal.h',
314            'hal_driver/Src/stm32f4xx_hal_timebase_tim_template.c',
315            'hal_driver/Src/stm32f4xx_hal_eth.c',
316        ]
317        path = pathlib.Path('/test/path/to/stm32cube')
318        sources, headers = find_files.get_sources_and_headers(files, path)
319        self.assertSetEqual(
320            set(
321                [
322                    path / 'hal_driver/Src/stm32f4xx_hal_adc.c',
323                    path / 'hal_driver/Src/stm32f4xx_hal_eth.c',
324                ]
325            ),
326            set(sources),
327        )
328        self.assertSetEqual(
329            set(
330                [
331                    path / 'cmsis_core/Include/core_cm4.h',
332                    path / 'cmsis_device/Include/stm32f4xx.h',
333                    path / 'cmsis_device/Include/stm32f439xx.h',
334                    path / 'hal_driver/Inc/stm32f4xx_hal_eth.h',
335                    path / 'hal_driver/Inc/stm32f4xx_hal.h',
336                ]
337            ),
338            set(headers),
339        )
340
341
342if __name__ == '__main__':
343    unittest.main()
344