0

I'm trying to set up a cross-compile environment in VSCode on Ubuntu using CMake with Yocto SDK. I need to ensure that the necessary environment variables are set before running VSCode in the project folder. Currently, I set the environment variables by running the following command in the terminal:

$ source /opt/poky/3.1.2/environment-setup-XxXxXx

Without these environment variables, CMake is unable to locate the required dependencies.

I would like to know if it's possible to automate this step within VSCode, such that the necessary Yocto SDK environment variables are automatically set every time I open the project. Can this be achieved through modifications in the settings.json file or any other method?

Update#1

Script for configuring environment variables:

if [ ! -z "$LD_LIBRARY_PATH" ]; then
    echo "Your environment is misconfigured, you probably need to 'unset LD_LIBRARY_PATH'"
    echo "but please check why this was set in the first place and that it's safe to unset."
    echo "The SDK will not operate correctly in most cases when LD_LIBRARY_PATH is set."
    echo "For more references see:"
    echo "  http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html#AEN80"
    echo "  http://xahlee.info/UnixResource_dir/_/ldpath.html"
    return 1
fi

export SDKTARGETSYSROOT=/opt/poky/3.3.4/sysroots/corei7-64-poky-linux
export PATH=/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/sbin:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/bin:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/sbin:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin/../x86_64-pokysdk-linux/bin:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin/x86_64-poky-linux:/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin/x86_64-poky-linux-musl:$PATH
export PKG_CONFIG_SYSROOT_DIR=$SDKTARGETSYSROOT
export PKG_CONFIG_PATH=$SDKTARGETSYSROOT/usr/lib/pkgconfig:$SDKTARGETSYSROOT/usr/share/pkgconfig
export CONFIG_SITE=/opt/poky/3.3.4/site-config-corei7-64-poky-linux
export OECORE_NATIVE_SYSROOT="/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux"
export OECORE_TARGET_SYSROOT="$SDKTARGETSYSROOT"
export OECORE_ACLOCAL_OPTS="-I /opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/share/aclocal"
export OECORE_BASELIB="lib"
export OECORE_TARGET_ARCH="x86_64"
export OECORE_TARGET_OS="linux"
unset command_not_found_handle
export CC="x86_64-poky-linux-gcc  -m64 -march=nehalem -mtune=generic -mfpmath=sse -msse4.2 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export CXX="x86_64-poky-linux-g++  -m64 -march=nehalem -mtune=generic -mfpmath=sse -msse4.2 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export CPP="x86_64-poky-linux-gcc -E  -m64 -march=nehalem -mtune=generic -mfpmath=sse -msse4.2 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=$SDKTARGETSYSROOT"
export AS="x86_64-poky-linux-as  "
export LD="x86_64-poky-linux-ld   --sysroot=$SDKTARGETSYSROOT"
export GDB=x86_64-poky-linux-gdb
export STRIP=x86_64-poky-linux-strip
export RANLIB=x86_64-poky-linux-ranlib
export OBJCOPY=x86_64-poky-linux-objcopy
export OBJDUMP=x86_64-poky-linux-objdump
export READELF=x86_64-poky-linux-readelf
export AR=x86_64-poky-linux-ar
export NM=x86_64-poky-linux-nm
export M4=m4
export TARGET_PREFIX=x86_64-poky-linux-
export CONFIGURE_FLAGS="--target=x86_64-poky-linux --host=x86_64-poky-linux --build=x86_64-linux --with-libtool-sysroot=$SDKTARGETSYSROOT"
export CFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export CXXFLAGS=" -O2 -pipe -g -feliminate-unused-debug-types "
export LDFLAGS="-Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-z,relro,-z,now"
export CPPFLAGS=""
export KCFLAGS="--sysroot=$SDKTARGETSYSROOT"
export OECORE_DISTRO_VERSION="3.3.4"
export OECORE_SDK_VERSION="3.3.4"
export ARCH=x86
export CROSS_COMPILE=x86_64-poky-linux-

# Append environment subscripts
if [ -d "$OECORE_TARGET_SYSROOT/environment-setup.d" ]; then
    for envfile in $OECORE_TARGET_SYSROOT/environment-setup.d/*.sh; do
        . $envfile
    done
fi
if [ -d "$OECORE_NATIVE_SYSROOT/environment-setup.d" ]; then
    for envfile in $OECORE_NATIVE_SYSROOT/environment-setup.d/*.sh; do
        . $envfile
    done
fi

pmundt
  • 57
  • 9
  • related on the topic of environment variables and VS Code: https://stackoverflow.com/a/76191074/11107541. my answer there even touches on some CMake Tools settings – starball May 08 '23 at 20:11
  • does your script have any control flow? If not, you can just switch to using [CMake presets](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html), or use the CMake tools settings like `cmake.environment`, etc. If it does have control flow, how complicated is the logic? How many various outcomes can it have? – starball May 08 '23 at 20:15
  • Thank you for your answer. I'll look into the details of your previous answer in the link you shared. By the way, regarding the control flow, I've also posted my script. – pmundt May 09 '23 at 10:09

1 Answers1

0

To enable VsCode/CMake to locate the necessary dependencies, I created a cmake-kits.json file inside the .vscode folder of my project. This file serves as a configuration for CMake kits. Here is an example cmake-kits.json file:

[
  {
    "name": "Yocto-Poky: GCC 10.3.0 x86_64-poky-linux",
    "compilers": {
      "C": "/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin/x86_64-poky-linux/x86_64-poky-linux-gcc",
      "CXX": "/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/bin/x86_64-poky-linux/x86_64-poky-linux-g++"
    },
    "environmentSetupScript": "/opt/poky/3.3.4/environment-setup-corei7-64-poky-linux",
    "toolchainFile": "/opt/poky/3.3.4/sysroots/x86_64-pokysdk-linux/usr/share/cmake/OEToolchainConfig.cmake"
  }
]
pmundt
  • 57
  • 9