File: UsePythonExtensions.cmake

package info (click to toggle)
zfp 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,744 kB
  • sloc: cpp: 20,656; ansic: 18,871; pascal: 1,231; f90: 907; python: 255; makefile: 183; sh: 79; fortran: 70
file content (320 lines) | stat: -rw-r--r-- 10,349 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#.rst:
#
# The following functions are defined:
#
# .. cmake:command:: add_python_library
#
# Add a library that contains a mix of C, C++, Fortran, Cython, F2PY, Template,
# and Tempita sources. The required targets are automatically generated to
# "lower" source files from their high-level representation to a file that the
# compiler can accept.
#
#
#   add_python_library(<Name>
#                      SOURCES [source1 [source2 ...]]
#                      [INCLUDE_DIRECTORIES [dir1 [dir2 ...]]
#                      [LINK_LIBRARIES [lib1 [lib2 ...]]
#                      [DEPENDS [source1 [source2 ...]]])
#
#
# Example usage
# ^^^^^^^^^^^^^
#
# .. code-block:: cmake
#
#   find_package(PythonExtensions)
#
#   file(GLOB arpack_sources ARPACK/SRC/*.f ARPACK/UTIL/*.f)
#
#    add_python_library(arpack_scipy
#      SOURCES ${arpack_sources}
#              ${g77_wrapper_sources}
#      INCLUDE_DIRECTORIES ARPACK/SRC
#    )
#
# .. cmake:command:: add_python_extension
#
# Add a extension that contains a mix of C, C++, Fortran, Cython, F2PY, Template,
# and Tempita sources. The required targets are automatically generated to
# "lower" source files from their high-level representation to a file that the
# compiler can accept.
#
#
#   add_python_extension(<Name>
#                        SOURCES [source1 [source2 ...]]
#                        [INCLUDE_DIRECTORIES [dir1 [dir2 ...]]
#                        [LINK_LIBRARIES [lib1 [lib2 ...]]
#                        [DEPENDS [source1 [source2 ...]]])
#
#
# Example usage
# ^^^^^^^^^^^^^
#
# .. code-block:: cmake
#
#   find_package(PythonExtensions)
#
#   file(GLOB arpack_sources ARPACK/SRC/*.f ARPACK/UTIL/*.f)
#
#    add_python_extension(arpack_scipy
#      SOURCES ${arpack_sources}
#              ${g77_wrapper_sources}
#      INCLUDE_DIRECTORIES ARPACK/SRC
#    )
#
#
#=============================================================================
# Copyright 2011 Kitware, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#=============================================================================

macro(_remove_whitespace _output)
  string(REGEX REPLACE "[ \r\n\t]+" " " ${_output} "${${_output}}")
  string(STRIP "${${_output}}" ${_output})
endmacro()

function(add_python_library _name)
  set(options STATIC SHARED MODULE)
  set(multiValueArgs SOURCES INCLUDE_DIRECTORIES LINK_LIBRARIES COMPILE_DEFINITIONS DEPENDS)
  cmake_parse_arguments(_args "${options}" "" "${multiValueArgs}" ${ARGN} )

  # Validate arguments to allow simpler debugging
  if(NOT _args_SOURCES)
    message(
      FATAL_ERROR
      "You have called add_python_library for library ${_name} without "
      "any source files. This typically indicates a problem with "
      "your CMakeLists.txt file"
    )
  endif()

  # Initialize the list of sources
  set(_sources ${_args_SOURCES})

  # Generate targets for all *.src files
  set(_processed )
  foreach(_source IN LISTS _sources)
    if(${_source} MATCHES ".pyf.src$" OR ${_source} MATCHES "\\.f\\.src$")
      if(NOT NumPy_FOUND)
        message(
          FATAL_ERROR
          "NumPy is required to process *.src Template files"
        )
      endif()
      string(REGEX REPLACE "\\.[^.]*$" "" _source_we ${_source})
      add_custom_command(
        OUTPUT ${_source_we}
        COMMAND ${NumPy_FROM_TEMPLATE_EXECUTABLE}
                ${CMAKE_CURRENT_SOURCE_DIR}/${_source}
                ${CMAKE_CURRENT_BINARY_DIR}/${_source_we}
        DEPENDS ${_source} ${_args_DEPENDS}
        COMMENT "Generating ${_source_we} from template ${_source}"
      )
      list(APPEND _processed ${_source_we})
    elseif(${_source} MATCHES "\\.c\\.src$")
      if(NOT NumPy_FOUND)
        message(
          FATAL_ERROR
          "NumPy is required to process *.src Template files"
        )
      endif()
      string(REGEX REPLACE "\\.[^.]*$" "" _source_we ${_source})
      add_custom_command(
        OUTPUT ${_source_we}
        COMMAND ${NumPy_CONV_TEMPLATE_EXECUTABLE}
                ${CMAKE_CURRENT_SOURCE_DIR}/${_source}
                ${CMAKE_CURRENT_BINARY_DIR}/${_source_we}
        DEPENDS ${_source} ${_args_DEPENDS}
        COMMENT "Generating ${_source_we} from template ${_source}"
      )
      list(APPEND _processed ${_source_we})
    elseif(${_source} MATCHES "\\.pyx\\.in$")
      if(NOT Cython_FOUND)
        message(
          FATAL_ERROR
          "Cython is required to process *.in Tempita files"
        )
      endif()
      string(REGEX REPLACE "\\.[^.]*$" "" _source_we ${_source})
      configure_file(
          ${CMAKE_CURRENT_SOURCE_DIR}/${_source}
          ${CMAKE_CURRENT_BINARY_DIR}/${_source}
          COPYONLY
      )
      set(_tempita_command
          "
            import os;
            import sys;
            from Cython.Tempita import Template;
            cwd = os.getcwd();
            open(os.path.join(cwd, '${_source_we}'), 'w+')
            .write(
                Template.from_filename(os.path.join(cwd, '${_source}'),
                encoding=sys.getdefaultencoding()).substitute()
            )
          "
      )
      _remove_whitespace(_tempita_command)
      add_custom_command(
        OUTPUT ${_source_we}
        COMMAND ${PYTHON_EXECUTABLE} -c "${_tempita_command}"
        DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_source}"
                ${_args_DEPENDS}
      )
      list(APPEND _processed ${_source_we})
    else()
      list(APPEND _processed  ${_source})
    endif()
  endforeach()
  set(_sources ${_processed})

  # If we're building a Python extension and we're given only Fortran sources,
  # We can conclude that we need to generate a Fortran interface file
  list(FILTER _processed EXCLUDE REGEX "(\\.f|\\.f90)$")
  if(NOT _processed AND _args_MODULE)
    if(NOT NumPy_FOUND)
        message(
          FATAL_ERROR
          "NumPy is required to process *.pyf F2PY files"
        )
    endif()
    set(_sources_abs )
    foreach(_source IN LISTS _sources)
      if(NOT IS_ABSOLUTE ${_source})
        set(_source ${CMAKE_CURRENT_SOURCE_DIR}/${_source})
      endif()
      list(APPEND _sources_abs ${_source})
    endforeach()
    add_custom_command(
        OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_name}.pyf
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        COMMAND ${F2PY_EXECUTABLE}
        ARGS -h ${_name}.pyf -m ${_name} --overwrite-signature
             ${_sources_abs}
        DEPENDS ${_sources} ${_args_DEPENDS}
        COMMENT "Generating ${_name} Fortran interface file"
    )
    list(APPEND _sources ${_name}.pyf)
  endif()

  # Are there F2PY targets?
  set(_has_f2py_targets OFF)
  set(_has_cython_targets OFF)

  # Generate targets for all *.pyx and *.pyf files
  set(_processed )
  foreach(_source IN LISTS _sources)
    if(${_source} MATCHES \\.pyx$)
      if(NOT Cython_FOUND)
        message(
          FATAL_ERROR
          "Cython is required to process *.pyx Cython files"
        )
      endif()
      string(REGEX REPLACE "\\.[^.]*$" "" _pyx_target_name ${_source})
      set(_has_cython_targets ON)
      add_cython_target(${_pyx_target_name}
          ${_source}
          OUTPUT_VAR _pyx_target_output
          DEPENDS ${_args_DEPENDS}
      )
      list(APPEND _processed ${_pyx_target_output})
    elseif(${_source} MATCHES \\.pyf$)
      if(NOT NumPy_FOUND)
          message(
            FATAL_ERROR
            "NumPy is required to process *.pyf F2PY files"
          )
      endif()
      string(REGEX REPLACE "\\.[^.]*$" "" _pyf_target_name ${_source})
      set(_has_f2py_targets ON)
      add_f2py_target(${_pyf_target_name}
          ${_source}
          OUTPUT_VAR _pyf_target_output
          DEPENDS ${_args_DEPENDS}
      )
      list(APPEND _processed  ${_pyf_target_output})
    else()
      list(APPEND _processed ${_source})
    endif()
  endforeach()
  set(_sources ${_processed})

  if(_args_SHARED)
    add_library(${_name} SHARED ${_sources})
  elseif(_args_MODULE)
    add_library(${_name} MODULE ${_sources})
  else()
    # Assume static
    add_library(${_name} STATIC ${_sources})
  endif()

  target_include_directories(${_name} PRIVATE ${_args_INCLUDE_DIRECTORIES})
  target_link_libraries(${_name} ${SKBUILD_LINK_LIBRARIES_KEYWORD} ${_args_LINK_LIBRARIES})

  if(_has_f2py_targets)
    target_include_directories(${_name} PRIVATE ${F2PY_INCLUDE_DIRS})
    target_link_libraries(${_name} ${SKBUILD_LINK_LIBRARIES_KEYWORD} ${F2PY_LIBRARIES})
  endif()

  if(_args_COMPILE_DEFINITIONS)
    target_compile_definitions(${_name} PRIVATE ${_args_COMPILE_DEFINITIONS})
  endif()

  if(_args_DEPENDS)
    add_custom_target(
      "${_name}_depends"
      DEPENDS ${_args_DEPENDS}
    )
    add_dependencies(${_name} "${_name}_depends")
  endif()
endfunction()

function(add_python_extension _name)
  # FIXME: make sure that extensions with the same name can happen
  # in multiple directories

  set(multiValueArgs SOURCES INCLUDE_DIRECTORIES LINK_LIBRARIES COMPILE_DEFINITIONS DEPENDS)
  cmake_parse_arguments(_args "" "" "${multiValueArgs}" ${ARGN} )

  # Validate arguments to allow simpler debugging
  if(NOT _args_SOURCES)
    message(
      FATAL_ERROR
      "You have called add_python_extension for library ${_name} without "
      "any source files. This typically indicates a problem with "
      "your CMakeLists.txt file"
    )
  endif()

  add_python_library(${_name} MODULE
    SOURCES ${_args_SOURCES}
    INCLUDE_DIRECTORIES ${_args_INCLUDE_DIRECTORIES}
    LINK_LIBRARIES ${_args_LINK_LIBRARIES}
    COMPILE_DEFINITIONS ${_args_COMPILE_DEFINITIONS}
    DEPENDS ${_args_DEPENDS}
  )
  python_extension_module(${_name})

  file(RELATIVE_PATH _relative "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
  if(_relative STREQUAL "")
    set(_relative ".")
  endif()

  install(
    TARGETS ${_name}
    LIBRARY DESTINATION "${_relative}"
    RUNTIME DESTINATION "${_relative}"
  )
endfunction()