#-------------------------------------------------------------------------------
# nest in situ vis
#
# Copyright (c) 2017-2018 RWTH Aachen University, Germany,
# Virtual Reality & Immersive Visualisation Group.
#-------------------------------------------------------------------------------
#                                  License
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------

# flags for default warning levels
set(WARNING_LEVELS_RWTH_CLANG
  -Werror
  -Weverything
  -pedantic
  -pedantic-errors
  -Wextra-tokens
  -Wambiguous-member-template
  -Wbind-to-temporary-copy
  -Wno-c++98-compat
  -Wno-weak-vtables
)

set(WARNING_LEVELS_RWTH_GCC
  -Werror
  -Wall
  -Wextra
  -Wmissing-braces
  -pedantic
  -pedantic-errors
)

set(WARNING_LEVELS_RWTH_MSVC
  -WX
  -W4
  "$<$<CXX_COMPILER_ID:MSVC>:/w34062>"
  "$<$<CXX_COMPILER_ID:MSVC>:/w24302>"
  "$<$<CXX_COMPILER_ID:MSVC>:/w14311>"
  "$<$<CXX_COMPILER_ID:MSVC>:/w14312>"
)


# flags for overriding warning levels for files generated by Qt's MOC
set(WARNING_LEVELS_RWTH_CLANG_QT_MOC_OVERRIDE
  -Wno-padded
  -Wno-undefined-reinterpret-cast
)

set(WARNING_LEVELS_RWTH_GCC_QT_MOC_OVERRIDE
  -Wno-padded
  -Wno-undefined-reinterpret-cast
)

set(WARNING_LEVELS_RWTH_MSVC_QT_MOC_OVERRIDE
  -W0
)


# compiler identification
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND
    CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  set(IS_CLANG 1)
elseif(CMAKE_C_COMPILER_ID MATCHES "MSVC" AND
    CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  set(IS_MSVC 1)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" AND
    CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  set(IS_GCC 1)
endif()

# warning level setter macros
macro(SET_WARNING_LEVELS_RWTH TARGET)
  set(options)
  set(oneValueArgs SUPPRESS_WARNINGS_HEADER)
  set(multiValueArgs)
  cmake_parse_arguments(SWLR
    "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  if (SWLR_SUPPRESS_WARNINGS_HEADER)
    set(SUPPRESS_WARNING_HEADER_FILE ${SWLR_SUPPRESS_WARNINGS_HEADER})
  else()
    set(SUPPRESS_WARNING_HEADER_FILE ${CMAKE_CURRENT_BINARY_DIR}/${target}/suppress_warnings.hpp) 
  endif()
  
  find_file(SUPPRESS_WARNING_HEADER_TEMPLATE suppress_warnings.hpp.in
    NO_DEFAULT_PATH
    PATHS ${CMAKE_MODULE_PATH}
  )
  configure_file(${SUPPRESS_WARNING_HEADER_TEMPLATE} ${SUPPRESS_WARNING_HEADER_FILE})
  
  if(IS_CLANG)
    target_compile_options(${TARGET} PRIVATE ${WARNING_LEVELS_RWTH_CLANG})
    if (CMAKE_AUTOMOC)
      if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_autogen/moc_compilation.cpp)
      set_source_files_properties(
        ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_autogen/moc_compilation.cpp
        PROPERTIES COMPILE_FLAGS ${WARNING_LEVELS_RWTH_CLANG_QT_MOC_OVERRIDE})
    endif ()
    endif()
    target_compile_options(${TARGET} PRIVATE "-include${SUPPRESS_WARNING_HEADER_FILE}")
  elseif(IS_GCC)
    target_compile_options(${TARGET} PRIVATE ${WARNING_LEVELS_RWTH_GCC})
    if (CMAKE_AUTOMOC)
      if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_autogen/moc_compilation.cpp)
      set_source_files_properties(
        ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_autogen/moc_compilation.cpp
        PROPERTIES COMPILE_FLAGS ${WARNING_LEVELS_RWTH_GCC_QT_MOC_OVERRIDE})
    endif ()
    endif()
    target_compile_options(${TARGET} PRIVATE "-include${SUPPRESS_WARNING_HEADER_FILE}")
  elseif(IS_MSVC)
    target_compile_options(${TARGET} PRIVATE ${WARNING_LEVELS_RWTH_MSVC})
    if (CMAKE_AUTOMOC)
      if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_autogen/moc_compilation.cpp)
      set_source_files_properties(
        ${CMAKE_CURRENT_BINARY_DIR}/${target}_autogen/moc_compilation.cpp
        PROPERTIES COMPILE_FLAGS ${WARNING_LEVELS_RWTH_MSVC_QT_MOC_OVERRIDE})
    endif ()
    endif()
    target_compile_options(${TARGET} PRIVATE "/FI ${SUPPRESS_WARNING_HEADER_FILE}")
  else()
    message(WARNING "SET_WARNING_LEVELS_RWTH not implemented for your compiler. "
      "Implement it to support our no-warning policy.")
  endif()
endmacro()
  
