From e534c5efd7e7a0ed476fa1e9ec2f37ccd3a1b63d Mon Sep 17 00:00:00 2001 From: Peter Bell Date: Tue, 30 Nov 2021 19:12:17 -0800 Subject: [PATCH] CMake: Include instead of copying cpu kernel files (#67656) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/67656 Currently, each cpu kernel file is copied into the build folder 3 times to give them different compilation flags. This changes it to instead generate 3 files that `#include` the original file. The biggest difference is that updating a copied file requires `cmake` to re-run, whereas include dependencies are natively handled by `ninja`. A side benefit is that included files show up directly in the build dependency graph, whereas `cmake` file copies don't. Test Plan: Imported from OSS Reviewed By: dagitses Differential Revision: D32566108 Pulled By: malfet fbshipit-source-id: ae75368fede37e7ca03be6ade3d4e4a63479440d --- cmake/Codegen.cmake | 6 +++--- cmake/IncludeSource.cpp.in | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 cmake/IncludeSource.cpp.in diff --git a/cmake/Codegen.cmake b/cmake/Codegen.cmake index 188513d2b6e4..dda9a52b5d29 100644 --- a/cmake/Codegen.cmake +++ b/cmake/Codegen.cmake @@ -67,7 +67,7 @@ if(INTERN_BUILD_ATEN_OPS) set_source_files_properties(${CMAKE_CURRENT_LIST_DIR}/../aten/src/ATen/MapAllocator.cpp PROPERTIES COMPILE_FLAGS "-fno-openmp") endif() - file(GLOB cpu_kernel_cpp_in "${CMAKE_CURRENT_LIST_DIR}/../aten/src/ATen/native/cpu/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/../aten/src/ATen/native/quantized/cpu/kernels/*.cpp") + file(GLOB cpu_kernel_cpp_in "${PROJECT_SOURCE_DIR}/aten/src/ATen/native/cpu/*.cpp" "${PROJECT_SOURCE_DIR}/aten/src/ATen/native/quantized/cpu/kernels/*.cpp") list(APPEND CPU_CAPABILITY_NAMES "DEFAULT") list(APPEND CPU_CAPABILITY_FLAGS "${OPT_FLAG}") @@ -129,10 +129,10 @@ if(INTERN_BUILD_ATEN_OPS) # See NOTE [ Linking AVX and non-AVX files ] foreach(i RANGE ${NUM_CPU_CAPABILITY_NAMES}) foreach(IMPL ${cpu_kernel_cpp_in}) - string(REPLACE "${CMAKE_CURRENT_LIST_DIR}/../aten/src/ATen/" "" NAME ${IMPL}) + file(RELATIVE_PATH NAME "${PROJECT_SOURCE_DIR}/aten/src/ATen/" "${IMPL}") list(GET CPU_CAPABILITY_NAMES ${i} CPU_CAPABILITY) set(NEW_IMPL ${CMAKE_BINARY_DIR}/aten/src/ATen/${NAME}.${CPU_CAPABILITY}.cpp) - configure_file(${IMPL} ${NEW_IMPL} COPYONLY) + configure_file("${PROJECT_SOURCE_DIR}/cmake/IncludeSource.cpp.in" ${NEW_IMPL}) set(cpu_kernel_cpp ${NEW_IMPL} ${cpu_kernel_cpp}) # Create list of copies list(GET CPU_CAPABILITY_FLAGS ${i} FLAGS) if(MSVC) diff --git a/cmake/IncludeSource.cpp.in b/cmake/IncludeSource.cpp.in new file mode 100644 index 000000000000..1ab08af8f089 --- /dev/null +++ b/cmake/IncludeSource.cpp.in @@ -0,0 +1 @@ +#include "@IMPL@"