GetPrerequisites.cmake 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. #.rst:
  2. # GetPrerequisites
  3. # ----------------
  4. #
  5. # Functions to analyze and list executable file prerequisites.
  6. #
  7. # This module provides functions to list the .dll, .dylib or .so files
  8. # that an executable or shared library file depends on. (Its
  9. # prerequisites.)
  10. #
  11. # It uses various tools to obtain the list of required shared library
  12. # files:
  13. #
  14. # ::
  15. #
  16. # dumpbin (Windows)
  17. # objdump (MinGW on Windows)
  18. # ldd (Linux/Unix)
  19. # otool (Mac OSX)
  20. #
  21. # The following functions are provided by this module:
  22. #
  23. # ::
  24. #
  25. # get_prerequisites
  26. # list_prerequisites
  27. # list_prerequisites_by_glob
  28. # gp_append_unique
  29. # is_file_executable
  30. # gp_item_default_embedded_path
  31. # (projects can override with gp_item_default_embedded_path_override)
  32. # gp_resolve_item
  33. # (projects can override with gp_resolve_item_override)
  34. # gp_resolved_file_type
  35. # (projects can override with gp_resolved_file_type_override)
  36. # gp_file_type
  37. #
  38. # Requires CMake 2.6 or greater because it uses function, break, return
  39. # and PARENT_SCOPE.
  40. #
  41. # ::
  42. #
  43. # GET_PREREQUISITES(<target> <prerequisites_var> <exclude_system> <recurse>
  44. # <exepath> <dirs> [<rpaths>])
  45. #
  46. # Get the list of shared library files required by <target>. The list
  47. # in the variable named <prerequisites_var> should be empty on first
  48. # entry to this function. On exit, <prerequisites_var> will contain the
  49. # list of required shared library files.
  50. #
  51. # <target> is the full path to an executable file. <prerequisites_var>
  52. # is the name of a CMake variable to contain the results.
  53. # <exclude_system> must be 0 or 1 indicating whether to include or
  54. # exclude "system" prerequisites. If <recurse> is set to 1 all
  55. # prerequisites will be found recursively, if set to 0 only direct
  56. # prerequisites are listed. <exepath> is the path to the top level
  57. # executable used for @executable_path replacment on the Mac. <dirs> is
  58. # a list of paths where libraries might be found: these paths are
  59. # searched first when a target without any path info is given. Then
  60. # standard system locations are also searched: PATH, Framework
  61. # locations, /usr/lib...
  62. #
  63. # ::
  64. #
  65. # LIST_PREREQUISITES(<target> [<recurse> [<exclude_system> [<verbose>]]])
  66. #
  67. # Print a message listing the prerequisites of <target>.
  68. #
  69. # <target> is the name of a shared library or executable target or the
  70. # full path to a shared library or executable file. If <recurse> is set
  71. # to 1 all prerequisites will be found recursively, if set to 0 only
  72. # direct prerequisites are listed. <exclude_system> must be 0 or 1
  73. # indicating whether to include or exclude "system" prerequisites. With
  74. # <verbose> set to 0 only the full path names of the prerequisites are
  75. # printed, set to 1 extra informatin will be displayed.
  76. #
  77. # ::
  78. #
  79. # LIST_PREREQUISITES_BY_GLOB(<glob_arg> <glob_exp>)
  80. #
  81. # Print the prerequisites of shared library and executable files
  82. # matching a globbing pattern. <glob_arg> is GLOB or GLOB_RECURSE and
  83. # <glob_exp> is a globbing expression used with "file(GLOB" or
  84. # "file(GLOB_RECURSE" to retrieve a list of matching files. If a
  85. # matching file is executable, its prerequisites are listed.
  86. #
  87. # Any additional (optional) arguments provided are passed along as the
  88. # optional arguments to the list_prerequisites calls.
  89. #
  90. # ::
  91. #
  92. # GP_APPEND_UNIQUE(<list_var> <value>)
  93. #
  94. # Append <value> to the list variable <list_var> only if the value is
  95. # not already in the list.
  96. #
  97. # ::
  98. #
  99. # IS_FILE_EXECUTABLE(<file> <result_var>)
  100. #
  101. # Return 1 in <result_var> if <file> is a binary executable, 0
  102. # otherwise.
  103. #
  104. # ::
  105. #
  106. # GP_ITEM_DEFAULT_EMBEDDED_PATH(<item> <default_embedded_path_var>)
  107. #
  108. # Return the path that others should refer to the item by when the item
  109. # is embedded inside a bundle.
  110. #
  111. # Override on a per-project basis by providing a project-specific
  112. # gp_item_default_embedded_path_override function.
  113. #
  114. # ::
  115. #
  116. # GP_RESOLVE_ITEM(<context> <item> <exepath> <dirs> <resolved_item_var>
  117. # [<rpaths>])
  118. #
  119. # Resolve an item into an existing full path file.
  120. #
  121. # Override on a per-project basis by providing a project-specific
  122. # gp_resolve_item_override function.
  123. #
  124. # ::
  125. #
  126. # GP_RESOLVED_FILE_TYPE(<original_file> <file> <exepath> <dirs> <type_var>
  127. # [<rpaths>])
  128. #
  129. # Return the type of <file> with respect to <original_file>. String
  130. # describing type of prerequisite is returned in variable named
  131. # <type_var>.
  132. #
  133. # Use <exepath> and <dirs> if necessary to resolve non-absolute <file>
  134. # values -- but only for non-embedded items.
  135. #
  136. # Possible types are:
  137. #
  138. # ::
  139. #
  140. # system
  141. # local
  142. # embedded
  143. # other
  144. #
  145. # Override on a per-project basis by providing a project-specific
  146. # gp_resolved_file_type_override function.
  147. #
  148. # ::
  149. #
  150. # GP_FILE_TYPE(<original_file> <file> <type_var>)
  151. #
  152. # Return the type of <file> with respect to <original_file>. String
  153. # describing type of prerequisite is returned in variable named
  154. # <type_var>.
  155. #
  156. # Possible types are:
  157. #
  158. # ::
  159. #
  160. # system
  161. # local
  162. # embedded
  163. # other
  164. #=============================================================================
  165. # Copyright 2008-2009 Kitware, Inc.
  166. #
  167. # Distributed under the OSI-approved BSD License (the "License");
  168. # see accompanying file Copyright.txt for details.
  169. #
  170. # This software is distributed WITHOUT ANY WARRANTY; without even the
  171. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  172. # See the License for more information.
  173. #=============================================================================
  174. # (To distribute this file outside of CMake, substitute the full
  175. # License text for the above reference.)
  176. function(gp_append_unique list_var value)
  177. set(contains 0)
  178. foreach(item ${${list_var}})
  179. if(item STREQUAL "${value}")
  180. set(contains 1)
  181. break()
  182. endif()
  183. endforeach()
  184. if(NOT contains)
  185. set(${list_var} ${${list_var}} "${value}" PARENT_SCOPE)
  186. endif()
  187. endfunction()
  188. function(is_file_executable file result_var)
  189. #
  190. # A file is not executable until proven otherwise:
  191. #
  192. set(${result_var} 0 PARENT_SCOPE)
  193. get_filename_component(file_full "${file}" ABSOLUTE)
  194. string(TOLOWER "${file_full}" file_full_lower)
  195. # If file name ends in .exe on Windows, *assume* executable:
  196. #
  197. if(WIN32 AND NOT UNIX)
  198. if("${file_full_lower}" MATCHES "\\.exe$")
  199. set(${result_var} 1 PARENT_SCOPE)
  200. return()
  201. endif()
  202. # A clause could be added here that uses output or return value of dumpbin
  203. # to determine ${result_var}. In 99%+? practical cases, the exe name
  204. # match will be sufficient...
  205. #
  206. endif()
  207. # Use the information returned from the Unix shell command "file" to
  208. # determine if ${file_full} should be considered an executable file...
  209. #
  210. # If the file command's output contains "executable" and does *not* contain
  211. # "text" then it is likely an executable suitable for prerequisite analysis
  212. # via the get_prerequisites macro.
  213. #
  214. if(UNIX)
  215. if(NOT file_cmd)
  216. find_program(file_cmd "file")
  217. mark_as_advanced(file_cmd)
  218. endif()
  219. if(file_cmd)
  220. execute_process(COMMAND "${file_cmd}" "${file_full}"
  221. OUTPUT_VARIABLE file_ov
  222. OUTPUT_STRIP_TRAILING_WHITESPACE
  223. )
  224. # Replace the name of the file in the output with a placeholder token
  225. # (the string " _file_full_ ") so that just in case the path name of
  226. # the file contains the word "text" or "executable" we are not fooled
  227. # into thinking "the wrong thing" because the file name matches the
  228. # other 'file' command output we are looking for...
  229. #
  230. string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
  231. string(TOLOWER "${file_ov}" file_ov)
  232. #message(STATUS "file_ov='${file_ov}'")
  233. if("${file_ov}" MATCHES "executable")
  234. #message(STATUS "executable!")
  235. if("${file_ov}" MATCHES "text")
  236. #message(STATUS "but text, so *not* a binary executable!")
  237. else()
  238. set(${result_var} 1 PARENT_SCOPE)
  239. return()
  240. endif()
  241. endif()
  242. # Also detect position independent executables on Linux,
  243. # where "file" gives "shared object ... (uses shared libraries)"
  244. if("${file_ov}" MATCHES "shared object.*\(uses shared libs\)")
  245. set(${result_var} 1 PARENT_SCOPE)
  246. return()
  247. endif()
  248. # "file" version 5.22 does not print "(used shared libraries)"
  249. # but uses "interpreter"
  250. if("${file_ov}" MATCHES "shared object.*interpreter")
  251. set(${result_var} 1 PARENT_SCOPE)
  252. return()
  253. endif()
  254. else()
  255. message(STATUS "warning: No 'file' command, skipping execute_process...")
  256. endif()
  257. endif()
  258. endfunction()
  259. function(gp_item_default_embedded_path item default_embedded_path_var)
  260. # On Windows and Linux, "embed" prerequisites in the same directory
  261. # as the executable by default:
  262. #
  263. set(path "@executable_path")
  264. set(overridden 0)
  265. # On the Mac, relative to the executable depending on the type
  266. # of the thing we are embedding:
  267. #
  268. if(APPLE)
  269. #
  270. # The assumption here is that all executables in the bundle will be
  271. # in same-level-directories inside the bundle. The parent directory
  272. # of an executable inside the bundle should be MacOS or a sibling of
  273. # MacOS and all embedded paths returned from here will begin with
  274. # "@executable_path/../" and will work from all executables in all
  275. # such same-level-directories inside the bundle.
  276. #
  277. # By default, embed things right next to the main bundle executable:
  278. #
  279. set(path "@executable_path/../../Contents/MacOS")
  280. # Embed .dylibs right next to the main bundle executable:
  281. #
  282. if(item MATCHES "\\.dylib$")
  283. set(path "@executable_path/../MacOS")
  284. set(overridden 1)
  285. endif()
  286. # Embed frameworks in the embedded "Frameworks" directory (sibling of MacOS):
  287. #
  288. if(NOT overridden)
  289. if(item MATCHES "[^/]+\\.framework/")
  290. set(path "@executable_path/../Frameworks")
  291. set(overridden 1)
  292. endif()
  293. endif()
  294. endif()
  295. # Provide a hook so that projects can override the default embedded location
  296. # of any given library by whatever logic they choose:
  297. #
  298. if(COMMAND gp_item_default_embedded_path_override)
  299. gp_item_default_embedded_path_override("${item}" path)
  300. endif()
  301. set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
  302. endfunction()
  303. function(gp_resolve_item context item exepath dirs resolved_item_var)
  304. set(resolved 0)
  305. set(resolved_item "${item}")
  306. if(ARGC GREATER 5)
  307. set(rpaths "${ARGV5}")
  308. else()
  309. set(rpaths "")
  310. endif()
  311. # Is it already resolved?
  312. #
  313. if(IS_ABSOLUTE "${resolved_item}" AND EXISTS "${resolved_item}")
  314. set(resolved 1)
  315. endif()
  316. if(NOT resolved)
  317. if(item MATCHES "^@executable_path")
  318. #
  319. # @executable_path references are assumed relative to exepath
  320. #
  321. string(REPLACE "@executable_path" "${exepath}" ri "${item}")
  322. get_filename_component(ri "${ri}" ABSOLUTE)
  323. if(EXISTS "${ri}")
  324. #message(STATUS "info: embedded item exists (${ri})")
  325. set(resolved 1)
  326. set(resolved_item "${ri}")
  327. else()
  328. message(STATUS "warning: embedded item does not exist '${ri}'")
  329. endif()
  330. endif()
  331. endif()
  332. if(NOT resolved)
  333. if(item MATCHES "^@loader_path")
  334. #
  335. # @loader_path references are assumed relative to the
  336. # PATH of the given "context" (presumably another library)
  337. #
  338. get_filename_component(contextpath "${context}" PATH)
  339. string(REPLACE "@loader_path" "${contextpath}" ri "${item}")
  340. get_filename_component(ri "${ri}" ABSOLUTE)
  341. if(EXISTS "${ri}")
  342. #message(STATUS "info: embedded item exists (${ri})")
  343. set(resolved 1)
  344. set(resolved_item "${ri}")
  345. else()
  346. message(STATUS "warning: embedded item does not exist '${ri}'")
  347. endif()
  348. endif()
  349. endif()
  350. if(NOT resolved)
  351. if(item MATCHES "^@rpath")
  352. #
  353. # @rpath references are relative to the paths built into the binaries with -rpath
  354. # We handle this case like we do for other Unixes
  355. #
  356. string(REPLACE "@rpath/" "" norpath_item "${item}")
  357. set(ri "ri-NOTFOUND")
  358. find_file(ri "${norpath_item}" ${exepath} ${dirs} ${rpaths} NO_DEFAULT_PATH)
  359. if(ri)
  360. #message(STATUS "info: 'find_file' in exepath/dirs/rpaths (${ri})")
  361. set(resolved 1)
  362. set(resolved_item "${ri}")
  363. set(ri "ri-NOTFOUND")
  364. endif()
  365. endif()
  366. endif()
  367. if(NOT resolved)
  368. set(ri "ri-NOTFOUND")
  369. find_file(ri "${item}" ${exepath} ${dirs} NO_DEFAULT_PATH)
  370. find_file(ri "${item}" ${exepath} ${dirs} /usr/lib)
  371. if(ri)
  372. #message(STATUS "info: 'find_file' in exepath/dirs (${ri})")
  373. set(resolved 1)
  374. set(resolved_item "${ri}")
  375. set(ri "ri-NOTFOUND")
  376. endif()
  377. endif()
  378. if(NOT resolved)
  379. if(item MATCHES "[^/]+\\.framework/")
  380. set(fw "fw-NOTFOUND")
  381. find_file(fw "${item}"
  382. "~/Library/Frameworks"
  383. "/Library/Frameworks"
  384. "/System/Library/Frameworks"
  385. )
  386. if(fw)
  387. #message(STATUS "info: 'find_file' found framework (${fw})")
  388. set(resolved 1)
  389. set(resolved_item "${fw}")
  390. set(fw "fw-NOTFOUND")
  391. endif()
  392. endif()
  393. endif()
  394. # Using find_program on Windows will find dll files that are in the PATH.
  395. # (Converting simple file names into full path names if found.)
  396. #
  397. if(WIN32 AND NOT UNIX)
  398. if(NOT resolved)
  399. set(ri "ri-NOTFOUND")
  400. find_program(ri "${item}" PATHS "${exepath};${dirs}" NO_DEFAULT_PATH)
  401. find_program(ri "${item}" PATHS "${exepath};${dirs}")
  402. if(ri)
  403. #message(STATUS "info: 'find_program' in exepath/dirs (${ri})")
  404. set(resolved 1)
  405. set(resolved_item "${ri}")
  406. set(ri "ri-NOTFOUND")
  407. endif()
  408. endif()
  409. endif()
  410. # Provide a hook so that projects can override item resolution
  411. # by whatever logic they choose:
  412. #
  413. if(COMMAND gp_resolve_item_override)
  414. gp_resolve_item_override("${context}" "${item}" "${exepath}" "${dirs}" resolved_item resolved)
  415. endif()
  416. if(NOT resolved)
  417. message(STATUS "
  418. warning: cannot resolve item '${item}'
  419. possible problems:
  420. need more directories?
  421. need to use InstallRequiredSystemLibraries?
  422. run in install tree instead of build tree?
  423. ")
  424. # message(STATUS "
  425. #******************************************************************************
  426. #warning: cannot resolve item '${item}'
  427. #
  428. # possible problems:
  429. # need more directories?
  430. # need to use InstallRequiredSystemLibraries?
  431. # run in install tree instead of build tree?
  432. #
  433. # context='${context}'
  434. # item='${item}'
  435. # exepath='${exepath}'
  436. # dirs='${dirs}'
  437. # resolved_item_var='${resolved_item_var}'
  438. #******************************************************************************
  439. #")
  440. endif()
  441. set(${resolved_item_var} "${resolved_item}" PARENT_SCOPE)
  442. endfunction()
  443. function(gp_resolved_file_type original_file file exepath dirs type_var)
  444. if(ARGC GREATER 5)
  445. set(rpaths "${ARGV5}")
  446. else()
  447. set(rpaths "")
  448. endif()
  449. #message(STATUS "**")
  450. if(NOT IS_ABSOLUTE "${original_file}")
  451. message(STATUS "warning: gp_resolved_file_type expects absolute full path for first arg original_file")
  452. endif()
  453. set(is_embedded 0)
  454. set(is_local 0)
  455. set(is_system 0)
  456. set(resolved_file "${file}")
  457. if("${file}" MATCHES "^@(executable|loader)_path")
  458. set(is_embedded 1)
  459. endif()
  460. if(NOT is_embedded)
  461. if(NOT IS_ABSOLUTE "${file}")
  462. gp_resolve_item("${original_file}" "${file}" "${exepath}" "${dirs}" resolved_file "${rpaths}")
  463. endif()
  464. string(TOLOWER "${original_file}" original_lower)
  465. string(TOLOWER "${resolved_file}" lower)
  466. if(UNIX)
  467. if(resolved_file MATCHES "^(/lib/|/lib32/|/lib64/|/usr/lib/|/usr/lib32/|/usr/lib64/|/usr/X11R6/|/usr/bin/)")
  468. set(is_system 1)
  469. endif()
  470. endif()
  471. if(APPLE)
  472. if(resolved_file MATCHES "^(/System/Library/|/usr/lib/)")
  473. set(is_system 1)
  474. endif()
  475. endif()
  476. if(WIN32)
  477. string(TOLOWER "$ENV{SystemRoot}" sysroot)
  478. file(TO_CMAKE_PATH "${sysroot}" sysroot)
  479. string(TOLOWER "$ENV{windir}" windir)
  480. file(TO_CMAKE_PATH "${windir}" windir)
  481. if(lower MATCHES "^(${sysroot}/sys(tem|wow)|${windir}/sys(tem|wow)|(.*/)*msvc[^/]+dll)")
  482. set(is_system 1)
  483. endif()
  484. if(UNIX)
  485. # if cygwin, we can get the properly formed windows paths from cygpath
  486. find_program(CYGPATH_EXECUTABLE cygpath)
  487. if(CYGPATH_EXECUTABLE)
  488. execute_process(COMMAND ${CYGPATH_EXECUTABLE} -W
  489. OUTPUT_VARIABLE env_windir
  490. OUTPUT_STRIP_TRAILING_WHITESPACE)
  491. execute_process(COMMAND ${CYGPATH_EXECUTABLE} -S
  492. OUTPUT_VARIABLE env_sysdir
  493. OUTPUT_STRIP_TRAILING_WHITESPACE)
  494. string(TOLOWER "${env_windir}" windir)
  495. string(TOLOWER "${env_sysdir}" sysroot)
  496. if(lower MATCHES "^(${sysroot}/sys(tem|wow)|${windir}/sys(tem|wow)|(.*/)*msvc[^/]+dll)")
  497. set(is_system 1)
  498. endif()
  499. endif()
  500. endif()
  501. endif()
  502. if(NOT is_system)
  503. get_filename_component(original_path "${original_lower}" PATH)
  504. get_filename_component(path "${lower}" PATH)
  505. if(original_path STREQUAL path)
  506. set(is_local 1)
  507. else()
  508. string(LENGTH "${original_path}/" original_length)
  509. string(LENGTH "${lower}" path_length)
  510. if(${path_length} GREATER ${original_length})
  511. string(SUBSTRING "${lower}" 0 ${original_length} path)
  512. if("${original_path}/" STREQUAL path)
  513. set(is_embedded 1)
  514. endif()
  515. endif()
  516. endif()
  517. endif()
  518. endif()
  519. # Return type string based on computed booleans:
  520. #
  521. set(type "other")
  522. if(is_system)
  523. set(type "system")
  524. elseif(is_embedded)
  525. set(type "embedded")
  526. elseif(is_local)
  527. set(type "local")
  528. endif()
  529. #message(STATUS "gp_resolved_file_type: '${file}' '${resolved_file}'")
  530. #message(STATUS " type: '${type}'")
  531. if(NOT is_embedded)
  532. if(NOT IS_ABSOLUTE "${resolved_file}")
  533. if(lower MATCHES "^msvc[^/]+dll" AND is_system)
  534. message(STATUS "info: non-absolute msvc file '${file}' returning type '${type}'")
  535. else()
  536. message(STATUS "warning: gp_resolved_file_type non-absolute file '${file}' returning type '${type}' -- possibly incorrect")
  537. endif()
  538. endif()
  539. endif()
  540. # Provide a hook so that projects can override the decision on whether a
  541. # library belongs to the system or not by whatever logic they choose:
  542. #
  543. if(COMMAND gp_resolved_file_type_override)
  544. gp_resolved_file_type_override("${resolved_file}" type)
  545. endif()
  546. set(${type_var} "${type}" PARENT_SCOPE)
  547. #message(STATUS "**")
  548. endfunction()
  549. function(gp_file_type original_file file type_var)
  550. if(NOT IS_ABSOLUTE "${original_file}")
  551. message(STATUS "warning: gp_file_type expects absolute full path for first arg original_file")
  552. endif()
  553. get_filename_component(exepath "${original_file}" PATH)
  554. set(type "")
  555. gp_resolved_file_type("${original_file}" "${file}" "${exepath}" "" type)
  556. set(${type_var} "${type}" PARENT_SCOPE)
  557. endfunction()
  558. function(get_prerequisites target prerequisites_var exclude_system recurse exepath dirs)
  559. set(verbose 0)
  560. set(eol_char "E")
  561. if(ARGC GREATER 6)
  562. set(rpaths "${ARGV6}")
  563. else()
  564. set(rpaths "")
  565. endif()
  566. if(NOT IS_ABSOLUTE "${target}")
  567. message("warning: target '${target}' is not absolute...")
  568. endif()
  569. if(NOT EXISTS "${target}")
  570. message("warning: target '${target}' does not exist...")
  571. endif()
  572. set(gp_cmd_paths ${gp_cmd_paths}
  573. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  574. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  575. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  576. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  577. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  578. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  579. "/usr/local/bin"
  580. "/usr/bin"
  581. )
  582. # <setup-gp_tool-vars>
  583. #
  584. # Try to choose the right tool by default. Caller can set gp_tool prior to
  585. # calling this function to force using a different tool.
  586. #
  587. if(NOT gp_tool)
  588. set(gp_tool "ldd")
  589. if(APPLE)
  590. set(gp_tool "otool")
  591. endif()
  592. if(WIN32 AND NOT UNIX) # This is how to check for cygwin, har!
  593. find_program(gp_dumpbin "dumpbin" PATHS ${gp_cmd_paths})
  594. if(gp_dumpbin)
  595. set(gp_tool "dumpbin")
  596. else() # Try harder. Maybe we're on MinGW
  597. set(gp_tool "objdump")
  598. endif()
  599. endif()
  600. endif()
  601. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  602. if(NOT gp_cmd)
  603. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  604. return()
  605. endif()
  606. if(gp_tool STREQUAL "ldd")
  607. set(gp_cmd_args "")
  608. set(gp_regex "^[\t ]*[^\t ]+ => ([^\t\(]+) .*${eol_char}$")
  609. set(gp_regex_error "not found${eol_char}$")
  610. set(gp_regex_fallback "^[\t ]*([^\t ]+) => ([^\t ]+).*${eol_char}$")
  611. set(gp_regex_cmp_count 1)
  612. elseif(gp_tool STREQUAL "otool")
  613. set(gp_cmd_args "-L")
  614. set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
  615. set(gp_regex_error "")
  616. set(gp_regex_fallback "")
  617. set(gp_regex_cmp_count 3)
  618. elseif(gp_tool STREQUAL "dumpbin")
  619. set(gp_cmd_args "/dependents")
  620. set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
  621. set(gp_regex_error "")
  622. set(gp_regex_fallback "")
  623. set(gp_regex_cmp_count 1)
  624. elseif(gp_tool STREQUAL "objdump")
  625. set(gp_cmd_args "-p")
  626. set(gp_regex "^\t*DLL Name: (.*\\.[Dd][Ll][Ll])${eol_char}$")
  627. set(gp_regex_error "")
  628. set(gp_regex_fallback "")
  629. set(gp_regex_cmp_count 1)
  630. else()
  631. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  632. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  633. message(STATUS "Valid gp_tool values are dumpbin, ldd, objdump and otool.")
  634. return()
  635. endif()
  636. if(gp_tool STREQUAL "dumpbin")
  637. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  638. # PATH. It will already be in the PATH if being run from a Visual Studio
  639. # command prompt. Add it to the PATH here in case we are running from a
  640. # different command prompt.
  641. #
  642. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  643. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  644. # Use cmake paths as a user may have a PATH element ending with a backslash.
  645. # This will escape the list delimiter and create havoc!
  646. if(EXISTS "${gp_cmd_dlls_dir}")
  647. # only add to the path if it is not already in the path
  648. set(gp_found_cmd_dlls_dir 0)
  649. file(TO_CMAKE_PATH "$ENV{PATH}" env_path)
  650. foreach(gp_env_path_element ${env_path})
  651. if(gp_env_path_element STREQUAL gp_cmd_dlls_dir)
  652. set(gp_found_cmd_dlls_dir 1)
  653. endif()
  654. endforeach()
  655. if(NOT gp_found_cmd_dlls_dir)
  656. file(TO_NATIVE_PATH "${gp_cmd_dlls_dir}" gp_cmd_dlls_dir)
  657. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  658. endif()
  659. endif()
  660. endif()
  661. #
  662. # </setup-gp_tool-vars>
  663. if(gp_tool STREQUAL "ldd")
  664. set(old_ld_env "$ENV{LD_LIBRARY_PATH}")
  665. set(new_ld_env "${exepath}")
  666. foreach(dir ${dirs})
  667. set(new_ld_env "${new_ld_env}:${dir}")
  668. endforeach()
  669. set(ENV{LD_LIBRARY_PATH} "${new_ld_env}:$ENV{LD_LIBRARY_PATH}")
  670. endif()
  671. # Track new prerequisites at each new level of recursion. Start with an
  672. # empty list at each level:
  673. #
  674. set(unseen_prereqs)
  675. # Run gp_cmd on the target:
  676. #
  677. execute_process(
  678. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  679. OUTPUT_VARIABLE gp_cmd_ov
  680. )
  681. if(gp_tool STREQUAL "ldd")
  682. set(ENV{LD_LIBRARY_PATH} "${old_ld_env}")
  683. endif()
  684. if(verbose)
  685. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  686. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  687. message(STATUS "</RawOutput>")
  688. endif()
  689. get_filename_component(target_dir "${target}" PATH)
  690. # Convert to a list of lines:
  691. #
  692. string(REPLACE ";" "\\;" candidates "${gp_cmd_ov}")
  693. string(REPLACE "\n" "${eol_char};" candidates "${candidates}")
  694. # check for install id and remove it from list, since otool -L can include a
  695. # reference to itself
  696. set(gp_install_id)
  697. if(gp_tool STREQUAL "otool")
  698. execute_process(
  699. COMMAND otool -D ${target}
  700. OUTPUT_VARIABLE gp_install_id_ov
  701. )
  702. # second line is install name
  703. string(REGEX REPLACE ".*:\n" "" gp_install_id "${gp_install_id_ov}")
  704. if(gp_install_id)
  705. # trim
  706. string(REGEX MATCH "[^\n ].*[^\n ]" gp_install_id "${gp_install_id}")
  707. #message("INSTALL ID is \"${gp_install_id}\"")
  708. endif()
  709. endif()
  710. # Analyze each line for file names that match the regular expression:
  711. #
  712. foreach(candidate ${candidates})
  713. if("${candidate}" MATCHES "${gp_regex}")
  714. # Extract information from each candidate:
  715. if(gp_regex_error AND "${candidate}" MATCHES "${gp_regex_error}")
  716. string(REGEX REPLACE "${gp_regex_fallback}" "\\1" raw_item "${candidate}")
  717. else()
  718. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  719. endif()
  720. if(gp_regex_cmp_count GREATER 1)
  721. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  722. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  723. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  724. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  725. endif()
  726. if(gp_regex_cmp_count GREATER 2)
  727. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  728. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  729. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  730. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  731. endif()
  732. # Use the raw_item as the list entries returned by this function. Use the
  733. # gp_resolve_item function to resolve it to an actual full path file if
  734. # necessary.
  735. #
  736. set(item "${raw_item}")
  737. # Add each item unless it is excluded:
  738. #
  739. set(add_item 1)
  740. if(item STREQUAL gp_install_id)
  741. set(add_item 0)
  742. endif()
  743. if(add_item AND ${exclude_system})
  744. set(type "")
  745. gp_resolved_file_type("${target}" "${item}" "${exepath}" "${dirs}" type "${rpaths}")
  746. if(type STREQUAL "system")
  747. set(add_item 0)
  748. endif()
  749. endif()
  750. if(add_item)
  751. list(LENGTH ${prerequisites_var} list_length_before_append)
  752. gp_append_unique(${prerequisites_var} "${item}")
  753. list(LENGTH ${prerequisites_var} list_length_after_append)
  754. if(${recurse})
  755. # If item was really added, this is the first time we have seen it.
  756. # Add it to unseen_prereqs so that we can recursively add *its*
  757. # prerequisites...
  758. #
  759. # But first: resolve its name to an absolute full path name such
  760. # that the analysis tools can simply accept it as input.
  761. #
  762. if(NOT list_length_before_append EQUAL list_length_after_append)
  763. gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item "${rpaths}")
  764. set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
  765. endif()
  766. endif()
  767. endif()
  768. else()
  769. if(verbose)
  770. message(STATUS "ignoring non-matching line: '${candidate}'")
  771. endif()
  772. endif()
  773. endforeach()
  774. list(LENGTH ${prerequisites_var} prerequisites_var_length)
  775. if(prerequisites_var_length GREATER 0)
  776. list(SORT ${prerequisites_var})
  777. endif()
  778. if(${recurse})
  779. set(more_inputs ${unseen_prereqs})
  780. foreach(input ${more_inputs})
  781. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}" "${rpaths}")
  782. endforeach()
  783. endif()
  784. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  785. endfunction()
  786. function(list_prerequisites target)
  787. if(ARGC GREATER 1 AND NOT "${ARGV1}" STREQUAL "")
  788. set(all "${ARGV1}")
  789. else()
  790. set(all 1)
  791. endif()
  792. if(ARGC GREATER 2 AND NOT "${ARGV2}" STREQUAL "")
  793. set(exclude_system "${ARGV2}")
  794. else()
  795. set(exclude_system 0)
  796. endif()
  797. if(ARGC GREATER 3 AND NOT "${ARGV3}" STREQUAL "")
  798. set(verbose "${ARGV3}")
  799. else()
  800. set(verbose 0)
  801. endif()
  802. set(count 0)
  803. set(count_str "")
  804. set(print_count "${verbose}")
  805. set(print_prerequisite_type "${verbose}")
  806. set(print_target "${verbose}")
  807. set(type_str "")
  808. get_filename_component(exepath "${target}" PATH)
  809. set(prereqs "")
  810. get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
  811. if(print_target)
  812. message(STATUS "File '${target}' depends on:")
  813. endif()
  814. foreach(d ${prereqs})
  815. math(EXPR count "${count} + 1")
  816. if(print_count)
  817. set(count_str "${count}. ")
  818. endif()
  819. if(print_prerequisite_type)
  820. gp_file_type("${target}" "${d}" type)
  821. set(type_str " (${type})")
  822. endif()
  823. message(STATUS "${count_str}${d}${type_str}")
  824. endforeach()
  825. endfunction()
  826. function(list_prerequisites_by_glob glob_arg glob_exp)
  827. message(STATUS "=============================================================================")
  828. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  829. message(STATUS "")
  830. file(${glob_arg} file_list ${glob_exp})
  831. foreach(f ${file_list})
  832. is_file_executable("${f}" is_f_executable)
  833. if(is_f_executable)
  834. message(STATUS "=============================================================================")
  835. list_prerequisites("${f}" ${ARGN})
  836. message(STATUS "")
  837. endif()
  838. endforeach()
  839. endfunction()