From dcc5bd66f199aaa2bab2f93092180537d5e982b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Oct 2024 09:20:04 +0100 Subject: [PATCH 1/2] Test to show that a static-library recipe won't change its pkg_type when header_only option is used --- .../graph/core/test_auto_package_type.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/integration/graph/core/test_auto_package_type.py b/test/integration/graph/core/test_auto_package_type.py index 01db28fe8fd..d3457aa3591 100644 --- a/test/integration/graph/core/test_auto_package_type.py +++ b/test/integration/graph/core/test_auto_package_type.py @@ -1,3 +1,5 @@ +import textwrap + import pytest from conan.test.utils.tools import TestClient @@ -43,3 +45,19 @@ def test_auto_package_type(conanfile): assert "package_type: header-library" in c.out c.run("graph info . --filter package_type -o header_only=True -o shared=False") assert "package_type: header-library" in c.out + +def test_package_type_and_header_library(): + """ Show that forcing a package_type and header_only=True does not change the package_type""" + tc = TestClient(light=True) + tc.save({"conanfile.py": textwrap.dedent(""" + from conan import ConanFile + + class Pkg(ConanFile): + package_type = "static-library" + options = {"header_only": [True, False]} + + """)}) + tc.run("graph info . --filter package_type -o &:header_only=False") + assert "package_type: static-library" in tc.out + tc.run("graph info . --filter package_type -o &:header_only=True") + assert "package_type: static-library" in tc.out From a82b368b1b4467c629504f85726efed2601f677b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abril=20Rinc=C3=B3n=20Blanco?= Date: Mon, 28 Oct 2024 09:49:50 +0100 Subject: [PATCH 2/2] Add warning --- conans/model/pkg_type.py | 5 +++++ test/integration/graph/core/test_auto_package_type.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/conans/model/pkg_type.py b/conans/model/pkg_type.py index 29c207952c5..d850d03d4ba 100644 --- a/conans/model/pkg_type.py +++ b/conans/model/pkg_type.py @@ -58,6 +58,11 @@ def deduce_from_options(): if conanfile_type is PackageType.UNKNOWN: raise ConanException(f"{conanfile}: Package type is 'library'," " but no 'shared' option declared") + elif any(option in conanfile.options for option in ["shared", "header_only"]): + conanfile.output.warning(f"{conanfile}: package_type '{conanfile_type}' is defined, " + "but 'shared' and/or 'header_only' options are present. " + "The package_type will have precedence over the options " + "regardless of their value.") conanfile.package_type = conanfile_type else: # automatic default detection with option shared/header-only conanfile.package_type = deduce_from_options() diff --git a/test/integration/graph/core/test_auto_package_type.py b/test/integration/graph/core/test_auto_package_type.py index d3457aa3591..9e12c11ccfa 100644 --- a/test/integration/graph/core/test_auto_package_type.py +++ b/test/integration/graph/core/test_auto_package_type.py @@ -38,6 +38,7 @@ def test_auto_package_type(conanfile): c.run("graph info . --filter package_type") assert "package_type: static-library" in c.out c.run("graph info . --filter package_type -o shared=True") + assert "The package_type will have precedence over the options" not in c.out assert "package_type: shared-library" in c.out c.run("graph info . --filter package_type -o shared=True -o header_only=False") assert "package_type: shared-library" in c.out @@ -59,5 +60,7 @@ class Pkg(ConanFile): """)}) tc.run("graph info . --filter package_type -o &:header_only=False") assert "package_type: static-library" in tc.out + assert "The package_type will have precedence over the options" in tc.out tc.run("graph info . --filter package_type -o &:header_only=True") assert "package_type: static-library" in tc.out + assert "The package_type will have precedence over the options" in tc.out