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