Skip to content

Commit 2414a6c

Browse files
committed
feat: set tool path
1 parent 9baed3a commit 2414a6c

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

conan_provider.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,30 @@ function(conan_install)
210210
# success
211211
set_property(GLOBAL PROPERTY CONAN_INSTALL_SUCCESS TRUE)
212212
endif()
213+
214+
# set tool path
215+
set(CONAN_TOOL_PATH)
216+
string(JSON NUMBER_OF_NODES LENGTH ${conan_stdout} graph nodes)
217+
math(EXPR NUMBER_OF_NODES "${NUMBER_OF_NODES}-1")
218+
foreach(INDEX RANGE ${NUMBER_OF_NODES})
219+
string(JSON CONTEXT GET ${conan_stdout} graph nodes ${INDEX} context)
220+
if(NOT CONTEXT STREQUAL "build")
221+
continue()
222+
endif()
223+
224+
string(JSON BINDIRS GET ${conan_stdout} graph nodes ${INDEX} cpp_info root bindirs)
225+
string(JSON BINDIRS_NODES LENGTH ${BINDIRS})
226+
if(BINDIRS_NODES EQUAL 0)
227+
continue()
228+
endif()
229+
math(EXPR BINDIRS_NODES "${BINDIRS_NODES}-1")
230+
foreach(BINDIRS_INDEX RANGE ${BINDIRS_NODES})
231+
string(JSON BINDIR GET ${BINDIRS} ${BINDIRS_INDEX})
232+
list(APPEND CONAN_TOOL_PATH "${BINDIR}")
233+
endforeach()
234+
endforeach()
235+
set(CONAN_TOOL_PATH ${CONAN_TOOL_PATH} CACHE STRING "Path to build tools")
236+
message(STATUS "CMake-Conan: CONAN_TOOL_PATH=${CONAN_TOOL_PATH}")
213237
endfunction()
214238

215239

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(MyToolApp CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
find_package(hello REQUIRED)
6+
find_program(
7+
NINJA_EXECUTABLE ninja
8+
PATHS ${CONAN_TOOL_PATH} REQUIRED
9+
NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)

tests/resources/tool/conanfile.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[requires]
2+
hello/0.1
3+
4+
[tool_requires]
5+
ninja/1.11.1

tests/test_smoke.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import platform
34
import shutil
45
import subprocess
@@ -134,6 +135,26 @@ def test_reconfigure_on_conanfile_changes(self, capfd, chdir_build):
134135
assert all(expected in out for expected in expected_conan_install_outputs)
135136

136137

138+
class TestTool:
139+
@pytest.fixture(scope="class", autouse=True)
140+
def tool_setup(self):
141+
"Layout for tool test"
142+
src_dir = Path(__file__).parent.parent
143+
shutil.copytree(src_dir / 'tests' / 'resources' / 'tool', ".", dirs_exist_ok=True)
144+
yield
145+
146+
@unix
147+
def test_tool_path(self, capfd, chdir_build):
148+
"Tools are available in CMake"
149+
generator = "-GNinja" if platform.system() == "Windows" else ""
150+
151+
run(f"cmake .. -DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=conan_provider.cmake -DCMAKE_BUILD_TYPE=Release {generator}")
152+
out, _ = capfd.readouterr()
153+
regex = re.compile(r".*--CMake-Conan:CONAN_TOOL_PATH=\/.*\/bin.*")
154+
out_without_whitespace = "".join(out.split())
155+
assert regex.match(out_without_whitespace)
156+
157+
137158
class TestSubdir:
138159
@pytest.fixture(scope="class", autouse=True)
139160
def subdir_setup(self):

0 commit comments

Comments
 (0)