tools: Add support for building gstreamer tools against gst-full
[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 documented_projects = ''
52 # Make it possible to use msys2 built zlib which fails
53 # when not using the mingw toolchain as it uses unistd.h
54 if not meson.is_subproject() and cc.get_id() == 'msvc'
55   uname = find_program('uname', required: false)
56   if uname.found()
57     ret = run_command(uname, '-o', check: false)
58     if ret.returncode() == 0 and ret.stdout().to_lower() == 'msys'
59       ret = run_command(uname, '-r', check: false)
60       # The kernel version returned by uname is actually the msys version
61       if ret.returncode() == 0 and ret.stdout().startswith('2')
62         # If a system zlib is found, disable UNIX features in zlib.h and zconf.h
63         if cc.find_library('z').found()
64           add_global_arguments('-DZ_SOLO', language: 'c')
65         endif
66       endif
67     endif
68   endif
69 endif
70
71 # Ensure that MSVC interprets all source code as UTF-8. Only do this when we're
72 # not a subproject, because subprojects are not allowed to call
73 # add_global_arguments().
74 if not meson.is_subproject() and cc.get_id() == 'msvc'
75   add_global_arguments(
76       cc.get_supported_arguments(['/utf-8']), # set the input encoding to utf-8
77       language: ['c', 'cpp'])
78 endif
79
80 building_full = get_option('default_library') == 'static'
81 tools_option = 'tools=auto'
82 if building_full and not get_option('tools').disabled()
83   # Do not build subprojects tools when we build them against gst-full
84   tools_option = 'tools=disabled'
85 endif
86
87 # Ordered list of subprojects (dict has no ordering guarantees)
88 subprojects = [
89   ['gstreamer', {'build-hotdoc': true, 'subproject_options': [tools_option]}],
90   ['gst-plugins-base', {'option': get_option('base'), 'build-hotdoc': true}],
91   ['gst-plugins-good', {'option': get_option('good'), 'build-hotdoc': true}],
92   ['libnice', { 'option': get_option('libnice'), 'match_gst_version': false}],
93   ['gst-plugins-bad', { 'option': get_option('bad'), 'build-hotdoc': true}],
94   ['gst-plugins-ugly', { 'option': get_option('ugly'), 'build-hotdoc': true}],
95   ['gst-libav', { 'option': get_option('libav'), 'build-hotdoc': true}],
96   ['gst-rtsp-server', { 'option': get_option('rtsp_server'), 'build-hotdoc': true}],
97   ['gst-devtools', { 'option': get_option('devtools'), 'build-hotdoc': true, 'subproject_options': [tools_option]}],
98   ['gst-integration-testsuites', { 'option': get_option('devtools') }],
99   ['gst-editing-services', { 'option': get_option('ges'), 'build-hotdoc': true, 'subproject_options': [tools_option]}],
100   ['gstreamer-vaapi', { 'option': get_option('vaapi'), 'build-hotdoc': true}],
101   ['gst-omx', { 'option': get_option('omx'), 'build-hotdoc': true}],
102   ['gstreamer-sharp', { 'option': get_option('sharp') }],
103   ['pygobject', { 'option': get_option('python'), 'match_gst_version': false, 'sysdep': 'pygobject-3.0', 'sysdep_version': '>= 3.8' }],
104   ['gst-python', { 'option': get_option('python')}],
105   ['gst-examples', { 'option': get_option('gst-examples'), 'match_gst_versions': false}],
106   ['gst-plugins-rs', { 'option': get_option('rs'), 'match_gst_version': false}],
107 ]
108
109 symlink = '''
110 import os
111
112 os.symlink(os.path.join('@1@', 'subprojects', '@0@'),
113   os.path.join('@1@', '@0@'))
114 '''
115
116 if build_system == 'windows'
117   subproject('win-flex-bison-binaries')
118   subproject('win-nasm')
119 elif build_system == 'darwin'
120   subproject('macos-bison-binary')
121 endif
122
123 orc_subproject = subproject('orc', required: get_option('orc'))
124
125 foreach custom_subproj: get_option('custom_subprojects').split(',')
126     if custom_subproj != ''
127         message ('Adding custom subproject ' + custom_subproj)
128         subprojects += [[custom_subproj, {'match_gst_version': false}]]
129     endif
130 endforeach
131
132
133 subprojects_names = []
134 plugins_doc_caches = []
135 orc_update_targets = []
136 all_plugins = []
137 all_tools = {}
138 # Using a list and not a dict to keep the ordering to build the chain of `gir`
139 # dependencies
140 all_libraries = []
141 foreach sp : subprojects
142   project_name = sp[0]
143   build_infos = sp[1]
144   is_required = build_infos.get('option', true)
145   sysdep = build_infos.get('sysdep', '')
146   sysdep_version = build_infos.get('sysdep_version', '')
147   match_gst_version = build_infos.get('match_gst_version', true)
148   default_options =  build_infos.get('subproject_options', [])
149
150   if match_gst_version
151     subproj = subproject(project_name, version: gst_version, required: is_required, default_options: default_options)
152   elif sysdep != ''
153       sysdep_dep = dependency(sysdep, version: sysdep_version, required: false, default_options: default_options)
154       if not sysdep_dep.found()
155         subproj = subproject(project_name, required: is_required, default_options: default_options)
156       endif
157   else
158     subproj = subproject(project_name, required: is_required, default_options: default_options)
159   endif
160
161   if subproj.found()
162     plugins = subproj.get_variable('plugins', [])
163     all_plugins += plugins
164     all_libraries += subproj.get_variable('libraries', [])
165     all_tools += subproj.get_variable('gst_tools', {})
166
167     orc_update_targets += subproj.get_variable('orc_update_targets', [])
168
169     subprojects_names += [project_name]
170
171     if not meson.is_cross_build() and build_infos.get('build-hotdoc', false)
172       if plugins.length() > 0
173         plugins_doc_caches += [subproj.get_variable('plugins_doc_dep', [])]
174       endif
175       if documented_projects != ''
176         documented_projects += ','
177       endif
178       documented_projects  += project_name
179     endif
180   endif
181 endforeach
182
183 # Check if we need to also build glib-networking for TLS modules
184 glib_dep = dependency('glib-2.0')
185 if glib_dep.type_name() == 'internal'
186   subproject('glib-networking', required : get_option('tls'),
187              default_options: ['gnutls=auto', 'openssl=auto'])
188 endif
189
190 plugins_doc_dep = custom_target('plugins-doc-cache',
191   command: [python3, '-c', 'print("Built all doc caches")'],
192   input: plugins_doc_caches,
193   output: 'plugins_doc_caches',
194   capture: true,
195 )
196
197 if meson.is_cross_build() or build_machine.system() == 'windows'
198     if get_option('doc').enabled()
199         error('Documentation enabled but building the doc while cross building or building on windows is not supported yet.')
200     endif
201
202     documented_projects = ''
203     message('Documentation not built as building the documentation while cross building or building on windows is not supported yet.')
204 else
205   hotdoc_p = find_program('hotdoc', required : get_option('doc'))
206   if not hotdoc_p.found()
207     documented_projects = ''
208     message('Not building documentation as hotdoc was not found')
209   endif
210 endif
211
212 write_file_contents = '''
213 import os
214 import sys
215
216 assert len(sys.argv) >= 3
217 fname = sys.argv[1]
218 contents = sys.argv[2]
219
220 with open(fname, 'w') as f:
221     f.write(contents)
222 '''
223
224 configure_file(
225   output : 'GstDocumentedSubprojects',
226   command : [python3,
227              '-c', write_file_contents,
228              '@OUTPUT@',
229              documented_projects]
230 )
231
232 if documented_projects != ''
233   subproject('gst-docs', required: get_option('doc').enabled())
234   message('Gst docs subprojects: ' + documented_projects)
235 endif
236
237 all_plugins_paths = []
238 all_plugins_dirs = []
239 foreach plugin: all_plugins
240   all_plugins_paths += plugin.full_path()
241   all_plugins_dirs += fs.parent(plugin.full_path())
242 endforeach
243 # Work around meson bug: https://github.com/mesonbuild/meson/pull/6770
244 pathsep = host_machine.system() == 'windows' ? ';' : ':'
245 all_plugins_paths = pathsep.join(all_plugins_paths)
246
247 devenv = environment()
248 devenv.prepend('GST_PLUGIN_PATH', all_plugins_dirs)
249 devenv.set('CURRENT_GST', meson.current_source_dir())
250 devenv.set('GST_VERSION', meson.project_version())
251 devenv.set('GST_ENV', 'gst-' + meson.project_version())
252 devenv.set('GST_REGISTRY', meson.current_build_dir() / 'registry.dat')
253 devenv.set('GST_PLUGIN_SYSTEM_PATH', '')
254 meson.add_devenv(devenv)
255
256 generate_plugins_paths = find_program('scripts/generate_plugins_path.py')
257 configure_file(
258   output : 'GstPluginsPath.json',
259   command : [generate_plugins_paths,
260              '@OUTPUT@',
261              all_plugins_paths]
262 )
263
264 if building_full
265   cdata = configuration_data()
266   cdata.set_quoted('GST_API_VERSION', apiversion)
267   cdata.set_quoted('GETTEXT_PACKAGE', 'gstreamer-full-1.0')
268   cdata.set_quoted('PACKAGE_VERSION', gst_version)
269   cdata.set_quoted('GST_PACKAGE_ORIGIN', get_option('package-origin'))
270   configure_file(output : 'config.h', configuration : cdata)
271   configinc = include_directories('.')
272   gst_c_args = ['-DHAVE_CONFIG_H']
273
274   # Generate a .c file which declare and register all built plugins
275   plugins_names = []
276   foreach plugin: all_plugins
277     plugins_names += plugin.full_path()
278   endforeach
279   all_plugin_names = ';'.join(plugins_names)
280
281   static_plugins = get_option('gst-full-plugins')
282   if static_plugins == '*'
283     static_plugins = all_plugin_names
284   endif
285   generate_init_static_plugins = find_program('scripts/generate_init_static_plugins.py')
286   init_static_plugins_c = configure_file(
287     output: 'gstinitstaticplugins.c',
288     command : [generate_init_static_plugins,
289                '-o ' + '@OUTPUT@',
290                '-p ' + static_plugins,
291                '-e ' + get_option('gst-full-elements'),
292                '-t ' + get_option('gst-full-typefind-functions'),
293                '-d ' + get_option('gst-full-device-providers'),
294                '-T ' + get_option('gst-full-dynamic-types')
295                ]
296   )
297
298   gstfull_link_args = cc.get_supported_link_arguments(['-Wl,-Bsymbolic-functions'])
299
300   # Get a list of libraries that needs to be exposed in the ABI.
301   exposed_libs = []
302   exposed_deps = []
303   exposed_girs = []
304   incdir_deps = []
305   wanted_libs = ['gstreamer-1.0'] + get_option('gst-full-libraries')
306   all_libs = '*' in wanted_libs
307
308   foreach pkgname_library : all_libraries
309     pkg_name = pkgname_library[0]
310     lib_def = pkgname_library[1]
311
312     if pkg_name in wanted_libs or all_libs
313       if lib_def.has_key('lib')
314         exposed_deps += dependency(pkg_name)
315         incdir_deps += dependency(pkg_name).partial_dependency(includes: true, sources: true)
316         exposed_libs += [lib_def['lib']]
317       endif
318
319       if lib_def.has_key('gir')
320         exposed_girs += lib_def['gir']
321       endif
322     endif
323   endforeach
324
325   # glib and gobject are part of our public API. If we are using glib from the
326   # system then our pkg-config file must require it. If we built it as
327   # subproject then we need to link_whole it.
328   glib_deps = []
329   glib_dep = dependency('glib-2.0')
330   gobject_dep = dependency('gobject-2.0')
331   if gobject_dep.type_name() == 'internal'
332     glib_subproject = subproject('glib')
333     exposed_libs += glib_subproject.get_variable('libglib')
334     exposed_libs += glib_subproject.get_variable('libgobject')
335     incdir_deps += [
336       glib_dep.partial_dependency(includes: true),
337       gobject_dep.partial_dependency(includes: true),
338     ]
339   else
340     glib_deps = [glib_dep, gobject_dep]
341   endif
342
343   link_deps = []
344   if get_option('gst-full-version-script') != ''
345     symbol_map = meson.current_source_dir() / get_option('gst-full-version-script')
346     link_arg = '-Wl,--version-script=' + symbol_map
347     if cc.has_link_argument(link_arg)
348       gstfull_link_args += link_arg
349       link_deps += symbol_map
350     elif cc.get_id() == 'msvc'
351       warning('FIXME: Provide a def file to publish the public symbols')
352     else
353       warning('FIXME: Linker does not support the supplied version script (' + symbol_map + '), please disable the "gst-full-version-script" option')
354     endif
355   endif
356
357   # Build both shared and static library
358   gstfull = both_libraries('gstreamer-full-1.0',
359     init_static_plugins_c,
360     link_with : all_plugins,
361     link_args: gstfull_link_args,
362     link_whole : exposed_libs,
363     dependencies : incdir_deps + glib_deps,
364     link_depends : link_deps,
365     install : true,
366   )
367
368   gst_full_dep = declare_dependency(link_with: gstfull.get_shared_lib(),
369     dependencies : incdir_deps + glib_deps,
370     include_directories: include_directories('.')
371   )
372
373   gst_full_libs_private = cc.get_supported_link_arguments(['-Wl,--undefined=gst_init_static_plugins'])
374   if gst_full_libs_private == []
375     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.')
376   endif
377
378   if not get_option('introspection').disabled()
379     built_girs = {}
380     foreach gir: exposed_girs
381       includes = []
382       foreach include: gir.get('includes', [])
383         includes += [built_girs.get(include, include)]
384       endforeach
385
386       gir += {
387         'includes': includes,
388         'extra_args': gir.get('extra_args', []) + ['--add-include-path=' + meson.current_build_dir()],
389         'install': true,
390       }
391       built_girs += {gir.get('namespace') + '-' + gir.get('nsversion'): gnome.generate_gir(gstfull, kwargs: gir)[0]}
392     endforeach
393   endif
394
395   pkgconfig.generate(gstfull,
396     requires: glib_deps,
397     libraries_private: gst_full_libs_private,
398     subdirs : 'gstreamer-1.0')
399   meson.override_dependency('gstreamer-full-1.0', gst_full_dep)
400
401   if not get_option('tools').disabled()
402     foreach tool, data: all_tools
403       exe_name = '@0@-@1@'.format(tool, apiversion)
404       extra_args = data.get('extra_c_args', [])
405       sources = data.get('files')
406       deps = []
407       foreach d : data.get('deps', [])
408         if d not in exposed_deps
409           deps += d
410         endif
411       endforeach
412
413       executable(exe_name,
414         sources,
415         install: true,
416         include_directories : [configinc],
417         dependencies : [gst_full_dep] + deps,
418         c_args: extra_args + gst_c_args + ['-DG_LOG_DOMAIN="@0@"'.format(exe_name)],
419       )
420
421       if data.has_key('man_page')
422         install_man(data.get('man_page'))
423       endif
424
425     endforeach
426   endif
427 endif
428
429 message('Building subprojects: ' + ', '.join(subprojects_names))
430
431 setenv = find_program('gst-env.py')
432
433 devenv_cmd = [setenv, '--builddir=@0@'.format(meson.global_build_root()),
434               '--srcdir=@0@'.format(meson.global_source_root())]
435
436 subdir('tests')
437 if meson.can_run_host_binaries() and build_machine.system() == 'linux' and host_machine.system() == 'windows'
438   # FIXME: Ideally we could get the wrapper directly from meson
439   devenv_cmd += ['--wine', host_machine.cpu_family() == 'x86_64' ? 'wine64' : 'wine32']
440   sysroot = meson.get_cross_property('sys_root')
441   if sysroot != ''
442     # Logic from meson
443     devenv_cmd += ['--winepath', 'Z:' + join_paths(sysroot, 'bin')]
444   endif
445 endif
446
447 run_target('devenv', command : devenv_cmd)
448
449 if orc_subproject.found() and orc_update_targets.length() > 0
450   alias_target('update-orc-dist', orc_update_targets)
451 endif
452
453 summary({
454   'gstreamer-full': building_full,
455 }, section: 'Build options', bool_yn: true, list_sep: '  ')