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 4 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
4 changes: 4 additions & 0 deletions conan/tools/options/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from conan.tools.options.helpers import (
handle_common_config_options,
handle_common_configure_options
)
12 changes: 12 additions & 0 deletions conan/tools/options/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

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")
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
8 changes: 8 additions & 0 deletions conans/model/conan_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,11 @@ 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)
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
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.
41 changes: 41 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,41 @@
from conan.tools.options import (
handle_common_config_options,
handle_common_configure_options
)
from conans.test.utils.mocks import MockSettings, ConanFileMock, MockOptions


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}
3 changes: 3 additions & 0 deletions 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