]> git.proxmox.com Git - ceph.git/blame - ceph/cmake/modules/Distutils.cmake
import 15.2.9
[ceph.git] / ceph / cmake / modules / Distutils.cmake
CommitLineData
7c673cae
FG
1include(CMakeParseArguments)
2
3function(distutils_install_module name)
d2e6a577 4 set(py_srcs setup.py README.rst requirements.txt test-requirements.txt bin ${name})
7c673cae 5 foreach(src ${py_srcs})
d2e6a577
FG
6 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${src})
7 list(APPEND py_clone ${CMAKE_CURRENT_BINARY_DIR}/${src})
8 add_custom_command(
9 OUTPUT ${src}
10 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src}
11 COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/${src} ${src})
12 endif()
7c673cae 13 endforeach()
9f95a23c
TL
14 if(NOT TARGET ${name}-clone)
15 add_custom_target(${name}-clone ALL
16 DEPENDS ${py_clone})
17 endif()
18 cmake_parse_arguments(DU "" "INSTALL_SCRIPT" "" ${ARGN})
7c673cae
FG
19 install(CODE "
20 set(options --prefix=${CMAKE_INSTALL_PREFIX})
21 if(DEFINED ENV{DESTDIR})
22 if(EXISTS /etc/debian_version)
23 list(APPEND options --install-layout=deb)
24 endif()
11fdf7f2
TL
25 list(APPEND options
26 --root=\$ENV{DESTDIR}
27 --single-version-externally-managed)
7c673cae
FG
28 if(NOT \"${DU_INSTALL_SCRIPT}\" STREQUAL \"\")
29 list(APPEND options --install-script=${DU_INSTALL_SCRIPT})
30 endif()
31 endif()
32 execute_process(
9f95a23c 33 COMMAND ${Python3_EXECUTABLE}
7c673cae
FG
34 setup.py install \${options}
35 WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")")
36endfunction(distutils_install_module)
37
9f95a23c 38function(distutils_add_cython_module target name src)
7c673cae
FG
39 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
40 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
11fdf7f2
TL
41 # When using ccache, CMAKE_C_COMPILER is ccache executable absolute path
42 # and the actual C compiler is CMAKE_C_COMPILER_ARG1.
43 # However with a naive
44 # set(PY_CC ${compiler_launcher} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
45 # distutils tries to execve something like "/usr/bin/cmake gcc" and fails.
46 # Removing the leading whitespace from CMAKE_C_COMPILER_ARG1 helps to avoid
47 # the failure.
48 string(STRIP "${CMAKE_C_COMPILER_ARG1}" c_compiler_arg1)
49 string(STRIP "${CMAKE_CXX_COMPILER_ARG1}" cxx_compiler_arg1)
50 # Note: no quotes, otherwise distutils will execute "/usr/bin/ccache gcc"
51 # CMake's implicit conversion between strings and lists is wonderful, isn't it?
52 string(REPLACE " " ";" cflags ${CMAKE_C_FLAGS})
53 list(APPEND cflags -iquote${CMAKE_SOURCE_DIR}/src/include -w)
54 # This little bit of magic wipes out __Pyx_check_single_interpreter()
55 # Note: this is reproduced in distutils_install_cython_module
56 list(APPEND cflags -D'void0=dead_function\(void\)')
57 list(APPEND cflags -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0')
58 set(PY_CC ${compiler_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1} ${cflags})
59 set(PY_CXX ${compiler_launcher} ${CMAKE_CXX_COMPILER} ${cxx_compiler_arg1})
60 set(PY_LDSHARED ${link_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1} "-shared")
9f95a23c
TL
61
62 set(suffix_var "EXT_SUFFIX")
63 execute_process(COMMAND "${Python3_EXECUTABLE}" -c
64 "from distutils import sysconfig; print(sysconfig.get_config_var('${suffix_var}'))"
65 RESULT_VARIABLE result
66 OUTPUT_VARIABLE ext_suffix
67 ERROR_VARIABLE error
68 OUTPUT_STRIP_TRAILING_WHITESPACE)
69 if(NOT result EQUAL 0)
70 message(FATAL_ERROR "Unable to tell python extension's suffix: ${error}")
71 endif()
72 set(output_dir "${CYTHON_MODULE_DIR}/lib.3")
73 set(setup_py ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)
74 add_custom_command(
75 OUTPUT ${output_dir}/${name}${ext_suffix}
7c673cae
FG
76 COMMAND
77 env
11fdf7f2
TL
78 CC="${PY_CC}"
79 CXX="${PY_CXX}"
80 LDSHARED="${PY_LDSHARED}"
7c673cae
FG
81 OPT=\"-DNDEBUG -g -fwrapv -O2 -w\"
82 LDFLAGS=-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
83 CYTHON_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
84 CEPH_LIBDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
9f95a23c 85 ${Python3_EXECUTABLE} ${setup_py}
7c673cae 86 build --verbose --build-base ${CYTHON_MODULE_DIR}
9f95a23c
TL
87 --build-platlib ${output_dir}
88 MAIN_DEPENDENCY ${src}
89 DEPENDS ${setup_py}
90 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
91 add_custom_target(${target} ALL
92 DEPENDS ${output_dir}/${name}${ext_suffix})
7c673cae
FG
93endfunction(distutils_add_cython_module)
94
95function(distutils_install_cython_module name)
11fdf7f2
TL
96 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
97 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
98 set(PY_CC "${compiler_launcher} ${CMAKE_C_COMPILER}")
99 set(PY_LDSHARED "${link_launcher} ${CMAKE_C_COMPILER} -shared")
7c673cae 100 install(CODE "
11fdf7f2
TL
101 set(ENV{CC} \"${PY_CC}\")
102 set(ENV{LDSHARED} \"${PY_LDSHARED}\")
103 set(ENV{CPPFLAGS} \"-iquote${CMAKE_SOURCE_DIR}/src/include
104 -D'void0=dead_function\(void\)' \
105 -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0'\")
106 set(ENV{LDFLAGS} \"-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
107 set(ENV{CYTHON_BUILD_DIR} \"${CMAKE_CURRENT_BINARY_DIR}\")
108 set(ENV{CEPH_LIBDIR} \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
109
7c673cae
FG
110 set(options --prefix=${CMAKE_INSTALL_PREFIX})
111 if(DEFINED ENV{DESTDIR})
112 if(EXISTS /etc/debian_version)
113 list(APPEND options --install-layout=deb)
114 endif()
115 list(APPEND options --root=\$ENV{DESTDIR})
116 else()
117 list(APPEND options --root=/)
118 endif()
119 execute_process(
11fdf7f2 120 COMMAND
9f95a23c 121 ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py
7c673cae 122 build --verbose --build-base ${CYTHON_MODULE_DIR}
9f95a23c 123 --build-platlib ${CYTHON_MODULE_DIR}/lib.3
7c673cae
FG
124 build_ext --cython-c-in-temp --build-temp ${CMAKE_CURRENT_BINARY_DIR} --cython-include-dirs ${PROJECT_SOURCE_DIR}/src/pybind/rados
125 install \${options} --single-version-externally-managed --record /dev/null
126 egg_info --egg-base ${CMAKE_CURRENT_BINARY_DIR}
127 --verbose
128 WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\"
129 RESULT_VARIABLE install_res)
130 if(NOT \"\${install_res}\" STREQUAL 0)
131 message(FATAL_ERROR \"Failed to build and install ${name} python module\")
132 endif()
133 ")
134endfunction(distutils_install_cython_module)