1 <refentry id="glib-mkenums" lang="en">
5 <productname>GObject</productname>
8 <contrib>Developer</contrib>
9 <firstname>Owen</firstname>
10 <surname>Taylor</surname>
16 <refentrytitle>glib-mkenums</refentrytitle>
17 <manvolnum>1</manvolnum>
18 <refmiscinfo class="manual">User Commands</refmiscinfo>
22 <refname>glib-mkenums</refname>
23 <refpurpose>C language enum description generation utility</refpurpose>
28 <command>glib-mkenums</command>
29 <arg choice="opt" rep="repeat">OPTION</arg>
30 <arg choice="opt" rep="repeat">FILE</arg>
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.
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.
47 <refsect2><title>Production text substitutions</title>
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:
53 <informalexample><programlisting>
56 PREFIX_THE_XVALUE = 1 << 3,
57 PREFIX_ANOTHER_VALUE = 1 << 4
59 </programlisting></informalexample>
62 <term><literal>@EnumName@</literal>></term>
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>).
71 <term><literal>@enum_name@</literal></term>
73 The enum name with words lowercase and word-separated by underscores
74 (e.g. <literal>prefix_the_xenum</literal>).
79 <term><literal>@ENUMNAME@</literal></term>
81 The enum name with words uppercase and word-separated by underscores
82 (e.g. <literal>PREFIX_THE_XENUM</literal>).
87 <term><literal>@ENUMSHORT@</literal></term>
89 The enum name with words uppercase and word-separated by underscores,
90 prefix stripped (e.g. <literal>THE_XENUM</literal>).
95 <term><literal>@ENUMPREFIX@</literal></term>
97 The prefix of the enum name (e.g. <literal>PREFIX</literal>).
102 <term><literal>@VALUENAME@</literal></term>
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>).
112 <term><literal>@valuenick@</literal></term>
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>).
122 <term><literal>@valuenum@</literal></term>
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)
132 <term><literal>@type@</literal></term>
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>).
140 <term><literal>@Type@</literal></term>
142 The same as <literal>@type@</literal> with the first letter capitalized (e.g. <literal>Flags</literal>).
147 <term><literal>@TYPE@</literal></term>
149 The same as <literal>@type@</literal> with all letters uppercased (e.g. <literal>FLAGS</literal>).
154 <term><literal>@filename@</literal></term>
156 The full path of the input file currently being processed (e.g. <literal>/build/environment/project/src/foo.h</literal>).
161 <term><literal>@basename@</literal></term>
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)
170 <refsect2><title>Trigraph extensions</title>
172 Some C comments are treated specially in the parsed enum definitions,
173 such comments start out with the trigraph sequence <literal>/*<</literal>
174 and end with the trigraph sequence <literal>>*/</literal>.
177 <para>The following options can be specified per enum definition:</para>
180 <term><literal>skip</literal></term>
182 Indicates this enum definition should be skipped.
186 <term><literal>flags</literal></term>
188 Indicates this enum should be treated as a flags definition.
192 <term><literal>underscore_name</literal></term>
194 Specifies the word separation used in the <function>*_get_type()</function>
195 function. For instance, <literal>/*< underscore_name=gnome_vfs_uri_hide_options >*/</literal>.
199 <term><literal>since</literal></term>
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)
208 <para>The following options can be specified per value definition:</para>
211 <term><literal>skip</literal></term>
213 Indicates the value should be skipped.
217 <term><literal>nick</literal></term>
219 Specifies the otherwise auto-generated nickname.
224 <para>Examples:</para>
225 <informalexample><programlisting>
226 typedef enum /*< skip >*/
229 } PrefixThisEnumWillBeSkipped;
230 typedef enum /*< flags,prefix=PREFIX,since=1.0 >*/
232 PREFIX_THE_ZEROTH_VALUE, /*< skip >*/
233 PREFIX_THE_FIRST_VALUE,
234 PREFIX_THE_SECOND_VALUE,
235 PREFIX_THE_THIRD_VALUE, /*< nick=the-last-value >*/
236 } PrefixTheFlagsEnum;
237 </programlisting></informalexample>
241 <refsect1><title>Options</title>
245 <term><option>--fhead</option> <replaceable>TEXT</replaceable></term>
247 Emits <replaceable>TEXT</replaceable> prior to processing input files.
250 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
251 will be concatenated.
254 When used along with a template file, <replaceable>TEXT</replaceable>
255 will be prepended to the template's <literal>file-header</literal> section.
260 <term><option>--fprod</option> <replaceable>TEXT</replaceable></term>
262 Emits <replaceable>TEXT</replaceable> every time a new input file
266 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
267 will be concatenated.
270 When used along with a template file, <replaceable>TEXT</replaceable>
271 will be appended to the template's <literal>file-production</literal> section.
276 <term><option>--ftail</option> <replaceable>TEXT</replaceable></term>
278 Emits <replaceable>TEXT</replaceable> after all input files have been
282 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
283 will be concatenated.
286 When used along with a template file, <replaceable>TEXT</replaceable>
287 will be appended to the template's <literal>file-tail</literal> section.
292 <term><option>--eprod</option> <replaceable>TEXT</replaceable></term>
294 Emits <replaceable>TEXT</replaceable> every time an enum is encountered
300 <term><option>--vhead</option> <replaceable>TEXT</replaceable></term>
302 Emits <replaceable>TEXT</replaceable> before iterating over the set of
306 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
307 will be concatenated.
310 When used along with a template file, <replaceable>TEXT</replaceable>
311 will be prepended to the template's <literal>value-header</literal> section.
316 <term><option>--vprod</option> <replaceable>TEXT</replaceable></term>
318 Emits <replaceable>TEXT</replaceable> for every value of an enum.
321 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
322 will be concatenated.
325 When used along with a template file, <replaceable>TEXT</replaceable>
326 will be appended to the template's <literal>value-production</literal> section.
331 <term><option>--vtail</option> <replaceable>TEXT</replaceable></term>
333 Emits <replaceable>TEXT</replaceable> after iterating over all values
337 You can specify this option multiple times, and the <replaceable>TEXT</replaceable>
338 will be concatenated.
341 When used along with a template file, <replaceable>TEXT</replaceable>
342 will be appended to the template's <literal>value-tail</literal> section.
347 <term><option>--comments</option> <replaceable>TEXT</replaceable></term>
349 Template for auto-generated comments, the default (for C code generations) is
350 <literal>"/* @comment@ */"</literal>.
355 <term><option>--template</option> <replaceable>FILE</replaceable></term>
357 Read templates from the given file. The templates are enclosed in
358 specially-formatted C comments:
360 <informalexample><programlisting>
361 /*** BEGIN section ***/
362 /*** END section ***/
363 </programlisting></informalexample>
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>.
374 <term><option>--identifier-prefix</option> <replaceable>PREFIX</replaceable></term>
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.
385 <term><option>--symbol-prefix</option> <replaceable>PREFIX</replaceable></term>
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.
398 <term><option>--help</option></term>
400 Print brief help and exit.
405 <term><option>--version</option></term>
407 Print version and exit.
412 <term><option>--output=FILE</option></term>
414 Write output to FILE instead of stdout.
419 <term><option>@RSPFILE</option></term>
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.
430 <refsect1><title>Using templates</title>
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.
438 A C header template file will typically look like this:
440 <informalexample><programlisting>
441 /*** BEGIN file-header ***/
444 /* Include the main project header */
448 /*** END file-header ***/
450 /*** BEGIN file-production ***/
452 /* enumerations from "@basename@" */
453 /*** END file-production ***/
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 ***/
460 /*** BEGIN file-tail ***/
462 /*** END file-tail ***/
463 </programlisting></informalexample>
466 A C source template file will typically look like this:
468 <informalexample><programlisting>
469 /*** BEGIN file-header ***/
471 #include "enum-types.h"
473 /*** END file-header ***/
475 /*** BEGIN file-production ***/
476 /* enumerations from "@basename@" */
477 /*** END file-production ***/
479 /*** BEGIN value-header ***/
481 @enum_name@_get_type (void)
483 static volatile gsize g_@type@_type_id__volatile;
485 if (g_once_init_enter (&g_define_type_id__volatile))
487 static const G@Type@Value values[] = {
488 /*** END value-header ***/
490 /*** BEGIN value-production ***/
491 { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
492 /*** END value-production ***/
494 /*** BEGIN value-tail ***/
498 GType g_@type@_type_id =
499 g_@type@_register_static (g_intern_static_string ("@EnumName@"), values);
501 g_once_init_leave (&g_@type@_type_id__volatile, g_@type@_type_id);
503 return g_@type@_type_id__volatile;
506 /*** END value-tail ***/
507 </programlisting></informalexample>
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.
516 <refsect1><title>Using glib-mkenums with Meson</title>
518 Meson supports generating enumeration types using <command>glib-mkenums</command>
519 out of the box in its "gnome" module.
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:
527 <informalexample><programlisting>
534 gnome = import('gnome')
535 enum_files = gnome.mkenums_simple('enum-types',
536 sources: project_headers,
538 </programlisting></informalexample>
541 The <literal>enum_files</literal> variable will contain an array of two elements
542 in the following order:
545 <listitem><para>a build target for the source file</para></listitem>
546 <listitem><para>a build target for the header file</para></listitem>
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:
553 <informalexample><programlisting>
554 mainlib = library('project',
555 sources: project_sources + enum_files,
558 </programlisting></informalexample>
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:
564 <informalexample><programlisting>
565 mainlib_dep = declare_dependency(sources: enum_files[1], link_with: mainlib)
566 </programlisting></informalexample>
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>.
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:
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,
587 </programlisting></informalexample>
589 For more information, see the <ulink url="https://mesonbuild.com/Gnome-module.html#gnomegenmarshal">Meson
590 documentation for <literal>gnome.mkenums()</literal></ulink>.
594 <refsect1><title>Using glib-mkenums with Autotools</title>
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.
602 <informalexample><programlisting>
603 PKG_PROG_PKG_CONFIG([0.28])
605 PKG_CHECK_VAR([GLIB_MKENUMS], [glib-2.0], [glib_mkenums])
606 </programlisting></informalexample>
608 In your <filename>Makefile.am</filename> file you will typically use rules
611 <informalexample><programlisting>
612 # A list of headers to inspect
618 enum-types.h: $(project_headers) enum-types.h.in
619 $(AM_V_GEN)$(GLIB_MKENUMS) \
620 --template=enum-types.h.in \
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 \
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>
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>.
645 <refsect1><title>See also</title>
648 <refentrytitle>glib-genmarshal</refentrytitle>
649 <manvolnum>1</manvolnum>