Skip to content

Commit

Permalink
More friendly cmake_layout (#11391)
Browse files Browse the repository at this point in the history
* WIP

* Fixing tests

* Two more

* Win fixes

* Linux fixes

* review
  • Loading branch information
lasote committed Jun 6, 2022
1 parent 43a7ca7 commit 005fc53
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 96 deletions.
18 changes: 9 additions & 9 deletions conan/tools/cmake/layout.py
Expand Up @@ -20,17 +20,17 @@ def cmake_layout(conanfile, generator=None, src_folder="."):
except ConanException:
raise ConanException("'build_type' setting not defined, it is necessary for cmake_layout()")

suffix = get_build_folder_vars_suffix(conanfile)
build_folder = "build"
custom_conf = get_build_folder_custom_vars(conanfile)
if custom_conf:
build_folder = "{}/{}".format(build_folder, custom_conf)

if multi:
conanfile.folders.build = "build"
conanfile.folders.build = build_folder
else:
conanfile.folders.build = "cmake-build-{}".format(str(build_type).lower())

if suffix:
conanfile.folders.build += "-{}".format(suffix)
conanfile.folders.build = "{}/{}".format(build_folder, build_type)

conanfile.folders.generators = os.path.join("build" if not suffix else "build-{}".format(suffix),
"generators")
conanfile.folders.generators = "{}/{}".format(build_folder, "generators")

conanfile.cpp.source.includedirs = ["include"]

Expand All @@ -42,7 +42,7 @@ def cmake_layout(conanfile, generator=None, src_folder="."):
conanfile.cpp.build.bindirs = ["."]


def get_build_folder_vars_suffix(conanfile):
def get_build_folder_custom_vars(conanfile):

build_vars = conanfile.conf.get("tools.cmake.cmake_layout:build_folder_vars",
default=[], check_type=list)
Expand Down
27 changes: 16 additions & 11 deletions conan/tools/cmake/presets.py
Expand Up @@ -2,7 +2,7 @@
import os
import platform

from conan.tools.cmake.layout import get_build_folder_vars_suffix
from conan.tools.cmake.layout import get_build_folder_custom_vars
from conan.tools.cmake.utils import is_multi_configuration
from conans.errors import ConanException
from conans.util.files import save, load
Expand All @@ -20,22 +20,27 @@ def _add_build_preset(conanfile, multiconfig):

def _build_preset_name(conanfile):
build_type = conanfile.settings.get_safe("build_type")
suffix = get_build_folder_vars_suffix(conanfile)
if suffix:
custom_conf = get_build_folder_custom_vars(conanfile)
if custom_conf:
if build_type:
return "{}-{}".format(build_type, suffix)
return "{}-{}".format(custom_conf, build_type.lower())
else:
return suffix
return build_type or "default"
return custom_conf
return build_type.lower() if build_type else "default"


def _configure_preset_name(conanfile, multiconfig):
build_type = conanfile.settings.get_safe("build_type")
suffix = get_build_folder_vars_suffix(conanfile)
base = "default" if multiconfig or not build_type else build_type
if suffix:
return "{}-{}".format(base, suffix)
return base
custom_conf = get_build_folder_custom_vars(conanfile)

if multiconfig or not build_type:
return "default" if not custom_conf else custom_conf

if custom_conf:
return "{}-{}".format(custom_conf, str(build_type).lower())
else:
return str(build_type).lower()



def _add_configure_preset(conanfile, generator, cache_variables, toolchain_file, multiconfig):
Expand Down
Expand Up @@ -172,7 +172,7 @@ def package(self):
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"))
contents = load(os.path.join(client.current_folder, "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
Expand Down
6 changes: 2 additions & 4 deletions conans/test/functional/layout/test_editable_cmake.py
Expand Up @@ -11,8 +11,6 @@


def editable_cmake(generator, build_folder=None):
multi = (generator is None and platform.system() == "Windows") or \
generator in ("Ninja Multi-Config", "Xcode")
c = TestClient()
if generator is not None:
c.save({"global.conf": "tools.cmake.cmaketoolchain:generator={}".format(generator)},
Expand All @@ -36,12 +34,12 @@ def build_dep():

def build_pkg(msg):
c.run("build . -if=install_release")
folder = os.path.join("build", "Release") if multi else "cmake-build-release"
folder = os.path.join("build", "Release")
c.run_command(os.sep.join([".", folder, "pkg"]))
assert "main: Release!" in c.out
assert "{}: Release!".format(msg) in c.out
c.run("build . -if=install_debug")
folder = os.path.join("build", "Debug") if multi else "cmake-build-debug"
folder = os.path.join("build", "Debug")
c.run_command(os.sep.join([".", folder, "pkg"]))
assert "main: Debug!" in c.out
assert "{}: Debug!".format(msg) in c.out
Expand Down

0 comments on commit 005fc53

Please sign in to comment.