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 2 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
47 changes: 47 additions & 0 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,53 @@ def context(self):

return {"vs_runtimes": config_dict}

class VSDebuggerEnvironment(Block):
template = textwrap.dedent("""
# Definition of CMAKE_VS_DEBUGGER_ENVIRONMENT
{% if vs_debugger_path %}
{% set genexpr = namespace(str='') %}
{% for config, value in vs_debugger_path.items() %}
{% set genexpr.str = genexpr.str +
'$<$<CONFIG:' + config + '>:' + value|string + '>' %}
jcar87 marked this conversation as resolved.
Show resolved Hide resolved
{% endfor %}
set(CMAKE_VS_DEBUGGER_ENVIRONMENT "PATH={{ genexpr.str }};%PATH%")
{% endif %}
""")

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

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

generator = self._conanfile.conf.get("tools.cmake.cmaketoolchain:generator", default="Visual Studio")
if not "Visual Studio" in generator:
return None
jcar87 marked this conversation as resolved.
Show resolved Hide resolved

config_dict = {}
if os.path.exists(CONAN_TOOLCHAIN_FILENAME):
existing_include = load(CONAN_TOOLCHAIN_FILENAME)
vs_debugger_environment = re.search(r"set\(CMAKE_VS_DEBUGGER_ENVIRONMENT \"PATH=([^)]*)>;%PATH%\"\)",
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)
Copy link
Member

Choose a reason for hiding this comment

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

This is the part we might want to refactor, but that might be done later in another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the string generation from the jinja, and into python, although still very tricky to get right with the regexes. Fixed some issues when we have more than 2 configs, so it should be more robust.

IN a later PR I have some ideas for how to support multiconfig in a more straightforward way


host_deps = self._conanfile.dependencies.host.values()
test_deps = self._conanfile.dependencies.test.values()
bin_dirs = [p.replace('\\','/') for dep in host_deps for p in dep.cpp_info.aggregated_components().bindirs]
test_bin_dirs = [p.replace('\\','/') for dep in test_deps for p in dep.cpp_info.aggregated_components().bindirs]
bin_dirs.extend(test_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

return {"vs_debugger_path": config_dict}


class FPicBlock(Block):
template = textwrap.dedent("""
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
25 changes: 25 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,31 @@ 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.run("new -d name=mylib -d version=1.0 -f cmake_lib")
client.run("create . -s:h build_type=Release -o:h mylib/*:shared=True -tf=''")
client.run("create . -s:h build_type=Debug -o:h mylib/*:shared=True -tf=''")

client.run("install --require=mylib/1.0 -o:h mylib/*:shared=True -s:h build_type=Debug -g CMakeToolchain" \
" --format=json", redirect_stdout="debug.json")
client.run("install --require=mylib/1.0 -o:h mylib/*:shared=True -s:h build_type=Release -g CMakeToolchain" \
" --format=json", redirect_stdout="release.json")

debug_graph = json.loads(client.load('debug.json'))
debug_bindir = debug_graph['graph']['nodes']['1']['cpp_info']['root']['bindirs'][0].replace('\\', '/')
release_graph = json.loads(client.load('release.json'))
release_bindir = release_graph['graph']['nodes']['1']['cpp_info']['root']['bindirs'][0].replace('\\', '/')

debugger_environment = f"PATH=$<$<CONFIG:Debug>:{debug_bindir}>$<$<CONFIG:Release>:{release_bindir}>;%PATH%"

toolchain = client.load("conan_toolchain.cmake")
assert f'set(CMAKE_VS_DEBUGGER_ENVIRONMENT "{debugger_environment}")' in toolchain


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