# Copyright (c) <2014> <Newton Game Dynamics>
# 
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# 
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely.

if (NEWTON_DISABLE_ASSERT)
  add_definitions(-DDG_DISABLE_ASSERT)
endif()

if (NEWTON_USE_DOUBLE)
  add_definitions(-D_NEWTON_USE_DOUBLE)
endif()

# dMath
file(GLOB dMath_srcs dMath/*.cpp)
file(GLOB dMath_includes dMath/*.h)
add_library(dMath ${dMath_srcs} ${dMath_includes})
target_include_directories(dMath PUBLIC dMath/)

set_property(
  TARGET dMath
  PROPERTY COMPILE_DEFINITIONS _LIB _CRT_SECURE_NO_WARNINGS
)

install(TARGETS dMath DESTINATION ${CMAKE_INSTALL_LIBDIR})
FILE(GLOB dMathFiles "${CMAKE_CURRENT_SOURCE_DIR}/dMath/*.h")
install(FILES ${dMathFiles} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


# dContainers
file(GLOB dContainers_srcs dContainers/*.cpp)
file(GLOB dContainers_includes dContainers/*.h)

if (NEWTON_BUILD_STATIC)
  add_library(dContainers STATIC ${dContainers_srcs} ${dContainers_includes})
  set_property(
    TARGET dContainers
    PROPERTY COMPILE_DEFINITIONS _LIB _CRT_SECURE_NO_WARNINGS
  )
else (NEWTON_BUILD_STATIC)
  add_library(dContainers SHARED ${dContainers_srcs} ${dContainers_includes})
  set_property(
    TARGET dContainers
    PROPERTY COMPILE_DEFINITIONS _LIB _CRT_SECURE_NO_WARNINGS _DCONTAINERS_DLL _DCONTAINERS_EXPORT
  )
endif(NEWTON_BUILD_STATIC)

target_include_directories(dContainers PUBLIC dContainers/)
target_link_libraries(dContainers dMath)
install(TARGETS dContainers DESTINATION ${CMAKE_INSTALL_LIBDIR})
FILE(GLOB dContainersFiles "${CMAKE_CURRENT_SOURCE_DIR}/dContainers/*.h")
install(FILES ${dContainersFiles} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


# dCustomJoints
file(GLOB dCustomJoints_srcs dCustomJoints/*.cpp)
file(GLOB dCustomJoints_includes dCustomJoints/*.h)

if (NEWTON_BUILD_STATIC)
  add_library(dCustomJoints STATIC ${dCustomJoints_srcs} ${dCustomJoints_includes})
  set_property(
    TARGET dCustomJoints
    PROPERTY COMPILE_DEFINITIONS _CUSTOM_JOINTS_STATIC_LIB _LIB _CRT_SECURE_NO_WARNINGS
  )
else (NEWTON_BUILD_STATIC)
  add_library(dCustomJoints SHARED ${dCustomJoints_srcs} ${dCustomJoints_includes})
  set_property(
    TARGET dCustomJoints
    PROPERTY COMPILE_DEFINITIONS _DCONTAINERS_DLL _CUSTOM_JOINTS_BUILD_DLL _LIB _CRT_SECURE_NO_WARNINGS
  )
endif(NEWTON_BUILD_STATIC)

target_include_directories(dCustomJoints PUBLIC dCustomJoints/)
target_link_libraries(dCustomJoints dContainers Newton)

install(TARGETS dCustomJoints DESTINATION ${CMAKE_INSTALL_LIBDIR})
FILE(GLOB dCustomJointsFiles "${CMAKE_CURRENT_SOURCE_DIR}/dCustomJoints/*.h")
install(FILES ${dCustomJointsFiles} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


# dNewton
file(GLOB dNewton_srcs dNewton/*.cpp)
file(GLOB dNewton_includes dNewton/*.h)

if (NEWTON_BUILD_STATIC)
  add_library(dNewton STATIC ${dNewton_srcs} ${dNewton_includes})
  
  set_property(
    TARGET dNewton
    PROPERTY COMPILE_DEFINITIONS _CNEWTON_STATIC_LIB _NEWTON_STATIC_LIB _CUSTOM_JOINTS_STATIC_LIB  _LIB _CRT_SECURE_NO_WARNINGS
  )
else (NEWTON_BUILD_STATIC)
  add_library(dNewton SHARED ${dNewton_srcs} ${dNewton_includes})
  set_property(
    TARGET dNewton
    PROPERTY COMPILE_DEFINITIONS _CNEWTON_BUILD_DLL _LIB _CRT_SECURE_NO_WARNINGS
  )
endif(NEWTON_BUILD_STATIC)

target_include_directories(dNewton PUBLIC dNewton/)
target_link_libraries(dNewton dCustomJoints Newton)
                
install(TARGETS dNewton DESTINATION ${CMAKE_INSTALL_LIBDIR})
FILE(GLOB dNewtonFiles "${CMAKE_CURRENT_SOURCE_DIR}/dNewton/*.h")
install(FILES ${dNewtonFiles} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


# dScene
if (TINYXML_FOUND)
  file(GLOB dScene_srcs dScene/*.cpp)
  file(GLOB dScene_includes dScene/*.h)

  if (NEWTON_BUILD_STATIC)
    add_library(dScene STATIC ${dScene_srcs} ${dScene_includes})
    
    set_property(
      TARGET dScene
      PROPERTY COMPILE_DEFINITIONS _NEWTON_STATIC_LIB _LIB _CRT_SECURE_NO_WARNINGS
    )
  else (NEWTON_BUILD_STATIC)
    add_library(dScene SHARED ${dScene_srcs} ${dScene_includes})
    set_property(
      TARGET dScene
      PROPERTY COMPILE_DEFINITIONS _DSCENE_DLL _DSCENE_EXPORTS _CNEWTON_BUILD_DLL _DCONTAINERS_DLL _LIB _CRT_SECURE_NO_WARNINGS
    )
  endif(NEWTON_BUILD_STATIC)
  
  target_include_directories(dScene PUBLIC dScene/)
  target_link_libraries(dScene dMath dContainers Newton)
  
  install(TARGETS dScene DESTINATION ${CMAKE_INSTALL_LIBDIR})
  FILE(GLOB dSceneFiles "${CMAKE_CURRENT_SOURCE_DIR}/dScene/*.h")
  install(FILES ${dSceneFiles} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  
endif(TINYXML_FOUND)

# dVisualDebuggerServer
#file(GLOB dVisualDebuggerServer_srcs dVisualDebuggerServer/*.cpp)
#add_library(dVisualDebuggerServer ${dVisualDebuggerServer_srcs})

#target_include_directories(dVisualDebuggerServer PUBLIC dVisualDebuggerServer)
#target_link_libraries(dVisualDebuggerServer dContainers dMath Newton)

#find_package(ENet)
#include_directories(${ENet_INCLUDE_DIRS})
#target_link_libraries(dVisualDebuggerServer ${ENet_LIBRARY})


if (NOT ANDROID)
  if (${UNIX})
    if (BUILD_64)
      add_definitions(-D_POSIX_VER_64)
    else (BUILD_64)
      add_definitions(-D_POSIX_VER)
    endif (BUILD_64)
  endif(${UNIX})
endif(NOT ANDROID)

if (CMAKE_COMPILER_IS_GNUCC)
  if (ANDROID)
    add_definitions(-ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant -c -Wall -Wno-strict-aliasing) 
  elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
    add_definitions(-ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant -c -Wall -Wno-strict-aliasing)
  elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
    add_definitions(-ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant -c -Wall -Wno-strict-aliasing)
  else()
    add_definitions(-msse -msse2 -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant -c -Wall -Wno-strict-aliasing) 
  endif()
endif(CMAKE_COMPILER_IS_GNUCC)