Skip to content

Commit

Permalink
[MesonToolchain] Showing a warning message if raw options are used (#…
Browse files Browse the repository at this point in the history
…14692)

* reverted check if raw options. Added those cases in test

* Added name property. Added warning message if consumer is using raw options
  • Loading branch information
franramirez688 authored Sep 7, 2023
1 parent 89b1852 commit b2b7eac
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 4 additions & 2 deletions conan/tools/meson/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from conan.api.output import ConanOutput
from conan.tools.build.flags import cppstd_msvc_flag
from conans.errors import ConanException
from conans.model.options import _PackageOption
Expand Down Expand Up @@ -100,8 +101,9 @@ def to_meson_value(value):
elif isinstance(value, list):
return '[{}]'.format(', '.join([str(to_meson_value(val)) for val in value]))
elif isinstance(value, _PackageOption):
raise ConanException("Conan options cannot be specified directly as a valid Meson data type."
" Please, use another Python data type like int, str, list or boolean.")
ConanOutput().warning(f"Please, do not use a Conan option value directly. "
f"Convert 'options.{value.name}' into a valid Python"
f"data type, e.g, bool(self.options.shared)", warn_tag="deprecated")
return value


Expand Down
4 changes: 4 additions & 0 deletions conans/model/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def __eq__(self, other):
return False # Other is not None here
return other == self.__str__()

@property
def name(self):
return self._name

@property
def value(self):
return self._value
Expand Down
26 changes: 17 additions & 9 deletions conans/test/functional/toolchains/meson/test_meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def test_definition_of_global_options(self):
class App(ConanFile):
settings = "os", "arch", "compiler", "build_type"
options = {{"shared": [True, False], "fPIC": [True, False]}}
default_options = {{"shared": False, "fPIC": True}}
options = {{"shared": [True, False], "fPIC": [True, False], "msg": ["ANY"]}}
default_options = {{"shared": False, "fPIC": True, "msg": "Hi everyone!"}}
def config_options(self):
if self.settings.os == "Windows":
Expand All @@ -44,7 +44,11 @@ def generate(self):
tc.project_options["FALSE_DEFINITION"] = False
tc.project_options["INT_DEFINITION"] = 42
tc.project_options["ARRAY_DEFINITION"] = ["Text1", "Text2"]
tc.project_options["DYNAMIC"] = {dynamic}
# Meson converts True/False into true/false boolean values
tc.project_options["DYNAMIC"] = self.options.shared
# This one will fail because HELLO_MSG = Hi everyone is an invalid value
# The good one should be HELLO_MSG = "Hi everyone"
tc.project_options["HELLO_MSG"] = {msg}
tc.generate()
def build(self):
Expand Down Expand Up @@ -79,25 +83,29 @@ def build(self):

# Issue related: https://github.com/conan-io/conan/issues/14453
# At first, let's add a raw option to see that it fails
self.t.save({"conanfile.py": conanfile_py.format(dynamic="self.options.shared"),
self.t.save({"conanfile.py": conanfile_py.format(msg="self.options.msg"),
"meson.build": meson_build,
"meson_options.txt": meson_options_txt,
"hello.h": hello_h,
"hello.cpp": hello_cpp,
"main.cpp": app})
self.t.run("build .", assert_error=True)
assert "Conan options cannot be specified directly as a valid Meson data type. " \
"Please, use another Python data type like int, str, list or boolean." in self.t.out
# Using directly the options, the result is -> HELLO_MSG = Hi everyone, which is incorrect
assert "WARN: deprecated: Please, do not use a Conan option value directly. " \
"Convert 'options.shared' into" in self.t.out
assert "WARN: deprecated: Please, do not use a Conan option value directly. " \
"Convert 'options.msg' into" in self.t.out
assert "ERROR: Malformed value in machine file variable 'HELLO_MSG'." in self.t.out
# Let's transform the Conan option into other allowed data type to solve the issue
self.t.save({"conanfile.py": conanfile_py.format(dynamic="True if self.options.shared "
"else False")})
self.t.save({"conanfile.py": conanfile_py.format(msg="str(self.options.msg)")})
self.t.run("build .")
content = self.t.load(os.path.join("build", "gen_folder", "conan_meson_native.ini"))
self.assertIn("[project options]", content)
self.assertIn("STRING_DEFINITION = 'Text'", content)
self.assertIn("TRUE_DEFINITION = true", content)
self.assertIn("FALSE_DEFINITION = false", content)
self.assertIn("DYNAMIC = false", content)
self.assertIn("DYNAMIC = False", content) # Meson transforms correctly this value into bool
self.assertIn("HELLO_MSG = 'Hi everyone!'", content)
self.assertIn("INT_DEFINITION = 42", content)
self.assertIn("ARRAY_DEFINITION = ['Text1', 'Text2']", content)
self.assertIn("[built-in options]", content)
Expand Down

0 comments on commit b2b7eac

Please sign in to comment.