GetPrerequisites.cmake 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. RESULT_VARIABLE file_rv
  222. OUTPUT_VARIABLE file_ov
  223. ERROR_VARIABLE file_ev
  224. OUTPUT_STRIP_TRAILING_WHITESPACE
  225. )
  226. if(NOT file_rv STREQUAL "0")
  227. message(FATAL_ERROR "${file_cmd} failed: ${file_rv}\n${file_ev}")
  228. endif()
  229. # Replace the name of the file in the output with a placeholder token
  230. # (the string " _file_full_ ") so that just in case the path name of
  231. # the file contains the word "text" or "executable" we are not fooled
  232. # into thinking "the wrong thing" because the file name matches the
  233. # other 'file' command output we are looking for...
  234. #
  235. string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
  236. string(TOLOWER "${file_ov}" file_ov)
  237. #message(STATUS "file_ov='${file_ov}'")
  238. if("${file_ov}" MATCHES "executable")
  239. #message(STATUS "executable!")
  240. if("${file_ov}" MATCHES "text")
  241. #message(STATUS "but text, so *not* a binary executable!")
  242. else()
  243. set(${result_var} 1 PARENT_SCOPE)
  244. return()
  245. endif()
  246. endif()
  247. # Also detect position independent executables on Linux,
  248. # where "file" gives "shared object ... (uses shared libraries)"
  249. if("${file_ov}" MATCHES "shared object.*\(uses shared libs\)")
  250. set(${result_var} 1 PARENT_SCOPE)
  251. return()
  252. endif()
  253. # "file" version 5.22 does not print "(used shared libraries)"
  254. # but uses "interpreter"
  255. if("${file_ov}" MATCHES "shared object.*interpreter")
  256. set(${result_var} 1 PARENT_SCOPE)
  257. return()
  258. endif()
  259. else()
  260. message(STATUS "warning: No 'file' command, skipping execute_process...")
  261. endif()
  262. endif()
  263. endfunction()
  264. function(gp_item_default_embedded_path item default_embedded_path_var)
  265. # On Windows and Linux, "embed" prerequisites in the same directory
  266. # as the executable by default:
  267. #
  268. set(path "@executable_path")
  269. set(overridden 0)
  270. # On the Mac, relative to the executable depending on the type
  271. # of the thing we are embedding:
  272. #
  273. if(APPLE)
  274. #
  275. # The assumption here is that all executables in the bundle will be
  276. # in same-level-directories inside the bundle. The parent directory
  277. # of an executable inside the bundle should be MacOS or a sibling of
  278. # MacOS and all embedded paths returned from here will begin with
  279. # "@executable_path/../" and will work from all executables in all
  280. # such same-level-directories inside the bundle.
  281. #
  282. # By default, embed things right next to the main bundle executable:
  283. #
  284. set(path "@executable_path/../../Contents/MacOS")
  285. # Embed .dylibs right next to the main bundle executable:
  286. #
  287. if(item MATCHES "\\.dylib$")
  288. set(path "@executable_path/../MacOS")
  289. set(overridden 1)
  290. endif()
  291. # Embed frameworks in the embedded "Frameworks" directory (sibling of MacOS):
  292. #
  293. if(NOT overridden)
  294. if(item MATCHES "[^/]+\\.framework/")
  295. set(path "@executable_path/../Frameworks")
  296. set(overridden 1)
  297. endif()
  298. endif()
  299. endif()
  300. # Provide a hook so that projects can override the default embedded location
  301. # of any given library by whatever logic they choose:
  302. #
  303. if(COMMAND gp_item_default_embedded_path_override)
  304. gp_item_default_embedded_path_override("${item}" path)
  305. endif()
  306. set(${default_embedded_path_var} "${path}" PARENT_SCOPE)
  307. endfunction()
  308. function(gp_resolve_item context item exepath dirs resolved_item_var)
  309. set(resolved 0)
  310. set(resolved_item "${item}")
  311. if(ARGC GREATER 5)
  312. set(rpaths "${ARGV5}")
  313. else()
  314. set(rpaths "")
  315. endif()
  316. # Is it already resolved?
  317. #
  318. if(IS_ABSOLUTE "${resolved_item}" AND EXISTS "${resolved_item}")
  319. set(resolved 1)
  320. endif()
  321. if(NOT resolved)
  322. if(item MATCHES "^@executable_path")
  323. #
  324. # @executable_path references are assumed relative to exepath
  325. #
  326. string(REPLACE "@executable_path" "${exepath}" 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 "^@loader_path")
  339. #
  340. # @loader_path references are assumed relative to the
  341. # PATH of the given "context" (presumably another library)
  342. #
  343. get_filename_component(contextpath "${context}" PATH)
  344. string(REPLACE "@loader_path" "${contextpath}" ri "${item}")
  345. get_filename_component(ri "${ri}" ABSOLUTE)
  346. if(EXISTS "${ri}")
  347. #message(STATUS "info: embedded item exists (${ri})")
  348. set(resolved 1)
  349. set(resolved_item "${ri}")
  350. else()
  351. message(STATUS "warning: embedded item does not exist '${ri}'")
  352. endif()
  353. endif()
  354. endif()
  355. if(NOT resolved)
  356. if(item MATCHES "^@rpath")
  357. #
  358. # @rpath references are relative to the paths built into the binaries with -rpath
  359. # We handle this case like we do for other Unixes
  360. #
  361. string(REPLACE "@rpath/" "" norpath_item "${item}")
  362. set(ri "ri-NOTFOUND")
  363. find_file(ri "${norpath_item}" ${exepath} ${dirs} ${rpaths} NO_DEFAULT_PATH)
  364. if(ri)
  365. #message(STATUS "info: 'find_file' in exepath/dirs/rpaths (${ri})")
  366. set(resolved 1)
  367. set(resolved_item "${ri}")
  368. set(ri "ri-NOTFOUND")
  369. endif()
  370. endif()
  371. endif()
  372. if(NOT resolved)
  373. set(ri "ri-NOTFOUND")
  374. find_file(ri "${item}" ${exepath} ${dirs} NO_DEFAULT_PATH)
  375. find_file(ri "${item}" ${exepath} ${dirs} /usr/lib)
  376. if(ri)
  377. #message(STATUS "info: 'find_file' in exepath/dirs (${ri})")
  378. set(resolved 1)
  379. set(resolved_item "${ri}")
  380. set(ri "ri-NOTFOUND")
  381. endif()
  382. endif()
  383. if(NOT resolved)
  384. if(item MATCHES "[^/]+\\.framework/")
  385. set(fw "fw-NOTFOUND")
  386. find_file(fw "${item}"
  387. "~/Library/Frameworks"
  388. "/Library/Frameworks"
  389. "/System/Library/Frameworks"
  390. )
  391. if(fw)
  392. #message(STATUS "info: 'find_file' found framework (${fw})")
  393. set(resolved 1)
  394. set(resolved_item "${fw}")
  395. set(fw "fw-NOTFOUND")
  396. endif()
  397. endif()
  398. endif()
  399. # Using find_program on Windows will find dll files that are in the PATH.
  400. # (Converting simple file names into full path names if found.)
  401. #
  402. if(WIN32 AND NOT UNIX)
  403. if(NOT resolved)
  404. set(ri "ri-NOTFOUND")
  405. find_program(ri "${item}" PATHS "${exepath};${dirs}" NO_DEFAULT_PATH)
  406. find_program(ri "${item}" PATHS "${exepath};${dirs}")
  407. if(ri)
  408. #message(STATUS "info: 'find_program' in exepath/dirs (${ri})")
  409. set(resolved 1)
  410. set(resolved_item "${ri}")
  411. set(ri "ri-NOTFOUND")
  412. endif()
  413. endif()
  414. endif()
  415. # Provide a hook so that projects can override item resolution
  416. # by whatever logic they choose:
  417. #
  418. if(COMMAND gp_resolve_item_override)
  419. gp_resolve_item_override("${context}" "${item}" "${exepath}" "${dirs}" resolved_item resolved)
  420. endif()
  421. if(NOT resolved)
  422. message(STATUS "
  423. warning: cannot resolve item '${item}'
  424. possible problems:
  425. need more directories?
  426. need to use InstallRequiredSystemLibraries?
  427. run in install tree instead of build tree?
  428. ")
  429. # message(STATUS "
  430. #******************************************************************************
  431. #warning: cannot resolve item '${item}'
  432. #
  433. # possible problems:
  434. # need more directories?
  435. # need to use InstallRequiredSystemLibraries?
  436. # run in install tree instead of build tree?
  437. #
  438. # context='${context}'
  439. # item='${item}'
  440. # exepath='${exepath}'
  441. # dirs='${dirs}'
  442. # resolved_item_var='${resolved_item_var}'
  443. #******************************************************************************
  444. #")
  445. endif()
  446. set(${resolved_item_var} "${resolved_item}" PARENT_SCOPE)
  447. endfunction()
  448. function(gp_resolved_file_type original_file file exepath dirs type_var)
  449. if(ARGC GREATER 5)
  450. set(rpaths "${ARGV5}")
  451. else()
  452. set(rpaths "")
  453. endif()
  454. #message(STATUS "**")
  455. if(NOT IS_ABSOLUTE "${original_file}")
  456. message(STATUS "warning: gp_resolved_file_type expects absolute full path for first arg original_file")
  457. endif()
  458. get_filename_component(original_file "${original_file}" ABSOLUTE) # canonicalize path
  459. set(is_embedded 0)
  460. set(is_local 0)
  461. set(is_system 0)
  462. set(resolved_file "${file}")
  463. if("${file}" MATCHES "^@(executable|loader)_path")
  464. set(is_embedded 1)
  465. endif()
  466. if(NOT is_embedded)
  467. if(NOT IS_ABSOLUTE "${file}")
  468. gp_resolve_item("${original_file}" "${file}" "${exepath}" "${dirs}" resolved_file "${rpaths}")
  469. endif()
  470. get_filename_component(resolved_file "${resolved_file}" ABSOLUTE) # canonicalize path
  471. string(TOLOWER "${original_file}" original_lower)
  472. string(TOLOWER "${resolved_file}" lower)
  473. if(UNIX)
  474. if(resolved_file MATCHES "^(/lib/|/lib32/|/lib64/|/usr/lib/|/usr/lib32/|/usr/lib64/|/usr/X11R6/|/usr/bin/)")
  475. set(is_system 1)
  476. endif()
  477. endif()
  478. if(APPLE)
  479. if(resolved_file MATCHES "^(/System/Library/|/usr/lib/)")
  480. set(is_system 1)
  481. endif()
  482. endif()
  483. if(WIN32)
  484. string(TOLOWER "$ENV{SystemRoot}" sysroot)
  485. file(TO_CMAKE_PATH "${sysroot}" sysroot)
  486. string(TOLOWER "$ENV{windir}" windir)
  487. file(TO_CMAKE_PATH "${windir}" windir)
  488. if(lower MATCHES "^(api-ms-win-|${sysroot}/sys(tem|wow)|${windir}/sys(tem|wow)|(.*/)*msvc[^/]+dll)")
  489. set(is_system 1)
  490. endif()
  491. if(UNIX)
  492. # if cygwin, we can get the properly formed windows paths from cygpath
  493. find_program(CYGPATH_EXECUTABLE cygpath)
  494. if(CYGPATH_EXECUTABLE)
  495. execute_process(COMMAND ${CYGPATH_EXECUTABLE} -W
  496. RESULT_VARIABLE env_rv
  497. OUTPUT_VARIABLE env_windir
  498. ERROR_VARIABLE env_ev
  499. OUTPUT_STRIP_TRAILING_WHITESPACE)
  500. if(NOT env_rv STREQUAL "0")
  501. message(FATAL_ERROR "${CYGPATH_EXECUTABLE} -W failed: ${env_rv}\n${env_ev}")
  502. endif()
  503. execute_process(COMMAND ${CYGPATH_EXECUTABLE} -S
  504. RESULT_VARIABLE env_rv
  505. OUTPUT_VARIABLE env_sysdir
  506. ERROR_VARIABLE env_ev
  507. OUTPUT_STRIP_TRAILING_WHITESPACE)
  508. if(NOT env_rv STREQUAL "0")
  509. message(FATAL_ERROR "${CYGPATH_EXECUTABLE} -S failed: ${env_rv}\n${env_ev}")
  510. endif()
  511. string(TOLOWER "${env_windir}" windir)
  512. string(TOLOWER "${env_sysdir}" sysroot)
  513. if(lower MATCHES "^(api-ms-win-|${sysroot}/sys(tem|wow)|${windir}/sys(tem|wow)|(.*/)*msvc[^/]+dll)")
  514. set(is_system 1)
  515. endif()
  516. endif()
  517. endif()
  518. endif()
  519. if(NOT is_system)
  520. get_filename_component(original_path "${original_lower}" PATH)
  521. get_filename_component(path "${lower}" PATH)
  522. if(original_path STREQUAL path)
  523. set(is_local 1)
  524. else()
  525. string(LENGTH "${original_path}/" original_length)
  526. string(LENGTH "${lower}" path_length)
  527. if(${path_length} GREATER ${original_length})
  528. string(SUBSTRING "${lower}" 0 ${original_length} path)
  529. if("${original_path}/" STREQUAL path)
  530. set(is_embedded 1)
  531. endif()
  532. endif()
  533. endif()
  534. endif()
  535. endif()
  536. # Return type string based on computed booleans:
  537. #
  538. set(type "other")
  539. if(is_system)
  540. set(type "system")
  541. elseif(is_embedded)
  542. set(type "embedded")
  543. elseif(is_local)
  544. set(type "local")
  545. endif()
  546. #message(STATUS "gp_resolved_file_type: '${file}' '${resolved_file}'")
  547. #message(STATUS " type: '${type}'")
  548. if(NOT is_embedded)
  549. if(NOT IS_ABSOLUTE "${resolved_file}")
  550. if(lower MATCHES "^msvc[^/]+dll" AND is_system)
  551. message(STATUS "info: non-absolute msvc file '${file}' returning type '${type}'")
  552. else()
  553. message(STATUS "warning: gp_resolved_file_type non-absolute file '${file}' returning type '${type}' -- possibly incorrect")
  554. endif()
  555. endif()
  556. endif()
  557. # Provide a hook so that projects can override the decision on whether a
  558. # library belongs to the system or not by whatever logic they choose:
  559. #
  560. if(COMMAND gp_resolved_file_type_override)
  561. gp_resolved_file_type_override("${resolved_file}" type)
  562. endif()
  563. set(${type_var} "${type}" PARENT_SCOPE)
  564. #message(STATUS "**")
  565. endfunction()
  566. function(gp_file_type original_file file type_var)
  567. if(NOT IS_ABSOLUTE "${original_file}")
  568. message(STATUS "warning: gp_file_type expects absolute full path for first arg original_file")
  569. endif()
  570. get_filename_component(exepath "${original_file}" PATH)
  571. set(type "")
  572. gp_resolved_file_type("${original_file}" "${file}" "${exepath}" "" type)
  573. set(${type_var} "${type}" PARENT_SCOPE)
  574. endfunction()
  575. function(get_prerequisites target prerequisites_var exclude_system recurse exepath dirs)
  576. set(verbose 0)
  577. set(eol_char "E")
  578. if(ARGC GREATER 6)
  579. set(rpaths "${ARGV6}")
  580. else()
  581. set(rpaths "")
  582. endif()
  583. if(NOT IS_ABSOLUTE "${target}")
  584. message("warning: target '${target}' is not absolute...")
  585. endif()
  586. if(NOT EXISTS "${target}")
  587. message("warning: target '${target}' does not exist...")
  588. endif()
  589. set(gp_cmd_paths ${gp_cmd_paths}
  590. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\14.0;InstallDir]/../../VC/bin"
  591. "$ENV{VS140COMNTOOLS}/../../VC/bin"
  592. "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin"
  593. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\12.0;InstallDir]/../../VC/bin"
  594. "$ENV{VS120COMNTOOLS}/../../VC/bin"
  595. "C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin"
  596. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\11.0;InstallDir]/../../VC/bin"
  597. "$ENV{VS110COMNTOOLS}/../../VC/bin"
  598. "C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin"
  599. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\10.0;InstallDir]/../../VC/bin"
  600. "$ENV{VS100COMNTOOLS}/../../VC/bin"
  601. "C:/Program Files (x86)/Microsoft Visual Studio 10.0/VC/bin"
  602. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0;InstallDir]/../../VC/bin"
  603. "$ENV{VS90COMNTOOLS}/../../VC/bin"
  604. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  605. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  606. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0;InstallDir]/../../VC/bin"
  607. "$ENV{VS80COMNTOOLS}/../../VC/bin"
  608. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  609. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  610. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]/../../VC7/bin"
  611. "$ENV{VS71COMNTOOLS}/../../VC7/bin"
  612. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  613. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  614. "/usr/local/bin"
  615. "/usr/bin"
  616. )
  617. # <setup-gp_tool-vars>
  618. #
  619. # Try to choose the right tool by default. Caller can set gp_tool prior to
  620. # calling this function to force using a different tool.
  621. #
  622. if(NOT gp_tool)
  623. set(gp_tool "ldd")
  624. if(APPLE)
  625. set(gp_tool "otool")
  626. endif()
  627. if(WIN32 AND NOT UNIX) # This is how to check for cygwin, har!
  628. find_program(gp_dumpbin "dumpbin" PATHS ${gp_cmd_paths})
  629. if(gp_dumpbin)
  630. set(gp_tool "dumpbin")
  631. else() # Try harder. Maybe we're on MinGW
  632. set(gp_tool "objdump")
  633. endif()
  634. endif()
  635. endif()
  636. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  637. if(NOT gp_cmd)
  638. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  639. return()
  640. endif()
  641. set(gp_cmd_maybe_filter) # optional command to pre-filter gp_tool results
  642. if(gp_tool STREQUAL "ldd")
  643. set(gp_cmd_args "")
  644. set(gp_regex "^[\t ]*[^\t ]+ => ([^\t\(]+) .*${eol_char}$")
  645. set(gp_regex_error "not found${eol_char}$")
  646. set(gp_regex_fallback "^[\t ]*([^\t ]+) => ([^\t ]+).*${eol_char}$")
  647. set(gp_regex_cmp_count 1)
  648. elseif(gp_tool STREQUAL "otool")
  649. set(gp_cmd_args "-L")
  650. set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
  651. set(gp_regex_error "")
  652. set(gp_regex_fallback "")
  653. set(gp_regex_cmp_count 3)
  654. elseif(gp_tool STREQUAL "dumpbin")
  655. set(gp_cmd_args "/dependents")
  656. set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
  657. set(gp_regex_error "")
  658. set(gp_regex_fallback "")
  659. set(gp_regex_cmp_count 1)
  660. elseif(gp_tool STREQUAL "objdump")
  661. set(gp_cmd_args "-p")
  662. set(gp_regex "^\t*DLL Name: (.*\\.[Dd][Ll][Ll])${eol_char}$")
  663. set(gp_regex_error "")
  664. set(gp_regex_fallback "")
  665. set(gp_regex_cmp_count 1)
  666. # objdump generates copious output so we create a grep filter to pre-filter results
  667. if(WIN32)
  668. find_program(gp_grep_cmd findstr)
  669. else()
  670. find_program(gp_grep_cmd grep)
  671. endif()
  672. if(gp_grep_cmd)
  673. set(gp_cmd_maybe_filter COMMAND ${gp_grep_cmd} "^[[:blank:]]*DLL Name: ")
  674. endif()
  675. else()
  676. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  677. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  678. message(STATUS "Valid gp_tool values are dumpbin, ldd, objdump and otool.")
  679. return()
  680. endif()
  681. if(gp_tool STREQUAL "dumpbin")
  682. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  683. # PATH. It will already be in the PATH if being run from a Visual Studio
  684. # command prompt. Add it to the PATH here in case we are running from a
  685. # different command prompt.
  686. #
  687. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  688. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  689. # Use cmake paths as a user may have a PATH element ending with a backslash.
  690. # This will escape the list delimiter and create havoc!
  691. if(EXISTS "${gp_cmd_dlls_dir}")
  692. # only add to the path if it is not already in the path
  693. set(gp_found_cmd_dlls_dir 0)
  694. file(TO_CMAKE_PATH "$ENV{PATH}" env_path)
  695. foreach(gp_env_path_element ${env_path})
  696. if(gp_env_path_element STREQUAL gp_cmd_dlls_dir)
  697. set(gp_found_cmd_dlls_dir 1)
  698. endif()
  699. endforeach()
  700. if(NOT gp_found_cmd_dlls_dir)
  701. file(TO_NATIVE_PATH "${gp_cmd_dlls_dir}" gp_cmd_dlls_dir)
  702. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  703. endif()
  704. endif()
  705. endif()
  706. #
  707. # </setup-gp_tool-vars>
  708. if(gp_tool STREQUAL "ldd")
  709. set(old_ld_env "$ENV{LD_LIBRARY_PATH}")
  710. set(new_ld_env "${exepath}")
  711. foreach(dir ${dirs})
  712. set(new_ld_env "${new_ld_env}:${dir}")
  713. endforeach()
  714. set(ENV{LD_LIBRARY_PATH} "${new_ld_env}:$ENV{LD_LIBRARY_PATH}")
  715. endif()
  716. # Track new prerequisites at each new level of recursion. Start with an
  717. # empty list at each level:
  718. #
  719. set(unseen_prereqs)
  720. # Run gp_cmd on the target:
  721. #
  722. execute_process(
  723. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  724. ${gp_cmd_maybe_filter}
  725. RESULT_VARIABLE gp_rv
  726. OUTPUT_VARIABLE gp_cmd_ov
  727. ERROR_VARIABLE gp_ev
  728. )
  729. if(NOT gp_rv STREQUAL "0")
  730. if(gp_tool STREQUAL "dumpbin")
  731. # dumpbin error messages seem to go to stdout
  732. message(FATAL_ERROR "${gp_cmd} failed: ${gp_rv}\n${gp_ev}\n${gp_cmd_ov}")
  733. else()
  734. message(FATAL_ERROR "${gp_cmd} failed: ${gp_rv}\n${gp_ev}")
  735. endif()
  736. endif()
  737. if(gp_tool STREQUAL "ldd")
  738. set(ENV{LD_LIBRARY_PATH} "${old_ld_env}")
  739. endif()
  740. if(verbose)
  741. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  742. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  743. message(STATUS "</RawOutput>")
  744. endif()
  745. get_filename_component(target_dir "${target}" PATH)
  746. # Convert to a list of lines:
  747. #
  748. string(REPLACE ";" "\\;" candidates "${gp_cmd_ov}")
  749. string(REPLACE "\n" "${eol_char};" candidates "${candidates}")
  750. # check for install id and remove it from list, since otool -L can include a
  751. # reference to itself
  752. set(gp_install_id)
  753. if(gp_tool STREQUAL "otool")
  754. execute_process(
  755. COMMAND otool -D ${target}
  756. RESULT_VARIABLE otool_rv
  757. OUTPUT_VARIABLE gp_install_id_ov
  758. ERROR_VARIABLE otool_ev
  759. )
  760. if(NOT otool_rv STREQUAL "0")
  761. message(FATAL_ERROR "otool -D failed: ${otool_rv}\n${otool_ev}")
  762. endif()
  763. # second line is install name
  764. string(REGEX REPLACE ".*:\n" "" gp_install_id "${gp_install_id_ov}")
  765. if(gp_install_id)
  766. # trim
  767. string(REGEX MATCH "[^\n ].*[^\n ]" gp_install_id "${gp_install_id}")
  768. #message("INSTALL ID is \"${gp_install_id}\"")
  769. endif()
  770. endif()
  771. # Analyze each line for file names that match the regular expression:
  772. #
  773. foreach(candidate ${candidates})
  774. if("${candidate}" MATCHES "${gp_regex}")
  775. # Extract information from each candidate:
  776. if(gp_regex_error AND "${candidate}" MATCHES "${gp_regex_error}")
  777. string(REGEX REPLACE "${gp_regex_fallback}" "\\1" raw_item "${candidate}")
  778. else()
  779. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  780. endif()
  781. if(gp_regex_cmp_count GREATER 1)
  782. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  783. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  784. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  785. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  786. endif()
  787. if(gp_regex_cmp_count GREATER 2)
  788. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  789. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  790. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  791. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  792. endif()
  793. # Use the raw_item as the list entries returned by this function. Use the
  794. # gp_resolve_item function to resolve it to an actual full path file if
  795. # necessary.
  796. #
  797. set(item "${raw_item}")
  798. # Add each item unless it is excluded:
  799. #
  800. set(add_item 1)
  801. if(item STREQUAL gp_install_id)
  802. set(add_item 0)
  803. endif()
  804. if(add_item AND ${exclude_system})
  805. set(type "")
  806. gp_resolved_file_type("${target}" "${item}" "${exepath}" "${dirs}" type "${rpaths}")
  807. if(type STREQUAL "system")
  808. set(add_item 0)
  809. endif()
  810. endif()
  811. if(add_item)
  812. list(LENGTH ${prerequisites_var} list_length_before_append)
  813. gp_append_unique(${prerequisites_var} "${item}")
  814. list(LENGTH ${prerequisites_var} list_length_after_append)
  815. if(${recurse})
  816. # If item was really added, this is the first time we have seen it.
  817. # Add it to unseen_prereqs so that we can recursively add *its*
  818. # prerequisites...
  819. #
  820. # But first: resolve its name to an absolute full path name such
  821. # that the analysis tools can simply accept it as input.
  822. #
  823. if(NOT list_length_before_append EQUAL list_length_after_append)
  824. gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item "${rpaths}")
  825. set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
  826. endif()
  827. endif()
  828. endif()
  829. else()
  830. if(verbose)
  831. message(STATUS "ignoring non-matching line: '${candidate}'")
  832. endif()
  833. endif()
  834. endforeach()
  835. list(LENGTH ${prerequisites_var} prerequisites_var_length)
  836. if(prerequisites_var_length GREATER 0)
  837. list(SORT ${prerequisites_var})
  838. endif()
  839. if(${recurse})
  840. set(more_inputs ${unseen_prereqs})
  841. foreach(input ${more_inputs})
  842. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}" "${rpaths}")
  843. endforeach()
  844. endif()
  845. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  846. endfunction()
  847. function(list_prerequisites target)
  848. if(ARGC GREATER 1 AND NOT "${ARGV1}" STREQUAL "")
  849. set(all "${ARGV1}")
  850. else()
  851. set(all 1)
  852. endif()
  853. if(ARGC GREATER 2 AND NOT "${ARGV2}" STREQUAL "")
  854. set(exclude_system "${ARGV2}")
  855. else()
  856. set(exclude_system 0)
  857. endif()
  858. if(ARGC GREATER 3 AND NOT "${ARGV3}" STREQUAL "")
  859. set(verbose "${ARGV3}")
  860. else()
  861. set(verbose 0)
  862. endif()
  863. set(count 0)
  864. set(count_str "")
  865. set(print_count "${verbose}")
  866. set(print_prerequisite_type "${verbose}")
  867. set(print_target "${verbose}")
  868. set(type_str "")
  869. get_filename_component(exepath "${target}" PATH)
  870. set(prereqs "")
  871. get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
  872. if(print_target)
  873. message(STATUS "File '${target}' depends on:")
  874. endif()
  875. foreach(d ${prereqs})
  876. math(EXPR count "${count} + 1")
  877. if(print_count)
  878. set(count_str "${count}. ")
  879. endif()
  880. if(print_prerequisite_type)
  881. gp_file_type("${target}" "${d}" type)
  882. set(type_str " (${type})")
  883. endif()
  884. message(STATUS "${count_str}${d}${type_str}")
  885. endforeach()
  886. endfunction()
  887. function(list_prerequisites_by_glob glob_arg glob_exp)
  888. message(STATUS "=============================================================================")
  889. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  890. message(STATUS "")
  891. file(${glob_arg} file_list ${glob_exp})
  892. foreach(f ${file_list})
  893. is_file_executable("${f}" is_f_executable)
  894. if(is_f_executable)
  895. message(STATUS "=============================================================================")
  896. list_prerequisites("${f}" ${ARGN})
  897. message(STATUS "")
  898. endif()
  899. endforeach()
  900. endfunction()