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

Add new layout() test for projects sharing common code in parent folders #11556

Merged
merged 8 commits into from Jul 12, 2022
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
6 changes: 3 additions & 3 deletions conan/tools/cmake/layout.py
Expand Up @@ -3,7 +3,7 @@
from conans.errors import ConanException


def cmake_layout(conanfile, generator=None, src_folder="."):
def cmake_layout(conanfile, generator=None, src_folder=".", subfolder=None):
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,13 +14,13 @@ def cmake_layout(conanfile, generator=None, src_folder="."):
else:
multi = False

conanfile.folders.source = src_folder
conanfile.folders.source = src_folder if not subfolder else os.path.join(subfolder, src_folder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sub folder is above source dir? 😕 Is that from the perspective of the root folder?

Maybe "nested" or "project" not sure whats better, maybe some is enough?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea is that subfolder is from the "root" folder, to get you back to the subproject folder where the conanfile.py is. And then, the src_folder defines the source folder within that subproject (typically, where the CMakeLists.txt of that subproject is)

try:
build_type = str(conanfile.settings.build_type)
except ConanException:
raise ConanException("'build_type' setting not defined, it is necessary for cmake_layout()")

build_folder = "build"
build_folder = "build" if not subfolder else os.path.join(subfolder, "build")
custom_conf = get_build_folder_custom_vars(conanfile)
if custom_conf:
build_folder = "{}/{}".format(build_folder, custom_conf)
Expand Down
113 changes: 113 additions & 0 deletions conans/test/functional/layout/test_in_subfolder.py
@@ -1,5 +1,9 @@
import textwrap

import pytest

from conans.test.assets.cmake import gen_cmakelists
from conans.test.assets.sources import gen_function_cpp
from conans.test.utils.tools import TestClient


Expand Down Expand Up @@ -54,3 +58,112 @@ def build(self):
c.run("build conan")
assert "conanfile.py (pkg/0.1): MYCMAKE-BUILD: mycmake!" in c.out
assert c.load("build/mylib.a") == "mylib"


def test_exports_sources_common_code():
""" very similar to the above, but intended for a multi-package project sharing some
common code
"""
c = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.files import load, copy, save
class Pkg(ConanFile):
name = "pkg"
version = "0.1"

def layout(self):
self.folders.root = ".."
self.folders.source = "pkg"
self.folders.build = "build"

def export_sources(self):
source_folder = os.path.join(self.recipe_folder, "..")
copy(self, "*.txt", source_folder, self.export_sources_folder)
copy(self, "*.cmake", source_folder, self.export_sources_folder)

def source(self):
cmake = load(self, "CMakeLists.txt")
self.output.info("MYCMAKE-SRC: {}".format(cmake))

def build(self):
path = os.path.join(self.source_folder, "CMakeLists.txt")
cmake = load(self, path)
self.output.info("MYCMAKE-BUILD: {}".format(cmake))

path = os.path.join(self.export_sources_folder, "common", "myutils.cmake")
cmake = load(self, path)
self.output.info("MYUTILS-BUILD: {}".format(cmake))
""")
c.save({"pkg/conanfile.py": conanfile,
"pkg/CMakeLists.txt": "mycmake!",
"common/myutils.cmake": "myutils!"})
c.run("create pkg")
assert "pkg/0.1: MYCMAKE-SRC: mycmake!" in c.out
assert "pkg/0.1: MYCMAKE-BUILD: mycmake!" in c.out
assert "pkg/0.1: MYUTILS-BUILD: myutils!" in c.out

# Local flow
c.run("install pkg")
# SOURCE NOT CALLED! It doesnt make sense (will fail due to local exports)
c.run("build pkg")
assert "conanfile.py (pkg/0.1): MYCMAKE-BUILD: mycmake!" in c.out
assert "conanfile.py (pkg/0.1): MYUTILS-BUILD: myutils!" in c.out


@pytest.mark.tool_cmake
def test_exports_sources_common_code_layout():
""" Equal to the previous test, but actually building and using cmake_layout
"""
c = TestClient()
conanfile = textwrap.dedent("""
import os
from conan import ConanFile
from conan.tools.cmake import cmake_layout, CMake
from conan.tools.files import load, copy, save
class Pkg(ConanFile):
name = "pkg"
version = "0.1"
settings = "os", "compiler", "build_type", "arch"
generators = "CMakeToolchain"

def layout(self):
self.folders.root = ".."
cmake_layout(self, subfolder="pkg")
memsharded marked this conversation as resolved.
Show resolved Hide resolved

def export_sources(self):
source_folder = os.path.join(self.recipe_folder, "..")
copy(self, "*", source_folder, self.export_sources_folder)

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
self.run(os.path.join(self.cpp.build.bindirs[0], "myapp"))
""")
cmake_include = "include(${CMAKE_CURRENT_LIST_DIR}/../common/myutils.cmake)"
c.save({"pkg/conanfile.py": conanfile,
"pkg/app.cpp": gen_function_cpp(name="main", includes=["../common/myheader"],
preprocessor=["MYDEFINE"]),
"pkg/CMakeLists.txt": gen_cmakelists(appsources=["app.cpp"],
custom_content=cmake_include),
"common/myutils.cmake": 'message(STATUS "MYUTILS.CMAKE!")',
"common/myheader.h": '#define MYDEFINE "MYDEFINEVALUE"'})
c.run("create pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Release!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out

# Local flow
c.run("install pkg")
c.run("build pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Release!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out

c.run("install pkg -s build_type=Debug")
c.run("build pkg")
assert "MYUTILS.CMAKE!" in c.out
assert "main: Debug!" in c.out
assert "MYDEFINE: MYDEFINEVALUE" in c.out
Empty file.