]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | include(CMakeParseArguments) |
2 | ||
33c7a0ef TL |
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 | ||
7c673cae | 14 | function(distutils_install_module name) |
d2e6a577 | 15 | set(py_srcs setup.py README.rst requirements.txt test-requirements.txt bin ${name}) |
7c673cae | 16 | foreach(src ${py_srcs}) |
d2e6a577 FG |
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() | |
7c673cae | 24 | endforeach() |
9f95a23c TL |
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}) | |
7c673cae FG |
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() | |
11fdf7f2 TL |
36 | list(APPEND options |
37 | --root=\$ENV{DESTDIR} | |
38 | --single-version-externally-managed) | |
f67539c2 TL |
39 | endif() |
40 | if(NOT \"${DU_INSTALL_SCRIPT}\" STREQUAL \"\") | |
41 | list(APPEND options --install-script=${DU_INSTALL_SCRIPT}) | |
7c673cae FG |
42 | endif() |
43 | execute_process( | |
9f95a23c | 44 | COMMAND ${Python3_EXECUTABLE} |
7c673cae FG |
45 | setup.py install \${options} |
46 | WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")") | |
47 | endfunction(distutils_install_module) | |
48 | ||
9f95a23c | 49 | function(distutils_add_cython_module target name src) |
7c673cae FG |
50 | get_property(compiler_launcher GLOBAL PROPERTY RULE_LAUNCH_COMPILE) |
51 | get_property(link_launcher GLOBAL PROPERTY RULE_LAUNCH_LINK) | |
11fdf7f2 TL |
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? | |
20effc67 TL |
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) | |
11fdf7f2 TL |
69 | # This little bit of magic wipes out __Pyx_check_single_interpreter() |
70 | # Note: this is reproduced in distutils_install_cython_module | |
20effc67 TL |
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}) | |
11fdf7f2 TL |
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") | |
9f95a23c TL |
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) | |
20effc67 TL |
89 | if(DEFINED ENV{VERBOSE}) |
90 | set(maybe_verbose --verbose) | |
91 | endif() | |
9f95a23c TL |
92 | add_custom_command( |
93 | OUTPUT ${output_dir}/${name}${ext_suffix} | |
7c673cae FG |
94 | COMMAND |
95 | env | |
11fdf7f2 | 96 | CC="${PY_CC}" |
20effc67 TL |
97 | CFLAGS="${PY_CFLAGS}" |
98 | CPPFLAGS="${PY_CPPFLAGS}" | |
11fdf7f2 TL |
99 | CXX="${PY_CXX}" |
100 | LDSHARED="${PY_LDSHARED}" | |
7c673cae FG |
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} | |
9f95a23c | 105 | ${Python3_EXECUTABLE} ${setup_py} |
20effc67 | 106 | build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR} |
9f95a23c TL |
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}) | |
7c673cae FG |
113 | endfunction(distutils_add_cython_module) |
114 | ||
115 | function(distutils_install_cython_module name) | |
11fdf7f2 TL |
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") | |
20effc67 TL |
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() | |
7c673cae | 127 | install(CODE " |
11fdf7f2 TL |
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\)' \ | |
20effc67 TL |
132 | -D'__Pyx_check_single_interpreter\(ARG\)=ARG \#\# 0' \ |
133 | ${CFLAG_DISABLE_VTA}\") | |
11fdf7f2 TL |
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 | ||
7c673cae FG |
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( | |
11fdf7f2 | 148 | COMMAND |
9f95a23c | 149 | ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/setup.py |
20effc67 | 150 | build ${maybe_verbose} --build-base ${CYTHON_MODULE_DIR} |
9f95a23c | 151 | --build-platlib ${CYTHON_MODULE_DIR}/lib.3 |
7c673cae FG |
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} | |
20effc67 | 155 | ${maybe_verbose} |
7c673cae FG |
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) |