cmake_minimum_required(VERSION 2.8.3)
project(hos_cosgi_scripting_python)

## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
  hos_celix
  hos_cosgi_core
  hos_cosgi_scripting
  hos_cosgi_devicerepository
  hos_cpp_utils
  hos_deps
)

set(USE_PYTHON_VERSION 2 CACHE STRING "Use Python version")

## System dependencies are found with CMake's conventions
find_package(APR REQUIRED)
find_package(CELIX REQUIRED)
find_package(libvariant REQUIRED)
find_package(PythonLibs ${USE_PYTHON_VERSION} REQUIRED)

if (USE_PYTHON_VERSION EQUAL 2)
	find_package(Boost REQUIRED COMPONENTS python)
elseif(USE_PYTHON_VERSION EQUAL 3)
	#find_package(Boost REQUIRED COMPONENTS python-py34)
	
	# Taken from http://code.metager.de/source/xref/lib/OpenImageIO/src/python/CMakeLists.txt:
	#Finding the python3 component for boost is a little tricky, since it has
	#different names on different systems. Try the most common ones
	#(boost_python3, boost_python-py34, …).
	foreach (_boost_py3_lib python3 python-py34 python-py33 python-py32)
		find_package (Boost 1.42 QUIET COMPONENTS ${_boost_py3_lib})
		string (TOUPPER ${_boost_py3_lib} boost_py3_lib_name)
		if (Boost_${boost_py3_lib_name}_FOUND)
			#Not the most beautiful thing to do, but that gets them included in
			#the target_link_libraries(…) call farther down
			set (Boost_PYTHON_LIBRARIES ${Boost_${boost_py3_lib_name}_LIBRARIES})
			break ()
		endif ()
	endforeach ()
	
else()
	message( FATAL_ERROR "Unsupported Python version ${USE_PYTHON_VERSION}" )
endif()


###################################
## catkin specific configuration ##
###################################

catkin_package(
	CATKIN_DEPENDS hos_celix hos_cosgi_core hos_cosgi_scripting hos_cpp_utils
)


###########
## Build ##
###########

add_definitions(
	-std=c++11
	-D_PYTHON_VERSION_=${USE_PYTHON_VERSION}
)

include_directories(
	${catkin_INCLUDE_DIRS}
	${APR_INCLUDE_DIR}
	${APR_INCLUDE_DIR}/apr-1.0
	${libvariant_INCLUDE_DIR}
	${PYTHON_INCLUDE_DIRS}
)

set(BUNDLE_SYMBOLICNAME "hos.hos_scripting_python${USE_PYTHON_VERSION}")
set(BUNDLE_VERSION "1.0.0")
set(BUNDLE_NAME "hos_scripting_python${USE_PYTHON_VERSION}")

bundle(hos_cosgi_scripting_python${USE_PYTHON_VERSION}
	SOURCES 
	${PROJECT_SOURCE_DIR}/src/BundleActivator
	${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/PyScriptingServiceImpl
	${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/utils/utils
	${PROJECT_SOURCE_DIR}/src/hos/cosgi/service/scripting/python/hosapi/PyHOSAPI
)

target_link_libraries(hos_cosgi_scripting_python${USE_PYTHON_VERSION}
	${catkin_LIBRARIES}
	${APR_LIBRARIES}
	${Boost_LIBRARIES}
	${libvariant_LIBRARIES}
	${PYTHON_LIBRARIES}
)


#############
## Install ##
#############

install(FILES
	${CMAKE_BINARY_DIR}/hos_cosgi_scripting_python${USE_PYTHON_VERSION}.zip
	DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/bundles
)

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/doc
   DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
   OPTIONAL
)


###################
## Documentation ##
###################

find_package(Doxygen)
if(DOXYGEN_FOUND)
	configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
	add_custom_target(doc
		${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
		WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
	)
endif(DOXYGEN_FOUND)

