CMAKE_MINIMUM_REQUIRED( VERSION 3.1 )

PROJECT( VSP_SUPER_PROJECT )

set( CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH} )

INCLUDE( ExternalProject )
INCLUDE( ExternalProject_ForceBuild )

IF(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "amd64")
	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
	SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
ENDIF()

ExternalProject_Add( Libraries
	SOURCE_DIR ${CMAKE_SOURCE_DIR}/../Libraries
	CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
		-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
		-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
		-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
		-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
		-DVSP_NO_GRAPHICS=${VSP_NO_GRAPHICS}
		-DVSP_USE_SYSTEM_CPPTEST=${VSP_USE_SYSTEM_CPPTEST}
		-DVSP_USE_SYSTEM_LIBXML2=${VSP_USE_SYSTEM_LIBXML2}
		-DVSP_USE_SYSTEM_EIGEN=${VSP_USE_SYSTEM_EIGEN}
		-DVSP_USE_SYSTEM_CODEELI=${VSP_USE_SYSTEM_CODEELI}
		-DVSP_USE_SYSTEM_FLTK=${VSP_USE_SYSTEM_FLTK}
		-DVSP_USE_SYSTEM_GLM=${VSP_USE_SYSTEM_GLM}
		-DVSP_USE_SYSTEM_GLEW=${VSP_USE_SYSTEM_GLEW}
		-DVSP_USE_SYSTEM_CMINPACK=${VSP_USE_SYSTEM_CMINPACK}
		-DVSP_USE_SYSTEM_LIBIGES=${VSP_USE_SYSTEM_LIBIGES}
		-DVSP_USE_SYSTEM_STEPCODE=${VSP_USE_SYSTEM_STEPCODE}
		-DVSP_USE_SYSTEM_EXPRPARSE=${VSP_USE_SYSTEM_EXPRPARSE}
		-DVSP_USE_SYSTEM_TRIANGLE=${VSP_USE_SYSTEM_TRIANGLE}
		-DVSP_USE_SYSTEM_ADEPT2=${VSP_USE_SYSTEM_ADEPT2}
	INSTALL_COMMAND ""
)
ExternalProject_Get_Property( Libraries BINARY_DIR )
SET( VSP_LIBRARY_PATH ${BINARY_DIR} )
EP_ForceBuild( Libraries )

ExternalProject_Add( OpenVSP
	SOURCE_DIR ${CMAKE_SOURCE_DIR}/..
	CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
		-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
		-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
		-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
		-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
		-DVSP_LIBRARY_PATH=${VSP_LIBRARY_PATH}
		-DC_OMP_COMPILER=${C_OMP_COMPILER}
		-DCXX_OMP_COMPILER=${CXX_OMP_COMPILER}
		-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
		-DPYTHON_LIBRARY=${PYTHON_LIBRARY}
		-DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR}
		-DPYTHON_INCLUDE_PATH=${PYTHON_INCLUDE_PATH}
		-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
		-DVSP_INSTALL_API_TEST=${VSP_INSTALL_API_TEST}
	INSTALL_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target install
	DEPENDS Libraries
)
EP_ForceBuild( OpenVSP )

ExternalProject_Add_Step( OpenVSP package
    COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --target package
    DEPENDEES build
)
