Debian Codegraph @

subprojects/meson-docs/docs/markdown/Porting-from-autotools.md

raw
1 # Porting from Autotools
2
3 This page uses
4 [AppStream-glib](https://github.com/hughsie/appstream-glib/) as an
5 example project. AppStream-Glib contains some libraries, GObject
6 Introspection data, tests, man pages, i18n, bash-completion with
7 optional flags to build/not build support for some things.
8
9 Meson comes with a helper script `ac_converter` that you can use to
10 convert the basic autoconf checks for your project.
11
12 ## Configure.ac
13
14 First let's look at `configure.ac` and write the same in
15 `meson.build`.
16
17 ```autoconf
18 AC_PREREQ(2.63)
19 ```
20 Meson doesn't provide the same function, so just ignore this.
21
22 ### Defining variables
23 `configure.ac`:
24 ```autoconf
25 m4_define([as_major_version], [0])
26 m4_define([as_minor_version], [3])
27 m4_define([as_micro_version], [6])
28 m4_define([as_version],
29 [as_major_version.as_minor_version.as_micro_version])
30 ```
31 `meson.build`:
32 ```meson
33
34 as_version = meson.project_version() # set in project() below
35 ver_arr = as_version.split('.')
36
37 as_major_version = ver_arr[0]
38 as_minor_version = ver_arr[1]
39 as_micro_version = ver_arr[2]
40 ```
41
42 ### Initializing project and setting compilers
43 `configure.ac`:
44 ```autoconf
45 AC_INIT([appstream-glib],[as_version])
46 AC_PROG_CC
47 ```
48 `meson.build`:
49 ```meson
50 project('appstream-glib', 'c', version : '0.3.6')
51 ```
52 Note that this must be the first line of your `meson.build` file.
53
54 ### AC_SUBST
55 `configure.ac`:
56 ```autoconf
57 AC_SUBST(AS_MAJOR_VERSION)
58 AC_SUBST(AS_MINOR_VERSION)
59 AC_SUBST(AS_MICRO_VERSION)
60 AC_SUBST(AS_VERSION)
61 ```
62
63 You don't need to do the same in Meson, because it does not have two
64 different types of files (Makefile, configure).
65
66 ### Auto headers
67
68 `configure.ac`:
69
70 ```autoconf
71 AC_CONFIG_HEADERS([config.h])
72 ```
73
74 `meson.build`:
75
76 ```meson
77 conf = configuration_data()
78 # Surround the version in quotes to make it a C string
79 conf.set_quoted('VERSION', as_version)
80 configure_file(output : 'config.h',
81 configuration : conf)
82 ```
83
84 Meson doesn't support autoheaders, you need to manually specify what
85 do you want to see in header file, write `configuration_data()` object
86 and use `configure_file()`.
87
88 You can also substitute variables of type `@SOME_VAR@` with configure
89 data. The details are on the [configuration page](Configuration.md).
90
91 ### Finding programs
92
93 `configure.ac`:
94
95 ```autoconf
96 AC_PATH_PROG(GPERF, [gperf], [no])
97 if test x$GPERF != xno ; then
98 AC_DEFINE(HAVE_GPERF,[1], [Use gperf])
99 fi
100 AM_CONDITIONAL(HAVE_GPERF, [test x$GPERF != xno])
101 ```
102
103 `meson.build`:
104
105 ```meson
106 gperf = find_program('gperf', required : false)
107 if gperf.found()
108 conf.set('HAVE_GPERF', 1)
109 endif
110 ```
111
112 ### Finding pkg-config modules
113
114 `configure.ac`:
115
116 ```autoconf
117 PKG_CHECK_MODULES(SOUP, libsoup-2.4 >= 2.24)
118 ```
119
120 `meson.build`:
121
122 ```meson
123 soup = dependency('libsoup-2.4', version : '>= 2.24')
124 ```
125
126 ### Arguments
127
128 `configure.ac`:
129
130 ```autoconf
131 AC_ARG_ENABLE(dep11, AS_HELP_STRING([--enable-dep11],[enable DEP-11]),
132 enable_dep11=$enableval,enable_dep11=yes)
133 AM_CONDITIONAL(HAVE_DEP11, test x$enable_dep11 = xyes)
134 if test x$enable_dep11 = xyes; then
135 AC_CHECK_HEADER(yaml.h, [], [AC_MSG_ERROR([No yaml.h])])
136 YAML_LIBS="-lyaml"
137 AC_SUBST(YAML_LIBS)
138 AC_DEFINE(AS_BUILD_DEP11,1,[Build DEP-11 code])
139 fi
140 ```
141
142 `meson.build`:
143
144 ```meson
145 if get_option('enable-dep11')
146 yaml = dependency('yaml-0.1')
147 conf.set('AS_BUILD_DEP11', 1)
148 else
149 yaml = dependency('yaml-0.1', required : false)
150 endif
151 ```
152
153 `meson.options`:
154
155 ```meson
156 option('enable-dep11', type : 'boolean', value : true, description : 'enable DEP-11')
157 ```
158
159 ## Makefile.am
160
161 Next step is `Makefile.am`. In Meson you don't need to have other
162 file, you still use `meson.build`.
163
164 ### Sub directories
165
166 `Makefile.am`:
167
168 ```make
169 SUBDIRS = \
170 libappstream-glib
171 ```
172
173 `meson.build`:
174
175 ```meson
176 subdir('libappstream-glib')
177 ```
178
179 ### *CLEANFILES, EXTRA_DIST, etc.
180
181 `Makefile.am`:
182
183 ```make
184 DISTCLEANFILES = \
185 appstream-glib-*.tar.xz
186
187 MAINTAINERCLEANFILES = \
188 *~ \
189 ABOUT-NLS \
190 aclocal.m4 \
191 ChangeLog \
192 compile \
193 config.guess \
194 config.h.* \
195 config.rpath
196
197 EXTRA_DIST = \
198 COPYING \
199 MAINTAINERS \
200 AUTHORS \
201 README.md \
202 NEWS \
203 autogen.sh \
204 config.h
205 ```
206
207 In Meson you don't need have `*CLEANFILES`, because in Meson you are
208 building in temporary directory (usually called `build`), you manually
209 removing it. You also not need to use `EXTRA_DIST`, because you will
210 make tarballs via `git archive` or something like this.
211
212 ### glib-compile-resources
213
214 `Makefile.am`:
215 ```make
216 as-resources.c: appstream-glib.gresource.xml \
217 as-stock-icons.txt \
218 as-license-ids.txt \
219 as-blacklist-ids.txt \
220 as-category-ids.txt \
221 as-environment-ids.txt
222 $(AM_V_GEN) \
223 glib-compile-resources \
224 --sourcedir=$(srcdir) \
225 --sourcedir=$(top_builddir)/data \
226 --target=$@ \
227 --generate-source \
228 --c-name as \
229 $(srcdir)/appstream-glib.gresource.xml
230 as-resources.h: appstream-glib.gresource.xml \
231 as-stock-icons.txt \
232 as-license-ids.txt \
233 as-blacklist-ids.txt \
234 as-category-ids.txt \
235 as-environment-ids.txt
236 $(AM_V_GEN) \
237 glib-compile-resources \
238 --sourcedir=$(srcdir) \
239 --sourcedir=$(top_builddir)/data \
240 --target=$@ \
241 --generate-header \
242 --c-name as \
243 $(srcdir)/appstream-glib.gresource.xml
244
245 BUILT_SOURCES = \
246 as-resources.c \
247 as-resources.h
248 ```
249
250 `meson.build`:
251
252 ```meson
253 asresources = gnome.compile_resources(
254 'as-resources', 'appstream-glib.gresource.xml',
255 source_dir : '.',
256 c_name : 'as')
257 ```
258
259 ### Headers
260
261 `Makefile.am`:
262
263 ```make
264 libappstream_glib_includedir = $(includedir)/libappstream-glib
265 libappstream_glib_include_HEADERS = \
266 appstream-glib.h \
267 as-app.h \
268 as-bundle.h \
269 as-enums.h \
270 as-icon.h \
271 as-image.h \
272 as-inf.h \
273 as-node.h \
274 as-problem.h \
275 as-provide.h \
276 as-release.h \
277 as-screenshot.h \
278 as-store.h \
279 as-tag.h \
280 as-utils.h \
281 as-version.h
282 ```
283
284 `meson.build`:
285
286 ```meson
287 headers = [
288 'appstream-glib.h',
289 'as-app.h',
290 'as-bundle.h',
291 'as-enums.h',
292 'as-icon.h',
293 'as-image.h',
294 'as-inf.h',
295 'as-node.h',
296 'as-problem.h',
297 'as-provide.h',
298 'as-release.h',
299 'as-screenshot.h',
300 'as-store.h',
301 'as-tag.h',
302 'as-utils.h',
303 'as-version.h']
304 install_headers(headers, subdir : 'libappstream-glib')
305 ```
306
307 ### Libraries
308
309 `Makefile.am`:
310 ```make
311 lib_LTLIBRARIES = \
312 libappstream-glib.la
313 libappstream_glib_la_SOURCES = \
314 as-app.c \
315 as-app-desktop.c \
316 as-app-inf.c \
317 as-app-private.h \
318 as-app-validate.c \
319 as-bundle.c \
320 as-bundle-private.h \
321 as-cleanup.h \
322 as-enums.c \
323 as-icon.c \
324 as-icon-private.h \
325 as-image.c \
326 as-image-private.h \
327 as-inf.c \
328 as-inf.h \
329 as-node.c \
330 as-node-private.h \
331 as-problem.c \
332 as-problem.h \
333 as-provide.c \
334 as-provide-private.h \
335 as-release.c \
336 as-release-private.h \
337 as-resources.c \
338 as-resources.h \
339 as-screenshot.c \
340 as-screenshot-private.h \
341 as-store.c \
342 as-tag.c \
343 as-utils.c \
344 as-utils-private.h \
345 as-version.h \
346 as-yaml.c \
347 as-yaml.h
348
349 libappstream_glib_la_LIBADD = \
350 $(GLIB_LIBS) \
351 $(GDKPIXBUF_LIBS) \
352 $(LIBARCHIVE_LIBS) \
353 $(SOUP_LIBS) \
354 $(YAML_LIBS)
355
356 libappstream_glib_la_LDFLAGS = \
357 -version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE) \
358 -export-dynamic \
359 -no-undefined \
360 -export-symbols-regex '^as_.*'
361 ```
362
363 `meson.build`:
364
365 ```meson
366 sources = [
367 'as-app.c',
368 'as-app-desktop.c',
369 'as-app-inf.c',
370 'as-app-private.h',
371 'as-app-validate.c',
372 'as-bundle.c',
373 'as-bundle-private.h',
374 'as-cleanup.h',
375 'as-enums.c',
376 'as-icon.c',
377 'as-icon-private.h',
378 'as-image.c',
379 'as-image-private.h',
380 'as-inf.c',
381 'as-inf.h',
382 'as-node.c',
383 'as-node-private.h',
384 'as-problem.c',
385 'as-problem.h',
386 'as-provide.c',
387 'as-provide-private.h',
388 'as-release.c',
389 'as-release-private.h',
390 asresources,
391 'as-screenshot.c',
392 'as-screenshot-private.h',
393 'as-store.c',
394 'as-tag.c',
395 'as-utils.c',
396 'as-utils-private.h',
397 'as-version.h',
398 'as-yaml.c',
399 'as-yaml.h']
400
401 deps = [glib, gdkpixbuf, libarchive, soup, yaml]
402
403 mapfile = 'appstream-glib.map'
404 vflag = '-Wl,--version-script,@0@/@1@'.format(meson.current_source_dir(), mapfile)
405 asglib = shared_library(
406 'appstream-glib', sources,
407 soversion : lt_current,
408 version : lt_version,
409 dependencies : deps,
410 include_directories : include_directories('@0@/..'.format(meson.current_build_dir())),
411 link_args : ['-Wl,--no-undefined', vflag],
412 link_depends : mapfile,
413 install : true)
414 ```
415
416 `appstream-glib.map`:
417
418 ```
419 {
420 global:
421 as_*;
422 local:
423 *;
424 };
425 ```
426
427 ### Custom targets
428
429 `Makefile.am`:
430
431 ```make
432 if HAVE_GPERF
433 as-tag-private.h: as-tag.gperf
434 $(AM_V_GEN) gperf < $< > $@
435
436 libappstream_glib_la_SOURCES += as-tag-private.h
437 BUILT_SOURCES += as-tag-private.h
438 endif
439 ```
440
441 `meson.build`:
442
443 ```meson
444 if gperf.found()
445 astagpriv = custom_target('gperf as-tag',
446 output : 'as-tag-private.h',
447 input : 'as-tag.gperf',
448 command : [gperf, '@INPUT@', '--output-file', '@OUTPUT@'])
449 sources = sources + [astagpriv]
450 endif
451 ```
452
453 ### Global CFLAGS
454
455 `Makefile.am`:
456
457 ```make
458 AM_CPPFLAGS = \
459 -DAS_COMPILATION \
460 -DLOCALSTATEDIR=\""$(localstatedir)"\" \
461 -DG_LOG_DOMAIN=\"As\"
462 ```
463
464 `meson.build`:
465
466 ```meson
467 add_project_arguments('-DG_LOG_DOMAIN="As"', language : 'c')
468 add_project_arguments('-DAS_COMPILATION', language : 'c')
469 add_project_arguments('-DLOCALSTATEDIR="/var"', language : 'c')
470 ```
471
472 ### Tests
473
474 `Makefile.am`:
475
476 ```make
477 check_PROGRAMS = \
478 as-self-test
479 as_self_test_SOURCES = \
480 as-self-test.c
481 as_self_test_LDADD = \
482 $(GLIB_LIBS) \
483 $(GDKPIXBUF_LIBS) \
484 $(LIBARCHIVE_LIBS) \
485 $(SOUP_LIBS) \
486 $(YAML_LIBS) \
487 $(lib_LTLIBRARIES)
488 as_self_test_CFLAGS = -DTESTDATADIR=\""$(top_srcdir)/data/tests"\"
489
490 TESTS = as-self-test
491 ```
492
493 `meson.build`:
494
495 ```meson
496 selftest = executable(
497 'as-self-test', 'as-self-test.c',
498 include_directories : include_directories('@0@/..'.format(meson.current_build_dir())),
499 dependencies : deps,
500 c_args : '-DTESTDATADIR="@0@/../data/tests"'.format(meson.current_source_dir()),
501 link_with : asglib)
502 test('as-self-test', selftest)
503 ```
504
505 ### GObject Introspection
506
507 `Makefile.am`:
508
509 ```make
510 introspection_sources = \
511 as-app.c \
512 as-app-validate.c \
513 as-app.h \
514 as-bundle.c \
515 as-bundle.h \
516 as-enums.c \
517 as-enums.h \
518 as-icon.c \
519 as-icon.h \
520 as-image.c \
521 as-image.h \
522 as-inf.c \
523 as-inf.h \
524 as-node.c \
525 as-node.h \
526 as-problem.c \
527 as-problem.h \
528 as-provide.c \
529 as-provide.h \
530 as-release.c \
531 as-release.h \
532 as-screenshot.c \
533 as-screenshot.h \
534 as-store.c \
535 as-store.h \
536 as-tag.c \
537 as-tag.h \
538 as-utils.c \
539 as-utils.h \
540 as-version.h
541
542 AppStreamGlib-1.0.gir: libappstream-glib.la
543 AppStreamGlib_1_0_gir_INCLUDES = GObject-2.0 Gio-2.0 GdkPixbuf-2.0
544 AppStreamGlib_1_0_gir_CFLAGS = $(AM_CPPFLAGS)
545 AppStreamGlib_1_0_gir_SCANNERFLAGS = --identifier-prefix=As \
546 --symbol-prefix=as_ \
547 --warn-all \
548 --add-include-path=$(srcdir)
549 AppStreamGlib_1_0_gir_EXPORT_PACKAGES = appstream-glib
550 AppStreamGlib_1_0_gir_LIBS = libappstream-glib.la
551 AppStreamGlib_1_0_gir_FILES = $(introspection_sources)
552 INTROSPECTION_GIRS += AppStreamGlib-1.0.gir
553
554 girdir = $(datadir)/gir-1.0
555 gir_DATA = $(INTROSPECTION_GIRS)
556
557 typelibdir = $(libdir)/girepository-1.0
558 typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
559
560 CLEANFILES += $(gir_DATA) $(typelib_DATA)
561 ```
562
563 `meson.build`:
564
565 ```meson
566 introspection_sources = [
567 'as-app.c',
568 'as-app-validate.c',
569 'as-app.h',
570 'as-bundle.c',
571 'as-bundle.h',
572 'as-enums.c',
573 'as-enums.h',
574 'as-icon.c',
575 'as-icon.h',
576 'as-image.c',
577 'as-image.h',
578 'as-inf.c',
579 'as-inf.h',
580 'as-node.c',
581 'as-node.h',
582 'as-problem.c',
583 'as-problem.h',
584 'as-provide.c',
585 'as-provide.h',
586 'as-release.c',
587 'as-release.h',
588 'as-screenshot.c',
589 'as-screenshot.h',
590 'as-store.c',
591 'as-store.h',
592 'as-tag.c',
593 'as-tag.h',
594 'as-utils.c',
595 'as-utils.h',
596 'as-version.h']
597
598 gnome.generate_gir(asglib,
599 sources : introspection_sources,
600 nsversion : '1.0',
601 namespace : 'AppStreamGlib',
602 symbol_prefix : 'as_',
603 identifier_prefix : 'As',
604 export_packages : 'appstream-glib',
605 includes : ['GObject-2.0', 'Gio-2.0', 'GdkPixbuf-2.0'],
606 install : true
607 )
608 ```
609
610 ### GSettings
611
612 `configure.ac`:
613 ```sh
614 GLIB_GSETTINGS
615 ```
616
617 `Makefile.am`:
618 ```make
619 gsettings_SCHEMAS = foo.gschema.xml
620 @GSETTINGS_RULES@
621 ```
622
623 `meson.build`:
624 ```meson
625 install_data('foo.gschema.xml', install_dir: get_option('datadir') / 'glib-2.0' / 'schemas')
626 meson.add_install_script('meson_post_install.py')
627 ```
628
629 `meson_post_install.py`:
630 ```python
631 #!/usr/bin/env python3
632
633 import os
634 import subprocess
635
636 schemadir = os.path.join(os.environ['MESON_INSTALL_PREFIX'], 'share', 'glib-2.0', 'schemas')
637
638 if not os.environ.get('DESTDIR'):
639 print('Compiling gsettings schemas...')
640 subprocess.call(['glib-compile-schemas', schemadir])
641 ```
642
643 ### gettext
644
645 Note this example does not include `intltool` usage.
646
647 `configure.ac`:
648 ```m4
649 AM_GNU_GETTEXT([external])
650 AM_GNU_GETTEXT_VERSION([0.19.7])
651
652 GETTEXT_PACKAGE=foo
653 AC_SUBST(GETTEXT_PACKAGE)
654 AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", [The prefix for our gettext translation domains.])
655 ```
656
657 `po/Makevars`:
658 ```make
659 XGETTEXT_OPTIONS = --from-code=UTF-8 --keyword=_ --keyword=N_ --keyword=C_:1c,2 --keyword=NC_:1c,2 --keyword=g_dngettext:2,3 --add-comments
660 ```
661
662 `Makefile.am`:
663 ```make
664 %.desktop: %.desktop.in
665 $(AM_V_GEN)$(MSGFMT) --desktop --template $< -d $(top_srcdir)/po -o $@
666
667 %.appdata.xml: %.appdata.xml.in
668 $(AM_V_GEN)$(MSGFMT) --xml --template $< -d $(top_srcdir)/po -o $@
669 ```
670
671 `meson.build`:
672 ```meson
673 i18n = import('i18n')
674
675 gettext_package = 'foo'
676 add_project_arguments('-DGETTEXT_PACKAGE=' + gettext_package, language: 'c')
677 subdir('po')
678
679 i18n.merge_file(
680 input: 'foo.desktop.in',
681 output: 'foo.desktop',
682 type: 'desktop',
683 po_dir: 'po',
684 install: true,
685 install_dir: get_option('datadir') / 'applications'
686 )
687
688 i18n.merge_file(
689 input: 'foo.appdata.xml.in',
690 output: 'foo.appdata.xml',
691 po_dir: 'po',
692 install: true,
693 install_dir: get_option('datadir') / 'appdata'
694 )
695 ```
696
697 `po/meson.build`:
698 ```meson
699 i18n.gettext(gettext_package, preset: 'glib')
700 ```