0

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 just contains two tests with empty main() functions.

While conan create . conantest/stable works fine on linux it stop with the error

conantest/0.1@conantest/stable: ERROR: Package '...' build failed
conantest/0.1@conantest/stable: WARN: Build folder C:\Users\...\.conan\data\conantest\0.1\conantest\stable\build\...
ERROR: conantest/0.1@conantest/stable: Error in build() method, line 17
        cmake.test()
        ConanException: Error 1 while executing cmake --build "C:\Users\...\.conan\data\conantest\0.1\conantest\stable\build\..." --target test

on windows.

The problem is that on windows with this example cmake.test() doesn't invoke the multi-config RUN_TESTS target as described here but test.

From the docs i got the impression that it should distinguish automatically but i also don't know if that's correct or what's missing to enable this automatism.

It is possible to hint at the build type by providing settings:

settings = "os", "compiler", "build_type", "arch"

When Conan generates a compiled binary for a package with a given combination of the settings above, it generates a unique ID for that binary by hashing the current values of these settings.

but as described here usually those are set to None explicitely for header only libraries.

As i understand it, the tests are only built and executed during packaging but their binary form is not part of the package. Is there a way to define the recipe for only one unique ID while still being able to build and run the tests during packaging on any platform?

The test project is available on git.

These are its contents:

CMakeLists.txt

cmake_minimum_required(VERSION 3.8.0)
project(conantest VERSION 0.1 LANGUAGES CXX)

include(CTest)
enable_testing()

add_executable(conantest1 conantest1.cpp)
add_test(conantest1 conantest1)
add_executable(conantest2 conantest2.cpp)
add_test(conantest2 conantest2)

conanfile.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from conans import ConanFile, CMake

class ConantestConan(ConanFile):
    name = "conantest"
    version = "0.1"
    generators = "cmake"
    exports_sources = "*"
    no_copy_source = True

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()
        cmake.test()
    def package(self):
        self.copy("*.hpp")
    def package_id(self):
        self.info.header_only()

conantest1.cpp and conantest2.cpp

#include <cstdlib>
int main() {
    return EXIT_SUCCESS;
}
jesses
  • 559
  • 3
  • 15
  • You can try adding config name as args `cmake.test(args=['--', 'ARGS=--config %s'] % self.settings.build_type)` – uilianries Apr 12 '21 at 17:31
  • @uilianries from [the description](https://docs.conan.io/en/latest/reference/build_helpers/cmake.html#test) i got the impression that conan would choose the right target automatically: "If not defined RUN_TESTS or test will be used". I also found the [cmake property GENERATOR_IS_MULTI_CONFIG](https://cmake.org/cmake/help/latest/prop_gbl/GENERATOR_IS_MULTI_CONFIG.html) and some mentions of [conanbuildinfo_multi.cmake](https://docs.conan.io/en/1.7/integrations/cmake/cmake_multi_generator.html#creating-packages) but tbh i'm not sure if this is even remotly related. – jesses Apr 12 '21 at 17:53
  • 1
    The problem is you don't have settings declared in your recipe. I still can use same package ID. Without settings, Conan has no hint about build type. – uilianries Apr 12 '21 at 18:15
  • @uilianries i edited the question. As far as i understand the tests are only built and run during packaging but are themselfes in binary form not part of the package. Artificially adding a unique ID for binaries which are not part of the package seems wrong. Is there a way to specify those settings only for the Ctest part of the build? Here's the [settings documentation](https://docs.conan.io/en/latest/reference/conanfile/attributes.html#settings) for reference. – jesses Apr 12 '21 at 21:01
  • No. Because your package is header-only, it will use the same package ID, doesn't matter what build type did you use. Still, if you want to build with a specific build type, you MUST add settings. The package ID will be the same, but at least you can control between Release/Debug for your unit tests, Conan was not designed for such advanced CTest settings, you are mixing different thing. – uilianries Apr 13 '21 at 12:18
  • What you can do is moving your tests to [test_package](https://docs.conan.io/en/latest/creating_packages/getting_started.html#the-test-package-folder) and running from there. Test package can use full settings and consume your header-only. However, test_package is not designed for unit tests, but for package validation only (run a simple test). – uilianries Apr 13 '21 at 12:19

0 Answers0