ctkFunctionGenerateDGraphInput.cmake 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.apache.org/licenses/LICENSE-2.0.txt
  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. if(${target_project_name}_OPTIONAL_DEPENDENCIES)
  70. list(APPEND dependencies ${${target_project_name}_OPTIONAL_DEPENDENCIES})
  71. endif()
  72. # Make sure the variable is cleared
  73. set(ctk_dependencies)
  74. if(MY_WITH_EXTERNALS)
  75. set(ctk_dependencies ${dependencies})
  76. else()
  77. # filter dependencies starting with CTK org org_commontk_
  78. ctkMacroGetAllProjectTargetLibraries("${dependencies}" ctk_dependencies)
  79. endif()
  80. if(ctk_dependencies)
  81. list(APPEND vertices ${target_project_name})
  82. else()
  83. # isolated vertex candidate
  84. list(APPEND isolated_vertex_candidates ${target_project_name})
  85. endif()
  86. # Generate XML related to the dependencies
  87. foreach(dependency_name ${ctk_dependencies})
  88. list(APPEND edges ${dependency_name})
  89. set(dgraph_list ${dgraph_list} "${target_project_name} ${dependency_name}\n")
  90. list(APPEND vertices ${dependency_name})
  91. endforeach()
  92. endif()
  93. endforeach()
  94. foreach(isolated_vertex_candidate ${isolated_vertex_candidates})
  95. set(_found 0)
  96. foreach(dgraph_entry ${dgraph_list})
  97. string(REPLACE "\n" "" dgraph_entry "${dgraph_entry}")
  98. string(REPLACE " " ";" dgraph_entry "${dgraph_entry}")
  99. list(FIND dgraph_entry ${isolated_vertex_candidate} _index)
  100. if(_index GREATER -1)
  101. set(_found 1)
  102. break()
  103. endif()
  104. endforeach()
  105. if(NOT _found)
  106. list(APPEND vertices ${isolated_vertex_candidate})
  107. set(dgraph_list "${isolated_vertex_candidate}\n" ${dgraph_list})
  108. endif()
  109. endforeach()
  110. if(vertices)
  111. list(REMOVE_DUPLICATES vertices)
  112. endif()
  113. list(LENGTH vertices numberOfVertices)
  114. list(LENGTH edges numberOfEdges)
  115. set(dgraph_list "${numberOfVertices} ${numberOfEdges}\n" ${dgraph_list})
  116. if(MY_WITH_OPTION)
  117. set(filename "${dir}/DGraphInput.txt")
  118. elseif(MY_WITH_EXTERNALS)
  119. set(filename "${dir}/DGraphInput-alldep-withext.txt")
  120. else()
  121. set(filename "${dir}/DGraphInput-alldep.txt")
  122. endif()
  123. file(WRITE ${filename} ${dgraph_list})
  124. message(STATUS "Generated: ${filename}")
  125. endfunction()