GetPrerequisites.cmake 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. # GetPrerequisites.cmake
  2. #
  3. # This script provides functions to list the .dll, .dylib or .so files that an
  4. # executable or shared library file depends on. (Its prerequisites.)
  5. #
  6. # It uses various tools to obtain the list of required shared library files:
  7. # dumpbin (Windows)
  8. # ldd (Linux/Unix)
  9. # otool (Mac OSX)
  10. #
  11. # The following functions are provided by this script:
  12. # gp_append_unique
  13. # gp_file_type
  14. # is_file_executable
  15. # gp_item_default_embedded_path
  16. # (projects can override with gp_item_default_embedded_path_override)
  17. # gp_resolve_item
  18. # (projects can override with gp_resolve_item_override)
  19. # get_prerequisites
  20. # list_prerequisites
  21. # list_prerequisites_by_glob
  22. #
  23. # Requires CMake 2.6 or greater because it uses function, break, return and
  24. # PARENT_SCOPE.
  25. # gp_append_unique list_var value
  26. #
  27. # Append value to the list variable ${list_var} only if the value is not
  28. # already in the list.
  29. #
  30. function(gp_append_unique list_var value)
  31. set(contains 0)
  32. foreach(item ${${list_var}})
  33. if("${item}" STREQUAL "${value}")
  34. set(contains 1)
  35. break()
  36. endif("${item}" STREQUAL "${value}")
  37. endforeach(item)
  38. if(NOT contains)
  39. set(${list_var} ${${list_var}} "${value}" PARENT_SCOPE)
  40. endif(NOT contains)
  41. endfunction(gp_append_unique)
  42. # gp_file_type original_file file type_var
  43. #
  44. # Return the type of ${file} with respect to ${original_file}. String
  45. # describing type of prerequisite is returned in variable named ${type_var}.
  46. #
  47. # Possible types are:
  48. # system
  49. # local
  50. # embedded
  51. # other
  52. #
  53. function(gp_file_type original_file file type_var)
  54. set(is_embedded 0)
  55. set(is_local 0)
  56. set(is_system 0)
  57. string(TOLOWER "${original_file}" original_lower)
  58. string(TOLOWER "${file}" lower)
  59. if("${file}" MATCHES "^@(executable|loader)_path")
  60. set(is_embedded 1)
  61. endif("${file}" MATCHES "^@(executable|loader)_path")
  62. if(NOT is_embedded)
  63. if(UNIX)
  64. if("${file}" MATCHES "^(/lib/|/lib32/|/lib64/)")
  65. set(is_system 1)
  66. endif("${file}" MATCHES "^(/lib/|/lib32/|/lib64/)")
  67. endif(UNIX)
  68. if(APPLE)
  69. if("${file}" MATCHES "^(/System/Library/|/usr/lib/)")
  70. set(is_system 1)
  71. endif("${file}" MATCHES "^(/System/Library/|/usr/lib/)")
  72. endif(APPLE)
  73. if(WIN32)
  74. string(TOLOWER "$ENV{SystemRoot}" sysroot)
  75. string(REGEX REPLACE "\\\\" "/" sysroot "${sysroot}")
  76. string(TOLOWER "$ENV{windir}" windir)
  77. string(REGEX REPLACE "\\\\" "/" windir "${windir}")
  78. if("${lower}" MATCHES "^(${sysroot}/system|${windir}/system|msvc[^/]+dll)")
  79. set(is_system 1)
  80. endif("${lower}" MATCHES "^(${sysroot}/system|${windir}/system|msvc[^/]+dll)")
  81. endif(WIN32)
  82. if(NOT is_system)
  83. get_filename_component(original_path "${original_lower}" PATH)
  84. get_filename_component(path "${lower}" PATH)
  85. if("${original_path}" STREQUAL "${path}")
  86. set(is_local 1)
  87. endif("${original_path}" STREQUAL "${path}")
  88. endif(NOT is_system)
  89. endif(NOT is_embedded)
  90. # Return type string based on computed booleans:
  91. #
  92. set(type "other")
  93. if(is_system)
  94. set(type "system")
  95. else(is_system)
  96. if(is_embedded)
  97. set(type "embedded")
  98. else(is_embedded)
  99. if(is_local)
  100. set(type "local")
  101. endif(is_local)
  102. endif(is_embedded)
  103. endif(is_system)
  104. set(${type_var} "${type}" PARENT_SCOPE)
  105. endfunction(gp_file_type)
  106. # is_file_executable file result_var
  107. #
  108. # Return 1 in ${result_var} if ${file} is a binary executable.
  109. #
  110. # Return 0 in ${result_var} otherwise.
  111. #
  112. function(is_file_executable file result_var)
  113. #
  114. # A file is not executable until proven otherwise:
  115. #
  116. set(${result_var} 0 PARENT_SCOPE)
  117. get_filename_component(file_full "${file}" ABSOLUTE)
  118. string(TOLOWER "${file_full}" file_full_lower)
  119. # If file name ends in .exe or .dll on Windows, *assume* executable:
  120. #
  121. if(WIN32)
  122. if("${file_full_lower}" MATCHES "\\.(exe|dll)$")
  123. set(${result_var} 1 PARENT_SCOPE)
  124. return()
  125. endif("${file_full_lower}" MATCHES "\\.(exe|dll)$")
  126. # A clause could be added here that uses output or return value of dumpbin
  127. # to determine ${result_var}. In 99%+? practical cases, the exe|dll name
  128. # match will be sufficient...
  129. #
  130. endif(WIN32)
  131. # Use the information returned from the Unix shell command "file" to
  132. # determine if ${file_full} should be considered an executable file...
  133. #
  134. # If the file command's output contains "executable" and does *not* contain
  135. # "text" then it is likely an executable suitable for prerequisite analysis
  136. # via the get_prerequisites macro.
  137. #
  138. if(UNIX)
  139. if(NOT file_cmd)
  140. find_program(file_cmd "file")
  141. endif(NOT file_cmd)
  142. if(file_cmd)
  143. execute_process(COMMAND "${file_cmd}" "${file_full}"
  144. OUTPUT_VARIABLE file_ov
  145. OUTPUT_STRIP_TRAILING_WHITESPACE
  146. )
  147. # Replace the name of the file in the output with a placeholder token
  148. # (the string " _file_full_ ") so that just in case the path name of
  149. # the file contains the word "text" or "executable" we are not fooled
  150. # into thinking "the wrong thing" because the file name matches the
  151. # other 'file' command output we are looking for...
  152. #
  153. string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
  154. string(TOLOWER "${file_ov}" file_ov)
  155. #message(STATUS "file_ov='${file_ov}'")
  156. if("${file_ov}" MATCHES "executable")
  157. #message(STATUS "executable!")
  158. if("${file_ov}" MATCHES "text")
  159. #message(STATUS "but text, so *not* a binary executable!")
  160. else("${file_ov}" MATCHES "text")
  161. set(${result_var} 1 PARENT_SCOPE)
  162. return()
  163. endif("${file_ov}" MATCHES "text")
  164. endif("${file_ov}" MATCHES "executable")
  165. else(file_cmd)
  166. message(STATUS "warning: No 'file' command, skipping execute_process...")
  167. endif(file_cmd)
  168. endif(UNIX)
  169. endfunction(is_file_executable)
  170. # gp_item_default_embedded_path item default_embedded_path_var
  171. #
  172. # Return the path that others should refer to the item by when the item
  173. # is embedded inside a bundle.
  174. #
  175. # Override on a per-project basis by providing a project-specific
  176. # gp_item_default_embedded_path_override function.
  177. #
  178. function(gp_item_default_embedded_path item default_embedded_path_var)
  179. #
  180. # The assumption here is that all executables in the bundle will be
  181. # in same-level-directories inside the bundle. The parent directory
  182. # of an executable inside the bundle should be MacOS or a sibling of
  183. # MacOS and all embedded paths returned from here will begin with
  184. # "@executable_path/../" and will work from all executables in all
  185. # such same-level-directories inside the bundle.
  186. #
  187. # By default, embed things right next to the main bundle executable:
  188. #
  189. set(path "@executable_path/../../Contents/MacOS")
  190. set(overridden 0)
  191. # Embed .dylibs right next to the main bundle executable:
  192. #
  193. if(item MATCHES "\\.dylib$")
  194. set(path "@executable_path/../MacOS")
  195. set(overridden 1)
  196. endif(item MATCHES "\\.dylib$")
  197. # Embed frameworks in the embedded "Frameworks" directory (sibling of MacOS):
  198. #
  199. if(NOT overridden)
  200. if(item MATCHES "[^/]+\\.framework/")
  201. set(path "@executable_path/../Frameworks")
  202. set(overridden 1)
  203. endif(item MATCHES "[^/]+\\.framework/")
  204. endif(NOT overridden)
  205. # Provide a hook so that projects can override the default embedded location of
  206. # any given library by whatever logic they choose:
  207. #
  208. if(COMMAND gp_item_default_embedded_path_override)
  209. gp_item_default_embedded_path_override("${item}" path)
  210. endif(COMMAND gp_item_default_embedded_path_override)
  211. set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
  212. endfunction(gp_item_default_embedded_path)
  213. # gp_resolve_item context item exepath dirs resolved_item_var
  214. #
  215. # Resolve an item into an existing full path file.
  216. #
  217. # Override on a per-project basis by providing a project-specific
  218. # gp_resolve_item_override function.
  219. #
  220. function(gp_resolve_item context item exepath dirs resolved_item_var)
  221. set(resolved 0)
  222. set(resolved_item "${item}")
  223. # Is it already resolved?
  224. #
  225. if(EXISTS "${resolved_item}")
  226. set(resolved 1)
  227. endif(EXISTS "${resolved_item}")
  228. if(NOT resolved)
  229. if(item MATCHES "@executable_path")
  230. #
  231. # @executable_path references are assumed relative to exepath
  232. #
  233. string(REPLACE "@executable_path" "${exepath}" ri "${item}")
  234. get_filename_component(ri "${ri}" ABSOLUTE)
  235. if(EXISTS "${ri}")
  236. #message(STATUS "info: embedded item exists (${ri})")
  237. set(resolved 1)
  238. set(resolved_item "${ri}")
  239. else(EXISTS "${ri}")
  240. message(STATUS "info: embedded item does not exist '${ri}'")
  241. endif(EXISTS "${ri}")
  242. endif(item MATCHES "@executable_path")
  243. endif(NOT resolved)
  244. if(NOT resolved)
  245. if(item MATCHES "@loader_path")
  246. #
  247. # @loader_path references are assumed relative to the
  248. # PATH of the given "context" (presumably another library)
  249. #
  250. get_filename_component(contextpath "${context}" PATH)
  251. string(REPLACE "@loader_path" "${contextpath}" ri "${item}")
  252. get_filename_component(ri "${ri}" ABSOLUTE)
  253. if(EXISTS "${ri}")
  254. #message(STATUS "info: embedded item exists (${ri})")
  255. set(resolved 1)
  256. set(resolved_item "${ri}")
  257. else(EXISTS "${ri}")
  258. message(STATUS "info: embedded item does not exist '${ri}'")
  259. endif(EXISTS "${ri}")
  260. endif(item MATCHES "@loader_path")
  261. endif(NOT resolved)
  262. if(NOT resolved)
  263. set(ri "ri-NOTFOUND")
  264. find_file(ri "${item}" ${dirs})
  265. if(ri)
  266. #message(STATUS "info: found item in dirs (${ri})")
  267. set(resolved 1)
  268. set(resolved_item "${ri}")
  269. set(ri "ri-NOTFOUND")
  270. endif(ri)
  271. endif(NOT resolved)
  272. if(NOT resolved)
  273. if(item MATCHES "[^/]+\\.framework/")
  274. set(fw "fw-NOTFOUND")
  275. find_file(fw "${item}"
  276. "~/Library/Frameworks"
  277. "/Library/Frameworks"
  278. "/System/Library/Frameworks"
  279. )
  280. if(fw)
  281. #message(STATUS "info: found framework (${fw})")
  282. set(resolved 1)
  283. set(resolved_item "${fw}")
  284. set(fw "fw-NOTFOUND")
  285. endif(fw)
  286. endif(item MATCHES "[^/]+\\.framework/")
  287. endif(NOT resolved)
  288. # Using find_program on Windows will find dll files that are in the PATH.
  289. # (Converting simple file names into full path names if found.)
  290. #
  291. if(WIN32)
  292. if(NOT resolved)
  293. set(ri "ri-NOTFOUND")
  294. find_program(ri "${item}" PATHS "${dirs}")
  295. if(ri)
  296. set(resolved 1)
  297. set(resolved_item "${ri}")
  298. set(ri "ri-NOTFOUND")
  299. endif(ri)
  300. endif(NOT resolved)
  301. endif(WIN32)
  302. # Provide a hook so that projects can override item resolution
  303. # by whatever logic they choose:
  304. #
  305. if(COMMAND gp_resolve_item_override)
  306. gp_resolve_item_override("${context}" "${item}" "${exepath}" "${dirs}" resolved_item resolved)
  307. endif(COMMAND gp_resolve_item_override)
  308. if(NOT resolved)
  309. message(STATUS "warning: cannot resolve item '${item}'")
  310. endif(NOT resolved)
  311. set(${resolved_item_var} "${resolved_item}" PARENT_SCOPE)
  312. endfunction(gp_resolve_item)
  313. # get_prerequisites target prerequisites_var exclude_system recurse dirs
  314. #
  315. # Get the list of shared library files required by ${target}. The list in
  316. # the variable named ${prerequisites_var} should be empty on first entry to
  317. # this function. On exit, ${prerequisites_var} will contain the list of
  318. # required shared library files.
  319. #
  320. # target is the full path to an executable file
  321. #
  322. # prerequisites_var is the name of a CMake variable to contain the results
  323. #
  324. # exclude_system is 0 or 1: 0 to include "system" prerequisites , 1 to
  325. # exclude them
  326. #
  327. # recurse is 0 or 1: 0 for direct prerequisites only, 1 for all prerequisites
  328. # recursively
  329. #
  330. # exepath is the path to the top level executable used for @executable_path
  331. # replacment on the Mac
  332. #
  333. # dirs is a list of paths where libraries might be found: these paths are
  334. # searched first when a target without any path info is given. Then standard
  335. # system locations are also searched: PATH, Framework locations, /usr/lib...
  336. #
  337. function(get_prerequisites target prerequisites_var exclude_system recurse exepath dirs)
  338. set(verbose 0)
  339. set(eol_char "E")
  340. if(NOT IS_ABSOLUTE "${target}")
  341. message("warning: target '${target}' is not absolute...")
  342. endif(NOT IS_ABSOLUTE "${target}")
  343. if(NOT EXISTS "${target}")
  344. message("warning: target '${target}' does not exist...")
  345. endif(NOT EXISTS "${target}")
  346. # <setup-gp_tool-vars>
  347. #
  348. # Try to choose the right tool by default. Caller can set gp_tool prior to
  349. # calling this function to force using a different tool.
  350. #
  351. if("${gp_tool}" STREQUAL "")
  352. set(gp_tool "ldd")
  353. if(APPLE)
  354. set(gp_tool "otool")
  355. endif(APPLE)
  356. if(WIN32)
  357. set(gp_tool "dumpbin")
  358. endif(WIN32)
  359. endif("${gp_tool}" STREQUAL "")
  360. set(gp_tool_known 0)
  361. if("${gp_tool}" STREQUAL "ldd")
  362. set(gp_cmd_args "")
  363. set(gp_regex "^[\t ]*[^\t ]+ => ([^\t ]+).*${eol_char}$")
  364. set(gp_regex_cmp_count 1)
  365. set(gp_tool_known 1)
  366. endif("${gp_tool}" STREQUAL "ldd")
  367. if("${gp_tool}" STREQUAL "otool")
  368. set(gp_cmd_args "-L")
  369. set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
  370. set(gp_regex_cmp_count 3)
  371. set(gp_tool_known 1)
  372. endif("${gp_tool}" STREQUAL "otool")
  373. if("${gp_tool}" STREQUAL "dumpbin")
  374. set(gp_cmd_args "/dependents")
  375. set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
  376. set(gp_regex_cmp_count 1)
  377. set(gp_tool_known 1)
  378. set(ENV{VS_UNICODE_OUTPUT} "") # Block extra output from inside VS IDE.
  379. endif("${gp_tool}" STREQUAL "dumpbin")
  380. if(NOT gp_tool_known)
  381. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  382. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  383. message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
  384. return()
  385. endif(NOT gp_tool_known)
  386. set(gp_cmd_paths ${gp_cmd_paths}
  387. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  388. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  389. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  390. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  391. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  392. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  393. "/usr/local/bin"
  394. "/usr/bin"
  395. )
  396. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  397. if(NOT gp_cmd)
  398. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  399. return()
  400. endif(NOT gp_cmd)
  401. if("${gp_tool}" STREQUAL "dumpbin")
  402. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  403. # PATH. It will already be in the PATH if being run from a Visual Studio
  404. # command prompt. Add it to the PATH here in case we are running from a
  405. # different command prompt.
  406. #
  407. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  408. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  409. if(EXISTS "${gp_cmd_dlls_dir}")
  410. # only add to the path if it is not already in the path
  411. if(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
  412. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  413. endif(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
  414. endif(EXISTS "${gp_cmd_dlls_dir}")
  415. endif("${gp_tool}" STREQUAL "dumpbin")
  416. #
  417. # </setup-gp_tool-vars>
  418. # Track new prerequisites at each new level of recursion. Start with an
  419. # empty list at each level:
  420. #
  421. set(unseen_prereqs)
  422. # Run gp_cmd on the target:
  423. #
  424. execute_process(
  425. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  426. OUTPUT_VARIABLE gp_cmd_ov
  427. )
  428. if(verbose)
  429. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  430. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  431. message(STATUS "</RawOutput>")
  432. endif(verbose)
  433. get_filename_component(target_dir "${target}" PATH)
  434. # Convert to a list of lines:
  435. #
  436. string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
  437. string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
  438. # Analyze each line for file names that match the regular expression:
  439. #
  440. foreach(candidate ${candidates})
  441. if("${candidate}" MATCHES "${gp_regex}")
  442. # Extract information from each candidate:
  443. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  444. if(gp_regex_cmp_count GREATER 1)
  445. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  446. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  447. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  448. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  449. endif(gp_regex_cmp_count GREATER 1)
  450. if(gp_regex_cmp_count GREATER 2)
  451. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  452. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  453. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  454. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  455. endif(gp_regex_cmp_count GREATER 2)
  456. # Use the raw_item as the list entries returned by this function. Use the
  457. # gp_resolve_item function to resolve it to an actual full path file if
  458. # necessary.
  459. #
  460. set(item "${raw_item}")
  461. # Add each item unless it is excluded:
  462. #
  463. set(add_item 1)
  464. if(${exclude_system})
  465. set(type "")
  466. gp_file_type("${target}" "${item}" type)
  467. if("${type}" STREQUAL "system")
  468. set(add_item 0)
  469. endif("${type}" STREQUAL "system")
  470. endif(${exclude_system})
  471. if(add_item)
  472. list(LENGTH ${prerequisites_var} list_length_before_append)
  473. gp_append_unique(${prerequisites_var} "${item}")
  474. list(LENGTH ${prerequisites_var} list_length_after_append)
  475. if(${recurse})
  476. # If item was really added, this is the first time we have seen it.
  477. # Add it to unseen_prereqs so that we can recursively add *its*
  478. # prerequisites...
  479. #
  480. # But first: resolve its name to an absolute full path name such
  481. # that the analysis tools can simply accept it as input.
  482. #
  483. if(NOT list_length_before_append EQUAL list_length_after_append)
  484. gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item)
  485. set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
  486. endif(NOT list_length_before_append EQUAL list_length_after_append)
  487. endif(${recurse})
  488. endif(add_item)
  489. else("${candidate}" MATCHES "${gp_regex}")
  490. if(verbose)
  491. message(STATUS "ignoring non-matching line: '${candidate}'")
  492. endif(verbose)
  493. endif("${candidate}" MATCHES "${gp_regex}")
  494. endforeach(candidate)
  495. list(SORT ${prerequisites_var})
  496. if(${recurse})
  497. set(more_inputs ${unseen_prereqs})
  498. foreach(input ${more_inputs})
  499. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}")
  500. endforeach(input)
  501. endif(${recurse})
  502. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  503. endfunction(get_prerequisites)
  504. # list_prerequisites target all exclude_system verbose
  505. #
  506. # ARGV0 (target) is the full path to an executable file
  507. #
  508. # optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
  509. # 1 for all prerequisites recursively
  510. #
  511. # optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
  512. # prerequisites , 1 to exclude them
  513. #
  514. # optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
  515. # names of prerequisites, 1 to print extra information
  516. #
  517. function(list_prerequisites target)
  518. if("${ARGV1}" STREQUAL "")
  519. set(all 1)
  520. else("${ARGV1}" STREQUAL "")
  521. set(all "${ARGV1}")
  522. endif("${ARGV1}" STREQUAL "")
  523. if("${ARGV2}" STREQUAL "")
  524. set(exclude_system 0)
  525. else("${ARGV2}" STREQUAL "")
  526. set(exclude_system "${ARGV2}")
  527. endif("${ARGV2}" STREQUAL "")
  528. if("${ARGV3}" STREQUAL "")
  529. set(verbose 0)
  530. else("${ARGV3}" STREQUAL "")
  531. set(verbose "${ARGV3}")
  532. endif("${ARGV3}" STREQUAL "")
  533. set(count 0)
  534. set(count_str "")
  535. set(print_count "${verbose}")
  536. set(print_prerequisite_type "${verbose}")
  537. set(print_target "${verbose}")
  538. set(type_str "")
  539. get_filename_component(exepath "${target}" PATH)
  540. set(prereqs "")
  541. get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
  542. if(print_target)
  543. message(STATUS "File '${target}' depends on:")
  544. endif(print_target)
  545. foreach(d ${prereqs})
  546. math(EXPR count "${count} + 1")
  547. if(print_count)
  548. set(count_str "${count}. ")
  549. endif(print_count)
  550. if(print_prerequisite_type)
  551. gp_file_type("${target}" "${d}" type)
  552. set(type_str " (${type})")
  553. endif(print_prerequisite_type)
  554. message(STATUS "${count_str}${d}${type_str}")
  555. endforeach(d)
  556. endfunction(list_prerequisites)
  557. # list_prerequisites_by_glob glob_arg glob_exp
  558. #
  559. # glob_arg is GLOB or GLOB_RECURSE
  560. #
  561. # glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
  562. # of matching files. If a matching file is executable, its prerequisites are
  563. # listed.
  564. #
  565. # Any additional (optional) arguments provided are passed along as the
  566. # optional arguments to the list_prerequisites calls.
  567. #
  568. function(list_prerequisites_by_glob glob_arg glob_exp)
  569. message(STATUS "=============================================================================")
  570. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  571. message(STATUS "")
  572. file(${glob_arg} file_list ${glob_exp})
  573. foreach(f ${file_list})
  574. is_file_executable("${f}" is_f_executable)
  575. if(is_f_executable)
  576. message(STATUS "=============================================================================")
  577. list_prerequisites("${f}" ${ARGN})
  578. message(STATUS "")
  579. endif(is_f_executable)
  580. endforeach(f)
  581. endfunction(list_prerequisites_by_glob)