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 << 3, 57 PREFIX_ANOTHER_VALUE = 1 << 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 name of the input file currently being processed (e.g. <literal>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>). Typically 164you want to use <literal>@basename@</literal> in place of <literal>@filename@</literal> in your templates, to improve the reproducibility of the build. (Since: 2.22) 165</para></listitem> 166</varlistentry> 167</variablelist> 168</refsect2> 169<refsect2><title>Trigraph extensions</title> 170<para> 171Some C comments are treated specially in the parsed enum definitions, 172such comments start out with the trigraph sequence <literal>/*<</literal> 173and end with the trigraph sequence <literal>>*/</literal>. 174Per enum definition, the options <literal>skip</literal> and <literal>flags</literal> can be specified, to 175indicate this enum definition to be skipped, or for it to be treated as 176a flags definition, or to specify the common prefix to be stripped from 177all values to generate value nicknames, respectively. The <literal>underscore_name</literal> 178option can be used to specify the word separation used in the <function>*_get_type()</function> 179function. For instance, <literal>/*< underscore_name=gnome_vfs_uri_hide_options >*/</literal>. 180</para> 181<para> 182Per value definition, the options <literal>skip</literal> and <literal>nick</literal> are supported. 183The former causes the value to be skipped, and the latter can be used to 184specify the otherwise auto-generated nickname. 185Examples: 186</para> 187<informalexample><programlisting> 188typedef enum /*< skip >*/ 189{ 190 PREFIX_FOO 191} PrefixThisEnumWillBeSkipped; 192typedef enum /*< flags,prefix=PREFIX >*/ 193{ 194 PREFIX_THE_ZEROTH_VALUE, /*< skip >*/ 195 PREFIX_THE_FIRST_VALUE, 196 PREFIX_THE_SECOND_VALUE, 197 PREFIX_THE_THIRD_VALUE, /*< nick=the-last-value >*/ 198} PrefixTheFlagsEnum; 199</programlisting></informalexample> 200</refsect2> 201</refsect1> 202 203<refsect1><title>Options</title> 204<variablelist> 205 206<varlistentry> 207<term><option>--fhead</option> <replaceable>TEXT</replaceable></term> 208<listitem><para> 209Emits <replaceable>TEXT</replaceable> prior to processing input files. 210</para> 211<para> 212You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 213will be concatenated. 214</para> 215<para> 216When used along with a template file, <replaceable>TEXT</replaceable> 217will be prepended to the template's <literal>file-header</literal> section. 218</para></listitem> 219</varlistentry> 220 221<varlistentry> 222<term><option>--fprod</option> <replaceable>TEXT</replaceable></term> 223<listitem><para> 224Emits <replaceable>TEXT</replaceable> every time a new input file 225is being processed. 226</para> 227<para> 228You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 229will be concatenated. 230</para> 231<para> 232When used along with a template file, <replaceable>TEXT</replaceable> 233will be appended to the template's <literal>file-production</literal> section. 234</para></listitem> 235</varlistentry> 236 237<varlistentry> 238<term><option>--ftail</option> <replaceable>TEXT</replaceable></term> 239<listitem><para> 240Emits <replaceable>TEXT</replaceable> after all input files have been 241processed. 242</para> 243<para> 244You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 245will be concatenated. 246</para> 247<para> 248When used along with a template file, <replaceable>TEXT</replaceable> 249will be appended to the template's <literal>file-tail</literal> section. 250</para></listitem> 251</varlistentry> 252 253<varlistentry> 254<term><option>--eprod</option> <replaceable>TEXT</replaceable></term> 255<listitem><para> 256Emits <replaceable>TEXT</replaceable> every time an enum is encountered 257in the input files. 258</para></listitem> 259</varlistentry> 260 261<varlistentry> 262<term><option>--vhead</option> <replaceable>TEXT</replaceable></term> 263<listitem><para> 264Emits <replaceable>TEXT</replaceable> before iterating over the set of 265values of an enum. 266</para> 267<para> 268You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 269will be concatenated. 270</para> 271<para> 272When used along with a template file, <replaceable>TEXT</replaceable> 273will be prepended to the template's <literal>value-header</literal> section. 274</para></listitem> 275</varlistentry> 276 277<varlistentry> 278<term><option>--vprod</option> <replaceable>TEXT</replaceable></term> 279<listitem><para> 280Emits <replaceable>TEXT</replaceable> for every value of an enum. 281</para> 282<para> 283You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 284will be concatenated. 285</para> 286<para> 287When used along with a template file, <replaceable>TEXT</replaceable> 288will be appended to the template's <literal>value-production</literal> section. 289</para></listitem> 290</varlistentry> 291 292<varlistentry> 293<term><option>--vtail</option> <replaceable>TEXT</replaceable></term> 294<listitem><para> 295Emits <replaceable>TEXT</replaceable> after iterating over all values 296of an enum. 297</para> 298<para> 299You can specify this option multiple times, and the <replaceable>TEXT</replaceable> 300will be concatenated. 301</para> 302<para> 303When used along with a template file, <replaceable>TEXT</replaceable> 304will be appended to the template's <literal>value-tail</literal> section. 305</para></listitem> 306</varlistentry> 307 308<varlistentry> 309<term><option>--comments</option> <replaceable>TEXT</replaceable></term> 310<listitem><para> 311Template for auto-generated comments, the default (for C code generations) is 312<literal>"/* @comment@ */"</literal>. 313</para></listitem> 314</varlistentry> 315 316<varlistentry> 317<term><option>--template</option> <replaceable>FILE</replaceable></term> 318<listitem><para> 319Read templates from the given file. The templates are enclosed in 320specially-formatted C comments: 321</para> 322<informalexample><programlisting> 323/*** BEGIN section ***/ 324/*** END section ***/ 325</programlisting></informalexample> 326<para> 327<replaceable>section</replaceable> may be <literal>file-header</literal>, 328<literal>file-production</literal>, <literal>file-tail</literal>, 329<literal>enumeration-production</literal>, <literal>value-header</literal>, 330<literal>value-production</literal>, <literal>value-tail</literal> or 331<literal>comment</literal>. 332</para></listitem> 333</varlistentry> 334 335<varlistentry> 336<term><option>--identifier-prefix</option> <replaceable>PREFIX</replaceable></term> 337<listitem><para> 338Indicates what portion of the enum name should be interpreted as the 339prefix (eg, the "<literal>Gtk</literal>" in 340"<literal>GtkDirectionType</literal>"). Normally this will be figured 341out automatically, but you may need to override the default if your 342namespace is capitalized oddly. 343</para></listitem> 344</varlistentry> 345 346<varlistentry> 347<term><option>--symbol-prefix</option> <replaceable>PREFIX</replaceable></term> 348<listitem><para> 349Indicates what prefix should be used to correspond to the identifier 350prefix in related C function names (eg, the "<literal>gtk</literal>" 351in "<literal>gtk_direction_type_get_type</literal>". Equivalently, 352this is the lowercase version of the prefix component of the enum 353value names (eg, the "<literal>GTK</literal>" in 354"<literal>GTK_DIR_UP</literal>". The default value is the identifier 355prefix, converted to lowercase. 356</para></listitem> 357</varlistentry> 358 359<varlistentry> 360<term><option>--help</option></term> 361<listitem><para> 362Print brief help and exit. 363</para></listitem> 364</varlistentry> 365 366<varlistentry> 367<term><option>--version</option></term> 368<listitem><para> 369Print version and exit. 370</para></listitem> 371</varlistentry> 372 373<varlistentry> 374<term><option>--output=FILE</option></term> 375<listitem><para> 376Write output to FILE instead of stdout. 377</para></listitem> 378</varlistentry> 379 380<varlistentry> 381<term><option>@RSPFILE</option></term> 382<listitem><para> 383When passed as the sole argument, read and parse the actual arguments from 384<literal>RSPFILE</literal>. Useful on systems with a low command-line length 385limit. For example, Windows has a limit of 8191 characters. 386</para></listitem> 387</varlistentry> 388 389</variablelist> 390</refsect1> 391 392<refsect1><title>Using glib-mkenums with Autotools</title> 393<para> 394In order to use <command>glib-mkenums</command> in your project when using 395Autotools as the build system, you will first need to modify your 396<filename>configure.ac</filename> file to ensure you find the appropriate 397command using <command>pkg-config</command>, similarly as to how you discover 398the compiler and linker flags for GLib. 399</para> 400<informalexample><programlisting> 401PKG_PROG_PKG_CONFIG([0.28]) 402 403PKG_CHECK_VAR([GLIB_MKENUMS], [glib-2.0], [glib_mkenums]) 404</programlisting></informalexample> 405<para> 406In your <filename>Makefile.am</filename> file you will typically use rules 407like these: 408</para> 409<informalexample><programlisting> 410# A list of headers to inspect 411project_headers = \ 412 project-foo.h \ 413 project-bar.h \ 414 project-baz.h 415 416enum-types.h: $(project_headers) enum-types.h.in 417 $(AM_V_GEN)$(GLIB_MKENUMS) \ 418 --template=enum-types.h.in \ 419 --output=$@ \ 420 $(project_headers) 421 422enum-types.c: $(project_headers) enum-types.c.in enum-types.h 423 $(AM_V_GEN)$(GLIB_MKENUMS) \ 424 --template=enum-types.c.in \ 425 --output=$@ \ 426 $(project_headers) 427 428BUILT_SOURCES += enum-types.h enum-types.c 429CLEANFILES += enum-types.h enum-types.c 430EXTRA_DIST += enum-types.h.in enum-types.c.in 431</programlisting></informalexample> 432<para> 433In the example above, we have a variable called <literal>project_headers</literal> 434where we reference all header files we want to inspect for generating enumeration 435GTypes. In the <filename>enum-types.h</filename> rule we use <command>glib-mkenums</command> 436with a template called <filename>enum-types.h.in</filename> in order to generate the 437header file; a header template file will typically look like this: 438</para> 439<informalexample><programlisting> 440/*** BEGIN file-header ***/ 441#pragma once 442 443/* Include the main project header */ 444#include "project.h" 445 446G_BEGIN_DECLS 447/*** END file-header ***/ 448 449/*** BEGIN file-production ***/ 450 451/* enumerations from "@filename@" */ 452/*** END file-production ***/ 453 454/*** BEGIN value-header ***/ 455GType @enum_name@_get_type (void) G_GNUC_CONST; 456#define @ENUMPREFIX@_TYPE_@ENUMSHORT@ (@enum_name@_get_type ()) 457/*** END value-header ***/ 458 459/*** BEGIN file-tail ***/ 460G_END_DECLS 461/*** END file-tail ***/ 462</programlisting></informalexample> 463<para> 464The <filename>enum-types.c</filename> rule is similar to the rule for the 465header file, but will use a different <filename>enum-types.c.in</filename> template 466file, similar to 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 "@filename@" */ 477/*** END file-production ***/ 478 479/*** BEGIN value-header ***/ 480GType 481@enum_name@_get_type (void) 482{ 483 static volatile gsize g_@type@_type_id__volatile; 484 485 if (g_once_init_enter (&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 (&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</refsect1> 509 510<refsect1><title>See also</title> 511<para> 512<citerefentry> 513<refentrytitle>glib-genmarshal</refentrytitle> 514<manvolnum>1</manvolnum> 515</citerefentry> 516</para> 517</refsect1> 518</refentry> 519