#this file depends on Build_Options.cmake
#Change compiler flags here and they will propagate to the library and every project

#get processor count
INCLUDE(ProcessorCount)
ProcessorCount(N)

#Flags for MSVC (Visual Studio/Microsoft C++ Compiler)
IF(MSVC)
  ADD_DEFINITIONS (-DWIN32)
  ADD_DEFINITIONS (-DNOMINMAX)
  ADD_DEFINITIONS (-D_CRT_SECURE_NO_WARNINGS)
  ADD_DEFINITIONS (-MP)
  ADD_DEFINITIONS (-WX)
  ADD_DEFINITIONS (-wd4996)
  ADD_DEFINITIONS (-wd4355)
  ADD_DEFINITIONS (-wd4150)
  SET(OS_LIBRARIES psapi)
ENDIF(MSVC)

#Flags for g++ (GNU C++ Compiler)
IF(CMAKE_COMPILER_IS_GNUCXX)
   ADD_DEFINITIONS (-Werror)
   ADD_DEFINITIONS (-Wall)
   SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -funroll-loops")
   #TODO: Detect architecture and set SSE/AVX capabilities accordingly
   SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -msse3 -mfpmath=sse")
   SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
   ADD_DEFINITIONS (-Winit-self) 
   ADD_DEFINITIONS (-Woverloaded-virtual) 
   ADD_DEFINITIONS (-Wstrict-aliasing=2) 
   ADD_DEFINITIONS (-fno-strict-aliasing)
   ADD_DEFINITIONS (-Wno-strict-overflow)
   ADD_DEFINITIONS (-fPIC)
   SET(CTEST_BUILD_FLAGS -j${N})
ENDIF(CMAKE_COMPILER_IS_GNUCXX)

#Flags for icpc (Intel C++ Compiler/Composer)
IF(${CMAKE_CXX_COMPILER} MATCHES "icpc.*$")
   #This flag sets optimization levels based on host system capability
   #i.e. -xSSE4.2 if SSE4.2 is supported
   SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -xHOST")
   #icpc's Interprocedural Optimization (IPO) is extremely slow, use with caution
   #SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ipo")
ENDIF(${CMAKE_CXX_COMPILER} MATCHES "icpc.*$")

#Compiler-independent settings
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DCOMPILE_ID_TYPES_AS_INT")

#each build option in Build_Options should have a corresponding IF statement below
IF(PHYSBAM_USE_LIBPNG)
    ADD_DEFINITIONS(-DUSE_LIBPNG)
ENDIF()
IF(PHYSBAM_USE_LIBJPEG)
    ADD_DEFINITIONS(-DUSE_LIBJPEG)
ENDIF()
#Sanity checks for MPI
IF(PHYSBAM_USE_OPENMPI AND PHYSBAM_USE_LAMMPI)
    MESSAGE(FATAL_ERROR "PhysBAM supports either OpenMPI or LAM-MPI, not both at the same time.")
ENDIF(PHYSBAM_USE_OPENMPI AND PHYSBAM_USE_LAMMPI)
IF(PHYSBAM_USE_LAMMPI AND WIN32)
    MESSAGE(FATAL_ERROR "PhysBAM supports only OpenMPI on Windows.")
ENDIF(PHYSBAM_USE_LAMMPI AND WIN32)
IF(PHYSBAM_USE_OPENMPI)
    ADD_DEFINITIONS(-DUSE_MPI)
    ADD_DEFINITIONS(-DOPEN_MPI)
ENDIF()
IF(PHYSBAM_USE_LAMMPI)
    ADD_DEFINITIONS(-DUSE_MPI)
    ADD_DEFINITIONS(-DLAM_MPI)
    #these flags are valid because LAM-MPI will only be compiled with g++
    #install LAM-MPI into the default location (/usr/local)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include -pthread -L/usr/local/lib -llammpio -llammpi++ -llamf77mpi -lmpi -llam -lutil -ldl")
ENDIF()
IF(PHYSBAM_USE_PTHREADS)
    ADD_DEFINITIONS(-DUSE_PTHREADS)
ENDIF()
IF(PHYSBAM_USE_CUDA)
    ADD_DEFINITIONS(-DUSE_CUDA)
ENDIF(PHYSBAM_USE_CUDA)
IF(PHYSBAM_USE_OPTIX)
    IF(NOT PHYSBAM_USE_CUDA)
      MESSAGE(FATAL_ERROR "You need to enable CUDA support for OptiX to work.")
    ENDIF(NOT PHYSBAM_USE_CUDA)
    ADD_DEFINITIONS(-DUSE_OPTIX)
ENDIF(PHYSBAM_USE_OPTIX)
IF(PHYSBAM_USE_TBB)
    ADD_DEFINITIONS(-DUSE_TBB)
ENDIF(PHYSBAM_USE_TBB)
IF(PHYSBAM_COMPILE_WITHOUT_DYADIC_SUPPORT)
    ADD_DEFINITIONS(-DCOMPILE_WITHOUT_DYADIC_SUPPORT)
ENDIF()
IF(PHYSBAM_COMPILE_WITHOUT_RLE_SUPPORT)
    ADD_DEFINITIONS(-DCOMPILE_WITHOUT_RLE_SUPPORT)
ENDIF()
IF(PHYSBAM_COMPILE_WITHOUT_DOUBLE_SUPPORT)
    ADD_DEFINITIONS(-DCOMPILE_WITHOUT_DOUBLE_SUPPORT)
ENDIF()
IF(PHYSBAM_COMPILE_WITHOUT_READ_WRITE_SUPPORT)
    ADD_DEFINITIONS(-DCOMPILE_WITHOUT_READ_WRITE_SUPPORT)
ENDIF()
