]> git.proxmox.com Git - ceph.git/blob - ceph/cmake/modules/Distutils.cmake
import quincy beta 17.1.0
[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 endif()
29 if(NOT \"${DU_INSTALL_SCRIPT}\" STREQUAL \"\")
30 list(APPEND options --install-script=${DU_INSTALL_SCRIPT})
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 set(PY_CFLAGS ${COMPILE_OPTIONS})
53 cmake_parse_arguments(DU "DISABLE_VTA" "" "" ${ARGN})
54 if(DU_DISABLE_VTA AND HAS_VTA)
55 list(APPEND PY_CFLAGS -fno-var-tracking-assignments)
56 endif()
57 list(APPEND PY_CPPFLAGS -iquote${CMAKE_SOURCE_DIR}/src/include -w)
58 # This little bit of magic wipes out __Pyx_check_single_interpreter()
59 # Note: this is reproduced in distutils_install_cython_module
60 list(APPEND PY_CPPFLAGS -D'void0=dead_function\(void\)')
61 list(APPEND PY_CPPFLAGS -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0')
62 set(PY_CC ${compiler_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1})
63 set(PY_CXX ${compiler_launcher} ${CMAKE_CXX_COMPILER} ${cxx_compiler_arg1})
64 set(PY_LDSHARED ${link_launcher} ${CMAKE_C_COMPILER} ${c_compiler_arg1} "-shared")
65
66 set(suffix_var "EXT_SUFFIX")
67 execute_process(COMMAND "${Python3_EXECUTABLE}" -c
68 "from distutils import sysconfig; print(sysconfig.get_config_var('${suffix_var}'))"
69 RESULT_VARIABLE result
70 OUTPUT_VARIABLE ext_suffix
71 ERROR_VARIABLE error
72 OUTPUT_STRIP_TRAILING_WHITESPACE)
73 if(NOT result EQUAL 0)
74 message(FATAL_ERROR "Unable to tell python extension's suffix: ${error}")
75 endif()
76 set(output_dir "${CYTHON_MODULE_DIR}/lib.3")
77 set(setup_py ${CMAKE_CURRENT_SOURCE_DIR}/setup.py)
78 if(DEFINED ENV{VERBOSE})
79 set(maybe_verbose --verbose)
80 endif()
81 add_custom_command(
82 OUTPUT ${output_dir}/${name}${ext_suffix}
83 COMMAND
84 env
85 CC="${PY_CC}"
86 CFLAGS="${PY_CFLAGS}"
87 CPPFLAGS="${PY_CPPFLAGS}"
88 CXX="${PY_CXX}"
89 LDSHARED="${PY_LDSHARED}"
90 OPT=\"-DNDEBUG -g -fwrapv -O2 -w\"
91 LDFLAGS=-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
92 CYTHON_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
93 CEPH_LIBDIR=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
94 ${Python3_EXECUTABLE} ${setup_py}
95 build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR}
96 --build-platlib ${output_dir}
97 MAIN_DEPENDENCY ${src}
98 DEPENDS ${setup_py}
99 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
100 add_custom_target(${target} ALL
101 DEPENDS ${output_dir}/${name}${ext_suffix})
102 endfunction(distutils_add_cython_module)
103
104 function(distutils_install_cython_module name)
105 get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
106 get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK)
107 set(PY_CC "${compiler_launcher} ${CMAKE_C_COMPILER}")
108 set(PY_LDSHARED "${link_launcher} ${CMAKE_C_COMPILER} -shared")
109 cmake_parse_arguments(DU "DISABLE_VTA" "" "" ${ARGN})
110 if(DU_DISABLE_VTA AND HAS_VTA)
111 set(CFLAG_DISABLE_VTA -fno-var-tracking-assignments)
112 endif()
113 if(DEFINED ENV{VERBOSE})
114 set(maybe_verbose --verbose)
115 endif()
116 install(CODE "
117 set(ENV{CC} \"${PY_CC}\")
118 set(ENV{LDSHARED} \"${PY_LDSHARED}\")
119 set(ENV{CPPFLAGS} \"-iquote${CMAKE_SOURCE_DIR}/src/include
120 -D'void0=dead_function\(void\)' \
121 -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0' \
122 ${CFLAG_DISABLE_VTA}\")
123 set(ENV{LDFLAGS} \"-L${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
124 set(ENV{CYTHON_BUILD_DIR} \"${CMAKE_CURRENT_BINARY_DIR}\")
125 set(ENV{CEPH_LIBDIR} \"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}\")
126
127 set(options --prefix=${CMAKE_INSTALL_PREFIX})
128 if(DEFINED ENV{DESTDIR})
129 if(EXISTS /etc/debian_version)
130 list(APPEND options --install-layout=deb)
131 endif()
132 list(APPEND options --root=\$ENV{DESTDIR})
133 else()
134 list(APPEND options --root=/)
135 endif()
136 execute_process(
137 COMMAND
138 ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py
139 build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR}
140 --build-platlib ${CYTHON_MODULE_DIR}/lib.3
141 build_ext --cython-c-in-temp --build-temp ${CMAKE_CURRENT_BINARY_DIR} --cython-include-dirs ${PROJECT_SOURCE_DIR}/src/pybind/rados
142 install \${options} --single-version-externally-managed --record /dev/null
143 egg_info --egg-base ${CMAKE_CURRENT_BINARY_DIR}
144 ${maybe_verbose}
145 WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\"
146 RESULT_VARIABLE install_res)
147 if(NOT \"\${install_res}\" STREQUAL 0)
148 message(FATAL_ERROR \"Failed to build and install ${name} python module\")
149 endif()
150 ")
151 endfunction(distutils_install_cython_module)