GetPrerequisites.cmake 30 KB

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