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    '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]
165
166srcs_stdlib_stdio = [
167    'dtoa.c',
168    'ecvtbuf.c',
169    'efgcvt.c',
170    'gdtoa-gethex.c',
171    'gdtoa-hexnan.c',
172    'mprec.c',
173    'strtod.c',
174    'strtodg.c',
175    'strtol.c',
176    'strtoul.c',
177    'strtoll.c',
178    'strtoull.c',
179    'strtoimax.c',
180    'strtoumax.c',
181]
182
183lsrcs_stdlib = [
184  'strtold.c',
185  'strtorx.c',
186  'wcstold.c'
187]
188
189if not tinystdio
190  srcs_stdlib += srcs_stdlib_stdio
191  if have_long_double
192    srcs_stdlib += lsrcs_stdlib
193    if io_long_double
194      srcs_stdlib += 'ldtoa.c'
195    endif
196  endif
197endif
198
199# Work around compiler optimizing calls involving malloc/free
200
201c_args_malloc = []
202if cc.has_argument('-fno-builtin-malloc')
203  c_args_malloc += ['-fno-builtin-malloc']
204endif
205
206if cc.has_argument('-fno-builtin-free')
207  c_args_malloc += ['-fno-builtin-free']
208endif
209
210if newlib_nano_malloc
211  srcs_stdlib_malloc = nano_malloc_srcs_stdlib
212else
213  srcs_stdlib_malloc = std_malloc_srcs_stdlib
214endif
215
216if picoexit
217  srcs_stdlib += pico_exit_srcs_stdlib
218else
219  srcs_stdlib += newlib_exit_srcs_stdlib
220endif
221
222hdrs_stdlib = [
223    'arc4random.h',
224    'atexit.h',
225    'chacha_private.h',
226    'gd_qnan.h',
227    'gdtoa.h',
228    'local.h',
229    'mbctype.h',
230    'mprec.h',
231    'rand48.h',
232    'std.h',
233]
234
235srcs_stdlib_use = []
236foreach file : srcs_stdlib
237  s_file = fs.replace_suffix(file, '.S')
238  if file in srcs_machine
239    message('libc/stdlib/' + file + ': machine overrides generic')
240  elif s_file in srcs_machine
241    message('libc/stdlib/' + s_file + ': machine overrides generic')
242  else
243    srcs_stdlib_use += file
244  endif
245endforeach
246
247srcs_stdlib_malloc_use = []
248foreach file : srcs_stdlib_malloc
249  s_file = fs.replace_suffix(file, '.S')
250  if file in srcs_machine
251    message('libc/stdlib/' + file + ': machine overrides generic')
252  elif s_file in srcs_machine
253    message('libc/stdlib/' + s_file + ': machine overrides generic')
254  else
255    srcs_stdlib_malloc_use += file
256  endif
257endforeach
258
259src_stdlib = files(srcs_stdlib_use)
260
261foreach target : targets
262  value = get_variable('target_' + target)
263
264  # Have to build a sub-library to get custom c_args for malloc
265  set_variable('lib_stdlib' + target,
266	       static_library('stdlib_malloc' + target,
267			      srcs_stdlib_malloc_use,
268			      pic: false,
269			      include_directories: inc,
270			      c_args: value[1] + c_args + c_args_malloc))
271
272endforeach
273