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

[feature] Manage shared, fPIC and header_only options automatically #14194

Merged
merged 23 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conan/tools/options/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from conan.tools.options.helpers import (
handle_common_config_options,
handle_common_configure_options,
handle_common_package_id_options
)
17 changes: 17 additions & 0 deletions conan/tools/options/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

def handle_common_config_options(conanfile):
if conanfile.settings.get_safe("os") == "Windows":
conanfile.options.rm_safe("fPIC")


def handle_common_configure_options(conanfile):
if conanfile.options.get_safe("header_only"):
conanfile.options.rm_safe("fPIC")
conanfile.options.rm_safe("shared")
elif conanfile.options.get_safe("shared"):
conanfile.options.rm_safe("fPIC")


def handle_common_package_id_options(conanfile):
if conanfile.options.get_safe("header_only"):
conanfile.info.clear()
4 changes: 4 additions & 0 deletions conans/client/conanfile/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def run_configure_method(conanfile, down_options, profile_options, ref):
if hasattr(conanfile, "config_options"):
with conanfile_exception_formatter(conanfile, "config_options"):
conanfile.config_options()
else:
conanfile._conan_config_options_default()

# Assign only the current package options values, but none of the dependencies
is_consumer = conanfile._conan_is_consumer
Expand All @@ -17,6 +19,8 @@ def run_configure_method(conanfile, down_options, profile_options, ref):
if hasattr(conanfile, "configure"):
with conanfile_exception_formatter(conanfile, "configure"):
conanfile.configure()
else:
conanfile._conan_configure_default()

self_options, up_options = conanfile.options.get_upstream_options(down_options, ref,
is_consumer)
Expand Down
2 changes: 2 additions & 0 deletions conans/client/graph/compute_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ def run_validate_package_id(conanfile):
with conanfile_exception_formatter(conanfile, "package_id"):
with conanfile_remove_attr(conanfile, ['cpp_info', 'settings', 'options'], "package_id"):
conanfile.package_id()
else:
conanfile._conan_package_id_default()

conanfile.info.validate()
12 changes: 12 additions & 0 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,15 @@ def set_deploy_folder(self, deploy_folder):
self.buildenv_info.deploy_base_folder(self.package_folder, deploy_folder)
self.runenv_info.deploy_base_folder(self.package_folder, deploy_folder)
self.folders.set_base_package(deploy_folder)

def _conan_config_options_default(self):
from conan.tools.options import handle_common_config_options
handle_common_config_options(self)

def _conan_configure_default(self):
from conan.tools.options import handle_common_configure_options
handle_common_configure_options(self)

def _conan_package_id_default(self):
from conan.tools.options import handle_common_package_id_options
handle_common_package_id_options(self)
2 changes: 2 additions & 0 deletions conans/model/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def rm_safe(self, field):
delattr(self, field)
except ConanException:
pass
except KeyError:
pass

def validate(self):
for child in self._data.values():
Expand Down
4 changes: 1 addition & 3 deletions conans/test/functional/toolchains/cmake/test_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def test_toolchain_win(self, compiler, build_type, runtime, version, cppstd, arc
options = {"shared": shared}
save(self.client.cache.new_config_path, "tools.build:jobs=1")
install_out = self._run_build(settings, options)
self.assertIn("WARN: Toolchain: Ignoring fPIC option defined for Windows", install_out)

# FIXME: Hardcoded VS version and partial toolset check
toolchain_path = os.path.join(self.client.current_folder, "build",
Expand Down Expand Up @@ -314,7 +313,6 @@ def test_toolchain_mingw_win(self, build_type, libcxx, version, cppstd, arch, sh
}
options = {"shared": shared}
install_out = self._run_build(settings, options)
self.assertIn("WARN: Toolchain: Ignoring fPIC option defined for Windows", install_out)
self.assertIn("The C compiler identification is GNU", self.client.out)
toolchain_path = os.path.join(self.client.current_folder, "build",
"conan_toolchain.cmake").replace("\\", "/")
Expand Down Expand Up @@ -396,7 +394,7 @@ def test_toolchain_linux(self, build_type, cppstd, arch, libcxx, shared):
"CMAKE_SHARED_LINKER_FLAGS": arch_str,
"CMAKE_EXE_LINKER_FLAGS": arch_str,
"COMPILE_DEFINITIONS": defines,
"CMAKE_POSITION_INDEPENDENT_CODE": "ON"
"CMAKE_POSITION_INDEPENDENT_CODE": "ON" if not shared else ""
}

def _verify_out(marker=">>"):
Expand Down
144 changes: 144 additions & 0 deletions conans/test/integration/options/test_configure_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import textwrap
import unittest

from parameterized import parameterized

from conans.test.utils.tools import TestClient


class ConfigureOptionsTest(unittest.TestCase):
"""
Test config_options() and configure() methods can manage shared, fPIC and header_only options
automatically
"""

@parameterized.expand([
["Linux", False, False, False, [False, False, False]],
["Windows", False, False, False, [False, None, False]],
["Windows", True, False, False, [True, None, False]],
["Windows", False, False, True, [None, None, True]],
["Linux", False, False, True, [None, None, True]],
["Linux", True, True, False, [True, None, False]],
["Linux", True, False, False, [True, None, False]],
["Linux", True, True, True, [None, None, True]],
["Linux", True, True, True, [None, None, True]],
["Linux", False, True, False, [False, True, False]],
["Linux", False, True, False, [False, True, False]],
])
def test_methods_not_defined(self, settings_os, shared, fpic, header_only, result):
"""
Test that options are managed automatically when methods config_otpions and configure are not
defined
"""
client = TestClient()
conanfile = textwrap.dedent(f"""\
from conan import ConanFile

class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
options = {{"shared": [True, False], "fPIC": [True, False], "header_only": [True, False]}}
default_options = {{"shared": {shared}, "fPIC": {fpic}, "header_only": {header_only}}}

def build(self):
shared = self.options.get_safe("shared")
fpic = self.options.get_safe("fPIC")
header_only = self.options.get_safe("header_only")
self.output.info(f"shared: {{shared}}, fPIC: {{fpic}}, header only: {{header_only}}")
""")
client.save({"conanfile.py": conanfile})
client.run(f"create . --name=pkg --version=0.1 -s os={settings_os}")
result = f"shared: {result[0]}, fPIC: {result[1]}, header only: {result[2]}"
self.assertIn(result, client.out)

@parameterized.expand([
["Linux", False, False, False, [False, False, False]],
["Linux", False, False, True, [False, False, True]],
["Linux", False, True, False, [False, True, False]],
["Linux", False, True, True, [False, True, True]],
["Linux", True, False, False, [True, False, False]],
["Linux", True, False, True, [True, False, True]],
["Linux", True, True, False, [True, True, False]],
["Linux", True, True, True, [True, True, True]],
["Windows", False, False, False, [False, False, False]],
["Windows", False, False, True, [False, False, True]],
["Windows", False, True, False, [False, True, False]],
["Windows", False, True, True, [False, True, True]],
["Windows", True, False, False, [True, False, False]],
["Windows", True, False, True, [True, False, True]],
["Windows", True, True, False, [True, True, False]],
["Windows", True, True, True, [True, True, True]],
])
def test_optout(self, settings_os, shared, fpic, header_only, result):
"""
Test that options are not managed automatically when methods are defined
"""
client = TestClient()
conanfile = textwrap.dedent(f"""\
from conan import ConanFile

class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
options = {{"shared": [True, False], "fPIC": [True, False], "header_only": [True, False]}}
default_options = {{"shared": {shared}, "fPIC": {fpic}, "header_only": {header_only}}}

def config_options(self):
pass

def configure(self):
pass

def build(self):
shared = self.options.get_safe("shared")
fpic = self.options.get_safe("fPIC")
header_only = self.options.get_safe("header_only")
self.output.info(f"shared: {{shared}}, fPIC: {{fpic}}, header only: {{header_only}}")
""")
client.save({"conanfile.py": conanfile})
client.run(f"create . --name=pkg --version=0.1 -s os={settings_os}")
result = f"shared: {result[0]}, fPIC: {result[1]}, header only: {result[2]}"
self.assertIn(result, client.out)

@parameterized.expand([
["Linux", False, False, False, [False, False, False]],
["Windows", False, False, False, [False, None, False]],
["Windows", True, False, False, [True, None, False]],
["Windows", False, False, True, [None, None, True]],
["Linux", False, False, True, [None, None, True]],
["Linux", True, True, False, [True, None, False]],
["Linux", True, False, False, [True, None, False]],
["Linux", True, True, True, [None, None, True]],
["Linux", True, True, True, [None, None, True]],
["Linux", False, True, False, [False, True, False]],
["Linux", False, True, False, [False, True, False]],
])
def test_methods_defined_explicit(self, settings_os, shared, fpic, header_only, result):
"""
Test that options are managed automatically when methods config_otpions and configure are not
defined
"""
client = TestClient()
conanfile = textwrap.dedent(f"""\
from conan import ConanFile
from conan.tools.options import handle_common_config_options, handle_common_configure_options

class Pkg(ConanFile):
settings = "os", "compiler", "arch", "build_type"
options = {{"shared": [True, False], "fPIC": [True, False], "header_only": [True, False]}}
default_options = {{"shared": {shared}, "fPIC": {fpic}, "header_only": {header_only}}}

def config_options(self):
handle_common_config_options(self)

def configure(self):
handle_common_configure_options(self)

def build(self):
shared = self.options.get_safe("shared")
fpic = self.options.get_safe("fPIC")
header_only = self.options.get_safe("header_only")
self.output.info(f"shared: {{shared}}, fPIC: {{fpic}}, header only: {{header_only}}")
""")
client.save({"conanfile.py": conanfile})
client.run(f"create . --name=pkg --version=0.1 -s os={settings_os}")
result = f"shared: {result[0]}, fPIC: {result[1]}, header only: {result[2]}"
self.assertIn(result, client.out)
Empty file.
60 changes: 60 additions & 0 deletions conans/test/unittests/tools/options/test_options_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from conan.tools.options import (
handle_common_config_options,
handle_common_configure_options, handle_common_package_id_options
)
from conans.test.utils.mocks import MockSettings, ConanFileMock, MockOptions, MockConanfile


def test_handle_common_config_options():
"""
If windows, then fpic option should be removed
"""
conanfile = ConanFileMock()
conanfile.settings = MockSettings({"os": "Linux"})
conanfile.options = MockOptions({"shared": False, "fPIC": False})
handle_common_config_options(conanfile)
assert conanfile.options.values == {"shared": False, "fPIC": False}
conanfile.settings = MockSettings({"os": "Windows"})
handle_common_config_options(conanfile)
assert conanfile.options.values == {"shared": False}


def test_handle_common_configure_options():
"""
If header only, fpic and shared options should be removed.
If shared, fpic removed and header only is false
"""
conanfile = ConanFileMock()
conanfile.settings = MockSettings({"os": "Linux"})
conanfile.options = MockOptions({"header_only": False, "shared": False, "fPIC": False})
handle_common_configure_options(conanfile)
assert conanfile.options.values == {"header_only": False, "shared": False, "fPIC": False}
conanfile.options = MockOptions({"header_only": True, "shared": False, "fPIC": False})
handle_common_configure_options(conanfile)
assert conanfile.options.values == {"header_only": True}
conanfile.options = MockOptions({"header_only": False, "shared": True, "fPIC": False})
handle_common_configure_options(conanfile)
assert conanfile.options.values == {"header_only": False, "shared": True}
# Wrong options combination, but result should be right as well
conanfile.options = MockOptions({"header_only": True, "shared": True, "fPIC": False})
handle_common_configure_options(conanfile)
assert conanfile.options.values == {"header_only": True}


def test_handle_common_package_id_options():
"""
When header_only option is False, the original settings and options is kept in package id info
When the heacer_only option is True, the package id info is cleared
"""
original_settings = {"os": "Linux", "compiler": "gcc"}
options = MockOptions({"header_only": False})
conanfile = MockConanfile(original_settings, options)
handle_common_package_id_options(conanfile)
assert conanfile.info.settings == original_settings
assert conanfile.info.options == options

options = MockOptions({"header_only": True})
conanfile = MockConanfile(original_settings, options)
handle_common_package_id_options(conanfile)
assert conanfile.info.settings == {}
assert conanfile.info.options == {}
11 changes: 10 additions & 1 deletion conans/test/utils/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __getattr__(self, name):
except KeyError:
raise ConanException("'%s' value not defined" % name)

def rm_safe(self, name):
self.values.pop(name)


class MockCppInfo(object):
def __init__(self):
Expand Down Expand Up @@ -90,9 +93,15 @@ def __init__(self, settings, options=None, runner=None):
self.conf = Conf()

class MockConanInfo:
pass
settings = None
options = None

def clear(self):
self.settings = {}
self.options = {}
self.info = MockConanInfo()
self.info.settings = settings # Incomplete, only settings for Cppstd Min/Max tests
self.info.options = options

def run(self, *args, **kwargs):
if self.runner:
Expand Down