AddFlexTarget.cmake 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Filename: AddFlexTarget.cmake
  2. # Description: This file defines the function add_flex_target which instructs
  3. # cmake to use flex on an input .lxx file. If flex is not available on
  4. # the system, add_flex_target tries to use .prebuilt .cxx files instead.
  5. #
  6. # Usage:
  7. # add_flex_target(output_cxx input_lxx [DEFINES output_h] [PREFIX prefix])
  8. #
  9. # Define add_flex_target()
  10. function(add_flex_target output_cxx input_lxx)
  11. set(arguments "")
  12. set(outputs "${output_cxx}")
  13. set(keyword "")
  14. # Parse the extra arguments to the function.
  15. foreach(arg ${ARGN})
  16. if(arg STREQUAL "DEFINES")
  17. set(keyword "DEFINES")
  18. elseif(arg STREQUAL "PREFIX")
  19. set(keyword "PREFIX")
  20. elseif(arg STREQUAL "CASE_INSENSITIVE")
  21. list(APPEND arguments -i)
  22. elseif(keyword STREQUAL "PREFIX")
  23. list(APPEND arguments -P "${arg}")
  24. elseif(keyword STREQUAL "DEFINES")
  25. list(APPEND arguments --header-file="${arg}")
  26. list(APPEND outputs "${arg}")
  27. else()
  28. message(SEND_ERROR "Unexpected argument ${arg} to add_flex_target")
  29. endif()
  30. endforeach()
  31. if(keyword STREQUAL arg AND NOT keyword STREQUAL "")
  32. message(SEND_ERROR "Expected argument after ${keyword}")
  33. endif()
  34. if(HAVE_FLEX)
  35. get_source_file_property(input_lxx "${input_lxx}" LOCATION)
  36. # If we have flex, we can of course just run it.
  37. add_custom_command(
  38. OUTPUT ${outputs}
  39. COMMAND ${FLEX_EXECUTABLE}
  40. -o "${output_cxx}" ${arguments}
  41. "${input_lxx}"
  42. MAIN_DEPENDENCY "${input_lxx}"
  43. )
  44. else()
  45. # Look for prebuilt versions of the outputs.
  46. set(commands "")
  47. set(depends "")
  48. foreach(output ${outputs})
  49. set(prebuilt_file "${output}.prebuilt")
  50. get_filename_component(prebuilt_file "${prebuilt_file}" ABSOLUTE)
  51. if(NOT EXISTS "${prebuilt_file}")
  52. message(SEND_ERROR "Flex was not found and ${prebuilt_file} does not exist!")
  53. endif()
  54. list(APPEND depends "${prebuilt_file}")
  55. list(APPEND commands COMMAND ${CMAKE_COMMAND} -E copy ${prebuilt_file} ${output})
  56. endforeach()
  57. add_custom_command(
  58. OUTPUT ${outputs}
  59. ${commands}
  60. DEPENDS ${depends}
  61. )
  62. endif()
  63. endfunction(add_flex_target)