question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[question] Test and documentation of a header-only package

See original GitHub issue

I am trying to package a header-only library, but I encounter a two main problems.

At least this works: Local testing and documentation

First, I succeeded to get these command work and do what I want them to do:

conan install conan/conanfile.py --build=missing --install-folder=build -pr:b=conan/profiles/clang_13 -pr:h=conan/profiles/clang_13
cd build
cmake -D CMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake ..
cmake --build .
ctest
make docs

The dependencies are successfully installed on my mac, the tests are built, run and I can generate the documentation with doxygen.

Problem 1: dependencies conflict between OS

Even if I like running things locally for testing, I also happen to use Github workflows that run on Ubuntu 22.04. In this case, the previous commands fail, and so I had to tweak the dependencies versions in my conan/conanfile.py, where I ended up with these lines:

    # zlib overriden because conflict on ubuntu 22.04 (gh-actions)
    #requires = "boost/1.80.0", "gdal/3.5.2", "zlib/1.2.13"
    # on my Macos Monterey clang 13
    requires = "boost/[>1.75 <1.80]", "gdal/[>=3.4.0]"

How to do that properly with Conan?

Problem 2: Creating the package fails

I would like to make the package available on Artifactory. I would like the packaging method to build and run the unit tests. So I tried to run conan create ./conan demo/testing -pr:b=conan/profiles/clang_13 -pr:h=conan/profiles/clang_13. It fails with the following error:

quetzal-CoaTL/0.1@demo/testing: Generator 'CMakeDeps' calling 'generate()'
quetzal-CoaTL/0.1@demo/testing: Calling generate()
quetzal-CoaTL/0.1@demo/testing: Aggregating env generators
quetzal-CoaTL/0.1@demo/testing: Calling build()
CMake Error: The source directory "/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/source" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.
quetzal-CoaTL/0.1@demo/testing: 
quetzal-CoaTL/0.1@demo/testing: ERROR: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' build failed
quetzal-CoaTL/0.1@demo/testing: WARN: Build folder /Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
ERROR: quetzal-CoaTL/0.1@demo/testing: Error in build() method, line 33
	cmake.configure()
	ConanException: Error 1 while executing cd '/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' && cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_OSX_SYSROOT="/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="apple-clang" -DCONAN_COMPILER_VERSION="13" -DCONAN_LIBCXX="libc++" -DCMAKE_INSTALL_PREFIX="/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/package/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_PREFIX_PATH="/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9" -DCMAKE_MODULE_PATH="/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -DCONAN_EXPORTED="1" -Wno-dev '/Users/arnaudbecheler/.conan/data/quetzal-CoaTL/0.1/demo/testing/source'

I tried to modify the generate and the build functions, but without success: I do not understand what causes this behavior. Any idea?

Here is my conanfile.py:

from conans import ConanFile, CMake
from conan.tools.cmake import CMakeDeps

class QuetzalCoaTLConan(ConanFile):
    name = "quetzal-CoaTL"
    url = "https://github.com/Quetzal-framework/quetzal-CoaTL"
    license = "GPLv3"
    description = "Quetzal Coalescence Template Library"
    version = "0.1"
    settings = "os", "compiler", "arch", "build_type"
    exports_sources = "include/*", "CMakeLists.txt", "test/*", "cmake/*", "docs/*"
    no_copy_source = True
    generators = "cmake", "CMakeToolchain", "CMakeDeps"
    # zlib overriden because conflict on ubuntu 22.04 (gh-actions)
    #requires = "boost/1.80.0", "gdal/3.5.2", "zlib/1.2.13"
    # on Macos Monterey clang 13
    requires = "boost/[>1.75 <1.80]", "gdal/[>=3.4.0]"

    # by default, the config files (xxx-config.cmake) files are not generated: see generate()
    tool_requires = "cmake/3.22.0", "doxygen/1.9.2"

    # run after the installation of the dependency graph, but before build() method
    def generate(self):
        # generate one xxxx-config.cmake file per dependency
        cmake = CMakeDeps(self)
        # generate the config files for the tool require
        cmake.build_context_activated = ["cmake/3.22.0", "doxygen/1.9.2"]
        cmake.generate()

    # this is not building a library, just tests
    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
        # run CTest, launch binaries, etc
        cmake.test(output_on_failure=True)

    def package(self):
        self.copy("*.h")

    def package_id(self):
        self.info.clear()

Thanks!

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
memshardedcommented, Nov 29, 2022

A few issues:

  • You are still relying on your CMakeLists.txt on include(.../conanbuildinfo.cmake) which has been deprecated, it is part of the cmake legacy generator that you just removed. You need to remove that line, and rely on standard find_package(XXX CONFIG REQUIRES) instructions and then target_link_libraries(.... xxx::xxx)
  • You are using only the build profile which doesn’t sound right. Typically you specify both, the host and build one.
1reaction
memshardedcommented, Nov 28, 2022

I wonder if this is why the include files are not found in the conan create call.

It could be that the self.copy("*.h") is not copying correctly the headers. You can paste here the output, it should have some messages summarizing what has been copied. It is also possible to inspect the final package folders, if you read the output and go to your disk package folder, you should be able to package the navigated files.

In any case, it is also recommended to modernize that self.copy() to copy(self, ... which will be more explicit, requiring to define src folder (typically something from self.build_folder) and dst folder (typically something with self.package_folder).

Please check that regarding the final packaged artifacts and let us know.

Regarding the build() method, the only thing that has changed is the .test(). You can import from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, and use all the same, except cmake.test(), because output_on_failure=True has been removed (you can control it externally with an env-var CTEST_OUTPUT_ON_FAILURE in your profile, for example in [buildenv] section)

Read more comments on GitHub >

github_iconTop Results From Across the Web

GitHub - catchorg/Catch2
A modern, C++-native, test framework for unit-tests, TDD and BDD - using C++14, C++17 and later (C++11 support is in v2.x branch, and...
Read more >
How to create a conan package including tests for a header ...
I'm trying to create a header only package with conan which is based on CMake and CTest. It's as empty as possible and...
Read more >
How to package header-only libraries - Conan Docs
Packaging a header only library, without requiring to build and run unit tests for it within Conan, can be done with a very...
Read more >
CMake line by line - creating a header-only library
CMake can be hard to figure out. I love CMake, but unfortunately, its documentation is more focused on completeness than on providing ...
Read more >
Tutorial
The full source for Catch2, including test projects, documentation, ... If you have installed Catch2 from system package manager, or CMake package, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found