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#
35nano_malloc_srcs_stdlib = [
36  'nano-malloc-calloc.c',
37  'nano-malloc-cfree.c',
38  'nano-malloc-free.c',
39  'nano-malloc-getpagesize.c',
40  'nano-malloc-mallinfo.c',
41  'nano-malloc-malloc.c',
42  'nano-malloc-malloc_stats.c',
43  'nano-malloc-malloc_usable_size.c',
44  'nano-malloc-mallopt.c',
45  'nano-malloc-memalign.c',
46  'nano-malloc-posix_memalign.c',
47  'nano-malloc-pvalloc.c',
48  'nano-malloc-realloc.c',
49  'nano-malloc-valloc.c',
50]
51
52std_malloc_srcs_stdlib = [
53  'malloc-calloc.c',
54  'malloc-cfree.c',
55  'malloc-free.c',
56  'malloc-mallinfo.c',
57  'malloc-malloc.c',
58  'malloc-malloc_stats.c',
59  'malloc-malloc_usable_size.c',
60  'malloc-mallopt.c',
61  'malloc-memalign.c',
62  'malloc-pvalloc.c',
63  'malloc-realloc.c',
64  'malloc-valloc.c',
65]
66
67pico_exit_srcs_stdlib = [
68  'pico-atexit.c',
69  'pico-exit.c',
70  'pico-onexit.c',
71  'pico-cxa-atexit.c',
72]
73
74newlib_exit_srcs_stdlib = [
75  '__atexit.c',
76  '__call_atexit.c',
77  'atexit.c',
78  'cxa_atexit.c',
79  'cxa_finalize.c',
80  'exit.c',
81  'on_exit.c',
82  'quick_exit.c',
83]
84
85srcs_stdlib = [
86    'a64l.c',
87    'abort.c',
88    'abs.c',
89    'aligned_alloc.c',
90    'arc4random.c',
91    'arc4random_uniform.c',
92    'assert.c',
93    'atof.c',
94    'atoff.c',
95    'atoi.c',
96    'atol.c',
97    'atoll.c',
98    'btowc.c',
99    'div.c',
100    'drand48.c',
101    'environ.c',
102    'eprintf.c',
103    'erand48.c',
104    '_Exit.c',
105    'getenv.c',
106    'getenv_r.c',
107    'getopt.c',
108    'getsubopt.c',
109    'imaxabs.c',
110    'imaxdiv.c',
111    'itoa.c',
112    'jrand48.c',
113    'l64a.c',
114    'labs.c',
115    'lcong48.c',
116    'ldiv.c',
117    'llabs.c',
118    'lldiv.c',
119    'lrand48.c',
120    'mblen.c',
121    'mbrlen.c',
122    'mbrtowc.c',
123    'mbsinit.c',
124    'mbsnrtowcs.c',
125    'mbsrtowcs.c',
126    'mbstowcs.c',
127    'mbtowc.c',
128    'mbtowc_r.c',
129    'mrand48.c',
130    'mstats.c',
131    'nrand48.c',
132    'putenv.c',
133    'rand48.c',
134    'rand.c',
135    'random.c',
136    'rand_r.c',
137    'reallocarray.c',
138    'reallocf.c',
139    'rpmatch.c',
140    'sb_charsets.c',
141    'seed48.c',
142    'setenv.c',
143    'srand.c',
144    'srand48.c',
145    'srandom.c',
146    'system.c',
147    'utoa.c',
148    'wcrtomb.c',
149    'wcsnrtombs.c',
150    'wcsrtombs.c',
151    'wcstod.c',
152    'wcstoimax.c',
153    'wcstol.c',
154    'wcstoll.c',
155    'wcstombs.c',
156    'wcstoul.c',
157    'wcstoull.c',
158    'wcstoumax.c',
159    'wctob.c',
160    'wctomb.c',
161    'wctomb_r.c',
162]
163
164srcs_stdlib_stdio = [
165    'dtoa.c',
166    'ecvtbuf.c',
167    'efgcvt.c',
168    'gdtoa-gethex.c',
169    'gdtoa-hexnan.c',
170    'mprec.c',
171    'strtod.c',
172    'strtodg.c',
173    'strtol.c',
174    'strtoul.c',
175    'strtoll.c',
176    'strtoull.c',
177    'strtoimax.c',
178    'strtoumax.c',
179]
180
181lsrcs_stdlib = [
182  'strtold.c',
183  'strtorx.c',
184  'wcstold.c'
185]
186
187if not tinystdio
188  srcs_stdlib += srcs_stdlib_stdio
189  if have_long_double
190    srcs_stdlib += lsrcs_stdlib
191    if io_long_double
192      srcs_stdlib += 'ldtoa.c'
193    endif
194  endif
195endif
196
197# Work around compiler optimizing calls involving malloc/free
198
199c_args_malloc = []
200if cc.has_argument('-fno-builtin-malloc')
201  c_args_malloc += ['-fno-builtin-malloc']
202endif
203
204if cc.has_argument('-fno-builtin-free')
205  c_args_malloc += ['-fno-builtin-free']
206endif
207
208if newlib_nano_malloc
209  srcs_stdlib_malloc = nano_malloc_srcs_stdlib
210else
211  srcs_stdlib_malloc = std_malloc_srcs_stdlib
212endif
213
214if picoexit
215  srcs_stdlib += pico_exit_srcs_stdlib
216else
217  srcs_stdlib += newlib_exit_srcs_stdlib
218endif
219
220hdrs_stdlib = [
221    'arc4random.h',
222    'atexit.h',
223    'chacha_private.h',
224    'gd_qnan.h',
225    'gdtoa.h',
226    'local.h',
227    'mbctype.h',
228    'mprec.h',
229    'on_exit_args.h',
230    'rand48.h',
231    'std.h',
232]
233
234srcs_stdlib_use = []
235foreach file : srcs_stdlib
236  s_file = fs.replace_suffix(file, '.S')
237  if file in srcs_machine
238    message('libc/stdlib/' + file + ': machine overrides generic')
239  elif s_file in srcs_machine
240    message('libc/stdlib/' + s_file + ': machine overrides generic')
241  else
242    srcs_stdlib_use += file
243  endif
244endforeach
245
246srcs_stdlib_malloc_use = []
247foreach file : srcs_stdlib_malloc
248  s_file = fs.replace_suffix(file, '.S')
249  if file in srcs_machine
250    message('libc/stdlib/' + file + ': machine overrides generic')
251  elif s_file in srcs_machine
252    message('libc/stdlib/' + s_file + ': machine overrides generic')
253  else
254    srcs_stdlib_malloc_use += file
255  endif
256endforeach
257
258src_stdlib = files(srcs_stdlib_use)
259
260foreach target : targets
261  value = get_variable('target_' + target)
262
263  # Have to build a sub-library to get custom c_args for malloc
264  set_variable('lib_stdlib' + target,
265	       static_library('stdlib_malloc' + target,
266			      srcs_stdlib_malloc_use,
267			      pic: false,
268			      include_directories: inc,
269			      c_args: value[1] + ['-DINTERNAL_NEWLIB'] + c_args_malloc))
270
271endforeach
272