]> git.proxmox.com Git - ceph.git/blob - ceph/cmake/modules/Distutils.cmake
import ceph quincy 17.2.1
[ceph.git] / ceph / cmake / modules / Distutils.cmake
1 include(CMakeParseArguments)
2
3 # ensure that we are using the exact python version specified by
4 # 'WITH_PYTHON3', in case some included 3rd party libraries call
5 # 'find_package(Python3 ...) without specifying the exact version number. if
6 # the building host happens to have a higher version of python3, that version
7 # would be picked up instead by find_package(Python3). and that is not want we
8 # expect.
9 find_package(Python3 ${WITH_PYTHON3} EXACT
10 QUIET
11 REQUIRED
12 COMPONENTS Interpreter)
13
14 function(distutils_install_module name)
15 set(py_srcs setup.py README.rst requirements.txt test-requirements.txt bin ${name})
16 foreach(src ${py_srcs})
17 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${src})
18 list(APPEND py_clone ${CMAKE_CURRENT_BINARY_DIR}/${src})
19 add_custom_command(
20 OUTPUT ${src}
21 DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src}
22 COMMAND ${CMAKE_COMMAND} -E create_symlink ${CMAKE_CURRENT_SOURCE_DIR}/${src} ${src})
23 endif()
24 endforeach()
25 if(NOT TARGET ${name}-clone)
26 add_custom_target(${name}-clone ALL
27 DEPENDS ${py_clone})
28 endif()
29 cmake_parse_arguments(DU "" "INSTALL_SCRIPT" "" ${ARGN})
30 install(CODE "
31 set(options --prefix=${CMAKE_INSTALL_PREFIX})
32 if(DEFINED ENV{DESTDIR})
33 if(EXISTS /etc/debian_version)
34 list(APPEND options --install-layout=deb)
35 endif()
36 list(APPEND options
37 --root=\$ENV{DESTDIR}
38 --single-version-externally-managed)
39 endif()
40 if(NOT \"${DU_INSTALL_SCRIPT}\" STREQUAL \"\")
41 list(APPEND options --install-script=${DU_INSTALL_SCRIPT})
42 endif()
43 execute_process(
44 COMMAND ${Python3_EXECUTABLE}
45 setup.py install \${options}
46 WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")")
47 endfunction(distutils_install_module)
48
49 function(distutils_add_cython_module target name src)
50 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
51 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
52 # When using ccache, CMAKE_C_COMPILER is ccache executable absolute path
53 # and the actual C compiler is CMAKE_C_COMPILER_ARG1.
54 # However with a naive
55 # set(PY_CC ${compiler_launcher} ${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1})
56 # distutils tries to execve something like "/usr/bin/cmake gcc" and fails.
57 # Removing the leading whitespace from CMAKE_C_COMPILER_ARG1 helps to avoid
58 # the failure.
59 string(STRIP "${CMAKE_C_COMPILER_ARG1}" c_compiler_arg1)
60 string(STRIP "${CMAKE_CXX_COMPILER_ARG1}" cxx_compiler_arg1)
61 # Note: no quotes, otherwise distutils will execute "/usr/bin/ccache gcc"
62 # CMake's implicit conversion between strings and lists is wonderful, isn't it?
63 set(PY_CFLAGS ${COMPILE_OPTIONS})
64 cmake_parse_arguments(DU "DISABLE_VTA" "" "" ${ARGN})
65 if(DU_DISABLE_VTA AND HAS_VTA)
66 list(APPEND PY_CFLAGS -fno-var-tracking-assignments)
67 endif()
68 list(APPEND PY_CPPFLAGS -iquote${CMAKE_SOURCE_DIR}/src/include -w)
69 # This little bit of magic wipes out __Pyx_check_single_interpreter()
70 # Note: this is reproduced in distutils_install_cython_module
71 list(APPEND PY_CPPFLAGS -D'void0=dead_function\(void\)')
72 list(APPEND PY_CPPFLAGS -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0')
73 set(PY_CC ${compiler_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1})
74 set(PY_CXX ${compiler_launcher} ${CMAKE_CXX_COMPILER} ${cxx_compiler_arg1})
75 set(PY_LDSHARED ${link_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1} "-shared")
76
77 set(suffix_var "EXT_SUFFIX")
78 execute_process(COMMAND "${Python3_EXECUTABLE}" -c
79 "from distutils import sysconfig; print(sysconfig.get_config_var('${suffix_var}'))"
80 RESULT_VARIABLE result
81 OUTPUT_VARIABLE ext_suffix
82 ERROR_VARIABLE error
83 OUTPUT_STRIP_TRAILING_WHITESPACE)
84 if(NOT result EQUAL 0)
85 message(FATAL_ERROR "Unable to tell python extension's suffix: ${error}")
86 endif()
87 set(output_dir "${CYTHON_MODULE_DIR}/lib.3")
88 set(setup_py ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)
89 if(DEFINED ENV{VERBOSE})
90 set(maybe_verbose --verbose)
91 endif()
92 add_custom_command(
93 OUTPUT ${output_dir}/${name}${ext_suffix}
94 COMMAND
95 env
96 CC="${PY_CC}"
97 CFLAGS="${PY_CFLAGS}"
98 CPPFLAGS="${PY_CPPFLAGS}"
99 CXX="${PY_CXX}"
100 LDSHARED="${PY_LDSHARED}"
101 OPT=\"-DNDEBUG -g -fwrapv -O2 -w\"
102 LDFLAGS=-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
103 CYTHON_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
104 CEPH_LIBDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
105 ${Python3_EXECUTABLE} ${setup_py}
106 build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR}
107 --build-platlib ${output_dir}
108 MAIN_DEPENDENCY ${src}
109 DEPENDS ${setup_py}
110 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
111 add_custom_target(${target} ALL
112 DEPENDS ${output_dir}/${name}${ext_suffix})
113 endfunction(distutils_add_cython_module)
114
115 function(distutils_install_cython_module name)
116 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
117 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
118 set(PY_CC "${compiler_launcher} ${CMAKE_C_COMPILER}")
119 set(PY_LDSHARED "${link_launcher} ${CMAKE_C_COMPILER} -shared")
120 cmake_parse_arguments(DU "DISABLE_VTA" "" "" ${ARGN})
121 if(DU_DISABLE_VTA AND HAS_VTA)
122 set(CFLAG_DISABLE_VTA -fno-var-tracking-assignments)
123 endif()
124 if(DEFINED ENV{VERBOSE})
125 set(maybe_verbose --verbose)
126 endif()
127 install(CODE "
128 set(ENV{CC} \"${PY_CC}\")
129 set(ENV{LDSHARED} \"${PY_LDSHARED}\")
130 set(ENV{CPPFLAGS} \"-iquote${CMAKE_SOURCE_DIR}/src/include
131 -D'void0=dead_function\(void\)' \
132 -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0' \
133 ${CFLAG_DISABLE_VTA}\")
134 set(ENV{LDFLAGS} \"-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
135 set(ENV{CYTHON_BUILD_DIR} \"${CMAKE_CURRENT_BINARY_DIR}\")
136 set(ENV{CEPH_LIBDIR} \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
137
138 set(options --prefix=${CMAKE_INSTALL_PREFIX})
139 if(DEFINED ENV{DESTDIR})
140 if(EXISTS /etc/debian_version)
141 list(APPEND options --install-layout=deb)
142 endif()
143 list(APPEND options --root=\$ENV{DESTDIR})
144 else()
145 list(APPEND options --root=/)
146 endif()
147 execute_process(
148 COMMAND
149 ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py
150 build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR}
151 --build-platlib ${CYTHON_MODULE_DIR}/lib.3
152 build_ext --cython-c-in-temp --build-temp ${CMAKE_CURRENT_BINARY_DIR} --cython-include-dirs ${PROJECT_SOURCE_DIR}/src/pybind/rados
153 install \${options} --single-version-externally-managed --record /dev/null
154 egg_info --egg-base ${CMAKE_CURRENT_BINARY_DIR}
155 ${maybe_verbose}
156 WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\"
157 RESULT_VARIABLE install_res)
158 if(NOT \"\${install_res}\" STREQUAL 0)
159 message(FATAL_ERROR \"Failed to build and install ${name} python module\")
160 endif()
161 ")
162 endfunction(distutils_install_cython_module)