1#
2# SPDX-License-Identifier: BSD-3-Clause
3#
4# Copyright © 2019 Keith Packard
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above
14#    copyright notice, this list of conditions and the following
15#    disclaimer in the documentation and/or other materials provided
16#    with the distribution.
17#
18# 3. Neither the name of the copyright holder nor the names of its
19#    contributors may be used to endorse or promote products derived
20#    from this software without specific prior written permission.
21#
22# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33# OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35foreach target : targets
36  value = get_variable('target_' + target)
37
38  _libs = [get_variable('lib_c' + target)]
39  if is_variable('lib_semihost' + target)
40    _libs += [get_variable('lib_semihost' + target)]
41  endif
42
43  _lib_files=[]
44  foreach _lib : _libs
45    _lib_files += _lib.full_path()
46  endforeach
47
48  if is_variable('crt0_semihost' + target)
49    _objs = [get_variable('crt0_semihost'+ target)]
50  elif is_variable('crt0_hosted' + target)
51    _objs = [get_variable('crt0_hosted'+ target)]
52  else
53    _objs = []
54  endif
55
56  _c_args = value[1] + get_variable('test_c_args_' + target, test_c_args)
57  _link_args = value[1] + get_variable('test_link_args_' + target, test_link_args)
58  _link_depends = get_variable('test_link_depends_' + target, test_link_depends)
59
60  t1 = 'printf_scanf'
61  if target == ''
62    t1_name = t1
63  else
64    t1_name = t1 + '_' + target
65  endif
66
67  test(t1_name,
68       executable(t1_name, ['printf_scanf.c', 'lock-valid.c'],
69		  c_args: double_printf_compile_args + _c_args,
70		  link_args: double_printf_link_args + _lib_files + _link_args,
71		  objects: _objs,
72		  link_depends:  _link_depends + _libs,
73		  include_directories: inc),
74       depends: bios_bin,
75       env: test_env)
76
77  t1 = 'printff_scanff'
78  if target == ''
79    t1_name = t1
80  else
81    t1_name = t1 + '_' + target
82  endif
83
84  test(t1_name,
85       executable(t1_name, ['printf_scanf.c', 'lock-valid.c'],
86		  c_args: float_printf_compile_args + _c_args,
87		  link_args: float_printf_link_args + _lib_files + _link_args,
88		  objects: _objs,
89		  link_depends:  _link_depends + _libs,
90		  include_directories: inc),
91       depends: bios_bin,
92       env: test_env)
93
94  t1 = 'printfl_scanfl'
95  if target == ''
96    t1_name = t1
97  else
98    t1_name = t1 + '_' + target
99  endif
100
101  test(t1_name,
102       executable(t1_name, ['printf_scanf.c', 'lock-valid.c'],
103		  c_args: llong_printf_compile_args + _c_args,
104		  link_args: llong_printf_link_args + _lib_files + _link_args,
105		  objects: _objs,
106		  link_depends:  _link_depends + _libs,
107		  include_directories: inc),
108       depends: bios_bin,
109       env: test_env)
110
111  t1 = 'printfi_scanfi'
112  if target == ''
113    t1_name = t1
114  else
115    t1_name = t1 + '_' + target
116  endif
117
118  test(t1_name,
119       executable(t1_name, ['printf_scanf.c', 'lock-valid.c'],
120		  c_args: int_printf_compile_args + _c_args,
121		  link_args: int_printf_link_args + _lib_files + _link_args,
122		  objects: _objs,
123		  link_depends:  _link_depends + _libs,
124		  include_directories: inc),
125       depends: bios_bin,
126       env: test_env)
127
128  t1 = 'printfm_scanfm'
129  if target == ''
130    t1_name = t1
131  else
132    t1_name = t1 + '_' + target
133  endif
134
135  test(t1_name,
136       executable(t1_name, ['printf_scanf.c', 'lock-valid.c'],
137		  c_args: min_printf_compile_args + _c_args,
138		  link_args: min_printf_link_args + _lib_files + _link_args,
139		  objects: _objs,
140		  link_depends:  _link_depends + _libs,
141		  include_directories: inc),
142       depends: bios_bin,
143       env: test_env)
144
145  t1 = 'printf-tests'
146  if target == ''
147    t1_name = t1
148  else
149    t1_name = t1 + '_' + target
150  endif
151
152  test(t1_name,
153       executable(t1_name, ['printf-tests.c', 'lock-valid.c'],
154		  c_args: double_printf_compile_args + _c_args,
155		  link_args: double_printf_link_args + _lib_files + _link_args,
156		  objects: _objs,
157		  link_depends: _libs,
158		  include_directories: inc),
159       depends: bios_bin,
160       env: test_env)
161
162  t1 = 'printff-tests'
163  if target == ''
164    t1_name = t1
165  else
166    t1_name = t1 + '_' + target
167  endif
168
169  test(t1_name,
170       executable(t1_name, ['printf-tests.c', 'lock-valid.c'],
171		  c_args: float_printf_compile_args + _c_args,
172		  link_args: float_printf_link_args + _lib_files + _link_args,
173		  objects: _objs,
174		  link_depends: _libs,
175		  include_directories: inc),
176       depends: bios_bin,
177       env: test_env)
178
179  t1 = 'printfi-tests'
180  if target == ''
181    t1_name = t1
182  else
183    t1_name = t1 + '_' + target
184  endif
185
186  test(t1_name,
187       executable(t1_name, ['printf-tests.c', 'lock-valid.c'],
188		  c_args: int_printf_compile_args + _c_args,
189		  link_args: int_printf_link_args + _lib_files + _link_args,
190		  objects: _objs,
191		  link_depends: _libs,
192		  include_directories: inc),
193       depends: bios_bin,
194       env: test_env)
195
196  t1 = 'printfm-tests'
197  if target == ''
198    t1_name = t1
199  else
200    t1_name = t1 + '_' + target
201  endif
202
203  test(t1_name,
204       executable(t1_name, ['printf-tests.c', 'lock-valid.c'],
205		  c_args: min_printf_compile_args + _c_args,
206		  link_args: min_printf_link_args + _lib_files + _link_args,
207		  objects: _objs,
208		  link_depends: _libs,
209		  include_directories: inc),
210       depends: bios_bin,
211       env: test_env)
212
213  t1 = 'try-ilp32'
214  if target == ''
215    t1_name = t1
216  else
217    t1_name = t1 + '_' + target
218  endif
219
220  t1 = 'time-sprintf'
221  if target == ''
222    t1_name = t1
223  else
224    t1_name = t1 + '_' + target
225  endif
226
227  test(t1_name,
228       executable(t1_name, ['time-sprintf.c', 'lock-valid.c'],
229		  c_args: int_printf_compile_args + _c_args + ['-fno-builtin'],
230		  link_args: int_printf_link_args + _lib_files + _link_args,
231		  objects: _objs,
232		  link_depends: _libs,
233		  include_directories: inc),
234       depends: bios_bin,
235       env: test_env)
236
237  t1 = 'try-ilp32'
238  if target == ''
239    t1_name = t1
240  else
241    t1_name = t1 + '_' + target
242  endif
243
244  test(t1_name,
245       executable(t1_name, ['try-ilp32.c', 'try-ilp32-sub.c', 'lock-valid.c'],
246		  c_args: _c_args,
247		  link_args: _link_args,
248		  objects: _objs,
249		  link_with: _libs,
250		  include_directories: inc),
251       depends: bios_bin,
252       env: test_env)
253
254  t1 = 'hosted-exit'
255  if target == ''
256    t1_name = t1
257    t1_fail_name = t1 + '-fail'
258  else
259    t1_name = t1 + '_' + target
260    t1_fail_name = t1 + '-fail_' + target
261  endif
262
263  test(t1_name,
264       executable(t1_name, ['hosted-exit.c', 'lock-valid.c'],
265		  c_args: _c_args,
266		  link_args:  _link_args,
267		  objects: _objs,
268		  link_with: _libs,
269		  include_directories: inc),
270       depends: bios_bin,
271       env: test_env)
272
273  test(t1_fail_name,
274       executable(t1_fail_name, ['hosted-exit.c', 'lock-valid.c'],
275		  c_args: _c_args + ['-DRETVAL=1'],
276		  link_args:  _link_args,
277		  objects: _objs,
278		  link_with: _libs,
279		  include_directories: inc),
280       depends: bios_bin,
281       env: test_env,
282       should_fail: true
283      )
284
285  t1 = 'abort'
286  if target == ''
287    t1_name = t1
288  else
289    t1_name = t1 + '_' + target
290  endif
291
292  test(t1_name,
293       executable(t1_name, ['abort.c', 'lock-valid.c'],
294		  c_args: _c_args,
295		  link_args:  _link_args,
296		  objects: _objs,
297		  link_with: _libs,
298		  include_directories: inc),
299       depends: bios_bin,
300       env: test_env,
301       should_fail: true
302      )
303
304  # POSIX console code requires a constructor to run, so
305  # it is incompatible with the minimal crt0 when using stdout
306  # (which lock-valid.c uses).
307
308  if is_variable('crt0_minimal' + target) and not posix_console
309    _objs_minimal = [get_variable('crt0_minimal'+ target)]
310    t1 = 'constructor-skip'
311    if target == ''
312      t1_name = t1
313    else
314      t1_name = t1 + '_' + target
315    endif
316
317    test(t1_name,
318	 executable(t1_name, ['constructor-skip.c', 'lock-valid.c'],
319		    c_args: _c_args,
320		    link_args:  _link_args,
321		    objects: _objs_minimal,
322		    link_with: _libs,
323		    include_directories: inc),
324         depends: bios_bin,
325	 env: test_env)
326  endif
327
328  t1 = 'math_errhandling'
329  if target == ''
330    t1_name = t1
331  else
332    t1_name = t1 + '_' + target
333  endif
334
335  test(t1_name,
336       executable(t1_name, ['math_errhandling.c'],
337		  c_args: arg_fnobuiltin + _c_args,
338		  link_args: _link_args,
339		  objects: _objs,
340		  link_with: _libs,
341		  include_directories: inc),
342       depends: bios_bin,
343       env: test_env)
344
345  t1 = 'rounding-mode'
346  if target == ''
347    t1_name = t1
348  else
349    t1_name = t1 + '_' + target
350  endif
351
352  test(t1_name,
353       executable(t1_name, ['rounding-mode.c', 'rounding-mode-sub.c'],
354		  c_args: arg_fnobuiltin + _c_args,
355		  link_args: _link_args,
356		  objects: _objs,
357		  link_with: _libs,
358		  include_directories: inc),
359       depends: bios_bin,
360       env: test_env)
361
362  t1 = 'test-except'
363  t1_src = t1 + '.c'
364  if target == ''
365    t1_name = t1
366  else
367    t1_name = t1 + '_' + target
368  endif
369
370  test(t1_name,
371       executable(t1_name, [t1_src, 'lock-valid.c'],
372		  c_args: double_printf_compile_args + arg_fnobuiltin + _c_args,
373		  link_args: double_printf_link_args + _link_args,
374		  objects: _objs,
375		  link_with: _libs,
376		  link_depends:  _link_depends,
377		  include_directories: inc),
378       depends: bios_bin,
379       env: test_env,
380       should_fail: true
381      )
382
383  t1 = 'long_double'
384  t1_src = t1 + '.c'
385  if target == ''
386    t1_name = t1
387  else
388    t1_name = t1 + '_' + target
389  endif
390
391  test(t1_name,
392       executable(t1_name, t1_src,
393		  c_args: double_printf_compile_args + _c_args,
394		  link_args: double_printf_link_args + _link_args,
395		  objects: _objs,
396		  link_with: _libs,
397		  link_depends:  _link_depends,
398		  include_directories: inc),
399       depends: bios_bin,
400       env: test_env,
401       timeout: 90,
402      )
403
404  if get_option('test-stdin')
405    t1 = 'test-gets'
406    t1_src = 'test-gets.c'
407    if target == ''
408      t1_name = t1
409    else
410      t1_name = t1 + '_' + target
411    endif
412
413    test_gets = executable(t1_name, t1_src,
414		           c_args: _c_args + ['-D_FORTIFY_SOURCE=2'],
415		           link_args: _link_args,
416		           objects: _objs,
417		           link_with: _libs,
418		           link_depends:  _link_depends,
419		           include_directories: inc)
420
421    test(t1_name + '-success',
422         test_gets,
423         depends: bios_bin,
424         args: ['hi'],
425         env: test_env,
426         timeout: 90
427        )
428
429
430    test(t1_name + '-failure',
431         test_gets,
432         depends: bios_bin,
433         args: ['this is a long string that should overflow the buffer'],
434         env: test_env,
435         timeout: 90,
436         should_fail: true
437        )
438  endif
439
440  if have_ubsan
441    t1 = 'test-ubsan'
442    t1_src = t1 + '.c'
443    if target == ''
444      t1_name = t1
445    else
446      t1_name = t1 + '_' + target
447    endif
448
449    test_ubsan_flags = []
450    if sanitize_trap_on_error
451      test_ubsan_flags = ['-DSANITIZE_TRAP_ON_ERROR']
452    endif
453
454    test(t1_name,
455	 executable(t1_name, t1_src,
456		    c_args: c_sanitize_bounds_flags + _c_args + test_ubsan_flags,
457		    link_args: _link_args,
458		    objects: _objs,
459		    link_with: _libs,
460		    link_depends:  _link_depends,
461		    include_directories: inc),
462	 depends: bios_bin,
463	 env: test_env,
464	 should_fail: sanitize_trap_on_error
465	)
466  endif
467
468  t1 = 'test-long-longl'
469  if target == ''
470    t1_name = t1
471  else
472    t1_name = t1 + '_' + target
473  endif
474
475  test(t1_name,
476       executable(t1_name, ['test-long-long.c', 'lock-valid.c'],
477		  c_args: llong_printf_compile_args + _c_args,
478		  link_args: llong_printf_link_args + _lib_files + _link_args,
479		  objects: _objs,
480		  link_depends:  _link_depends + _libs,
481		  include_directories: inc),
482       depends: bios_bin,
483       timeout: 60,
484       env: test_env)
485
486  t1 = 'test-long-long'
487  if target == ''
488    t1_name = t1
489  else
490    t1_name = t1 + '_' + target
491  endif
492
493  test(t1_name,
494       executable(t1_name, ['test-long-long.c', 'lock-valid.c'],
495		  c_args: double_printf_compile_args + _c_args,
496		  link_args: double_printf_link_args + _lib_files + _link_args,
497		  objects: _objs,
498		  link_depends:  _link_depends + _libs,
499		  include_directories: inc),
500       depends: bios_bin,
501       timeout: 60,
502       env: test_env)
503
504  plain_tests = ['rand', 'regex', 'ungetc', 'fenv',
505		 'malloc', 'tls',
506		 'ffs', 'setjmp', 'atexit', 'on_exit',
507		 'math-funcs', 'timegm', 'time-tests',
508                 'test-strtod', 'test-strchr',
509		 'test-memset', 'test-put',
510		 'test-efcvt', 'test-atomic',
511		 'test-raise',
512                 'test-fma',
513		]
514
515  if have_attr_ctor_dtor
516    plain_tests += 'constructor'
517  endif
518
519  if have_complex
520    plain_tests += 'complex-funcs'
521  endif
522
523  if newlib_nano_malloc or tests_enable_full_malloc_stress
524    plain_tests += 'malloc_stress'
525  endif
526
527  if tinystdio
528    plain_tests += 't_fmemopen'
529  endif
530
531  if (posix_io or not tinystdio) and tests_enable_posix_io
532    plain_tests += ['posix-io']
533
534    # legacy stdio doesn't work on semihosting, so just skip it
535    if tinystdio
536      plain_tests += ['test-fopen', 'test-mktemp', 'test-fread-fwrite']
537    endif
538  endif
539
540  if tests_enable_stack_protector
541    plain_tests += 'stack-smash'
542  endif
543
544  foreach t1 : plain_tests
545    t1_src = t1 + '.c'
546    if target == ''
547      t1_name = t1
548    else
549      t1_name = t1 + '_' + target
550    endif
551    test_file_name_arg=['-DTEST_FILE_NAME="' + t1_name + '.txt"']
552
553    test(t1_name,
554	 executable(t1_name, [t1_src, 'lock-valid.c'],
555		    c_args: double_printf_compile_args + test_file_name_arg + _c_args,
556		    link_args: double_printf_link_args + _link_args,
557		    objects: _objs,
558		    link_with: _libs,
559		    link_depends:  _link_depends,
560		    include_directories: inc),
561         depends: bios_bin,
562	 timeout: 60,
563	 env: test_env)
564  endforeach
565
566endforeach
567
568if enable_native_tests
569
570  native_lib_m = cc.find_library('m', required: false)
571
572  if native_lib_m.found()
573    test('long_double_native',
574	 executable('long_double_native', 'long_double.c',
575		    c_args: native_c_args,
576		    link_args: native_c_args,
577		    dependencies: native_lib_m))
578    test('test-fma_native',
579	 executable('test-fma_native', 'test-fma.c',
580		    c_args: native_c_args,
581		    link_args: native_c_args,
582		    dependencies: native_lib_m))
583    test('math_errhandling_native',
584	 executable('math_errhandling_native', 'math_errhandling.c',
585		    c_args: native_c_args,
586		    link_args: native_c_args,
587		    dependencies: native_lib_m))
588    test('rounding-mode_native',
589	 executable('rounding-mode_native',
590		    ['rounding-mode.c', 'rounding-mode-sub.c'],
591		    c_args: native_c_args,
592		    link_args: native_c_args,
593		    dependencies: native_lib_m))
594    test('printf-tests_native',
595	 executable('printf-tests_native',
596		    'printf-tests.c',
597		    c_args: native_c_args,
598		    link_args: native_c_args,
599		    dependencies: native_lib_m))
600    test('printf_scanf_native',
601	 executable('printf_scanf_native',
602		    'printf_scanf.c',
603		    c_args: native_c_args,
604		    link_args: native_c_args,
605		    dependencies: native_lib_m))
606    test('test-efcvt_native',
607	 executable('test-efcvt_native',
608		    'test-efcvt.c',
609		    c_args: native_c_args,
610		    link_args: native_c_args,
611		    dependencies: native_lib_m))
612    test('test-strtod_native',
613	 executable('test-strtod_native',
614		    'test-strtod.c',
615		    c_args: native_c_args,
616		    link_args: native_c_args,
617		    dependencies: native_lib_m))
618
619    test('fenv-native',
620         executable('fenv-native',
621                    'fenv.c',
622                    c_args: native_c_args,
623                    link_args: native_c_args,
624                    dependencies: native_lib_m))
625  endif
626
627  test('test-atomic-native',
628       executable('test-atomic-native', 'test-atomic.c',
629		  c_args: native_c_args,
630		  link_args: native_c_args))
631
632  test('test-fread-fwrite-native',
633       executable('test-fread-fwrite-native', 'test-fread-fwrite.c',
634		  c_args: native_c_args + ['-DTEST_FILE_NAME="test-fread-fwrite-native.txt"'],
635		  link_args: native_c_args))
636
637endif
638
639subdir('libc-testsuite')
640
641if has_arm_semihost
642  subdir('semihost')
643endif
644