GetPrerequisites.cmake 27 KB

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