Setting up Vulkan, glfw and spdlog correctly using CMake and conan package manager - cmake

I'm working on a renderer for Vulkan API. I have big trouble setting up the project correctly using CMake and conan package manager. Let's take a look at my conanfile.py for dependency setup:
from conans import ConanFile, CMake
class InexorConan(ConanFile):
settings = (
"os",
"compiler",
"build_type",
"arch"
)
requires = (
"benchmark/1.5.0",
"glm/0.9.9.7",
"gtest/1.10.0",
"spdlog/1.5.0",
"glfw/3.3.2#bincrafters/stable",
"toml11/3.1.0",
"imgui/1.75",
"assimp/5.0.1",
"enet/1.3.14 "
)
generators = "cmake"
default_options = {
}
def imports(self):
# Copies all dll files from packages bin folder to my "bin" folder (win)
self.copy("*.dll", dst="bin", src="bin")
# Copies all dylib files from packages lib folder to my "lib" folder (macosx)
self.copy("*.dylib*", dst="lib", src="lib") # From lib to lib
# Copies all so files from packages lib folder to my "lib" folder (linux)
self.copy("*.so*", dst="lib", src="lib") # From lib to lib
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
All the conan setup is working correctly, as can be seen by CMake output:
Sadly, there is no conan setup for Vulkan API. So I am using some code of Sascha Willem's github repository. My CMake file looks like this:
cmake_minimum_required(VERSION 3.4)
project(inexor-vulkan-renderer)
file(GLOB_RECURSE source_files
"src/*.hpp"
"src/*.cpp"
)
# Use the folder structure in source code directory as project structure in Visual Studio.
function(assign_source_group)
foreach(source_files IN ITEMS ${ARGN})
if (IS_ABSOLUTE "${source_files}")
file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${source_files}")
else()
set(_source_rel "${source_files}")
endif()
get_filename_component(_source_path "${_source_rel}" PATH)
string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
source_group("${_source_path_msvc}" FILES "${source_files}")
endforeach()
endfunction(assign_source_group)
# Use CMake to find Vulkan SDK.
if (NOT CMAKE_VERSION VERSION_LESS 3.7.0)
message(STATUS "Using module to find Vulkan")
find_package(Vulkan)
endif()
# Dependency setup via conan.
# Download conan executer in case it does not exists.
if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.14/conan.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/conan.cmake")
endif()
# Execute conan build instructions.
include(${CMAKE_CURRENT_BINARY_DIR}/conan.cmake)
conan_cmake_run(CONANFILE conanfile.py
BASIC_SETUP
BUILD outdated
PROFILE default
PROFILE_AUTO build_type
KEEP_RPATHS
)
# Use the folder structure in source code directory as project structure in Visual Studio.
assign_source_group(${source_files})
add_executable(inexor-vulkan-renderer src/main.cpp ${source_files})
target_link_libraries(inexor-vulkan-renderer PUBLIC ${Vulkan_LIBS} ${CONAN_LIBS})
# Use C++17!
target_compile_features(inexor-vulkan-renderer PRIVATE cxx_std_17)
IF(WIN32)
target_compile_definitions(inexor-vulkan-renderer PRIVATE VK_USE_PLATFORM_WIN32_KHR)
ENDIF()
target_include_directories(inexor-vulkan-renderer PRIVATE Vulkan::Vulkan)
target_link_libraries(inexor-vulkan-renderer Vulkan::Vulkan)
When I generate a Visual Studio project file with this setup, I have to add glfw3.lib, spdlogd.lib and fmtd.lib to the project manually. Other repositories like the official Vulkan samples take a more traditional approachand just paste the libs directly into the repo folders. I don't want to do this since I want conan to work for me. I've been trying to fix this problem for 6 months now.
Why does CMake not link the needed libraries although conan can find them?
Thanks you.

I found the solution: There is a function called conan_target_link_libraries which must be used instead of target_link_libraries. Thanks for the answer.

Related

CMake: Can I wrap an ExternalProject in some object that I can just link to my target?

I'm including this library as an external project. Based on the documentation, with some small tweaks, I have this:
# LIEF dependency
# ===========================
set(LIEF_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/LIEF")
set(LIEF_INSTALL_DIR "${LIEF_PREFIX}")
set(LIEF_INCLUDE_DIRS "${LIEF_PREFIX}/include")
# LIEF static library
set(LIEF_LIBFILE
"${LIEF_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}LIEF${CMAKE_STATIC_LIBRARY_SUFFIX}")
# URL of the LIEF repo (Can be your fork)
set(LIEF_GIT_URL "https://github.com/lief-project/LIEF.git")
# LIEF's version to be used (can be 'master')
set(LIEF_VERSION 0.11.5)
# LIEF compilation config
set(LIEF_CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DLIEF_DOC=off
-DLIEF_PYTHON_API=off
-DLIEF_EXAMPLES=off
-DLIEF_OAT=off
-DLIEF_DEX=off
-DLIEF_VDEX=off
-DLIEF_ART=off
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
)
# Specify MSVCRT on MSVC
if(MSVC)
list(APPEND ${LIEF_CMAKE_ARGS} -DLIEF_USE_CRT_RELEASE=MT)
list(APPEND ${LIEF_CMAKE_ARGS} -DLIEF_USE_CRT_DEBUG=MTd)
endif()
# External project
ExternalProject_Add(LIEF_extproj
PREFIX "${LIEF_PREFIX}"
GIT_REPOSITORY ${LIEF_GIT_URL}
GIT_TAG ${LIEF_VERSION}
INSTALL_DIR ${LIEF_INSTALL_DIR}
CMAKE_ARGS ${LIEF_CMAKE_ARGS}
BUILD_BYPRODUCTS ${LIEF_LIBFILE}
UPDATE_COMMAND ""
)
However, the original docs simply included the directories and linked separately. Can I somehow wrap these into a single target, where if I link to that target I get everything from that library?
EDIT:
My current attempt at setting up an imported target is this:
add_library(LIEF_depimpl STATIC IMPORTED)
set_target_properties(LIEF_depimpl PROPERTIES
IMPORTED_LOCATION ${LIEF_LIBFILE}
INTERFACE_INCLUDE_DIRECTORIES ${LIEF_INCLUDE_DIRS}
)
add_dependencies(LIEF_depimpl LIEF_extproj)
When I use target_link_libraries() to link LIEF against my project, CMake generates successfully, but then I get an error in the generated makefile.
add_executable(testapp lief-test.cpp)
...
# Link the executable with LIEF
target_link_libraries(testapp PUBLIC ${LIEF_depimpl})
The syntax for target_link_libraries() isn't what I thought it was. The dependency variable should not be expanded, like this:
target_link_libraries(testapp PUBLIC LIEF_depimpl)
In addition, CMake will throw an error if it can't find the include directories for an external project, so you should create that folder in your CMake file like so:
set(LIEF_INCLUDE_DIRS "${LIEF_PREFIX}/include")
file(MAKE_DIRECTORY ${LIEF_INCLUDE_DIRS})

Why my <package>-config.cmake have <package>_include_dir and <package>_librairies empty

I am trying to make a cross-platform CMake for my project (Windows and Linux).
I need to use external libraries (yaml-cpp). On Linux, I just had to do an apt get and use find_package. But on Windows, I need to append the CMAKE_MODULE_PATH in order for my program to find the yaml-cpp-config.cmake.
So I start by installing yaml-cpp (https://github.com/jbeder/yaml-cpp) with CMake GUI 3.16 and mingw32 (mingw32-make install).
I have tried the library on a hello world project, and it works fine.
cmake_minimum_required(VERSION 3.1)
project (yaml_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
message (STATUS "Yaml-cpp include = $ENV{YAML_CPP_INCLUDE_DIR}")
message (STATUS "Yaml-cpp library = $ENV{YAML_CPP_LIBRARIES}")
include_directories ($ENV{YAML_CPP_INCLUDE_DIR})
add_executable(yaml_test main.cpp)
target_link_libraries(yaml_test $ENV{YAML_CPP_LIBRARIES})
But now, I want to include the library in my project and use find_package. But the yaml-cpp-config.cmake looks like this:
# - Config file for the yaml-cpp package
# It defines the following variables
# YAML_CPP_INCLUDE_DIR - include directory
# YAML_CPP_LIBRARIES - libraries to link against
# Compute paths
get_filename_component(YAML_CPP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(YAML_CPP_INCLUDE_DIR "")
# Our library dependencies (contains definitions for IMPORTED targets)
include("${YAML_CPP_CMAKE_DIR}/yaml-cpp-targets.cmake")
# These are IMPORTED targets created by yaml-cpp-targets.cmake
set(YAML_CPP_LIBRARIES "")
The YAML_CPP_INCLUDE_DIR and YAML_CPP_LIBRARIES variables are empty, and even if CMake found yaml-cpp-config.cmake, It doesn't work. So what do I have missing in the installation of yaml-cpp? Should I have set the paths by hand?
The absence of definition of YAML_CPP_INCLUDE_DIR and YAML_CPP_LIBRARIES variables is the issue with the yaml-cpp project which is already reported here.
Instead of variables described in this config file, use target yaml-cpp:
add_executable(yaml_test main.cpp)
# This provides both include directories and libraries.
target_link_libraries(yaml_test yaml-cpp)
Linking with an IMPORTED target (yaml-cpp in this case) is known as CMake "modern way".

Include the CGAL shared libraries to deploy a CGAL program

I'm building a C++ program using CGAL, and I'm writing CMake install rules to deploy said program so that I can CPack the result and the end user doesn't have to install CGAL or any of its dependencies to use it. In order to do that, I need to include every shared library (DLLs on Windows etc) but I can't find a CMake variable that lets me do that. I searched around in the CGAL repo, but no luck. I tried using ${CGAL_LIBRARIES} but those don't give paths, and it doesn't seem like ${CGAL_LIBRARIES_DIRS} is a thing.
My current CMakeLists is based off the one generated by the dedicated CGAL script :
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
project( MeshCleaner )
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# CGAL and its components
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
include_directories( BEFORE include )
# include for local package
# Creating entries for target: meshCleaner
# ############################
add_executable( ${PROJECT_NAME} main.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS ${PROJECT_NAME} )
# Link the executable to CGAL and third-party libraries
target_link_libraries(${PROJECT_NAME} ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
install(TARGETS ${PROJECT_NAME} DESTINATION . COMPONENT Libraries)
message(${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
if(WIN32)
set(CMAKE_INSTALL_OPENMP_LIBRARIES TRUE)
include(InstallRequiredSystemLibraries)
endif()
include(${PROJECT_NAME}CPack)
The simplest way to "fix" that issue is to use CGAL as header-only. Please check the manual. Here is the direct link to the installation manual of CGAL-4.14, section "Header-only with CMake Configuration".

Create CMake/CPack <Library>Config.cmake for shared library

I have the simplest possible c-library which builds and is packed using the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project (libfoo C)
add_library(foo SHARED impl.c)
target_link_libraries(foo)
install(TARGETS foo LIBRARY DESTINATION lib/)
install(FILES public_header.h DESTINATION include/libfoo)
set(CPACK_GENERATOR "TGZ")
include(CPack)
Working example is located here: https://github.com/bjarkef/cmake-simple/tree/master/libfoo
I execute mkdir -p build; (cd build/; cmake ../; make all package;) to build a .tar.gz package with the compiled shared library along with its public header file. This is all working fine.
Now I wish to modify the CMakeLists.txt to create the FooConfig.cmake and FooConfigVersion.cmake files needed for CMake find_package in a different project to find the foo library. How do I do this?
I have discovered I should used the CMakePackageConfigHelpers: configure_package_config_file and write_basic_package_version_file, and I should create a FooLibraryConfig.cmake.in file. However I cannot figure out how to put it all together.
Note that it is important the the resulting .cmake files only contains relative paths.
I have cmake module included in the top level CmakeList.txt:
# Generate and install package config files
include(PackageConfigInstall)
Within the generic PackageConfigInstall.cmake file, the config files are created from the cmake.in files, and installed. This module can be reused for other packages.
include(CMakePackageConfigHelpers)
# Generate package config cmake files
set(${PACKAGE_NAME}_LIBRARY_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PACKAGE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
configure_package_config_file(${PACKAGE_NAME}-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_DIR}/${PACKAGE_NAME}
PATH_VARS LIB_INSTALL_DIR INCLUDE_INSTALL_DIR APP_INCLUDE_INSTALL_DIR )
configure_file(${PACKAGE_NAME}-config-version.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake #ONLY)
# Install package config cmake files
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake
DESTINATION
${CMAKE_INSTALL_DIR}/${PACKAGE_NAME}
COMPONENT
devel
)
You'll need a package file for your library, such as your_lib-config.cmake.in, which will become your_lib-config.cmake. This will contain the include and library variables that can be used.
get_filename_component(YOUR_LIB_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
# flag required by CMakePackageConfigHelpers
#PACKAGE_INIT#
set_and_check(YOUR_LIB_INCLUDE_DIR #PACKAGE_YOUR_LIB_INCLUDE_INSTALL_DIR#/hal)
set_and_check(YOUR_LIB_LIBRARY #PACKAGE_LIB_INSTALL_DIR#/#CMAKE_STATIC_LIBRARY_PREFIX##PROJECT_NAME_LIB##CMAKE_STATIC_LIBRARY_SUFFIX#)
set_and_check(YOUR_LIB_LIBRARIES #PACKAGE_LIB_INSTALL_DIR#/#CMAKE_STATIC_LIBRARY_PREFIX##PROJECT_NAME_LIB##CMAKE_STATIC_LIBRARY_SUFFIX#)
You'll also want a config-version.cmake.in file like this:
set(PACKAGE_VERSION #PACKAGE_VERSION#)
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
There's quite a bit to the packaging scripts to get it all to work just right. I went through a lot of trial and error to finally get something that works on different targets (both linux server and embedded target). I might have left something out, so please just comment and I'll update answer.

Issues Configuring CLion, Cmake, and SFML

I am currently trying to configure my Cmake file to include the SFML libraries.
My CMakeLists.txt. I'm using OS X Yosemite if that matter at all.
cmake_minimum_required(VERSION 2.8.4)
project(SFMLTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=lib++")
set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})
#Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.2 REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${main.cpp} ${SFML_Libraries})
endif()
and the error I am currently getting is
Error:By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has asked
CMake to find a package configuration file provided by "SFML", but CMake did notfind one.
Could not find a package configuration file provided by "SFML" (requested version 2.2)
with any of the following names:
SFMLConfig.cmake sfml-config.cmake
Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or
set "SFML_DIR"to a directory containing one of the above files.
If "SFML" provides a separate development package or SDK, be sure it has been installed.
my FindSFML.cmake is located at
/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules
Your question says the full path to the find module is
/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules/FindSFML.cmake
But you're adding ${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules to your CMAKE_MODULE_PATH. ${CMAKE_SOURCE_DIR} is the absolute path to the directory containing the top-level CMakeLists.txt file. If you know the absolute path to the module file you want to include, you should most certainly not prefix it with the source tree path. Just change the line to this:
set(CMAKE_MODULE_PATH "/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})
Also note that since you have REQUIRED specified among the arguments to find_package(), CMake will terminate with an error if the package cannot be found. Having if(SFML_FOUND) is therefore pointless.
This solution is perfect: https://oxymeos.shost.ca/article.php?about=work_with_the_SFML_in_CLion. I recommend it! It worked for me
You create a folder named "cmake_modules" at the root of the project and you place in this folder the "FindSFML.cmake" file (on Windows and Mac OS X: "[Your_SFML Location]/cmake/Modules/FindSFML.cmake", and on Linux: "[Your_SFML_location]/share/SFML/cmake/Modules/FindSFML.cmake".
The CMake configuration file (CMakeLists.txt) is then presented in this form:
cmake_minimum_required(VERSION 3.7)
project([your_project])
# Define the source and the executable
set(EXECUTABLE_NAME "[name_executable]")
add_executable(${EXECUTABLE_NAME} [project_files])
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()