1
0

ParseMakefile.cmake 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(ParseMakefile MKFile)
  9. _ParseMakefile(${MKFile} ${ARGN})
  10. endfunction()
  11. macro(_ParseMakefile MKFile)
  12. message(CHECK_START "Parsing Makefile")
  13. list(APPEND CMAKE_MESSAGE_INDENT " ")
  14. message(STATUS "Reading \"${MKFile}\"")
  15. file(READ "${MKFile}" FileContents)
  16. # replace the \ newlines with spaces
  17. string(REGEX REPLACE "\\\\\r?\n *" " " FileContents ${FileContents})
  18. # turn each line into an item in a list
  19. string(REGEX REPLACE "\r?\n" ";" FileLines ${FileContents})
  20. list(REMOVE_ITEM FileLines "")
  21. foreach(line ${FileLines})
  22. # remove comments from the ends of each line
  23. string(REGEX REPLACE "#.*" "" line ${line})
  24. # remove now-empty lines
  25. if("${line}" STREQUAL "")
  26. continue()
  27. endif()
  28. # try to process includes, if the file exists
  29. if(line MATCHES "^-?include (.+)$")
  30. set(MAKE_CHILD ${CMAKE_MATCH_1})
  31. if(EXISTS ${MAKE_CHILD})
  32. _ParseMakefile("${MAKE_CHILD}" ${ARGN})
  33. else()
  34. message(STATUS "Could not read ${MAKE_CHILD}")
  35. endif()
  36. continue()
  37. endif()
  38. # turn the assignment into a list with the first item being the variable name
  39. string(REPLACE "=" ";" line_split ${line})
  40. list(LENGTH line_split count)
  41. if(count LESS 2)
  42. message(STATUS "Skipping ${line}")
  43. continue()
  44. endif()
  45. list(GET line_split -1 value)
  46. string(STRIP ${value} value)
  47. # separate_arguments(value)
  48. # string(REPLACE " " ";" value ${value})
  49. list(REMOVE_AT line_split -1)
  50. foreach(var_name ${line_split})
  51. string(STRIP ${var_name} var_name)
  52. # replace $(?) with the variable ? from cmake
  53. if(value MATCHES "\\$\\(([^\\(\\)]+)\\)")
  54. set(MAKE_VARIABLE "${CMAKE_MATCH_1}")
  55. string(REPLACE "$(${MAKE_VARIABLE})" "${${MAKE_VARIABLE}}" value ${value})
  56. endif()
  57. # look for +, assuming it used to be +=
  58. if(${var_name} MATCHES "([^ \\+]+) *\\+")
  59. message(STATUS "Appending \"${CMAKE_MATCH_1}\" with \"${value}\"")
  60. # read parent variable in local & append
  61. set(LOCAL_${CMAKE_MATCH_1} ${CMAKE_MATCH_1})
  62. # APPEND accepts spaces between values
  63. list(APPEND LOCAL_${CMAKE_MATCH_1} ${value})
  64. set(${CMAKE_MATCH_1} ${LOCAL_${CMAKE_MATCH_1}})
  65. set(${CMAKE_MATCH_1} ${LOCAL_${CMAKE_MATCH_1}} PARENT_SCOPE)
  66. else()
  67. # set needs ; between elements to be considered a list
  68. string(REGEX REPLACE " +" ";" value ${value})
  69. # try to find variable in cache and FORCE wtih INTERNAL if it exists
  70. if(DEFINED CACHE${${var_name}})
  71. message(STATUS "Caching \"${var_name}\" to \"${value}\"")
  72. # set locally so replacement still work
  73. set(${var_name} ${value})
  74. set(${var_name} ${value} CACHE INTERNAL "")
  75. else()
  76. message(STATUS "Setting \"${var_name}\" to \"${value}\"")
  77. set(${var_name} ${value})
  78. set(${var_name} ${value} PARENT_SCOPE)
  79. endif()
  80. endif()
  81. endforeach()
  82. endforeach()
  83. list(POP_BACK CMAKE_MESSAGE_INDENT)
  84. message(CHECK_PASS "Complete")
  85. endmacro()