ctkFunctionGenerateDGraphInput.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. ###########################################################################
  2. #
  3. # Library: CTK
  4. #
  5. # Copyright (c) Kitware Inc.
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.commontk.org/LICENSE
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. ###########################################################################
  20. #
  21. # Generate a DGrapgh input file expected by DGraph executable.
  22. #
  23. FUNCTION(ctkFunctionGenerateDGraphInput dir target_directories)
  24. IF(NOT EXISTS ${dir})
  25. MESSAGE(FATAL_ERROR "Directory ${dir} doesn't exist!")
  26. ENDIF()
  27. CtkMacroParseArguments(MY
  28. ""
  29. "WITH_OPTION;WITH_EXTERNALS"
  30. ${ARGN}
  31. )
  32. SET(dgraph_list )
  33. SET(edges)
  34. SET(vertices)
  35. SET(isolated_vertex_candidates)
  36. FOREACH(target_info ${target_directories})
  37. # extract target_dir and option_name
  38. STRING(REPLACE "^^" "\\;" target_info ${target_info})
  39. SET(target_info_list ${target_info})
  40. LIST(GET target_info_list 0 target_dir)
  41. LIST(GET target_info_list 1 option_name)
  42. #MESSAGE(STATUS target_dir:${target_dir})
  43. #MESSAGE(STATUS option_name:${option_name})
  44. #MESSAGE(STATUS option_name-value:${${option_name}})
  45. # make sure the directory exists
  46. IF(NOT EXISTS ${target_dir}/CMakeLists.txt)
  47. MESSAGE(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
  48. ENDIF()
  49. SET(include_dep 1)
  50. IF(MY_WITH_OPTION)
  51. SET(include_dep ${${option_name}})
  52. ENDIF()
  53. IF(${include_dep})
  54. # extract project name from CMakeLists.txt
  55. FILE(STRINGS "${target_dir}/CMakeLists.txt" project_string
  56. REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
  57. LIMIT_COUNT 10)
  58. STRING(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
  59. STRING(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
  60. IF(${target_project_name} STREQUAL "")
  61. MESSAGE(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
  62. ENDIF()
  63. #MESSAGE(STATUS target_project_name:${target_project_name})
  64. # Make sure the variable is cleared
  65. SET(dependencies )
  66. # get dependencies
  67. ctkFunctionCollectTargetLibraryNames(${target_dir} dependencies)
  68. # Make sure the variable is cleared
  69. SET(ctk_dependencies)
  70. IF(MY_WITH_EXTERNALS)
  71. SET(ctk_dependencies ${dependencies})
  72. ELSE()
  73. # filter dependencies starting with CTK org org_commontk_
  74. ctkMacroGetAllCTKTargetLibraries("${dependencies}" ctk_dependencies)
  75. ENDIF()
  76. IF(ctk_dependencies)
  77. LIST(APPEND vertices ${target_project_name})
  78. ELSE()
  79. # isolated vertex candidate
  80. LIST(APPEND isolated_vertex_candidates ${target_project_name})
  81. ENDIF()
  82. # Generate XML related to the dependencies
  83. FOREACH(dependency_name ${ctk_dependencies})
  84. LIST(APPEND edges ${dependency_name})
  85. SET(dgraph_list ${dgraph_list} "${target_project_name} ${dependency_name}\n")
  86. LIST(APPEND vertices ${dependency_name})
  87. ENDFOREACH()
  88. ENDIF()
  89. ENDFOREACH()
  90. FOREACH(isolated_vertex_candidate ${isolated_vertex_candidates})
  91. SET(_found 0)
  92. FOREACH(dgraph_entry ${dgraph_list})
  93. STRING(REPLACE "\n" "" dgraph_entry "${dgraph_entry}")
  94. STRING(REPLACE " " ";" dgraph_entry "${dgraph_entry}")
  95. LIST(FIND dgraph_entry ${isolated_vertex_candidate} _index)
  96. IF(_index GREATER -1)
  97. SET(_found 1)
  98. BREAK()
  99. ENDIF()
  100. ENDFOREACH()
  101. IF(NOT _found)
  102. LIST(APPEND vertices ${isolated_vertex_candidate})
  103. SET(dgraph_list "${isolated_vertex_candidate}\n" ${dgraph_list})
  104. ENDIF()
  105. ENDFOREACH()
  106. IF(vertices)
  107. LIST(REMOVE_DUPLICATES vertices)
  108. ENDIF()
  109. LIST(LENGTH vertices numberOfVertices)
  110. LIST(LENGTH edges numberOfEdges)
  111. SET(dgraph_list "${numberOfVertices} ${numberOfEdges}\n" ${dgraph_list})
  112. IF(MY_WITH_OPTION)
  113. SET(filename "${dir}/DGraphInput.txt")
  114. ELSEIF(MY_WITH_EXTERNALS)
  115. SET(filename "${dir}/DGraphInput-alldep-withext.txt")
  116. ELSE()
  117. SET(filename "${dir}/DGraphInput-alldep.txt")
  118. ENDIF()
  119. FILE(WRITE ${filename} ${dgraph_list})
  120. MESSAGE(STATUS "Generated: ${filename}")
  121. ENDFUNCTION()