meson: Subprjects can define both "plugins" and "gst_plugins"
[platform/upstream/gstreamer.git] / meson.build
1 project('gstreamer-full', 'c',
2   version : '1.21.0.1',
3   meson_version : '>= 0.60.0',
4   default_options : ['buildtype=debugoptimized',
5                      # Needed due to https://github.com/mesonbuild/meson/issues/1889,
6                      # but this can cause problems in the future. Remove it
7                      # when it's no longer necessary.
8                      'cpp_std=c++14'])
9
10 apiversion = '1.0'
11 gst_version = '>= @0@'.format(meson.project_version())
12
13 build_system = build_machine.system()
14 cc = meson.get_compiler('c')
15
16 fs = import('fs')
17 gnome = import('gnome')
18 pkgconfig = import('pkgconfig')
19 python3 = import('python').find_installation()
20 # Ensure that we're not being run from inside the development environment
21 # because that will confuse meson, and it might find the already-built
22 # gstreamer. It's fine if people run `ninja` as long as it doesn't run
23 # reconfigure because ninja doesn't care about the env.
24 ensure_not_devenv = '''
25 import os
26 assert('GST_ENV' not in os.environ)
27 '''
28 cmdres = run_command(python3, '-c', ensure_not_devenv, check: false)
29 if cmdres.returncode() != 0
30   error('Do not run `ninja reconfigure` or `meson` for gst-build inside the development environment, you will run into problems')
31 endif
32
33 # Install gst-indent pre-commit hook
34 run_command(python3, '-c', 'import shutil; shutil.copy("scripts/git-hooks/multi-pre-commit.hook", ".git/hooks/pre-commit")', check: false)
35
36 # Ensure that the user does not have Strawberry Perl in PATH, since it ships
37 # with a pkg-config.bat and broken pkgconfig files for libffi and zlib. Will
38 # cause a build error, such as in
39 # https://gitlab.freedesktop.org/gstreamer/gst-build/-/issues/41
40 ensure_no_strawberry_perl = '''
41 import os
42 assert(r'Strawberry\perl\bin' not in os.environ['PATH'])
43 '''
44 if build_system == 'windows' and meson.version().version_compare('<0.60.0')
45   cmdres = run_command(python3, '-c', ensure_no_strawberry_perl, check: false)
46   if cmdres.returncode() != 0
47     error('You have Strawberry Perl in PATH which is known to cause build issues with Meson < 0.60.0. Please remove it from PATH, uninstall it, or upgrade Meson.')
48   endif
49 endif
50
51 # On macOS, you have to run "Install Certificates.command" otherwise Python
52 # doesn't have access to the latest SSL CA Certificates, and Meson will fail to
53 # download wrap files from websites that use, for example, Let's Encrypt.
54 # We already recommend this in the README, but add a warning here as well.
55 # Can't make this an error because the user might be using XCode's Python
56 # 3 which doesn't have this script.
57 if build_system == 'darwin'
58   python3_cacert_file = python3.get_path('data') / 'etc/openssl/cert.pem'
59   install_cert_cmd = '/Applications/Python @0@/Install Certificates.command'.format(python3.language_version())
60   if not fs.is_symlink(python3_cacert_file) and fs.is_file(install_cert_cmd)
61     warning('Please run "@0@" so that Python has access to the latest SSL certificates. Meson might fail to download some wraps without it.'.format(install_cert_cmd))
62   endif
63 endif
64
65 documented_projects = ''
66 # Make it possible to use msys2 built zlib which fails
67 # when not using the mingw toolchain as it uses unistd.h
68 if not meson.is_subproject() and cc.get_id() == 'msvc'
69   uname = find_program('uname', required: false)
70   if uname.found()
71     ret = run_command(uname, '-o', check: false)
72     if ret.returncode() == 0 and ret.stdout().to_lower() == 'msys'
73       ret = run_command(uname, '-r', check: false)
74       # The kernel version returned by uname is actually the msys version
75       if ret.returncode() == 0 and ret.stdout().startswith('2')
76         # If a system zlib is found, disable UNIX features in zlib.h and zconf.h
77         if cc.find_library('z').found()
78           add_global_arguments('-DZ_SOLO', language: 'c')
79         endif
80       endif
81     endif
82   endif
83 endif
84
85 # Ensure that MSVC interprets all source code as UTF-8. Only do this when we're
86 # not a subproject, because subprojects are not allowed to call
87 # add_global_arguments().
88 if not meson.is_subproject() and cc.get_id() == 'msvc'
89   add_global_arguments(
90       cc.get_supported_arguments(['/utf-8']), # set the input encoding to utf-8
91       language: ['c', 'cpp'])
92 endif
93
94 building_full = get_option('default_library') == 'static'
95 tools_option = 'tools=auto'
96 if building_full and not get_option('tools').disabled()
97   # Do not build subprojects tools when we build them against gst-full
98   tools_option = 'tools=disabled'
99 endif
100
101 # Ordered list of subprojects (dict has no ordering guarantees)
102 subprojects = [
103   ['gstreamer', {'build-hotdoc': true, 'subproject_options': [tools_option]}],
104   ['gst-plugins-base', {'option': get_option('base'), 'build-hotdoc': true}],
105   ['gst-plugins-good', {'option': get_option('good'), 'build-hotdoc': true}],
106   ['libnice', { 'option': get_option('libnice'), 'match_gst_version': false}],
107   ['gst-plugins-bad', { 'option': get_option('bad'), 'build-hotdoc': true}],
108   ['gst-plugins-ugly', { 'option': get_option('ugly'), 'build-hotdoc': true}],
109   ['gst-libav', { 'option': get_option('libav'), 'build-hotdoc': true}],
110   ['gst-rtsp-server', { 'option': get_option('rtsp_server'), 'build-hotdoc': true}],
111   ['gst-devtools', { 'option': get_option('devtools'), 'build-hotdoc': true, 'subproject_options': [tools_option]}],
112   ['gst-integration-testsuites', { 'option': get_option('devtools') }],
113   ['gst-editing-services', { 'option': get_option('ges'), 'build-hotdoc': true, 'subproject_options': [tools_option]}],
114   ['gstreamer-vaapi', { 'option': get_option('vaapi'), 'build-hotdoc': true}],
115   ['gst-omx', { 'option': get_option('omx'), 'build-hotdoc': true}],
116   ['gstreamer-sharp', { 'option': get_option('sharp') }],
117   ['pygobject', { 'option': get_option('python'), 'match_gst_version': false, 'sysdep': 'pygobject-3.0', 'sysdep_version': '>= 3.8' }],
118   ['gst-python', { 'option': get_option('python')}],
119   ['gst-examples', { 'option': get_option('gst-examples'), 'match_gst_versions': false}],
120   ['gst-plugins-rs', { 'option': get_option('rs'), 'match_gst_version': false}],
121 ]
122
123 symlink = '''
124 import os
125
126 os.symlink(os.path.join('@1@', 'subprojects', '@0@'),
127   os.path.join('@1@', '@0@'))
128 '''
129
130 if build_system == 'windows'
131   subproject('win-flex-bison-binaries')
132   subproject('win-nasm')
133 elif build_system == 'darwin'
134   subproject('macos-bison-binary')
135 endif
136
137 orc_subproject = subproject('orc', required: get_option('orc'))
138
139 foreach custom_subproj: get_option('custom_subprojects').split(',')
140     if custom_subproj != ''
141         message ('Adding custom subproject ' + custom_subproj)
142         subprojects += [[custom_subproj, {'match_gst_version': false}]]
143     endif
144 endforeach
145
146
147 subprojects_names = []
148 plugins_doc_caches = []
149 orc_update_targets = []
150 all_plugins = []
151 all_tools = {}
152 # Using a list and not a dict to keep the ordering to build the chain of `gir`
153 # dependencies
154 all_libraries = []
155 foreach sp : subprojects
156   project_name = sp[0]
157   build_infos = sp[1]
158   is_required = build_infos.get('option', true)
159   sysdep = build_infos.get('sysdep', '')
160   sysdep_version = build_infos.get('sysdep_version', '')
161   match_gst_version = build_infos.get('match_gst_version', true)
162   default_options =  build_infos.get('subproject_options', [])
163
164   if match_gst_version
165     subproj = subproject(project_name, version: gst_version, required: is_required, default_options: default_options)
166   elif sysdep != ''
167       sysdep_dep = dependency(sysdep, version: sysdep_version, required: false, default_options: default_options)
168       if not sysdep_dep.found()
169         subproj = subproject(project_name, required: is_required, default_options: default_options)
170       endif
171   else
172     subproj = subproject(project_name, required: is_required, default_options: default_options)
173   endif
174
175   if subproj.found()
176     plugins = subproj.get_variable('gst_plugins', [])
177     legacy_plugins = subproj.get_variable('plugins', [])
178     all_plugins += plugins
179     if plugins.length() == 0 and legacy_plugins.length() > 0
180       warning(f'DEPRECATED use of the `plugins` variable in @project_name@.')
181       warning('The variable should now be called `gst_plugins` and use:')
182       warning('`declare_dependency( link_with: <plugin_target>, variable: {\'full_path\': <plugin_target>.full_path()})` instead')
183       foreach plugin: legacy_plugins
184         all_plugins += [declare_dependency(link_with: plugin, variables: {'full_path': plugin.full_path()})]
185       endforeach
186     endif
187
188     all_libraries += subproj.get_variable('gst_libraries', [])
189     if not get_option('tools').disabled()
190       all_tools += subproj.get_variable('gst_tools', {})
191     endif
192
193     orc_update_targets += subproj.get_variable('orc_update_targets', [])
194
195     subprojects_names += [project_name]
196
197     if not meson.is_cross_build() and build_infos.get('build-hotdoc', false)
198       plugins_doc_caches += [subproj.get_variable('gst_plugins_doc_dep', [])]
199       if documented_projects != ''
200         documented_projects += ','
201       endif
202       documented_projects  += project_name
203     endif
204   endif
205 endforeach
206
207 # Check if we need to also build glib-networking for TLS modules
208 glib_dep = dependency('glib-2.0')
209 if glib_dep.type_name() == 'internal'
210   subproject('glib-networking', required : get_option('tls'),
211              default_options: ['gnutls=auto', 'openssl=auto'])
212 endif
213
214 gst_plugins_doc_dep = custom_target('plugins-doc-cache',
215   command: [python3, '-c', 'print("Built all doc caches")'],
216   input: plugins_doc_caches,
217   output: 'plugins_doc_caches',
218   capture: true,
219 )
220
221 if meson.is_cross_build() or build_machine.system() == 'windows'
222     if get_option('doc').enabled()
223         error('Documentation enabled but building the doc while cross building or building on windows is not supported yet.')
224     endif
225
226     documented_projects = ''
227     message('Documentation not built as building the documentation while cross building or building on windows is not supported yet.')
228 else
229   hotdoc_p = find_program('hotdoc', required : get_option('doc'))
230   if not hotdoc_p.found()
231     documented_projects = ''
232     message('Not building documentation as hotdoc was not found')
233   endif
234 endif
235
236 write_file_contents = '''
237 import os
238 import sys
239
240 assert len(sys.argv) >= 3
241 fname = sys.argv[1]
242 contents = sys.argv[2]
243
244 with open(fname, 'w') as f:
245     f.write(contents)
246 '''
247
248 configure_file(
249   output : 'GstDocumentedSubprojects',
250   command : [python3,
251              '-c', write_file_contents,
252              '@OUTPUT@',
253              documented_projects]
254 )
255
256 if documented_projects != ''
257   subproject('gst-docs', required: get_option('doc').enabled())
258   message('Gst docs subprojects: ' + documented_projects)
259 endif
260
261 all_plugins_paths = []
262 all_plugins_dirs = []
263 plugins_names = []
264 foreach plugin: all_plugins
265   plugin_path = plugin.get_variable('full_path')
266   all_plugins_paths += plugin_path
267   all_plugins_dirs += fs.parent(plugin_path)
268   plugins_names += plugin_path
269 endforeach
270
271 # Work around meson bug: https://github.com/mesonbuild/meson/pull/6770
272 pathsep = host_machine.system() == 'windows' ? ';' : ':'
273 all_plugins_paths = pathsep.join(all_plugins_paths)
274
275 devenv = environment()
276 if not building_full
277   devenv.prepend('GST_PLUGIN_PATH', all_plugins_dirs)
278 else
279   # Make sure the current build directory is first in PATH so we prefer tools
280   # built here that links on gst-full instead instead of those built in
281   # subprojects.
282   devenv.prepend('PATH', meson.current_build_dir())
283 endif
284 devenv.set('CURRENT_GST', meson.current_source_dir())
285 devenv.set('GST_VERSION', meson.project_version())
286 devenv.set('GST_ENV', 'gst-' + meson.project_version())
287 devenv.set('GST_REGISTRY', meson.current_build_dir() / 'registry.dat')
288 devenv.set('GST_PLUGIN_SYSTEM_PATH', '')
289 meson.add_devenv(devenv)
290
291 generate_plugins_paths = find_program('scripts/generate_plugins_path.py')
292 configure_file(
293   output : 'GstPluginsPath.json',
294   command : [generate_plugins_paths,
295              '@OUTPUT@',
296              all_plugins_paths]
297 )
298
299 if building_full
300   cdata = configuration_data()
301   cdata.set_quoted('GST_API_VERSION', apiversion)
302   cdata.set_quoted('GETTEXT_PACKAGE', 'gstreamer-full-1.0')
303   cdata.set_quoted('PACKAGE_VERSION', gst_version)
304   cdata.set_quoted('GST_PACKAGE_ORIGIN', get_option('package-origin'))
305   configure_file(output : 'config.h', configuration : cdata)
306   configinc = include_directories('.')
307   gst_c_args = ['-DHAVE_CONFIG_H']
308
309   # Generate a .c file which declare and register all built plugins
310   all_plugin_names = ';'.join(plugins_names)
311
312   static_plugins = get_option('gst-full-plugins')
313   if static_plugins == '*'
314     static_plugins = all_plugin_names
315   endif
316   generate_init_static_plugins = find_program('scripts/generate_init_static_plugins.py')
317   init_static_plugins_c = configure_file(
318     output: 'gstinitstaticplugins.c',
319     command : [generate_init_static_plugins,
320                '-o ' + '@OUTPUT@',
321                '-p ' + static_plugins,
322                '-e ' + get_option('gst-full-elements'),
323                '-t ' + get_option('gst-full-typefind-functions'),
324                '-d ' + get_option('gst-full-device-providers'),
325                '-T ' + get_option('gst-full-dynamic-types')
326                ]
327   )
328
329   gstfull_link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions'])
330
331   # Get a list of libraries that needs to be exposed in the ABI.
332   exposed_libs = []
333   exposed_deps = []
334   exposed_girs = []
335   incdir_deps = []
336   wanted_libs = ['gstreamer-1.0'] + get_option('gst-full-libraries')
337   all_libs = '*' in wanted_libs
338
339   foreach pkgname_library : all_libraries
340     pkg_name = pkgname_library[0]
341     lib_def = pkgname_library[1]
342
343     if pkg_name in wanted_libs or all_libs
344       if lib_def.has_key('lib')
345         exposed_deps += dependency(pkg_name)
346         incdir_deps += dependency(pkg_name).partial_dependency(includes: true, sources: true)
347         exposed_libs += [lib_def['lib']]
348       endif
349
350       if lib_def.has_key('gir')
351         exposed_girs += lib_def['gir']
352       endif
353     endif
354   endforeach
355
356   # glib and gobject are part of our public API. If we are using glib from the
357   # system then our pkg-config file must require it. If we built it as
358   # subproject then we need to link_whole it.
359   glib_deps = []
360   glib_dep = dependency('glib-2.0')
361   gobject_dep = dependency('gobject-2.0')
362   if gobject_dep.type_name() == 'internal'
363     glib_subproject = subproject('glib')
364     exposed_libs += glib_subproject.get_variable('libglib')
365     exposed_libs += glib_subproject.get_variable('libgobject')
366     incdir_deps += [
367       glib_dep.partial_dependency(includes: true),
368       gobject_dep.partial_dependency(includes: true),
369     ]
370   else
371     glib_deps = [glib_dep, gobject_dep]
372   endif
373
374   link_deps = []
375   if get_option('gst-full-version-script') != ''
376     symbol_map = meson.current_source_dir() / get_option('gst-full-version-script')
377     link_arg = '-Wl,--version-script=' + symbol_map
378     if cc.has_link_argument(link_arg)
379       gstfull_link_args += link_arg
380       link_deps += symbol_map
381     elif cc.get_id() == 'msvc'
382       warning('FIXME: Provide a def file to publish the public symbols')
383     else
384       warning('FIXME: Linker does not support the supplied version script (' + symbol_map + '), please disable the "gst-full-version-script" option')
385     endif
386   endif
387
388   # Build both shared and static library
389   gstfull = both_libraries('gstreamer-full-1.0',
390     init_static_plugins_c,
391     link_args: gstfull_link_args,
392     link_whole : exposed_libs,
393     dependencies : [incdir_deps, glib_deps, all_plugins],
394     link_depends : link_deps,
395     install : true,
396   )
397
398   gst_full_dep = declare_dependency(link_with: gstfull.get_shared_lib(),
399     dependencies : incdir_deps + glib_deps,
400     include_directories: include_directories('.')
401   )
402
403   gst_full_libs_private = cc.get_supported_link_arguments(['-Wl,--undefined=gst_init_static_plugins'])
404   if gst_full_libs_private == []
405     warning('The compiler does not support `-Wl,--undefined` linker flag. The method `gst_init_static_plugins` might be dropped during the link stage of an application using libgstreamer-full-1.0.a, preventing plugins registration.')
406   endif
407
408   if not get_option('introspection').disabled()
409     built_girs = {}
410     foreach gir: exposed_girs
411       includes = []
412       foreach include: gir.get('includes', [])
413         includes += [built_girs.get(include, include)]
414       endforeach
415
416       gir += {
417         'includes': includes,
418         'extra_args': gir.get('extra_args', []) + ['--add-include-path=' + meson.current_build_dir()],
419         'install': true,
420       }
421       built_girs += {gir.get('namespace') + '-' + gir.get('nsversion'): gnome.generate_gir(gstfull, kwargs: gir)[0]}
422     endforeach
423   endif
424
425   pkgconfig.generate(gstfull,
426     requires: glib_deps,
427     libraries_private: gst_full_libs_private,
428     subdirs : 'gstreamer-1.0')
429   meson.override_dependency('gstreamer-full-1.0', gst_full_dep)
430
431   if not get_option('tools').disabled()
432     foreach tool, data: all_tools
433       exe_name = '@0@-@1@'.format(tool, apiversion)
434       extra_args = data.get('extra_c_args', [])
435       sources = data.get('files')
436       deps = []
437       foreach d : data.get('deps', [])
438         if d not in exposed_deps
439           deps += d
440         endif
441       endforeach
442
443       executable(exe_name,
444         sources,
445         install: true,
446         include_directories : [configinc],
447         dependencies : [gst_full_dep] + deps,
448         c_args: extra_args + gst_c_args + ['-DG_LOG_DOMAIN="@0@"'.format(exe_name)],
449       )
450
451       if data.has_key('man_page')
452         install_man(data.get('man_page'))
453       endif
454
455     endforeach
456   endif
457 endif
458
459 message('Building subprojects: ' + ', '.join(subprojects_names))
460
461 setenv = find_program('gst-env.py')
462
463 devenv_cmd = [setenv, '--builddir=@0@'.format(meson.global_build_root()),
464               '--srcdir=@0@'.format(meson.global_source_root())]
465
466 subdir('tests')
467 subdir('ci/fuzzing')
468
469 if meson.can_run_host_binaries() and build_machine.system() == 'linux' and host_machine.system() == 'windows'
470   # FIXME: Ideally we could get the wrapper directly from meson
471   devenv_cmd += ['--wine', host_machine.cpu_family() == 'x86_64' ? 'wine64' : 'wine32']
472   sysroot = meson.get_cross_property('sys_root')
473   if sysroot != ''
474     # Logic from meson
475     devenv_cmd += ['--winepath', 'Z:' + join_paths(sysroot, 'bin')]
476   endif
477 endif
478
479 run_target('devenv', command : devenv_cmd)
480
481 if orc_subproject.found() and orc_update_targets.length() > 0
482   alias_target('update-orc-dist', orc_update_targets)
483 endif
484
485 dotnet_format = find_program('dotnet-format', required: false)
486 if dotnet_format.found()
487     run_target('csharp_format_check',
488         command: [join_paths(meson.current_source_dir(), 'scripts', 'format-csharp'),
489             '--check'
490         ],
491     )
492     run_target('csharp_format_apply',
493         command: [join_paths(meson.current_source_dir(), 'scripts', 'format-csharp'),
494         ],
495     )
496 endif
497
498 summary({
499   'gstreamer-full library': building_full,
500 }, section: 'Build options', bool_yn: true, list_sep: '  ')
501
502 gst_tools = []
503 foreach tool, data: all_tools
504   gst_tools += tool
505 endforeach
506
507 summary({
508     'Tools': gst_tools,
509 }, section: 'Build options', list_sep: ', ')