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

CMakeToolchain: implement vs debugger path #15830

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,54 @@ def context(self):
return {"vs_runtimes": config_dict}


class VSDebuggerEnvironment(Block):
template = textwrap.dedent("""
{% if vs_debugger_path %}
# Definition of CMAKE_VS_DEBUGGER_ENVIRONMENT
set(CMAKE_VS_DEBUGGER_ENVIRONMENT "{{ vs_debugger_path }}")
{% endif %}
""")

def context(self):
os_ = self._conanfile.settings.get_safe("os")
build_type = self._conanfile.settings.get_safe("build_type")

if (os_ and "Windows" not in os_) or not build_type:
return None

if "Visual" not in self._toolchain.generator:
return None
franramirez688 marked this conversation as resolved.
Show resolved Hide resolved

config_dict = {}
if os.path.exists(CONAN_TOOLCHAIN_FILENAME):
existing_include = load(CONAN_TOOLCHAIN_FILENAME)
pattern = r"set\(CMAKE_VS_DEBUGGER_ENVIRONMENT \"PATH=([^)]*);%PATH%\"\)"
vs_debugger_environment = re.search(pattern, existing_include)
if vs_debugger_environment:
capture = vs_debugger_environment.group(1)
matches = re.findall(r"\$<\$<CONFIG:([A-Za-z]*)>:([^>]*)>", capture)
config_dict = dict(matches)

host_deps = self._conanfile.dependencies.host.values()
test_deps = self._conanfile.dependencies.test.values()
bin_dirs = [p for dep in host_deps for p in dep.cpp_info.aggregated_components().bindirs]
test_bindirs = [p for dep in test_deps for p in dep.cpp_info.aggregated_components().bindirs]
bin_dirs.extend(test_bindirs)
bin_dirs = [p.replace("\\", "/") for p in bin_dirs]

bin_dirs = ";".join(bin_dirs)
config_dict[build_type] = bin_dirs
if all(value is '' for value in config_dict.values()):
return None
jcar87 marked this conversation as resolved.
Show resolved Hide resolved

vs_debugger_path = ""
for config, value in config_dict.items():
vs_debugger_path += f"$<$<CONFIG:{config}>:{value}>"
vs_debugger_path = f"PATH={vs_debugger_path};%PATH%"

return {"vs_debugger_path": vs_debugger_path}


class FPicBlock(Block):
template = textwrap.dedent("""
{% if fpic %}
Expand Down
4 changes: 3 additions & 1 deletion conan/tools/cmake/toolchain/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from conan.tools.cmake.toolchain.blocks import ToolchainBlocks, UserToolchain, GenericSystemBlock, \
AndroidSystemBlock, AppleSystemBlock, FPicBlock, ArchitectureBlock, GLibCXXBlock, VSRuntimeBlock, \
CppStdBlock, ParallelBlock, CMakeFlagsInitBlock, TryCompileBlock, FindFiles, PkgConfigBlock, \
SkipRPath, SharedLibBock, OutputDirsBlock, ExtraFlagsBlock, CompilersBlock, LinkerScriptsBlock
SkipRPath, SharedLibBock, OutputDirsBlock, ExtraFlagsBlock, CompilersBlock, LinkerScriptsBlock, \
VSDebuggerEnvironment
from conan.tools.cmake.utils import is_multi_configuration
from conan.tools.env import VirtualBuildEnv, VirtualRunEnv
from conan.tools.intel import IntelCC
Expand Down Expand Up @@ -160,6 +161,7 @@ def __init__(self, conanfile, generator=None):
("linker_scripts", LinkerScriptsBlock),
("libcxx", GLibCXXBlock),
("vs_runtime", VSRuntimeBlock),
("vs_debugger_environment", VSDebuggerEnvironment),
("cppstd", CppStdBlock),
("parallel", ParallelBlock),
("extra_flags", ExtraFlagsBlock),
Expand Down
41 changes: 41 additions & 0 deletions conans/test/functional/toolchains/cmake/test_cmake_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,47 @@ def test_cmake_toolchain_without_build_type():
assert "CMAKE_BUILD_TYPE" not in toolchain


@pytest.mark.skipif(platform.system() != "Windows", reason="Only on Windows with msvc")
@pytest.mark.tool("cmake")
def test_cmake_toolchain_cmake_vs_debugger_environment():
client = TestClient()
client.save({"conanfile.py": GenConanfile("pkg", "1.0").with_package_type("shared-library")
.with_settings("build_type")})
client.run("create . -s build_type=Release")
client.run("create . -s build_type=Debug")
client.run("create . -s build_type=MinSizeRel")

client.run("install --require=pkg/1.0 -s build_type=Debug -g CMakeToolchain --format=json")
debug_graph = json.loads(client.stdout)
debug_bindir = debug_graph['graph']['nodes']['1']['cpp_info']['root']['bindirs'][0]
debug_bindir = debug_bindir.replace('\\', '/')

toolchain = client.load("conan_toolchain.cmake")
debugger_environment = f"PATH=$<$<CONFIG:Debug>:{debug_bindir}>;%PATH%"
assert debugger_environment in toolchain

client.run("install --require=pkg/1.0 -s build_type=Release -g CMakeToolchain --format=json")
release_graph = json.loads(client.stdout)
release_bindir = release_graph['graph']['nodes']['1']['cpp_info']['root']['bindirs'][0]
release_bindir = release_bindir.replace('\\', '/')

toolchain = client.load("conan_toolchain.cmake")
debugger_environment = f"PATH=$<$<CONFIG:Debug>:{debug_bindir}>" \
f"$<$<CONFIG:Release>:{release_bindir}>;%PATH%"
assert debugger_environment in toolchain

client.run("install --require=pkg/1.0 -s build_type=MinSizeRel -g CMakeToolchain --format=json")
minsizerel_graph = json.loads(client.stdout)
minsizerel_bindir = minsizerel_graph['graph']['nodes']['1']['cpp_info']['root']['bindirs'][0]
minsizerel_bindir = minsizerel_bindir.replace('\\', '/')

toolchain = client.load("conan_toolchain.cmake")
debugger_environment = f"PATH=$<$<CONFIG:Debug>:{debug_bindir}>" \
f"$<$<CONFIG:Release>:{release_bindir}>" \
f"$<$<CONFIG:MinSizeRel>:{minsizerel_bindir}>;%PATH%"
assert debugger_environment in toolchain
jcar87 marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.tool("cmake")
def test_cmake_toolchain_multiple_user_toolchain():
""" A consumer consuming two packages that declare:
Expand Down