ObKo / stm32-cmake

CMake for stm32 developing.
MIT License
1.18k stars 335 forks source link

Calling find_package() to find CMSIS multiple times leads to errors #326

Open Joaliasbarb opened 1 year ago

Joaliasbarb commented 1 year ago

Describe the bug CMake errors are popping up when calling find_package multiple times.

CMake Error at C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/cmake/FindCMSIS.cmake:107 (add_custom_target):
  add_custom_target cannot create target "CMSIS_LD_F401CB" because another
  target with the same name already exists.  The existing target is a custom
  target created in source directory
  "C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/examples/blinky".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/cmake/FindCMSIS.cmake:268 (cmsis_generate_default_linker_script)
  CMakeLists.txt:41 (find_package)

CMake Error at C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/cmake/FindCMSIS.cmake:266 (add_library):
  add_library cannot create imported target "CMSIS::STM32::F401CC" because
  another target with the same name already exists.
Call Stack (most recent call first):
  CMakeLists.txt:41 (find_package)

CMake Error at C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/cmake/FindCMSIS.cmake:107 (add_custom_target):
  add_custom_target cannot create target "CMSIS_LD_F401CC" because another
  target with the same name already exists.  The existing target is a custom
  target created in source directory
  "C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/examples/blinky".
  See documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
  C:/heig-vd/projects/stm32-cmake/stm32-cmake-multiple-find-package-calls/cmake/FindCMSIS.cmake:268 (cmsis_generate_default_linker_script)
  CMakeLists.txt:41 (find_package)

Sources to reproduce Please provide link to repository showing the problem. link to the commit causing an issue

Steps to reproduce

  1. Add multiple calls to the find_package function for CMSIS.

Expected behavior On the first find_package call, the behavior shall not change. On subsequent calls, already existing custom targets shall not be created again.

Environment :

Joaliasbarb commented 1 year ago

Here's a fix proposition (not sure if this is correct though).

Hish15 commented 1 year ago

It's weird to call the find_package command twice. We can hide the error but consider this :

find_package(CMSIS v1.0.0 COMPONENTS STM32F4 REQUIRED)
.
.
.
#With you proposal this will fail silently (not retrieving the v2.0.0 version)
find_package(CMSIS v2.0.0 COMPONENTS STM32F4 REQUIRED) 
Joaliasbarb commented 1 year ago

Of course, if we look at the modified blinky example, this may seem weird to call find_package twice. I stumbled upon this error because I have a repo for a project where multiple executables are compiled and sometime, for the same platform. For each executable, I would like to find the required CMSIS + HAL components.

The error is actually located in the cmsis_generate_default_linker_script function. Maybe it would be better to place the if statement in there to avoid creating the custom targets multiple time.

Hish15 commented 1 year ago

Ok, do open a PR so we can see :)

nische commented 4 months ago

Hi, did you have any news here?

Hish15 commented 4 months ago

Hello, Do not hesitate to open request so we can discuss the changes.

nische commented 4 months ago

I open a pull request with a little fix. That works for me at the moment but i dont know if anything other is broken now :-D

(Think i make something wrong while linking of pullrequest and issue)

Pullrequest: #343