]> git.proxmox.com Git - ceph.git/blob - ceph/cmake/modules/Distutils.cmake
import 15.2.0 Octopus source
[ceph.git] / ceph / cmake / modules / Distutils.cmake
1 include(CMakeParseArguments)
2
3 function(distutils_install_module name)
4 set(py_srcs setup.py README.rst requirements.txt test-requirements.txt bin ${name})
5 foreach(src ${py_srcs})
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()
13 endforeach()
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})
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()
25 list(APPEND options
26 --root=\$ENV{DESTDIR}
27 --single-version-externally-managed)
28 if(NOT \"${DU_INSTALL_SCRIPT}\" STREQUAL \"\")
29 list(APPEND options --install-script=${DU_INSTALL_SCRIPT})
30 endif()
31 endif()
32 execute_process(
33 COMMAND ${Python3_EXECUTABLE}
34 setup.py install \${options}
35 WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")")
36 endfunction(distutils_install_module)
37
38 function(distutils_add_cython_module target name src)
39 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
40 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
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")
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}
76 COMMAND
77 env
78 CC="${PY_CC}"
79 CXX="${PY_CXX}"
80 LDSHARED="${PY_LDSHARED}"
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}
85 ${Python3_EXECUTABLE} ${setup_py}
86 build --verbose --build-base ${CYTHON_MODULE_DIR}
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})
93 endfunction(distutils_add_cython_module)
94
95 function(distutils_install_cython_module name)
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")
100 install(CODE "
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
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(
120 COMMAND
121 ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py
122 build --verbose --build-base ${CYTHON_MODULE_DIR}
123 --build-platlib ${CYTHON_MODULE_DIR}/lib.3
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 ")
134 endfunction(distutils_install_cython_module)