Skip to content

Commit 90a670e

Browse files
committed
Bypass find_package(torch) for find_package_torch_headers to help out cross compilation
To be honest, I don't understand how the Android demo compiles currently. This should make it more robust, but I'm submitting this as a draft and will wait until I have a concrete need for this. ghstack-source-id: a984326 ghstack-comment-id: 2675755240 Pull Request resolved: #8629
1 parent 52a3a9a commit 90a670e

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

build/Utils.cmake

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,25 @@ function(resolve_python_executable)
329329
endfunction()
330330

331331
# 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()
351347

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)
355351
if(NOT PYTHON_EXECUTABLE)
356352
resolve_python_executable()
357353
endif()
@@ -370,6 +366,13 @@ function(add_torch_to_cmake_prefix_path)
370366
message("Output:\n${_tmp_torch_path}")
371367
message(FATAL_ERROR "Error:\n${_tmp_torch_path_error}")
372368
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)
373376
list(APPEND CMAKE_PREFIX_PATH "${_tmp_torch_path}")
374377
set(CMAKE_PREFIX_PATH
375378
"${CMAKE_PREFIX_PATH}"

0 commit comments

Comments
 (0)