GetPrerequisites.cmake 26 KB

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