raspberrypi / pico-sdk

BSD 3-Clause "New" or "Revised" License
3.69k stars 917 forks source link

RP2040 (hardware/structs/accessctrl.h) build error with Pico 2.0.0 SDK #1917

Closed iCMDdev closed 1 month ago

iCMDdev commented 1 month ago

I'm not entirely sure if this is an error with Pico SDK or the CMakeLists.txt I'm using, but when I try to build, I'm getting the following error:

pico-sdk/src/rp2_common/hardware_base/include/hardware/address_mapped.h:177:10: error: 'hardware/structs/accessctrl.h' file not found
175 | #if !PICO_RP2040
176 | // include this here to avoid the check in every other hardware/structs header that needs it
177 | #include "hardware/structs/accessctrl.h"
    |          `- error: 'hardware/structs/accessctrl.h' file not found
178 | #endif
179 | 

The hardware/structs/accessctrl.h file is indirectly included by pico/stdlib.h.

Clearly, something's off: it seems that even though I'm compiling for RP2040 (the PICO_BOARD environment variable is set accordingly), the #if !PICO_RP2040 fails. This does not occur in previous Pico SDK versions.

I figured this is somehow related to the add_custom_command and / or add_dependencies steps. In one case, removing the add_dependencies fixed this issue, but that's just a workaround, not a fix (if the dependency takes more time to compile, this will fail).

~To further debug this, I tried to search for the definition of PICO_RP2040, but I couldn't find it yet.~

Edit: I assume this is wherePICO_RP2040 is set (as a CMake variable, not a preprocessor macro though). Maybe this is related to a specific target_compile_definitions.

kilograham commented 1 month ago
  1. are you using a fresh build directory? - probably, but worth checking
  2. where is this error coming from? - the SDK compile, or the swift bit
  3. can you print the compile command if it is SDK
lurch commented 1 month ago

Edit: I assume this is wherePICO_RP2040 is set (as a CMake variable, not a preprocessor macro though). Maybe this is related to a specific target_compile_definitions.

Just for the sake of completion, here is where it gets set as a preprocessor macro.

iCMDdev commented 1 month ago
  1. are you using a fresh build directory? - probably, but worth checking

Great question. Yes.

  1. where is this error coming from? - the SDK compile, or the swift bit

The SDK throws the error I mentioned, but the custom Swift parts added to the CMakeLists.txt seem to trigger it somehow... I'm not sure if it's a problem with the Swift compilation or withtin the SDK, that's the thing I'm trying to figure out.

I've tried to replicate this in a C-only blink project using my own add_custom_command:

cmake_minimum_required(VERSION 3.13)
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

project(blink)

pico_sdk_init()

add_executable(blink)
add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
    COMMAND
        arm-none-eabi-gcc
        -c ${CMAKE_CURRENT_LIST_DIR}/blink_simple.c
        -o ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o
    DEPENDS
        ${CMAKE_CURRENT_LIST_DIR}/blink_simple.c
)
add_custom_target(swift-blinky-swiftcode DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)

# pull in common dependencies
target_link_libraries(blink ${CMAKE_CURRENT_BINARY_DIR}/_swiftcode.o)

add_dependencies(blink swift-blinky-swiftcode)

# create map/bin/hex file etc.
pico_add_extra_outputs(blink)

But this time, it works... So maybe this is related to Swift?

By the way, if I manually define #define PICO_RP2040 1 inside the hardware/structs/accessctrl.h file, the build succeeds. The build also works fine for RP2350.

  1. can you print the compile command if it is SDK

Sure, it's the usual cmake compile commands:

$ cmake -G Ninja -B build .
$ cmake --build build

I also tried with make instead of Ninja, and the same error occurs.

iCMDdev commented 1 month ago

Edit: I assume this is wherePICO_RP2040 is set (as a CMake variable, not a preprocessor macro though). Maybe this is related to a specific target_compile_definitions.

Just for the sake of completion, here is where it gets set as a preprocessor macro.

Thanks!

I've added a manual message(STATUS "here") in this file, and it doesn't show up when I run the commands above... šŸ¤”

kilograham commented 1 month ago

Sure, it's the usual cmake compile commands:

$ cmake -G Ninja -B build . $ cmake --build build

I also tried with make instead of Ninja, and the same error occurs.

Can you do:

$ cmake -G "Unix Makefiles" -B build .
$ cd build
$ make VERBOSE=1

and check the compile definitions

iCMDdev commented 1 month ago

Sure, it's the usual cmake compile commands: $ cmake -G Ninja -B build . $ cmake --build build I also tried with make instead of Ninja, and the same error occurs.

Can you do:

$ cmake -G "Unix Makefiles" -B build .
$ cd build
$ make VERBOSE=1

and check the compile definitions

Yes, sure. It's properly added to the arm-none-eabi-gcc:

[  1%] Building ASM object pico-sdk/src/rp2040/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
cd /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/build/pico-sdk/src/rp2040/boot_stage2 && /opt/homebrew/bin/arm-none-eabi-gcc -DPICO_32BIT=1 -DPICO_BOARD=\"pico\" -DPICO_BUILD=1 -DPICO_NO_HARDWARE=0 -DPICO_ON_DEVICE=1 -DPICO_RP2040=1 -I/Users/user/pico/pico-sdk/src/rp2040/boot_stage2/asminclude -isystem /Users/user/pico/pico-sdk/src/rp2040/hardware_regs/include -isystem /Users/user/pico/pico-sdk/src/rp2_common/hardware_base/include -isystem /Users/user/pico/pico-sdk/src/common/pico_base_headers/include -isystem /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/build/generated/pico_base -isystem /Users/user/pico/pico-sdk/src/boards/include -isystem /Users/user/pico/pico-sdk/src/rp2040/pico_platform/include -isystem /Users/user/pico/pico-sdk/src/rp2_common/pico_platform_compiler/include -isystem /Users/user/pico/pico-sdk/src/rp2_common/pico_platform_panic/include -isystem /Users/user/pico/pico-sdk/src/rp2_common/pico_platform_sections/include -isystem /Users/user/pico/pico-sdk/src/rp2040/boot_stage2/include -mcpu=cortex-m0plus -mthumb -g -O3 -DNDEBUG -o CMakeFiles/bs2_default.dir/compile_time_choice.S.obj   -c /Users/user/pico/pico-sdk/src/rp2040/boot_stage2/compile_time_choice.S

But, if I look further into this, it crashes at Generating _swiftcode.o:

/usr/bin/swiftc -target armv6m-none-none-eabi -Xcc -mfloat-abi=soft -Xcc -fshort-enums -Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library $( echo '/Users/user/pico/pico-sdk/src/common/pico_stdlib_headers/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_gpio/include;/Users/user/pico/pico-sdk/src/common/pico_base_headers/include;/Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/build/generated/pico_base;/Users/user/pico/pico-sdk/src/boards/include;/Users/user/pico/pico-sdk/src/rp2040/pico_platform/include;/Users/user/pico/pico-sdk/src/rp2040/hardware_regs/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_base/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_platform_compiler/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_platform_panic/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_platform_sections/include;/Users/user/pico/pico-sdk/src/rp2040/hardware_structs/include;/Users/user/pico/pico-sdk/src/common/hardware_claim/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_sync/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_sync_spin_lock/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_irq/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_uart/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_resets/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_clocks/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_pll/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_vreg/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_watchdog/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_ticks/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_xosc/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_divider/include;/Users/user/pico/pico-sdk/src/common/pico_time/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_timer/include;/Users/user/pico/pico-sdk/src/common/pico_sync/include;/Users/user/pico/pico-sdk/src/common/pico_util/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_time_adapter/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_runtime/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_runtime_init/include;/Users/user/pico/pico-sdk/src/common/pico_bit_ops_headers/include;/Users/user/pico/pico-sdk/src/common/pico_divider_headers/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_double/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_float/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_malloc/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_atomic/include;/Users/user/pico/pico-sdk/src/common/pico_binary_info/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_printf/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_stdio/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_stdio_uart/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_bootrom/include;/Users/user/pico/pico-sdk/src/common/boot_picoboot_headers/include;/Users/user/pico/pico-sdk/src/rp2_common/hardware_boot_lock/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_int64_ops/include;/Users/user/pico/pico-sdk/src/rp2_common/pico_mem_ops/include;/Users/user/pico/pico-sdk/src/rp2040/boot_stage2/include;/Users/user/pico/pico-sdk/src/common/boot_picobin_headers/include' | tr ';' '\n' | sed -e 's/\(.*\)/-Xcc -I\1/g' ) $( echo '/Applications/ArmGNUToolchain/13.3.rel1/arm-none-eabi/lib/gcc/arm-none-eabi/13.3.1/include /Applications/ArmGNUToolchain/13.3.rel1/arm-none-eabi/lib/gcc/arm-none-eabi/13.3.1/include-fixed /Applications/ArmGNUToolchain/13.3.rel1/arm-none-eabi/arm-none-eabi/include' | tr ' ' '\n' | sed -e 's/\(.*\)/-Xcc -I\1/g' ) -import-bridging-header /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/BridgingHeader.h /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/Main.swift -c -o /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/build/_swiftcode.o
error: generate-pch command failed with exit code 1 (use -v to see invocation)
/Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/BridgingHeader.h:14:10: note: in file included from /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/BridgingHeader.h:14:
12 | #pragma once
13 | 
14 | #include "pico/stdlib.h"
   |          `- note: in file included from /Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/BridgingHeader.h:14:
15 | 

/Users/user/pico/pico-sdk/src/common/pico_stdlib_headers/include/pico/stdlib.h:12:10: note: in file included from /Users/user/pico/pico-sdk/src/common/pico_stdlib_headers/include/pico/stdlib.h:12:
10 | #include "pico.h"
11 | #include "pico/stdio.h"
12 | #include "pico/time.h"
   |          `- note: in file included from /Users/user/pico/pico-sdk/src/common/pico_stdlib_headers/include/pico/stdlib.h:12:
13 | #include "hardware/gpio.h"
14 | #include "hardware/uart.h"

/Users/user/pico/pico-sdk/src/common/pico_time/include/pico/time.h:11:10: note: in file included from /Users/user/pico/pico-sdk/src/common/pico_time/include/pico/time.h:11:
  9 | 
 10 | #include "pico.h"
 11 | #include "hardware/timer.h"
    |          `- note: in file included from /Users/user/pico/pico-sdk/src/common/pico_time/include/pico/time.h:11:
 12 | 
 13 | #ifdef __cplusplus

/Users/user/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:11:10: note: in file included from /Users/user/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:11:
  9 | 
 10 | #include "pico.h"
 11 | #include "hardware/structs/timer.h"
    |          `- note: in file included from /Users/user/pico/pico-sdk/src/rp2_common/hardware_timer/include/hardware/timer.h:11:
 12 | #include "hardware/regs/intctrl.h"
 13 | 

/Users/user/pico/pico-sdk/src/rp2040/hardware_structs/include/hardware/structs/timer.h:15:10: note: in file included from /Users/user/pico/pico-sdk/src/rp2040/hardware_structs/include/hardware/structs/timer.h:15:
 13 |  */
 14 | 
 15 | #include "hardware/address_mapped.h"
    |          `- note: in file included from /Users/user/pico/pico-sdk/src/rp2040/hardware_structs/include/hardware/structs/timer.h:15:
 16 | #include "hardware/regs/timer.h"
 17 | 

1 error generated.
<unknown>:0: error: failed to emit precompiled header '/var/folders/qq/dv49h6m55wz7_w2f1lcjhs680000gn/T/TemporaryDirectory.l2s17U/BridgingHeader.pch' for bridging header '/Users/user/pico/examples/swift-embedded-examples/pico-blink-sdk/BridgingHeader.h'
/Users/user/pico/pico-sdk/src/rp2_common/hardware_base/include/hardware/address_mapped.h:177:10: error: 'hardware/structs/accessctrl.h' file not found
175 | #if !PICO_RP2040
176 | // include this here to avoid the check in every other hardware/structs header that needs it
177 | #include "hardware/structs/accessctrl.h"
    |          `- error: 'hardware/structs/accessctrl.h' file not found
178 | #endif
179 | 
make[2]: *** [_swiftcode.o] Error 1
make[1]: *** [CMakeFiles/swift-blinky-swiftcode.dir/all] Error 2
make: *** [all] Error 2

I think I figured out a possible reason for all of this: this build step is the custom Swift. The "main" file of this project is a Swift file: Main.swift uses BridgingHeader.h to include pico/stdlib.h, but there's no Pico SDK compile definitions passed to the swift compiler. And this is obviously because the Pico SDK doesn't handle this file on its own, it doesn't manage this compilation. So I'd have to somehow provide these compile definitions on my own in the CMakeLists.txt file. Right?

kilograham commented 1 month ago

Yes, this is rather what i thought might be happening, but you had said it wasn't coming from the swift bit

The swift-embedded should be using the compile definitions for the headers the same as for the regular build, or you may get inconsistencies - this was the case before, but here you now have a compile failure rather than potential silent weirdness

You can either set explicitly, do something hacky like https://github.com/raspberrypi/pico-playground/blob/9930bb057b7272176fbcd924b1d5856dfbc07503/standalone/static_sdk/CMakeLists.txt#L57 (to recursively gather the non-generator-expression compile definitions off your binary) or see if you can correctly get CMake to pass the compile definitions to your swift header import

iCMDdev commented 1 month ago

All right, thanks a lot for your help! I closed this as it isn't an SDK issue.

I'll try what you suggested. Thanks again!

iCMDdev commented 1 month ago

I tried the hacky way from the pico-playground repo and it worked flawlessly! šŸ‘ Thanks a lot!