After multiple times compiling drivers on multiple environments, specifically OpenWrt, I'd decided to see if I could compile the driver of my wifi adapter, rtl8821cu, for my Raspberry Pi 3 from my laptop. Raspberry Pi ran on DietPi, which is based on Debian.
Compile on Raspberry Pi
First, I installed raspberrypi-kernel-header, which is a package containing the kernel source of Raspberry Pi. Afterwards, I ran this command to compile the driver, it worked:
make -j4
I tried out this trick I learned from r8152 source compiling, but it failed:
make -C <kernel source> M=$(pwd) -j$(nproc)
It didn't work when I added cross compiling parameters, either:
make ARCH=arm64 CROSS_COMPILE= -C <kernel source> M=$(pwd) -j$(nproc)
Compile on x86
I downloaded the Raspberry Pi Kernel Source Code from Github and installed the package gcc-aarch64-linux-gnu. I used the following line but that didn't work:
make -C <kernel source> M=$(pwd) -j$(nproc)
So is the next one:
make ARCH=arm64 CROSS_COMPILE= -C <kernel source> M=$(pwd) -j$(nproc)
After reading Makefile in the rtl8821cu folder and found a line at the modules part:
$(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KSRC) M=$(shell pwd) modules
I rechecked my command line. While ARCH, CROSS_COMPILE I filled in correctly, I should be filling in KSRC=<kernel source> instead of -C <kernel source>. It worked afterwards:
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- KSRC=~/Documents/Programming/linux-rpi-6.1.y/ -j$(nproc)
However, the output told me to go to the kernel source and run make oldconfig && make prepare. So I did:
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- oldconfig && make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- prepare
It asked a ton of questions though.
About to finish, it came out with this error.
I looked into line 2501 of Makefile of the driver, it was the module part, so not the place yet. Line 1929 of Makefile of the kernel source showed more promise.
I looked into Makefile.modfinal. It's like the file and the error are both looking for module.lds but it's not available in the kernel source. Temporarily, I copied module.lds.S to module.lds. It did compile, but it didn't work on Raspberry Pi, likely because of kernel 6.1.21 currently on Pi, compared to kernel source 6.1.35.
What happened if you missed ARCH and CROSS_COMPILE?
Missing ARCH came out with this error.
Missing CROSS_COMPILE came out with this error.
So I guess for now I just compile the driver against the source on the device then. Until I get the hang of cross-compiling.
References: