- Source code: cmake/modules/PHP/Re2c.cmake
This module provides a command to find the re2c command-line lexer generator and generate lexer files with re2c.
Load this module in CMake with:
include(PHP/Re2c)This module provides the following command:
Generates lexer file from the given template file using the re2c generator:
php_re2c(
<name>
<input>
<output>
[HEADER <header>]
[ADD_DEFAULT_OPTIONS]
[OPTIONS <options>...]
[DEPENDS <depends>...]
[COMPUTED_GOTOS <TRUE|FALSE>]
[CODEGEN]
[WORKING_DIRECTORY <dir>]
[ABSOLUTE_PATHS]
)This command creates a target <name> and adds a command that generates lexer
file <output> from the given <input> template file using the re2c lexer
generator. If generated files are already available (for example, shipped with
the released archive), and re2c is not found, it will create a target but skip
the re2c command-line execution.
When the CMAKE_ROLE global property value is not PROJECT it generates the
files right away without creating a target. For example, running in command-line
scripts (script mode).
-
<name>Unique identifier for the command invocation and the name of a custom target.
-
<input>A template file from which the re2c lexer generator generates output files. Relative source file path
<input>is interpreted as being relative to the current source directory (CMAKE_CURRENT_SOURCE_DIR). -
<output>Name of the generated lexer file. Relative
<output>file path is interpreted as being relative to the current binary directory (CMAKE_CURRENT_BINARY_DIR). -
HEADER <header>Generates a given
<header>file. Relative header file path is interpreted as being relative to the current binary directory (CMAKE_CURRENT_BINARY_DIR). -
ADD_DEFAULT_OPTIONSWhen specified, the options from the
PHP_RE2C_OPTIONSconfiguration variable are prepended to the currentre2ccommand-line invocation. This module provides some sensible defaults. -
OPTIONS <options>...A list of additional options to pass to the
re2ccommand-line tool. Generator expressions are supported. In script modes (whenCMAKE_ROLEis notPROJECT) generator expressions are stripped as they can't be determined. -
DEPENDS <depends>...Optional list of dependent files to regenerate the output file.
-
COMPUTED_GOTOS <TRUE|FALSE>Set to
TRUEto add the--computed-gotos(-g) command-line option if the non-standard C computed goto extension is supported by the C compiler. When callingre2c()in some script mode (CMAKE_ROLEvalue other thanPROJECT), compiler checking is skipped and option is added unconditionally. -
CODEGENAdds the
CODEGENoption to theadd_custom_command()call. This option is available starting with CMake 3.31 when the policyCMP0171is set toNEW. It provides acodegentarget for convenience, allowing to run only code-generation-related targets while skipping the majority of the build:cmake --build <build-dir> --target codegen
-
WORKING_DIRECTORY <dir>The path where the
re2cis executed. Relative<dir>path is interpreted as being relative to the current binary directory (CMAKE_CURRENT_BINARY_DIR). If not set,re2cis by default executed in thePHP_SOURCE_DIRwhen building the php-src repository. Otherwise it is executed in the directory of the<output>file. If variablePHP_RE2C_WORKING_DIRECTORYis set before calling thephp_re2c()without this option, it will set the default working directory to that instead. -
ABSOLUTE_PATHSWhether to use absolute file paths in the
re2ccommand-line invocations. By default all file paths are added as relative to the working directory. Using relative paths is convenient when line directives (#line ...) are generated in the output files committed to Git repository.When this option is enabled, generated file(s) will contain lines, such as:
#line 108 "/home/user/php-src/ext/phar/phar_path_check.c"Without this option relative paths are generated:
#line 108 "ext/phar/phar_path_check.c"
These variables can be set before using this module to configure behavior:
-
PHP_RE2C_COMPUTED_GOTOSAdd the
COMPUTED_GOTOS TRUEoption to allphp_re2c()invocations in the directory scope. -
PHP_RE2C_OPTIONSA semicolon-separated list of default re2c command-line options when
php_re2c(ADD_DEFAULT_OPTIONS)is used. -
PHP_RE2C_VERSIONThe version constraint, when looking for RE2C package with
find_package(RE2C <version-constraint> ...)in this module. -
PHP_RE2C_VERSION_DOWNLOADWhen re2c cannot be found on the system or the found version is not suitable, this module can also download and build it from its release archive sources as part of the project build. Set which version should be downloaded.
-
PHP_RE2C_WORKING_DIRECTORYSet the default global working directory for all
php_re2c()invocations in the directory scope where theWORKING_DIRECTORY <dir>option is not set.
# CMakeLists.txt
include(PHP/Re2c)
php_re2c(foo foo.re foo.c OPTIONS --bit-vectors --conditions)
# This will run:
# re2c --bit-vectors --conditions --output foo.c foo.reThis module provides some default options when using the ADD_DEFAULT_OPTIONS:
include(PHP/Re2c)
php_re2c(foo foo.re foo.c ADD_DEFAULT_OPTIONS OPTIONS --conditions)
# This will run:
# re2c --no-debug-info --no-generation-date --conditions --output foo.c foo.reinclude(PHP/Re2c)
php_re2c(foo foo.re foo.c OPTIONS $<$<CONFIG:Debug>:--debug-output> -F)
# When build type is Debug, this will run:
# re2c --debug-output -F --output foo.c foo.re
# For other build types, including the script modes (CMAKE_ROLE is not PROJECT):
# re2c -F --output foo.c foo.reTarget created by php_re2c() can be used to specify additional dependencies:
# CMakeLists.txt
include(PHP/Re2c)
php_re2c(foo_lexer lexer.re lexer.c)
add_dependencies(some_target foo_lexer)Running only the foo_lexer target to generate the lexer-related files:
cmake --build <build-dir> --target foo_lexerTo specify different minimum required re2c version than the module's default,
the find_package(RE2C) can be called before php_re2c():
include(PHP/Re2c)
find_package(RE2C 3.1)
php_re2c(...)