Blob Blame History Raw
# 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"

	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 "$lz_default_underscore"__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"
		musl_cflags_shared="$musl_cflags_shared -DSHARED"
	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 $lz_project_dir/arch/$lz_arch/bits $lz_build_dir/include

	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 $musl_cflags_no_complex"
	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_cflags_no_complex="-DLIBC_NO_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
}