AddBisonTarget.cmake 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. set(arguments ${arguments} -p "${arg}")
  22. elseif(keyword STREQUAL "DEFINES")
  23. set(arguments ${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. set(commands ${commands} COMMAND ${CMAKE_COMMAND} -E copy ${prebuilt_file} ${output})
  54. endforeach()
  55. add_custom_command(
  56. OUTPUT ${outputs}
  57. ${commands}
  58. DEPENDS ${depends}
  59. )
  60. endif()
  61. endfunction(add_bison_target)