AddBisonTarget.cmake 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Filename: AddBisonTarget.cmake
  2. # Description: This file defines the function add_bison_target which instructs
  3. # cmake to use bison on an input .yxx file. If bison is not available on
  4. # the system, add_bison_target tries to use .prebuilt .cxx files instead.
  5. #
  6. # Usage:
  7. # add_bison_target(output_cxx input_yxx [DEFINES output_h] [PREFIX prefix])
  8. #
  9. # Define add_bison_target()
  10. function(add_bison_target output_cxx input_yxx)
  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(keyword STREQUAL "PREFIX")
  21. list(APPEND arguments -p "${arg}")
  22. elseif(keyword STREQUAL "DEFINES")
  23. list(APPEND arguments --defines="${arg}")
  24. list(APPEND outputs "${arg}")
  25. else()
  26. message(SEND_ERROR "Unexpected argument ${arg} to add_bison_target")
  27. endif()
  28. endforeach()
  29. if(keyword STREQUAL arg AND NOT keyword STREQUAL "")
  30. message(SEND_ERROR "Expected argument after ${keyword}")
  31. endif()
  32. if(HAVE_BISON)
  33. get_source_file_property(input_yxx "${input_yxx}" LOCATION)
  34. # If we have bison, we can of course just run it.
  35. add_custom_command(
  36. OUTPUT ${outputs}
  37. COMMAND ${BISON_EXECUTABLE}
  38. -o "${output_cxx}" ${arguments}
  39. "${input_yxx}"
  40. MAIN_DEPENDENCY "${input_yxx}"
  41. )
  42. else()
  43. # Look for prebuilt versions of the outputs.
  44. set(commands "")
  45. set(depends "")
  46. foreach(output ${outputs})
  47. set(prebuilt_file "${output}.prebuilt")
  48. get_filename_component(prebuilt_file "${prebuilt_file}" ABSOLUTE)
  49. if(NOT EXISTS "${prebuilt_file}")
  50. message(SEND_ERROR "Bison was not found and ${prebuilt_file} does not exist!")
  51. endif()
  52. list(APPEND depends "${prebuilt_file}")
  53. list(APPEND commands COMMAND ${CMAKE_COMMAND} -E copy ${prebuilt_file} ${output})
  54. endforeach()
  55. add_custom_command(
  56. OUTPUT ${outputs}
  57. ${commands}
  58. DEPENDS ${depends})
  59. endif()
  60. endfunction(add_bison_target)