# Copyright (c) <2014-2017> <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 3.9.0 FATAL_ERROR)

set (projectName "newtonPy")
message (${projectName})
if(MSVC OR MINGW)
	set (swig "${CMAKE_SOURCE_DIR}/thirdParty/swigwin/swigwin-4.0.2/swig.exe")
else()
	set (swig "/usr/bin/swig")
endif()

set (wrapper "${PROJECT_BINARY_DIR}/applications/toolsAndWrapers/${projectName}")
set (swigScript "${CMAKE_SOURCE_DIR}/applications/toolsAndWrapers/${projectName}")

execute_process(COMMAND ${swig} -c++ -python ${swigScript}/newton.i)

# source and header files
file(GLOB CPP_SOURCE 
	*.h
	*.cpp
	*.cxx
	*.i
	*.py
)

#source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/" PREFIX source/ FILES ${CPP_SOURCE})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/" FILES ${CPP_SOURCE})

include_directories(.)
include_directories(../../../sdk/dCore)
include_directories(../../../sdk/dNewton)
include_directories(../../../sdk/dTinyxml)
include_directories(../../../sdk/dCollision)
include_directories(../../../sdk/dNewton/dJoints)
include_directories(../../../sdk/dNewton/dModels)
include_directories(../../../sdk/dNewton/dIkSolver)
include_directories(../../../sdk/dNewton/dParticles)
include_directories(../../../sdk/dNewton/dModels/dVehicle)
include_directories(../../../sdk/dNewton/dModels/dCharacter)

add_library(${projectName} SHARED ${CPP_SOURCE})

if(DEFINED ENV{Python})
	#message ($ENV{PythonInclude})
	target_include_directories(${projectName} BEFORE PRIVATE "$ENV{Python}/Include")

	#message ("$ENV{Python}")
	target_link_directories(${projectName} BEFORE PRIVATE "$ENV{Python}/Libs")
else()
	find_package (Python COMPONENTS Development REQUIRED)
	target_include_directories(${projectName} PUBLIC ${Python_INCLUDE_DIRS})
	target_link_libraries(${projectName} ${Python_LIBRARIES})
endif()

if (NEWTON_BUILD_CREATE_SUB_PROJECTS)
	target_link_libraries (${projectName} ndNewton ndCollision ndCore)
else()
	target_link_libraries (${projectName} ndNewton)
endif()

if(NEWTON_ENABLE_AVX2_SOLVER)
	target_link_libraries (${projectName} ndSolverAvx2)
endif()

if (NEWTON_ENABLE_CUDA_SOLVER)
	target_link_libraries (${projectName} ndSolverCuda)
endif()

if(MSVC OR MINGW)
    if(CMAKE_VS_MSBUILD_COMMAND OR CMAKE_VS_DEVENV_COMMAND)
		set_target_properties(${projectName} PROPERTIES COMPILE_FLAGS "/YunewtonStdafx.h")
		set_source_files_properties(newtonStdafx.cpp PROPERTIES COMPILE_FLAGS "/YcnewtonStdafx.h")
		set_source_files_properties(newton_wrap.cxx PROPERTIES COMPILE_FLAGS "/Y-")
    endif()
endif()

set (pySrc "${CMAKE_CURRENT_SOURCE_DIR}")
list(APPEND PY_SOURCE 
	ndbpy.py
	newton.py
	newtonWorld.py)

if (MSVC OR MINGW)
  set (pySuffix ".pyd")
else()
  set (pySuffix ".so")
endif()

foreach(file IN LISTS PY_SOURCE)
	add_custom_command(
	TARGET ${projectName} POST_BUILD COMMAND ${CMAKE_COMMAND}
	ARGS -E copy ${pySrc}/${file} ${wrapper}/${projectName}/${file})
endforeach()

add_custom_command(
	TARGET ${projectName} POST_BUILD COMMAND ${CMAKE_COMMAND}
	ARGS -E copy $<TARGET_FILE:${projectName}> ${wrapper}/${projectName}/${projectName}${CMAKE_DEBUG_POSTFIX}${pySuffix})

#if(DEFINED ENV{Blender})
#  foreach(file IN LISTS PY_SOURCE)
#	add_custom_command(
#	TARGET ${projectName} POST_BUILD COMMAND ${CMAKE_COMMAND}
#	ARGS -E copy ${pySrc}/${file} $ENV{Blender}/scripts/addons/${projectName}/${file})
#    endforeach()
#    add_custom_command(
#	TARGET ${projectName} POST_BUILD COMMAND ${CMAKE_COMMAND}
#	ARGS -E copy $<TARGET_FILE:${projectName}> $ENV{Blender}/scripts/addons/${projectName}/_newton${pySuffix})
#endif()
