AddFlexTarget.cmake 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. else()
  44. # Look for prebuilt versions of the outputs.
  45. set(commands "")
  46. set(depends "")
  47. foreach(output ${outputs})
  48. set(prebuilt_file "${output}.prebuilt")
  49. get_filename_component(prebuilt_file "${prebuilt_file}" ABSOLUTE)
  50. if(NOT EXISTS "${prebuilt_file}")
  51. message(SEND_ERROR "Flex was not found and ${prebuilt_file} does not exist!")
  52. endif()
  53. list(APPEND depends "${prebuilt_file}")
  54. list(APPEND commands COMMAND ${CMAKE_COMMAND} -E copy ${prebuilt_file} ${output})
  55. endforeach()
  56. add_custom_command(
  57. OUTPUT ${outputs}
  58. ${commands}
  59. DEPENDS ${depends})
  60. endif()
  61. if(MSVC)
  62. set_source_files_properties(${outputs} PROPERTIES COMPILE_DEFINITIONS YY_NO_UNISTD_H=1)
  63. endif()
  64. endfunction(add_flex_target)