Skip to content

Commit dad4443

Browse files
committed
Give targets that link torch_cpu_library torch's compile settings back
Linking the torch target does more than link. It also adds torch's include directories as system headers and TORCH_CXX_FLAGS to every target that links it. torch_cpu_library, which these targets now link instead so that a CPU-only torch can load them, carries neither. So two things changed for those targets. torch/csrc/api/include became a normal include directory, so warnings inside torch's headers are now reported as if they were our code. And a TORCH_CXX_FLAGS set at configure time no longer reached them. For example, compiling kernels/portable/cpu/op_allclose.cpp with -Wall -Wextra -Werror went from no errors to 25 errors, all inside torch's headers. Default builds were not affected. Add a small helper, executorch_target_torch_compile_settings, that gives a target those two settings, and call it for each target that links torch_cpu_library.
1 parent 3a21f4d commit dad4443

6 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,7 @@ if(EXECUTORCH_BUILD_PYBIND)
14611461
aten_bridge PRIVATE torch_cpu_library c10 executorch extension_tensor
14621462
)
14631463
endif()
1464+
executorch_target_torch_compile_settings(aten_bridge)
14641465
endif()
14651466

14661467
# pybind portable_lib

‎extension/llm/custom_ops/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ if(EXECUTORCH_BUILD_KERNELS_LLM_AOT)
235235
custom_ops_aot_lib PUBLIC cpublas torch_cpu_library extension_tensor
236236
extension_threadpool
237237
)
238+
executorch_target_torch_compile_settings(custom_ops_aot_lib)
238239
if(EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE_OPTIMIZED)
239240
target_compile_definitions(
240241
custom_ops_aot_lib PRIVATE EXECUTORCH_QUANTIZED_MOE_USE_TORCHAO=1

‎extension/llm/runner/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ if(EXECUTORCH_BUILD_PYBIND)
127127
_llm_runner PRIVATE extension_llm_runner tokenizers::tokenizers
128128
portable_lib ${TORCH_PYTHON_LIBRARY} torch_cpu_library
129129
)
130+
executorch_target_torch_compile_settings(_llm_runner)
130131
executorch_target_link_shared_runtime(_llm_runner)
131132

132133
set_target_properties(

‎extension/training/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ if(EXECUTORCH_BUILD_PYBIND)
9696
-fexceptions>
9797
)
9898
target_link_libraries(_training_lib PRIVATE ${_pybind_training_dep_libs})
99+
executorch_target_torch_compile_settings(_training_lib)
99100

100101
if(EXECUTORCH_BUILD_SHARED
101102
AND EXECUTORCH_BUILD_XNNPACK

‎tools/cmake/Codegen.cmake‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ function(gen_custom_ops_aot_lib)
336336
include_directories(${TORCH_INCLUDE_DIRS})
337337
# torch adds libtorch_cuda with a CUDA build of PyTorch; torch_cpu adds MKL.
338338
target_link_libraries(${GEN_LIB_NAME} PRIVATE torch_cpu_library)
339+
executorch_target_torch_compile_settings(${GEN_LIB_NAME})
339340

340341
executorch_target_link_options_shared_lib(${GEN_LIB_NAME})
341342
if(TARGET portable_lib)

‎tools/cmake/Utils.cmake‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ macro(find_package_torch)
280280
endif()
281281
endmacro()
282282

283+
# Compiles target_name the way linking torch would: torch's include directories
284+
# as system headers and TORCH_CXX_FLAGS. A target that links torch_cpu_library
285+
# instead of torch gets neither from it.
286+
function(executorch_target_torch_compile_settings target_name)
287+
target_include_directories(
288+
${target_name} SYSTEM PRIVATE ${TORCH_INCLUDE_DIRS}
289+
)
290+
target_compile_options(${target_name} PRIVATE ${TORCH_CXX_FLAGS})
291+
endfunction()
292+
283293
# Modify ${targetName}'s INTERFACE_INCLUDE_DIRECTORIES by wrapping each entry in
284294
# $<BUILD_INTERFACE:...> so that they work with CMake EXPORT.
285295
function(executorch_move_interface_include_directories_to_build_time_only

0 commit comments

Comments
 (0)