Ubuntu 22.04 - GCC 12 Kernel Driver Build Issue

Problem

I just tried to build a fresh clone of the pre release version of v2.4.7 on Ubuntu 22.04 and got the following error during the linux kernel driver build:

dawalters:/media/opencpi/opencpi$ make driver
Building the linux kernel driver for ubuntu22_04
make[1]: Entering directory '/media/opencpi/opencpi/os/linux/driver'
Building/cleaning driver in a CDK environment for platform: ubuntu22_04
OCPI_TARGET_KERNEL_DIR was specified, we are now using: KERNELDIR=/usr/src/linux-headers-6.5.0-14-generic ARCH= CROSS_COMPILE=
Building kernel module target-ubuntu22_04/opencpi.ko
make[2]: Entering directory '/usr/src/linux-headers-6.5.0-14-generic'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
  You are using:
Building/cleaning driver in a CDK environment for platform: ubuntu22_04
  CC [M]  /media/opencpi/opencpi/os/linux/driver/opencpi.o - due to target missing
gcc: error: unrecognized command-line option ‘-ftrivial-auto-var-init=zero’
make[4]: *** [scripts/Makefile.build:251: /media/opencpi/opencpi/os/linux/driver/opencpi.o] Error 1
make[3]: *** [/usr/src/linux-headers-6.5.0-14-generic/Makefile:2037: /media/opencpi/opencpi/os/linux/driver] Error 2
make[2]: *** [Makefile:234: __sub-make] Error 2
make[2]: Leaving directory '/usr/src/linux-headers-6.5.0-14-generic'
make[1]: *** [Makefile:175: target-ubuntu22_04/opencpi.ko] Error 2
make[1]: Leaving directory '/media/opencpi/opencpi/os/linux/driver'
make: *** [Makefile:153: driver] Error 2

Investigation

I found this on StackExchange:

Which led to a post by the same user on AskUbuntu:

Canonical have switched to build the kernel using gcc-12 instead of gcc-11 which is the default gcc on Ubuntu 22.04.

This switch appears to then cause the build process for the OpenCPI linux driver to try to use flags for gcc-12, which causes the crash.

Current Resolution

I don’t know if this is the best solution but it worked for me:

# Install `gcc-12`
sudo apt-get install gcc-12

# Switch the `gcc` symlink to point to `gcc-12`
ln -frs /usr/bin/gcc-12 /usr/bin/gcc

The ubuntu22_04-packages.sh script could be modified to do this automatically, but changing the default gcc provider could cause issues with other build environments that are coresident on the same machine.

I ran into the same issue yesterday. Instead of the symlink I added OcpiCC=gcc-12 in the projects/core/rcc/platforms/ubuntu22_04/ubuntu22_04.mk . Then I was able to build the OpenCPI linux driver and get through the install.

I will update and add this line shortly.

I was testing Ubuntu 22.04 and Vivado 2023.2 and everything built correctly.

Yeah, that sounds like a better solution than making the symlink.

The fix for this issue has been merged into develop branch: fix ubuntu 22.04 kernel driver build failure gcc-12 (!1154) · Merge requests · OpenCPI / OpenCPI · GitLab