Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to validate cppstd and compiler for header-only packages? #12210

Closed
1 task done
uilianries opened this issue Sep 28, 2022 · 3 comments
Closed
1 task done
Assignees
Milestone

Comments

@uilianries
Copy link
Member

uilianries commented Sep 28, 2022

Using the new validate approach recommended by the migration guide, we should use self.info.settings instead of self.settings as in the past. Also, for header-only projects, we should consider self.info.clear() instead of self.info.header_only()

However, we have cases where we need to check the cppstd or even the compiler version to be sure that's is enough, for instance:

from conan import ConanFile
from conan.errors import ConanInvalidConfiguration
from conan.tools.files import get, copy
from conan.tools.build import check_min_cppstd
from conan.tools.scm import Version
from conan.tools.layout import basic_layout

import os

required_conan_version = ">=1.52.0"

class PackageConan(ConanFile):
    settings = "os", "arch", "compiler", "build_type"
    no_copy_source = True

    @property
    def _minimum_cpp_standard(self):
        return 17

    @property
    def _compilers_minimum_version(self):
        return {
            "Visual Studio": "15.7",
            "msvc": "191",
            "gcc": "7",
            "clang": "7",
            "apple-clang": "10",
        }

    def layout(self):
        basic_layout(self, src_folder="src")

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

    def validate(self):
        if self.info.settings.get_safe("compiler.cppstd"):
            check_min_cppstd(self, self._minimum_cpp_standard)
        minimum_version = self._compilers_minimum_version.get(str(self.info.settings.compiler), False)
        if minimum_version and Version(self.info.settings.get_safe("compiler.version")) < minimum_version:
            raise ConanInvalidConfiguration(f"{self.ref} requires C++{self._minimum_cpp_standard}, which your compiler does not support.")

    def source(self):
        get(self, **self.conan_data["sources"][self.version],
            destination=self.source_folder, strip_root=True)

    def build(self):
        pass

    def package(self):
        copy(self, pattern="LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
        copy(self, pattern="*.h", dst=os.path.join(self.package_folder, "include"), src=os.path.join(self.source_folder, "include"))

    def package_info(self):
        self.cpp_info.bindirs = []
        self.cpp_info.frameworkdirs = []
        self.cpp_info.libdirs = []
        self.cpp_info.resdirs = []

This is a very regular header-only recipe from CCI. The important thing here is: it requires C++17, and at least some new compiler version.

When using the new self.info.clear, then self.info.settings.compiler will no longer exist when running conan create which is fine, since we are only copying files to the package. However, when running conan install to get that pre-built package, the same occurs, because the package ID is unique, so the user will not check that validate and eventually will fail during the compilation due an older and incompatible compiler.

So the question is, how to validate the compiler in this case?

@uilianries uilianries changed the title [question] How to validate cppstd for header-only packages? [question] How to validate cppstd and compiler for header-only packages? Sep 28, 2022
@danimtb
Copy link
Member

danimtb commented Oct 6, 2022

I have created a PR to the docs clarifying this case. As there still some uncertainties around how to manage cppstd, using self.settings in validate() is still the recommended way to do it. See conan-io/docs#2778

@memsharded
Copy link
Member

memsharded commented Nov 17, 2022

Will be closed by #12555

@memsharded
Copy link
Member

Closed by #12555, will be in beta.6

And for 1.55 the approach is: use self.settings in validate(), not self.info.settings

conan-center-bot pushed a commit to conan-io/conan-center-index that referenced this issue Nov 28, 2022
* polymorphic_value: conan v2 support

done as part of @prince-chrismc's webinar

* apply review suggestions

* work around self.info being already cleared

see conan-io/conan#12210

* use conan.tools.build.can_run

* Revert "work around self.info being already cleared"

This reverts commit 84f85e4.

* Apply suggestions from code review

Co-authored-by: Uilian Ries <uilianries@gmail.com>

* align test package to templates

Co-authored-by: Uilian Ries <uilianries@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants