Refactor target_compile_options into torch_compile_options (#29730)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/29730

Back in the day, Caffe2 had a good idea: instead of spattering
target_compile_options all over the codebase, define a helper
function which sets all the options for a target.  This is especially
helpful if I want to split libtorch.so into libtorch_cpu.so
and libtorch_cuda.so; I need a way to easily apply options
to multiple targets.  A shared helper function is just the ticket.

I moved every target_compile_options call in caffe2/CMakeLists.txt
that didn't seem target dependent (exclusions included OpenMP flags,
API-related macros, ONNX related macros and HIP flags) into
torch_compile_options.  I slavishly preserved the structure:
there's a nearly redundant WERROR if() in the output but I preserved
it.

There is one thing I don't like about this, which is that now
the compile options are off in a random directory that no one would
expect.  But c'est la vie...

Signed-off-by: Edward Z. Yang <ezyang@fb.com>

Test Plan: Imported from OSS

Differential Revision: D18571166

Pulled By: ezyang

fbshipit-source-id: 21cd5f7663485077600782078fbb1787fab09035
This commit is contained in:
Edward Yang
2019-11-18 07:03:16 -08:00
committed by Facebook Github Bot
parent 1381301d46
commit 0e5200adfe
2 changed files with 77 additions and 90 deletions

View File

@ -661,64 +661,9 @@ if (NOT INTERN_BUILD_MOBILE)
add_dependencies(torch Caffe2_PROTO)
endif()
torch_compile_options(torch) # see cmake/public/utils.cmake
if (NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
# until they can be unified, keep these lists synced with setup.py
if(MSVC)
if (MSVC_Z7_OVERRIDE)
set(MSVC_DEBINFO_OPTION "/Z7")
else()
set(MSVC_DEBINFO_OPTION "/Zi")
endif()
target_compile_options(torch PUBLIC
${MSVC_RUNTIME_LIBRARY_OPTION}
${MSVC_DEBINFO_OPTION}
/EHa
/DNOMINMAX
/wd4267
/wd4251
/wd4522
/wd4522
/wd4838
/wd4305
/wd4244
/wd4190
/wd4101
/wd4996
/wd4275
/bigobj
)
else()
target_compile_options(torch PUBLIC
# -std=c++11
-Wall
-Wextra
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wno-write-strings
-Wno-unknown-pragmas
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
-Wno-missing-braces
)
if(NOT APPLE)
target_compile_options(torch PRIVATE
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
-Wno-maybe-uninitialized)
endif()
endif()
if (MSVC)
elseif (WERROR)
target_compile_options(torch PRIVATE -Werror -Wno-strict-overflow)
endif()
if (NOT NO_API)
target_include_directories(torch PRIVATE
${TORCH_SRC_DIR}/csrc/api
@ -787,9 +732,6 @@ ENDIF()
${TORCH_ROOT}/third_party/miniz-2.0.8)
set_property(TARGET torch PROPERTY CXX_STANDARD 11)
install(DIRECTORY "${TORCH_SRC_DIR}/csrc"
DESTINATION ${TORCH_INSTALL_INCLUDE_DIR}/torch
FILES_MATCHING PATTERN "*.h")
@ -898,18 +840,6 @@ if(USE_ROCM)
)
endif()
if (NOT WIN32 AND NOT USE_ASAN)
# Enable hidden visibility by default to make it easier to debug issues with
# TORCH_API annotations. Hidden visibility with selective default visibility
# behaves close enough to Windows' dllimport/dllexport.
#
# Unfortunately, hidden visibility messes up some ubsan warnings because
# templated classes crossing library boundary get duplicated (but identical)
# definitions. It's easier to just disable it.
target_compile_options(torch PRIVATE "-fvisibility=hidden")
endif()
if (NOT INTERN_BUILD_MOBILE OR BUILD_CAFFE2_MOBILE)
caffe2_interface_library(caffe2_protos caffe2_protos_whole)
target_link_libraries(torch PRIVATE caffe2_protos_whole)
@ -1047,8 +977,6 @@ if (MSVC AND BUILD_SHARED_LIBS)
# to be used externally. Make sure that references match the export.
target_compile_options(torch PRIVATE "-DONNX_BUILD_MAIN_LIB")
endif()
# Use -O2 for release builds (-O3 doesn't improve perf, and -Os results in perf regression)
target_compile_options(torch PRIVATE "$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-O2>")
install(TARGETS torch EXPORT Caffe2Targets DESTINATION "${TORCH_INSTALL_LIB_DIR}")
@ -1121,11 +1049,6 @@ if(USE_ROCM)
target_include_directories(torch INTERFACE $<INSTALL_INTERFACE:include>)
endif()
# ---[ Check if warnings should be errors.
if (WERROR)
target_compile_options(torch PRIVATE -Werror)
endif()
# ---[ Test binaries.
if (BUILD_TEST)
foreach(test_src ${Caffe2_CPU_TEST_SRCS})

View File

@ -194,17 +194,81 @@ endmacro()
# Usage:
# torch_compile_options(lib_name)
function(torch_compile_options libname)
target_compile_options(${libname}
PRIVATE
-Wall
-Wextra
-fexceptions
-Wno-missing-field-initializers
-Wno-strict-overflow
-Wno-type-limits
-Wno-unused-parameter
-Wno-unknown-warning-option
-Wno-unknown-pragmas)
set_property(TARGET ${libname} PROPERTY CXX_STANDARD 11)
if (NOT INTERN_BUILD_MOBILE OR NOT BUILD_CAFFE2_MOBILE)
# until they can be unified, keep these lists synced with setup.py
if(MSVC)
if (MSVC_Z7_OVERRIDE)
set(MSVC_DEBINFO_OPTION "/Z7")
else()
set(MSVC_DEBINFO_OPTION "/Zi")
endif()
target_compile_options(${libname} PUBLIC
${MSVC_RUNTIME_LIBRARY_OPTION}
${MSVC_DEBINFO_OPTION}
/EHa
/DNOMINMAX
/wd4267
/wd4251
/wd4522
/wd4522
/wd4838
/wd4305
/wd4244
/wd4190
/wd4101
/wd4996
/wd4275
/bigobj
)
else()
target_compile_options(${libname} PUBLIC
# -std=c++11
-Wall
-Wextra
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wno-write-strings
-Wno-unknown-pragmas
# Clang has an unfixed bug leading to spurious missing braces
# warnings, see https://bugs.llvm.org/show_bug.cgi?id=21629
-Wno-missing-braces
)
if(NOT APPLE)
target_compile_options(${libname} PRIVATE
# Considered to be flaky. See the discussion at
# https://github.com/pytorch/pytorch/pull/9608
-Wno-maybe-uninitialized)
endif()
endif()
if (MSVC)
elseif (WERROR)
target_compile_options(${libname} PRIVATE -Werror -Wno-strict-overflow)
endif()
endif()
if (NOT WIN32 AND NOT USE_ASAN)
# Enable hidden visibility by default to make it easier to debug issues with
# TORCH_API annotations. Hidden visibility with selective default visibility
# behaves close enough to Windows' dllimport/dllexport.
#
# Unfortunately, hidden visibility messes up some ubsan warnings because
# templated classes crossing library boundary get duplicated (but identical)
# definitions. It's easier to just disable it.
target_compile_options(${libname} PRIVATE "-fvisibility=hidden")
endif()
# Use -O2 for release builds (-O3 doesn't improve perf, and -Os results in perf regression)
target_compile_options(${libname} PRIVATE "$<$<OR:$<CONFIG:Release>,$<CONFIG:RelWithDebInfo>>:-O2>")
# ---[ Check if warnings should be errors.
# TODO: Dedupe with WERROR check above
if (WERROR)
target_compile_options(${libname} PRIVATE -Werror)
endif()