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

basic_layout and external_sources for cmake_layout, removed meson_layout #10659

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions conan/tools/cmake/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from conans.errors import ConanException


def cmake_layout(conanfile, generator=None):
def cmake_layout(conanfile, generator=None, src_folder="."):
gen = conanfile.conf.get("tools.cmake.cmaketoolchain:generator", default=generator)
if gen:
multi = "Visual" in gen or "Xcode" in gen or "Multi-Config" in gen
Expand All @@ -14,7 +14,7 @@ def cmake_layout(conanfile, generator=None):
else:
multi = False

conanfile.folders.source = "."
conanfile.folders.source = src_folder
try:
build_type = str(conanfile.settings.build_type)
except ConanException:
Expand All @@ -27,7 +27,12 @@ def cmake_layout(conanfile, generator=None):
conanfile.folders.build = "cmake-build-{}".format(build_type)
conanfile.folders.generators = os.path.join(conanfile.folders.build, "conan")

conanfile.cpp.source.includedirs = ["src"]
if src_folder == ".":
# The CMakeLists.txt is in the root, the includes (and sources) typically don't
lasote marked this conversation as resolved.
Show resolved Hide resolved
conanfile.cpp.source.includedirs = ["src"]
else:
conanfile.cpp.source.includedirs = ["."]

if multi:
conanfile.cpp.build.libdirs = ["{}".format(build_type)]
conanfile.cpp.build.bindirs = ["{}".format(build_type)]
Expand Down
11 changes: 11 additions & 0 deletions conan/tools/layout/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import os
# FIXME: Temporary fixes to avoid breaking 1.45, to be removed 2.0
from conan.tools.cmake import cmake_layout, clion_layout
from conan.tools.microsoft import vs_layout


def basic_layout(conanfile, src_folder="."):
conanfile.folders.build = "build"
if conanfile.settings.get_safe("build_type"):
conanfile.folders.build += "-{}".format(str(conanfile.settings.build_type).lower())
conanfile.folders.generators = os.path.join(conanfile.folders.build, "conan")
conanfile.cpp.build.bindirs = ["."]
conanfile.cpp.build.libdirs = ["."]
conanfile.folders.source = src_folder
2 changes: 1 addition & 1 deletion conan/tools/meson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from conan.tools.meson.toolchain import MesonToolchain
from conan.tools.meson.meson import Meson
from conan.tools.meson.layout import meson_layout

8 changes: 0 additions & 8 deletions conan/tools/meson/layout.py

This file was deleted.

10 changes: 6 additions & 4 deletions conans/assets/templates/new_v2_meson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

conanfile_sources_v2 = """import os
from conan import ConanFile
from conan.tools.meson import MesonToolchain, Meson, meson_layout
from conan.tools.meson import MesonToolchain, Meson
from conan.tools.layout import basic_layout
from conan.tools.files import copy

class {package_name}Conan(ConanFile):
Expand All @@ -22,7 +23,7 @@ def config_options(self):
del self.options.fPIC

def layout(self):
meson_layout(self)
basic_layout(self)

def generate(self):
tc = MesonToolchain(self)
Expand Down Expand Up @@ -50,7 +51,8 @@ def package_info(self):
test_conanfile_v2 = """import os
from conan import ConanFile
from conans import tools
from conan.tools.meson import MesonToolchain, Meson, meson_layout
from conan.tools.meson import MesonToolchain, Meson
from conan.tools.layout import basic_layout


class {package_name}TestConan(ConanFile):
Expand All @@ -66,7 +68,7 @@ def build(self):
meson.build()

def layout(self):
meson_layout(self)
basic_layout(self)

def test(self):
if not tools.cross_building(self):
Expand Down
149 changes: 148 additions & 1 deletion conans/test/functional/layout/test_build_system_layout_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import textwrap

import pytest
import six

from conans.model.ref import ConanFileReference
from conans.test.assets.genconanfile import GenConanfile
from conans.test.utils.tools import TestClient, TurboTestClient
from conans.util.files import save
from conans.util.files import save, load


@pytest.fixture
Expand Down Expand Up @@ -146,3 +147,149 @@ def layout(self):
client.save({"conanfile.py": conanfile})
client.run('install .', assert_error=True)
assert " 'build_type' setting not defined, it is necessary for cmake_layout()" in client.out


@pytest.mark.skipif(six.PY2, reason="only Py3")
def test_cmake_layout_external_sources():
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.cmake import cmake_layout
from conan.tools.files import save, copy, load
class Pkg(ConanFile):
settings = "os", "build_type"
exports_sources = "exported.txt"

def layout(self):
cmake_layout(self, src_folder="src")

def generate(self):
save(self, "generate.txt", "generate")

def source(self):
save(self, "source.txt", "foo")

def build(self):
c1 = load(self, os.path.join(self.source_folder, "source.txt"))
c2 = load(self, os.path.join(self.source_folder, "..", "exported.txt"))
save(self, "build.txt", c1 + c2)

def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder, "res"))
""")

client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out

# Local flow
client.run("install . foo/1.0 -s os=Linux")
assert os.path.exists(os.path.join(client.current_folder, "cmake-build-release", "conan", "generate.txt"))
client.run("source .")
assert os.path.exists(os.path.join(client.current_folder, "src", "source.txt"))
client.run("build .")
contents = load(os.path.join(client.current_folder, "cmake-build-release", "build.txt"))
assert contents == "fooexported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out


@pytest.mark.skipif(six.PY2, reason="only Py3")
@pytest.mark.parametrize("with_build_type", [True, False])
def test_basic_layout_external_sources(with_build_type):
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.layout import basic_layout
from conan.tools.files import save, load, copy
class Pkg(ConanFile):
settings = "os", "compiler", "arch"{}
exports_sources = "exported.txt"

def layout(self):
basic_layout(self, src_folder="src")

def generate(self):
save(self, "generate.txt", "generate")

def source(self):
save(self, "source.txt", "foo")

def build(self):
c1 = load(self, os.path.join(self.source_folder, "source.txt"))
c2 = load(self, os.path.join(self.source_folder, "..", "exported.txt"))
save(self, "build.txt", c1 + c2)

def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder, "res"))
""")
if with_build_type:
conanfile = conanfile.format(', "build_type"')
else:
conanfile = conanfile.format("")
client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out

# Local flow
build_folder = "build-release" if with_build_type else "build"
client.run("install . foo/1.0 -s os=Linux")
assert os.path.exists(os.path.join(client.current_folder, build_folder, "conan", "generate.txt"))
client.run("source .")
assert os.path.exists(os.path.join(client.current_folder, "src", "source.txt"))
client.run("build .")
contents = load(os.path.join(client.current_folder, build_folder, "build.txt"))
assert contents == "fooexported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out


@pytest.mark.skipif(six.PY2, reason="only Py3")
@pytest.mark.parametrize("with_build_type", [True, False])
def test_basic_layout_no_external_sources(with_build_type):
conanfile = textwrap.dedent("""
import os
from conans import ConanFile
from conan.tools.layout import basic_layout
from conan.tools.files import save, load, copy
class Pkg(ConanFile):
settings = "os", "compiler", "arch"{}
exports_sources = "exported.txt"

def layout(self):
basic_layout(self)

def generate(self):
save(self, "generate.txt", "generate")

def build(self):
contents = load(self, os.path.join(self.source_folder, "exported.txt"))
save(self, "build.txt", contents)

def package(self):
copy(self, "build.txt", self.build_folder, os.path.join(self.package_folder,
"res"))
""")
if with_build_type:
conanfile = conanfile.format(', "build_type"')
else:
conanfile = conanfile.format("")

client = TestClient()
client.save({"conanfile.py": conanfile, "exported.txt": "exported_contents"})
client.run("create . foo/1.0@ -s os=Linux")
assert "Packaged 1 '.txt' file: build.txt" in client.out

# Local flow
client.run("install . foo/1.0 -s os=Linux")

build_folder = "build-release" if with_build_type else "build"
assert os.path.exists(os.path.join(client.current_folder, build_folder, "conan", "generate.txt"))

client.run("build .")
contents = load(os.path.join(client.current_folder, build_folder, "build.txt"))
assert contents == "exported_contents"
client.run("export-pkg . foo/1.0@ --force")
assert "Packaged 1 '.txt' file: build.txt" in client.out