1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7inherit toolchain-scripts-base siteinfo kernel-arch
8
9# We want to be able to change the value of MULTIMACH_TARGET_SYS, because it
10# doesn't always match our expectations... but we default to the stock value
11REAL_MULTIMACH_TARGET_SYS ?= "${MULTIMACH_TARGET_SYS}"
12TARGET_CC_ARCH:append:libc-musl = " -mmusl"
13
14# default debug prefix map isn't valid in the SDK
15DEBUG_PREFIX_MAP = ""
16
17EXPORT_SDK_PS1 = "${@ 'export PS1=\\"%s\\"' % d.getVar('SDK_PS1') if d.getVar('SDK_PS1') else ''}"
18
19# This function creates an environment-setup-script for use in a deployable SDK
20toolchain_create_sdk_env_script () {
21	# Create environment setup script.  Remember that $SDKTARGETSYSROOT should
22	# only be expanded on the target at runtime.
23	base_sbindir=${10:-${base_sbindir_nativesdk}}
24	base_bindir=${9:-${base_bindir_nativesdk}}
25	sbindir=${8:-${sbindir_nativesdk}}
26	sdkpathnative=${7:-${SDKPATHNATIVE}}
27	prefix=${6:-${prefix_nativesdk}}
28	bindir=${5:-${bindir_nativesdk}}
29	libdir=${4:-${libdir}}
30	sysroot=${3:-${SDKTARGETSYSROOT}}
31	multimach_target_sys=${2:-${REAL_MULTIMACH_TARGET_SYS}}
32	script=${1:-${SDK_OUTPUT}/${SDKPATH}/environment-setup-$multimach_target_sys}
33	rm -f $script
34	touch $script
35
36	echo '# Check for LD_LIBRARY_PATH being set, which can break SDK and generally is a bad practice' >> $script
37	echo '# http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80' >> $script
38	echo '# http://xahlee.info/UnixResource_dir/_/ldpath.html' >> $script
39	echo '# Only disable this check if you are absolutely know what you are doing!' >> $script
40	echo 'if [ ! -z "${LD_LIBRARY_PATH:-}" ]; then' >> $script
41	echo "    echo \"Your environment is misconfigured, you probably need to 'unset LD_LIBRARY_PATH'\"" >> $script
42	echo "    echo \"but please check why this was set in the first place and that it's safe to unset.\"" >> $script
43	echo '    echo "The SDK will not operate correctly in most cases when LD_LIBRARY_PATH is set."' >> $script
44	echo '    echo "For more references see:"' >> $script
45	echo '    echo "  http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80"' >> $script
46	echo '    echo "  http://xahlee.info/UnixResource_dir/_/ldpath.html"' >> $script
47	echo '    return 1' >> $script
48	echo 'fi' >> $script
49
50	echo "${EXPORT_SDK_PS1}" >> $script
51	echo 'export SDKTARGETSYSROOT='"$sysroot" >> $script
52	EXTRAPATH=""
53	for i in ${CANADIANEXTRAOS}; do
54		EXTRAPATH="$EXTRAPATH:$sdkpathnative$bindir/${TARGET_ARCH}${TARGET_VENDOR}-$i"
55	done
56	echo "export PATH=$sdkpathnative$bindir:$sdkpathnative$sbindir:$sdkpathnative$base_bindir:$sdkpathnative$base_sbindir:$sdkpathnative$bindir/../${HOST_SYS}/bin:$sdkpathnative$bindir/${TARGET_SYS}"$EXTRAPATH':"$PATH"' >> $script
57	echo 'export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT' >> $script
58	echo 'export PKG_CONFIG_PATH=$SDKTARGETSYSROOT'"$libdir"'/pkgconfig:$SDKTARGETSYSROOT'"$prefix"'/share/pkgconfig' >> $script
59	echo 'export CONFIG_SITE=${SDKPATH}/site-config-'"${multimach_target_sys}" >> $script
60	echo "export OECORE_NATIVE_SYSROOT=\"$sdkpathnative\"" >> $script
61	echo 'export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"' >> $script
62	echo "export OECORE_ACLOCAL_OPTS=\"-I $sdkpathnative/usr/share/aclocal\"" >> $script
63	echo 'export OECORE_BASELIB="${baselib}"' >> $script
64	echo 'export OECORE_TARGET_ARCH="${TARGET_ARCH}"' >>$script
65	echo 'export OECORE_TARGET_OS="${TARGET_OS}"' >>$script
66
67	echo 'unset command_not_found_handle' >> $script
68
69	toolchain_shared_env_script
70}
71
72# This function creates an environment-setup-script in B which enables
73# a OE-core IDE to integrate with the build tree
74# Caller must ensure CONFIG_SITE is setup
75toolchain_create_tree_env_script () {
76	script=${B}/environment-setup-${REAL_MULTIMACH_TARGET_SYS}
77	rm -f $script
78	touch $script
79	echo 'standalone_sysroot_target="${STAGING_DIR}/${MACHINE}"' >> $script
80	echo 'standalone_sysroot_native="${STAGING_DIR}/${BUILD_ARCH}"' >> $script
81	echo 'orig=`pwd`; cd ${COREBASE}; . ./oe-init-build-env ${TOPDIR}; cd $orig' >> $script
82	echo 'export PATH=$standalone_sysroot_native/${bindir_native}:$standalone_sysroot_native/${bindir_native}/${TARGET_SYS}:$PATH' >> $script
83	echo 'export PKG_CONFIG_SYSROOT_DIR=$standalone_sysroot_target' >> $script
84	echo 'export PKG_CONFIG_PATH=$standalone_sysroot_target'"$libdir"'/pkgconfig:$standalone_sysroot_target'"$prefix"'/share/pkgconfig' >> $script
85	echo 'export CONFIG_SITE="${CONFIG_SITE}"' >> $script
86	echo 'export SDKTARGETSYSROOT=$standalone_sysroot_target' >> $script
87	echo 'export OECORE_NATIVE_SYSROOT=$standalone_sysroot_native' >> $script
88	echo 'export OECORE_TARGET_SYSROOT=$standalone_sysroot_target' >> $script
89	echo 'export OECORE_ACLOCAL_OPTS="-I $standalone_sysroot_native/usr/share/aclocal"' >> $script
90	echo 'export OECORE_BASELIB="${baselib}"' >> $script
91	echo 'export OECORE_TARGET_ARCH="${TARGET_ARCH}"' >>$script
92	echo 'export OECORE_TARGET_OS="${TARGET_OS}"' >>$script
93
94	toolchain_shared_env_script
95
96	cat >> $script <<EOF
97
98if [ -d "\$OECORE_NATIVE_SYSROOT/${datadir}/post-relocate-setup.d/" ]; then
99	for s in \$OECORE_NATIVE_SYSROOT/${datadir}/post-relocate-setup.d/*; do
100		if [ ! -x \$s ]; then
101			continue
102		fi
103		\$s "\$1"
104		status=\$?
105		if [ \$status != 0 ]; then
106			echo "post-relocate command \"\$s \$1\" failed with status \$status" >&2
107			exit \$status
108		fi
109	done
110fi
111EOF
112}
113
114toolchain_shared_env_script () {
115	echo 'export CC="${TARGET_PREFIX}gcc ${TARGET_CC_ARCH} --sysroot=$SDKTARGETSYSROOT"' >> $script
116	echo 'export CXX="${TARGET_PREFIX}g++ ${TARGET_CC_ARCH} --sysroot=$SDKTARGETSYSROOT"' >> $script
117	echo 'export CPP="${TARGET_PREFIX}gcc -E ${TARGET_CC_ARCH} --sysroot=$SDKTARGETSYSROOT"' >> $script
118	echo 'export AS="${TARGET_PREFIX}as ${TARGET_AS_ARCH}"' >> $script
119	echo 'export LD="${TARGET_PREFIX}ld ${TARGET_LD_ARCH} --sysroot=$SDKTARGETSYSROOT"' >> $script
120	echo 'export GDB=${TARGET_PREFIX}gdb' >> $script
121	echo 'export STRIP=${TARGET_PREFIX}strip' >> $script
122	echo 'export RANLIB=${TARGET_PREFIX}ranlib' >> $script
123	echo 'export OBJCOPY=${TARGET_PREFIX}objcopy' >> $script
124	echo 'export OBJDUMP=${TARGET_PREFIX}objdump' >> $script
125	echo 'export READELF=${TARGET_PREFIX}readelf' >> $script
126	echo 'export AR=${TARGET_PREFIX}ar' >> $script
127	echo 'export NM=${TARGET_PREFIX}nm' >> $script
128	echo 'export M4=m4' >> $script
129	echo 'export TARGET_PREFIX=${TARGET_PREFIX}' >> $script
130	echo 'export CONFIGURE_FLAGS="--target=${TARGET_SYS} --host=${TARGET_SYS} --build=${SDK_ARCH}-linux --with-libtool-sysroot=$SDKTARGETSYSROOT"' >> $script
131	echo 'export CFLAGS="${TARGET_CFLAGS}"' >> $script
132	echo 'export CXXFLAGS="${TARGET_CXXFLAGS}"' >> $script
133	echo 'export LDFLAGS="${TARGET_LDFLAGS}"' >> $script
134	echo 'export CPPFLAGS="${TARGET_CPPFLAGS}"' >> $script
135	echo 'export KCFLAGS="--sysroot=$SDKTARGETSYSROOT"' >> $script
136	echo 'export OECORE_DISTRO_VERSION="${DISTRO_VERSION}"' >> $script
137	echo 'export OECORE_SDK_VERSION="${SDK_VERSION}"' >> $script
138	echo 'export ARCH=${ARCH}' >> $script
139	echo 'export CROSS_COMPILE=${TARGET_PREFIX}' >> $script
140	echo 'export OECORE_TUNE_CCARGS="${TUNE_CCARGS}"' >> $script
141
142    cat >> $script <<EOF
143
144# Append environment subscripts
145if [ -d "\$OECORE_TARGET_SYSROOT/environment-setup.d" ]; then
146    for envfile in \$OECORE_TARGET_SYSROOT/environment-setup.d/*.sh; do
147	    . \$envfile
148    done
149fi
150if [ -d "\$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
151    for envfile in \$OECORE_NATIVE_SYSROOT/environment-setup.d/*.sh; do
152	    . \$envfile
153    done
154fi
155EOF
156}
157
158toolchain_create_post_relocate_script() {
159	relocate_script=$1
160	env_dir=$2
161	rm -f $relocate_script
162	touch $relocate_script
163
164	cat >> $relocate_script <<EOF
165if [ -d "${SDKPATHNATIVE}/post-relocate-setup.d/" ]; then
166	# Source top-level SDK env scripts in case they are needed for the relocate
167	# scripts.
168	for env_setup_script in ${env_dir}/environment-setup-*; do
169		. \$env_setup_script
170		status=\$?
171		if [ \$status != 0 ]; then
172			echo "\$0: Failed to source \$env_setup_script with status \$status"
173			exit \$status
174		fi
175
176		for s in ${SDKPATHNATIVE}/post-relocate-setup.d/*; do
177			if [ ! -x \$s ]; then
178				continue
179			fi
180			\$s "\$1"
181			status=\$?
182			if [ \$status != 0 ]; then
183				echo "post-relocate command \"\$s \$1\" failed with status \$status" >&2
184				exit \$status
185			fi
186		done
187	done
188	rm -rf "${SDKPATHNATIVE}/post-relocate-setup.d"
189fi
190EOF
191}
192
193#we get the cached site config in the runtime
194TOOLCHAIN_CONFIGSITE_NOCACHE = "${@' '.join(siteinfo_get_files(d)[0])}"
195TOOLCHAIN_CONFIGSITE_SYSROOTCACHE = "${STAGING_DIR}/${MLPREFIX}${MACHINE}/${target_datadir}/${TARGET_SYS}_config_site.d"
196TOOLCHAIN_NEED_CONFIGSITE_CACHE ??= "virtual/${MLPREFIX}libc ncurses"
197DEPENDS += "${TOOLCHAIN_NEED_CONFIGSITE_CACHE}"
198
199#This function create a site config file
200toolchain_create_sdk_siteconfig () {
201	local siteconfig=$1
202
203	rm -f $siteconfig
204	touch $siteconfig
205
206	for sitefile in ${TOOLCHAIN_CONFIGSITE_NOCACHE} ; do
207		cat $sitefile >> $siteconfig
208	done
209
210	#get cached site config
211	for sitefile in ${TOOLCHAIN_NEED_CONFIGSITE_CACHE}; do
212		# Resolve virtual/* names to the real recipe name using sysroot-providers info
213		case $sitefile in virtual/*)
214			sitefile=`echo $sitefile | tr / _`
215			sitefile=`cat ${STAGING_DIR_TARGET}/sysroot-providers/$sitefile`
216		esac
217
218		if [ -r ${TOOLCHAIN_CONFIGSITE_SYSROOTCACHE}/${sitefile}_config ]; then
219			cat ${TOOLCHAIN_CONFIGSITE_SYSROOTCACHE}/${sitefile}_config >> $siteconfig
220		fi
221	done
222}
223# The immediate expansion above can result in unwanted path dependencies here
224toolchain_create_sdk_siteconfig[vardepsexclude] = "TOOLCHAIN_CONFIGSITE_SYSROOTCACHE"
225
226python __anonymous () {
227    import oe.classextend
228    deps = ""
229    for dep in (d.getVar('TOOLCHAIN_NEED_CONFIGSITE_CACHE') or "").split():
230        deps += " %s:do_populate_sysroot" % dep
231        for variant in (d.getVar('MULTILIB_VARIANTS') or "").split():
232            clsextend = oe.classextend.ClassExtender(variant, d)
233            newdep = clsextend.extend_name(dep)
234            deps += " %s:do_populate_sysroot" % newdep
235    d.appendVarFlag('do_configure', 'depends', deps)
236}
237