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