cmake_minimum_required(VERSION 3.13) set(libcaosdb_VERSION 0.0.1) project(libcaosdb VERSION ${libcaosdb_VERSION} DESCRIPTION "Plain C client libraries for CaosDB" LANGUAGES C) set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") add_subdirectory(src) add_subdirectory(include) add_subdirectory(doc) add_library(libcaosdb STATIC ${libcaosdb_INCL} ${libcaosdb_SRC}) add_executable(caosdbcli src/caosdbcli.c) target_link_libraries(caosdbcli libcaosdb) if("${CMAKE_BUILD_TYPE}" MATCHES "Debug") target_link_libraries(libcaosdb gcov) enable_testing() add_subdirectory(test) append_coverage_compiler_flags() endif() # These variables slightly modify the install location to allow for version # specific installations. set(libcaosdb_INCLUDE_DEST "include/libcaosdb-${libcaosdb_VERSION}") set(libcaosdb_LIB_DEST "lib/libcaosdb-${libcaosdb_VERSION}") # generator expressions are needed for the include directories, since # installing headers changes the include path specify that libcaosdb requires # the files located in the include/ directory at compile time. This would # normally look like # target_include_directories(libcaosdb PUBLIC include/) # PUBLIC means that other libraries including libcaosdb should also include # the directory include/. # However, there is a catch. If we are installing the project in # CMAKE_INSTALL_PREFIX, we can't specify include/ in the build directory: we # have copied the contents of include to CMAKE_INSTALL_PREFIX/include and we # would like other projects to include this directory instead of include/. # The following CMake command handles this. $<BUILD_INTERFACE:...> and # $<INSTALL_INTERFACE:...> are macros whose values change depending on if we # are simply building the code or if we are installing it. target_include_directories(libcaosdb PUBLIC # headers to include when building from source $<BUILD_INTERFACE:${libcaosdb_SOURCE_DIR}/include> $<BUILD_INTERFACE:${libcaosdb_BINARY_DIR}/include> # headers to include when installing (implicitly prefixes with ${CMAKE_INSTALL_PREFIX}). $<INSTALL_INTERFACE:include> ) # Install libcaosdb in CMAKE_INSTALL_PREFIX (defaults to /usr/local on linux). # To change the install location, run # cmake -DCMAKE_INSTALL_PREFIX=<desired-install-path> .. # install(...) specifies installation rules for the project. It can specify # location of installed files on the system, user permissions, build # configurations, etc. Here, we are only copying files. # install(TARGETS ...) specifies rules for installing targets. # Here, we are taking a target or list of targets (libcosdb) and telling # CMake the following: # - put shared libraries associated with libcaosdb in ${libcaosdb_LIB_DEST} # - put static libraries associated with libcaosdb in ${libcaosdb_LIB_DEST} # - put include files associated with libcaosdb in ${libcaosdb_INCLUDE_DEST} # We also need to specify the export that is associated with libcaosdb; an # export is just a list of targets to be installed. So we are associating # libcaosdb with libcaosdbTargets. install( # targets to install TARGETS libcaosdb # name of the CMake "export group" containing the targets we want to install EXPORT libcaosdbTargets # Dynamic, static library and include destination locations after running # "make install" LIBRARY DESTINATION ${libcaosdb_LIB_DEST} ARCHIVE DESTINATION ${libcaosdb_LIB_DEST} INCLUDES DESTINATION ${libcaosdb_INCLUDE_DEST} ) # We now need to install the export libcaosdbTargets that we defined above. # This is needed in order for another project to import libcaosdb using # find_package(libcaosdb) # find_package(libcaosdb) will look for libcaosdb-config.cmake to provide # information about the targets contained in the project libcaosdb. # Fortunately, this is specified in the export libcaosdbTargets, so we will # install this too. # install(EXPORT ...) will install the information about an export. Here, we # save it to a file {$libcaosdb_LIB_DEST}/libcaosdbTargets.cmake and prepend # everything inside libcaosdbTargets with the namespace libcaosdb::. install( # The export we want to save (matches name defined above containing the # install targets) EXPORT libcaosdbTargets # CMake file in which to store the export's information FILE libcaosdbTargets.cmake # Namespace prepends all targets in the export (when we import later, we # will use libcaosdb::libcaosdb) NAMESPACE libcaosdb:: # where to place the resulting file (here, we're putting it with the library) DESTINATION ${libcaosdb_LIB_DEST} ) # install(FILES ...) simply puts files in a certain place with certain # properties. We're just copying them to the desired place here. install(FILES ${libcaosdb_INC} DESTINATION ${libcaosdb_INCLUDE_DEST})