-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Manage shared, fPIC and header_only options automatically (#…
…14194) * [feature] Manage shared, fPIC and header_options automatically * get os safe * rename to _conan * manage exception * fix cmake test * fix win tests * Add package_id method * fix import * stupid fix * add unit test * add pid to tests * move out of conanfile * add package type * add comment * review * remove keyerror to show failing tests * Update conan/tools/options/helpers.py Co-authored-by: James <memsharded@gmail.com> * review * Update conans/test/utils/mocks.py Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es> * change behavior * fix test * review after discussion --------- Co-authored-by: James <memsharded@gmail.com> Co-authored-by: Rubén Rincón Blanco <git@rinconblanco.es>
- Loading branch information
1 parent
936218b
commit 2af05b5
Showing
8 changed files
with
219 additions
and
9 deletions.
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
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 default_config_options(conanfile): | ||
if conanfile.settings.get_safe("os") == "Windows": | ||
conanfile.options.rm_safe("fPIC") | ||
|
||
|
||
def default_configure(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 default_package_id(conanfile): | ||
if conanfile.options.get_safe("header_only") or conanfile.package_type is PackageType.HEADER: | ||
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
169 changes: 169 additions & 0 deletions
169
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,169 @@ | ||
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 when the tool is used in methods defined by user. | ||
Check that header only package gets its unique package ID. | ||
""" | ||
client = TestClient() | ||
conanfile = textwrap.dedent(f"""\ | ||
from conan import ConanFile | ||
from conan.tools import default_config_options, default_configure | ||
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): | ||
default_config_options(self) | ||
def configure(self): | ||
default_configure(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) |
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