Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding header references to interface library #393

Closed
lefebvresam opened this issue Nov 22, 2024 · 5 comments
Closed

Adding header references to interface library #393

lefebvresam opened this issue Nov 22, 2024 · 5 comments

Comments

@lefebvresam
Copy link

I have a folder where I gather all my specific files for my target:

image

The CMakeLists.txt looks like:

# Copyright (c) 2023 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-hmc20 INTERFACE)

target_sources(mbed-hmc20 INTERFACE
    PeripheralPins.c
)

target_include_directories(mbed-hmc20 INTERFACE
    .
)

target_link_libraries(mbed-hmc20 INTERFACE 
    mbed-stm32u575xg
)

PeripheralPins.c has:

#include "PeripheralPins.h"
#include "mbed_toolchain.h"
#include "PeripheralPinMaps.h"

In PinNames.h I define the pins and a bit further I use a MACRO:

#include "cmsis.h"
#include "PinNamesTypes.h"
...
    // STDIO for console print
#ifdef MBED_CONF_TARGET_STDIO_UART_TX
    CONSOLE_TX = MBED_CONF_TARGET_STDIO_UART_TX,
#else
    #ifdef STDOUT_USB
    CONSOLE_TX = PA_0,
    #else
    CONSOLE_TX = PB_6,
    #endif
#endif
#ifdef MBED_CONF_TARGET_STDIO_UART_RX
    CONSOLE_RX = MBED_CONF_TARGET_STDIO_UART_RX,
#else
    #ifdef STDOUT_USB
    CONSOLE_RX = PA_1,
    #else
    CONSOLE_RX = PB_7,
    #endif
#endif

But I want STDOUT_USB to define in my main.h file in my source code, where other settings are.

Normally I could do something like:

target_include_directories(mbed-hmc20 PRIVATE 
    "${APP_SOURCE_PATH}"
)

But this is not working because mbed-hmc20 is an interface library, just to let 'mbed-stm32u575xg' know that there are some extra files to compile.

If I put #include "main.h" in PinNames.h is is not found and if I put app_source_path I get:

CMake Error at HMC20/CMakeLists.txt:14 (target_include_directories):
  target_include_directories may only set INTERFACE properties on INTERFACE
  targets

and with INTERFACE I get:

/home/sam/git/hmipanel/HMC20/./PinNames.h:36:10: fatal error: main.h: No such file or directory
   36 | #include "main.h"

How can I avoid having to put my setting MACRO's in PinNames.h?

@multiplemonomials
Copy link
Collaborator

Hmm... assuming main.h is in your project root directory, it might be easiest to just include it by relative path in PinNames.h:

#include "../main.h"

Though this should also work:

target_include_directories(mbed-hmc20 INTERFACE
    "${APP_SOURCE_PATH}"
)

If it's not working, perhaps the APP_SOURCE_PATH variable is not defined at this point in the build script?

Also, keep in mind that doing it this way means that every Mbed source file will include main.h, which may or not be what you want. main.h must be includable from C, and any change to main.h will rebuild the entire project.

Last but not least, for the future, since this is not an actual issue with Mbed CE, this kind of thing would be better as a discussion than an issue. OK for now, just remember for next time!

Hope this is helpful, let me know if that works!

@lefebvresam
Copy link
Author

lefebvresam commented Nov 25, 2024

This is working now. I think there was an older issue where I defined global functions in my main.h and if you include then main.h over multiple compilation units you have double function definitions because they are not methods in a class.

The strange thing here is that you need at least one empty c file PeripheralPins.c that is doing the three includes as mentioned above and PinName.h is separate but imported via the include_directories into the mbed-stm32u575xg with target_link_libraries. Is that right? This looks a bit odd but it's the only way to get it working.

@multiplemonomials
Copy link
Collaborator

That sounds more or less right, but I'm surprised you need an empty PeripheralPins.c. Not having that .c file should still let the build work, as long as the actual peripheral pins arrays that are supposed to go there are declared somewhere else. If that's not it's behaving, please post your project in a discussion and we will take a look!

@lefebvresam
Copy link
Author

lefebvresam commented Nov 28, 2024

It's not working after a full compile I get a message for each part that is using PinNames.h:

In file included from /home/sam/git/hmipanel/mbed-os/targets/TARGET_STM/TARGET_STM32U5/./objects.h:22,
                 from /home/sam/git/hmipanel/mbed-os/targets/TARGET_STM/./device.h:38,
                 from /home/sam/git/hmipanel/mbed-os/hal/include/hal/ticker_api.h:25,
                 from /home/sam/git/hmipanel/mbed-os/drivers/./include/drivers/TimerEvent.h:20,
                 from /home/sam/git/hmipanel/mbed-os/platform/include/platform/internal/SysTimer.h:22,
                 from /home/sam/git/hmipanel/mbed-os/platform/include/platform/internal/mbed_os_timer.h:21,
                 from /home/sam/git/hmipanel/mbed-os/rtos/./include/rtos/Kernel.h:31,
                 from /home/sam/git/hmipanel/mbed-os/rtos/./include/rtos/Mutex.h:30,
                 from /home/sam/git/hmipanel/mbed-os/drivers/usb/include/usb/internal/AsyncOp.h:21,
                 from /home/sam/git/hmipanel/mbed-os/drivers/usb/source/OperationListBase.cpp:19:
/home/sam/git/hmipanel/HMC20/./PinNames.h:25:10: fatal error: main.h: No such file or directory
   25 | #include "main.h"

And it's a whole series ...

CMakeLists.txt is:

# Copyright (c) 2023 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-hmc20 INTERFACE)

target_sources(mbed-hmc20 INTERFACE
    PeripheralPins.c
)

target_include_directories(mbed-hmc20 INTERFACE
    .
    "${APP_SOURCE_PATH}"
)

target_link_libraries(mbed-hmc20 INTERFACE 
    mbed-stm32u575xg
)

@lefebvresam
Copy link
Author

lefebvresam commented Nov 28, 2024

It's working now. Indeed I have to put APP_SOURCE_PATH before the add_subdirectory(HMC20), and not in the subdirectory source where the HMC20 subdir was already added:

set(APP_SOURCE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/source CACHE INTERNAL "")

# Include folder with the custom targets.
add_subdirectory(nlohmann_json)
add_subdirectory(ra8875)
add_subdirectory(HMC20)
add_subdirectory(mbed-os)
add_subdirectory(source)

mbed_finalize_build()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants