Blame project/clbuild.sh

670ee5
#!/bin/sh
670ee5
# a poor man's universal driver (but stay tuned...)
670ee5
670ee5
error_msg()
670ee5
{
670ee5
	echo $@ >&2
670ee5
}
670ee5
ca8f8a
mb_cc=
ca8f8a
mb_ar=
670ee5
mb_source_dir=
670ee5
mb_project_dir=
ca8f8a
mb_sysroot=
d1f2e0
version_header=
d1f2e0
version_macro=
ff191a
link_target=
670ee5
670ee5
# one: args
670ee5
for arg ; do
670ee5
	case "$arg" in
670ee5
		--help)	usage
670ee5
			;;
ca8f8a
		--cc=*)
ca8f8a
			mb_cc=${arg#*=}
ca8f8a
			;;
ca8f8a
		--ar=*)
ca8f8a
			mb_ar=${arg#*=}
ca8f8a
			;;
670ee5
		--source-dir=*)
670ee5
			mb_source_dir=${arg#*=}
670ee5
			;;
670ee5
		--project-dir=*)
670ee5
			mb_project_dir=${arg#*=}
670ee5
			;;
ca8f8a
		--sysroot=*)
ca8f8a
			mb_sysroot=${arg#*=}
ca8f8a
			;;
d1f2e0
		--version-header=*)
d1f2e0
			version_header=${arg#*=}
d1f2e0
			;;
d1f2e0
		--version-macro=*)
d1f2e0
			version_macro='-D'${arg#*=}'=\\\"unknown\\\"'
d1f2e0
			;;
ff191a
		--link-target=*)
ff191a
			link_target=${arg#*=}
ff191a
			;;
670ee5
		*)
670ee5
			error_msg ${arg#}: "unsupported config argument."
670ee5
			exit 2
670ee5
			;;
670ee5
	esac
670ee5
done
670ee5
ca8f8a
if [ -z "$mb_cc" ] || [ -z "$mb_ar" ]           \
ca8f8a
		   || [ -z "$mb_source_dir" ]   \
ca8f8a
		   || [ -z "$mb_project_dir" ]; then
670ee5
	usage
670ee5
fi
670ee5
670ee5
# two: clean
670ee5
make -s clean || exit 1
670ee5
670ee5
# three: init
670ee5
echo '@echo on'
670ee5
echo '@dir . > nul'
670ee5
echo
670ee5
echo
670ee5
echo 'REM override variables either here or from the shell'
670ee5
echo 'set source_dir=%source_dir%'
670ee5
echo 'set sysroot=%sysroot%'
670ee5
echo
670ee5
echo
670ee5
670ee5
# four: build tree
670ee5
cmd_abort_on_error='@if %errorlevel% neq 0 exit /b %errorlevel%'
670ee5
sysroot_include_arg='-I%sysroot%/include'
670ee5
670ee5
dirs=`make dirs.tag tree.tag                    \
670ee5
	 | grep -v 'make\[1\]:'                 \
670ee5
	 | grep -v 'touch dirs.tag'             \
670ee5
	 | grep -v 'touch tree.tag'             \
670ee5
	 | sed                                  \
670ee5
		-e 's/mkdir -p//g'              \
670ee5
		-e 's@/@\\\\@g'`
670ee5
d1f2e0
for d in build $dirs; do
670ee5
	printf 'if not exist %-20smkdir %s\n' "$d" "$d"
670ee5
done
670ee5
d1f2e0
# version.tag
d1f2e0
make version.tag > /dev/null
d1f2e0
d1f2e0
echo
d1f2e0
echo "type nul > build/$version_header"
d1f2e0
echo
d1f2e0
echo "$cmd_abort_on_error"
d1f2e0
echo
d1f2e0
670ee5
echo
670ee5
echo "$cmd_abort_on_error"
670ee5
echo
670ee5
6eec14
# five: compile assembly sources
6eec14
make shared-objs-asm static-objs-asm                            \
6eec14
	 | grep -v 'make\[1\]:'                                 \
6eec14
	 | grep -v "$mb_project_dir/sysinfo/host"               \
6eec14
	 | grep -v 'touch dirs.tag'                             \
6eec14
	 | grep -v 'touch host.tag'                             \
6eec14
	 | grep -v 'touch app.tag'                              \
6eec14
	 | sed                                                  \
6eec14
		-e 's@'"$mb_cc -c "'@ml64 /nologo @'            \
6eec14
		-e 's@ '"$mb_source_dir"'@ /c %source_dir%@'    \
6eec14
		-e 's@-o @/Fo @g'                               \
6eec14
		-e 's@\.s@\.asm@g'                              \
6eec14
		-e 's,$,\n\n'"$cmd_abort_on_error"'\n,g'
6eec14
6eec14
# six: compile C sources
670ee5
make shared-objs static-objs app-objs                           \
670ee5
	 | grep -v 'make\[1\]:'                                 \
670ee5
	 | grep -v "$mb_project_dir/sysinfo/host"               \
670ee5
	 | grep -v 'touch dirs.tag'                             \
670ee5
	 | grep -v 'touch host.tag'                             \
670ee5
	 | grep -v 'touch version.tag'                          \
670ee5
	 | grep -v 'touch app.tag'                              \
670ee5
	 | sed                                                  \
ca8f8a
		-e 's@'"$mb_cc -c "'@cl /nologo -c @'           \
670ee5
		-e 's@ '"$mb_source_dir"'@ %source_dir%@'       \
670ee5
		-e 's@-I'"$mb_source_dir"'@-I%source_dir%@g'    \
b6306b
		-e 's@-ffreestanding@/X /GS-@g'                 \
670ee5
		-e 's@-o @/Fo:@g'                               \
670ee5
		-e 's@-std=c99 @@g'                             \
670ee5
		-e 's@-fPIC@@g'                                 \
670ee5
		-e 's@-fpic@@g'                                 \
670ee5
		-e 's@-g @/Zi @g'                               \
670ee5
		-e 's@-g0 @/Zi @g'                              \
670ee5
		-e 's@-g1 @/Zi @g'                              \
670ee5
		-e 's@-g2 @/Zi @g'                              \
670ee5
		-e 's@-g3 @/Zi @g'                              \
670ee5
		-e 's@-O @/O2 @g'                               \
670ee5
		-e 's@-Os @/O1 @g'                              \
670ee5
		-e 's@-O0 @/Od @g'                              \
670ee5
		-e 's@-O1 @/O2 @g'                              \
670ee5
		-e 's@-O2 @/O2 @g'                              \
670ee5
		-e 's@-O3 @/O2 @g'                              \
670ee5
		-e 's@-Wall @/Wall @g'                          \
670ee5
		-e 's@-Werror @/WX @g'                          \
670ee5
		-e 's@-Wextra @@g'                              \
670ee5
		-e 's@-Wundef @/Wall @g'                        \
f3aa75
		-e 's@-Wno-missing-field-initializers @ @g'     \
f8831f
		-e 's@-Wno-unused-parameter @/wd4100 @g'        \
5293d4
		-e 's@-mno-stack-arg-probe @/Gs65536 @g'        \
ca8f8a
		-e 's@'"$mb_sysroot "'@ @'                      \
d1f2e0
		-e 's,$, '"$version_macro"' -I%sysroot%/include\n\n'"$cmd_abort_on_error"'\n,g'
670ee5
670ee5
echo
670ee5
echo '@echo.'
670ee5
echo '@echo clbuild: successfully built: shared-objs static-objs app-objs.'
670ee5
echo '@echo.'
670ee5
echo
670ee5
6eec14
# seven: create static archive
ca8f8a
make static                                             \
ca8f8a
	 | grep -v 'make\[1\]:'                         \
ca8f8a
	 | grep -v "rm -f "                             \
ca8f8a
	 | sed                                          \
ca8f8a
		-e 's@'"$mb_ar"' rcs @lib /out:@'       \
670ee5
		-e 's,$, /nologo\n,g'
670ee5
670ee5
echo
ca8f8a
echo "$cmd_abort_on_error"
ca8f8a
echo
ca8f8a
ca8f8a
echo
670ee5
echo '@echo.'
670ee5
echo '@echo clbuild: successfully created: static archive.'
670ee5
echo '@echo.'
670ee5
echo
670ee5
ff191a
# eight: link
ff191a
make LDFLAGS_IMPLIB= LDFLAGS_SONAME= $link_target                     \
fd7fac
	 | grep -v 'make\[1\]:'                                       \
ff191a
	 | grep -v 'rm -f app\.tag'                                   \
fd7fac
	 | sed                                                        \
fd7fac
		-e 's@'"$mb_cc"' -shared @link /dll /nologo @'        \
a3424c
		-e 's@'"$mb_cc"' -static @link /nologo @'             \
ff191a
		-e 's@'"$mb_cc"' @link /nologo @'                     \
fd7fac
		-e 's@-o @/out:@g'                                    \
fd7fac
		-e 's@ -L@ /libpath:@g'                               \
fd7fac
		-e 's,-static -l\([^ ]\+\), %sysroot%/lib/lib\1.a,g'  \
fd7fac
		-e 's, -l\([^ ]\+\), %sysroot%/lib/lib\1.lib,g'       \
fd7fac
		-e 's@-nostdlib @/nodefaultlib @g'                    \
fd7fac
		-e 's@-Wl,--subsystem=@/subsystem:@g'                 \
fd7fac
		-e 's@-Wl,--entry -Wl,'"''"'@/entry:@g'               \
fd7fac
		-e 's@-Wl,--entry -Wl,@/entry:@g'                     \
fd7fac
		-e 's@-Wl,--no-undefined@@g'                          \
fd7fac
		-e 's@-Wl,--exclude-all-symbols@@g'                   \
fd7fac
		-e 's@'"$mb_sysroot "'@ @'                            \
ca8f8a
ca8f8a
echo
ca8f8a
echo "$cmd_abort_on_error"
ca8f8a
echo
670ee5
670ee5
echo
670ee5
echo '@echo.'
670ee5
echo '@echo clbuild: successfully linked: shared library.'
670ee5
echo '@echo.'
670ee5
echo
670ee5
670ee5
echo
670ee5
echo '@echo.'
670ee5
echo '@echo yay.'
670ee5
echo '@echo.'
670ee5
echo
670ee5
670ee5
# all done
670ee5
exit 0