Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake: disable array-bounds check & treat warnings as errors for pyextobj and oneflow_internal & fix warnings #5838

Merged
merged 7 commits into from
Aug 12, 2021
83 changes: 58 additions & 25 deletions cmake/oneflow.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
include(python)
include(CheckCXXCompilerFlag)

function(oneflow_add_executable)
if (BUILD_CUDA)
Expand All @@ -16,6 +17,58 @@ function(oneflow_add_library)
endif()
endfunction()

function(target_try_compile_option target flag)
message(STATUS "Try to add ${flag} to target ${target}")
# We cannot check for -Wno-foo as this won't throw a warning so we must check for the -Wfoo option directly
# http://stackoverflow.com/questions/38785168/cc1plus-unrecognized-command-line-option-warning-on-any-other-warning
string(REGEX REPLACE "^-Wno-" "-W" checkedFlag ${flag})
string(REGEX REPLACE "[-=]" "_" varName CXX_FLAG${checkedFlag})
# Avoid double checks. A compiler will not magically support a flag it did not before
if(NOT DEFINED ${varName}_SUPPORTED)
check_cxx_compiler_flag(${checkedFlag} ${varName}_SUPPORTED)
endif()
if (${varName}_SUPPORTED)
PragmaTwice marked this conversation as resolved.
Show resolved Hide resolved
message(STATUS "Add ${flag} to target ${target}")
PragmaTwice marked this conversation as resolved.
Show resolved Hide resolved
target_compile_options(${target} PRIVATE ${flag})
endif ()
endfunction()

function(target_try_compile_options target)
foreach(flag ${ARGN})
target_try_compile_option(${target} ${flag})
endforeach()
endfunction()

function(target_treat_warnings_as_errors target)
if (TREAT_WARNINGS_AS_ERRORS)
target_compile_options(${target} PRIVATE -Werror)

# TODO: remove it while fixing all deprecated call
target_try_compile_options(${target} -Wno-error=deprecated-declarations)

# disable unused-* for different compile mode (maybe unused in cpu.cmake, but used in cuda.cmake)
target_try_compile_options(${target}
-Wno-error=unused-const-variable
-Wno-error=unused-variable
-Wno-error=unused-local-typedefs
-Wno-error=unused-private-field
-Wno-error=unused-lambda-capture
)

target_try_compile_options(${target} -Wno-error=instantiation-after-specialization)

# the mangled name between `struct X` and `class X` is different in MSVC ABI, remove it while windows is supported (in MSVC/cl or clang-cl)
target_try_compile_options(${target} -Wno-error=mismatched-tags)

# disable for pointer operations of intrusive linked lists
target_try_compile_options(${target} -Wno-error=array-bounds)

# avoid check of memcpy for non-trivial types in opencv headers
target_try_compile_options(${target} -Wno-error=class-memaccess)

endif()
endfunction()

# source_group
if(WIN32)
set(oneflow_platform "windows")
Expand Down Expand Up @@ -257,31 +310,7 @@ if (BUILD_SHARED_LIBS)
endif()

target_compile_options(of_ccobj PRIVATE -Werror=return-type)

if (TREAT_WARNINGS_AS_ERRORS)
target_compile_options(of_ccobj PRIVATE -Werror)

# TODO: remove it while fixing all deprecated call
target_compile_options(of_ccobj PRIVATE -Wno-error=deprecated-declarations)

# disable unused-* for different compile mode (maybe unused in cpu.cmake, but used in cuda.cmake)
target_compile_options(of_ccobj PRIVATE -Wno-error=unused-const-variable)
target_compile_options(of_ccobj PRIVATE -Wno-error=unused-variable)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(of_ccobj PRIVATE -Wno-error=unused-private-field)
target_compile_options(of_ccobj PRIVATE -Wno-error=unused-local-typedef)
target_compile_options(of_ccobj PRIVATE -Wno-error=unused-lambda-capture)
target_compile_options(of_ccobj PRIVATE -Wno-error=instantiation-after-specialization)
endif()

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# the mangled name between `struct X` and `class X` is different in MSVC ABI, remove it while windows is supported (in MSVC/cl or clang-cl)
target_compile_options(of_ccobj PRIVATE -Wno-error=mismatched-tags)

# TODO: remove it while `oneflow/user/kernels/upsample_kernel.h:141:9: error: implicit conversion from 'double' to 'int' changes value from -0.75 to 0 [-Wliteral-conversion]` is fixed
target_compile_options(of_ccobj PRIVATE -Wno-error=literal-conversion)
endif()
endif()
target_treat_warnings_as_errors(of_ccobj)

# py ext lib
add_library(of_pyext_obj ${of_pyext_obj_cc})
Expand All @@ -292,6 +321,7 @@ if(BUILD_SHARED_LIBS AND APPLE)
endif()
add_dependencies(of_pyext_obj of_ccobj)
target_compile_options(of_pyext_obj PRIVATE -Werror=return-type)
target_treat_warnings_as_errors(of_pyext_obj)

if(APPLE)
set(of_libs -Wl,-force_load ${ONEFLOW_CUDA_LIBS} of_ccobj of_protoobj of_cfgobj)
Expand All @@ -310,6 +340,9 @@ set_target_properties(oneflow_internal PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${ON
target_link_libraries(oneflow_internal PRIVATE ${of_libs} ${oneflow_third_party_libs} of_pyext_obj ${oneflow_exe_third_party_libs})
target_include_directories(oneflow_internal PRIVATE ${Python_INCLUDE_DIRS} ${Python_NumPy_INCLUDE_DIRS})

target_compile_options(oneflow_internal PRIVATE -Werror=return-type)
target_treat_warnings_as_errors(oneflow_internal)

set(gen_pip_args "")
if (BUILD_CUDA)
list(APPEND gen_pip_args --cuda=${CUDA_VERSION})
Expand Down
2 changes: 1 addition & 1 deletion oneflow/api/python/functional/python_arg.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct AnyDataBase {
};

template<typename T>
struct AnyData : public AnyDataBase {
struct AnyData final : public AnyDataBase {
T content;
explicit AnyData(const T& v) : content(v) {}

Expand Down
2 changes: 1 addition & 1 deletion oneflow/core/framework/eager_blob_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace oneflow {

namespace compatible_py {

class EagerPhysicalBlobHeader : public BlobHeaderTrait {
class EagerPhysicalBlobHeader final : public BlobHeaderTrait {
public:
EagerPhysicalBlobHeader(const std::shared_ptr<Shape>& static_shape,
const std::shared_ptr<Shape>& shape, DataType dtype);
Expand Down
2 changes: 1 addition & 1 deletion oneflow/user/ops/batch_gather_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ REGISTER_USER_OP("batch_gather")
.PartialSum(user_op::OpArg("out", 0))
.Build();
} else {
std::shared_ptr<cfg::ErrorProto> err;
auto err = std::make_shared<cfg::ErrorProto>();
err->set_msg("BatchGatherOp: indices_num_axes equals " + std::to_string(indices_num_axes)
+ " (should be bigger than 1).");
err->mutable_check_failed_error();
Expand Down