GetPrerequisites.cmake 29 KB

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