summaryrefslogtreecommitdiffhomepage
path: root/musl.lzy
blob: b3f0f4e8c45cc3d489204970f750a258f25da48f (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
# lazy mark: project recipe
lz_project_rules()
{
	lz_rules="all install install_no_complex \
		libc libc_shared libc_static \
		libc_no_complex libc_shared_no_complex libc_static_no_complex"
}


lz_project_definitions()
{
	lz_cflags_common="-std=c99 -pipe -ffreestanding -nostdinc -D_XOPEN_SOURCE=700 -fomit-frame-pointer"

	lz_cflags_include_common="-I$lz_project_dir/src/internal \
				-I$lz_project_dir/arch/$lz_arch \
				-I$lz_project_dir/include \
				-I$lz_build_dir/arch/$lz_arch \
				-I$lz_build_dir/include"

	musl_empty_lib_names="m rt pthread crypt util xnet resolv dl"
	musl_cflags_crt="-fPIC"
	musl_cflags_shared="-fPIC -DSHARED"

	if [ "$lz_pecoff_winnt"x = yesx ]; then
		pe_excluded_symbols="__libc_entry_point"
		pe_excluded_symbols="$pe_excluded_symbols,__syscall_vtbl"
		pe_excluded_symbols="$pe_excluded_symbols,__ldso_vtbl"
		pe_excluded_symbols="$pe_excluded_symbols,__psx_vtbl"
		pe_excluded_symbols="$pe_excluded_symbols,__teb_sys_idx"
		pe_excluded_symbols="$pe_excluded_symbols,__teb_libc_idx"
		pe_excluded_symbols="$pe_excluded_symbols,__vm_lock_impl"
		pe_excluded_symbols="$pe_excluded_symbols,__vm_unlock_impl"

		musl_libc_so_ldflags="-shared -e __libc_entry_point -Bsymbolic-functions \
					--subsystem windows \
					--output-def $lz_build_dir/lib/libc.so.def \
					--out-implib $lz_build_dir/lib/libc.lib.a \
					--exclude-symbols=$pe_excluded_symbols"
	else
		musl_libc_so_ldflags="-shared -e _start -Bsymbolic-functions"
	fi

	musl_libc_src_dirs="src arch/$lz_arch/src arch/$lz_arch/weak"
	musl_libc_mem_files="src/string/memcpy.c src/string/memmove.c src/string/memcmp.c src/string/memset.c"


	lz_flag_tests_begin

	for musl_test_flag in "-frounding-math" "-fexcess-precision=standard"; do
		lz_compiler_supports_flag $lz_c_compiler "$musl_test_flag" \
			&& lz_cflags_common="$lz_cflags_common $musl_test_flag"
	done

	for musl_test_flag in "-fno-tree-loop-distribute-patterns"; do
		lz_compiler_supports_flag $lz_c_compiler "$musl_test_flag" \
			&& musl_cflags_memops="$musl_cflags_memops $musl_test_flag"
	done

	for musl_test_flag in "--hash-style=both"; do
		lz_linker_supports_flag $lz_c_compiler $lz_ld "$musl_test_flag" \
			&& musl_libc_so_ldflags="$musl_libc_so_ldflags $musl_test_flag"
	done

	lz_flag_tests_end


	if [ "$lz_debug"x = yesx ]; then
		lz_cflags_debug="$lz_toolchain_cflags_debug"
	else
		lz_cflags_common="$lz_cflags_common -Os"
	fi

	# names of binary files and/or utilities
	musl_libc_a_name="$lz_build_dir/lib/libc$lz_stlib_ext"
	musl_libc_so_name="$lz_build_dir/lib/libc$lz_dylib_ext"

	# temporary files to store meta information
	musl_libc_so_obj_list=libc.so.objs
	musl_libc_so_src_list=libc.so.src.lst

	musl_libc_a_obj_list=libc.a.objs
	musl_libc_a_src_list=libc.a.src.lst

	musl_crt_obj_list=crt.objs
	musl_crt_src_list=crt.src.lst

	musl_install_sh=$lz_project_dir/tools/install.sh
	musl_ldso="$lz_syslibdir/ld-musl-$lz_arch$lz_subarch.so.1"
}


musl_headers()
{
	if [ -f $lz_build_dir/headers.tag ]; then
		lz_ok && return
	fi

	mkdir -p $lz_build_dir/include
	cp -r -t $lz_build_dir/include $lz_project_dir/arch/$lz_arch/bits

	touch $lz_build_dir/headers.tag
}


musl_alltypes()
{
	if [ -f $lz_build_dir/alltypes.tag ]; then
		lz_ok && return
	fi

	sed -f $lz_project_dir/tools/mkalltypes.sed \
		$lz_build_dir/include/bits/alltypes.h.in \
		$lz_project_dir/include/alltypes.h.in > \
			$lz_build_dir/include/bits/alltypes.h

	touch $lz_build_dir/alltypes.tag
}


musl_version()
{
	if [ -f $lz_build_dir/version.tag ]; then
		lz_ok && return
	fi

	lz_pushd $lz_project_dir
	lzp_version=`sh tools/version.sh`
	lz_popd

	printf '#define VERSION "%s"\n' $lzp_version \
		> $lz_build_dir/include/version.h

	touch $lz_build_dir/version.tag
}


musl_empty_libs()
{
	if [ -f $lz_build_dir/empty_libs.tag ]; then
		lz_ok && return
	fi

	for lzp_lib_name in $musl_empty_lib_names; do
		lzp_lib_file_name=$lz_build_dir/lib/lib$lzp_lib_name.a
		rm -f $lzp_lib_file_name
		$lz_ar rc $lzp_lib_file_name
	done

	touch $lz_build_dir/empty_libs.tag
}

musl_crt()
{
	musl_headers
	musl_alltypes

	lz_cflags_extra="$musl_cflags_crt"
	lz_src_dirs="crt"

        lz_compile "$musl_crt_obj_list" "$musl_crt_src_list" "$lz_stobj_ext"
}


musl_libc_common()
{
	musl_headers
	musl_alltypes
	musl_version
}


musl_libc_shared()
{
        musl_libc_common
	musl_crt

	# memory functions
	lz_cflags_extra="$musl_cflags_shared"
	lz_cflags_special="$musl_cflags_memops"
	lz_src_files="$musl_libc_mem_files"
	lz_src_dirs=$lz_ignore_src_dirs

        lz_compile "$musl_libc_so_obj_list" "$musl_libc_so_src_list" "$lz_dyobj_ext"

	lz_src_files=
	lz_cflags_special=

	# regular source files
	lz_cumulative_obj_list=yes
	lz_src_dirs=$musl_libc_src_dirs
	lz_exclude_src_files="$musl_libc_mem_files"
	lz_compiler_rt_libs="$lz_compiler_rt_libs $lz_link_libgcc"

	if [ "$lz_pecoff_winnt"x = yesx ]; then
		musl_crti_objs="musl_crti_objs.lst"
		musl_crtn_objs="musl_crtn_objs.lst"
		echo 'crt/crti.o' > $musl_crti_objs
		echo 'crt/crtn.o' > $musl_crtn_objs
	else
		musl_crti_objs=
		musl_crtn_objs=
	fi

	rm -f "$musl_libc_so_name"

        lz_compile "$musl_libc_so_obj_list" "$musl_libc_so_src_list" "$lz_dyobj_ext"
        lz_link    "$musl_crti_objs $musl_libc_so_obj_list $musl_crtn_objs" \
			"$musl_libc_so_src_list" \
			"$musl_libc_so_name" \
			"$musl_libc_so_ldflags" \
			"$lz_compiler_rt_libs"   \
			"$lz_arch_dynamic_syscall_libs"
}


musl_libc_static()
{
        musl_libc_common

	# memory functions
	lz_cflags_special="$musl_cflags_memops"
	lz_src_files="$musl_libc_mem_files"
	lz_src_dirs=$lz_ignore_src_dirs

        lz_compile "$musl_libc_a_obj_list" "$musl_libc_a_src_list" "$lz_stobj_ext"
	lz_src_files=
	lz_cflags_special=

	# regular source files
	lz_cumulative_obj_list=yes
	lz_src_dirs=$musl_libc_src_dirs
	lz_exclude_src_files="$musl_libc_mem_files"

	rm -f "$musl_libc_a_name"

        lz_compile "$musl_libc_a_obj_list" "$musl_libc_a_src_list" "$lz_stobj_ext"
        lz_archive "$musl_libc_a_obj_list" "$musl_libc_a_src_list" "$musl_libc_a_name"

	if [ "$lz_pecoff_winnt"x = yesx ]; then
		if [ "$lz_arch_dynamic_syscall_libs"x != x ]; then
			$lz_ar rc "$musl_libc_a_name" "$lz_arch_static_syscall_libs"
		fi
	fi
}


musl_libc_shared_no_complex()
{
	lz_link_libgcc=
	lz_exclude_src_dirs="src/complex"
	musl_libc_shared
}


musl_libc_static_no_complex()
{
	lz_exclude_src_dirs="src/complex"
	musl_libc_static
}


musl_libc()
{
	lz_link_libgcc="--as-needed `$lz_c_compiler -print-file-name=libgcc.a`"
	lz_step musl_crt
	lz_step musl_libc_shared
	lz_step musl_libc_static
	lz_step musl_empty_libs
}


musl_libc_no_complex()
{
	lz_step musl_crt
	lz_step musl_libc_shared_no_complex
	lz_step musl_libc_static_no_complex
	lz_step musl_empty_libs
}


musl_all()
{
	lz_step musl_libc
}


musl_install_crt()
{
	lz_pushd $lz_build_dir/crt

	musl_crt_files=`cat $lz_build_dir/crt.objs`

	for lzp_crt_file in $musl_crt_files; do
		lzp_crt_obj_file=`basename $lzp_crt_file`
		$musl_install_sh -D $lzp_crt_obj_file $lz_libdir/$lzp_crt_obj_file
	done

	lz_popd
}


musl_install_libs()
{
	lz_pushd $lz_build_dir/lib

	for lzp_lib_file in "c" $musl_empty_lib_names; do
		$musl_install_sh -D -m 644 lib$lzp_lib_file$lz_stlib_ext $lz_libdir/lib$lzp_lib_file$lz_stlib_ext
	done

	$musl_install_sh -D -m 755 libc$lz_dylib_ext $lz_libdir/libc$lz_dylib_ext

	if [ "$lz_pecoff_winnt"x = yesx ]; then
		# also install the import library
		$musl_install_sh -D -m 644 libc$lz_implib_ext  $lz_libdir/libc$lz_implib_ext
	fi

	lz_popd
}


musl_install_ldso()
{
	$musl_install_sh -D -l $lz_libdir/libc$lz_dylib_ext $musl_ldso
	$musl_install_sh -D -l $lz_libdir/libc$lz_dylib_ext $lz_bindir/ldd
}


musl_install_headers()
{
	lz_pushd $lz_project_dir/include

	musl_include_files=`find . -type f`

	for lzp_include_file in $musl_include_files; do
		$musl_install_sh -D -m 644 $lzp_include_file $lz_includedir/$lzp_include_file
	done

	lz_popd
}


musl_install_arch_headers()
{
	lz_pushd $lz_build_dir/include

	musl_include_files=`find ./bits -type f ! -name *.h.in`

	for lzp_include_file in $musl_include_files; do
		$musl_install_sh -D -m 644 $lzp_include_file $lz_includedir/$lzp_include_file
	done

	lz_popd
}


musl_install_specs()
{
	sh $lz_project_dir/tools/musl-gcc.specs.sh \
			$(readlink -f "$lz_includedir") \
			$(readlink -f "$lz_libdir") \
			$(readlink -f "$musl_ldso") \
		> $lz_libdir/musl-gcc.specs
}


musl_install_common()
{
	lz_step musl_install_crt
	lz_step musl_install_libs
	lz_step musl_install_ldso
	lz_step musl_install_headers
	lz_step musl_install_arch_headers
	lz_step musl_install_specs
}


musl_install()
{
	lz_step musl_all
	lz_step musl_install_common
}


musl_install_no_complex()
{
	lz_step musl_libc_no_complex
	lz_step musl_install_common
}