GetPrerequisites.cmake 29 KB

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