Skip to content

Commit

Permalink
[MesonToolchain] Adding default dirs (#11618)
Browse files Browse the repository at this point in the history
* added default dirs to MesonToolchain

* simplified templates

* Added functional test

* wip

* Completed func test

* comment

* Get rename back

* Replaced complete files
  • Loading branch information
franramirez688 committed Jul 14, 2022
1 parent d5f2e29 commit 744827f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 9 deletions.
2 changes: 2 additions & 0 deletions conan/tools/meson/meson.py
Expand Up @@ -54,6 +54,8 @@ def install(self):
self._conanfile.run(cmd)

def test(self):
if self._conanfile.conf.get("tools.build:skip_test"):
return
meson_build_folder = self._conanfile.build_folder
cmd = 'meson test -v -C "{}"'.format(meson_build_folder)
# TODO: Do we need vcvars for test?
Expand Down
42 changes: 42 additions & 0 deletions conan/tools/meson/toolchain.py
Expand Up @@ -126,6 +126,9 @@ def __init__(self, conanfile, backend=None):
self.project_options = {
"wrap_mode": "nofallback" # https://github.com/conan-io/conan/issues/10671
}
# Add all the default dirs
self.project_options.update(self._get_default_dirs())

self.preprocessor_definitions = {}
self.pkg_config_path = self._conanfile.generators_folder

Expand Down Expand Up @@ -188,6 +191,45 @@ def __init__(self, conanfile, backend=None):
self._resolve_apple_flags_and_variables(build_env)
self._resolve_android_cross_compilation()

def _get_default_dirs(self):
"""
Get all the default directories from cpp.package.
Issues related:
- https://github.com/conan-io/conan/issues/9713
- https://github.com/conan-io/conan/issues/11596
"""
def _get_cpp_info_value(name):
elements = getattr(self._conanfile.cpp.package, name)
return elements[0] if elements else None

if not self._conanfile.package_folder:
return {}

ret = {}
bindir = _get_cpp_info_value("bindirs")
datadir = _get_cpp_info_value("resdirs")
libdir = _get_cpp_info_value("libdirs")
includedir = _get_cpp_info_value("includedirs")
if bindir:
ret.update({
'bindir': bindir,
'sbindir': bindir,
'libexecdir': bindir
})
if datadir:
ret.update({
'datadir': datadir,
'localedir': datadir,
'mandir': datadir,
'infodir': datadir
})
if includedir:
ret["includedir"] = includedir
if libdir:
ret["libdir"] = libdir
return ret

def _resolve_apple_flags_and_variables(self, build_env):
if not self._is_apple_system:
return
Expand Down
7 changes: 1 addition & 6 deletions conans/assets/templates/new_v2_meson.py
Expand Up @@ -37,11 +37,6 @@ def build(self):
def package(self):
meson = Meson(self)
meson.install()
# Meson cannot install dll/so in "bin" and .a/.lib in "lib"
lib = os.path.join(self.package_folder, "lib")
bin = os.path.join(self.package_folder, "bin")
copy(self, "*.so", lib, bin)
copy(self, "*.dll", lib, bin)
def package_info(self):
self.cpp_info.libs = ["{name}"]
Expand Down Expand Up @@ -86,7 +81,7 @@ def test(self):

_meson_build = """\
project('{name} ', 'cpp')
library('{name}', 'src/{name}.cpp', install: true, install_dir: 'lib')
library('{name}', 'src/{name}.cpp', install: true)
install_headers('src/{name}.h')
"""

Expand Down
2 changes: 0 additions & 2 deletions conans/test/functional/toolchains/meson/test_install.py
Expand Up @@ -30,8 +30,6 @@ def layout(self):
def generate(self):
tc = MesonToolchain(self)
# https://mesonbuild.com/Release-notes-for-0-50-0.html#libdir-defaults-to-lib-when-cross-compiling
tc.project_options["libdir"] = "lib"
tc.generate()
def build(self):
Expand Down
75 changes: 74 additions & 1 deletion conans/test/functional/toolchains/meson/test_meson.py
@@ -1,6 +1,8 @@
import os
import platform
import textwrap

from conans.model.ref import ConanFileReference
from conans.test.assets.sources import gen_function_cpp, gen_function_h
from conans.test.functional.toolchains.meson._base import TestMesonBase

Expand Down Expand Up @@ -58,7 +60,7 @@ def build(self):
executable('demo', 'main.cpp', link_with: hello)
""")

def test_build(self):
def test_definition_of_global_options(self):
hello_h = gen_function_h(name="hello")
hello_cpp = gen_function_cpp(name="hello", preprocessor=["STRING_DEFINITION"])
app = gen_function_cpp(name="main", includes=["hello"], calls=["hello"])
Expand Down Expand Up @@ -94,3 +96,74 @@ def test_build(self):
self.assertNotIn("needs_exe_wrapper", content)

self._check_binary()

def test_meson_default_dirs(self):
self.t.run("new hello/1.0 -m meson_exe")
# self.t.run("new meson_exe -d name=hello -d version=1.0 -m meson_exe")

meson_build = textwrap.dedent("""
project('tutorial', 'cpp')
# Creating specific library
hello = library('hello', 'src/hello.cpp', install: true)
# Creating specific executable
executable('demo', 'src/main.cpp', link_with: hello, install: true)
# Creating specific data in src/ (they're going to be exported)
install_data(['src/file1.txt', 'src/file2.txt'])
""")
conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.meson import Meson
class HelloConan(ConanFile):
name = "hello"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
exports_sources = "meson.build", "src/*"
generators = "MesonToolchain"
def layout(self):
self.folders.build = "build"
# Only adding "res" to resdirs
self.cpp.package.resdirs = ["res"]
def build(self):
meson = Meson(self)
meson.configure()
meson.build()
def package(self):
meson = Meson(self)
meson.install()
""")
test_conanfile = textwrap.dedent("""
from conan import ConanFile
from conan.tools.build import cross_building
class HelloTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "VirtualRunEnv"
apply_env = False
def test(self):
if not cross_building(self):
self.run("demo", env="conanrun")
""")
# Replace meson.build, conanfile.py and test_package/conanfile.py
self.t.save({"meson.build": meson_build,
"conanfile.py": conanfile,
"test_package/conanfile.py": test_conanfile,
"src/file1.txt": "", "src/file2.txt": ""})
self.t.run("create .")
# Check if all the files are in the final directories
ref = ConanFileReference.loads("hello/1.0")
cache_package_folder = self.t.cache.package_layout(ref).packages()
package_folder = os.path.join(cache_package_folder, os.listdir(cache_package_folder)[0])
if platform.system() == "Windows":
assert os.path.exists(os.path.join(package_folder, "lib", "hello.lib"))
assert os.path.exists(os.path.join(package_folder, "bin", "hello.dll"))
assert os.path.exists(os.path.join(package_folder, "bin", "demo.exe"))
else:
ext = "dylib" if platform.system() == "Darwin" else "so"
assert os.path.exists(os.path.join(package_folder, "bin", "demo"))
assert os.path.exists(os.path.join(package_folder, "lib", "libhello." + ext))
# res/tutorial -> tutorial is being added automatically by Meson
assert os.path.exists(os.path.join(package_folder, "res", "tutorial", "file1.txt"))
assert os.path.exists(os.path.join(package_folder, "res", "tutorial", "file2.txt"))

0 comments on commit 744827f

Please sign in to comment.