For ones who dont know STM32 library structure. The library is not projected to cmake use (or any another build system as i think).
It have a bunch of source\headers files
stm32_hal.c
stm32_hal.h
They are provided with git repository, as example https://github.com/STMicroelectronics/STM32CubeF4
This files is depended by user provided stm32_hal_conf.h
The structure of project is looked like.
\ Repository
\--- HAL submodule library
\------ stm32_hal.c
\------ stm32_hal.h
\--- User library
\------ lib.c
\------ lib.h
\--- Project sources (depended from HAL)
\------ main.c
\------ main.h
\------ stm32_hal_conf.h (file, HAL library depended by)
The main question - HOW?
Obiviusly compilation HAL as generic add_library()
would fail, as HAL library doesnt have access and even dont know about stm32_hal_conf.h
There is a repository, where this problem seems solved, but there is too much high level CMake code-mess, i cant even make it works. https://github.com/ahessling/STM32F4Template
I dont want to hard copy HAL to project and dont want to include files as lists.
The code samples:
HAL FindPackage()
function(FindCmsisSTM32F4)
set(CMSIS_LIBRARY_NAME "LIB_CMSIS")
set(CMSIS_PATH ./submodules/stm32cubef4/Drivers/CMSIS)
list(APPEND CMSIS_INCLUDE
${CMSIS_PATH}/Device/ST/STM32F4xx/Include
${CMSIS_PATH}/Core/Include)
set(CMSIS_STM32_INCLUDE_PATH ${CMSIS_PATH}/Device/ST/STM32F4xx/include)
add_library(${CMSIS_LIBRARY_NAME} INTERFACE)
target_include_directories(${CMSIS_LIBRARY_NAME} INTERFACE ${CMSIS_INCLUDE})
endfunction(FindCmsisSTM32F4)
FindCmsisSTM32F4()
set(LIB_CMSIS "LIB_CMSIS")
Project CMakeLists.txt
find_package(STM32F4_CMSIS)
# Add sources to elf file
add_executable(${elf_file} ${sources})
# Link Generic stm32 libraries to project
target_include_directories(${elf_file} PUBLIC ./src) # hal_conf.h inclusion
target_link_libraries(${elf_file} PRIVATE ${LIB_HAL})