From 84aad872eca6ded1d07ad9191ef067290735bb64 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Mon, 27 Jan 2014 10:10:27 -0700 Subject: [PATCH] Define add_flex_target CMake macro. --- CMakeLists.txt | 1 + cmake/macros/AddFlexTarget.cmake | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 cmake/macros/AddFlexTarget.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b85a3ef6bf..c1d33035dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/macros/") # Include global modules include(AutoInclude) # Implements automatic include_directories finding include(AddBisonTarget) # Defines add_bison_target function +include(AddFlexTarget) # Defines add_flex_target function include(CompositeSources) # Defines composite_sources function include(PackageConfig) # Defines package_option AND target_use_packages include(Interrogate) # Defines target_interrogate AND add_python_module diff --git a/cmake/macros/AddFlexTarget.cmake b/cmake/macros/AddFlexTarget.cmake new file mode 100644 index 0000000000..6f47fb8ab9 --- /dev/null +++ b/cmake/macros/AddFlexTarget.cmake @@ -0,0 +1,73 @@ +# Filename: AddFlexTarget.cmake +# Description: This file defines the function add_flex_target which instructs +# cmake to use flex on an input .lxx file. If flex is not available on +# the system, add_flex_target tries to use .prebuilt .cxx files instead. +# +# Usage: +# add_flex_target(output_cxx input_lxx [DEFINES output_h] [PREFIX prefix]) +# + +# Define add_flex_target() +function(add_flex_target output_cxx input_lxx) + set(arguments "") + set(outputs "${output_cxx}") + set(keyword "") + + # Parse the extra arguments to the function. + foreach(arg ${ARGN}) + if(arg STREQUAL "DEFINES") + set(keyword "DEFINES") + elseif(arg STREQUAL "PREFIX") + set(keyword "PREFIX") + + elseif(keyword STREQUAL "PREFIX") + set(arguments ${arguments} -P "${arg}") + elseif(keyword STREQUAL "DEFINES") + set(arguments ${arguments} --header-file="${arg}") + list(APPEND outputs "${arg}") + + else() + message(SEND_ERROR "Unexpected argument ${arg} to add_flex_target") + endif() + endforeach() + + if(keyword STREQUAL arg AND NOT keyword STREQUAL "") + message(SEND_ERROR "Expected argument after ${keyword}") + endif() + + if(HAVE_FLEX) + get_source_file_property(input_lxx "${input_lxx}" LOCATION) + + # If we have flex, we can of course just run it. + add_custom_command( + OUTPUT ${outputs} + COMMAND ${FLEX_EXECUTABLE} + -o "${output_cxx}" ${arguments} + "${input_lxx}" + MAIN_DEPENDENCY "${input_lxx}" + ) + + else() + # Look for prebuilt versions of the outputs. + set(commands "") + set(depends "") + + foreach(output ${outputs}) + set(prebuilt_file "${output}.prebuilt") + get_filename_component(prebuilt_file "${prebuilt_file}" ABSOLUTE) + + if(NOT EXISTS "${prebuilt_file}") + message(SEND_ERROR "Flex was not found and ${prebuilt_file} does not exist!") + endif() + + list(APPEND depends "${prebuilt_file}") + set(commands ${commands} COMMAND ${CMAKE_COMMAND} -E copy ${prebuilt_file} ${output}) + endforeach() + + add_custom_command( + OUTPUT ${outputs} + ${commands} + DEPENDS ${depends} + ) + endif() +endfunction(add_flex_target)