GetPrerequisites.cmake 33 KB

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