Imported Upstream version 2.65.0
[platform/upstream/glib.git] / docs / reference / gobject / glib-mkenums.xml
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
36 extract enum definitions and produces enum descriptions based on text templates
37 specified by the user. Typically, you can use this tool to generate enumeration
38 types for the GType type system, for GObject properties and signal marshalling;
39 additionally, 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
43 input. The options specified control the text that generated, substituting various
44 keywords enclosed in <literal>@</literal> characters in the templates.
45 </para>
46
47 <refsect2><title>Production text substitutions</title>
48 <para>
49 Certain keywords enclosed in <literal>@</literal> characters will be substituted in the
50 emitted text. For the substitution examples of the keywords below,
51 the following example enum definition is assumed:
52 </para>
53 <informalexample><programlisting>
54 typedef 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>
64 The name of the enum currently being processed, enum names are assumed to be
65 properly namespaced and to use mixed capitalization to separate
66 words (e.g. <literal>PrefixTheXEnum</literal>).
67 </para></listitem>
68 </varlistentry>
69
70 <varlistentry>
71 <term><literal>@enum_name@</literal></term>
72 <listitem><para>
73 The 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>
81 The 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>
89 The enum name with words uppercase and word-separated by underscores,
90 prefix stripped (e.g. <literal>THE_XENUM</literal>).
91 </para></listitem>
92 </varlistentry>
93
94 <varlistentry>
95 <term><literal>@ENUMPREFIX@</literal></term>
96 <listitem><para>
97 The 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>
104 The enum value name currently being processed with words uppercase and
105 word-separated by underscores,
106 this 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>
114 A nick name for the enum value currently being processed, this is usually
115 generated by stripping common prefix words of all the enum values of the
116 current enum, the words are lowercase and underscores are substituted by a
117 minus (e.g. <literal>the-xvalue</literal>).
118 </para></listitem>
119 </varlistentry>
120
121 <varlistentry>
122 <term><literal>@valuenum@</literal></term>
123 <listitem><para>
124 The integer value for the enum value currently being processed. If the
125 evaluation fails then <command>glib-mkenums</command> will exit with an
126 error status, but this only happens if <literal>@valuenum@</literal>
127 appears 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>
134 This is substituted either by "enum" or "flags", depending on whether the
135 enum 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>
142 The 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>
149 The 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>
156 The 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>
163 The base name of the input file currently being processed (e.g. <literal>foo.h</literal>).
164 Typically you want to use <literal>@basename@</literal> in place of <literal>@filename@</literal>
165 in 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>
172 Some C comments are treated specially in the parsed enum definitions,
173 such comments start out with the trigraph sequence <literal>/*&lt;</literal>
174 and 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>
182 Indicates this enum definition should  be skipped.
183 </para></listitem>
184 </varlistentry>
185 <varlistentry>
186 <term><literal>flags</literal></term>
187 <listitem><para>
188 Indicates 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>
194 Specifies the word separation used in the <function>*_get_type()</function>
195 function. 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>
201 Specifies the version tag that will be used to substitute the <literal>@enumsince@</literal>
202 keyword 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>
213 Indicates the value should be skipped.
214 </para></listitem>
215 </varlistentry>
216 <varlistentry>
217 <term><literal>nick</literal></term>
218 <listitem><para>
219 Specifies the otherwise auto-generated nickname.
220 </para></listitem>
221 </varlistentry>
222 </variablelist>
223
224 <para>Examples:</para>
225 <informalexample><programlisting>
226 typedef enum /*&lt; skip &gt;*/
227 {
228   PREFIX_FOO
229 } PrefixThisEnumWillBeSkipped;
230 typedef 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>
247 Emits <replaceable>TEXT</replaceable> prior to processing input files.
248 </para>
249 <para>
250 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
251 will be concatenated.
252 </para>
253 <para>
254 When used along with a template file, <replaceable>TEXT</replaceable>
255 will 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>
262 Emits <replaceable>TEXT</replaceable> every time a new input file
263 is being processed.
264 </para>
265 <para>
266 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
267 will be concatenated.
268 </para>
269 <para>
270 When used along with a template file, <replaceable>TEXT</replaceable>
271 will 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>
278 Emits <replaceable>TEXT</replaceable> after all input files have been
279 processed.
280 </para>
281 <para>
282 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
283 will be concatenated.
284 </para>
285 <para>
286 When used along with a template file, <replaceable>TEXT</replaceable>
287 will 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>
294 Emits <replaceable>TEXT</replaceable> every time an enum is encountered
295 in the input files.
296 </para></listitem>
297 </varlistentry>
298
299 <varlistentry>
300 <term><option>--vhead</option> <replaceable>TEXT</replaceable></term>
301 <listitem><para>
302 Emits <replaceable>TEXT</replaceable> before iterating over the set of
303 values of an enum.
304 </para>
305 <para>
306 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
307 will be concatenated.
308 </para>
309 <para>
310 When used along with a template file, <replaceable>TEXT</replaceable>
311 will 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>
318 Emits <replaceable>TEXT</replaceable> for every value of an enum.
319 </para>
320 <para>
321 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
322 will be concatenated.
323 </para>
324 <para>
325 When used along with a template file, <replaceable>TEXT</replaceable>
326 will 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>
333 Emits <replaceable>TEXT</replaceable> after iterating over all values
334 of an enum.
335 </para>
336 <para>
337 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
338 will be concatenated.
339 </para>
340 <para>
341 When used along with a template file, <replaceable>TEXT</replaceable>
342 will 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>
349 Template 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>
357 Read templates from the given file. The templates are enclosed in
358 specially-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>
376 Indicates what portion of the enum name should be interpreted as the
377 prefix (eg, the "<literal>Gtk</literal>" in
378 "<literal>GtkDirectionType</literal>"). Normally this will be figured
379 out automatically, but you may need to override the default if your
380 namespace is capitalized oddly.
381 </para></listitem>
382 </varlistentry>
383
384 <varlistentry>
385 <term><option>--symbol-prefix</option> <replaceable>PREFIX</replaceable></term>
386 <listitem><para>
387 Indicates what prefix should be used to correspond to the identifier
388 prefix in related C function names (eg, the "<literal>gtk</literal>"
389 in "<literal>gtk_direction_type_get_type</literal>". Equivalently,
390 this is the lowercase version of the prefix component of the enum
391 value names (eg, the "<literal>GTK</literal>" in
392 "<literal>GTK_DIR_UP</literal>". The default value is the identifier
393 prefix, converted to lowercase.
394 </para></listitem>
395 </varlistentry>
396
397 <varlistentry>
398 <term><option>--help</option></term>
399 <listitem><para>
400 Print brief help and exit.
401 </para></listitem>
402 </varlistentry>
403
404 <varlistentry>
405 <term><option>--version</option></term>
406 <listitem><para>
407 Print version and exit.
408 </para></listitem>
409 </varlistentry>
410
411 <varlistentry>
412 <term><option>--output=FILE</option></term>
413 <listitem><para>
414 Write output to FILE instead of stdout.
415 </para></listitem>
416 </varlistentry>
417
418 <varlistentry>
419 <term><option>@RSPFILE</option></term>
420 <listitem><para>
421 When 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
423 limit. 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>
432 Instead of passing the various sections of the generated file to the command
433 line of <command>glib-mkenums</command>, it's strongly recommended to use a
434 template file, especially for generating C sources.
435 </para>
436
437 <para>
438 A 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
447 G_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 ***/
456 GType @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 ***/
461 G_END_DECLS
462 /*** END file-tail ***/
463 </programlisting></informalexample>
464
465 <para>
466 A 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 ***/
480 GType
481 @enum_name@_get_type (void)
482 {
483   static volatile gsize g_@type@_type_id__volatile;
484
485   if (g_once_init_enter (&amp;g_define_type_id__volatile))
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;g_@type@_type_id__volatile, g_@type@_type_id);
502     }
503   return g_@type@_type_id__volatile;
504 }
505
506 /*** END value-tail ***/
507 </programlisting></informalexample>
508
509 <para>
510 Template files are easier to modify and update, and can be used
511 to generate various types of outputs using the same command line
512 or tools during the build.
513 </para>
514 </refsect1>
515
516 <refsect1><title>Using glib-mkenums with Meson</title>
517 <para>
518 Meson supports generating enumeration types using <command>glib-mkenums</command>
519 out of the box in its "gnome" module.
520 </para>
521
522 <para>
523 In your <filename>meson.build</filename> file you will typically call the
524 <literal>gnome.mkenums_simple()</literal> method to generate idiomatic enumeration
525 types from a list of headers to inspect:
526 </para>
527 <informalexample><programlisting>
528 project_headers = [
529   'project-foo.h',
530   'project-bar.h',
531   'project-baz.h',
532 ]
533
534 gnome = import('gnome')
535 enum_files = gnome.mkenums_simple('enum-types',
536   sources: project_headers,
537 )
538 </programlisting></informalexample>
539
540 <para>
541 The <literal>enum_files</literal> variable will contain an array of two elements
542 in 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>
549 You should use the returned objects to provide a dependency on every other
550 build target that references the source or header file; for instance, if you
551 are using the source to build a library:
552 </para>
553 <informalexample><programlisting>
554 mainlib = library('project',
555   sources: project_sources + enum_files,
556   ...
557 )
558 </programlisting></informalexample>
559 <para>
560 Additionally, if you are including the generated header file inside a build
561 target that depends on the library you just built, you must ensure that the
562 internal dependency includes the generated header as a required source file:
563 </para>
564 <informalexample><programlisting>
565 mainlib_dep = declare_dependency(sources: enum_files[1], link_with: mainlib)
566 </programlisting></informalexample>
567 <para>
568 You should not include the generated source file as well, otherwise it will
569 be built separately for every target that depends on it, causing build
570 failures. 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">
572 corresponding Meson FAQ entry</ulink>.
573 </para>
574
575 <para>
576 If you are generating C header and source files that require special
577 templates, you can use <literal>gnome.mkenums()</literal> to provide those
578 headers, for instance:
579 </para>
580 <informalexample><programlisting>
581 enum_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>
589 For more information, see the <ulink url="https://mesonbuild.com/Gnome-module.html#gnomegenmarshal">Meson
590 documentation for <literal>gnome.mkenums()</literal></ulink>.
591 </para>
592 </refsect1>
593
594 <refsect1><title>Using glib-mkenums with Autotools</title>
595 <para>
596 In order to use <command>glib-mkenums</command> in your project when using
597 Autotools as the build system, you will first need to modify your
598 <filename>configure.ac</filename> file to ensure you find the appropriate
599 command using <command>pkg-config</command>, similarly as to how you discover
600 the compiler and linker flags for GLib.
601 </para>
602 <informalexample><programlisting>
603 PKG_PROG_PKG_CONFIG([0.28])
604
605 PKG_CHECK_VAR([GLIB_MKENUMS], [glib-2.0], [glib_mkenums])
606 </programlisting></informalexample>
607 <para>
608 In your <filename>Makefile.am</filename> file you will typically use rules
609 like these:
610 </para>
611 <informalexample><programlisting>
612 # A list of headers to inspect
613 project_headers = \
614         project-foo.h \
615         project-bar.h \
616         project-baz.h
617
618 enum-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
624 enum-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
631 BUILT_SOURCES += enum-types.h enum-types.c
632 CLEANFILES += enum-types.h enum-types.c
633 EXTRA_DIST += enum-types.h.in enum-types.c.in
634 </programlisting></informalexample>
635 <para>
636 In the example above, we have a variable called <literal>project_headers</literal>
637 where we reference all header files we want to inspect for generating enumeration
638 GTypes. In the <filename>enum-types.h</filename> rule we use <command>glib-mkenums</command>
639 with a template called <filename>enum-types.h.in</filename> in order to generate the
640 header file; similarly, in the <filename>enum-types.c</filename> rule we use a
641 template 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>