ParseHeader.cmake 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Simple CMake utility to read variables from MK files
  2. # - Gets contents from given file (name or path)
  3. # - Parses the assignment statements
  4. # - Makes the same assignments in the PARENT_SCOPE
  5. if(POLICY CMP0007)
  6. cmake_policy(SET CMP0007 NEW)
  7. endif()
  8. function(ParseHeader HeaderFile Prefix)
  9. _ParseHeader(${HeaderFile} ${Prefix})
  10. endfunction()
  11. macro(_ParseHeader HeaderFile Prefix)
  12. message(CHECK_START "Parsing Header")
  13. list(APPEND CMAKE_MESSAGE_INDENT " ")
  14. message(STATUS "Reading \"${HeaderFile}\"")
  15. file(READ "${HeaderFile}" FileContents)
  16. string(REGEX REPLACE "/\\*.*\\*/" "" FileContents ${FileContents})
  17. # replace the \ newlines with spaces
  18. string(REGEX REPLACE "\\\\\r?\n *" " " FileContents ${FileContents})
  19. # turn each line into an item in a list
  20. string(REGEX REPLACE "\r?\n" ";" FileLines ${FileContents})
  21. list(REMOVE_ITEM FileLines "")
  22. foreach(line ${FileLines})
  23. # remove comments from the ends of each line
  24. string(REGEX REPLACE "//.*" "" line ${line})
  25. # remove now-empty lines
  26. if("${line}" STREQUAL "")
  27. continue()
  28. endif()
  29. # try to process includes, if the file exists
  30. if(line MATCHES "^#include \"(.+)\"")
  31. set(INCLUDED_HEADER ${CMAKE_MATCH_1})
  32. if(EXISTS ${INCLUDED_HEADER})
  33. _ParseHeader("${INCLUDED_HEADER}" ${Prefix})
  34. else()
  35. message(STATUS "Could not read ${INCLUDED_HEADER}")
  36. endif()
  37. continue()
  38. endif()
  39. # array
  40. if(line MATCHES "#define ([A-Za-z0-9_]+) {(.*)}")
  41. set(VARIABLE_NAME ${CMAKE_MATCH_1})
  42. set(VARIABLE_VALUE ${CMAKE_MATCH_2})
  43. set(${Prefix}${VARIABLE_NAME} ${VARIABLE_VALUE})
  44. endif()
  45. # regular variable
  46. if(line MATCHES "#define ([A-Za-z0-9_]+) (.*)")
  47. set(VARIABLE_NAME ${CMAKE_MATCH_1})
  48. set(VARIABLE_VALUE ${CMAKE_MATCH_2})
  49. set(${Prefix}${VARIABLE_NAME} ${VARIABLE_VALUE})
  50. endif()
  51. endforeach()
  52. list(POP_BACK CMAKE_MESSAGE_INDENT)
  53. message(CHECK_PASS "Complete")
  54. endmacro()