PROJECT(LIGGGHTS C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(LIGGGHTS_MAJOR_VERSION 3)
SET(LIGGGHTS_MINOR_VERSION 2)
SET(LIGGGHTS_PATCH_VERSION 1)
SET(LIGGGHTS_VERSION ${LIGGGHTS_MAJOR_VERSION}.${LIGGGHTS_MINOR_VERSION}.${LIGGGHTS_PATCH_VERSION})

IF (NOT CMAKE_CXX_FLAGS)
  IF(CMAKE_COMPILER_IS_GNUCXX)
    SET(CMAKE_CXX_FLAGS "-O2 -funroll-loops -fstrict-aliasing -Wall -Wno-uninitialized -std=c++11 -Wno-literal-suffix")
  ENDIF()
ENDIF()

FILE(GLOB SOURCES *.cpp)
#LIST(REMOVE_ITEM SOURCES main.cpp)

IF(WIN32)
  #include (GenerateExportHeader)
  INCLUDE_DIRECTORIES(WINDOWS/extra)
  ADD_DEFINITIONS(-D_USE_MATH_DEFINES -DNOMINMAX)

  IF(MSVC)
    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
  ENDIF()

  FIND_PACKAGE(Cygwin REQUIRED)

  ADD_CUSTOM_TARGET(GenerateHeaders
           COMMAND GenerateHeaders.bat ${CYGWIN_INSTALL_PATH}
           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/WINDOWS)
ELSE()
  ADD_CUSTOM_TARGET(GenerateHeaders
           COMMAND sh Make.sh style
           COMMAND sh Make.sh models
           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
ENDIF()

#ADD_LIBRARY(liggghts STATIC ${SOURCES})
#ADD_LIBRARY(liggghts SHARED ${SOURCES})
#GENERATE_EXPORT_HEADER( liggghts
#             BASE_NAME liggghts
#             EXPORT_MACRO_NAME liggghts_EXPORT
#             EXPORT_FILE_NAME liggghts_Export.h
#             STATIC_DEFINE liggghts_BUILT_AS_STATIC
#)
#ADD_DEPENDENCIES(liggghts GenerateHeaders)

#ADD_EXECUTABLE(liggghts_bin main.cpp)
ADD_EXECUTABLE(liggghts_bin ${SOURCES})
#TARGET_LINK_LIBRARIES(liggghts_bin liggghts)
ADD_DEPENDENCIES(liggghts_bin GenerateHeaders)
SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES OUTPUT_NAME liggghts)

#=======================================

FIND_PACKAGE(VTK NO_MODULE)

IF(VTK_FOUND)
  INCLUDE(${VTK_USE_FILE})
  ADD_DEFINITIONS(-DLAMMPS_VTK)
#  TARGET_LINK_LIBRARIES(liggghts ${VTK_LIBRARIES})
  TARGET_LINK_LIBRARIES(liggghts_bin ${VTK_LIBRARIES})
  MESSAGE(STATUS "Found VTK")
ELSE(VTK_FOUND)
  MESSAGE(STATUS "VTK NOT found!")
ENDIF(VTK_FOUND)

#=======================================

FIND_PACKAGE(JPEG)

IF(JPEG_FOUND)
  INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
  ADD_DEFINITIONS(-DLAMMPS_JPEG)
  #TARGET_LINK_LIBRARIES(liggghts jpeg)
  TARGET_LINK_LIBRARIES(liggghts_bin jpeg)
ELSE(JPEG_FOUND)
  MESSAGE(STATUS "JPEG NOT found!")
ENDIF(JPEG_FOUND)

#=======================================

FIND_PACKAGE(MPI)

IF(MPI_FOUND)
  INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
#  TARGET_LINK_LIBRARIES(liggghts ${MPI_LIBRARIES})
  TARGET_LINK_LIBRARIES(liggghts_bin ${MPI_LIBRARIES})

  IF(MPI_COMPILE_FLAGS)
#    SET_TARGET_PROPERTIES(liggghts PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
    SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
  ENDIF()

  IF(MPI_LINK_FLAGS)
#    SET_TARGET_PROPERTIES(liggghts PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
    SET_TARGET_PROPERTIES(liggghts_bin PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
  ENDIF()
ELSE(MPI_FOUND)
  MESSAGE(STATUS "MPI NOT found! Using MPI Stubs instead.")
  ADD_SUBDIRECTORY(STUBS)
#  TARGET_LINK_LIBRARIES(liggghts mpi_stubs)
  TARGET_LINK_LIBRARIES(liggghts_bin mpi_stubs)
ENDIF(MPI_FOUND)

#=======================================

#install(TARGETS liggghts liggghts_bin
#        RUNTIME DESTINATION bin
#        LIBRARY DESTINATION lib)

install(TARGETS liggghts_bin
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib)
