You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindRagel.cmake 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # - Find Ragel executable and provides macros to generate custom build rules
  2. # The module defines the following variables:
  3. #
  4. # RAGEL_EXECUTABLE - path to the ragel program
  5. # RAGEL_VERSION - version of ragel
  6. # RAGEL_FOUND - true if the program was found
  7. #
  8. # If ragel is found, the module defines the macros:
  9. #
  10. # RAGEL_TARGET(<Name> INPUTS <inputs> OUTPUT <output>
  11. # [COMPILE_FLAGS <string>] [DEPENDS <depends>])
  12. #
  13. # which will create a custom rule to generate a state machine. <RagelInp> is
  14. # the path to a Ragel file. <CodeOutput> is the name of the source file
  15. # generated by ragel. If COMPILE_FLAGS option is specified, the next
  16. # parameter is added in the ragel command line.
  17. #
  18. # The macro defines a set of variables:
  19. # RAGEL_${Name}_DEFINED - true is the macro ran successfully
  20. # RAGEL_${Name}_INPUT - The input source file, an alias for <RagelInp>
  21. # RAGEL_${Name}_OUTPUT_SOURCE - The source file generated by ragel
  22. # RAGEL_${Name}_OUTPUT_HEADER - The header file generated by ragel
  23. # RAGEL_${Name}_OUTPUTS - The sources files generated by ragel
  24. # RAGEL_${Name}_COMPILE_FLAGS - Options used in the ragel command line
  25. #
  26. # ====================================================================
  27. # Example:
  28. #
  29. # find_package(RAGEL) # or e.g.: find_package(RAGEL 6.6 REQUIRED)
  30. # RAGEL_TARGET(MyMachine machine.rl ${CMAKE_CURRENT_BINARY_DIR}/machine.cc)
  31. # add_executable(Foo main.cc ${RAGEL_MyMachine_OUTPUTS})
  32. # ====================================================================
  33. # 2014-02-09, Georg Sauthoff <mail@georg.so>
  34. #
  35. # I don't think that these few lines are even copyrightable material,
  36. # but I am fine with using the BSD/MIT/GPL license on it ...
  37. #
  38. # I've used following references:
  39. # http://www.cmake.org/cmake/help/v2.8.12/cmake.html
  40. # /usr/share/cmake/Modules/FindFLEX.cmake
  41. # /usr/share/cmake/Modules/FindBISON.cmake
  42. find_program(RAGEL_EXECUTABLE NAMES ragel DOC "path to the ragel executable")
  43. mark_as_advanced(RAGEL_EXECUTABLE)
  44. if(RAGEL_EXECUTABLE)
  45. execute_process(COMMAND ${RAGEL_EXECUTABLE} --version
  46. OUTPUT_VARIABLE RAGEL_version_output
  47. ERROR_VARIABLE RAGEL_version_error
  48. RESULT_VARIABLE RAGEL_version_result
  49. OUTPUT_STRIP_TRAILING_WHITESPACE)
  50. if(${RAGEL_version_result} EQUAL 0)
  51. string(REGEX REPLACE "^Ragel State Machine Compiler version ([^ ]+) .*$"
  52. "\\1"
  53. RAGEL_VERSION "${RAGEL_version_output}")
  54. else()
  55. message(SEND_ERROR
  56. "Command \"${RAGEL_EXECUTABLE} --version\" failed with output:
  57. ${RAGEL_version_error}")
  58. endif()
  59. #============================================================
  60. # RAGEL_TARGET (public macro)
  61. #============================================================
  62. #
  63. macro(RAGEL_TARGET Name)
  64. CMAKE_PARSE_ARGUMENTS(RAGEL "" "OUTPUT"
  65. "INPUTS;DEPENDS;COMPILE_FLAGS" ${ARGN})
  66. file(RELATIVE_PATH RAGEL_OUTPUT_RELATIVE "${CMAKE_CURRENT_BINARY_DIR}"
  67. "${RAGEL_OUTPUT}")
  68. file(RELATIVE_PATH RAGEL_INPUTS_RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}"
  69. "${RAGEL_INPUTS}")
  70. add_custom_command(OUTPUT ${RAGEL_OUTPUT}
  71. COMMAND ${RAGEL_EXECUTABLE}
  72. ARGS ${RAGEL_COMPILE_FLAGS}
  73. -o${RAGEL_OUTPUT} ${RAGEL_INPUTS}
  74. DEPENDS ${RAGEL_INPUTS} ${RAGEL_DEPENDS}
  75. COMMENT
  76. "[RAGEL][${Name}] Compiling state machine with Ragel ${RAGEL_VERSION} -> ${RAGEL_OUTPUT}"
  77. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  78. get_filename_component(src_target ${RAGEL_INPUTS} NAME_WE)
  79. add_custom_target(ragel_${src_target} DEPENDS ${RAGEL_OUTPUT})
  80. set_source_files_properties(${RAGEL_OUTPUT} PROPERTIES GENERATED TRUE)
  81. set(RAGEL_${Name}_DEFINED TRUE)
  82. set(RAGEL_${Name}_OUTPUTS ${RAGEL_OUTPUT})
  83. set(RAGEL_${Name}_INPUT ${RAGEL_INPUTS})
  84. set(RAGEL_${Name}_COMPILE_FLAGS ${RAGEL_COMPILE_FLAGS})
  85. endmacro()
  86. endif()
  87. # use this include when module file is located under /usr/share/cmake/Modules
  88. #include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  89. # use this include when module file is located in build tree
  90. include(FindPackageHandleStandardArgs)
  91. FIND_PACKAGE_HANDLE_STANDARD_ARGS(RAGEL REQUIRED_VARS RAGEL_EXECUTABLE
  92. VERSION_VAR RAGEL_VERSION)