# Copyright 2006-2008 The FLWOR Foundation.
# 
# 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.

#-----------------------------------------------------------------------------
# Let's use the highest warning level.
#-----------------------------------------------------------------------------
IF(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")
#  # Use the highest warning level for visual studio.
#  IF(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
#    STRING(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
#  ELSE(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
#    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
#  ENDIF(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
#  IF(CMAKE_C_FLAGS MATCHES "/W[0-4]")
#    STRING(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
#  ELSE(CMAKE_C_FLAGS MATCHES "/W[0-4]")
#    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
#  ENDIF(CMAKE_C_FLAGS MATCHES "/W[0-4]")
#  # Disable deprecation warnings for standard C functions in VS2005 and later
#  IF(CMAKE_COMPILER_2005)
#    ADD_DEFINITIONS(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE)
#  ENDIF(CMAKE_COMPILER_2005)
ENDIF(CMAKE_BUILD_TOOL MATCHES "(msdev|devenv|nmake)")

IF(CMAKE_COMPILER_IS_GNUCXX)
  IF (ZORBA_WITH_CODE_COVERAGE)
    IF (NOT CMAKE_CXX_FLAGS MATCHES "profile-arcs")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs")
    ENDIF (NOT CMAKE_CXX_FLAGS MATCHES "profile-arcs")
    IF (NOT CMAKE_CXX_FLAGS MATCHES "test-coverage")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage")
    ENDIF (NOT CMAKE_CXX_FLAGS MATCHES "test-coverage")
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "profile-arcs")
      SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fprofile-arcs")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "profile-arcs")
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "test-coverage")
      SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -ftest-coverage")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "test-coverage")
  ENDIF (ZORBA_WITH_CODE_COVERAGE)
  IF (ZORBA_WITH_CODE_PROFILE)
    IF (NOT CMAKE_CXX_FLAGS MATCHES "pg")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
    ENDIF (NOT CMAKE_CXX_FLAGS MATCHES "pg")
  ENDIF (ZORBA_WITH_CODE_PROFILE)
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wall")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wall")
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wextra")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wextra")
  IF (UNIX AND NOT APPLE)
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "as-needed")
      SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "as-needed")
  ENDIF (UNIX AND NOT APPLE)
#  # Wabi (Warn when G++ generates code that is probably not compatible with the vendor-neutral C++ ABI.)
#  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wabi")
#    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wabi")
#  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wabi")
  # Warn when a class seems unusable because all the constructors or destructors in that class are private, and it has neither friends nor public static member functions.
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wctor-dtor-privacy")
    IF (NOT APPLE)  # apple g++ throws to many of these warnings for boost 
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wctor-dtor-privacy")
    ENDIF (NOT APPLE)
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wctor-dtor-privacy")
#  # Warn about violations of the following style guidelines from Scott Meyers’ Effective C++ book (Problem: std header do not obey this guideline)
#  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Weffc++")
#    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Weffc++")
#  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Weffc++")
#  # Warn if an old-style (C-style) cast to a non-void type is used within a C++ program.
#  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wold-style-cast")
#    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wold-style-cast")
#  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wold-style-cast")
  # Warn when a function declaration hides virtual functions from a base class.
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Woverloaded-virtual")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Woverloaded-virtual")
  # Warn when overload resolution chooses a promotion from unsigned or enumerated type to a signed type, over a conversion to an unsigned type of the same size.
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wsign-promo")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsign-promo")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wsign-promo")
  # Warn about uninitialized variables which are initialized with themselves.
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Winit-self")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winit-self")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Winit-self")
#  # Warn if a user-supplied include directory does not exist.
#  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wmissing-include-dirs")
#    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-include-dirs")
#  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wmissing-include-dirs")
  # Temporarly supresses the unused parameter warning
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-Wno-unused-parameter")
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-Wno-unused-parameter")
  IF(NOT CMAKE_CXX_FLAGS MATCHES "-fPIC")
    IF (UNIX AND NOT APPLE) # not for mach libraries
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") 
    ENDIF (UNIX AND NOT APPLE)
  ENDIF(NOT CMAKE_CXX_FLAGS MATCHES "-fPIC")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)

IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "all_load")
  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -all_load")
ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "all_load")

IF(CMAKE_COMPILER_IS_GNUCC)
  IF (ZORBA_WITH_CODE_COVERAGE)
    IF (NOT CMAKE_CXX_FLAGS MATCHES "profile-arcs")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs")
    ENDIF (NOT CMAKE_CXX_FLAGS MATCHES "profile-arcs")
    IF (NOT CMAKE_CXX_FLAGS MATCHES "test-coverage")
      SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage")
    ENDIF (NOT CMAKE_CXX_FLAGS MATCHES "test-coverage")
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "profile-arcs")
      SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "profile-arcs")
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "test-coverage")
      SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_FLAGS} -ftest-coverage")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "test-coverage")
  ENDIF (ZORBA_WITH_CODE_COVERAGE)
  IF(NOT CMAKE_C_FLAGS MATCHES "-Wall")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  ENDIF(NOT CMAKE_C_FLAGS MATCHES "-Wall")
  IF(NOT CMAKE_C_FLAGS MATCHES "-Wextra")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
  ENDIF(NOT CMAKE_C_FLAGS MATCHES "-Wextra")
  IF(NOT CMAKE_C_FLAGS MATCHES "-Wno-unused-parameter")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
  ENDIF(NOT CMAKE_C_FLAGS MATCHES "-Wno-unused-parameter")
  # Warn about uninitialized variables which are initialized with themselves.
  IF(NOT CMAKE_C_FLAGS MATCHES "-Winit-self")
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winit-self")
  ENDIF(NOT CMAKE_C_FLAGS MATCHES "-Winit-self")
  IF (ZORBA_WITH_CODE_COVERAGE)
    IF (NOT CMAKE_C_FLAGS MATCHES "profile-arcs")
      SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs")
    ENDIF (NOT CMAKE_C_FLAGS MATCHES "profile-arcs")
    IF (NOT CMAKE_C_FLAGS MATCHES "test-coverage")
      SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftest-coverage")
    ENDIF (NOT CMAKE_C_FLAGS MATCHES "test-coverage")
  ENDIF (ZORBA_WITH_CODE_COVERAGE)
  IF (ZORBA_WITH_CODE_PROFILE)
    IF (NOT CMAKE_C_FLAGS MATCHES "pg")
      SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
    ENDIF (NOT CMAKE_C_FLAGS MATCHES "pg")
  ENDIF (ZORBA_WITH_CODE_PROFILE)
  IF (UNIX AND NOT APPLE)
    IF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "as-needed")
      SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed")
    ENDIF (NOT CMAKE_SHARED_LINKER_FLAGS MATCHES "as-needed")
  ENDIF (UNIX AND NOT APPLE)
  IF(NOT CMAKE_C_FLAGS MATCHES "-fPIC")
    IF (UNIX AND NOT APPLE) # not for mach libraries
      SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") 
    ENDIF (UNIX AND NOT APPLE)
  ENDIF(NOT CMAKE_C_FLAGS MATCHES "-fPIC")
ENDIF(CMAKE_COMPILER_IS_GNUCC)
