GetPrerequisites.cmake 25 KB

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