• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<refentry id="glib-mkenums" lang="en">
2
3<refentryinfo>
4  <title>gdbus</title>
5  <productname>GObject</productname>
6  <authorgroup>
7    <author>
8      <contrib>Developer</contrib>
9      <firstname>Owen</firstname>
10      <surname>Taylor</surname>
11    </author>
12  </authorgroup>
13</refentryinfo>
14
15<refmeta>
16<refentrytitle>glib-mkenums</refentrytitle>
17<manvolnum>1</manvolnum>
18<refmiscinfo class="manual">User Commands</refmiscinfo>
19</refmeta>
20
21<refnamediv>
22<refname>glib-mkenums</refname>
23<refpurpose>C language enum description generation utility</refpurpose>
24</refnamediv>
25
26<refsynopsisdiv>
27<cmdsynopsis>
28<command>glib-mkenums</command>
29<arg choice="opt" rep="repeat">OPTION</arg>
30<arg choice="opt" rep="repeat">FILE</arg>
31</cmdsynopsis>
32</refsynopsisdiv>
33
34<refsect1><title>Description</title>
35<para><command>glib-mkenums</command> is a small utility that parses C code to
36extract enum definitions and produces enum descriptions based on text templates
37specified by the user. Typically, you can use this tool to generate enumeration
38types for the GType type system, for GObject properties and signal marshalling;
39additionally, you can use it to generate enumeration values of GSettings schemas.
40</para>
41
42<para><command>glib-mkenums</command> takes a list of valid C code files as
43input. The options specified control the text that generated, substituting various
44keywords enclosed in <literal>@</literal> characters in the templates.
45</para>
46
47<refsect2><title>Production text substitutions</title>
48<para>
49Certain keywords enclosed in <literal>@</literal> characters will be substituted in the
50emitted text. For the substitution examples of the keywords below,
51the following example enum definition is assumed:
52</para>
53<informalexample><programlisting>
54typedef enum
55{
56  PREFIX_THE_XVALUE    = 1 &lt;&lt; 3,
57  PREFIX_ANOTHER_VALUE = 1 &lt;&lt; 4
58} PrefixTheXEnum;
59</programlisting></informalexample>
60<variablelist>
61<varlistentry>
62<term><literal>@EnumName@</literal>></term>
63<listitem><para>
64The name of the enum currently being processed, enum names are assumed to be
65properly namespaced and to use mixed capitalization to separate
66words (e.g. <literal>PrefixTheXEnum</literal>).
67</para></listitem>
68</varlistentry>
69
70<varlistentry>
71<term><literal>@enum_name@</literal></term>
72<listitem><para>
73The enum name with words lowercase and word-separated by underscores
74(e.g. <literal>prefix_the_xenum</literal>).
75</para></listitem>
76</varlistentry>
77
78<varlistentry>
79<term><literal>@ENUMNAME@</literal></term>
80<listitem><para>
81The enum name with words uppercase and word-separated by underscores
82(e.g. <literal>PREFIX_THE_XENUM</literal>).
83</para></listitem>
84</varlistentry>
85
86<varlistentry>
87<term><literal>@ENUMSHORT@</literal></term>
88<listitem><para>
89The enum name with words uppercase and word-separated by underscores,
90prefix stripped (e.g. <literal>THE_XENUM</literal>).
91</para></listitem>
92</varlistentry>
93
94<varlistentry>
95<term><literal>@ENUMPREFIX@</literal></term>
96<listitem><para>
97The prefix of the enum name (e.g. <literal>PREFIX</literal>).
98</para></listitem>
99</varlistentry>
100
101<varlistentry>
102<term><literal>@VALUENAME@</literal></term>
103<listitem><para>
104The enum value name currently being processed with words uppercase and
105word-separated by underscores,
106this is the assumed literal notation of enum values in the C sources
107(e.g. <literal>PREFIX_THE_XVALUE</literal>).
108</para></listitem>
109</varlistentry>
110
111<varlistentry>
112<term><literal>@valuenick@</literal></term>
113<listitem><para>
114A nick name for the enum value currently being processed, this is usually
115generated by stripping common prefix words of all the enum values of the
116current enum, the words are lowercase and underscores are substituted by a
117minus (e.g. <literal>the-xvalue</literal>).
118</para></listitem>
119</varlistentry>
120
121<varlistentry>
122<term><literal>@valuenum@</literal></term>
123<listitem><para>
124The integer value for the enum value currently being processed. If the
125evaluation fails then <command>glib-mkenums</command> will exit with an
126error status, but this only happens if <literal>@valuenum@</literal>
127appears in your value production template. (Since: 2.26)
128</para></listitem>
129</varlistentry>
130
131<varlistentry>
132<term><literal>@type@</literal></term>
133<listitem><para>
134This is substituted either by "enum" or "flags", depending on whether the
135enum value definitions contained bit-shift operators or not (e.g. <literal>flags</literal>).
136</para></listitem>
137</varlistentry>
138
139<varlistentry>
140<term><literal>@Type@</literal></term>
141<listitem><para>
142The same as <literal>@type@</literal> with the first letter capitalized (e.g. <literal>Flags</literal>).
143</para></listitem>
144</varlistentry>
145
146<varlistentry>
147<term><literal>@TYPE@</literal></term>
148<listitem><para>
149The same as <literal>@type@</literal> with all letters uppercased (e.g. <literal>FLAGS</literal>).
150</para></listitem>
151</varlistentry>
152
153<varlistentry>
154<term><literal>@filename@</literal></term>
155<listitem><para>
156The full path of the input file currently being processed (e.g. <literal>/build/environment/project/src/foo.h</literal>).
157</para></listitem>
158</varlistentry>
159
160<varlistentry>
161<term><literal>@basename@</literal></term>
162<listitem><para>
163The base name of the input file currently being processed (e.g. <literal>foo.h</literal>).
164Typically you want to use <literal>@basename@</literal> in place of <literal>@filename@</literal>
165in your templates, to improve the reproducibility of the build. (Since: 2.22)
166</para></listitem>
167</varlistentry>
168</variablelist>
169</refsect2>
170<refsect2><title>Trigraph extensions</title>
171<para>
172Some C comments are treated specially in the parsed enum definitions,
173such comments start out with the trigraph sequence <literal>/*&lt;</literal>
174and end with the trigraph sequence <literal>&gt;*/</literal>.
175</para>
176
177<para>The following options can be specified per enum definition:</para>
178<variablelist>
179<varlistentry>
180<term><literal>skip</literal></term>
181<listitem><para>
182Indicates this enum definition should  be skipped.
183</para></listitem>
184</varlistentry>
185<varlistentry>
186<term><literal>flags</literal></term>
187<listitem><para>
188Indicates this enum should be treated as a flags definition.
189</para></listitem>
190</varlistentry>
191<varlistentry>
192<term><literal>underscore_name</literal></term>
193<listitem><para>
194Specifies the word separation used in the <function>*_get_type()</function>
195function. For instance, <literal>/*&lt; underscore_name=gnome_vfs_uri_hide_options &gt;*/</literal>.
196</para></listitem>
197</varlistentry>
198<varlistentry>
199<term><literal>since</literal></term>
200<listitem><para>
201Specifies the version tag that will be used to substitute the <literal>@enumsince@</literal>
202keyword in the template, useful when documenting methods generated from the enums
203(e.g. <literal>Since: @enumsince@</literal>). (Since: 2.66)
204</para></listitem>
205</varlistentry>
206</variablelist>
207
208<para>The following options can be specified per value definition:</para>
209<variablelist>
210<varlistentry>
211<term><literal>skip</literal></term>
212<listitem><para>
213Indicates the value should be skipped.
214</para></listitem>
215</varlistentry>
216<varlistentry>
217<term><literal>nick</literal></term>
218<listitem><para>
219Specifies the otherwise auto-generated nickname.
220</para></listitem>
221</varlistentry>
222</variablelist>
223
224<para>Examples:</para>
225<informalexample><programlisting>
226typedef enum /*&lt; skip &gt;*/
227{
228  PREFIX_FOO
229} PrefixThisEnumWillBeSkipped;
230typedef enum /*&lt; flags,prefix=PREFIX,since=1.0 &gt;*/
231{
232  PREFIX_THE_ZEROTH_VALUE,	/*&lt; skip &gt;*/
233  PREFIX_THE_FIRST_VALUE,
234  PREFIX_THE_SECOND_VALUE,
235  PREFIX_THE_THIRD_VALUE,	/*&lt; nick=the-last-value &gt;*/
236} PrefixTheFlagsEnum;
237</programlisting></informalexample>
238</refsect2>
239</refsect1>
240
241<refsect1><title>Options</title>
242<variablelist>
243
244<varlistentry>
245<term><option>--fhead</option> <replaceable>TEXT</replaceable></term>
246<listitem><para>
247Emits <replaceable>TEXT</replaceable> prior to processing input files.
248</para>
249<para>
250You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
251will be concatenated.
252</para>
253<para>
254When used along with a template file, <replaceable>TEXT</replaceable>
255will be prepended to the template's <literal>file-header</literal> section.
256</para></listitem>
257</varlistentry>
258
259<varlistentry>
260<term><option>--fprod</option> <replaceable>TEXT</replaceable></term>
261<listitem><para>
262Emits <replaceable>TEXT</replaceable> every time a new input file
263is being processed.
264</para>
265<para>
266You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
267will be concatenated.
268</para>
269<para>
270When used along with a template file, <replaceable>TEXT</replaceable>
271will be appended to the template's <literal>file-production</literal> section.
272</para></listitem>
273</varlistentry>
274
275<varlistentry>
276<term><option>--ftail</option> <replaceable>TEXT</replaceable></term>
277<listitem><para>
278Emits <replaceable>TEXT</replaceable> after all input files have been
279processed.
280</para>
281<para>
282You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
283will be concatenated.
284</para>
285<para>
286When used along with a template file, <replaceable>TEXT</replaceable>
287will be appended to the template's <literal>file-tail</literal> section.
288</para></listitem>
289</varlistentry>
290
291<varlistentry>
292<term><option>--eprod</option> <replaceable>TEXT</replaceable></term>
293<listitem><para>
294Emits <replaceable>TEXT</replaceable> every time an enum is encountered
295in the input files.
296</para></listitem>
297</varlistentry>
298
299<varlistentry>
300<term><option>--vhead</option> <replaceable>TEXT</replaceable></term>
301<listitem><para>
302Emits <replaceable>TEXT</replaceable> before iterating over the set of
303values of an enum.
304</para>
305<para>
306You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
307will be concatenated.
308</para>
309<para>
310When used along with a template file, <replaceable>TEXT</replaceable>
311will be prepended to the template's <literal>value-header</literal> section.
312</para></listitem>
313</varlistentry>
314
315<varlistentry>
316<term><option>--vprod</option> <replaceable>TEXT</replaceable></term>
317<listitem><para>
318Emits <replaceable>TEXT</replaceable> for every value of an enum.
319</para>
320<para>
321You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
322will be concatenated.
323</para>
324<para>
325When used along with a template file, <replaceable>TEXT</replaceable>
326will be appended to the template's <literal>value-production</literal> section.
327</para></listitem>
328</varlistentry>
329
330<varlistentry>
331<term><option>--vtail</option> <replaceable>TEXT</replaceable></term>
332<listitem><para>
333Emits <replaceable>TEXT</replaceable> after iterating over all values
334of an enum.
335</para>
336<para>
337You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
338will be concatenated.
339</para>
340<para>
341When used along with a template file, <replaceable>TEXT</replaceable>
342will be appended to the template's <literal>value-tail</literal> section.
343</para></listitem>
344</varlistentry>
345
346<varlistentry>
347<term><option>--comments</option> <replaceable>TEXT</replaceable></term>
348<listitem><para>
349Template for auto-generated comments, the default (for C code generations) is
350<literal>"/* @comment@ */"</literal>.
351</para></listitem>
352</varlistentry>
353
354<varlistentry>
355<term><option>--template</option> <replaceable>FILE</replaceable></term>
356<listitem><para>
357Read templates from the given file. The templates are enclosed in
358specially-formatted C comments:
359</para>
360<informalexample><programlisting>
361/*** BEGIN section ***/
362/*** END section ***/
363</programlisting></informalexample>
364<para>
365<replaceable>section</replaceable> may be <literal>file-header</literal>,
366<literal>file-production</literal>, <literal>file-tail</literal>,
367<literal>enumeration-production</literal>, <literal>value-header</literal>,
368<literal>value-production</literal>, <literal>value-tail</literal> or
369<literal>comment</literal>.
370</para></listitem>
371</varlistentry>
372
373<varlistentry>
374<term><option>--identifier-prefix</option> <replaceable>PREFIX</replaceable></term>
375<listitem><para>
376Indicates what portion of the enum name should be interpreted as the
377prefix (eg, the "<literal>Gtk</literal>" in
378"<literal>GtkDirectionType</literal>"). Normally this will be figured
379out automatically, but you may need to override the default if your
380namespace is capitalized oddly.
381</para></listitem>
382</varlistentry>
383
384<varlistentry>
385<term><option>--symbol-prefix</option> <replaceable>PREFIX</replaceable></term>
386<listitem><para>
387Indicates what prefix should be used to correspond to the identifier
388prefix in related C function names (eg, the "<literal>gtk</literal>"
389in "<literal>gtk_direction_type_get_type</literal>". Equivalently,
390this is the lowercase version of the prefix component of the enum
391value names (eg, the "<literal>GTK</literal>" in
392"<literal>GTK_DIR_UP</literal>". The default value is the identifier
393prefix, converted to lowercase.
394</para></listitem>
395</varlistentry>
396
397<varlistentry>
398<term><option>--help</option></term>
399<listitem><para>
400Print brief help and exit.
401</para></listitem>
402</varlistentry>
403
404<varlistentry>
405<term><option>--version</option></term>
406<listitem><para>
407Print version and exit.
408</para></listitem>
409</varlistentry>
410
411<varlistentry>
412<term><option>--output=FILE</option></term>
413<listitem><para>
414Write output to FILE instead of stdout.
415</para></listitem>
416</varlistentry>
417
418<varlistentry>
419<term><option>@RSPFILE</option></term>
420<listitem><para>
421When passed as the sole argument, read and parse the actual arguments from
422<literal>RSPFILE</literal>. Useful on systems with a low command-line length
423limit. For example, Windows has a limit of 8191 characters.
424</para></listitem>
425</varlistentry>
426
427</variablelist>
428</refsect1>
429
430<refsect1><title>Using templates</title>
431<para>
432Instead of passing the various sections of the generated file to the command
433line of <command>glib-mkenums</command>, it's strongly recommended to use a
434template file, especially for generating C sources.
435</para>
436
437<para>
438A C header template file will typically look like this:
439</para>
440<informalexample><programlisting>
441/*** BEGIN file-header ***/
442#pragma once
443
444/* Include the main project header */
445#include "project.h"
446
447G_BEGIN_DECLS
448/*** END file-header ***/
449
450/*** BEGIN file-production ***/
451
452/* enumerations from "@basename@" */
453/*** END file-production ***/
454
455/*** BEGIN value-header ***/
456GType @enum_name@_get_type (void) G_GNUC_CONST;
457#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ())
458/*** END value-header ***/
459
460/*** BEGIN file-tail ***/
461G_END_DECLS
462/*** END file-tail ***/
463</programlisting></informalexample>
464
465<para>
466A C source template file will typically look like this:
467</para>
468<informalexample><programlisting>
469/*** BEGIN file-header ***/
470#include "config.h"
471#include "enum-types.h"
472
473/*** END file-header ***/
474
475/*** BEGIN file-production ***/
476/* enumerations from "@basename@" */
477/*** END file-production ***/
478
479/*** BEGIN value-header ***/
480GType
481@enum_name@_get_type (void)
482{
483  static gsize static_g_@type@_type_id;
484
485  if (g_once_init_enter (&amp;static_g_@type@_type_id))
486    {
487      static const G@Type@Value values[] = {
488/*** END value-header ***/
489
490/*** BEGIN value-production ***/
491            { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
492/*** END value-production ***/
493
494/*** BEGIN value-tail ***/
495            { 0, NULL, NULL }
496      };
497
498      GType g_@type@_type_id =
499        g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
500
501      g_once_init_leave (&amp;static_g_@type@_type_id, g_@type@_type_id);
502    }
503  return static_g_@type@_type_id;
504}
505
506/*** END value-tail ***/
507</programlisting></informalexample>
508
509<para>
510Template files are easier to modify and update, and can be used
511to generate various types of outputs using the same command line
512or tools during the build.
513</para>
514</refsect1>
515
516<refsect1><title>Using glib-mkenums with Meson</title>
517<para>
518Meson supports generating enumeration types using <command>glib-mkenums</command>
519out of the box in its "gnome" module.
520</para>
521
522<para>
523In your <filename>meson.build</filename> file you will typically call the
524<literal>gnome.mkenums_simple()</literal> method to generate idiomatic enumeration
525types from a list of headers to inspect:
526</para>
527<informalexample><programlisting>
528project_headers = [
529  'project-foo.h',
530  'project-bar.h',
531  'project-baz.h',
532]
533
534gnome = import('gnome')
535enum_files = gnome.mkenums_simple('enum-types',
536  sources: project_headers,
537)
538</programlisting></informalexample>
539
540<para>
541The <literal>enum_files</literal> variable will contain an array of two elements
542in the following order:
543</para>
544<itemizedlist>
545  <listitem><para>a build target for the source file</para></listitem>
546  <listitem><para>a build target for the header file</para></listitem>
547</itemizedlist>
548<para>
549You should use the returned objects to provide a dependency on every other
550build target that references the source or header file; for instance, if you
551are using the source to build a library:
552</para>
553<informalexample><programlisting>
554mainlib = library('project',
555  sources: project_sources + enum_files,
556  ...
557)
558</programlisting></informalexample>
559<para>
560Additionally, if you are including the generated header file inside a build
561target that depends on the library you just built, you must ensure that the
562internal dependency includes the generated header as a required source file:
563</para>
564<informalexample><programlisting>
565mainlib_dep = declare_dependency(sources: enum_files[1], link_with: mainlib)
566</programlisting></informalexample>
567<para>
568You should not include the generated source file as well, otherwise it will
569be built separately for every target that depends on it, causing build
570failures. To know more about why all this is required, please refer to the
571<ulink url="https://mesonbuild.com/FAQ.html#how-do-i-tell-meson-that-my-sources-use-generated-headers">
572corresponding Meson FAQ entry</ulink>.
573</para>
574
575<para>
576If you are generating C header and source files that require special
577templates, you can use <literal>gnome.mkenums()</literal> to provide those
578headers, for instance:
579</para>
580<informalexample><programlisting>
581enum_files = gnome.mkenums('enum-types',
582  sources: project_headers,
583  h_template: 'enum-types.h.in',
584  c_template: 'enum-types.c.in',
585  install_header: true,
586)
587</programlisting></informalexample>
588<para>
589For more information, see the <ulink url="https://mesonbuild.com/Gnome-module.html#gnomegenmarshal">Meson
590documentation for <literal>gnome.mkenums()</literal></ulink>.
591</para>
592</refsect1>
593
594<refsect1><title>Using glib-mkenums with Autotools</title>
595<para>
596In order to use <command>glib-mkenums</command> in your project when using
597Autotools as the build system, you will first need to modify your
598<filename>configure.ac</filename> file to ensure you find the appropriate
599command using <command>pkg-config</command>, similarly as to how you discover
600the compiler and linker flags for GLib.
601</para>
602<informalexample><programlisting>
603PKG_PROG_PKG_CONFIG([0.28])
604
605PKG_CHECK_VAR([GLIB_MKENUMS], [glib-2.0], [glib_mkenums])
606</programlisting></informalexample>
607<para>
608In your <filename>Makefile.am</filename> file you will typically use rules
609like these:
610</para>
611<informalexample><programlisting>
612# A list of headers to inspect
613project_headers = \
614        project-foo.h \
615        project-bar.h \
616        project-baz.h
617
618enum-types.h: $(project_headers) enum-types.h.in
619        $(AM_V_GEN)$(GLIB_MKENUMS) \
620                --template=enum-types.h.in \
621                --output=$@ \
622                $(project_headers)
623
624enum-types.c: $(project_headers) enum-types.c.in enum-types.h
625        $(AM_V_GEN)$(GLIB_MKENUMS) \
626                --template=enum-types.c.in \
627                --output=$@ \
628                $(project_headers)
629
630# Build the enum types files before every other target
631BUILT_SOURCES += enum-types.h enum-types.c
632CLEANFILES += enum-types.h enum-types.c
633EXTRA_DIST += enum-types.h.in enum-types.c.in
634</programlisting></informalexample>
635<para>
636In the example above, we have a variable called <literal>project_headers</literal>
637where we reference all header files we want to inspect for generating enumeration
638GTypes. In the <filename>enum-types.h</filename> rule we use <command>glib-mkenums</command>
639with a template called <filename>enum-types.h.in</filename> in order to generate the
640header file; similarly, in the <filename>enum-types.c</filename> rule we use a
641template called <filename>enum-types.c.in</filename>.
642</para>
643</refsect1>
644
645<refsect1><title>See also</title>
646<para>
647<citerefentry>
648<refentrytitle>glib-genmarshal</refentrytitle>
649<manvolnum>1</manvolnum>
650</citerefentry>
651</para>
652</refsect1>
653</refentry>
654