mill1000 / raspdif

Raspberry Pi S/PDIF digital audio without a HAT.
MIT License
68 stars 7 forks source link

Compile on Pi OS Bookworm #24

Closed chris-hld closed 10 months ago

chris-hld commented 11 months ago

Thank you for this amazing project!

I had some trouble getting it compiled on the latest raspberry pi OS (Debian bookworm) - on a pi model 3B, 64 bit.

First, clang seems crashing with

clang: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Debian clang version 14.0.6

Then I also noticed that at least my os didn't come with /opt/vc/lib. After installing, I tried switching to gcc and got a

 source/main.c: In function ‘signal_handler’:
source/main.c:387:41: error: ‘sys_siglist’ undeclared (first use in this function)
  387 |   LOGW(TAG, "Received signal %s (%d).", sys_siglist[signal], signal);

I replaced the deprecated latter with

-  LOGW(TAG, "Received signal %s (%d).", sys_siglist[signal], signal);
-
+  LOGW(TAG, "Received signal %s (%d).", strsignal(signal), signal);

GCC compiles, but wouldn't link

gcc -L /opt/vc/lib -lbcm_host -lm -lvcos -lpthread -lstdc++ build/source/memory.c.o build/source/bcm283x/bcm283x_pcm.c.o build/source/bcm283x/bcm283x_dma.c.o build/source/bcm283x/bcm283x.c.o build/source/bcm283x/bcm283x_gpio.c.o build/source/bcm283x/bcm283x_clock.c.o build/source/log.c.o build/source/mailbox.c.o build/source/main.c.o build/source/spdif.c.o -o build/raspdif 
/usr/bin/ld: skipping incompatible /opt/vc/lib/libbcm_host.so when searching for -lbcm_host
/usr/bin/ld: skipping incompatible /opt/vc/lib/libvcos.so when searching for -lvcos
/usr/bin/ld: build/source/bcm283x/bcm283x.c.o: in function `bcm283x_init':
bcm283x.c:(.text+0xb4): undefined reference to `bcm_host_get_peripheral_address'
/usr/bin/ld: bcm283x.c:(.text+0xc0): undefined reference to `bcm_host_get_peripheral_size'
/usr/bin/ld: build/source/main.c.o: in function `raspdif_init':
main.c:(.text+0x410): undefined reference to `bcm_host_get_sdram_address'
/usr/bin/ld: main.c:(.text+0x500): undefined reference to `bcm_host_is_model_pi4'
/usr/bin/ld: main.c:(.text+0x554): undefined reference to `round'
/usr/bin/ld: build/source/main.c.o: in function `main':
main.c:(.text+0xe6c): undefined reference to `bcm_host_is_model_pi4'
collect2: error: ld returned 1 exit status

Also installed a system libbcm_host, which doesn't complain about beein incompatible, but would still result in the same undefined references. Not sure what's going on there. However, running then clang again wouldn't crash and even link successfully. So far the binaries seem to work flawlessly, but I was wondering if it's worth having a look into this?

mill1000 commented 11 months ago

Hi there.

Thanks for the fix regarding the signals. I'll make that change.

I'm able to successfully build on 32 bit Bookworm with clang. Does your system have the libraspberrypi-dev package installed? I'll have to stand up a 64-bit system to test your setup.

GCC has the same undefined references for me on 32-bit. Not sure what that's about, I checked to see that /usr/lib/arm-linux-gnueabihf/libbcm_host.so is in it's library path, but either way GCC won't link even if it found that lib since it doesn't have the __rbit intrinsic.

chris-hld commented 11 months ago

Alright, it seems it was a bug in clang-14, which is the current default with apt install clang. Updating to clang-15 resolved the crashes, luckily available with apt install clang-15.

What remains is the linker warning

clang-15 -L /opt/vc/lib -lbcm_host -lm -lvcos -lpthread -lstdc++ build/source/memory.c.o build/source/bcm283x/bcm283x_pcm.c.o build/source/bcm283x/bcm283x_dma.c.o build/source/bcm283x/bcm283x.c.o build/source/bcm283x/bcm283x_gpio.c.o build/source/bcm283x/bcm283x_clock.c.o build/source/log.c.o build/source/mailbox.c.o build/source/main.c.o build/source/spdif.c.o -o build/raspdif 
/usr/bin/ld: skipping incompatible /opt/vc/lib/libbcm_host.so when searching for -lbcm_host
/usr/bin/ld: skipping incompatible /opt/vc/lib/libvcos.so when searching for -lvcos

But it seems to be fine.

mill1000 commented 11 months ago

There must be a compatible libbcm_host on the system somewhere otherwise linking would fail.

What is the output of find / -name "libbcm_host*" 2> /dev/null

chris-hld commented 10 months ago
/usr/lib/aarch64-linux-gnu/libbcm_host.so
/usr/lib/aarch64-linux-gnu/libbcm_host.so.0
/opt/vc/lib/libbcm_host.so

where libbcm_host.so is just a symlink to libbcm_host.so.0. Seems like the two in /opt/ and /ust/lib/ are different? I'm not sure which package delivered each, it might have been libraspberrypi-dev and I think /opt/ came from an rpi-update. It mentions that https://github.com/raspberrypi/userland no longer ship with Pi OS Bookworm. Either way, for the moment, everything seems to be working!

mill1000 commented 10 months ago

Well at least it's working. Looks like I might have to reconsider how the project interfaces with the hardware sooner than later.

You can use dpkg -S <file> to see which package provided the library.

e.g.

$ dpkg -S /usr/lib/arm-linux-gnueabihf/libbcm_host.so.0
libraspberrypi0:armhf: /usr/lib/arm-linux-gnueabihf/libbcm_host.so.0
chris-hld commented 10 months ago

Ah, nice:

libraspberrypi0:arm64: /usr/lib/aarch64-linux-gnu/libbcm_host.so.0
dpkg-query: no path found matching pattern /opt/vc/lib/libbcm_host.so

So this also seems to indicate that /opt/vc/lib/libbcm_host.so comes not from a package repository.

mill1000 commented 10 months ago

So this also seems to indicate that /opt/vc/lib/libbcm_host.so comes not from a package repository.

Possibly, or maybe the tool isn't perfect.

Anyway glad you're up and running.

chris-hld commented 10 months ago

Closing the issue, might be worth to add a small comment in the readme about compilation on recent Pi OS.