jrl-umi3218 / jrl-cmakemodules

CMake utility toolbox
https://jrl-cmakemodules.readthedocs.io/en/master/
Other
57 stars 45 forks source link

CMake configure failure due to variable scope #596

Closed alxbilger closed 1 year ago

alxbilger commented 1 year ago

Hi,

I have a couple of errors in the configure step:

Windows 11, CMake 3.24.1

jcarpent commented 1 year ago

I guess you are embedding a project based on the JRL cmake module into another CMake project?

alxbilger commented 1 year ago

Yes, but the master project does not interact (voluntarily) with JRL cmake project. Do you support this configuration?

jcarpent commented 1 year ago

Normally yes, but we might still need to fix some issues with this cmake feature. Could you provide a minimalist reproducible example to move forward?

gergondet commented 1 year ago

one thing to look into is that we are relying on an old trick to trigger finalize calls (see details here) and while it still works in the most recent CMake versions for standalone projects it might not be working as nicely with more recent CMake versions (or under all circumstances when embedded inside another cmake project)

With CMake >= 3.19 we could rely on cmake_language(DEFER) to achieve the same thing

@alxbilger if you have some time, would you mind trying the following modification in base.cmake (around Line 214 which I pointed out earlier)

variable_watch(CMAKE_CURRENT_LIST_DIR SETUP_PROJECT_FINALIZE_HOOK)
function(SETUP_PROJECT_FINALIZE_HOOK VARIABLE ACCESS)
  if("${${VARIABLE}}" STREQUAL "")
    set(CMAKE_CURRENT_LIST_DIR ${PROJECT_JRL_CMAKE_MODULE_DIR})
    set(JRL_CMAKEMODULE_LOGGING_FILENAME
        "${PROJECT_JRL_CMAKE_BINARY_DIR}/config.log")
    setup_project_finalize()
    if(PROJECT_USE_CMAKE_EXPORT)
      setup_project_package_finalize()
    endif()
    set(CMAKE_CURRENT_LIST_DIR "") # restore value
    set(JRL_CMAKEMODULE_LOGGING_FILENAME "") # restore value
  endif()
endfunction()

to

function(do_finalize)
  setup_project_finalize()
  if(PROJECT_USE_CMAKE_EXPORT)
    setup_project_package_finalize()
  endif()
endfunction()
cmake_language(DEFER CALL do_finalize)

This will trigger jrl-cmakemodules finalize calls at the end of the included project CMakeLists.txt without fail.

alxbilger commented 1 year ago

@gergondet I tested your suggestion and it works properly. I added a watch on JRL_CMAKEMODULE_LOGGING_FILENAME and it always reads a defined value, whereas it was not the case before. I also confirm that the file config.log is well created and filled. And I no longer have build failure in the doc target.

olivier-stasse commented 1 year ago

I am closing this issue because it looks like that your issue has been solved. Feel free to reopen it if needed.