• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright ©
2
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9
10# The above copyright notice and this permission notice shall be included in
11# all copies or substantial portions of the Software.
12
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
20
21rust = import('rust')
22
23libmesa_rust_util_files = files(
24  'util/lib.rs',
25  'util/assert.rs',
26  'util/bitset.rs',
27  'util/feature.rs',
28  'util/properties.rs',
29  'util/ptr.rs',
30  'util/string.rs',
31)
32
33libmesa_rust_files = files(
34  'mesa/lib.rs',
35  'mesa/compiler.rs',
36  'mesa/compiler/clc.rs',
37  'mesa/compiler/clc/spirv.rs',
38  'mesa/compiler/nir.rs',
39  'mesa/pipe.rs',
40  'mesa/pipe/context.rs',
41  'mesa/pipe/device.rs',
42  'mesa/pipe/fence.rs',
43  'mesa/pipe/screen.rs',
44  'mesa/pipe/transfer.rs',
45)
46
47rusticl_proc_macros_files = files(
48  'proc/lib.rs',
49)
50
51rusticl_files = files(
52  'lib.rs',
53  'api.rs',
54  'api/context.rs',
55  'api/device.rs',
56  'api/event.rs',
57  'api/icd.rs',
58  'api/kernel.rs',
59  'api/memory.rs',
60  'api/platform.rs',
61  'api/program.rs',
62  'api/queue.rs',
63  'api/types.rs',
64  'api/util.rs',
65  'core.rs',
66  'core/context.rs',
67  'core/device.rs',
68  'core/format.rs',
69  'core/kernel.rs',
70  'core/memory.rs',
71  'core/platform.rs',
72  'core/program.rs',
73  'core/queue.rs',
74  'core/util.rs',
75  'core/version.rs',
76  'core/gl.rs',
77)
78
79rusticl_args = [
80  # we want unsafe blocks inside unsafe functions
81  '-Dunsafe_op_in_unsafe_fn',
82  # we error on all clippy warnings unless they are disabled
83  '-Dclippy::all',
84  # we want to add asserts in control flow
85  '-Aclippy::assertions_on_constants',
86  # warns on Arc<_> as keys
87  '-Aclippy::mutable_key_type',
88  '-Aclippy::not_unsafe_ptr_arg_deref',
89  # dunno, kind of looks nicier being explicit
90  '-Aclippy::redundant_field_names',
91  '-Aclippy::too_many_arguments',
92  '-Aclippy::type_complexity',
93]
94
95if with_platform_x11
96  rusticl_args += [
97    '--cfg', 'glx',
98  ]
99endif
100
101rusticl_gen_args = [
102  # can't do anything about it anyway
103  '-Aclippy::all',
104  '-Aimproper_ctypes',
105  # Some bindgen versions assume `unsafe_op_in_unsafe_fn`
106  '-Aunused_unsafe',
107  '-Anon_camel_case_types',
108  '-Anon_snake_case',
109  '-Anon_upper_case_globals',
110]
111
112rusticl_bindgen_args = [
113  '--no-convert-floats',
114  '--use-array-pointers-in-arguments',
115  '--default-enum-style', 'rust',
116  '--with-derive-partialeq',
117  '--with-derive-eq',
118  '--with-derive-partialord',
119  '--with-derive-ord',
120  '--with-derive-hash',
121  '--with-derive-default',
122  '--anon-fields-prefix', 'anon_',
123]
124
125bindgen_version = find_program('bindgen').version()
126
127if bindgen_version == 'unknown'
128  error('Failed to detect bindgen version. If you are using bindgen 0.69.0, please either update to 0.69.1 or downgrade to 0.68.1. You can install the latest version for your user with `cargo install bindgen-cli`.')
129endif
130
131if bindgen_version.version_compare('< 0.62')
132  error('rusticl requires bindgen 0.62 or newer. If your distribution does not ship a recent enough version, you can install the latest version for your user with `cargo install bindgen-cli`.')
133endif
134
135if bindgen_version.version_compare('< 0.65')
136  rusticl_bindgen_args += [
137    '--size_t-is-usize',
138  ]
139endif
140
141rusticl_bindgen_c_args = [
142  '-fno-builtin-malloc',
143]
144
145cl_c_args = [
146  '-DCL_USE_DEPRECATED_OPENCL_1_0_APIS',
147  '-DCL_USE_DEPRECATED_OPENCL_1_1_APIS',
148  '-DCL_USE_DEPRECATED_OPENCL_1_2_APIS',
149  '-DCL_USE_DEPRECATED_OPENCL_2_0_APIS',
150  '-DCL_USE_DEPRECATED_OPENCL_2_1_APIS',
151  '-DCL_USE_DEPRECATED_OPENCL_2_2_APIS',
152  '-DCL_TARGET_OPENCL_VERSION=300',
153]
154
155rusticl_opencl_bindings_rs = rust.bindgen(
156  input : [
157    'rusticl_opencl_bindings.h',
158    opencl_headers,
159  ],
160  output : 'rusticl_opencl_bindings.rs',
161  include_directories : [
162    inc_include,
163  ],
164  dependencies : [
165    dep_x11,
166  ],
167  c_args : [
168    rusticl_bindgen_c_args,
169    pre_args,
170    cl_c_args,
171  ],
172  args : [
173    rusticl_bindgen_args,
174    '--disable-header-comment',
175    '--ignore-functions',
176    # needed because bindgen adds *mut void fields...
177    '--raw-line', 'unsafe impl std::marker::Sync for _cl_icd_dispatch {}',
178    # _cl_image_desc contains a pointer to _cl_mem
179    '--raw-line', 'unsafe impl std::marker::Send for _cl_image_desc {}',
180    '--raw-line', 'unsafe impl std::marker::Sync for _cl_image_desc {}',
181    '--allowlist-type', 'cl_.*',
182    '--allowlist-var', 'CL_.*',
183    # needed for gl_sharing extension
184    '--allowlist-var', 'GL_.*',
185    '--allowlist-var', 'MESA_GLINTEROP_.*',
186    '--allowlist-type', 'PFNEGLGETPROCADDRESSPROC',
187    '--allowlist-type', 'PFNGLXGETPROCADDRESSPROC',
188    '--allowlist-type', 'PFNMESAGLINTEROP.*',
189    # some info types need to be strongly typed so we can implement various get_infos
190    '--new-type-alias-deref', 'cl_(mem|image|pipe|gl_texture)_info',
191    '--new-type-alias-deref', 'cl_kernel_(arg|work_group)_info',
192    '--new-type-alias-deref', 'cl_(event|profiling)_info',
193    # turn gl interop enums into constfields so we can compare with rust types
194    '--constified-enum', 'MESA_GLINTEROP_.*',
195  ],
196)
197
198rusticl_opencl_gen = static_library(
199  'rusticl_opencl_gen',
200  rusticl_opencl_bindings_rs,
201  gnu_symbol_visibility : 'hidden',
202  rust_abi : 'rust',
203  rust_args : [
204    rusticl_gen_args,
205  ],
206)
207
208rusticl_llvm_bindings_rs = rust.bindgen(
209  input : 'rusticl_llvm_bindings.hpp',
210  output : 'rusticl_llvm_bindings.rs',
211  c_args : [
212    rusticl_bindgen_c_args,
213    pre_args,
214  ],
215  dependencies : [
216    dep_clang,
217    dep_llvm,
218    dep_llvmspirvlib,
219  ],
220  args : [
221    # we want to limit what to generate bindings for
222    '--generate', 'constructors,functions,types',
223    # and all types will be opaque
224    '--opaque-type', '.*',
225    # LLVM/Clang/Translator stuff, only used for build-id
226    # also only use functions from very basic header files, otherwise bindgen might crash :')
227    '--allowlist-function', 'clang::getClangFullVersion',
228    '--allowlist-function', 'llvm::LLVMContext::LLVMContext',
229    '--allowlist-function', 'llvm::writeSpirv',
230  ],
231)
232
233rusticl_llvm_gen = static_library(
234  'rusticl_llvm_gen',
235  rusticl_llvm_bindings_rs,
236  gnu_symbol_visibility : 'hidden',
237  rust_abi : 'rust',
238  rust_args : [
239    rusticl_gen_args,
240  ],
241)
242
243rusticl_system_bindings_wrapper = static_library(
244  'system_bindings',
245  [
246    'rusticl_system_bindings.c',
247    'rusticl_system_bindings.h',
248  ],
249  gnu_symbol_visibility : 'hidden',
250  include_directories : [
251    inc_include,
252  ],
253  c_args : [
254    pre_args,
255  ],
256)
257
258rusticl_mesa_bindings_inline_wrapper = static_library(
259  'mesa_bindings_inline_wrapper',
260  [
261    'rusticl_mesa_inline_bindings_wrapper.c',
262    'rusticl_mesa_inline_bindings_wrapper.h',
263    'rusticl_nir.c',
264    'rusticl_nir.h',
265    sha1_h,
266  ],
267  gnu_symbol_visibility : 'hidden',
268  include_directories : [
269    inc_gallium,
270    inc_gallium_aux,
271    inc_include,
272    inc_nir,
273    inc_src,
274  ],
275  c_args : [
276    pre_args,
277    cl_c_args,
278  ],
279  dependencies: [
280    idep_nir_headers,
281    dep_valgrind,
282  ],
283)
284
285rusticl_libc_bindings_rs = rust.bindgen(
286  input : 'rusticl_libc_bindings.h',
287  output : 'rusticl_libc_bindings.rs',
288  dependencies: [
289    dep_valgrind,
290  ],
291  c_args : [
292    rusticl_bindgen_c_args,
293    pre_args,
294  ],
295  args : [
296    rusticl_bindgen_args,
297    '--allowlist-function',     'close',
298    '--allowlist-function',     'dlsym',
299    '--allowlist-function',     'free',
300    '--allowlist-function',     'malloc',
301  ]
302)
303
304rusticl_mesa_bindings_rs = rust.bindgen(
305  input : 'rusticl_mesa_bindings.h',
306  output : 'rusticl_mesa_bindings.rs',
307  include_directories : [
308    inc_gallium,
309    inc_gallium_aux,
310    inc_include,
311    inc_src,
312  ],
313  dependencies: [
314    idep_nir_headers,
315    dep_valgrind,
316  ],
317  c_args : [
318    rusticl_bindgen_c_args,
319    pre_args,
320  ],
321  args : [
322    rusticl_bindgen_args,
323    # mesa utils
324    '--allowlist-function',     'blob_.*',
325    '--allowlist-function',     'disk_cache_.*',
326    '--allowlist-type',         'float_controls',
327    '--allowlist-function',     'mesa_.*',
328    '--allowlist-var',          'OS_.*',
329    '--allowlist-function',     'rz?alloc_.*',
330    '--allowlist-function',     'SHA1.*',
331    '--allowlist-var',          'SHA1_.*',
332    '--allowlist-function',     'u_.*',
333    '--allowlist-function',     'util_format_.*',
334
335    # CL API
336    '--allowlist-type',         'cl_sampler_.*_mode',
337    '--constified-enum-module', 'cl_sampler_.*_mode',
338
339    # clc
340    '--allowlist-function',     'clc_.*',
341    '--allowlist-type',         'clc_kernel_arg_access_qualifier',
342    '--bitfield-enum',          'clc_kernel_arg_access_qualifier',
343    '--allowlist-type',         'clc_kernel_arg_type_qualifier',
344    '--bitfield-enum',          'clc_kernel_arg_type_qualifier',
345
346    # gl
347    '--allowlist-type',         'gl_access_qualifier',
348    '--bitfield-enum',          'gl_access_qualifier',
349    '--allowlist-function',     'glsl_.*',
350
351    # nir and spirv
352    '--allowlist-function',     'nir_.*',
353    '--allowlist-var',          'nir_debug',
354    '--allowlist-var',          'NIR_DEBUG_.*',
355    '--bitfield-enum',          'nir_lower_int64_options',
356    '--bitfield-enum',          'nir_opt_if_options',
357    '--bitfield-enum',          'nir_variable_mode',
358    '--allowlist-function',     'should_.*_nir',
359    '--allowlist-function',     'spirv_.*',
360
361    # gallium
362    '--allowlist-function',     'pipe_.*',
363    '--allowlist-var',          'PIPE_.*',
364    '--allowlist-type',         'pipe_endian',
365    '--bitfield-enum',          'pipe_map_flags',
366    '--allowlist-type',         'pipe_query_type',
367    '--constified-enum-module', 'pipe_query_type',
368    '--allowlist-type',         'pipe_resource_usage',
369    '--bitfield-enum',          'pipe_resource_usage',
370    '--allowlist-type',         'pipe_tex_filter',
371    '--constified-enum-module', 'pipe_tex_filter',
372    '--allowlist-type',         'pipe_tex_wrap',
373    '--constified-enum-module', 'pipe_tex_wrap',
374
375    # rusticl C functions
376    '--allowlist-function',     'rusticl_.*',
377    '--allowlist-function',     'std(err|out)_ptr',
378
379    # winsys
380    '--allowlist-var',          'WINSYS_HANDLE_TYPE_.*',
381  ],
382)
383
384idep_rusticl_gen = declare_dependency(
385  sources: [
386    rusticl_opencl_bindings_rs,
387  ],
388)
389
390libmesa_rust_gen = static_library(
391  'mesa_rust_gen',
392  rusticl_mesa_bindings_rs,
393  gnu_symbol_visibility : 'hidden',
394  link_with: [
395    libgallium,
396  ],
397  dependencies: [
398    idep_mesaclc,
399  ],
400  rust_abi : 'rust',
401  rust_args : [
402    rusticl_gen_args,
403  ],
404)
405
406libc_rust_gen = static_library(
407  'libc_rust_gen',
408  rusticl_libc_bindings_rs,
409  gnu_symbol_visibility : 'hidden',
410  rust_abi : 'rust',
411  rust_args : [
412    rusticl_gen_args,
413  ],
414)
415
416libmesa_rust_util = static_library(
417  'mesa_rust_util',
418  [libmesa_rust_util_files],
419  gnu_symbol_visibility : 'hidden',
420  rust_abi : 'rust',
421  rust_args : [
422    rusticl_args,
423  ],
424)
425
426libmesa_rust = static_library(
427  'mesa_rust',
428  [libmesa_rust_files],
429  gnu_symbol_visibility : 'hidden',
430  rust_abi : 'rust',
431  rust_args : [
432    rusticl_args,
433  ],
434  link_with : [
435    libc_rust_gen,
436    libmesa_rust_gen,
437    libmesa_rust_util,
438    rusticl_mesa_bindings_inline_wrapper,
439    rusticl_system_bindings_wrapper,
440  ]
441)
442
443rusticl_proc_macros = rust.proc_macro(
444  'rusticl_proc_macros',
445  [rusticl_proc_macros_files],
446  rust_args : [
447    rusticl_args,
448  ],
449)
450
451librusticl = static_library(
452  'rusticl',
453  [rusticl_files],
454  gnu_symbol_visibility : 'hidden',
455  rust_abi : 'c',
456  rust_args : [
457    rusticl_args,
458  ],
459  link_with : [
460    libc_rust_gen,
461    libmesa_rust,
462    libmesa_rust_gen,
463    libmesa_rust_util,
464    rusticl_llvm_gen,
465    rusticl_opencl_gen,
466    rusticl_proc_macros,
467  ],
468  dependencies : [
469    idep_rusticl_gen,
470  ],
471)
472