SlideShare a Scribd company logo
Mateusz Pusz
September 24, 2018
GIT, CMAKE, CONAN
HOW TO SHIP AND REUSE OUR C++ PROJECTS?
VERSION CONTROL SYSTEM
BUILDING
PACKAGE MANAGEMENT
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
2
VERSION CONTROL SYSTEM git
BUILDING
PACKAGE MANAGEMENT
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
2
VERSION CONTROL SYSTEM git
BUILDING CMake
PACKAGE MANAGEMENT
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
2
VERSION CONTROL SYSTEM git
BUILDING CMake
PACKAGE MANAGEMENT None
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
2
VERSION CONTROL SYSTEM git
BUILDING CMake
PACKAGE MANAGEMENT None
• Please do not partition our C++ development environment even more
• Other tools may sometimes be better at one aspect but probably are worse at others
• Until a strong game-changer appears on the market please use above tools
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
2
VERSION CONTROL SYSTEM git
BUILDING CMake
PACKAGE MANAGEMENT None
• Please do not partition our C++ development environment even more
• Other tools may sometimes be better at one aspect but probably are worse at others
• Until a strong game-changer appears on the market please use above tools
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE MOST COMMON C++ TOOLSET
Conan is a strong contender to become the C++ Package Manager
2
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
2 External source code as git submodules + CMake add_subdirectory()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
2 External source code as git submodules + CMake add_subdirectory()
3 CMake ExternalProject_Add() + CMake add_subdirectory()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
2 External source code as git submodules + CMake add_subdirectory()
3 CMake ExternalProject_Add() + CMake add_subdirectory()
4 Downloading and installing each dependency + CMake find_package()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
2 External source code as git submodules + CMake add_subdirectory()
3 CMake ExternalProject_Add() + CMake add_subdirectory()
4 Downloading and installing each dependency + CMake find_package()
5 Usage of other languages' toolsets (i.e. maven, NuGet, ...)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory()
2 External source code as git submodules + CMake add_subdirectory()
3 CMake ExternalProject_Add() + CMake add_subdirectory()
4 Downloading and installing each dependency + CMake find_package()
5 Usage of other languages' toolsets (i.e. maven, NuGet, ...)
6 Dedicated C++ package managers (i.e. Conan)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS
3
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
add_subdirectory() FOR DEPS?
4
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
add_subdirectory() FOR DEPS?
5
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
add_subdirectory() FOR DEPS?
6
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
add_subdirectory() FOR DEPS? PLEASE DON'T!
7
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
add_subdirectory() FOR DEPS? PLEASE DON'T!
Handling dependencies as subdirectories does not scale!
7
Not a build system!
Cross-platform C++ build generator
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CMake
8
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local
cmake --build .
ctest -VV
cmake --build . --target install
cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=~/.local
cmake --build . --config Release
ctest -VV -C Release
cmake --build . --target install --config Release
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL CMAKE WORKFLOW
GCC
VISUAL STUDIO
9
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake
10
Build flags don't scale!
• Every change in public flags has to be propagated upwards
• Not possible to maintain flags on a large scale
• Di erent targets may require conflicting flag values
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake
11
Build flags don't scale!
• Every change in public flags has to be propagated upwards
• Not possible to maintain flags on a large scale
• Di erent targets may require conflicting flag values
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake
Modern CMake is about Targets and Properties
11
EXECUTABLES add_executable
SHARED LIBRARIES add_library(SHARED)
STATIC LIBRARIES add_library(STATIC)
OBJECT LIBRARIES add_library(OBJECT)
INTERFACE LIBRARIES add_library(INTERFACE)
ALIAS LIBRARIES add_library(ALIAS)
IMPORTED LIBRARIES add_library(IMPORTED [GLOBAL])
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CMake TARGET TYPES
12
EXECUTABLES add_executable
SHARED LIBRARIES add_library(SHARED)
STATIC LIBRARIES add_library(STATIC)
OBJECT LIBRARIES add_library(OBJECT)
INTERFACE LIBRARIES add_library(INTERFACE)
ALIAS LIBRARIES add_library(ALIAS)
IMPORTED LIBRARIES add_library(IMPORTED [GLOBAL])
• If no type is given explicitly to add_library() the type is STATIC or SHARED based on whether the
current value of the variable BUILD_SHARED_LIBS is ON
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CMake TARGET TYPES
12
• Available since version 2.8.12 (Oct 2013)
• Specified by target_xxx() commands
target_link_libraries(<target>
<PRIVATE|PUBLIC|INTERFACE> <item>...
[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake: MODULAR DESIGN
13
• Available since version 2.8.12 (Oct 2013)
• Specified by target_xxx() commands
target_link_libraries(<target>
<PRIVATE|PUBLIC|INTERFACE> <item>...
[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
NEEDED BY ME NOT NEEDED BY ME
NEEDED BY DEPENDERS PUBLIC INTERFACE
NOT NEEDED BY DEPENDERS PRIVATE :-)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake: MODULAR DESIGN
13
• Available since version 2.8.12 (Oct 2013)
• Specified by target_xxx() commands
target_link_libraries(<target>
<PRIVATE|PUBLIC|INTERFACE> <item>...
[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
NEEDED BY ME NOT NEEDED BY ME
NEEDED BY DEPENDERS PUBLIC INTERFACE
NOT NEEDED BY DEPENDERS PRIVATE :-)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN CMake: MODULAR DESIGN
INTERFACE and PUBLIC dependencies are transitive while PRIVATE are not
13
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PHYSICAL DESIGN: OLD CMAKE STYLE
14
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PHYSICAL DESIGN: OLD CMAKE STYLE
Linking diagram only. Not useful to reveal real design problems.
14
• More than a linking diagram
• Logical dependencies included
• PRIVATE and PUBLIC dependencies make
di erence
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PHYSICAL DESIGN: MODERN CMAKE
15
• More than a linking diagram
• Logical dependencies included
• PRIVATE and PUBLIC dependencies make
di erence
• Now we see real design problems...
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PHYSICAL DESIGN: MODERN CMAKE
15
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
NO CYCLIC PHYSICAL DEPENDENCIES!
16
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
ALIAS TARGETS
17
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
# find_package(MyLibrary CONFIG REQUIRED)
target_link_libraries(MyLibraryTest
PUBLIC
MyCompany::MyLibrary
GTest::Main
)
• Unifies with find_package() target naming
• :: has to be followed with Target name (prevents typos)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
ALIAS TARGETS
17
target_compile_definitions(foo
PRIVATE
"VERBOSITY=$<IF:$<BOOL:${VERBOSE}>,30,10>")
• Use the $<> syntax
• Not evaluated by the command interpreter
• Evaluated during build system generation
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
18
add_executable(hello main.cpp)
if(CMAKE_BUILD_TYPE STREQUAL DEBUG)
target_sources(hello PRIVATE helper_debug.cpp)
else()
target_sources(hello PRIVATE helper_release.cpp)
endif()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
BAD
19
add_executable(hello main.cpp)
if(CMAKE_BUILD_TYPE STREQUAL DEBUG)
target_sources(hello PRIVATE helper_debug.cpp)
else()
target_sources(hello PRIVATE helper_release.cpp)
endif()
add_executable(hello main.cpp
$<IF:$<CONFIG:Debug>:helper_debug.cpp,helper_release.cpp>)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
BAD
GOOD
19
add_executable(hello main.cpp)
if(CMAKE_BUILD_TYPE STREQUAL DEBUG)
target_sources(hello PRIVATE helper_debug.cpp)
else()
target_sources(hello PRIVATE helper_release.cpp)
endif()
add_executable(hello main.cpp
$<IF:$<CONFIG:Debug>:helper_debug.cpp,helper_release.cpp>)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
BAD
GOOD
Never use CMAKE_BUILD_TYPE in if()
19
The library interface may change during installation. Use
BUILD_INTERFACE and INSTALL_INTERFACE generator expression
filters.
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
20
The library interface may change during installation. Use
BUILD_INTERFACE and INSTALL_INTERFACE generator expression
filters.
target_include_directories(Foo PUBLIC
$<BUILD_INTERFACE:${Foo_BINARY_DIR}/include>
$<BUILD_INTERFACE:${Foo_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
GENERATOR EXPRESSIONS
20
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
# dependencies
find_package(Foo 1.0 REQUIRED)
# library definition
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
target_compile_features(MyLibrary PUBLIC cxx_std_17)
target_include_directories(MyLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(MyLibrary PRIVATE Foo::Foo)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY EXAMPLE
21
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
# dependencies
find_package(Foo 1.0 REQUIRED)
# library definition
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
target_compile_features(MyLibrary PUBLIC cxx_std_17)
target_include_directories(MyLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(MyLibrary PRIVATE Foo::Foo)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY EXAMPLE
21
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
# dependencies
find_package(Foo 1.0 REQUIRED)
# library definition
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
target_compile_features(MyLibrary PUBLIC cxx_std_17)
target_include_directories(MyLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(MyLibrary PRIVATE Foo::Foo)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY EXAMPLE
21
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
# dependencies
find_package(Foo 1.0 REQUIRED)
# library definition
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
target_compile_features(MyLibrary PUBLIC cxx_std_17)
target_include_directories(MyLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(MyLibrary PRIVATE Foo::Foo)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY EXAMPLE
21
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
# dependencies
find_package(Foo 1.0 REQUIRED)
# library definition
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
target_compile_features(MyLibrary PUBLIC cxx_std_17)
target_include_directories(MyLibrary PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(MyLibrary PRIVATE Foo::Foo)
add_library(MyCompany::MyLibrary ALIAS MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY EXAMPLE
Avoid custom variables in the arguments of project commands
21
• Standalone library definition and installation
• Does not change compiler's warnings!
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
FILES ORGANIZATION
MYLIBRARY/SRC/CMAKELISTS.TXT
22
• Standalone library definition and installation
• Does not change compiler's warnings!
• Standalone unit tests definition
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
FILES ORGANIZATION
MYLIBRARY/SRC/CMAKELISTS.TXT
MYLIBRARY/TEST/CMAKELISTS.TXT
22
• Standalone library definition and installation
• Does not change compiler's warnings!
• Standalone unit tests definition
• Simple project wrapper
• Entry point for development
• src and test subdirectories added with
add_subdirectory()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
FILES ORGANIZATION
MYLIBRARY/SRC/CMAKELISTS.TXT
MYLIBRARY/TEST/CMAKELISTS.TXT
MYLIBRARY/CMAKELISTS.TXT
22
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
# add project code
add_subdirectory(src)
# add unit tests
enable_testing()
add_subdirectory(test)
• Useful for project development
– no need to install the library in order to run unit tests
• Entry point for IDEs like CLion or Visual Studio
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WRAPPER: LOCAL DEVELOPMENT ENTRY POINT
23
cmake_minimum_required(VERSION 3.8)
project(MyLibraryTests)
# dependencies
enable_testing()
find_package(GTest MODULE REQUIRED)
if(NOT TARGET MyCompany::MyLibrary)
find_package(MyLibrary CONFIG REQUIRED)
endif()
# target definition
add_executable(MyLibraryTests tests_source.cpp)
target_link_libraries(MyLibraryTests
PRIVATE
MyCompany::MyLibrary
GTest::Main
)
add_test(NAME MyLibrary.UnitTests
COMMAND MyLibraryTests
)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MODERN LIBRARY USAGE: TESTS
24
Done?
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
COMPILED, BUILT, TESTED
25
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
DON'T BE ASOCIAL!
26
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
27
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
find_package(Foo 1.0 REQUIRED)
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
CMAKELISTS.TXT
28
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
find_package(Foo 1.0 REQUIRED)
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
install(TARGETS MyLibrary EXPORT MyLibraryTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include)
install(EXPORT MyLibraryTargets
DESTINATION lib/cmake/MyLibrary
FILE MyLibraryTargets.cmake
NAMESPACE MyCompany::)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
CMAKELISTS.TXT
28
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
find_package(Foo 1.0 REQUIRED)
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
install(TARGETS MyLibrary EXPORT MyLibraryTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include)
install(EXPORT MyLibraryTargets
DESTINATION lib/cmake/MyLibrary
FILE MyLibraryTargets.cmake
NAMESPACE MyCompany::)
install(DIRECTORY include/MyLibrary
DESTINATION include)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
CMAKELISTS.TXT
28
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
find_package(Foo 1.0 REQUIRED)
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(MyLibraryConfigVersion.cmake
COMPATIBILITY SameMajorVersion)
install(FILES MyLibraryConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/MyLibraryConfigVersion.cmake
DESTINATION lib/cmake/MyLibrary)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
CMAKELISTS.TXT
29
cmake_minimum_required(VERSION 3.8)
project(MyLibrary VERSION 0.0.1)
find_package(Foo 1.0 REQUIRED)
add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(MyLibraryConfigVersion.cmake
COMPATIBILITY SameMajorVersion)
install(FILES MyLibraryConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/MyLibraryConfigVersion.cmake
DESTINATION lib/cmake/MyLibrary)
include(CMakeFindDependencyMacro)
find_dependency(Foo 1.0)
include("${CMAKE_CURRENT_LIST_DIR}/MyLibraryTargets.cmake")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
EXPORT YOUR LIBRARY INTERFACE
CMAKELISTS.TXT
MYLIBRARYCONFIG.CMAKE
29
• Create and install the package
mkdir src/build
cd src/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local
cmake --build . --target install
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE TESTING WORKFLOW
30
• Create and install the package
mkdir src/build
cd src/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local
cmake --build . --target install
• Compile and run tests using the installed library
mkdir test/build
cd test/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local
ctest -VV
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE TESTING WORKFLOW
30
• Build each repository in isolation, generate and install its binaries along with a CMake config file
• For each project that has dependencies, use find_package() to load the config file and use the library
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PURE CMAKE: DEPENDENCIES THE WRONG WAY
PROCESS
31
• Build each repository in isolation, generate and install its binaries along with a CMake config file
• For each project that has dependencies, use find_package() to load the config file and use the library
• Updating a program implies recompiling its package and then every one of its dependers manually
• It doesn't scale (many levels of dependencies, many configurations, ...)
• What about supporting di erent versions?
– Release, Debug, RelWithDebInfo, ...
– di erent compilers (gcc, clang, ...), compiler versions, C++ libraries (libc++ vs libstdc++)
– di erent runtime libraries
– di erent package configurations (no exceptions, shared lib, ...)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PURE CMAKE: DEPENDENCIES THE WRONG WAY
PROCESS
PROBLEMS
31
• One build process builds the project and all dependencies
• Only required dependencies are being rebuilt
– reuse of prebuilt binaries if available and up-to-date
• No need to manually download, build and install the dependencies
• Possibility to use our own versions of dependencies (ZLib, Boost, etc) instead of using system versions
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT WE WANT?
32
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER?
33
• Decentralized servers
– OSS, corporate, private, etc
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER?
33
• Decentralized servers
– OSS, corporate, private, etc
• Central cache on local PC
– sharing of the dependencies data among di erent projects
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER?
33
• Decentralized servers
– OSS, corporate, private, etc
• Central cache on local PC
– sharing of the dependencies data among di erent projects
• Support for
– sources only (do not use prebuilt binaries even if available)
– sources + prebuilt binaries (improves build performance)
– binaries only (for closed source projects and development tools)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER?
33
• Decentralized servers
– OSS, corporate, private, etc
• Central cache on local PC
– sharing of the dependencies data among di erent projects
• Support for
– sources only (do not use prebuilt binaries even if available)
– sources + prebuilt binaries (improves build performance)
– binaries only (for closed source projects and development tools)
• O line use
– so I can start a new project with popular dependencies in a plane flying to CppCon ;-)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER?
33
conan.ioC++ package manager
• Conan is OSS, with a MIT license
• Decentralized package manager with
a client-server architecture
• The servers are just a package storage
– they do not build nor create the packages
• The packages are created by the client
– packaging of prebuilt binaries
– building from sources if needed
• Portable to any platform supporting Python
• Works with any build system
• Uses Python as its scripting language
• Easy hosting in a cloud or on a local server
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN
34
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN CLIENT-SERVER ARCHITECTURE
35
• Console/terminal application
• Package creation and consumption
• Local cache for package storage
– allows o line work
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN CLIENT-SERVER ARCHITECTURE
CONAN CLIENT
36
• Console/terminal application
• Package creation and consumption
• Local cache for package storage
– allows o line work
• Provides public and free hosting service for OSS
conan packages
• Account is only needed to upload packages
(anonymous read access)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN CLIENT-SERVER ARCHITECTURE
CONAN CLIENT
JFROG BINTRAY
36
• Console/terminal application
• Package creation and consumption
• Local cache for package storage
– allows o line work
• Quite simple TCP server
• User can run it as a daemon or a service
• Provides public and free hosting service for OSS
conan packages
• Account is only needed to upload packages
(anonymous read access)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN CLIENT-SERVER ARCHITECTURE
CONAN CLIENT CONAN SERVER
JFROG BINTRAY
36
• Console/terminal application
• Package creation and consumption
• Local cache for package storage
– allows o line work
• Quite simple TCP server
• User can run it as a daemon or a service
• Provides public and free hosting service for OSS
conan packages
• Account is only needed to upload packages
(anonymous read access)
• O ers conan repositories
• More powerful than conan server (WebUI,
multiple auth protocols, High Availability, ...)
• JFrog Artifactory Community Edition for C/C++
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN CLIENT-SERVER ARCHITECTURE
CONAN CLIENT CONAN SERVER
JFROG BINTRAY JFROG ARTIFACTORY
36
• Moderated, curated and well-maintained packages
• Place in which you can share your packages with the community
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
POPULAR CONAN REMOTES ON BINTRAY
CONAN-CENTER
37
• Moderated, curated and well-maintained packages
• Place in which you can share your packages with the community
• The Bincra ers team builds binary so ware packages for the OSS community
• Contains a wide and growing variety of Conan packages from contributors
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
POPULAR CONAN REMOTES ON BINTRAY
CONAN-CENTER
BINCRAFTERS
37
• Moderated, curated and well-maintained packages
• Place in which you can share your packages with the community
• The Bincra ers team builds binary so ware packages for the OSS community
• Contains a wide and growing variety of Conan packages from contributors
• Created by Conan developers
• An incubator for maturing packages
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
POPULAR CONAN REMOTES ON BINTRAY
CONAN-CENTER
BINCRAFTERS
CONAN-COMMUNITY
37
• Usually project/library name
• Owner of the package version
• Namespace that allows di erent users to have
their own packages for the same library with
the same name
• Any string
• Allows di erent packages for the same library
• Usually denote the maturity of a package
("stable", "testing")
package_name/package_version@user/channel
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE IDENTIFIER
PACKAGE_NAME
USER
PACKAGE_VERSION
CHANNEL
38
• gtest/1.8.0@bincra ers/stable
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
39
• gtest/1.8.0@bincra ers/stable
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
40
• List packages in the local cache
conan search
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SEARCHING FOR PACKAGES
41
• List packages in the local cache
conan search
• List all gtest packages in the conan-center
conan search gtest -r conan-center
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SEARCHING FOR PACKAGES
41
• List packages in the local cache
conan search
• List all gtest packages in the conan-center
conan search gtest -r conan-center
• Inspect binary package details
conan search gtest/1.8.1@bincrafters/stable
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SEARCHING FOR PACKAGES
41
• List packages in the local cache
conan search
• List all gtest packages in the conan-center
conan search gtest -r conan-center
• Inspect binary package details
conan search gtest/1.8.1@bincrafters/stable
• Generate table of binary packages of gtest/1.8.1@bincrafters/stable on conan-center
conan search gtest/1.8.1@bincrafters/stable -r conan-center --table=conan_matrix.html
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SEARCHING FOR PACKAGES
41
conan search gtest/1.8.1@bincrafters/stable
Existing packages for recipe gtest/1.8.1@bincrafters/stable:
Package_ID: a4062ec0208a59375ac653551e662b6cc469fe58
[options]
build_gmock: True
fPIC: True
shared: False
[settings]
arch: x86_64
build_type: Release
compiler: gcc
compiler.libcxx: libstdc++11
compiler.version: 7
os: Linux
Outdated from recipe: False
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE DETAILS
42
• Inspect your current project’s dependencies
conan info ..
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSPECTING DEPENDENCIES
43
• Inspect your current project’s dependencies
conan info ..
• Inspect dependencies of OpenSSL/1.1.0i@conan/stable
conan info OpenSSL/1.1.0i@conan/stable
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSPECTING DEPENDENCIES
43
• Inspect your current project’s dependencies
conan info ..
• Inspect dependencies of OpenSSL/1.1.0i@conan/stable
conan info OpenSSL/1.1.0i@conan/stable
• Generates a graph of dependencies of current project
conan info .. --graph=graph.html
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSPECTING DEPENDENCIES
43
OpenSSL/1.1.0i@conan/stable
ID: 75b541b6f810787a4fcee8cec9cfdbf95959366f
BuildID: None
Remote: conan-center=https://conan.bintray.com
URL: http://github.com/lasote/conan-openssl
License: The current OpenSSL licence is an 'Apache style' license: https://www.openssl.org/source/license.html
Recipe: Downloaded
Binary: Download
Binary remote: conan-center
Creation date: 2018-08-28 09:02:07
Required by:
PROJECT
Requires:
zlib/1.2.11@conan/stable
zlib/1.2.11@conan/stable
ID: 6ae331b72e7e265ca2a3d1d8246faf73aa030238
BuildID: None
Remote: conan-center=https://conan.bintray.com
URL: http://github.com/lasote/conan-zlib
License: http://www.zlib.net/zlib_license.html
Recipe: Cache
Binary: Cache
Binary remote: conan-center
Creation date: 2018-08-30 15:46:32
Required by:
OpenSSL/1.1.0i@conan/stable
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE DEPENDENCIES
44
• Installing specific package by hand
conan install gtest/1.8.1@bincrafters/stable -g cmake
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSTALLING DEPENDENCIES
45
• Installing specific package by hand
conan install gtest/1.8.1@bincrafters/stable -g cmake
• Usage of project-specific conanfile.txt or conanfile.py files
conan install ..
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSTALLING DEPENDENCIES
45
• Installing specific package by hand
conan install gtest/1.8.1@bincrafters/stable -g cmake
• Usage of project-specific conanfile.txt or conanfile.py files
conan install ..
• Optionally a profile name can be provided (otherwise profile named default is used)
conan install .. -pr vs2017
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSTALLING DEPENDENCIES
45
• Installing specific package by hand
conan install gtest/1.8.1@bincrafters/stable -g cmake
• Usage of project-specific conanfile.txt or conanfile.py files
conan install ..
• Optionally a profile name can be provided (otherwise profile named default is used)
conan install .. -pr vs2017
• Build missing packages from sources (if prebuilt ones are not available)
conan install .. -b missing
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
INSTALLING DEPENDENCIES
45
[requires]
Foo/1.0@my_channel/stable
gtest/1.8.1@bincrafters/stable
[options]
[generators]
cmake
• cmake generator creates conanbuildinfo.cmake file that defines
– CMake variables (module paths include paths, library names, ...)
– CMake helper functions (defining targets, configuring CMake environment, ...)
• Many di erent generators provided to support any build system
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN FILES
CONANFILE.TXT
46
[requires]
Foo/1.0@my_channel/stable
gtest/1.8.1@bincrafters/stable
[options]
[generators]
cmake
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
generators = "cmake"
• cmake generator creates conanbuildinfo.cmake file that defines
– CMake variables (module paths include paths, library names, ...)
– CMake helper functions (defining targets, configuring CMake environment, ...)
• Many di erent generators provided to support any build system
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN FILES
CONANFILE.TXT CONANFILE.PY
46
[requires]
Foo/1.0@my_channel/stable
gtest/1.8.1@bincrafters/stable
[options]
gtest:shared=True
[generators]
cmake
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
default_options = "gtest:shared=True"
generators = "cmake"
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SETTING PACKAGE OPTIONS
CONANFILE.TXT CONANFILE.PY
47
[requires]
Foo/1.0@my_channel/stable
gtest/1.8.1@bincrafters/stable
[options]
gtest:shared=True
[generators]
cmake
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
default_options = "gtest:shared=True"
generators = "cmake"
• Command line override
conan install .. -o gtest:shared=True
conan install .. -o *:shared=True
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SETTING PACKAGE OPTIONS
CONANFILE.TXT CONANFILE.PY
47
[requires]
Foo/1.0@my_channel/stable
gtest/1.8.1@bincrafters/stable
[options]
gtest:shared=True
[generators]
cmake
[imports]
bin, *.dll -> ./bin
lib, *.dylib* -> ./bin
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
default_options = "gtest:shared=True"
generators = "cmake"
def imports(self):
self.copy("*.dll", dst="bin", src="bin")
self.copy("*.dylib*", dst="bin", src="lib")
• Copies all *.dll files from packages bin folder to my ./bin folder
• Copies all *.dylib* files from packages lib folder to my ./bin folder
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
FIXING IMPORTS FOR SHARED LIBRARIES
CONANFILE.TXT CONANFILE.PY
48
from conans import ConanFile, CMake
class MyLibraryConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
generators = "cmake"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake.install()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MORE POWER WITH CONANFILE.PY
49
from conans import ConanFile, CMake
class MyLibraryConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = (
"Foo/1.0@my_channel/stable",
"gtest/1.8.1@bincrafters/stable"
)
generators = "cmake"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
cmake.install()
conan install . -pr vs2017 --install-folder build
conan build . --build-folder build
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MORE POWER WITH CONANFILE.PY
49
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"gtest/1.8.1@bincrafters/stable"
)
options = { "testing": [True, False] }
default_options = "testing=False"
generators = "cmake"
def requirements(self):
if self.options.testing:
self.requires("Foo/2.0@my_channel/testing")
else:
self.requires("Foo/1.0@my_channel/stable")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MORE POWER WITH CONANFILE.PY
50
from conans import ConanFile
class MyLibraryConan(ConanFile):
requires = (
"gtest/1.8.1@bincrafters/stable"
)
options = { "testing": [True, False] }
default_options = "testing=False"
generators = "cmake"
def requirements(self):
if self.options.testing:
self.requires("Foo/2.0@my_channel/testing")
else:
self.requires("Foo/1.0@my_channel/stable")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
MORE POWER WITH CONANFILE.PY
And many more features...
50
[settings]
setting=value
[options]
MyLib:shared=True
[env]
env_var=value
[build_requires]
Tool1/0.1@user/channel
• Stored in the default profile folder or anywhere in a project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PROFILES
51
conan profile show clang6
Configuration for profile clang6:
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=clang
compiler.version=6.0
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]
CC=/usr/bin/clang
CXX=/usr/bin/clang++
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PROFILES
52
conan profile show clang6
Configuration for profile clang6:
[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=clang
compiler.version=6.0
compiler.libcxx=libstdc++11
build_type=Release
[options]
[build_requires]
[env]
CC=/usr/bin/clang
CXX=/usr/bin/clang++
• Easy to override or extend the profile
conan install .. -pr clang6 -s build_type=Debug
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PROFILES
52
• Example of a Conan profile file distributed with the project source code
include(default)
[settings]
build_type=Debug
[options]
Poco:shared=True
Poco:enable_apacheconnector=False
OpenSSL:shared=True
conan install .. -pr=../debug_shared
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PROFILES
DEBUG_SHARED
53
1 The package recipe independent of the project source code repository
• packaging 3rd party libraries
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
54
1 The package recipe independent of the project source code repository
2 The package recipe inside the project source code repository
• packaging own library
• 2 approaches
– exporting snapshot of the source code together with the recipe
– exporting only the recipe and obtaining the source code from the project repository
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
55
1 The package recipe independent of the project source code repository
2 The package recipe inside the project source code repository
3 Packaging existing binaries
• only prebuilt 3rd party binaries are available
• artifacts already built and no need to rebuild
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
56
1 The package recipe independent of the project source code repository
2 The package recipe inside the project source code repository
3 Packaging existing binaries
4 Packaging of the development tools
• distribution of the development tools needed to build the project (i.e. CMake, nasm)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGES
57
• ./conanfile.py
– main recipe file used to create a package
• ./LICENSE
– licence of the Conan package source code
– not to be confused with a license of a
packaged project
• ./test_package
– simple package consumer project
• ./test_package/conanfile.py
– recipe of a testing project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
TYPICAL CONAN PACKAGE CONTENTS
58
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
# ...
• Basic package information
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
59
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
# ...
• The license of the packaged project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
60
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
url = "https://github.com/mpusz/conan-google-benchmark"
# ...
• Link to the Conan package repository (not the packaged project)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
61
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
url = "https://github.com/mpusz/conan-google-benchmark"
exports = ["LICENSE"]
# ...
• Copies a packaging code license to be stored within a package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
62
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
url = "https://github.com/mpusz/conan-google-benchmark"
exports = ["LICENSE"]
settings = "os", "arch", "compiler", "build_type"
# ...
• The configuration of the di erent binary packages
• Any change in those parameters will generate a di erent binary package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
63
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
url = "https://github.com/mpusz/conan-google-benchmark"
exports = ["LICENSE"]
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
default_options = ("shared=False", "exceptions=True", "lto=False")
# ...
• Package options and their default values
• Any change in those parameters will generate a di erent binary package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
64
class GoogleBenchmarkConan(ConanFile):
name = "google-benchmark"
version = "1.4.1"
description = "A microbenchmark support library"
license = "https://github.com/google/benchmark/blob/master/LICENSE"
url = "https://github.com/mpusz/conan-google-benchmark"
exports = ["LICENSE"]
settings = "os", "arch", "compiler", "build_type"
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
default_options = ("shared=False", "exceptions=True", "lto=False")
scm = {
"type": "git",
"url": "https://github.com/google/benchmark.git",
"revision": "v%s" % version
}
# ...
• Packaged project repository information
• Used to obtain project source code to build and pack
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE ATTRIBUTES
65
• Project-wide and common to all the dependencies in order for them to link together without issues
with the project being built
• Provided by the environment and cannot be defaulted in the recipe
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SETTINGS VS OPTIONS
SETTINGS
66
• Project-wide and common to all the dependencies in order for them to link together without issues
with the project being built
• Provided by the environment and cannot be defaulted in the recipe
• Package-specific configuration and each dependency can provide di erent set of options to fulfill their
configuration needs
• Default option values provided in the recipe file can be overridden by the user
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SETTINGS VS OPTIONS
SETTINGS
OPTIONS
66
• generators - not needed only if a package does not have any dependencies
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
67
• generators - not needed only if a package does not have any dependencies
• requires - package dependencies
class MyLibConan(ConanFile):
requires = (("Hello/[>1.0,<2.0]@user/testing"),
("Bye/2.1@coder/beta", "private"),
("Say/0.2@dummy/stable", "override"))
– private - not exposed to clients, non-transitive
– override - does not introduce a new dependency, overrides a version if it is a dependency already
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
67
• generators - not needed only if a package does not have any dependencies
• requires - package dependencies
class MyLibConan(ConanFile):
requires = (("Hello/[>1.0,<2.0]@user/testing"),
("Bye/2.1@coder/beta", "private"),
("Say/0.2@dummy/stable", "override"))
– private - not exposed to clients, non-transitive
– override - does not introduce a new dependency, overrides a version if it is a dependency already
– version ranges
>1.1,<2.1 # in such range
2.8 # equivalent to =2.8
~=3.0 # compatible, according to semver
>1.1 || 0.8 # conditions can be OR'ed
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
67
• build_requires - build-time only dependencies
– only needed to build a package from sources
– dev tools, compilers, build systems, code analyzers, testing libraries, etc.
– non-transitive
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
68
• build_requires - build-time only dependencies
– only needed to build a package from sources
– dev tools, compilers, build systems, code analyzers, testing libraries, etc.
– non-transitive
• exports - copies provided files to be stored in a package along with the recipe
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
68
• build_requires - build-time only dependencies
– only needed to build a package from sources
– dev tools, compilers, build systems, code analyzers, testing libraries, etc.
– non-transitive
• exports - copies provided files to be stored in a package along with the recipe
• exports_sources - creates a snapshot of the packaged project source code in the package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
68
• build_requires - build-time only dependencies
– only needed to build a package from sources
– dev tools, compilers, build systems, code analyzers, testing libraries, etc.
– non-transitive
• exports - copies provided files to be stored in a package along with the recipe
• exports_sources - creates a snapshot of the packaged project source code in the package
• homepage - the home web page of the library being packaged
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL ATTRIBUTES
68
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
cmake = CMake(self)
cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF"
cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF"
cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF"
cmake.configure()
return cmake
# ...
• Helper method to configure CMake
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
69
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
cmake = CMake(self)
cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF"
cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF"
cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF"
cmake.configure()
return cmake
# ...
• CMake helper automatically appends some definitions based on popular settings
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
70
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
cmake = CMake(self)
cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF"
cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF"
cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF"
cmake.configure()
return cmake
# ...
• Compiling unit tests makes the packaging longer and does not influence the binary package content
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
71
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
# ...
def build(self):
cmake = self._configure_cmake()
cmake.build()
# ...
• Configures and builds the project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
72
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
# ...
def build(self):
cmake = self._configure_cmake()
cmake.build()
def package(self):
cmake = self._configure_cmake()
cmake.install()
self.copy("license*", dst="licenses", ignore_case=True, keep_path=False)
# ...
• Copies packaged project binaries, CMake configuration files, and its license to the package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
73
class GoogleBenchmarkConan(ConanFile):
options = {
"shared": [True, False],
"exceptions": [True, False],
"lto": [True, False],
}
# ...
def _configure_cmake(self):
# ...
def build(self):
cmake = self._configure_cmake()
cmake.build()
def package(self):
cmake = self._configure_cmake()
cmake.install()
self.copy("license*", dst="licenses", ignore_case=True, keep_path=False)
def package_info(self):
self.cpp_info.libs = ["benchmark"]
# ...
• Defines the target that the consumer must link with when using this package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE RECIPE METHODS
74
• source() - retrieves the source code from external origin (git, *.tgz, ...)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
• build_requirements() - defines dependencies used when the package is built from sources
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
• build_requirements() - defines dependencies used when the package is built from sources
• imports() - copies files from the local store to client's project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
• build_requirements() - defines dependencies used when the package is built from sources
• imports() - copies files from the local store to client's project
• package_id() - creates a unique ID for the package
def package_id(self):
self.info.header_only()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
• build_requirements() - defines dependencies used when the package is built from sources
• imports() - copies files from the local store to client's project
• package_id() - creates a unique ID for the package
def package_id(self):
self.info.header_only()
• configure() - configures or constrains/validates the available options in a package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• source() - retrieves the source code from external origin (git, *.tgz, ...)
• requirements() - defines more advanced requirements logic
• build_requirements() - defines dependencies used when the package is built from sources
• imports() - copies files from the local store to client's project
• package_id() - creates a unique ID for the package
def package_id(self):
self.info.header_only()
• configure() - configures or constrains/validates the available options in a package
• config_options() - constrains the available options in a package, before they are given a value
def config_options(self):
if self.settings.os == 'Windows':
del self.options.fPIC
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
OTHER USEFUL METHODS
75
• test_package di ers from the library unit or integration tests (which should be more comprehensive)
• Packaging tests to verify that
– the package is properly created
– the package consumers will be able to link against it and reuse it
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE test_package DIRECTORY
76
from conans import ConanFile, CMake
class GoogleBenchmarkTestConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake_paths"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def imports(self):
self.copy("*.dll", dst="bin", src="bin")
self.copy("*.dylib*", dst="bin", src="lib")
self.copy('*.so*', dst='bin', src='lib')
def test(self):
cmake = CMake(self)
self.run("ctest -VV -C %s" % cmake.build_type)
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE test_package RECIPE
77
from conans import ConanFile, CMake
class GoogleBenchmarkTestConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake_paths"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def imports(self):
self.copy("*.dll", dst="bin", src="bin")
self.copy("*.dylib*", dst="bin", src="lib")
self.copy('*.so*', dst='bin', src='lib')
def test(self):
cmake = CMake(self)
self.run("ctest -VV -C %s" % cmake.build_type)
• requires attribute skipped as all needed information (e.g. project version) is automatically injected
by Conan during the package creation
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
THE test_package RECIPE
77
conan create . google-benchark/testing
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
conan create . google-benchark/testing
• Exports the conanfile.py and files specified by the exports_sources field into the local cache
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
conan create . google-benchark/testing
• Exports the conanfile.py and files specified by the exports_sources field into the local cache
• Installs the package, forcing it to be built from the sources
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
conan create . google-benchark/testing
• Exports the conanfile.py and files specified by the exports_sources field into the local cache
• Installs the package, forcing it to be built from the sources
• Creates a temporary ./test_package/build directory
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
conan create . google-benchark/testing
• Exports the conanfile.py and files specified by the exports_sources field into the local cache
• Installs the package, forcing it to be built from the sources
• Creates a temporary ./test_package/build directory
• Executes the conan install .., to install the requirements of the ./test_package/conanfile.py
and the packaged project
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
conan create . google-benchark/testing
• Exports the conanfile.py and files specified by the exports_sources field into the local cache
• Installs the package, forcing it to be built from the sources
• Creates a temporary ./test_package/build directory
• Executes the conan install .., to install the requirements of the ./test_package/conanfile.py
and the packaged project
• Builds and launches the example consuming application, calling the ./test_package/conanfile.py
build() and test() methods
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE CREATION
78
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGING STEPS
79
conan create . user/channel
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE
80
conan create . user/channel
conan create . user/channel --keep-source
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE
80
conan create . user/channel
conan create . user/channel --keep-source
conan create . user/channel --keep-build
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE
80
conan create . user/channel
conan create . user/channel --keep-source
conan create . user/channel --keep-build
conan test test_package MyLibrary/0.1@user/channel
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE
80
conan source . --source-folder=tmp/source
conan install . --install-folder=tmp/build [--profile XXXX]
conan build . --source-folder=tmp/source --build-folder=tmp/build
conan package . --source-folder=tmp/source --build-folder=tmp/build --package-folder=tmp/package
conan export-pkg . user/testing --package-folder=tmp/package
conan test test_package MyLibrary/0.1@user/channel
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: IN-SOURCE
81
conan source . --source-folder=tmp/source
conan install . --install-folder=tmp/build [--profile XXXX]
conan build . --source-folder=tmp/source --build-folder=tmp/build
conan package . --source-folder=tmp/source --build-folder=tmp/build --package-folder=tmp/package
conan export-pkg . user/testing --package-folder=tmp/package
conan test test_package MyLibrary/0.1@user/channel
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
CONAN PACKAGE DEVELOPMENT FLOW: IN-SOURCE
Even smaller steps are possible with --configure, --build, --install
options of conan build command
81
• Recipe should include description, license, and url attributes
• Use only lowercase letters in package names
• Include test_package
• Raise errors on invalid configurations
• Do not use version ranges
• Distribute packaged so ware license files in binary packages
• Export packaging code license
• Use clean python code and the latest Conan features
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
PACKAGE QUALITY
82
• If not done already, add the Conan remote
conan remote add conan-mpusz https://bintray.com/mpusz/conan-mpusz
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
UPLOADING A PACKAGE
83
• If not done already, add the Conan remote
conan remote add conan-mpusz https://bintray.com/mpusz/conan-mpusz
• Upload the package recipe and all binaries to the Conan remote
conan upload google-benchmark/1.4.1@mpusz/stable -r conan_mpusz --all
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
UPLOADING A PACKAGE
83
1 cmake
2 cmake_multi
3 cmake_paths
4 cmake_find_package
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
USING CONAN WITH CMAKE: GENERATORS
84
• Creates conanbuildinfo.cmake file that defines CMake
– variables (module paths include paths, library names, ...)
– helper functions (defining targets, configuring CMake environment, ...)
• Changes to CMakeLists.txt file are required
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
endif()
# ...
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake GENERATOR
85
• Creates conanbuildinfo_multi.cmake file
• Used with multi-configuration environments like Visual Studio and Xcode
• Does not configure for a specific build_type, like Debug or Release
• Cannot be used to create packages and issues with find_package() usage
• Changes to CMakeLists.txt file are required
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
conan_basic_setup(TARGETS)
endif()
# ...
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_multi GENERATOR
86
• Creates conan_paths.cmake file that defines
– CMAKE_MODULE_PATH used by find_package() to find CMake configuration and FindXXX.cmake files
– CMAKE_PREFIX_PATH used by find_library() to locate library files in packages
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL)
# ...
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_paths GENERATOR
87
• Creates conan_paths.cmake file that defines
– CMAKE_MODULE_PATH used by find_package() to find CMake configuration and FindXXX.cmake files
– CMAKE_PREFIX_PATH used by find_library() to locate library files in packages
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL)
# ...
• Can work without doing any changes to CMake files
cmake .. -DCMAKE_PROJECT_MyLibrary_INCLUDE=conan_paths.cmake -DCMAKE_BUILD_TYPE=Release
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_paths GENERATOR
87
• Creates Find<package_name>.cmake file for each requirement specified in the conanfile
• When used with CMake helper in build() method of conanfile.py no changes to CMake files needed
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_find_package GENERATOR
88
• Creates Find<package_name>.cmake file for each requirement specified in the conanfile
• When used with CMake helper in build() method of conanfile.py no changes to CMake files needed
• When used with conanfile.txt
– can be used together with cmake_paths generator
[requires]
...
[generators]
cmake_find_package
cmake_paths
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_find_package GENERATOR
88
• Creates Find<package_name>.cmake file for each requirement specified in the conanfile
• When used with CMake helper in build() method of conanfile.py no changes to CMake files needed
• When used with conanfile.txt
– can be used together with cmake_paths generator
[requires]
...
[generators]
cmake_find_package
cmake_paths
– CMAKE_MODULE_PATH can be updated manually
cmake_minimum_required(VERSION 3.5)
project(MyLibrary)
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
# ...
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
cmake_find_package GENERATOR
88
class MyLibraryConan(ConanFile):
# ...
generators = "cmake"
def source(self):
# ...
tools.replace_in_file("%s/CMakeLists.txt" % self.subfolder,
"project(MyLibrary)",
"""project(MyLibrary)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()""")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: AUTOMATION OF UPDATING CMakeLists.txt FILE
OPTION 1
89
class MyLibraryConan(ConanFile):
# ...
exports_sources = ["CMakeLists.txt"]
generators = "cmake"
scm = {
"type": "git",
"subfolder": "source_subfolder",
# ...
}
project(MyLibraryWrapper)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_subdirectory("source_subfolder")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: AUTOMATION OF UPDATING CMakeLists.txt FILE
OPTION 2
90
class MyLibraryConan(ConanFile):
# ...
generators = "cmake_paths"
def build_requirements(self):
if tools.get_env("CONAN_RUN_TESTS", False):
self.build_requires("gtest/1.8.1@bincrafters/stable")
def _configure_cmake(self):
cmake = CMake(self)
# ...
cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "ON" if tools.get_env("CONAN_RUN_TESTS", False) else "OFF"
cmake.definitions["CMAKE_PROJECT_benchmark_INCLUDE"] = "conan_paths.cmake"
cmake.configure()
return cmake
def build(self):
cmake = self._configure_cmake()
cmake.build()
if tools.get_env("CONAN_RUN_TESTS", False):
cmake.test()
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: HOW TO RUN UNIT TESTS DURING PACKAGING PROCESS?
91
class MyLibraryConan(ConanFile):
# ...
generators = "cmake_paths"
def build_requirements(self):
if tools.get_env("CONAN_RUN_TESTS", False):
self.build_requires("gtest/1.8.1@bincrafters/stable")
def _configure_cmake(self):
cmake = CMake(self)
# ...
cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "ON" if tools.get_env("CONAN_RUN_TESTS", False) else "OFF"
cmake.definitions["CMAKE_PROJECT_benchmark_INCLUDE"] = "conan_paths.cmake"
cmake.configure()
return cmake
def build(self):
cmake = self._configure_cmake()
cmake.build()
if tools.get_env("CONAN_RUN_TESTS", False):
cmake.test()
• To enable building and running tests define environment variable, set it in a profile file, or run
conan create . user/channel -e CONAN_RUN_TESTS=1
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: HOW TO RUN UNIT TESTS DURING PACKAGING PROCESS?
91
• Package is asocial (does not have CMake configuration files or FindXXX.cmake file)
• Existing "o icial" FindXXX.cmake file is not able to find our libraries
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: CUSTOM FindXXX.cmake FILE
REASON
92
• Package is asocial (does not have CMake configuration files or FindXXX.cmake file)
• Existing "o icial" FindXXX.cmake file is not able to find our libraries
• Create a custom FindXXX.cmake file in package root folder and export it with the package
class MyLibraryConan(ConanFile):
name = "my-library"
version = "0.1"
# ...
exports_sources = ["FindMyLibrary.cmake"]
def package(self):
# ...
self.copy("FindMyLibrary.cmake", ".", ".")
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: CUSTOM FindXXX.cmake FILE
REASON
BKM
92
class MyLibraryConan(ConanFile):
name = "my-library"
version = "0.1"
something = ""
some_dynamic_path = ""
def source(self):
self.something = "Hello"
some_dynamic_path = find_some_dynamic_path()
def build(self):
print(self.something)
use_some_dynamic_path(some_dynamic_path)
def package(self):
print(self.something)
use_some_dynamic_path(some_dynamic_path)
• Works fine with conan create . user/channel because it calls all the methods in sequence
• Problems with commands that execute only a part of the sequence
– i.e. the source() and build() methods won’t be called at all when you run conan package
command
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
BKM: DO NOT USE CLASS VARIABLES TO SHARE STATE
93
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SUMMARY
94
• Many projects still do not use CMake at all
• Many projects still do not use CMake in a Modern way
• Many projects still do not provide installation option with proper CMake configuration files generation
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SUMMARY
CMAKE
94
• Many projects still do not use CMake at all
• Many projects still do not use CMake in a Modern way
• Many projects still do not provide installation option with proper CMake configuration files generation
• Production quality Package Manager designed with C++ in mind
• For free and on MIT license
• Easy to use and the documentation is really good
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SUMMARY
CMAKE
CONAN
94
• Many projects still do not use CMake at all
• Many projects still do not use CMake in a Modern way
• Many projects still do not provide installation option with proper CMake configuration files generation
• Production quality Package Manager designed with C++ in mind
• For free and on MIT license
• Easy to use and the documentation is really good
• Give it a try!
pip install conan
CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects
SUMMARY
CMAKE
CONAN
94
Git, CMake, Conan - How to ship and reuse our C++ projects?
Git, CMake, Conan - How to ship and reuse our C++ projects?

More Related Content

What's hot

規格書で読むC++11のスレッド
規格書で読むC++11のスレッド規格書で読むC++11のスレッド
規格書で読むC++11のスレッド
Kohsuke Yuasa
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
Daniel Pfeifer
 
C/C++プログラマのための開発ツール
C/C++プログラマのための開発ツールC/C++プログラマのための開発ツール
C/C++プログラマのための開発ツール
MITSUNARI Shigeo
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
Angel Boy
 
Linux SMEP bypass techniques
Linux SMEP bypass techniquesLinux SMEP bypass techniques
Linux SMEP bypass techniques
Vitaly Nikolenko
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
Brendan Gregg
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
Safique Ahmed Faruque
 
Introduction to CMake
Introduction to CMakeIntroduction to CMake
Introduction to CMake
Dimitrios Platis
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
Shih-Hsiang Lin
 
C# for C++ Programmers
C# for C++ ProgrammersC# for C++ Programmers
C# for C++ Programmers
russellgmorley
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
Angel Boy
 
Linux Systems Performance 2016
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
Brendan Gregg
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
Brendan Gregg
 
cmake.pdf
cmake.pdfcmake.pdf
cmake.pdf
Thejeswara Reddy
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
SUSE Labs Taipei
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 
[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?
Akihiro Suda
 
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだconstexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだGenya Murakami
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニックGenya Murakami
 
Docker入門: コンテナ型仮想化技術の仕組みと使い方
Docker入門: コンテナ型仮想化技術の仕組みと使い方Docker入門: コンテナ型仮想化技術の仕組みと使い方
Docker入門: コンテナ型仮想化技術の仕組みと使い方
Yuichi Ito
 

What's hot (20)

規格書で読むC++11のスレッド
規格書で読むC++11のスレッド規格書で読むC++11のスレッド
規格書で読むC++11のスレッド
 
CMake - Introduction and best practices
CMake - Introduction and best practicesCMake - Introduction and best practices
CMake - Introduction and best practices
 
C/C++プログラマのための開発ツール
C/C++プログラマのための開発ツールC/C++プログラマのための開発ツール
C/C++プログラマのための開発ツール
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
Linux SMEP bypass techniques
Linux SMEP bypass techniquesLinux SMEP bypass techniques
Linux SMEP bypass techniques
 
Linux BPF Superpowers
Linux BPF SuperpowersLinux BPF Superpowers
Linux BPF Superpowers
 
Git in 10 minutes
Git in 10 minutesGit in 10 minutes
Git in 10 minutes
 
Introduction to CMake
Introduction to CMakeIntroduction to CMake
Introduction to CMake
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
C# for C++ Programmers
C# for C++ ProgrammersC# for C++ Programmers
C# for C++ Programmers
 
Sigreturn Oriented Programming
Sigreturn Oriented ProgrammingSigreturn Oriented Programming
Sigreturn Oriented Programming
 
Linux Systems Performance 2016
Linux Systems Performance 2016Linux Systems Performance 2016
Linux Systems Performance 2016
 
Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
 
cmake.pdf
cmake.pdfcmake.pdf
cmake.pdf
 
eBPF maps 101
eBPF maps 101eBPF maps 101
eBPF maps 101
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?[Container Plumbing Days 2023] Why was nerdctl made?
[Container Plumbing Days 2023] Why was nerdctl made?
 
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだconstexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
constexpr関数はコンパイル時処理。これはいい。実行時が霞んで見える。cpuの嬌声が聞こえてきそうだ
 
Constexpr 中3女子テクニック
Constexpr 中3女子テクニックConstexpr 中3女子テクニック
Constexpr 中3女子テクニック
 
Docker入門: コンテナ型仮想化技術の仕組みと使い方
Docker入門: コンテナ型仮想化技術の仕組みと使い方Docker入門: コンテナ型仮想化技術の仕組みと使い方
Docker入門: コンテナ型仮想化技術の仕組みと使い方
 

Similar to Git, CMake, Conan - How to ship and reuse our C++ projects?

IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
Alexandre Gouaillard
 
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
rodburns
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
sparkfabrik
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Codemotion
 
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
Amazon Web Services
 
Gitlab ci, cncf.sk
Gitlab ci, cncf.skGitlab ci, cncf.sk
Gitlab ci, cncf.sk
Juraj Hantak
 
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud DataflowHow to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
Lucas Arruda
 
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
tdc-globalcode
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
Sqreen
 
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open SourceEnhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
Nico Meisenzahl
 
CI/CD with Github Actions
CI/CD with Github ActionsCI/CD with Github Actions
CI/CD with Github Actions
Md. Minhazul Haque
 
Bounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise EnvironmentBounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise Environment
AdaCore
 
Serverless Data Architecture at scale on Google Cloud Platform
Serverless Data Architecture at scale on Google Cloud PlatformServerless Data Architecture at scale on Google Cloud Platform
Serverless Data Architecture at scale on Google Cloud Platform
MeetupDataScienceRoma
 
DEVNET-2006 Coding 210: Parsing JSON in C++
DEVNET-2006	Coding 210: Parsing JSON in C++DEVNET-2006	Coding 210: Parsing JSON in C++
DEVNET-2006 Coding 210: Parsing JSON in C++
Cisco DevNet
 
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinIntegrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Databricks
 
Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CI
Uilian Ries
 
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with StormDECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
Mike Lohmann
 

Similar to Git, CMake, Conan - How to ship and reuse our C++ projects? (20)

IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
IIT-RTC 2017 Qt WebRTC Tutorial (Qt Janus Client)
 
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
SC20 SYCL and C++ Birds of a Feather 19th Nov 2020
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
 
basics of c++
basics of c++basics of c++
basics of c++
 
basics of c++
basics of c++basics of c++
basics of c++
 
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
 
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
Container Power Hour with Jess, Clare, and Abby (CON362) - AWS re:Invent 2018
 
Gitlab ci, cncf.sk
Gitlab ci, cncf.skGitlab ci, cncf.sk
Gitlab ci, cncf.sk
 
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud DataflowHow to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
How to build an ETL pipeline with Apache Beam on Google Cloud Dataflow
 
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
TDC2017 | São Paulo - Trilha BigData How we figured out we had a SRE team at ...
 
Writing a Python C extension
Writing a Python C extensionWriting a Python C extension
Writing a Python C extension
 
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open SourceEnhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
Enhance Your Kubernetes CI/CD Pipelines With GitLab & Open Source
 
CI/CD with Github Actions
CI/CD with Github ActionsCI/CD with Github Actions
CI/CD with Github Actions
 
Bounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise EnvironmentBounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise Environment
 
de Valpine NIMBLE
de Valpine NIMBLEde Valpine NIMBLE
de Valpine NIMBLE
 
Serverless Data Architecture at scale on Google Cloud Platform
Serverless Data Architecture at scale on Google Cloud PlatformServerless Data Architecture at scale on Google Cloud Platform
Serverless Data Architecture at scale on Google Cloud Platform
 
DEVNET-2006 Coding 210: Parsing JSON in C++
DEVNET-2006	Coding 210: Parsing JSON in C++DEVNET-2006	Coding 210: Parsing JSON in C++
DEVNET-2006 Coding 210: Parsing JSON in C++
 
Integrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther KundinIntegrating Existing C++ Libraries into PySpark with Esther Kundin
Integrating Existing C++ Libraries into PySpark with Esther Kundin
 
Gitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CIGitlab - Creating C++ applications with Gitlab CI
Gitlab - Creating C++ applications with Gitlab CI
 
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with StormDECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
 

More from Mateusz Pusz

Free Lunch is Over: Why is C++ so Important in the Modern World?
Free Lunch is Over: Why is C++ so Important in the Modern World?Free Lunch is Over: Why is C++ so Important in the Modern World?
Free Lunch is Over: Why is C++ so Important in the Modern World?
Mateusz Pusz
 
A Physical Units Library for the Next C++
A Physical Units Library for the Next C++A Physical Units Library for the Next C++
A Physical Units Library for the Next C++
Mateusz Pusz
 
Striving for ultimate Low Latency
Striving for ultimate Low LatencyStriving for ultimate Low Latency
Striving for ultimate Low Latency
Mateusz Pusz
 
Rethinking the Way We do Templates in C++
Rethinking the Way We do Templates in C++Rethinking the Way We do Templates in C++
Rethinking the Way We do Templates in C++
Mateusz Pusz
 
C++11 Was Only the Beginning
C++11 Was Only the BeginningC++11 Was Only the Beginning
C++11 Was Only the Beginning
Mateusz Pusz
 
Implementing a Physical Units Library for C++
Implementing a Physical Units Library for C++Implementing a Physical Units Library for C++
Implementing a Physical Units Library for C++
Mateusz Pusz
 
Effective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variantEffective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variant
Mateusz Pusz
 
Implementing Physical Units Library for C++
Implementing Physical Units Library for C++Implementing Physical Units Library for C++
Implementing Physical Units Library for C++
Mateusz Pusz
 
C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?
Mateusz Pusz
 
Effective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variantEffective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variant
Mateusz Pusz
 
Beyond C++17
Beyond C++17Beyond C++17
Beyond C++17
Mateusz Pusz
 
Pointless Pointers - How to make our interfaces efficient?
Pointless Pointers - How to make our interfaces efficient?Pointless Pointers - How to make our interfaces efficient?
Pointless Pointers - How to make our interfaces efficient?
Mateusz Pusz
 
Striving for ultimate Low Latency
Striving for ultimate Low LatencyStriving for ultimate Low Latency
Striving for ultimate Low Latency
Mateusz Pusz
 
Small Lie in Big O
Small Lie in Big OSmall Lie in Big O
Small Lie in Big O
Mateusz Pusz
 
std::shared_ptr<T> - (not so) Smart hammer for every pointy nail
std::shared_ptr<T> - (not so) Smart hammer for every pointy nailstd::shared_ptr<T> - (not so) Smart hammer for every pointy nail
std::shared_ptr<T> - (not so) Smart hammer for every pointy nail
Mateusz Pusz
 

More from Mateusz Pusz (15)

Free Lunch is Over: Why is C++ so Important in the Modern World?
Free Lunch is Over: Why is C++ so Important in the Modern World?Free Lunch is Over: Why is C++ so Important in the Modern World?
Free Lunch is Over: Why is C++ so Important in the Modern World?
 
A Physical Units Library for the Next C++
A Physical Units Library for the Next C++A Physical Units Library for the Next C++
A Physical Units Library for the Next C++
 
Striving for ultimate Low Latency
Striving for ultimate Low LatencyStriving for ultimate Low Latency
Striving for ultimate Low Latency
 
Rethinking the Way We do Templates in C++
Rethinking the Way We do Templates in C++Rethinking the Way We do Templates in C++
Rethinking the Way We do Templates in C++
 
C++11 Was Only the Beginning
C++11 Was Only the BeginningC++11 Was Only the Beginning
C++11 Was Only the Beginning
 
Implementing a Physical Units Library for C++
Implementing a Physical Units Library for C++Implementing a Physical Units Library for C++
Implementing a Physical Units Library for C++
 
Effective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variantEffective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variant
 
Implementing Physical Units Library for C++
Implementing Physical Units Library for C++Implementing Physical Units Library for C++
Implementing Physical Units Library for C++
 
C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?
 
Effective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variantEffective replacement of dynamic polymorphism with std::variant
Effective replacement of dynamic polymorphism with std::variant
 
Beyond C++17
Beyond C++17Beyond C++17
Beyond C++17
 
Pointless Pointers - How to make our interfaces efficient?
Pointless Pointers - How to make our interfaces efficient?Pointless Pointers - How to make our interfaces efficient?
Pointless Pointers - How to make our interfaces efficient?
 
Striving for ultimate Low Latency
Striving for ultimate Low LatencyStriving for ultimate Low Latency
Striving for ultimate Low Latency
 
Small Lie in Big O
Small Lie in Big OSmall Lie in Big O
Small Lie in Big O
 
std::shared_ptr<T> - (not so) Smart hammer for every pointy nail
std::shared_ptr<T> - (not so) Smart hammer for every pointy nailstd::shared_ptr<T> - (not so) Smart hammer for every pointy nail
std::shared_ptr<T> - (not so) Smart hammer for every pointy nail
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 

Git, CMake, Conan - How to ship and reuse our C++ projects?

  • 1. Mateusz Pusz September 24, 2018 GIT, CMAKE, CONAN HOW TO SHIP AND REUSE OUR C++ PROJECTS?
  • 2. VERSION CONTROL SYSTEM BUILDING PACKAGE MANAGEMENT CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET 2
  • 3. VERSION CONTROL SYSTEM git BUILDING PACKAGE MANAGEMENT CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET 2
  • 4. VERSION CONTROL SYSTEM git BUILDING CMake PACKAGE MANAGEMENT CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET 2
  • 5. VERSION CONTROL SYSTEM git BUILDING CMake PACKAGE MANAGEMENT None CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET 2
  • 6. VERSION CONTROL SYSTEM git BUILDING CMake PACKAGE MANAGEMENT None • Please do not partition our C++ development environment even more • Other tools may sometimes be better at one aspect but probably are worse at others • Until a strong game-changer appears on the market please use above tools CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET 2
  • 7. VERSION CONTROL SYSTEM git BUILDING CMake PACKAGE MANAGEMENT None • Please do not partition our C++ development environment even more • Other tools may sometimes be better at one aspect but probably are worse at others • Until a strong game-changer appears on the market please use above tools CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE MOST COMMON C++ TOOLSET Conan is a strong contender to become the C++ Package Manager 2
  • 8. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 9. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 10. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() 2 External source code as git submodules + CMake add_subdirectory() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 11. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() 2 External source code as git submodules + CMake add_subdirectory() 3 CMake ExternalProject_Add() + CMake add_subdirectory() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 12. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() 2 External source code as git submodules + CMake add_subdirectory() 3 CMake ExternalProject_Add() + CMake add_subdirectory() 4 Downloading and installing each dependency + CMake find_package() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 13. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() 2 External source code as git submodules + CMake add_subdirectory() 3 CMake ExternalProject_Add() + CMake add_subdirectory() 4 Downloading and installing each dependency + CMake find_package() 5 Usage of other languages' toolsets (i.e. maven, NuGet, ...) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 14. 1 external or 3rdparty subdirs with external projects' source code + CMake add_subdirectory() 2 External source code as git submodules + CMake add_subdirectory() 3 CMake ExternalProject_Add() + CMake add_subdirectory() 4 Downloading and installing each dependency + CMake find_package() 5 Usage of other languages' toolsets (i.e. maven, NuGet, ...) 6 Dedicated C++ package managers (i.e. Conan) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL WAYS TO HANDLE DEPENDENCIES IN C++ PROJECTS 3
  • 15. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects add_subdirectory() FOR DEPS? 4
  • 16. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects add_subdirectory() FOR DEPS? 5
  • 17. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects add_subdirectory() FOR DEPS? 6
  • 18. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects add_subdirectory() FOR DEPS? PLEASE DON'T! 7
  • 19. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects add_subdirectory() FOR DEPS? PLEASE DON'T! Handling dependencies as subdirectories does not scale! 7
  • 20. Not a build system! Cross-platform C++ build generator CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CMake 8
  • 21. cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local cmake --build . ctest -VV cmake --build . --target install cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=~/.local cmake --build . --config Release ctest -VV -C Release cmake --build . --target install --config Release CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL CMAKE WORKFLOW GCC VISUAL STUDIO 9
  • 22. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake 10
  • 23. Build flags don't scale! • Every change in public flags has to be propagated upwards • Not possible to maintain flags on a large scale • Di erent targets may require conflicting flag values CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake 11
  • 24. Build flags don't scale! • Every change in public flags has to be propagated upwards • Not possible to maintain flags on a large scale • Di erent targets may require conflicting flag values CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake Modern CMake is about Targets and Properties 11
  • 25. EXECUTABLES add_executable SHARED LIBRARIES add_library(SHARED) STATIC LIBRARIES add_library(STATIC) OBJECT LIBRARIES add_library(OBJECT) INTERFACE LIBRARIES add_library(INTERFACE) ALIAS LIBRARIES add_library(ALIAS) IMPORTED LIBRARIES add_library(IMPORTED [GLOBAL]) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CMake TARGET TYPES 12
  • 26. EXECUTABLES add_executable SHARED LIBRARIES add_library(SHARED) STATIC LIBRARIES add_library(STATIC) OBJECT LIBRARIES add_library(OBJECT) INTERFACE LIBRARIES add_library(INTERFACE) ALIAS LIBRARIES add_library(ALIAS) IMPORTED LIBRARIES add_library(IMPORTED [GLOBAL]) • If no type is given explicitly to add_library() the type is STATIC or SHARED based on whether the current value of the variable BUILD_SHARED_LIBS is ON CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CMake TARGET TYPES 12
  • 27. • Available since version 2.8.12 (Oct 2013) • Specified by target_xxx() commands target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <item>... [<PRIVATE|PUBLIC|INTERFACE> <item>...]...) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake: MODULAR DESIGN 13
  • 28. • Available since version 2.8.12 (Oct 2013) • Specified by target_xxx() commands target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <item>... [<PRIVATE|PUBLIC|INTERFACE> <item>...]...) NEEDED BY ME NOT NEEDED BY ME NEEDED BY DEPENDERS PUBLIC INTERFACE NOT NEEDED BY DEPENDERS PRIVATE :-) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake: MODULAR DESIGN 13
  • 29. • Available since version 2.8.12 (Oct 2013) • Specified by target_xxx() commands target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> <item>... [<PRIVATE|PUBLIC|INTERFACE> <item>...]...) NEEDED BY ME NOT NEEDED BY ME NEEDED BY DEPENDERS PUBLIC INTERFACE NOT NEEDED BY DEPENDERS PRIVATE :-) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN CMake: MODULAR DESIGN INTERFACE and PUBLIC dependencies are transitive while PRIVATE are not 13
  • 30. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PHYSICAL DESIGN: OLD CMAKE STYLE 14
  • 31. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PHYSICAL DESIGN: OLD CMAKE STYLE Linking diagram only. Not useful to reveal real design problems. 14
  • 32. • More than a linking diagram • Logical dependencies included • PRIVATE and PUBLIC dependencies make di erence CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PHYSICAL DESIGN: MODERN CMAKE 15
  • 33. • More than a linking diagram • Logical dependencies included • PRIVATE and PUBLIC dependencies make di erence • Now we see real design problems... CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PHYSICAL DESIGN: MODERN CMAKE 15
  • 34. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects NO CYCLIC PHYSICAL DEPENDENCIES! 16
  • 35. add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects ALIAS TARGETS 17
  • 36. add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) add_library(MyCompany::MyLibrary ALIAS MyLibrary) # find_package(MyLibrary CONFIG REQUIRED) target_link_libraries(MyLibraryTest PUBLIC MyCompany::MyLibrary GTest::Main ) • Unifies with find_package() target naming • :: has to be followed with Target name (prevents typos) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects ALIAS TARGETS 17
  • 37. target_compile_definitions(foo PRIVATE "VERBOSITY=$<IF:$<BOOL:${VERBOSE}>,30,10>") • Use the $<> syntax • Not evaluated by the command interpreter • Evaluated during build system generation CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS 18
  • 38. add_executable(hello main.cpp) if(CMAKE_BUILD_TYPE STREQUAL DEBUG) target_sources(hello PRIVATE helper_debug.cpp) else() target_sources(hello PRIVATE helper_release.cpp) endif() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS BAD 19
  • 39. add_executable(hello main.cpp) if(CMAKE_BUILD_TYPE STREQUAL DEBUG) target_sources(hello PRIVATE helper_debug.cpp) else() target_sources(hello PRIVATE helper_release.cpp) endif() add_executable(hello main.cpp $<IF:$<CONFIG:Debug>:helper_debug.cpp,helper_release.cpp>) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS BAD GOOD 19
  • 40. add_executable(hello main.cpp) if(CMAKE_BUILD_TYPE STREQUAL DEBUG) target_sources(hello PRIVATE helper_debug.cpp) else() target_sources(hello PRIVATE helper_release.cpp) endif() add_executable(hello main.cpp $<IF:$<CONFIG:Debug>:helper_debug.cpp,helper_release.cpp>) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS BAD GOOD Never use CMAKE_BUILD_TYPE in if() 19
  • 41. The library interface may change during installation. Use BUILD_INTERFACE and INSTALL_INTERFACE generator expression filters. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS 20
  • 42. The library interface may change during installation. Use BUILD_INTERFACE and INSTALL_INTERFACE generator expression filters. target_include_directories(Foo PUBLIC $<BUILD_INTERFACE:${Foo_BINARY_DIR}/include> $<BUILD_INTERFACE:${Foo_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects GENERATOR EXPRESSIONS 20
  • 43. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) # dependencies find_package(Foo 1.0 REQUIRED) # library definition add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) target_compile_features(MyLibrary PUBLIC cxx_std_17) target_include_directories(MyLibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) target_link_libraries(MyLibrary PRIVATE Foo::Foo) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY EXAMPLE 21
  • 44. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) # dependencies find_package(Foo 1.0 REQUIRED) # library definition add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) target_compile_features(MyLibrary PUBLIC cxx_std_17) target_include_directories(MyLibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) target_link_libraries(MyLibrary PRIVATE Foo::Foo) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY EXAMPLE 21
  • 45. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) # dependencies find_package(Foo 1.0 REQUIRED) # library definition add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) target_compile_features(MyLibrary PUBLIC cxx_std_17) target_include_directories(MyLibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) target_link_libraries(MyLibrary PRIVATE Foo::Foo) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY EXAMPLE 21
  • 46. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) # dependencies find_package(Foo 1.0 REQUIRED) # library definition add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) target_compile_features(MyLibrary PUBLIC cxx_std_17) target_include_directories(MyLibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) target_link_libraries(MyLibrary PRIVATE Foo::Foo) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY EXAMPLE 21
  • 47. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) # dependencies find_package(Foo 1.0 REQUIRED) # library definition add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) target_compile_features(MyLibrary PUBLIC cxx_std_17) target_include_directories(MyLibrary PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) target_link_libraries(MyLibrary PRIVATE Foo::Foo) add_library(MyCompany::MyLibrary ALIAS MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY EXAMPLE Avoid custom variables in the arguments of project commands 21
  • 48. • Standalone library definition and installation • Does not change compiler's warnings! CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects FILES ORGANIZATION MYLIBRARY/SRC/CMAKELISTS.TXT 22
  • 49. • Standalone library definition and installation • Does not change compiler's warnings! • Standalone unit tests definition CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects FILES ORGANIZATION MYLIBRARY/SRC/CMAKELISTS.TXT MYLIBRARY/TEST/CMAKELISTS.TXT 22
  • 50. • Standalone library definition and installation • Does not change compiler's warnings! • Standalone unit tests definition • Simple project wrapper • Entry point for development • src and test subdirectories added with add_subdirectory() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects FILES ORGANIZATION MYLIBRARY/SRC/CMAKELISTS.TXT MYLIBRARY/TEST/CMAKELISTS.TXT MYLIBRARY/CMAKELISTS.TXT 22
  • 51. cmake_minimum_required(VERSION 3.5) project(MyLibrary) # add project code add_subdirectory(src) # add unit tests enable_testing() add_subdirectory(test) • Useful for project development – no need to install the library in order to run unit tests • Entry point for IDEs like CLion or Visual Studio CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WRAPPER: LOCAL DEVELOPMENT ENTRY POINT 23
  • 52. cmake_minimum_required(VERSION 3.8) project(MyLibraryTests) # dependencies enable_testing() find_package(GTest MODULE REQUIRED) if(NOT TARGET MyCompany::MyLibrary) find_package(MyLibrary CONFIG REQUIRED) endif() # target definition add_executable(MyLibraryTests tests_source.cpp) target_link_libraries(MyLibraryTests PRIVATE MyCompany::MyLibrary GTest::Main ) add_test(NAME MyLibrary.UnitTests COMMAND MyLibraryTests ) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MODERN LIBRARY USAGE: TESTS 24
  • 53. Done? CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects COMPILED, BUILT, TESTED 25
  • 54. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects DON'T BE ASOCIAL! 26
  • 55. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE 27
  • 56. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) find_package(Foo 1.0 REQUIRED) add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE CMAKELISTS.TXT 28
  • 57. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) find_package(Foo 1.0 REQUIRED) add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) install(TARGETS MyLibrary EXPORT MyLibraryTargets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include) install(EXPORT MyLibraryTargets DESTINATION lib/cmake/MyLibrary FILE MyLibraryTargets.cmake NAMESPACE MyCompany::) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE CMAKELISTS.TXT 28
  • 58. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) find_package(Foo 1.0 REQUIRED) add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) install(TARGETS MyLibrary EXPORT MyLibraryTargets LIBRARY DESTINATION lib ARCHIVE DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include) install(EXPORT MyLibraryTargets DESTINATION lib/cmake/MyLibrary FILE MyLibraryTargets.cmake NAMESPACE MyCompany::) install(DIRECTORY include/MyLibrary DESTINATION include) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE CMAKELISTS.TXT 28
  • 59. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) find_package(Foo 1.0 REQUIRED) add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) include(CMakePackageConfigHelpers) write_basic_package_version_file(MyLibraryConfigVersion.cmake COMPATIBILITY SameMajorVersion) install(FILES MyLibraryConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/MyLibraryConfigVersion.cmake DESTINATION lib/cmake/MyLibrary) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE CMAKELISTS.TXT 29
  • 60. cmake_minimum_required(VERSION 3.8) project(MyLibrary VERSION 0.0.1) find_package(Foo 1.0 REQUIRED) add_library(MyLibrary lib_source.cpp lib_source.h include/MyLibrary/lib_header.h) include(CMakePackageConfigHelpers) write_basic_package_version_file(MyLibraryConfigVersion.cmake COMPATIBILITY SameMajorVersion) install(FILES MyLibraryConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/MyLibraryConfigVersion.cmake DESTINATION lib/cmake/MyLibrary) include(CMakeFindDependencyMacro) find_dependency(Foo 1.0) include("${CMAKE_CURRENT_LIST_DIR}/MyLibraryTargets.cmake") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects EXPORT YOUR LIBRARY INTERFACE CMAKELISTS.TXT MYLIBRARYCONFIG.CMAKE 29
  • 61. • Create and install the package mkdir src/build cd src/build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local cmake --build . --target install CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE TESTING WORKFLOW 30
  • 62. • Create and install the package mkdir src/build cd src/build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local cmake --build . --target install • Compile and run tests using the installed library mkdir test/build cd test/build cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/.local ctest -VV CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE TESTING WORKFLOW 30
  • 63. • Build each repository in isolation, generate and install its binaries along with a CMake config file • For each project that has dependencies, use find_package() to load the config file and use the library CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PURE CMAKE: DEPENDENCIES THE WRONG WAY PROCESS 31
  • 64. • Build each repository in isolation, generate and install its binaries along with a CMake config file • For each project that has dependencies, use find_package() to load the config file and use the library • Updating a program implies recompiling its package and then every one of its dependers manually • It doesn't scale (many levels of dependencies, many configurations, ...) • What about supporting di erent versions? – Release, Debug, RelWithDebInfo, ... – di erent compilers (gcc, clang, ...), compiler versions, C++ libraries (libc++ vs libstdc++) – di erent runtime libraries – di erent package configurations (no exceptions, shared lib, ...) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PURE CMAKE: DEPENDENCIES THE WRONG WAY PROCESS PROBLEMS 31
  • 65. • One build process builds the project and all dependencies • Only required dependencies are being rebuilt – reuse of prebuilt binaries if available and up-to-date • No need to manually download, build and install the dependencies • Possibility to use our own versions of dependencies (ZLib, Boost, etc) instead of using system versions CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT WE WANT? 32
  • 66. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER? 33
  • 67. • Decentralized servers – OSS, corporate, private, etc CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER? 33
  • 68. • Decentralized servers – OSS, corporate, private, etc • Central cache on local PC – sharing of the dependencies data among di erent projects CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER? 33
  • 69. • Decentralized servers – OSS, corporate, private, etc • Central cache on local PC – sharing of the dependencies data among di erent projects • Support for – sources only (do not use prebuilt binaries even if available) – sources + prebuilt binaries (improves build performance) – binaries only (for closed source projects and development tools) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER? 33
  • 70. • Decentralized servers – OSS, corporate, private, etc • Central cache on local PC – sharing of the dependencies data among di erent projects • Support for – sources only (do not use prebuilt binaries even if available) – sources + prebuilt binaries (improves build performance) – binaries only (for closed source projects and development tools) • O line use – so I can start a new project with popular dependencies in a plane flying to CppCon ;-) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects WHAT I WANT NOW AND I DID NOT REALIZE THAT EARLIER? 33
  • 71. conan.ioC++ package manager • Conan is OSS, with a MIT license • Decentralized package manager with a client-server architecture • The servers are just a package storage – they do not build nor create the packages • The packages are created by the client – packaging of prebuilt binaries – building from sources if needed • Portable to any platform supporting Python • Works with any build system • Uses Python as its scripting language • Easy hosting in a cloud or on a local server CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN 34
  • 72. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN CLIENT-SERVER ARCHITECTURE 35
  • 73. • Console/terminal application • Package creation and consumption • Local cache for package storage – allows o line work CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN CLIENT-SERVER ARCHITECTURE CONAN CLIENT 36
  • 74. • Console/terminal application • Package creation and consumption • Local cache for package storage – allows o line work • Provides public and free hosting service for OSS conan packages • Account is only needed to upload packages (anonymous read access) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN CLIENT-SERVER ARCHITECTURE CONAN CLIENT JFROG BINTRAY 36
  • 75. • Console/terminal application • Package creation and consumption • Local cache for package storage – allows o line work • Quite simple TCP server • User can run it as a daemon or a service • Provides public and free hosting service for OSS conan packages • Account is only needed to upload packages (anonymous read access) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN CLIENT-SERVER ARCHITECTURE CONAN CLIENT CONAN SERVER JFROG BINTRAY 36
  • 76. • Console/terminal application • Package creation and consumption • Local cache for package storage – allows o line work • Quite simple TCP server • User can run it as a daemon or a service • Provides public and free hosting service for OSS conan packages • Account is only needed to upload packages (anonymous read access) • O ers conan repositories • More powerful than conan server (WebUI, multiple auth protocols, High Availability, ...) • JFrog Artifactory Community Edition for C/C++ CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN CLIENT-SERVER ARCHITECTURE CONAN CLIENT CONAN SERVER JFROG BINTRAY JFROG ARTIFACTORY 36
  • 77. • Moderated, curated and well-maintained packages • Place in which you can share your packages with the community CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects POPULAR CONAN REMOTES ON BINTRAY CONAN-CENTER 37
  • 78. • Moderated, curated and well-maintained packages • Place in which you can share your packages with the community • The Bincra ers team builds binary so ware packages for the OSS community • Contains a wide and growing variety of Conan packages from contributors CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects POPULAR CONAN REMOTES ON BINTRAY CONAN-CENTER BINCRAFTERS 37
  • 79. • Moderated, curated and well-maintained packages • Place in which you can share your packages with the community • The Bincra ers team builds binary so ware packages for the OSS community • Contains a wide and growing variety of Conan packages from contributors • Created by Conan developers • An incubator for maturing packages CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects POPULAR CONAN REMOTES ON BINTRAY CONAN-CENTER BINCRAFTERS CONAN-COMMUNITY 37
  • 80. • Usually project/library name • Owner of the package version • Namespace that allows di erent users to have their own packages for the same library with the same name • Any string • Allows di erent packages for the same library • Usually denote the maturity of a package ("stable", "testing") package_name/package_version@user/channel CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE IDENTIFIER PACKAGE_NAME USER PACKAGE_VERSION CHANNEL 38
  • 81. • gtest/1.8.0@bincra ers/stable CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 39
  • 82. • gtest/1.8.0@bincra ers/stable CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 40
  • 83. • List packages in the local cache conan search CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SEARCHING FOR PACKAGES 41
  • 84. • List packages in the local cache conan search • List all gtest packages in the conan-center conan search gtest -r conan-center CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SEARCHING FOR PACKAGES 41
  • 85. • List packages in the local cache conan search • List all gtest packages in the conan-center conan search gtest -r conan-center • Inspect binary package details conan search gtest/1.8.1@bincrafters/stable CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SEARCHING FOR PACKAGES 41
  • 86. • List packages in the local cache conan search • List all gtest packages in the conan-center conan search gtest -r conan-center • Inspect binary package details conan search gtest/1.8.1@bincrafters/stable • Generate table of binary packages of gtest/1.8.1@bincrafters/stable on conan-center conan search gtest/1.8.1@bincrafters/stable -r conan-center --table=conan_matrix.html CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SEARCHING FOR PACKAGES 41
  • 87. conan search gtest/1.8.1@bincrafters/stable Existing packages for recipe gtest/1.8.1@bincrafters/stable: Package_ID: a4062ec0208a59375ac653551e662b6cc469fe58 [options] build_gmock: True fPIC: True shared: False [settings] arch: x86_64 build_type: Release compiler: gcc compiler.libcxx: libstdc++11 compiler.version: 7 os: Linux Outdated from recipe: False CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE DETAILS 42
  • 88. • Inspect your current project’s dependencies conan info .. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSPECTING DEPENDENCIES 43
  • 89. • Inspect your current project’s dependencies conan info .. • Inspect dependencies of OpenSSL/1.1.0i@conan/stable conan info OpenSSL/1.1.0i@conan/stable CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSPECTING DEPENDENCIES 43
  • 90. • Inspect your current project’s dependencies conan info .. • Inspect dependencies of OpenSSL/1.1.0i@conan/stable conan info OpenSSL/1.1.0i@conan/stable • Generates a graph of dependencies of current project conan info .. --graph=graph.html CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSPECTING DEPENDENCIES 43
  • 91. OpenSSL/1.1.0i@conan/stable ID: 75b541b6f810787a4fcee8cec9cfdbf95959366f BuildID: None Remote: conan-center=https://conan.bintray.com URL: http://github.com/lasote/conan-openssl License: The current OpenSSL licence is an 'Apache style' license: https://www.openssl.org/source/license.html Recipe: Downloaded Binary: Download Binary remote: conan-center Creation date: 2018-08-28 09:02:07 Required by: PROJECT Requires: zlib/1.2.11@conan/stable zlib/1.2.11@conan/stable ID: 6ae331b72e7e265ca2a3d1d8246faf73aa030238 BuildID: None Remote: conan-center=https://conan.bintray.com URL: http://github.com/lasote/conan-zlib License: http://www.zlib.net/zlib_license.html Recipe: Cache Binary: Cache Binary remote: conan-center Creation date: 2018-08-30 15:46:32 Required by: OpenSSL/1.1.0i@conan/stable CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE DEPENDENCIES 44
  • 92. • Installing specific package by hand conan install gtest/1.8.1@bincrafters/stable -g cmake CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSTALLING DEPENDENCIES 45
  • 93. • Installing specific package by hand conan install gtest/1.8.1@bincrafters/stable -g cmake • Usage of project-specific conanfile.txt or conanfile.py files conan install .. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSTALLING DEPENDENCIES 45
  • 94. • Installing specific package by hand conan install gtest/1.8.1@bincrafters/stable -g cmake • Usage of project-specific conanfile.txt or conanfile.py files conan install .. • Optionally a profile name can be provided (otherwise profile named default is used) conan install .. -pr vs2017 CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSTALLING DEPENDENCIES 45
  • 95. • Installing specific package by hand conan install gtest/1.8.1@bincrafters/stable -g cmake • Usage of project-specific conanfile.txt or conanfile.py files conan install .. • Optionally a profile name can be provided (otherwise profile named default is used) conan install .. -pr vs2017 • Build missing packages from sources (if prebuilt ones are not available) conan install .. -b missing CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects INSTALLING DEPENDENCIES 45
  • 96. [requires] Foo/1.0@my_channel/stable gtest/1.8.1@bincrafters/stable [options] [generators] cmake • cmake generator creates conanbuildinfo.cmake file that defines – CMake variables (module paths include paths, library names, ...) – CMake helper functions (defining targets, configuring CMake environment, ...) • Many di erent generators provided to support any build system CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN FILES CONANFILE.TXT 46
  • 97. [requires] Foo/1.0@my_channel/stable gtest/1.8.1@bincrafters/stable [options] [generators] cmake from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) generators = "cmake" • cmake generator creates conanbuildinfo.cmake file that defines – CMake variables (module paths include paths, library names, ...) – CMake helper functions (defining targets, configuring CMake environment, ...) • Many di erent generators provided to support any build system CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN FILES CONANFILE.TXT CONANFILE.PY 46
  • 98. [requires] Foo/1.0@my_channel/stable gtest/1.8.1@bincrafters/stable [options] gtest:shared=True [generators] cmake from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) default_options = "gtest:shared=True" generators = "cmake" CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SETTING PACKAGE OPTIONS CONANFILE.TXT CONANFILE.PY 47
  • 99. [requires] Foo/1.0@my_channel/stable gtest/1.8.1@bincrafters/stable [options] gtest:shared=True [generators] cmake from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) default_options = "gtest:shared=True" generators = "cmake" • Command line override conan install .. -o gtest:shared=True conan install .. -o *:shared=True CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SETTING PACKAGE OPTIONS CONANFILE.TXT CONANFILE.PY 47
  • 100. [requires] Foo/1.0@my_channel/stable gtest/1.8.1@bincrafters/stable [options] gtest:shared=True [generators] cmake [imports] bin, *.dll -> ./bin lib, *.dylib* -> ./bin from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) default_options = "gtest:shared=True" generators = "cmake" def imports(self): self.copy("*.dll", dst="bin", src="bin") self.copy("*.dylib*", dst="bin", src="lib") • Copies all *.dll files from packages bin folder to my ./bin folder • Copies all *.dylib* files from packages lib folder to my ./bin folder CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects FIXING IMPORTS FOR SHARED LIBRARIES CONANFILE.TXT CONANFILE.PY 48
  • 101. from conans import ConanFile, CMake class MyLibraryConan(ConanFile): settings = "os", "compiler", "build_type", "arch" requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) generators = "cmake" def build(self): cmake = CMake(self) cmake.configure() cmake.build() cmake.install() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MORE POWER WITH CONANFILE.PY 49
  • 102. from conans import ConanFile, CMake class MyLibraryConan(ConanFile): settings = "os", "compiler", "build_type", "arch" requires = ( "Foo/1.0@my_channel/stable", "gtest/1.8.1@bincrafters/stable" ) generators = "cmake" def build(self): cmake = CMake(self) cmake.configure() cmake.build() cmake.install() conan install . -pr vs2017 --install-folder build conan build . --build-folder build CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MORE POWER WITH CONANFILE.PY 49
  • 103. from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "gtest/1.8.1@bincrafters/stable" ) options = { "testing": [True, False] } default_options = "testing=False" generators = "cmake" def requirements(self): if self.options.testing: self.requires("Foo/2.0@my_channel/testing") else: self.requires("Foo/1.0@my_channel/stable") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MORE POWER WITH CONANFILE.PY 50
  • 104. from conans import ConanFile class MyLibraryConan(ConanFile): requires = ( "gtest/1.8.1@bincrafters/stable" ) options = { "testing": [True, False] } default_options = "testing=False" generators = "cmake" def requirements(self): if self.options.testing: self.requires("Foo/2.0@my_channel/testing") else: self.requires("Foo/1.0@my_channel/stable") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects MORE POWER WITH CONANFILE.PY And many more features... 50
  • 105. [settings] setting=value [options] MyLib:shared=True [env] env_var=value [build_requires] Tool1/0.1@user/channel • Stored in the default profile folder or anywhere in a project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PROFILES 51
  • 106. conan profile show clang6 Configuration for profile clang6: [settings] os=Linux os_build=Linux arch=x86_64 arch_build=x86_64 compiler=clang compiler.version=6.0 compiler.libcxx=libstdc++11 build_type=Release [options] [build_requires] [env] CC=/usr/bin/clang CXX=/usr/bin/clang++ CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PROFILES 52
  • 107. conan profile show clang6 Configuration for profile clang6: [settings] os=Linux os_build=Linux arch=x86_64 arch_build=x86_64 compiler=clang compiler.version=6.0 compiler.libcxx=libstdc++11 build_type=Release [options] [build_requires] [env] CC=/usr/bin/clang CXX=/usr/bin/clang++ • Easy to override or extend the profile conan install .. -pr clang6 -s build_type=Debug CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PROFILES 52
  • 108. • Example of a Conan profile file distributed with the project source code include(default) [settings] build_type=Debug [options] Poco:shared=True Poco:enable_apacheconnector=False OpenSSL:shared=True conan install .. -pr=../debug_shared CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PROFILES DEBUG_SHARED 53
  • 109. 1 The package recipe independent of the project source code repository • packaging 3rd party libraries CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 54
  • 110. 1 The package recipe independent of the project source code repository 2 The package recipe inside the project source code repository • packaging own library • 2 approaches – exporting snapshot of the source code together with the recipe – exporting only the recipe and obtaining the source code from the project repository CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 55
  • 111. 1 The package recipe independent of the project source code repository 2 The package recipe inside the project source code repository 3 Packaging existing binaries • only prebuilt 3rd party binaries are available • artifacts already built and no need to rebuild CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 56
  • 112. 1 The package recipe independent of the project source code repository 2 The package recipe inside the project source code repository 3 Packaging existing binaries 4 Packaging of the development tools • distribution of the development tools needed to build the project (i.e. CMake, nasm) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGES 57
  • 113. • ./conanfile.py – main recipe file used to create a package • ./LICENSE – licence of the Conan package source code – not to be confused with a license of a packaged project • ./test_package – simple package consumer project • ./test_package/conanfile.py – recipe of a testing project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects TYPICAL CONAN PACKAGE CONTENTS 58
  • 114. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" # ... • Basic package information CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 59
  • 115. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" # ... • The license of the packaged project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 60
  • 116. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" url = "https://github.com/mpusz/conan-google-benchmark" # ... • Link to the Conan package repository (not the packaged project) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 61
  • 117. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" url = "https://github.com/mpusz/conan-google-benchmark" exports = ["LICENSE"] # ... • Copies a packaging code license to be stored within a package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 62
  • 118. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" url = "https://github.com/mpusz/conan-google-benchmark" exports = ["LICENSE"] settings = "os", "arch", "compiler", "build_type" # ... • The configuration of the di erent binary packages • Any change in those parameters will generate a di erent binary package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 63
  • 119. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" url = "https://github.com/mpusz/conan-google-benchmark" exports = ["LICENSE"] settings = "os", "arch", "compiler", "build_type" options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } default_options = ("shared=False", "exceptions=True", "lto=False") # ... • Package options and their default values • Any change in those parameters will generate a di erent binary package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 64
  • 120. class GoogleBenchmarkConan(ConanFile): name = "google-benchmark" version = "1.4.1" description = "A microbenchmark support library" license = "https://github.com/google/benchmark/blob/master/LICENSE" url = "https://github.com/mpusz/conan-google-benchmark" exports = ["LICENSE"] settings = "os", "arch", "compiler", "build_type" options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } default_options = ("shared=False", "exceptions=True", "lto=False") scm = { "type": "git", "url": "https://github.com/google/benchmark.git", "revision": "v%s" % version } # ... • Packaged project repository information • Used to obtain project source code to build and pack CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE ATTRIBUTES 65
  • 121. • Project-wide and common to all the dependencies in order for them to link together without issues with the project being built • Provided by the environment and cannot be defaulted in the recipe CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SETTINGS VS OPTIONS SETTINGS 66
  • 122. • Project-wide and common to all the dependencies in order for them to link together without issues with the project being built • Provided by the environment and cannot be defaulted in the recipe • Package-specific configuration and each dependency can provide di erent set of options to fulfill their configuration needs • Default option values provided in the recipe file can be overridden by the user CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SETTINGS VS OPTIONS SETTINGS OPTIONS 66
  • 123. • generators - not needed only if a package does not have any dependencies CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 67
  • 124. • generators - not needed only if a package does not have any dependencies • requires - package dependencies class MyLibConan(ConanFile): requires = (("Hello/[>1.0,<2.0]@user/testing"), ("Bye/2.1@coder/beta", "private"), ("Say/0.2@dummy/stable", "override")) – private - not exposed to clients, non-transitive – override - does not introduce a new dependency, overrides a version if it is a dependency already CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 67
  • 125. • generators - not needed only if a package does not have any dependencies • requires - package dependencies class MyLibConan(ConanFile): requires = (("Hello/[>1.0,<2.0]@user/testing"), ("Bye/2.1@coder/beta", "private"), ("Say/0.2@dummy/stable", "override")) – private - not exposed to clients, non-transitive – override - does not introduce a new dependency, overrides a version if it is a dependency already – version ranges >1.1,<2.1 # in such range 2.8 # equivalent to =2.8 ~=3.0 # compatible, according to semver >1.1 || 0.8 # conditions can be OR'ed CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 67
  • 126. • build_requires - build-time only dependencies – only needed to build a package from sources – dev tools, compilers, build systems, code analyzers, testing libraries, etc. – non-transitive CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 68
  • 127. • build_requires - build-time only dependencies – only needed to build a package from sources – dev tools, compilers, build systems, code analyzers, testing libraries, etc. – non-transitive • exports - copies provided files to be stored in a package along with the recipe CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 68
  • 128. • build_requires - build-time only dependencies – only needed to build a package from sources – dev tools, compilers, build systems, code analyzers, testing libraries, etc. – non-transitive • exports - copies provided files to be stored in a package along with the recipe • exports_sources - creates a snapshot of the packaged project source code in the package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 68
  • 129. • build_requires - build-time only dependencies – only needed to build a package from sources – dev tools, compilers, build systems, code analyzers, testing libraries, etc. – non-transitive • exports - copies provided files to be stored in a package along with the recipe • exports_sources - creates a snapshot of the packaged project source code in the package • homepage - the home web page of the library being packaged CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL ATTRIBUTES 68
  • 130. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): cmake = CMake(self) cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF" cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF" cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF" cmake.configure() return cmake # ... • Helper method to configure CMake CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 69
  • 131. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): cmake = CMake(self) cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF" cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF" cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF" cmake.configure() return cmake # ... • CMake helper automatically appends some definitions based on popular settings CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 70
  • 132. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): cmake = CMake(self) cmake.definitions["BENCHMARK_ENABLE_EXCEPTIONS"] = "ON" if self.options.exceptions else "OFF" cmake.definitions["BENCHMARK_ENABLE_LTO"] = "ON" if self.options.lto else "OFF" cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "OFF" cmake.configure() return cmake # ... • Compiling unit tests makes the packaging longer and does not influence the binary package content CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 71
  • 133. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): # ... def build(self): cmake = self._configure_cmake() cmake.build() # ... • Configures and builds the project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 72
  • 134. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): # ... def build(self): cmake = self._configure_cmake() cmake.build() def package(self): cmake = self._configure_cmake() cmake.install() self.copy("license*", dst="licenses", ignore_case=True, keep_path=False) # ... • Copies packaged project binaries, CMake configuration files, and its license to the package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 73
  • 135. class GoogleBenchmarkConan(ConanFile): options = { "shared": [True, False], "exceptions": [True, False], "lto": [True, False], } # ... def _configure_cmake(self): # ... def build(self): cmake = self._configure_cmake() cmake.build() def package(self): cmake = self._configure_cmake() cmake.install() self.copy("license*", dst="licenses", ignore_case=True, keep_path=False) def package_info(self): self.cpp_info.libs = ["benchmark"] # ... • Defines the target that the consumer must link with when using this package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE RECIPE METHODS 74
  • 136. • source() - retrieves the source code from external origin (git, *.tgz, ...) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 137. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 138. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic • build_requirements() - defines dependencies used when the package is built from sources CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 139. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic • build_requirements() - defines dependencies used when the package is built from sources • imports() - copies files from the local store to client's project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 140. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic • build_requirements() - defines dependencies used when the package is built from sources • imports() - copies files from the local store to client's project • package_id() - creates a unique ID for the package def package_id(self): self.info.header_only() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 141. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic • build_requirements() - defines dependencies used when the package is built from sources • imports() - copies files from the local store to client's project • package_id() - creates a unique ID for the package def package_id(self): self.info.header_only() • configure() - configures or constrains/validates the available options in a package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 142. • source() - retrieves the source code from external origin (git, *.tgz, ...) • requirements() - defines more advanced requirements logic • build_requirements() - defines dependencies used when the package is built from sources • imports() - copies files from the local store to client's project • package_id() - creates a unique ID for the package def package_id(self): self.info.header_only() • configure() - configures or constrains/validates the available options in a package • config_options() - constrains the available options in a package, before they are given a value def config_options(self): if self.settings.os == 'Windows': del self.options.fPIC CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects OTHER USEFUL METHODS 75
  • 143. • test_package di ers from the library unit or integration tests (which should be more comprehensive) • Packaging tests to verify that – the package is properly created – the package consumers will be able to link against it and reuse it CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE test_package DIRECTORY 76
  • 144. from conans import ConanFile, CMake class GoogleBenchmarkTestConan(ConanFile): settings = "os", "arch", "compiler", "build_type" generators = "cmake_paths" def build(self): cmake = CMake(self) cmake.configure() cmake.build() def imports(self): self.copy("*.dll", dst="bin", src="bin") self.copy("*.dylib*", dst="bin", src="lib") self.copy('*.so*', dst='bin', src='lib') def test(self): cmake = CMake(self) self.run("ctest -VV -C %s" % cmake.build_type) CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE test_package RECIPE 77
  • 145. from conans import ConanFile, CMake class GoogleBenchmarkTestConan(ConanFile): settings = "os", "arch", "compiler", "build_type" generators = "cmake_paths" def build(self): cmake = CMake(self) cmake.configure() cmake.build() def imports(self): self.copy("*.dll", dst="bin", src="bin") self.copy("*.dylib*", dst="bin", src="lib") self.copy('*.so*', dst='bin', src='lib') def test(self): cmake = CMake(self) self.run("ctest -VV -C %s" % cmake.build_type) • requires attribute skipped as all needed information (e.g. project version) is automatically injected by Conan during the package creation CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects THE test_package RECIPE 77
  • 146. conan create . google-benchark/testing CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 147. conan create . google-benchark/testing • Exports the conanfile.py and files specified by the exports_sources field into the local cache CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 148. conan create . google-benchark/testing • Exports the conanfile.py and files specified by the exports_sources field into the local cache • Installs the package, forcing it to be built from the sources CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 149. conan create . google-benchark/testing • Exports the conanfile.py and files specified by the exports_sources field into the local cache • Installs the package, forcing it to be built from the sources • Creates a temporary ./test_package/build directory CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 150. conan create . google-benchark/testing • Exports the conanfile.py and files specified by the exports_sources field into the local cache • Installs the package, forcing it to be built from the sources • Creates a temporary ./test_package/build directory • Executes the conan install .., to install the requirements of the ./test_package/conanfile.py and the packaged project CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 151. conan create . google-benchark/testing • Exports the conanfile.py and files specified by the exports_sources field into the local cache • Installs the package, forcing it to be built from the sources • Creates a temporary ./test_package/build directory • Executes the conan install .., to install the requirements of the ./test_package/conanfile.py and the packaged project • Builds and launches the example consuming application, calling the ./test_package/conanfile.py build() and test() methods CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE CREATION 78
  • 152. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 153. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 154. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 155. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 156. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 157. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 158. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 159. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGING STEPS 79
  • 160. conan create . user/channel CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE 80
  • 161. conan create . user/channel conan create . user/channel --keep-source CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE 80
  • 162. conan create . user/channel conan create . user/channel --keep-source conan create . user/channel --keep-build CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE 80
  • 163. conan create . user/channel conan create . user/channel --keep-source conan create . user/channel --keep-build conan test test_package MyLibrary/0.1@user/channel CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: OUT-OF-SOURCE 80
  • 164. conan source . --source-folder=tmp/source conan install . --install-folder=tmp/build [--profile XXXX] conan build . --source-folder=tmp/source --build-folder=tmp/build conan package . --source-folder=tmp/source --build-folder=tmp/build --package-folder=tmp/package conan export-pkg . user/testing --package-folder=tmp/package conan test test_package MyLibrary/0.1@user/channel CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: IN-SOURCE 81
  • 165. conan source . --source-folder=tmp/source conan install . --install-folder=tmp/build [--profile XXXX] conan build . --source-folder=tmp/source --build-folder=tmp/build conan package . --source-folder=tmp/source --build-folder=tmp/build --package-folder=tmp/package conan export-pkg . user/testing --package-folder=tmp/package conan test test_package MyLibrary/0.1@user/channel CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects CONAN PACKAGE DEVELOPMENT FLOW: IN-SOURCE Even smaller steps are possible with --configure, --build, --install options of conan build command 81
  • 166. • Recipe should include description, license, and url attributes • Use only lowercase letters in package names • Include test_package • Raise errors on invalid configurations • Do not use version ranges • Distribute packaged so ware license files in binary packages • Export packaging code license • Use clean python code and the latest Conan features CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects PACKAGE QUALITY 82
  • 167. • If not done already, add the Conan remote conan remote add conan-mpusz https://bintray.com/mpusz/conan-mpusz CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects UPLOADING A PACKAGE 83
  • 168. • If not done already, add the Conan remote conan remote add conan-mpusz https://bintray.com/mpusz/conan-mpusz • Upload the package recipe and all binaries to the Conan remote conan upload google-benchmark/1.4.1@mpusz/stable -r conan_mpusz --all CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects UPLOADING A PACKAGE 83
  • 169. 1 cmake 2 cmake_multi 3 cmake_paths 4 cmake_find_package CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects USING CONAN WITH CMAKE: GENERATORS 84
  • 170. • Creates conanbuildinfo.cmake file that defines CMake – variables (module paths include paths, library names, ...) – helper functions (defining targets, configuring CMake environment, ...) • Changes to CMakeLists.txt file are required cmake_minimum_required(VERSION 3.5) project(MyLibrary) if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup(TARGETS) endif() # ... CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake GENERATOR 85
  • 171. • Creates conanbuildinfo_multi.cmake file • Used with multi-configuration environments like Visual Studio and Xcode • Does not configure for a specific build_type, like Debug or Release • Cannot be used to create packages and issues with find_package() usage • Changes to CMakeLists.txt file are required cmake_minimum_required(VERSION 3.5) project(MyLibrary) if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake) include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake) conan_basic_setup(TARGETS) endif() # ... CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_multi GENERATOR 86
  • 172. • Creates conan_paths.cmake file that defines – CMAKE_MODULE_PATH used by find_package() to find CMake configuration and FindXXX.cmake files – CMAKE_PREFIX_PATH used by find_library() to locate library files in packages cmake_minimum_required(VERSION 3.5) project(MyLibrary) include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL) # ... CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_paths GENERATOR 87
  • 173. • Creates conan_paths.cmake file that defines – CMAKE_MODULE_PATH used by find_package() to find CMake configuration and FindXXX.cmake files – CMAKE_PREFIX_PATH used by find_library() to locate library files in packages cmake_minimum_required(VERSION 3.5) project(MyLibrary) include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL) # ... • Can work without doing any changes to CMake files cmake .. -DCMAKE_PROJECT_MyLibrary_INCLUDE=conan_paths.cmake -DCMAKE_BUILD_TYPE=Release CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_paths GENERATOR 87
  • 174. • Creates Find<package_name>.cmake file for each requirement specified in the conanfile • When used with CMake helper in build() method of conanfile.py no changes to CMake files needed CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_find_package GENERATOR 88
  • 175. • Creates Find<package_name>.cmake file for each requirement specified in the conanfile • When used with CMake helper in build() method of conanfile.py no changes to CMake files needed • When used with conanfile.txt – can be used together with cmake_paths generator [requires] ... [generators] cmake_find_package cmake_paths CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_find_package GENERATOR 88
  • 176. • Creates Find<package_name>.cmake file for each requirement specified in the conanfile • When used with CMake helper in build() method of conanfile.py no changes to CMake files needed • When used with conanfile.txt – can be used together with cmake_paths generator [requires] ... [generators] cmake_find_package cmake_paths – CMAKE_MODULE_PATH can be updated manually cmake_minimum_required(VERSION 3.5) project(MyLibrary) set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH}) # ... CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects cmake_find_package GENERATOR 88
  • 177. class MyLibraryConan(ConanFile): # ... generators = "cmake" def source(self): # ... tools.replace_in_file("%s/CMakeLists.txt" % self.subfolder, "project(MyLibrary)", """project(MyLibrary) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup()""") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: AUTOMATION OF UPDATING CMakeLists.txt FILE OPTION 1 89
  • 178. class MyLibraryConan(ConanFile): # ... exports_sources = ["CMakeLists.txt"] generators = "cmake" scm = { "type": "git", "subfolder": "source_subfolder", # ... } project(MyLibraryWrapper) include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() add_subdirectory("source_subfolder") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: AUTOMATION OF UPDATING CMakeLists.txt FILE OPTION 2 90
  • 179. class MyLibraryConan(ConanFile): # ... generators = "cmake_paths" def build_requirements(self): if tools.get_env("CONAN_RUN_TESTS", False): self.build_requires("gtest/1.8.1@bincrafters/stable") def _configure_cmake(self): cmake = CMake(self) # ... cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "ON" if tools.get_env("CONAN_RUN_TESTS", False) else "OFF" cmake.definitions["CMAKE_PROJECT_benchmark_INCLUDE"] = "conan_paths.cmake" cmake.configure() return cmake def build(self): cmake = self._configure_cmake() cmake.build() if tools.get_env("CONAN_RUN_TESTS", False): cmake.test() CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: HOW TO RUN UNIT TESTS DURING PACKAGING PROCESS? 91
  • 180. class MyLibraryConan(ConanFile): # ... generators = "cmake_paths" def build_requirements(self): if tools.get_env("CONAN_RUN_TESTS", False): self.build_requires("gtest/1.8.1@bincrafters/stable") def _configure_cmake(self): cmake = CMake(self) # ... cmake.definitions["BENCHMARK_ENABLE_TESTING"] = "ON" if tools.get_env("CONAN_RUN_TESTS", False) else "OFF" cmake.definitions["CMAKE_PROJECT_benchmark_INCLUDE"] = "conan_paths.cmake" cmake.configure() return cmake def build(self): cmake = self._configure_cmake() cmake.build() if tools.get_env("CONAN_RUN_TESTS", False): cmake.test() • To enable building and running tests define environment variable, set it in a profile file, or run conan create . user/channel -e CONAN_RUN_TESTS=1 CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: HOW TO RUN UNIT TESTS DURING PACKAGING PROCESS? 91
  • 181. • Package is asocial (does not have CMake configuration files or FindXXX.cmake file) • Existing "o icial" FindXXX.cmake file is not able to find our libraries CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: CUSTOM FindXXX.cmake FILE REASON 92
  • 182. • Package is asocial (does not have CMake configuration files or FindXXX.cmake file) • Existing "o icial" FindXXX.cmake file is not able to find our libraries • Create a custom FindXXX.cmake file in package root folder and export it with the package class MyLibraryConan(ConanFile): name = "my-library" version = "0.1" # ... exports_sources = ["FindMyLibrary.cmake"] def package(self): # ... self.copy("FindMyLibrary.cmake", ".", ".") CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: CUSTOM FindXXX.cmake FILE REASON BKM 92
  • 183. class MyLibraryConan(ConanFile): name = "my-library" version = "0.1" something = "" some_dynamic_path = "" def source(self): self.something = "Hello" some_dynamic_path = find_some_dynamic_path() def build(self): print(self.something) use_some_dynamic_path(some_dynamic_path) def package(self): print(self.something) use_some_dynamic_path(some_dynamic_path) • Works fine with conan create . user/channel because it calls all the methods in sequence • Problems with commands that execute only a part of the sequence – i.e. the source() and build() methods won’t be called at all when you run conan package command CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects BKM: DO NOT USE CLASS VARIABLES TO SHARE STATE 93
  • 184. CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SUMMARY 94
  • 185. • Many projects still do not use CMake at all • Many projects still do not use CMake in a Modern way • Many projects still do not provide installation option with proper CMake configuration files generation CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SUMMARY CMAKE 94
  • 186. • Many projects still do not use CMake at all • Many projects still do not use CMake in a Modern way • Many projects still do not provide installation option with proper CMake configuration files generation • Production quality Package Manager designed with C++ in mind • For free and on MIT license • Easy to use and the documentation is really good CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SUMMARY CMAKE CONAN 94
  • 187. • Many projects still do not use CMake at all • Many projects still do not use CMake in a Modern way • Many projects still do not provide installation option with proper CMake configuration files generation • Production quality Package Manager designed with C++ in mind • For free and on MIT license • Easy to use and the documentation is really good • Give it a try! pip install conan CppCon 2018 | Git, CMake, Conan - How to ship and reuse our C++ projects SUMMARY CMAKE CONAN 94