src/platform/python/CMakeLists.txt (view raw)
1find_package(PythonLibs ${USE_PYTHON_VERSION})
2find_package(PythonInterp ${USE_PYTHON_VERSION})
3list(APPEND DEPENDENCY_LIB ${PYTHON_LIBRARIES})
4include_directories(AFTER ${PYTHON_INCLUDE_DIRS})
5
6get_property(INCLUDE_DIRECTORIES DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
7set(INCLUDE_FLAGS)
8foreach(DIR IN LISTS INCLUDE_DIRECTORIES)
9 list(APPEND INCLUDE_FLAGS "-I${DIR}")
10endforeach()
11
12file(GLOB PYTHON_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
13
14configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in ${CMAKE_CURRENT_BINARY_DIR}/setup.py)
15add_custom_command(OUTPUT build/lib/${BINARY_NAME}/__init__.py
16 COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build --build-base ${CMAKE_CURRENT_BINARY_DIR}
17 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
18 DEPENDS ${BINARY_NAME}
19 DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/setup.py
20 DEPENDS ${PYTHON_HEADERS}
21 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py)
22
23add_custom_command(OUTPUT lib.c
24 COMMAND BINDIR=${CMAKE_CURRENT_BINARY_DIR}/.. CPPFLAGS="${INCLUDE_FLAGS}" ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py
25 DEPENDS ${PYTHON_HEADERS}
26 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/_builder.py)
27
28set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/lib.c PROPERTIES GENERATED ON)
29
30file(GLOB PYTHON_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
31add_library(${BINARY_NAME}-pylib STATIC ${CMAKE_CURRENT_BINARY_DIR}/lib.c ${PYTHON_SRC})
32set_target_properties(${BINARY_NAME}-pylib PROPERTIES INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR};${INCLUDE_DIRECTORIES}")
33set_target_properties(${BINARY_NAME}-pylib PROPERTIES COMPILE_DEFINITIONS "${OS_DEFINES};${FEATURE_DEFINES};${FUNCTION_DEFINES}")
34set(PYTHON_LIBRARY ${BINARY_NAME}-pylib PARENT_SCOPE)
35
36add_custom_target(${BINARY_NAME}-py ALL
37 COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py build
38 DEPENDS ${BINARY_NAME}-pylib ${CMAKE_CURRENT_BINARY_DIR}/build/lib/${BINARY_NAME}/__init__.py)
39
40file(GLOB TESTS ${CMAKE_CURRENT_SOURCE_DIR}/test_*.py)
41foreach(TEST IN LISTS TESTS)
42 if(APPLE)
43 set(PATH DYLD_LIBRARY_PATH)
44 elseif(WIN32)
45 set(PATH PATH)
46 else()
47 set(PATH LD_LIBRARY_PATH)
48 endif()
49 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/test_" "" TEST_NAME "${TEST}")
50 string(REPLACE ".py" "" TEST_NAME "${TEST_NAME}")
51 add_test(python-${TEST_NAME} ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/setup.py pytest --addopts ${TEST})
52 set_tests_properties(python-${TEST_NAME} PROPERTIES ENVIRONMENT "${PATH}=${CMAKE_CURRENT_BINARY_DIR}/..")
53endforeach()