Skip to content

Commit

Permalink
cmake_layout add bindirs (#9276)
Browse files Browse the repository at this point in the history
* cmake_layout add bindirs

* remove print
  • Loading branch information
memsharded committed Jul 18, 2021
1 parent b61f761 commit 6976e5a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
2 changes: 2 additions & 0 deletions conan/tools/layout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ def cmake_layout(conanfile, generator=None):
conanfile.cpp.source.includedirs = ["."]
if multi:
conanfile.cpp.build.libdirs = ["{}".format(conanfile.settings.build_type)]
conanfile.cpp.build.bindirs = ["{}".format(conanfile.settings.build_type)]
else:
conanfile.cpp.build.libdirs = ["."]
conanfile.cpp.build.bindirs = ["."]


def clion_layout(conanfile):
Expand Down
24 changes: 16 additions & 8 deletions conans/test/assets/pkg_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from conans.test.assets.sources import gen_function_h, gen_function_cpp


def pkg_cmake(name, version, requires=None):
def pkg_cmake(name, version, requires=None, exe=False):
refs = [ConanFileReference.loads(r) for r in requires or []]
pkg_name = name
name = name.replace(".", "_")
Expand Down Expand Up @@ -40,6 +40,8 @@ def package(self):
self.copy("*.dylib*", dst="lib", keep_path=False)
self.copy("*.so", dst="lib", keep_path=False)
self.copy("*.a", dst="lib", keep_path=False)
self.copy("*app.exe", dst="bin", keep_path=False)
self.copy("*app", dst="bin", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["{name}"]
Expand All @@ -50,13 +52,20 @@ def package_info(self):
hdr = gen_function_h(name=name)
deps = [r.name.replace(".", "_") for r in refs]
src = gen_function_cpp(name=name, includes=deps, calls=deps)
deps = [r.name for r in refs]
cmake = gen_cmakelists(libname=name, libsources=["{}.cpp".format(name)], find_package=deps)

return {"src/{}.h".format(name): hdr,
"src/{}.cpp".format(name): src,
"src/CMakeLists.txt": cmake,
"conanfile.py": conanfile}
deps = [r.name for r in refs]
files = {"src/{}.h".format(name): hdr,
"src/{}.cpp".format(name): src,
"conanfile.py": conanfile}
if exe:
src_app = gen_function_cpp(name="main", includes=[name], calls=[name])
files["src/{}_app.cpp".format(name)] = src_app
cmake = gen_cmakelists(appname="{}_app".format(name), appsources=["{}_app.cpp".format(name)],
libname=name, libsources=["{}.cpp".format(name)], find_package=deps)
else:
cmake = gen_cmakelists(libname=name, libsources=["{}.cpp".format(name)], find_package=deps)
files["src/CMakeLists.txt"] = cmake
return files


def pkg_cmake_app(name, version, requires=None):
Expand Down Expand Up @@ -88,7 +97,6 @@ def build(self):
def package(self):
self.copy("*/app.exe", dst="bin", keep_path=False)
self.copy("*app", dst="bin", keep_path=False)
""")
deps = "requires = " + ", ".join('"{}"'.format(r) for r in requires) if requires else ""
conanfile = conanfile.format(pkg_name=pkg_name, name=name, version=version, deps=deps)
Expand Down
79 changes: 73 additions & 6 deletions conans/test/functional/layout/test_editable_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import pytest

from conan.tools.env.environment import environment_wrap_command
from conans.test.assets.pkg_cmake import pkg_cmake, pkg_cmake_app
from conans.test.assets.sources import gen_function_cpp
from conans.test.utils.mocks import ConanFileMock
from conans.test.utils.tools import TestClient


Expand All @@ -19,13 +21,16 @@ def editable_cmake(generator):
c.save(pkg_cmake_app("pkg", "0.1", requires=["dep/0.1"]),
path=os.path.join(c.current_folder, "pkg"))

with c.chdir("dep"):
c.run("editable add . dep/0.1@")
def build_dep():
c.run("install .")
c.run("build .")
c.run("install . -s build_type=Debug")
c.run("build .")

with c.chdir("dep"):
c.run("editable add . dep/0.1@")
build_dep()

def build_pkg(msg):
c.run("build . -if=install_release")
folder = os.path.join("build", "Release") if multi else "cmake-build-release"
Expand All @@ -46,10 +51,7 @@ def build_pkg(msg):
# Do a source change in the editable!
with c.chdir("dep"):
c.save({"src/dep.cpp": gen_function_cpp(name="dep", msg="SUPERDEP")})
c.run("install .")
c.run("build .")
c.run("install . -s build_type=Debug")
c.run("build .")
build_dep()

with c.chdir("pkg"):
build_pkg("SUPERDEP")
Expand Down Expand Up @@ -80,3 +82,68 @@ def test_editable_cmake_linux(generator):
@pytest.mark.tool_cmake(version="3.19")
def test_editable_cmake_osx(generator):
editable_cmake(generator)


def editable_cmake_exe(generator):
# This test works because it is not multi-config or single config, but explicit in
# --install folder
c = TestClient()
if generator is not None:
c.save({"global.conf": "tools.cmake.cmaketoolchain:generator={}".format(generator)},
path=os.path.join(c.cache.cache_folder))
c.save(pkg_cmake("dep", "0.1", exe=True), path=os.path.join(c.current_folder, "dep"))

def build_dep():
c.run("install . -o dep:shared=True")
c.run("build .")
c.run("install . -s build_type=Debug -o dep:shared=True")
c.run("build .")

with c.chdir("dep"):
c.run("editable add . dep/0.1@")
build_dep()

def run_pkg(msg):
# FIXME: This only works with ``--install-folder``, layout() will break this
cmd_release = environment_wrap_command(ConanFileMock(), "install_release/conanrunenv",
"dep_app", cwd=c.current_folder)
c.run_command(cmd_release)
assert "{}: Release!".format(msg) in c.out
cmd_release = environment_wrap_command(ConanFileMock(), "install_debug/conanrunenv",
"dep_app", cwd=c.current_folder)
c.run_command(cmd_release)
assert "{}: Debug!".format(msg) in c.out

with c.chdir("pkg"):
c.run("install dep/0.1@ -o dep:shared=True -if=install_release -g VirtualRunEnv")
c.run("install dep/0.1@ -o dep:shared=True -if=install_debug -s build_type=Debug "
"-g VirtualRunEnv")
run_pkg("dep")

# Do a source change in the editable!
with c.chdir("dep"):
c.save({"src/dep.cpp": gen_function_cpp(name="dep", msg="SUPERDEP")})
build_dep()

with c.chdir("pkg"):
run_pkg("SUPERDEP")


@pytest.mark.skipif(platform.system() != "Windows", reason="Only windows")
@pytest.mark.parametrize("generator", [None, "MinGW Makefiles"])
@pytest.mark.tool_mingw64
def test_editable_cmake_windows_exe(generator):
editable_cmake_exe(generator)


@pytest.mark.skipif(platform.system() != "Linux", reason="Only linux")
@pytest.mark.parametrize("generator", [None, "Ninja", "Ninja Multi-Config"])
def test_editable_cmake_linux_exe(generator):
editable_cmake_exe(generator)


@pytest.mark.skipif(platform.system() != "Darwin", reason="Requires Macos")
@pytest.mark.parametrize("generator", [None, "Ninja", "Xcode"])
@pytest.mark.tool_cmake(version="3.19")
def test_editable_cmake_osx_exe(generator):
editable_cmake_exe(generator)

0 comments on commit 6976e5a

Please sign in to comment.