ctkFunctionGenerateDGraphInput.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #! \ingroup CMakeAPI
  24. FUNCTION(ctkFunctionGenerateDGraphInput dir target_directories)
  25. IF(NOT EXISTS ${dir})
  26. MESSAGE(FATAL_ERROR "Directory ${dir} doesn't exist!")
  27. ENDIF()
  28. CtkMacroParseArguments(MY
  29. ""
  30. "WITH_OPTION;WITH_EXTERNALS"
  31. ${ARGN}
  32. )
  33. SET(dgraph_list )
  34. SET(edges)
  35. SET(vertices)
  36. SET(isolated_vertex_candidates)
  37. FOREACH(target_info ${target_directories})
  38. # extract target_dir and option_name
  39. STRING(REPLACE "^^" "\\;" target_info ${target_info})
  40. SET(target_info_list ${target_info})
  41. LIST(GET target_info_list 0 target_dir)
  42. LIST(GET target_info_list 1 option_name)
  43. #MESSAGE(STATUS target_dir:${target_dir})
  44. #MESSAGE(STATUS option_name:${option_name})
  45. #MESSAGE(STATUS option_name-value:${${option_name}})
  46. # make sure the directory exists
  47. IF(NOT EXISTS ${target_dir}/CMakeLists.txt)
  48. MESSAGE(FATAL_ERROR "Target directory ${target_dir}/CMakeLists.txt doesn't exists !")
  49. ENDIF()
  50. SET(include_dep 1)
  51. IF(MY_WITH_OPTION)
  52. SET(include_dep ${${option_name}})
  53. ENDIF()
  54. IF(${include_dep})
  55. # extract project name from CMakeLists.txt
  56. FILE(STRINGS "${target_dir}/CMakeLists.txt" project_string
  57. REGEX "^ *(P|p)(R|r)(O|o)(J|j)(E|e)(C|c)(T|t)\\("
  58. LIMIT_COUNT 10)
  59. STRING(REGEX MATCH "\\((.*)\\)" target_project_name ${project_string})
  60. STRING(REGEX REPLACE "\\(|\\)" "" target_project_name ${target_project_name})
  61. IF(${target_project_name} STREQUAL "")
  62. MESSAGE(FATAL_ERROR "Failed to extract project name from ${target_dir}/CMakeLists.txt")
  63. ENDIF()
  64. #MESSAGE(STATUS target_project_name:${target_project_name})
  65. # Make sure the variable is cleared
  66. SET(dependencies )
  67. # get dependencies
  68. ctkFunctionCollectTargetLibraryNames(${target_dir} dependencies)
  69. # Make sure the variable is cleared
  70. SET(ctk_dependencies)
  71. IF(MY_WITH_EXTERNALS)
  72. SET(ctk_dependencies ${dependencies})
  73. ELSE()
  74. # filter dependencies starting with CTK org org_commontk_
  75. ctkMacroGetAllProjectTargetLibraries("${dependencies}" ctk_dependencies)
  76. ENDIF()
  77. IF(ctk_dependencies)
  78. LIST(APPEND vertices ${target_project_name})
  79. ELSE()
  80. # isolated vertex candidate
  81. LIST(APPEND isolated_vertex_candidates ${target_project_name})
  82. ENDIF()
  83. # Generate XML related to the dependencies
  84. FOREACH(dependency_name ${ctk_dependencies})
  85. LIST(APPEND edges ${dependency_name})
  86. SET(dgraph_list ${dgraph_list} "${target_project_name} ${dependency_name}\n")
  87. LIST(APPEND vertices ${dependency_name})
  88. ENDFOREACH()
  89. ENDIF()
  90. ENDFOREACH()
  91. FOREACH(isolated_vertex_candidate ${isolated_vertex_candidates})
  92. SET(_found 0)
  93. FOREACH(dgraph_entry ${dgraph_list})
  94. STRING(REPLACE "\n" "" dgraph_entry "${dgraph_entry}")
  95. STRING(REPLACE " " ";" dgraph_entry "${dgraph_entry}")
  96. LIST(FIND dgraph_entry ${isolated_vertex_candidate} _index)
  97. IF(_index GREATER -1)
  98. SET(_found 1)
  99. BREAK()
  100. ENDIF()
  101. ENDFOREACH()
  102. IF(NOT _found)
  103. LIST(APPEND vertices ${isolated_vertex_candidate})
  104. SET(dgraph_list "${isolated_vertex_candidate}\n" ${dgraph_list})
  105. ENDIF()
  106. ENDFOREACH()
  107. IF(vertices)
  108. LIST(REMOVE_DUPLICATES vertices)
  109. ENDIF()
  110. LIST(LENGTH vertices numberOfVertices)
  111. LIST(LENGTH edges numberOfEdges)
  112. SET(dgraph_list "${numberOfVertices} ${numberOfEdges}\n" ${dgraph_list})
  113. IF(MY_WITH_OPTION)
  114. SET(filename "${dir}/DGraphInput.txt")
  115. ELSEIF(MY_WITH_EXTERNALS)
  116. SET(filename "${dir}/DGraphInput-alldep-withext.txt")
  117. ELSE()
  118. SET(filename "${dir}/DGraphInput-alldep.txt")
  119. ENDIF()
  120. FILE(WRITE ${filename} ${dgraph_list})
  121. MESSAGE(STATUS "Generated: ${filename}")
  122. ENDFUNCTION()