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

[workspaces] Improve generated CMake files #5291

Closed
jgsogo opened this issue Jun 4, 2019 · 0 comments
Closed

[workspaces] Improve generated CMake files #5291

jgsogo opened this issue Jun 4, 2019 · 0 comments

Comments

@jgsogo
Copy link
Contributor

jgsogo commented Jun 4, 2019

The generated files related to workspaces for CMake are not using the actual targets that CMake creates but the imported ones from the editable packages (that points to the files that the actual target is generating). This creates a kind of frankenstein-project that works, but it would fail if the dependencies are in the wrong order, and it cannot take the advantages of having the full project modeled by CMake.

Proposal: workspace command can generate all the files needed for the workspace:

  • root CMakeLists.txt:

    cmake_minimum_required(VERSION 3.10)
    project(workspace LANGUAGES CXX)
    
    include("${CMAKE_CURRENT_SOURCE_DIR}/conanbuildinfo.cmake")
    conan_basic_setup(TARGETS)
    
    include("${CMAKE_CURRENT_SOURCE_DIR}/conanworkspace.cmake")
  • conanbuildinfo.cmake: this file is generated on the fly using the provided settings/configuration, but without dependencies (from an empty conanfile.txt). We want this file to keep executing the Conan magic invoked in conan_basic_setup.

  • conanworkspace.cmake: this file orchestrate everything related to the workspace:

    cmake_minimum_required(VERSION 3.10)
    
    # List of targets involved in the workspace
    set(ws_targets "pkgA" "pkgB" "pkgC")
    
    # Override functions to avoid importing existing TARGETs (or calling again Conan Magic)
    function(conan_basic_setup)
        message("Ignored call to 'conan_basic_setup'")
    endfunction()
    
    # Do not use find_package for those packages handled within the workspace
    function(find_package)
        if(NOT "${ARG0}" IN_LIST ws_targets)
            # Note.- If it's been already overridden, it will recurse forever
            message("find_package(${ARG0})")
            _find_package(${ARGV})
        else()
            message("find_package(${ARG0}) ignored, it is a target handled by Conan workspace")
        endif()
    endfunction()
    
    # Add subdirectories for packages (like it is now) and create aliases
    add_subdirectory("sources/pkgA" "build/pkgA")
    add_library(pkgA::pkgA ALIAS pkgA)
    add_library(CONAN_PKG::pkgA ALIAS pkgA)  

Given these files, we would be creating the CMake project and all the packages included in the workspace would be actual CMake targets. Adding those aliases we guaranteed that we are able to link with the actual targets pkgA and also with CONAN_PKG::pkgA (generator cmake) and pkgA::pkgA (generator cmake_find_package)... for other genrators or packages linking with requirements using the ${CONAN_LIBS} this approach is not going to work out of the box.

Additionally, we should warn the user about packages that are in the graph but not included in the workspace because those packages are not built again and won't take into account changes of sources or libraries (but this is a different issue to be solved)

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

No branches or pull requests

2 participants