PROJECT(LIGGGHTS C CXX)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

SET(LIGGGHTS_MAJOR_VERSION 3)
SET(LIGGGHTS_MINOR_VERSION 7)
SET(LIGGGHTS_PATCH_VERSION 0)
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 -fPIC")
  ENDIF()
ENDIF()

FILE(GLOB SOURCES *.cpp)
FILE(GLOB HEADERS *.h)

#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)
  # enable parallelization
  foreach(flag_var
          CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
          CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
            set(${flag_var} "${${flag_var}} /MP4")
  endforeach()
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_obj OBJECT ${SOURCES})
ADD_DEPENDENCIES(liggghts_obj GenerateHeaders)

ADD_LIBRARY(liggghts_static STATIC $<TARGET_OBJECTS:liggghts_obj>)
SET_TARGET_PROPERTIES(liggghts_static PROPERTIES OUTPUT_NAME liggghts)
SET_PROPERTY(TARGET liggghts_static PROPERTY PUBLIC_HEADER ${HEADERS})

ADD_LIBRARY(liggghts_shared SHARED $<TARGET_OBJECTS:liggghts_obj>)
SET_TARGET_PROPERTIES(liggghts_shared PROPERTIES OUTPUT_NAME liggghts)
SET_PROPERTY(TARGET liggghts_shared PROPERTY PUBLIC_HEADER ${HEADERS})

#ADD_EXECUTABLE(liggghts_bin main.cpp)
#TARGET_LINK_LIBRARIES(liggghts_bin liggghts)
ADD_EXECUTABLE(liggghts_bin $<TARGET_OBJECTS:liggghts_obj>)
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_static ${VTK_LIBRARIES})
  TARGET_LINK_LIBRARIES(liggghts_shared ${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_static jpeg)
  TARGET_LINK_LIBRARIES(liggghts_shared 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_static ${MPI_LIBRARIES})
  TARGET_LINK_LIBRARIES(liggghts_shared ${MPI_LIBRARIES})
  TARGET_LINK_LIBRARIES(liggghts_bin ${MPI_LIBRARIES})

  IF(MPI_COMPILE_FLAGS)
    SET_TARGET_PROPERTIES(liggghts_static PROPERTIES COMPILE_FLAGS "${MPI_COMPILE_FLAGS}")
    SET_TARGET_PROPERTIES(liggghts_shared 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_static PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}")
    SET_TARGET_PROPERTIES(liggghts_shared 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_static mpi_stubs)
  TARGET_LINK_LIBRARIES(liggghts_shared 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)

install(TARGETS liggghts_bin liggghts_static liggghts_shared
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        PUBLIC_HEADER DESTINATION include)
IF (WIN32)
    install(DIRECTORY WINDOWS DESTINATION include)
ENDIF()
