-
Notifications
You must be signed in to change notification settings - Fork 988
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
memsharded
merged 23 commits into
conan-io:release/2.0
from
danimtb:feature/manage_options
Jul 11, 2023
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
39bf70b
[feature] Manage shared, fPIC and header_options automatically
danimtb 3b621bb
get os safe
danimtb beab2e6
rename to _conan
danimtb bc5da23
manage exception
danimtb 665412a
fix cmake test
danimtb b4bc82f
fix win tests
danimtb bfab95b
Add package_id method
danimtb bfededf
fix import
danimtb bb2c4d8
stupid fix
danimtb 2a7fc2c
add unit test
danimtb 32bd668
add pid to tests
danimtb 4473856
move out of conanfile
danimtb 03c2425
add package type
danimtb d413cca
add comment
danimtb 39580bc
review
danimtb b1d47c0
remove keyerror to show failing tests
danimtb 9459974
Update conan/tools/options/helpers.py
danimtb 96da317
review
danimtb 6578fe4
Merge branch 'release/2.0' into feature/manage_options
danimtb 7784b66
Update conans/test/utils/mocks.py
danimtb 98d14fb
change behavior
danimtb 79d4031
fix test
danimtb 24602e0
review after discussion
danimtb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from conans.model.pkg_type import PackageType | ||
|
||
|
||
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") or conanfile.package_type == PackageType.HEADER: | ||
danimtb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
conanfile.info.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
conans/test/integration/options/test_configure_options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import textwrap | ||
import unittest | ||
|
||
from parameterized import parameterized | ||
|
||
from conans.test.utils.tools import TestClient | ||
|
||
|
||
class ConfigureOptionsTest(unittest.TestCase): | ||
""" | ||
Test config_options(), configure() and package_id() 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. | ||
Check that header only package gets its unique package ID. | ||
""" | ||
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) | ||
if header_only: | ||
self.assertIn("Package 'da39a3ee5e6b4b0d3255bfef95601890afd80709' created", 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. | ||
Check that header only package gets its unique package ID. | ||
""" | ||
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) | ||
if header_only: | ||
self.assertIn("Package 'da39a3ee5e6b4b0d3255bfef95601890afd80709' created", 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 | ||
danimtb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defined. | ||
Check that header only package gets its unique package ID. | ||
""" | ||
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) | ||
if header_only: | ||
self.assertIn("Package 'da39a3ee5e6b4b0d3255bfef95601890afd80709' created", client.out) | ||
|
||
def test_header_package_type_pid(self): | ||
""" | ||
Test that we get the pid for header only when package type is set to header-library | ||
""" | ||
client = TestClient() | ||
conanfile = textwrap.dedent(f"""\ | ||
from conan import ConanFile | ||
|
||
class Pkg(ConanFile): | ||
settings = "os", "compiler", "arch", "build_type" | ||
package_type = "header-library" | ||
|
||
""") | ||
client.save({"conanfile.py": conanfile}) | ||
client.run(f"create . --name=pkg --version=0.1") | ||
self.assertIn("Package 'da39a3ee5e6b4b0d3255bfef95601890afd80709' created", client.out) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from conan.tools.options import ( | ||
danimtb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
handle_common_config_options, | ||
handle_common_configure_options, handle_common_package_id_options | ||
) | ||
from conans.model.pkg_type import PackageType | ||
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. | ||
When package type is header, then 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 == {} | ||
|
||
conanfile = MockConanfile(original_settings) | ||
conanfile.package_type = PackageType.HEADER | ||
handle_common_package_id_options(conanfile) | ||
assert conanfile.info.settings == {} | ||
assert conanfile.info.options == {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that if by the point this gets executed the package type has already been properly computed, why rely on the option? I don't actually know if having it is better or worse, so happy to hear your opinion :)