Alvaro Palma Aste
Alvaro Palma Aste

Reputation: 409

CMake configuration to build a project from root or from subdirectories

I've started using CMake (2.8.12.2, the one included in CentOS 6.8) recently, and I think it's powerful enough to help acomplish what I want, but I haven't been able to figure out how :-), so I call for your wisdom to help me find the missing point.

I have a project layout like this:

BaseDir
|
+-->bin (generated by the process)
|    |
|    +-->Debug
|    +-->Release
|
+-->lib (generated by the process)
|    |
|    +-->Debug
|    +-->Release
|    
+-->CMakeLists.txt
|
+-->Library_A
|    |
|    +-->CMakeLists.txt
|    +-->include
|    +-->src
|    |    |
|    |    +-->CMakeLists.txt
|    |
|    +-->test # Small binary to test solely the library functions 
|        |
|        +-->CMakeLists.txt
|
+-->Library_B (depends on Library_A)
|    |
|    +-->CMakeLists.txt
|    +-->include
|    +-->src
|    |    |
|    |    +-->CMakeLists.txt
|    |
|    +-->test # Small binary to test solely the library functions 
|        |
|        +-->CMakeLists.txt
|
+-->Application_1 (depends on Library_B, hence transitivitely depends on Library_A)
|    |
|    +-->CMakeLists.txt
|    +-->include
|    +-->src
|        |
|        +-->CMakeLists.txt
|
+-->Application_2 (depends on Library_A)
    |
    +-->CMakeLists.txt
    +-->include
    +-->src
        |
        +-->CMakeLists.txt

It works like a charm when I place myself under BaseDir and run "cmake .". Application_1, Application_2, Library_A and Libray_B are all built in the proper order, linked, etc.

However, my idea is to be also able to build while standing under any of the subdirectories (Application_, Library_), and in that case, build only the code relevant to it (meaning itself, its tests and its dependencies). For example, while standing inside Library_A, only that folder is build, while from Library_B, Library_A is also built, and the equivalent that happens when standing under Application_1 or Application_2. Plus, indepedently on where I'm standing to trigger the cmake process, the build results (libs or bins) must always placed under BaseDir/{lib|bin}/{Debug/Release/etc}, never under the libraries or applications subdirectories. It implies, for example, that the linking of Library_B (which depends on Library_A) or Application_1 (which depends on Library_A and B) must look into BaseDir/lib/{Debug/Release}.

My goal is to have many App's under BaseDir, so I want to avoid having to build them all every time if that's not really necessary, just the single App I want.

I've looked into CMakeLists.txt files for multiple libraries and executables and also CMake and finding other projects and their dependencies, but that's not really the same situation than what I'm trying to achieve here.

I've tried something like the following:

BaseDir/CMakeLists.txt
|
|    cmake_minimum_required (VERSION 2.8)
|    project (BASE_DIR)
|
|    add_subdirectory (Library_A)     # No local dependencies
|    add_subdirectory (Library_B)     # Depends on A
|    add_subdirectory (Application_1) # Depends on A and B
|    add_subdirectory (Application_2) # Depends on A
|
|    # I want all binary outputs (executables and libraries) placed under BaseDir/{lib|bin}/{Debug|Release}
|    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}") # For the executables
|    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the static libraries
|    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
|
+-->BaseDir/Library_A/CMakeLists.txt (again, no dependencies)
|    |
|    |    cmake_minimum_required (VERSION 2.8)
|    |    project (LIB_A)
|    |    
|    |    # In case CMake is run from within BaseDir/Library_A and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
|    |    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
|    |    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
|    |    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
|    |
|    |    include_directories(include)
|    |    add_subdirectory (src)
|    |    add_subdirectory (test)
|    |    set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
|    |
|    +-->BaseDir/Library_A/src/CMakeLists.txt
|    |
|    |        cmake_minimum_required (VERSION 2.8)
|    |
|    |        # add all files in the current directory
|    |        file(GLOB LIB_A_SRCS "*.h" "*.cpp")
|    |
|    |        # Create a library called libA.a
|    |        add_library(A ${LIB_A_SRCS})
|    |
|    +-->BaseDir/Library_A/test/CMakeLists.txt
|    
|            cmake_minimum_required (VERSION 2.8)
|    
|            # add all files in the current directory
|            file(GLOB TEST_A_SRCS "*.h" "*.cpp")
|    
|            # Create an executable file from sources
|            add_executable(TEST_A ${TEST_A_SRCS})
|    
|            # Link this executable to the library it's testing
|            target_link_libraries(TEST_A A)
|    
+-->BaseDir/Library_B/CMakeLists.txt (dependency on A)
|    |    
|    |    cmake_minimum_required (VERSION 2.8)
|    |    project (LIB_B)
|    |    
|    |    # In case CMake is run from within BaseDir/Library_B and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
|    |    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}") # For the test executables
|    |    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the static libraries
|    |    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../lib/${CMAKE_BUILD_TYPE}") # For the dynamic libraries
|    |
|    |    include_directories(include)
|    |    add_subdirectory (src)
|    |    add_subdirectory (test)
|    |    set(${PROJECT_NAME}_INCLUDE_DIRECTORIES ${PROJECT_SOURCE_DIR}/include CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
|    |
|    +-->BaseDir/Library_B/src/CMakeLists.txt
|    |
|    |        cmake_minimum_required (VERSION 2.8)
|    |
|    |        # add all files in the current directory
|    |        file(GLOB LIB_B_SRCS "*.h" "*.c")
|    |
|    |        # Create a library called libB.a
|    |        add_library(B ${LIB_B_SRCS})
|    |        
|    |        # Add a dependency to Library_A
|    |        find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_A)
|    |        include_directories("$LIB_A_INCLUDE_DIRECTORIES")
|    |        target_link_libraries(B ${LIBRARY_A}) 
|    |
|    +-->BaseDir/Library_B/test/CMakeLists.txt
|
|            cmake_minimum_required (VERSION 2.8)
|
|            # add all files in the current directory
|            file(GLOB TEST_B_SRCS "*.h" "*.cpp")
|
|            # Create an executable file from sources, for both versions of the library
|            add_executable(TEST_B ${TEST_B_SRCS})
|            
|            # Link this executable to the library it's testing
|            target_link_libraries(TEST_B B)
|
+-->BaseDir/Application_1/CMakeLists.txt
|    |
|    |    cmake_minimum_required (VERSION 2.8)
|    |    project (APP_1)
|    |    
|    |    # In case CMake is run from within BaseDir/Application_1 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
|    |    # In this case, only executables are generated.
|    |    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
|    |
|    |    include_directories(include)
|    |    add_subdirectory (src)
|    |
|    +-->BaseDir/Application_1/src/CMakeLists.txt
|
|            cmake_minimum_required (VERSION 2.8)
|
|            # add all files in the current directory
|            file(GLOB APP_1_SRCS "*.cpp")
|
|            # Create an executable file from sources
|            add_executable(EXE_1 ${APP_1_SRCS})
|            
|            # This should automatically bring Library_A
|            find_library(LIBRARY_B B PATHS ${CMAKE_CURRENT_SOURCE_DIR}/../../Library_B)
|            include_directories(${LIB_B_INCLUDE_DIRECTORIES})
|            target_link_libraries(EXE_1 ${LIBRARY_B})
|
+-->BaseDir/Application_2/CMakeLists/CMakeLists.txt
    |
    |    cmake_minimum_required (VERSION 2.8)
    |    project (APP_2)
    |    
    |    # In case CMake is run from within BaseDir/Application_2 and not from BaseDir, I still want the outputs being placed under BaseDir/{lib|bin}/{Debug|Release}
    |    # In this case, only executables are generated.
    |    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${CMAKE_BUILD_TYPE}")
    |
    |    include_directories(include)
    |    add_subdirectory (src)
    |
    +-->BaseDir/Application_2/src/CMakeLists.txt

            cmake_minimum_required (VERSION 2.8)

            # add all files in the current directory
            file(GLOB APP_2_SRCS "*.cpp")

            # Create an executable file from sources
            add_executable(EXE_2 ${APP_2_SRCS})

            # Link this executable to the library it needs
            find_library(LIBRARY_A A PATHS ${CMAKE_CURRENT_SOURCE_DIR}../../Library_A)
            include_directories(${LIB_A_INCLUDE_DIRECTORIES})
            target_link_libraries(EXE_2 ${LIBRARY_A})

But with no luck, because when I run from the subdirectories (for example, BaseDir/Application_1), I just get:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: LIBRARY_B

I guess the "find_library()" call is not enough for what I want, which is basically to load all the settings contained in the libraries project, including not only the library itself, but also the include's directories added by that library. What is exactly the way of usage for find_library(), to point it to the location of the project creating the library, or the actual resulting library (".a" or ".so")?

So, my main doubt is: Is this layout doable? If so, what am I missing? Should I use something like find_package() or find_path()? How do I trigger the parsing of a CMakeLists.txt config file from another "same level" CMakeLists.txt?

PS: Do I really need to consider using "add_dependencies()" at any point? What's the point of that command if not?

Upvotes: 1

Views: 1888

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54747

First a general piece of advice: In CMake you will always want try to strictly separate your build directories from your source directories. In particular, you should never write files back to the source directory as part of the build process. Write your scripts so that they will also work with the source directory being mounted on a read-only file system. While this might seem like an arbitrary restriction at first, it actually will help you avoid many headaches in the long run.

As for your actual problem: find_library is a very low-level tool for solving the dependency problem. The idea here is basically that you have binaries for a third-party library (which itself knows nothing about CMake) installed somewhere on the system and you know need to find them simply by inspecting the contents of the filesystem. This is not the case here, as you build the dependencies as part of the same CMake configure run.

What you want to do instead is directly depend on the targets for the dependency. So instead of doing this:

find_library(LIBRARY_A A)
target_link_libraries(B ${LIBRARY_A}) 

you would directly write

target_link_libraries(B A)

This of course only works if A is a known target at that point, and this is the problem that you should focus on.

As long as you keep building the libraries together as part of the same CMake run, this is pretty straightforward: If a target is added by a parent or sibling directory, you should be able to use it right away. What complicates things in your situation is that you also want to be able to build the different libraries in isolation. That means you need a mechanism that imports a target into your build so that it looks as if that target was again produced as part of the same CMake run.

You can either do this manually by adding an imported target and setting its interface properties, or you can use CMake's packaging mechanism to have this done in an automated way. Note that neither of these is trivial to pull off, so you might want to start with the simple case where everything happens in one CMake run and then add support for the separated build later once you are more comfortable with using CMake.

Upvotes: 1

Related Questions