diff options
Diffstat (limited to 'Modules/FindBISON.cmake')
-rw-r--r-- | Modules/FindBISON.cmake | 286 |
1 files changed, 182 insertions, 104 deletions
diff --git a/Modules/FindBISON.cmake b/Modules/FindBISON.cmake index 4a3e68c68..d684ccd86 100644 --- a/Modules/FindBISON.cmake +++ b/Modules/FindBISON.cmake @@ -1,51 +1,85 @@ -# - Find bison executable and provides macros to generate custom build rules +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#.rst: +# FindBISON +# --------- +# +# Find ``bison`` executable and provide a macro to generate custom build rules. +# # The module defines the following variables: # -# BISON_EXECUTABLE - path to the bison program -# BISON_VERSION - version of bison -# BISON_FOUND - true if the program was found -# -# The minimum required version of bison can be specified using the -# standard CMake syntax, e.g. find_package(BISON 2.1.3) -# -# If bison is found, the module defines the macros: -# BISON_TARGET(<Name> <YaccInput> <CodeOutput> [VERBOSE <file>] -# [COMPILE_FLAGS <string>]) -# which will create a custom rule to generate a parser. <YaccInput> is -# the path to a yacc file. <CodeOutput> is the name of the source file -# generated by bison. A header file is also be generated, and contains -# the token list. If COMPILE_FLAGS option is specified, the next -# parameter is added in the bison command line. if VERBOSE option is -# specified, <file> is created and contains verbose descriptions of the -# grammar and parser. The macro defines a set of variables: -# BISON_${Name}_DEFINED - true is the macro ran successfully -# BISON_${Name}_INPUT - The input source file, an alias for <YaccInput> -# BISON_${Name}_OUTPUT_SOURCE - The source file generated by bison -# BISON_${Name}_OUTPUT_HEADER - The header file generated by bison -# BISON_${Name}_OUTPUTS - The sources files generated by bison -# BISON_${Name}_COMPILE_FLAGS - Options used in the bison command line -# -# ==================================================================== -# Example: +# ``BISON_EXECUTABLE`` +# path to the ``bison`` program # -# find_package(BISON) -# BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp) -# add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS}) -# ==================================================================== - -#============================================================================= -# Copyright 2009 Kitware, Inc. -# Copyright 2006 Tristan Carel +# ``BISON_VERSION`` +# version of ``bison`` +# +# ``BISON_FOUND`` +# true if the program was found +# +# The minimum required version of ``bison`` can be specified using the +# standard CMake syntax, e.g. ``find_package(BISON 2.1.3)``. +# +# If ``bison`` is found, the module defines the macro:: +# +# BISON_TARGET(<Name> <YaccInput> <CodeOutput> +# [COMPILE_FLAGS <flags>] +# [DEFINES_FILE <file>] +# [VERBOSE [<file>]] +# [REPORT_FILE <file>] +# ) +# +# which will create a custom rule to generate a parser. ``<YaccInput>`` is +# the path to a yacc file. ``<CodeOutput>`` is the name of the source file +# generated by bison. A header file is also be generated, and contains +# the token list. +# +# The options are: +# +# ``COMPILE_FLAGS <flags>`` +# Specify flags to be added to the ``bison`` command line. +# +# ``DEFINES_FILE <file>`` +# Specify a non-default header ``<file>`` to be generated by ``bison``. +# +# ``VERBOSE [<file>]`` +# Tell ``bison`` to write a report file of the grammar and parser. +# If ``<file>`` is given, it specifies path the report file is copied to. +# ``[<file>]`` is left for backward compatibility of this module. +# Use ``VERBOSE REPORT_FILE <file>``. +# +# ``REPORT_FILE <file>`` +# Specify a non-default report ``<file>``, if generated. # -# Distributed under the OSI-approved BSD License (the "License"); -# see accompanying file Copyright.txt for details. +# The macro defines the following variables: # -# This software is distributed WITHOUT ANY WARRANTY; without even the -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# See the License for more information. -#============================================================================= -# (To distribute this file outside of CMake, substitute the full -# License text for the above reference.) +# ``BISON_<Name>_DEFINED`` +# true is the macro ran successfully +# +# ``BISON_<Name>_INPUT`` +# The input source file, an alias for <YaccInput> +# +# ``BISON_<Name>_OUTPUT_SOURCE`` +# The source file generated by bison +# +# ``BISON_<Name>_OUTPUT_HEADER`` +# The header file generated by bison +# +# ``BISON_<Name>_OUTPUTS`` +# All files generated by bison including the source, the header and the report +# +# ``BISON_<Name>_COMPILE_FLAGS`` +# Options used in the ``bison`` command line +# +# Example usage: +# +# .. code-block:: cmake +# +# find_package(BISON) +# BISON_TARGET(MyParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp +# DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/parser.h) +# add_executable(Foo main.cpp ${BISON_MyParser_OUTPUTS}) find_program(BISON_EXECUTABLE NAMES bison win_bison DOC "path to the bison executable") mark_as_advanced(BISON_EXECUTABLE) @@ -68,98 +102,144 @@ if(BISON_EXECUTABLE) message(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}") else() # Bison++ - if("${BISON_version_output}" MATCHES "^bison\\+\\+") - string(REGEX REPLACE "^bison\\+\\+ Version ([^,]+).*" "\\1" - BISON_VERSION "${BISON_version_output}") + if("${BISON_version_output}" MATCHES "^bison\\+\\+ Version ([^,]+)") + set(BISON_VERSION "${CMAKE_MATCH_1}") # GNU Bison - elseif("${BISON_version_output}" MATCHES "^bison[^+]") - string(REGEX REPLACE "^bison \\(GNU Bison\\) ([^\n]+)\n.*" "\\1" - BISON_VERSION "${BISON_version_output}") - elseif("${BISON_version_output}" MATCHES "^GNU Bison ") - string(REGEX REPLACE "^GNU Bison (version )?([^\n]+).*" "\\2" - BISON_VERSION "${BISON_version_output}") + elseif("${BISON_version_output}" MATCHES "^bison \\(GNU Bison\\) ([^\n]+)\n") + set(BISON_VERSION "${CMAKE_MATCH_1}") + elseif("${BISON_version_output}" MATCHES "^GNU Bison (version )?([^\n]+)") + set(BISON_VERSION "${CMAKE_MATCH_2}") endif() endif() # internal macro - macro(BISON_TARGET_option_verbose Name BisonOutput filename) - list(APPEND BISON_TARGET_cmdopt "--verbose") - get_filename_component(BISON_TARGET_output_path "${BisonOutput}" PATH) - get_filename_component(BISON_TARGET_output_name "${BisonOutput}" NAME_WE) - add_custom_command(OUTPUT ${filename} - COMMAND ${CMAKE_COMMAND} - ARGS -E copy - "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output" - "${filename}" - DEPENDS - "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output" - COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}" - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) - set(BISON_${Name}_VERBOSE_FILE ${filename}) - list(APPEND BISON_TARGET_extraoutputs - "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output") - endmacro() - - # internal macro + # sets BISON_TARGET_cmdopt macro(BISON_TARGET_option_extraopts Options) + set(BISON_TARGET_cmdopt "") set(BISON_TARGET_extraopts "${Options}") separate_arguments(BISON_TARGET_extraopts) list(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts}) endmacro() + # internal macro + # sets BISON_TARGET_output_header and BISON_TARGET_cmdopt + macro(BISON_TARGET_option_defines BisonOutput Header) + if("${Header}" STREQUAL "") + # default header path generated by bison (see option -d) + string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${BisonOutput}") + string(REPLACE "c" "h" _fileext ${_fileext}) + string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}" + BISON_TARGET_output_header "${BisonOutput}") + list(APPEND BISON_TARGET_cmdopt "-d") + else() + set(BISON_TARGET_output_header "${Header}") + list(APPEND BISON_TARGET_cmdopt "--defines=${BISON_TARGET_output_header}") + endif() + endmacro() + + # internal macro + # sets BISON_TARGET_verbose_file and BISON_TARGET_cmdopt + macro(BISON_TARGET_option_report_file BisonOutput ReportFile) + if("${ReportFile}" STREQUAL "") + get_filename_component(BISON_TARGET_output_path "${BisonOutput}" PATH) + get_filename_component(BISON_TARGET_output_name "${BisonOutput}" NAME_WE) + set(BISON_TARGET_verbose_file + "${BISON_TARGET_output_path}/${BISON_TARGET_output_name}.output") + else() + set(BISON_TARGET_verbose_file "${ReportFile}") + list(APPEND BISON_TARGET_cmdopt "--report-file=${BISON_TARGET_verbose_file}") + endif() + endmacro() + + # internal macro + # adds a custom command and sets + # BISON_TARGET_cmdopt, BISON_TARGET_verbose_file, BISON_TARGET_extraoutputs + macro(BISON_TARGET_option_verbose Name BisonOutput filename) + list(APPEND BISON_TARGET_cmdopt "--verbose") + list(APPEND BISON_TARGET_extraoutputs + "${BISON_TARGET_verbose_file}") + if (NOT "${filename}" STREQUAL "") + add_custom_command(OUTPUT ${filename} + COMMAND ${CMAKE_COMMAND} -E copy + "${BISON_TARGET_verbose_file}" + "${filename}" + VERBATIM + DEPENDS + "${BISON_TARGET_verbose_file}" + COMMENT "[BISON][${Name}] Copying bison verbose table to ${filename}" + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(BISON_${Name}_VERBOSE_FILE ${filename}) + list(APPEND BISON_TARGET_extraoutputs + "${filename}") + endif() + endmacro() + #============================================================ # BISON_TARGET (public macro) #============================================================ # macro(BISON_TARGET Name BisonInput BisonOutput) - set(BISON_TARGET_output_header "") - set(BISON_TARGET_cmdopt "") set(BISON_TARGET_outputs "${BisonOutput}") - if(NOT ${ARGC} EQUAL 3 AND NOT ${ARGC} EQUAL 5 AND NOT ${ARGC} EQUAL 7) + set(BISON_TARGET_extraoutputs "") + + # Parsing parameters + set(BISON_TARGET_PARAM_OPTIONS + ) + set(BISON_TARGET_PARAM_ONE_VALUE_KEYWORDS + COMPILE_FLAGS + DEFINES_FILE + REPORT_FILE + ) + set(BISON_TARGET_PARAM_MULTI_VALUE_KEYWORDS + VERBOSE + ) + cmake_parse_arguments( + BISON_TARGET_ARG + "${BISON_TARGET_PARAM_OPTIONS}" + "${BISON_TARGET_PARAM_ONE_VALUE_KEYWORDS}" + "${BISON_TARGET_PARAM_MULTI_VALUE_KEYWORDS}" + ${ARGN} + ) + + if(NOT "${BISON_TARGET_ARG_UNPARSED_ARGUMENTS}" STREQUAL "") + message(SEND_ERROR "Usage") + elseif("${BISON_TARGET_ARG_VERBOSE}" MATCHES ";") + # [VERBOSE [<file>] hack: <file> is non-multi value by usage message(SEND_ERROR "Usage") else() - # Parsing parameters - if(${ARGC} GREATER 5 OR ${ARGC} EQUAL 5) - if("${ARGV3}" STREQUAL "VERBOSE") - BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV4}") - endif() - if("${ARGV3}" STREQUAL "COMPILE_FLAGS") - BISON_TARGET_option_extraopts("${ARGV4}") - endif() - endif() - - if(${ARGC} EQUAL 7) - if("${ARGV5}" STREQUAL "VERBOSE") - BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${ARGV6}") - endif() - if("${ARGV5}" STREQUAL "COMPILE_FLAGS") - BISON_TARGET_option_extraopts("${ARGV6}") + BISON_TARGET_option_extraopts("${BISON_TARGET_ARG_COMPILE_FLAGS}") + BISON_TARGET_option_defines("${BisonOutput}" "${BISON_TARGET_ARG_DEFINES_FILE}") + BISON_TARGET_option_report_file("${BisonOutput}" "${BISON_TARGET_ARG_REPORT_FILE}") + if(NOT "${BISON_TARGET_ARG_VERBOSE}" STREQUAL "") + BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${BISON_TARGET_ARG_VERBOSE}") + else() + # [VERBOSE [<file>]] is used with no argument or is not used + set(BISON_TARGET_args "${ARGN}") + list(FIND BISON_TARGET_args "VERBOSE" BISON_TARGET_args_indexof_verbose) + if(${BISON_TARGET_args_indexof_verbose} GREATER -1) + # VERBOSE is used without <file> + BISON_TARGET_option_verbose(${Name} ${BisonOutput} "") endif() endif() - # Header's name generated by bison (see option -d) - list(APPEND BISON_TARGET_cmdopt "-d") - string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\2" _fileext "${ARGV2}") - string(REPLACE "c" "h" _fileext ${_fileext}) - string(REGEX REPLACE "^(.*)(\\.[^.]*)$" "\\1${_fileext}" - BISON_${Name}_OUTPUT_HEADER "${ARGV2}") - list(APPEND BISON_TARGET_outputs "${BISON_${Name}_OUTPUT_HEADER}") + list(APPEND BISON_TARGET_outputs "${BISON_TARGET_output_header}") add_custom_command(OUTPUT ${BISON_TARGET_outputs} ${BISON_TARGET_extraoutputs} - COMMAND ${BISON_EXECUTABLE} - ARGS ${BISON_TARGET_cmdopt} -o ${ARGV2} ${ARGV1} - DEPENDS ${ARGV1} + COMMAND ${BISON_EXECUTABLE} ${BISON_TARGET_cmdopt} -o ${BisonOutput} ${BisonInput} + VERBATIM + DEPENDS ${BisonInput} COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) # define target variables set(BISON_${Name}_DEFINED TRUE) - set(BISON_${Name}_INPUT ${ARGV1}) - set(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs}) + set(BISON_${Name}_INPUT ${BisonInput}) + set(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs} ${BISON_TARGET_extraoutputs}) set(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt}) set(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}") + set(BISON_${Name}_OUTPUT_HEADER "${BISON_TARGET_output_header}") endif() endmacro() @@ -171,5 +251,3 @@ endif() include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS BISON_EXECUTABLE VERSION_VAR BISON_VERSION) - -# FindBISON.cmake ends here |