aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/meson.build
blob: 7e0956d73753192023692d564639fd00d9dab924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
project('mlibc', default_options: ['warning_level=2', 'cpp_std=c++20'])

fs = import('fs')

rtdl_include_dirs = [
	include_directories('options/internal/include'),
	include_directories('options/internal' / host_machine.cpu_family() + '-include'),
	include_directories('options/rtdl' / host_machine.cpu_family()),
	include_directories('options/rtdl/include'),
]
libc_include_dirs = [
	include_directories('options/internal/include'),
	include_directories('options/elf/include'),
	include_directories('options/lsb/include'),
	include_directories('options/rtdl/include'),
	include_directories('options/rtdl' / host_machine.cpu_family()),
	include_directories('options/internal' / host_machine.cpu_family() + '-include')
]

rtdl_sources = [ ]
rtdl_dso_sources = [ ]
libc_sources = [ ]
libc_sublibs = [ ]

libc_deps = [ ]
rtdl_deps = [ ]

headers_only = get_option('headers_only')
no_headers = get_option('mlibc_no_headers')
library_type = get_option('default_library')
build_tests = get_option('build_tests')
build_tests_host_libc = get_option('build_tests_host_libc')
disable_ansi_option = get_option('disable_ansi_option')
disable_posix_option = get_option('disable_posix_option')
disable_linux_option = get_option('disable_linux_option')
disable_iconv_option = get_option('disable_iconv_option')
disable_intl_option = get_option('disable_intl_option')
disable_glibc_option = get_option('disable_glibc_option')
disable_crypt_option = get_option('disable_crypt_option')
disable_bsd_option = get_option('disable_bsd_option')
disable_libgcc_dependency = get_option('disable_libgcc_dependency')
internal_conf = configuration_data()
mlibc_conf = configuration_data()

if not headers_only
	cshim_dep = dependency('cshim',
				required: false,
				fallback: ['cshim', 'cshim_dep'])
	libc_deps += cshim_dep
	rtdl_deps += cshim_dep

	cxxshim_dep = dependency('cxxshim',
				 required: false,
				 fallback: ['cxxshim', 'cxxshim_dep'])
	libc_deps += cxxshim_dep
	rtdl_deps += cxxshim_dep

	frigg_dep = dependency(
		'frigg',
		default_options: ['frigg_no_install=true'],
		fallback: ['frigg', 'frigg_dep'],
	)
	libc_deps += frigg_dep
	rtdl_deps += frigg_dep

	add_languages('c', 'cpp')
	c_compiler = meson.get_compiler('c')
	cpp_compiler = meson.get_compiler('cpp')

	add_project_arguments('-Wno-unused-function', '-D__MLIBC_BUILDING_MLIBC', language: ['c', 'cpp'])
	add_project_arguments('-nostdinc', '-fno-builtin', '-ffreestanding', language: ['c', 'cpp'])
	add_project_arguments('-Werror=misleading-indentation', language: ['c', 'cpp'])
	add_project_arguments('-fno-rtti', '-fno-exceptions', language: 'cpp')
	add_project_link_arguments('-nostdlib', language: ['c', 'cpp'])

	if not cshim_dep.found()
		searchdirs = run_command(c_compiler.cmd_array(), '-print-search-dirs',
					check: true).stdout()
		searchdirs_arr = searchdirs.split('\n')
		searchline = 'install: '
		ccdir = ''
		if c_compiler.get_id() == 'clang'
			searchline = 'libraries: ='
		endif

		foreach line : searchdirs_arr
			if line.startswith(searchline)
				ccdir = line.strip(searchline)
				ccdir = ccdir.split(':')[0]
				break
			endif
		endforeach

		if ccdir == ''
			error('could not find compiler-specific header directory')
		endif

		if c_compiler.get_id() == 'gcc' and fs.exists(ccdir / 'include-fixed')
			rtdl_include_dirs += include_directories(ccdir / 'include-fixed')
			libc_include_dirs += include_directories(ccdir / 'include-fixed')
		endif

		rtdl_include_dirs += include_directories(ccdir / 'include')
		libc_include_dirs += include_directories(ccdir / 'include')
	endif

	if not cxxshim_dep.found()
		cplusplus_include_path = []

		c_output = run_command(c_compiler.cmd_array(), '-E', '-v', '-x', 'c',
					'/dev/null', '-o', '-',
					capture: true,
					check: true).stderr().split('\n')

		cpp_output = run_command(cpp_compiler.cmd_array(), '-E', '-v', '-x',
					'c++', '/dev/null', '-o', '-',
					capture: true,
					check: true).stderr().split('\n')

		c_relevant_lines = []

		relevantmarker = '#include <...>'
		relevant_started = false

		foreach line : c_output
			if relevant_started
				if not line.startswith(' ')
					break
				endif
				c_relevant_lines += line.strip()
			elif line.startswith(relevantmarker)
				relevant_started = true
			endif
		endforeach

		relevant_started = false

		foreach line : cpp_output
			if relevant_started
				if not line.startswith(' ')
					break
				endif
				debug('maybe relevant', line)
				stripped = line.strip()
				if stripped in c_relevant_lines
					debug('not relevant (is C)', line)
					continue
				endif
				cplusplus_include_path += include_directories(stripped)
			elif line.startswith(relevantmarker)
				relevant_started = true
			endif
		endforeach

		rtdl_include_dirs += cplusplus_include_path
		libc_include_dirs += cplusplus_include_path
	endif
endif

internal_conf.set_quoted('MLIBC_SYSTEM_NAME', host_machine.system())
internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', false)
internal_conf.set10('MLIBC_MMAP_ALLOCATE_DSO', false)
internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', false)
internal_conf.set10('MLIBC_DEBUG_ALLOCATOR', get_option('debug_allocator'))

#----------------------------------------------------------------------------------------
# Configuration based on sysdeps.
#----------------------------------------------------------------------------------------

# Process sysdeps first, as sysdeps might want to disable unsupported options.
provides_bits_syscall_h = false
if host_machine.system() == 'linux'
	provides_bits_syscall_h = true
	rtdl_include_dirs += include_directories('sysdeps/linux/include')
	libc_include_dirs += include_directories('sysdeps/linux/include')

	if not headers_only
		if get_option('linux_kernel_headers') == ''
			error('linux_kernel_headers is not set')
		endif

		if not import('fs').is_dir(get_option('linux_kernel_headers'))
			error('linux_kernel_headers is not set to a valid path')
		endif

		rtdl_include_dirs += include_directories(get_option('linux_kernel_headers'))
		libc_include_dirs += include_directories(get_option('linux_kernel_headers'))
	endif

	internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
	internal_conf.set10('MLIBC_MMAP_ALLOCATE_DSO', true)
	subdir('sysdeps/linux')
elif host_machine.system() == 'aero'
	rtdl_include_dirs += include_directories('sysdeps/aero/include')
	libc_include_dirs += include_directories('sysdeps/aero/include')
	internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
	internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
	subdir('sysdeps/aero')
elif host_machine.system() == 'hyra'
	rtdl_include_dirs += include_directories('sysdeps/hyra/include')
	libc_include_dirs += include_directories('sysdeps/hyra/include')
	internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
	internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
	subdir('sysdeps/hyra')
elif host_machine.system() == 'managarm'
	# TODO: Adopt the include_directories() commands from the managarm meson.build.
	rtdl_include_dirs += include_directories('sysdeps/managarm/include')
	libc_include_dirs += include_directories('sysdeps/managarm/include')
	internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
	internal_conf.set10('MLIBC_MMAP_ALLOCATE_DSO', true)
	internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
	subdir('sysdeps/managarm')
elif host_machine.system() == 'ironclad'
	disable_linux_option = true
	rtdl_include_dirs += include_directories('sysdeps/ironclad/include')
	libc_include_dirs += include_directories('sysdeps/ironclad/include')
	subdir('sysdeps/ironclad')
elif host_machine.system() == 'keyronex'
	rtdl_include_dirs += include_directories('sysdeps/keyronex/include')
	libc_include_dirs += include_directories('sysdeps/keyronex/include')
	internal_conf.set10('MLIBC_MAP_DSO_SEGMENTS', true)
	internal_conf.set10('MLIBC_MAP_FILE_WINDOWS', true)
	subdir('sysdeps/keyronex')
elif host_machine.system() == 'lyre'
	disable_linux_option = true
	rtdl_include_dirs += include_directories('sysdeps/lyre/include')
	libc_include_dirs += include_directories('sysdeps/lyre/include')
	subdir('sysdeps/lyre')
elif host_machine.system() == 'lemon'
	rtdl_include_dirs += include_directories('sysdeps/lemon/include')
	libc_include_dirs += include_directories('sysdeps/lemon/include')
	subdir('sysdeps/lemon')
elif host_machine.system() == 'dripos'
	disable_linux_option = true
	rtdl_include_dirs += include_directories('sysdeps/dripos/include')
	libc_include_dirs += include_directories('sysdeps/dripos/include')
	subdir('sysdeps/dripos')
else
	error('No sysdeps defined for OS: ' + host_machine.system())
endif

#----------------------------------------------------------------------------------------
# Configuration based on enabled options.
#----------------------------------------------------------------------------------------

mlibc_conf.set10('__MLIBC_ANSI_OPTION', not disable_ansi_option)
mlibc_conf.set10('__MLIBC_POSIX_OPTION', not disable_posix_option)
mlibc_conf.set10('__MLIBC_LINUX_OPTION', not disable_linux_option)
mlibc_conf.set10('__MLIBC_INTL_OPTION', not disable_intl_option)
mlibc_conf.set10('__MLIBC_ICONV_OPTION', not disable_iconv_option)
mlibc_conf.set10('__MLIBC_GLIBC_OPTION', not disable_glibc_option)
mlibc_conf.set10('__MLIBC_CRYPT_OPTION', not disable_crypt_option)
mlibc_conf.set10('__MLIBC_BSD_OPTION', not disable_bsd_option)
mlibc_conf.set10('__MLIBC_SYSDEP_HAS_BITS_SYSCALL_H', provides_bits_syscall_h)

if not disable_ansi_option
	rtdl_include_dirs += include_directories('options/ansi/include')
	libc_include_dirs += include_directories('options/ansi/include')
endif

if not disable_posix_option
	rtdl_include_dirs += include_directories('options/posix/include')
	libc_include_dirs += include_directories('options/posix/include')
endif

if not disable_iconv_option
	rtdl_include_dirs += include_directories('options/iconv/include')
	libc_include_dirs += include_directories('options/iconv/include')
endif

if not disable_intl_option
	libc_include_dirs += include_directories('options/intl/include')
endif

if not disable_linux_option
	if not headers_only
		if get_option('linux_kernel_headers') == ''
			error('linux_kernel_headers is not set')
		endif

		if not import('fs').is_dir(get_option('linux_kernel_headers'))
			error('linux_kernel_headers is not set to a valid path')
		endif
	endif

	rtdl_include_dirs += include_directories('options/linux/include')
	libc_include_dirs += include_directories('options/linux/include')
	rtdl_include_dirs += include_directories(get_option('linux_kernel_headers'))
	libc_include_dirs += include_directories(get_option('linux_kernel_headers'))
endif

if not disable_glibc_option
	rtdl_include_dirs += include_directories('options/glibc/include')
	libc_include_dirs += include_directories('options/glibc/include')
endif

if not disable_crypt_option
	libc_include_dirs += include_directories('options/crypt/include')
endif

if not disable_bsd_option
	rtdl_include_dirs += include_directories('options/bsd/include')
	libc_include_dirs += include_directories('options/bsd/include')
endif

rtdl_include_dirs += include_directories('options/elf/include')
libc_include_dirs += include_directories('options/elf/include')
libc_include_dirs += include_directories('.')

#----------------------------------------------------------------------------------------

configure_file(input: 'internal-config.h.in',
	output: 'internal-config.h',
	configuration: internal_conf)

configure_file(input: 'mlibc-config.h.in',
	output: 'mlibc-config.h',
	configuration: mlibc_conf,
	install: not no_headers,
	install_dir: get_option('includedir'))

internal_sources = [
	'options/internal/generic/allocator.cpp',
	'options/internal/generic/charcode.cpp',
	'options/internal/generic/charset.cpp',
	'options/internal/generic/debug.cpp',
	'options/internal/generic/ensure.cpp',
	'options/internal/generic/essential.cpp',
	'options/internal/generic/frigg.cpp',
	'options/internal/generic/global-config.cpp',
	'options/internal/generic/inline-emitter.cpp',
	'options/internal/generic/locale.cpp',
	'options/internal/generic/sigset.cpp',
	'options/internal/generic/strings.cpp',
	'options/internal/generic/ubsan.cpp',
	'options/internal/generic/threads.cpp',
	'options/internal/gcc/stack_protector.cpp',
	'options/internal/gcc/guard-abi.cpp',
	'options/internal/gcc/initfini.cpp',
	'options/internal/gcc-extra/cxxabi.cpp',
	'options/internal' / host_machine.cpu_family() / 'setjmp.S',
	'options/internal' / host_machine.cpu_family() / 'fenv.S',
]

internal_dso_sources = [
	'options/internal' / host_machine.cpu_family() / 'mlibc_crtbegin.S',
	'options/internal' / host_machine.cpu_family() / 'mlibc_crtend.S',
]

if not no_headers
	install_headers(
		'options/internal/include/stdint.h'
	)
	install_headers(
		'options/internal/include/bits/wchar_t.h',
		'options/internal/include/bits/wchar.h',
		'options/internal/include/bits/wint_t.h',
		'options/internal/include/bits/size_t.h',
		'options/internal/include/bits/types.h',
		'options/internal/include/bits/ensure.h',
		'options/internal/include/bits/machine.h',
		'options/internal/include/bits/mbstate.h',
		'options/internal/include/bits/nl_item.h',
		'options/internal/include/bits/null.h',
		'options/internal/include/bits/off_t.h',
		'options/internal/include/bits/ssize_t.h',
		'options/internal/include/bits/sigset_t.h',
		'options/internal/include/bits/inline-definition.h',
		'options/internal/include/bits/ether_addr.h',
		'options/internal/include/bits/cpu_set.h',
		'options/internal/include/bits/threads.h',
		'options/internal/include/bits/winsize.h',
		subdir: 'bits'
	)
endif

rtdl_sources += [
	'options/internal/gcc/stack_protector.cpp',
	'options/internal/gcc/guard-abi.cpp',
	'options/internal/generic/allocator.cpp',
	'options/internal/generic/debug.cpp',
	'options/internal/generic/ensure.cpp',
	'options/internal/generic/essential.cpp',
	'options/internal/generic/inline-emitter.cpp',
	'options/internal/generic/frigg.cpp',
	'options/internal/generic/ubsan.cpp',
	'options/rtdl/generic/main.cpp',
	'options/rtdl/generic/linker.cpp',
	'options/rtdl' / host_machine.cpu_family() / 'runtime.S'
]

rtdl_dso_sources += ['options/rtdl' / host_machine.cpu_family() / 'entry.S']

subdir('options/elf')
subdir('options/ansi')
subdir('options/posix')
subdir('options/lsb')
subdir('options/glibc')
subdir('options/linux')
subdir('options/iconv')
subdir('options/intl')
subdir('options/crypt')
subdir('options/bsd')

rtlib_deps = []

if not headers_only
	if not disable_libgcc_dependency
		libgcc = meson.get_compiler('c').find_library('gcc', required: false)

		compiler_rt_name = 'libclang_rt.builtins-' + host_machine.cpu_family()
		compiler_rt = meson.get_compiler('c').find_library(compiler_rt_name, required: false)

		if not compiler_rt.found()
			compiler_rt_name = 'libclang_rt.builtins'
			compiler_rt = meson.get_compiler('c').find_library(compiler_rt_name, required: false)
		endif

		if libgcc.found()
			rtlib_deps += libgcc
		elif compiler_rt.found()
			rtlib_deps += compiler_rt
		else
			error('neither libgcc nor ' + compiler_rt_name + ' was found')
		endif
	endif

	ld_cpp_args = [
		'-fvisibility=hidden',
		'-fvisibility-inlines-hidden',
		'-fno-stack-protector',
		'-DMLIBC_BUILDING_RTDL'
	]

	libc_all_sources = [
		libc_sources,
		internal_sources,
		ansi_sources,
		lsb_sources,
	]

	# Our library have different behaviour when built as static and shared libraries.
	# Hence we need to rebuild the object files with a different define for each mode.
	if library_type in ['static', 'both']
		static_cpp_args = [
			'-DMLIBC_STATIC_BUILD',
			'-DFRIGG_HAVE_LIBC',
		]
		ld_static_lib = static_library('ld', rtdl_sources,
			name_prefix: '',
			cpp_args: ld_cpp_args + static_cpp_args,
			include_directories: rtdl_include_dirs,
			dependencies: rtdl_deps + rtlib_deps,
			install: false
		)
		libc_static = static_library('c', libc_all_sources,
			cpp_args: static_cpp_args + ['-fno-stack-protector'],
			include_directories: libc_include_dirs,
			dependencies: libc_deps + rtlib_deps,
			link_with: [ld_static_lib],
			link_whole: [libc_sublibs, ld_static_lib],
			install: true
		)
	endif
	if library_type in ['shared', 'both']
		ld_shared_lib = shared_library('ld', rtdl_sources + rtdl_dso_sources,
			name_prefix: '',
			cpp_args: ld_cpp_args,
			include_directories: rtdl_include_dirs,
			dependencies: rtdl_deps + rtlib_deps,
			install: true
		)
		libc_shared = shared_library('c', libc_all_sources + internal_dso_sources,
			include_directories: libc_include_dirs,
			dependencies: libc_deps + rtlib_deps,
			link_with: [ld_shared_lib],
			link_whole: libc_sublibs,
			install: true
		)
	endif

	library('pthread', 'dummy-libs/libpthread/src/dummy.cpp', install: true)
	library('rt', 'dummy-libs/librt/src/dummy.cpp', install: true)
	library('util', 'dummy-libs/libutil/src/dummy.cpp', install: true)
	library('m', 'dummy-libs/libm/src/dummy.cpp', install: true)
	if not disable_crypt_option
		library('crypt', 'dummy-libs/libcrypt/src/dummy.cpp', install: true)
	endif
	library('resolv', 'dummy-libs/libresolv/src/dummy.cpp', install: true)
	library('dl', 'dummy-libs/libdl/src/dummy.cpp', install: true)
	library('ssp', 'dummy-libs/libssp/src/dummy.cpp', install: true)
	library('ssp_nonshared', 'dummy-libs/libssp_nonshared/src/dummy.cpp', install: true)
endif

summary_info = {}
summary_info += {'Build tests': build_tests}
summary_info += {'Build host-libc tests': build_tests_host_libc}
summary(summary_info, bool_yn: true, section: 'tests')

summary_info = {}
summary_info += {'headers-only': headers_only}
summary_info += {'ANSI option': not disable_ansi_option}
summary_info += {'crypt option': not disable_crypt_option}
summary_info += {'POSIX option': not disable_posix_option}
summary_info += {'Linux option': not disable_linux_option}
summary_info += {'iconv option': not disable_iconv_option}
summary_info += {'intl option': not disable_intl_option}
summary_info += {'glibc option': not disable_glibc_option}
summary_info += {'BSD option': not disable_bsd_option}
summary_info += {'debug allocator': get_option('debug_allocator')}
summary_info += {'libgcc dependency': not disable_libgcc_dependency}
summary(summary_info, bool_yn: true, section: 'mlibc options')

if build_tests
	subdir('tests/')
endif

hdoc = find_program('hdoc', required: false)

conf_data = configuration_data()
conf_data.set('source_root', meson.source_root())
conf_data.set('build_root', meson.build_root())
configure_file(input: 'scripts/hdoc.toml.in',
	output: '.hdoc.toml', configuration: conf_data)

if hdoc.found()
	run_target('hdoc', command : [hdoc.full_path(), '--verbose'])
endif