# 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.

cmake_minimum_required(VERSION 2.8)

project(NewtonLib)

set(NEWTON_SOURCE ${PROJECT_SOURCE_DIR}/source)

file(GLOB dg_srcs source/core/*.cpp)         # low level library
file(GLOB amp_srcs source/ampPhysics/*.cpp)  # amp optim
file(GLOB physics_srcs source/physics/*.cpp) # physics engine
file(GLOB mesh_srcs source/meshUtil/*.cpp)   # mesh geometry
file(GLOB newton_srcs source/newton/*.cpp)   # Newton engine

file(GLOB dg_includes source/core/*.h)         # low level library
file(GLOB amp_includes source/ampPhysics/*.h)  # amp optim
file(GLOB physics_includes source/physics/*.h) # physics engine
file(GLOB mesh_includes source/meshUtil/*.h)   # mesh geometry
file(GLOB newton_includes source/newton/*.h)   # Newton engine

if (WIN32)
  file(GLOB_RECURSE pthread_srcs ${NEWTON_SOURCE}/pthreads.2/pthread.c)
else (WIN32)
  set(pthread_srcs "")
endif (WIN32)

if (NOT APPLE)
  
  if(MSVC12 OR MSVC13 OR MSVC14)
    add_library(NewtonObj OBJECT ${dg_srcs} ${amp_srcs} ${physics_srcs} ${mesh_srcs} ${newton_srcs} ${pthread_srcs} ${dg_includes} ${amp_includes} ${physics_includes} ${mesh_includes} ${newton_includes} ${pthread_includes})
  else()
    add_library(NewtonObj OBJECT ${dg_srcs} ${physics_srcs} ${mesh_srcs} ${newton_srcs} ${pthread_srcs} ${dg_includes} ${physics_includes} ${mesh_includes} ${newton_includes} ${pthread_includes})
  endif()
  
  # add modified pthread library
  if (WIN32)
    target_include_directories(NewtonObj PRIVATE ${NEWTON_SOURCE}/pthreads.2)
  endif (WIN32)
  
  target_include_directories(NewtonObj PRIVATE ${NEWTON_SOURCE}/ampPhysics)
  target_include_directories(NewtonObj PRIVATE ${NEWTON_SOURCE}/core)
  target_include_directories(NewtonObj PRIVATE ${NEWTON_SOURCE}/physics)
  target_include_directories(NewtonObj PRIVATE ${NEWTON_SOURCE}/meshUtil)
  target_include_directories(NewtonObj INTERFACE ${NEWTON_SOURCE}/newton)
  
  # create library
  if (NEWTON_BUILD_STATIC)
    add_library(Newton STATIC $<TARGET_OBJECTS:NewtonObj>)
  else (NEWTON_BUILD_STATIC)
    add_library(Newton SHARED $<TARGET_OBJECTS:NewtonObj>)
  endif(NEWTON_BUILD_STATIC)
  
  target_include_directories(Newton INTERFACE ${NEWTON_SOURCE}/newton)
else(NOT APPLE)
  add_library(Newton STATIC ${dg_srcs} ${physics_srcs} ${mesh_srcs} ${newton_srcs} ${pthread_srcs} ${dg_includes} ${physics_includes} ${mesh_includes} ${newton_includes} ${pthread_includes})
  
  # add modified pthread library
  if (WIN32)
    target_include_directories(Newton PRIVATE ${NEWTON_SOURCE}/pthreads.2)
  endif (WIN32)
  
  target_include_directories(Newton PRIVATE ${NEWTON_SOURCE}/core)
  target_include_directories(Newton PRIVATE ${NEWTON_SOURCE}/physics)
  target_include_directories(Newton PRIVATE ${NEWTON_SOURCE}/meshUtil)
  target_include_directories(Newton INTERFACE ${NEWTON_SOURCE}/newton)
endif(NOT APPLE)

if (UNIX)
  set_target_properties(Newton PROPERTIES OUTPUT_NAME Newton)
endif(UNIX)

install(TARGETS Newton DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${NEWTON_SOURCE}/newton/Newton.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

#TODO add a FIND CMAKE
if (WIN32)
  if (WIN64)
    install(FILES ${NEWTON_SOURCE}/pthreads.2/VS_2015_static_md/x64/pthread/Debug/pthread_d.lib DESTINATION ${CMAKE_INSTALL_LIBDIR})
    install(FILES ${NEWTON_SOURCE}/pthreads.2/VS_2015_static_md/x64/pthread/Release/pthread.lib DESTINATION ${CMAKE_INSTALL_LIBDIR})
  else (WIN64)
    install(FILES ${NEWTON_SOURCE}/pthreads.2/VS_2013_static_md/Win32/pthread/Debug/pthread_d.lib DESTINATION ${CMAKE_INSTALL_LIBDIR})
    install(FILES ${NEWTON_SOURCE}/pthreads.2/VS_2013_static_md/Win32/pthread/Release/pthread.lib DESTINATION ${CMAKE_INSTALL_LIBDIR})
  endif (WIN64)
endif (WIN32)

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

if (CMAKE_COMPILER_IS_GNUCC)
  if(ANDROID)
    add_definitions(-DDG_USE_THREAD_EMULATION -O2 -fpic -g -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant)
  elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
    add_definitions(-O3 -fpic -g -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant)
  elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
    add_definitions(-O3 -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant)
  else ()
    add_definitions(-fpic -msse -msse3 -mfpmath=sse -ffloat-store -ffast-math -freciprocal-math -funsafe-math-optimizations -fsingle-precision-constant)
  endif()
endif(CMAKE_COMPILER_IS_GNUCC)

if (MSVC)
  add_definitions(-D_CRT_SECURE_NO_WARNINGS -DPTW32_BUILD -DPTW32_STATIC_LIB)
  if (NEWTON_BUILD_STATIC)
    add_definitions(-D_LIB -D_NEWTON_STATIC_LIB)
  else (NEWTON_BUILD_STATIC)
    add_definitions(-D_NEWTON_BUILD_DLL)
  endif(NEWTON_BUILD_STATIC)
  
  if (NEWTON_DISABLE_ASSERT)
    add_definitions(-DDG_DISABLE_ASSERT)
  endif()

  set_source_files_properties(${NEWTON_SOURCE}/core/dgTypes.cpp PROPERTIES COMPILE_FLAGS "/YcdgStdafx.h")
  set_source_files_properties(${NEWTON_SOURCE}/newton/NewtonClass.cpp PROPERTIES COMPILE_FLAGS "/YcNewtonStdAfx.h")
  set_source_files_properties(${NEWTON_SOURCE}/physics/dgWorld.cpp PROPERTIES COMPILE_FLAGS "/YcdgPhysicsStdafx.h")
  
  set_target_properties(Newton PROPERTIES COMPILE_FLAGS "/YudgStdafx.h /YuNewtonStdAfx.h /YudgPhysicsStdafx.h")
endif(MSVC)

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