@@ -329,29 +329,25 @@ function(resolve_python_executable)
329
329
endfunction ()
330
330
331
331
# find_package(Torch CONFIG REQUIRED) replacement for targets that have a
332
- # header-only Torch dependency. Because find_package sets variables in the
333
- # parent scope, we use a macro to preserve this rather than maintaining our own
334
- # list of those variables.
335
- macro (find_package_torch_headers )
336
- # We cannot simply use CMAKE_FIND_ROOT_PATH_BOTH, because that does not
337
- # propagate into TorchConfig.cmake.
338
- foreach (mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE )
339
- set (OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}
340
- ${CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}
341
- )
342
- set (CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} BOTH )
343
- endforeach ()
344
- find_package_torch ()
345
- foreach (mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE )
346
- set (CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}
347
- ${OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}
348
- )
349
- endforeach ()
350
- endmacro ()
332
+ # header-only Torch dependency.
333
+ #
334
+ # Unlike find_package(Torch ...), this will only set
335
+ # TORCH_INCLUDE_DIRS in the parent scope. In particular, it will NOT
336
+ # set any of the following:
337
+ # - TORCH_FOUND
338
+ # - TORCH_LIBRARY
339
+ # - TORCH_CXX_FLAGS
340
+ function (find_package_torch_headers )
341
+ # We implement this way rather than using find_package so that
342
+ # cross-compilation can still use the host's installed copy of
343
+ # torch, since the headers should be fine.
344
+ get_torch_base_path (TORCH_BASE_PATH )
345
+ set (TORCH_INCLUDE_DIRS "${TORCH_BASE_PATH} /include;${TORCH_BASE_PATH} /include/torch/csrc/api/include" PARENT_SCOPE )
346
+ endfunction ()
351
347
352
- # Add the Torch CMake configuration to CMAKE_PREFIX_PATH so that find_package
353
- # can find Torch .
354
- function (add_torch_to_cmake_prefix_path )
348
+ # Return the base path to the installed Torch Python library in
349
+ # outVar .
350
+ function (get_torch_base_path outVar )
355
351
if (NOT PYTHON_EXECUTABLE )
356
352
resolve_python_executable ()
357
353
endif ()
@@ -370,6 +366,13 @@ function(add_torch_to_cmake_prefix_path)
370
366
message ("Output:\n ${_tmp_torch_path} " )
371
367
message (FATAL_ERROR "Error:\n ${_tmp_torch_path_error} " )
372
368
endif ()
369
+ set (${outVar} ${_tmp_torch_path} PARENT_SCOPE )
370
+ endfunction ()
371
+
372
+ # Add the Torch CMake configuration to CMAKE_PREFIX_PATH so that find_package
373
+ # can find Torch.
374
+ function (add_torch_to_cmake_prefix_path )
375
+ get_torch_base_path (_tmp_torch_path )
373
376
list (APPEND CMAKE_PREFIX_PATH "${_tmp_torch_path} " )
374
377
set (CMAKE_PREFIX_PATH
375
378
"${CMAKE_PREFIX_PATH} "
0 commit comments