cmake/Modules/FindBISON.cmake

308 lines
11 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2019-11-11 23:01:05 +01:00
#[=======================================================================[.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. :command:`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.
2021-09-14 00:13:48 +02:00
.. versionchanged:: 3.14
When :policy:`CMP0088` is set to ``NEW``, ``bison`` runs in the
:variable:`CMAKE_CURRENT_BINARY_DIR` directory.
2019-11-11 23:01:05 +01:00
The options are:
``COMPILE_FLAGS <flags>``
Specify flags to be added to the ``bison`` command line.
``DEFINES_FILE <file>``
2021-09-14 00:13:48 +02:00
.. versionadded:: 3.4
2019-11-11 23:01:05 +01:00
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.
2021-09-14 00:13:48 +02:00
.. deprecated:: 3.7
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>``.
2019-11-11 23:01:05 +01:00
``REPORT_FILE <file>``
2021-09-14 00:13:48 +02:00
.. versionadded:: 3.7
2019-11-11 23:01:05 +01:00
Specify a non-default report ``<file>``, if generated.
The macro defines the following 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``
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})
#]=======================================================================]
2009-10-04 10:30:41 +03:00
2020-08-30 11:54:41 +02:00
find_program(BISON_EXECUTABLE NAMES bison win-bison win_bison DOC "path to the bison executable")
2013-03-16 19:13:01 +02:00
mark_as_advanced(BISON_EXECUTABLE)
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
if(BISON_EXECUTABLE)
2011-01-16 11:35:12 +01:00
# the bison commands should be executed with the C locale, otherwise
# the message (which are parsed) may be translated
2013-03-16 19:13:01 +02:00
set(_Bison_SAVED_LC_ALL "$ENV{LC_ALL}")
set(ENV{LC_ALL} C)
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
execute_process(COMMAND ${BISON_EXECUTABLE} --version
2009-10-04 10:30:41 +03:00
OUTPUT_VARIABLE BISON_version_output
ERROR_VARIABLE BISON_version_error
RESULT_VARIABLE BISON_version_result
OUTPUT_STRIP_TRAILING_WHITESPACE)
2011-01-16 11:35:12 +01:00
2013-03-16 19:13:01 +02:00
set(ENV{LC_ALL} ${_Bison_SAVED_LC_ALL})
2011-01-16 11:35:12 +01:00
2013-03-16 19:13:01 +02:00
if(NOT ${BISON_version_result} EQUAL 0)
message(SEND_ERROR "Command \"${BISON_EXECUTABLE} --version\" failed with output:\n${BISON_version_error}")
else()
2012-02-18 12:40:36 +02:00
# Bison++
2015-04-27 22:25:09 +02:00
if("${BISON_version_output}" MATCHES "^bison\\+\\+ Version ([^,]+)")
set(BISON_VERSION "${CMAKE_MATCH_1}")
2012-02-18 12:40:36 +02:00
# GNU Bison
2015-04-27 22:25:09 +02:00
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}")
2013-03-16 19:13:01 +02:00
endif()
endif()
2009-10-04 10:30:41 +03:00
# internal macro
2016-10-30 18:24:19 +01:00
# sets BISON_TARGET_cmdopt
2013-03-16 19:13:01 +02:00
macro(BISON_TARGET_option_extraopts Options)
2016-10-30 18:24:19 +01:00
set(BISON_TARGET_cmdopt "")
2013-03-16 19:13:01 +02:00
set(BISON_TARGET_extraopts "${Options}")
separate_arguments(BISON_TARGET_extraopts)
list(APPEND BISON_TARGET_cmdopt ${BISON_TARGET_extraopts})
endmacro()
2009-10-04 10:30:41 +03:00
2015-11-17 17:22:37 +01:00
# internal macro
2016-10-30 18:24:19 +01:00
# 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()
2018-10-28 12:09:07 +01:00
if(NOT IS_ABSOLUTE "${BISON_TARGET_verbose_file}")
2019-11-11 23:01:05 +01:00
cmake_policy(GET CMP0088 _BISON_CMP0088
PARENT_SCOPE # undocumented, do not use outside of CMake
)
if("x${_BISON_CMP0088}x" STREQUAL "xNEWx")
set(BISON_TARGET_verbose_file "${CMAKE_CURRENT_BINARY_DIR}/${BISON_TARGET_verbose_file}")
else()
set(BISON_TARGET_verbose_file "${CMAKE_CURRENT_SOURCE_DIR}/${BISON_TARGET_verbose_file}")
endif()
unset(_BISON_CMP0088)
2018-10-28 12:09:07 +01:00
endif()
2016-10-30 18:24:19 +01:00
endmacro()
# internal macro
# adds a custom command and sets
2018-10-28 12:09:07 +01:00
# BISON_TARGET_cmdopt, BISON_TARGET_extraoutputs
2016-10-30 18:24:19 +01:00
macro(BISON_TARGET_option_verbose Name BisonOutput filename)
2019-11-11 23:01:05 +01:00
cmake_policy(GET CMP0088 _BISON_CMP0088
PARENT_SCOPE # undocumented, do not use outside of CMake
)
set(_BISON_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
if("x${_BISON_CMP0088}x" STREQUAL "xNEWx")
set(_BISON_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()
unset(_BISON_CMP0088)
2016-10-30 18:24:19 +01:00
list(APPEND BISON_TARGET_cmdopt "--verbose")
2018-10-28 12:09:07 +01:00
list(APPEND BISON_TARGET_outputs
2016-10-30 18:24:19 +01:00
"${BISON_TARGET_verbose_file}")
if (NOT "${filename}" STREQUAL "")
2018-10-28 12:09:07 +01:00
if(IS_ABSOLUTE "${filename}")
set(BISON_TARGET_verbose_extra_file "${filename}")
else()
2019-11-11 23:01:05 +01:00
set(BISON_TARGET_verbose_extra_file "${_BISON_WORKING_DIRECTORY}/${filename}")
2018-10-28 12:09:07 +01:00
endif()
2019-11-11 23:01:05 +01:00
2018-10-28 12:09:07 +01:00
add_custom_command(OUTPUT ${BISON_TARGET_verbose_extra_file}
2016-10-30 18:24:19 +01:00
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}"
2019-11-11 23:01:05 +01:00
WORKING_DIRECTORY ${_BISON_WORKING_DIRECTORY})
2016-10-30 18:24:19 +01:00
list(APPEND BISON_TARGET_extraoutputs
2018-10-28 12:09:07 +01:00
"${BISON_TARGET_verbose_extra_file}")
unset(BISON_TARGET_verbose_extra_file)
2019-11-11 23:01:05 +01:00
unset(_BISON_WORKING_DIRECTORY)
2016-10-30 18:24:19 +01:00
endif()
2015-11-17 17:22:37 +01:00
endmacro()
2009-10-04 10:30:41 +03:00
#============================================================
# BISON_TARGET (public macro)
#============================================================
#
2013-03-16 19:13:01 +02:00
macro(BISON_TARGET Name BisonInput BisonOutput)
set(BISON_TARGET_outputs "${BisonOutput}")
2016-10-30 18:24:19 +01:00
set(BISON_TARGET_extraoutputs "")
2015-11-17 17:22:37 +01:00
# Parsing parameters
2016-10-30 18:24:19 +01:00
set(BISON_TARGET_PARAM_OPTIONS
)
2015-11-17 17:22:37 +01:00
set(BISON_TARGET_PARAM_ONE_VALUE_KEYWORDS
COMPILE_FLAGS
DEFINES_FILE
2016-10-30 18:24:19 +01:00
REPORT_FILE
)
set(BISON_TARGET_PARAM_MULTI_VALUE_KEYWORDS
VERBOSE
2015-11-17 17:22:37 +01:00
)
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 "")
2013-03-16 19:13:01 +02:00
message(SEND_ERROR "Usage")
2016-10-30 18:24:19 +01:00
elseif("${BISON_TARGET_ARG_VERBOSE}" MATCHES ";")
# [VERBOSE [<file>] hack: <file> is non-multi value by usage
message(SEND_ERROR "Usage")
2013-03-16 19:13:01 +02:00
else()
2016-10-30 18:24:19 +01:00
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}")
2015-11-17 17:22:37 +01:00
if(NOT "${BISON_TARGET_ARG_VERBOSE}" STREQUAL "")
BISON_TARGET_option_verbose(${Name} ${BisonOutput} "${BISON_TARGET_ARG_VERBOSE}")
2016-10-30 18:24:19 +01:00
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()
2013-03-16 19:13:01 +02:00
endif()
2009-10-04 10:30:41 +03:00
2015-11-17 17:22:37 +01:00
list(APPEND BISON_TARGET_outputs "${BISON_TARGET_output_header}")
2011-01-16 11:35:12 +01:00
2019-11-11 23:01:05 +01:00
cmake_policy(GET CMP0088 _BISON_CMP0088
PARENT_SCOPE # undocumented, do not use outside of CMake
)
set(_BISON_WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set(_BisonInput "${BisonInput}")
if("x${_BISON_CMP0088}x" STREQUAL "xNEWx")
set(_BISON_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(NOT IS_ABSOLUTE "${_BisonInput}")
set(_BisonInput "${CMAKE_CURRENT_SOURCE_DIR}/${_BisonInput}")
endif()
endif()
unset(_BISON_CMP0088)
2013-03-16 19:13:01 +02:00
add_custom_command(OUTPUT ${BISON_TARGET_outputs}
2019-11-11 23:01:05 +01:00
COMMAND ${BISON_EXECUTABLE} ${BISON_TARGET_cmdopt} -o ${BisonOutput} ${_BisonInput}
2016-07-09 11:21:54 +02:00
VERBATIM
2019-11-11 23:01:05 +01:00
DEPENDS ${_BisonInput}
2009-10-04 10:30:41 +03:00
COMMENT "[BISON][${Name}] Building parser with bison ${BISON_VERSION}"
2019-11-11 23:01:05 +01:00
WORKING_DIRECTORY ${_BISON_WORKING_DIRECTORY})
unset(_BISON_WORKING_DIRECTORY)
2011-01-16 11:35:12 +01:00
2009-10-04 10:30:41 +03:00
# define target variables
2013-03-16 19:13:01 +02:00
set(BISON_${Name}_DEFINED TRUE)
2019-11-11 23:01:05 +01:00
set(BISON_${Name}_INPUT ${_BisonInput})
2016-10-30 18:24:19 +01:00
set(BISON_${Name}_OUTPUTS ${BISON_TARGET_outputs} ${BISON_TARGET_extraoutputs})
2013-03-16 19:13:01 +02:00
set(BISON_${Name}_COMPILE_FLAGS ${BISON_TARGET_cmdopt})
set(BISON_${Name}_OUTPUT_SOURCE "${BisonOutput}")
2015-11-17 17:22:37 +01:00
set(BISON_${Name}_OUTPUT_HEADER "${BISON_TARGET_output_header}")
2013-03-16 19:13:01 +02:00
2019-11-11 23:01:05 +01:00
unset(_BisonInput)
2013-03-16 19:13:01 +02:00
endif()
endmacro()
2009-10-04 10:30:41 +03:00
#
#============================================================
2013-03-16 19:13:01 +02:00
endif()
2009-10-04 10:30:41 +03:00
2013-03-16 19:13:01 +02:00
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
2010-11-13 01:00:53 +02:00
FIND_PACKAGE_HANDLE_STANDARD_ARGS(BISON REQUIRED_VARS BISON_EXECUTABLE
VERSION_VAR BISON_VERSION)