Excluding a sub directory from executing CMakeLists.txt - cmake

I have the following project structure
MainProj
|-SubDir1
| |-Docs
| |-CMakeLists.txt
| |-Src
| |-CMakeLists.txt
|-SubDir2
| |-Src
| |-CMakeLists.txt
|-CMakeLists.txt
In my Mainproj cmakeList.txt I am using
Add_subdirectory(subDir1)
Add_subdirectory(subDir2)
So now I want to exclude the mainproj/subdir1/doc from the build process. But I couldn't find a direct way of doing the same in CMake scripts. Any help in this regard would be appreciated.

Related

How to configure a subdirectory project in CMake in a way that find_package in another one is able to find it?

I am trying to build the netCDF C++ library as a dependency to my project on Windows 10 using MSVC shipped with VS2017 (I think v141 but not sure). Since some of the dependencies I will mention below require CMake > 3.12 I am forced to use CMake that is not shipped with VS2017. The problem is that netCDF C++ requires the netCDF C library (I guess the former is a wrapper for the latter), which in terms depends on the HDF5 library.
All can be found on github as CMake projects:
https://github.com/HDFGroup/hdf5
https://github.com/Unidata/netcdf-c
https://github.com/Unidata/netcdf-cxx4
Currently my project's structure is as follows:
MAIN PROJECT
|
*--- CMakeLists.txt
|
*--- examples (CMake subproject)
| |
| *---CMakeLists.txt
| |
| *--- example1 (CMake subproject, uses my_library)
| |
| *--- example2 (CMake subproject, uses my_library)
|
*--- deps (CMake subproject)
| |
| *--- CMakeLists.txt
| |
| *--- jsoncpp (CMake subproject, git clone)
| |
| *--- ...
| |
| *--- hdf5 (CMake subproject, git clone)
| |
| *--- netcdf-c (CMake subproject, git clone)
| |
| *--- netcdf-cxx4 (CMake subproject, git clone)
|
*--- my_library (CMake subproject, my own library that uses deps)
|
*--- my_binary (CMake subproject, my own executable that uses my_library and deps)
Until I started tinkering with netCDF I didn't have any issues. My deps CMakeLists.txt look like this:
add_subdirectory(sqlite3)
set(JSONCPP_WITH_EXAMPLE OFF CACHE BOOL "Compile JsonCpp example")
set(JSONCPP_WITH_TESTS OFF CACHE BOOL "Compile and (for jsoncpp_check) run JsonCpp test executables")
set(JSONCPP_WITH_POST_BUILD_UNITTEST OFF CACHE BOOL "Automatically run unit-tests as a post build step")
set(BUILD_STATIC_LIBS ON CACHE BOOL "Build jsoncpp_lib as a static library.")
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build jsoncpp_lib as a shared library.")
add_subdirectory(jsoncpp)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries")
set(BUILD_CURL_EXE OFF CACHE BOOL "Build curl EXE")
add_definitions("-DCURL_STATICLIB")
if(WIN32)
set(ENABLE_UNICODE OFF CACHE BOOL "Set to ON to use the Unicode version of the Windows API functions")
endif()
add_subdirectory(curl)
add_subdirectory(hdf5)
add_subdirectory(netcdf-c) # ISSUE with find_package and hdf5
add_subdirectory(netcdf-cxx4)
The netcdf-c subproject has the following lines, which cause a problem for me:
IF(MSVC)
SET(SEARCH_PACKAGE_NAME ${HDF5_PACKAGE_NAME})
FIND_PACKAGE(HDF5 NAMES ${SEARCH_PACKAGE_NAME} COMPONENTS C HL CONFIG REQUIRED ${NC_HDF5_LINK_TYPE})
ELSE(MSVC)
FIND_PACKAGE(HDF5 COMPONENTS C HL REQUIRED)
ENDIF(MSVC)
From what I saw the same applies to netcdf-cxx4 so solving this can hopefully be applied to it too.
I do not want to touch anything inside the respective dependenies' folders (I plan on adding those as git submodules later on). Is there a way to instruct the underlying sub-project (just like a did for e.g. JSON++) where to look without installing anything from the project one level up? Possibly a useful note here is that I also don't have any install() calls.

CMake: Referencing a sibling native Android Library module from App code

I have an Android Studio project with a general layout as follows:
Project/
|
+-- app/
| |
| +-- src/main/cpp/
| | |
| | +-- native-lib.cpp
| | +-- CMakeLists.txt
| |
| +-- build.gradle
| +-- CMakeLists.txt
| |
+-- sibling-lib/
| |
| +-- src/main/cpp/
| | |
| | +-- partA/CMakeLists.txt
| | +-- partB/CMakeLists.txt
| | +-- partC/CMakeLists.txt
| |
| +-- build.gradle
| +-- CMakeLists.txt
|
+-- build.gradle
+-- settings.gradle
sibling-lib is a third-party library comprised of multiple parts, each with separate sources and common headers, each of which is compiled into either a static or shared library, using the associated CMake file, and included as a subdirectory by the top-level library CMake file. Most of those parts are dependent on other parts.
The goal is to compile and link the parts of sibling-lib into library files within an Android Library (.aar) and use those libraries from the native files within the main app (e.g. native-lib.cpp). I have been able to compile each of the parts as a static library, with the final part linked together as a shared library (using target_link_libraries() on that part), and then load the library from the app's Java code. However, when I try to call any of sibling-lib's functions, I receive reference errors when compiling.
How should I configure the CMake and gradle files (for both the app and library) to properly compile and set up my project? Is there, however, a better way to configure the project to obtain the desired result?

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?
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.

How to set include_directories from a CMakeLists.txt file?

I seem to be having trouble setting the include path (-I) using the include_directories() command in CMake. My project directory is as follows:
Root
| - CMakeLists.txt
| - libs
| - | - CMakeLists.txt
| - | - inc
| - | - | - // lib specific includes
| - | - src
| - | - | - // lib specific sources
| - proj1
| - | - CMakeLists.txt
| - | - inc
| - | - | - // proj1 specific includes
| - | - src
| - | - | - // proj1 specific sources
The root CMakeLists.txt file looks like so:
project(ROOT)
add_subdirectory(libs)
add_subdirectory(proj1)
The CMakeLists.txt file under libs:
project(lib)
add_library(lib STATIC ${lib_hdrs} ${lib_srcs}) // for conciseness, omitted set()
And lastly, the CMakeLists.txt file under proj1:
project(proj1)
include_directories("${ROOT_SOURCE_DIR}/lib/inc") # <- problem line?
add_executable(proj1 ${proj1_srcs})
target_link_libraries(proj1 lib)
The goal is to create the library from the source and header files in libs, then link against the executable generated under proj1. Proj1 has some files that #include stuff in libs include, so I need to add the directories to be used with -I. Based on the documentation, that's what include_directories() is supposed to do. However despite explicitly setting that and following it with a debug message(${INCLUDE_DIRECTORIES}), the INCLUDE_DIRECTORIES variable is an empty string, and no directories are specified for the include path, so my compilation of proj1 fails.
I've also attempted removing the quotes around ${ROOT_SOURCE_DIR}/inc to see if that helped but no luck.
include_directories() populates a directory property called INCLUDE_DIRECTORIES:
http://www.cmake.org/cmake/help/v2.8.12/cmake.html#prop_dir:INCLUDE_DIRECTORIES
Note that CMake 2.8.11 learned the target_include_directories command, which populates the INCLUDE_DIRECTORIES target property.
http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:target_include_directories
Note also that you can encode the fact that 'to compile against the headers of the lib target, the include directory lib/inc is needed' into the lib target itself by using target_include_directories with the PUBLIC keyword.
add_library(lib STATIC ${lib_hdrs} ${lib_srcs}) # Why do you list the headers?
target_include_directories(lib PUBLIC "${ROOT_SOURCE_DIR}/lib/inc")
Note also I am assuming you don't install the lib library for others to use. In that case you would need to specify different header directories for the build location and for the installed location.
target_include_directories(lib
PUBLIC
# Headers used from source/build location:
"$<BUILD_INTERFACE:${ROOT_SOURCE_DIR}/lib/inc>"
# Headers used from installed location:
"$<INSTALL_INTERFACE:include>"
)
Anyway, that's only important if you are installing lib for others to use.
After the target_include_directories(lib ...) above you don't need the other include_directories() call. The lib target 'tells' proj1 the include directories it needs to use.
See also target_compile_defintions() and target_compile_options().

Copy artifacts of each module in multi-module project to specific folder [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Maven multi-module project - copying all “package” JARS from submodules into parent/target/
I have an pom structure:
project1
|
|______ pom.xml
|______ module 1
| |_____ pom.xml
| |_____ dist
| |_____ bin
| |_____ lib
|______ module 2
| |_____ pom.xml
| |_____ dist
| |_____ bin
| |_____ lib
|______ dist
| |____ bin
| |____ lib
Requirements:
+ When build each module, bundle file will be copied to its' dist/bin and dependencies copied to its' dist/lib
+ When build project1, bundle file of each module will be copied to project1/dist/bin and dependencies of all modules copied to project/dist/lib
Can we achieve this??? If it can, how we do it???
Just take a look here: https://github.com/khmarbaise/maven-copy-example . The only thing i changed is not to use a a folder like dist i use target/dist in the maven style.