=================================== Duktape config header, duk_config.h =================================== Purpose of duk_config.h ======================= **duk_config.h is an external configuration header ("config header") which provides all platform, compiler, and architecture specific features** so that the main Duktape source code can compile without relying on platform specific headers or functionality. The header also provides active Duktape config options (``DUK_USE_xxx``) for enabling/disabling various optional Duktape features. Some Duktape features (like ROM built-in support and Unicode tables used) also require changes to "prepared" Duktape source code. The ``configure.py`` utility combines the preparation of a ``duk_config.h`` header and Duktape source files; it accepts the same command line options as genconfig.py (and more). **Since Duktape 2.0 ``tools/configure.py`` is the recommended tool to create both a config header and prepared Duktape sources for build**. This document describes genconfig.py usage but you should normally use configure.py wherever genconfig.py is used. While an external config header provides much more flexibility it also needs a bit more thought especially when adapting Duktape to an exotic environment. This document describes various approaches on creating a config header and updating it when a new Duktape release is taken into use. Coming up with a duk_config.h ============================= ``duk_config.h`` is always external to Duktape main source code so that it's always possible, if necessary as a last resort, to manually edit the configuration file or even create one from scratch. As such there are multiple ways to come up with a config header; for common platforms you don't usually need to do much while for more exotic platforms more manual work may be needed. There's no "right way", but the more manual modifications are made, the more effort is needed to deal with Duktape updates. The basic options are: * **Use default duk_config.h in distribution**: Duktape distributable includes a default duk_config.h which autodetects the the platform, compiler, and architecture, and uses default values for all Duktape configuration options. This header should work "out of the box" for Linux, OS X, Windows, and also for several more exotic platforms. If you're using one of the supported platform and default options are acceptable, this should be your default choice. Note that ``DUK_OPT_xxx`` compiler command line options are no longer supported in Duktape 2.x; to use non-default options, run either configure.py (recommended) or genconfig.py. * **Use default duk_config.h with manual modifications**: You can modify the default duk_config.h directly if only a small change is needed. Such changes can be manual, or scripted using e.g. ``sed``. Using scripting is less error prone when Duktape is upgraded and the source duk_config.h changes (which is usual for new versions). See separate section below on how to tweak a header using a script. * **Use genconfig.py to create an autodetect duk_config.h**: You can use ``genconfig.py`` to create a custom autodetecting duk_config.h and specify config option overrides on genconfig command line. See separate section below on how to use genconfig. * **Use genconfig.py to create a barebones duk_config.h**: While the autodetect duk_config.h is convenient, it won't work on exotic platforms. To support exotic platforms, ``genconfig.py`` can generate a template duk_config.h for a specified platform, compiler, and architecture combination (each can be either specified or left as "autodetect") which should match your target as closely as possible. You can then modify the header manually or through scripting. * **Edit the genconfig metadata and regenerate duk_config.h**: You can also add support for your custom platform directly into the genconfig metadata. For example, to support a custom compiler, you'll need to add a compiler-specific C header snippets to detect the compiler and to override default macros which are inappropriate for that compiler. The ``duk_config.h`` can then be regenerated using updated metadata. * **Write a duk_config.h from scratch**: You could also write a duk_config.h from scratch, but because there are quite many typedefs, macros, and config options, it's probably easiest to modify the default or genconfig-generated ``duk_config.h``. NOTE: While you can run ``genconfig.py`` directly, it's recommended to use ``tools/configure.py`` instead. The same configuration options (and more) are accepted by configure.py. Using genconfig =============== Overview of genconfig --------------------- Genconfig (``tools/genconfig.py``) is a small utility for config handling with two basic purposes: * Generate a ``duk_config.h`` for a specified platform, compiler, and architecture. Each can be specified explicitly (e.g. use "gcc" for the compiler) or be left up to automatic compile-time detection. The default ``duk_config.h`` is generated with everything left up to automatic detection. A barebones, target specific header can be generated by defining platform, compiler, and architecture explicitly. * Generate documentation for config options. Config headers are generated based on config option and target metadata files, and manually edited header snippets which are combined to create a final header. Documentation is generated based on config option metadata. Metadata is expressed as YAML files for easy editing and good diff/merge behavior. Generating an autodetect duk_config.h ------------------------------------- To generate an autodetect header suitable for directly supported platforms:: $ cd duktape-2.0.0 $ python tools/genconfig.py \ --metadata config/ \ --output /tmp/duk_config.h \ duk-config-header The resulting header in ``/tmp/duk_config.h`` can then either be used as is or edited manually or through scripting. The equivalent operation using ``tools/configure.py`` is:: $ cd duktape-2.0.0 $ python tools/configure.py \ --source-directory src-input \ --config-metadata config/ \ --output-directory /tmp/output The result directory ``/tmp/output`` contains a ``duk_config.h`` header but also ``duktape.c`` and ``duktape.h`` to be included in your build. You can override individual defines using in several ways (see "Option overrides" section below for more details): C compiler format (-D and -U options), YAML config through a file or inline, or verbatim fixup header through a file or inline. If you're building Duktape as a DLL, you should use the ``--dll`` option:: $ python tools/genconfig.py \ --metadata config/ \ --dll \ --output /tmp/duk_config.h \ duk-config-header The ``configure.py`` equivalent:: $ python tools/configure.py \ --source-directory src-input \ --config-metadata config/ \ --output-directory /tmp/output \ --dll DLL builds cannot be detected automatically and they affect symbol visibility attributes on Windows. The ``-dll`` option creates a header which assumes that a DLL will be built. Some changes such as reworking ``#include`` statements cannot be represented as override files; you'll need to edit the resulting config header manually or using some scripting approach. Generating a barebones duk_config.h ----------------------------------- To generate a barebones header you need to specify a platform, compiler, and architecture for genconfig:: $ python tools/genconfig.py \ --metadata config/ \ --platform linux \ --compiler gcc \ --architecture x64 \ --output /tmp/duk_config.h \ duk-config-header The barebones header in ``/tmp/duk_config.h`` can then either be used as is or edited manually or through scripting. The platform, compiler, and architecture names map to genconfig header snippet files. Duktape config options will be assigned their default values specified in config option metadata files in ``config/config-options/``. You can override individual defines using in several ways (see "Option overrides" section below for more details): C compiler format (-D and -U options), YAML config through a file or inline, or verbatim fixup header through a file or inline. Some changes such as reworking ``#include`` statements cannot be represented as override files; you'll need to edit the resulting config header manually or using some scripting approach. Genconfig option overrides ========================== Genconfig provides multiple ways of overriding config options when generating an autodetect or barebones ``duk_config.h`` header: * C compiler format:: -DDUK_USE_TRACEBACK_DEPTH=100 -DDUK_USE_JX -UDUK_USE_JC * YAML config read from a file or given inline on the command line:: --option-file my_config.yaml --option-yaml 'DUK_USE_FASTINT: true' * Verbatim fixup header lines read from a file or given inline on the command line:: --fixup-file my_custom.h --fixup-line '#undef DUK_USE_JX' These option formats can be mixed which allows you to specify an option baseline (say ``--option-file low_memory.yaml``) and then apply further overrides in various ways. All forced options in C compiler format and YAML format are processed first, with the last override winning. Fixup headers are then emitted in order. C compiler format ----------------- The usual C compiler like format is supported because it's quite familiar. In this example a low memory base configuration is read from a YAML config file, and a few options are then tweaked using the C compiler format. An autodetect header is then generated:: $ cd duktape $ python tools/genconfig.py \ --metadata config/ \ --option-file low_memory.yaml \ -DDUK_USE_TRACEBACK_DEPTH=100 \ -UDUK_USE_JX -UDUK_USE_JC \ --output /tmp/duk_config.h \ duk-config-header YAML config ----------- A YAML config file allows options to be specified in a structured, programmatic manner. An example YAML config file, ``my_config.yaml`` could contain:: DUK_USE_OS_STRING: "\"hack-os\"" # force os name for Duktape.env DUK_USE_ALIGN_BY: 8 # force align-by-8 DUK_USE_FASTINT: true DUK_UNREF: verbatim: "#define DUK_UNREF(x) do { (void) (x); } while (0)" This file, another override file, and a few inline YAML forced options could be used as follows to generate a barebones header:: $ cd duktape $ python tools/genconfig.py \ --metadata config/ \ --platform linux \ --compiler gcc \ --architecture x64 \ --option-file my_config.yaml \ --option-file more_overrides.yaml \ --option-yaml 'DUK_USE_JX: false' \ --option-yaml 'DUK_USE_JC: false' \ --output /tmp/duk_config.h \ duk-config-header For inline YAML, multiple forced options can be given either by using a YAML value with multiple keys, or by using multiple options:: # Multiple values for one option --option-yaml '{ DUK_USE_JX: false, DUK_USE_DEBUG: true }' # Multiple options --option-yaml 'DUK_USE_JX: false' \ --option-yaml 'DUK_USE_DEBUG: true' The YAML format for specifying options is simple: the top level value must be an object whose keys are define names to override. Values are as follows: * ``false``: ``#undef`` option:: # Produces: #undef DUK_USE_DEBUG DUK_USE_DEBUG: false * ``true``: ``#define`` option:: # Produces: #define DUK_USE_DEBUG DUK_USE_DEBUG: true * number: decimal value for define:: # Produces: #define DUK_USE_TRACEBACK_DEPTH 10 DUK_USE_TRACEBACK_DEPTH: 10 # Produces: #define DUK_USE_TRACEBACK_DEPTH 100000L # (a long constant is used automatically if necessary) DUK_USE_TRACEBACK_DEPTH: 100000 * string: verbatim string used as the define value:: # Produces: #define DUK_USE_TRACEBACK_DEPTH (10 + 7) DUK_USE_TRACEBACK_DEPTH: "(10 + 7)" # Produces: #define DUK_USE_OS_STRING "linux" DUK_USE_OS_STRING: "\"linux\"" * C string for value:: # Produces: #define DUK_USE_OS_STRING "linux" DUK_USE_OS_STRING: string: "linux" * verbatim text for entire define:: # Produces: #define DUK_UNREF(x) do {} while (0) DUK_UNREF: verbatim: "#define DUK_UNREF(x) do {} while (0)" Fixup header ------------ In addition to YAML-based option overrides, genconfig has an option for appending direct "fixup headers" to deal with situations which cannot be handled with individual option overrides. For example, you may want to inject specific environment sanity checks, or set config option values based on environment #ifdefs. This mechanism is similar to Duktape 1.x ``duk_custom.h`` header, and you can in fact use ``duk_custom.h`` headers directly as inputs. Fixup headers are emitted after all individual option overrides (in either C compiler or YAML format) have been resolved, but before emitting option sanity checks (if enabled). For example, to generate a barebones header with two fixup headers:: $ python tools/genconfig.py \ --metadata config/ \ --platform linux \ --compiler gcc \ --architecture x64 \ --fixup-file my_env_strings.h \ --fixup-file my_no_json_fastpath.h \ --output /tmp/duk_config.h \ duk-config-header The ``my_env_strings.h`` fixup header could be:: /* Force OS string. */ #undef DUK_USE_OS_STRING #if !defined(__WIN32__) #error this header is Windows only #endif #define DUK_USE_OS_STRING "windows" /* Force arch string. */ #undef DUK_USE_ARCH_STRING #if !defined(__amd64__) #error this header is x64 only #endif #define DUK_USE_ARCH_STRING "x64" /* Force compiler string. */ #undef DUK_USE_COMPILER_STRING #if !defined(__GNUC__) #error this header is gcc only #endif #if defined(__cplusplus__) #define DUK_USE_COMPILER_STRING "g++" #else #define DUK_USE_COMPILER_STRING "gcc" #endif The example fixup header uses dynamic detection and other environment checks which cannot be easily expressed using individual option overrides. The ``my_no_json_fastpath.h`` fixup header could be:: /* Disable JSON fastpath for reduced footprint. */ #undef DUK_USE_JSON_STRINGIFY_FASTPATH This could have also been expressed using a simple override, e.g. as ``-UDUK_USE_JSON_STRINGIFY_FASTPATH``. Fixup headers are appended verbatim so they must be valid C header files, contain appropriate newlines, and must ``#undef`` any defines before redefining them if necessary. Fixup headers can only be used to tweak C preprocessor defines, they naturally cannot un-include headers or un-typedef types. There's also a command line option to append a single fixup line for convenience:: # Append two lines to forcibly enable fastints --fixup-line '#undef DUK_USE_FASTINT' \ --fixup-line '#define DUK_USE_FASTINT' These can be mixed with ``--fixup-file`` options and are processed in sequence. Modifying a duk_config.h manually or using scripting ==================================================== The basic approach when using scripted modifications is to take a base header (either an autodetect or barebones header) and then make specific changes using a script. The advantage of doing so is that if the base header is updated, the script may often still be valid without any manual changes. Scripting provides much more flexibility than tweaking individual options in genconfig, but the cost is more complicated maintenance over time. Using diff/patch ---------------- * Make the necessary changes to the base header manually. * Use ``diff`` to store the changes:: $ diff -u duk_config.h.base duk_config.h.edited > edits.diff * In your build script:: $ cp duk_config.h.base duk_config.h $ patch duk_config.h edits.diff * If the patch fails (e.g. there is too much offset), you need to rebuild the diff file manually. Using sed (or awk, etc) to modify an option in-place ---------------------------------------------------- If an option is defined on a single line in the base header, e.. either as:: #define DUK_USE_FOO or as:: #undef DUK_USE_FOO you can use ``sed`` to easily flip such an option:: # enable shuffle torture cat duk_config.h.base | \ sed -r -e 's/^#\w+\s+DUK_USE_SHUFFLE_TORTURE.*$/#define DUK_USE_SHUFFLE_TORTURE \/*forced*\//' \ > duk_config.h The above example would flip DUK_USE_SHUFFLE_TORTURE on, regardless of its previous setting. You can also use a more verbose sed format which is easier to read especially if there are multiple changes:: cat duk_config.h.base | sed -r -e ' s/^#\w+\s+DUK_USE_SHUFFLE_TORTURE.*$/#define DUK_USE_SHUFFLE_TORTURE \/*forced*\// s/^#\w+\s+DUK_USE_OS_STRING.*$/#define DUK_USE_OS_STRING "my-custom-os" \/*forced*\// ' > duk_config.h This approach won't work if the defined option is defined/undefined multiple times or if the define has a multiline value. For more stateful changes you can use ``awk`` or other scripting languages (Python, Perl, etc). Modifying defines at __OVERRIDE_DEFINES__ ----------------------------------------- Instead of modifying options in-place as in the sed example above, you can simply append additional preprocessor directives to undefine/redefine options as necessary. This is much easier to maintain in version updates than when modifications are made in-place. Genconfig has a direct option to append "fixups" after the main generated header:: # my_custom.h is applied after generated header; functionally similar # to Duktape 1.2.x duk_custom.h $ python tools/genconfig.py [...] --fixup-file my_custom.h [...] A genconfig-generated barebones header also has the following line near the end for detecting where to add override defines; this is easy to detect reliably:: /* __OVERRIDE_DEFINES__ */ The ``__OVERRIDE_DEFINES__`` line is near the end of the file, before any automatically generated option sanity checks (which are optional) so that the sanity checks will be applied after your tweaks have been done:: #!/bin/bash CONFIG_IN=duk_config.h.base CONFIG_OUT=duk_config.h.new cat $CONFIG_IN | sed -e ' /^\/\* __OVERRIDE_DEFINES__ \*\/$/ { r my_overrides.h d }' > $CONFIG_OUT Modifying defines near the end of the file is relatively easy but has a few limitations: * You can't change typedefs this way because there's no way to un-typedef. * You can't undo any ``#include`` directives executed. Modifying defines at the end of the file ---------------------------------------- Another simple approach is to simply assume that an ``#endif`` line (include guard) is the last line in the file, i.e. there are no trailing empty lines. Changes will then be applied after option sanity checks which is not ideal:: #!/bin/bash CONFIG_IN=duk_config.h.base CONFIG_OUT=duk_config.h.new if tail -1 $CONFIG_IN | grep endif ; then echo "Final line of $CONFIG_IN is an #endif as expected, modifying config" else echo "Final line of $CONFIG_IN is not an #endif!" exit 1 fi head -n -1 $CONFIG_IN > $CONFIG_OUT cat >> $CONFIG_OUT <``) needed by any of the config header macros, including: - Standard library functions like ``sprintf()`` and ``memset()`` - Math functions like ``acos()`` - Any other functions called by macros defined in duk_config.h, e.g. the functions needed by a custom Date provider * Typedefs for integer and floating point types (``duk_uint8_t``, etc), and their limit defines. * Some IEEE double constants, including NaN and Infinity, because some constants cannot be reliably expressed as constants in all compilers. * Wrapper macros for platform functions, covering string operations, file I/O, math, etc. For example: ``DUK_FOPEN()``, ``DUK_SPRINTF()``, ``DUK_ACOS()``), etc. Typically these are just mapped 1:1 to platform functions, but sometimes tweaks are needed. * Various compiler specific macros: unreachable code, unreferenced variable, symbol visibility attributes, inlining control, etc. * Duktape config options, ``DUK_USE_xxx``, including a possible custom Date provider. The required defines and typedefs are also available in a machine parseable metadata form: * ``config/other-defines/c_types.yaml``: required integer and other types and their limits. * ``config/other-defines/platform_functions.yaml``: required platform function wrappers. * ``config/other-defines/other_defines.yaml``: compiler specific macros and other misc defines. * ``config/config-options/DUK_USE_*.yaml``: Duktape config options.