1
0

GetPrerequisites.cmake 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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_error "not found${eol_char}$")
  461. set(gp_regex_fallback "^[\t ]*([^\t ]+) => ([^\t ]+).*${eol_char}$")
  462. set(gp_regex_cmp_count 1)
  463. set(gp_tool_known 1)
  464. endif("${gp_tool}" STREQUAL "ldd")
  465. if("${gp_tool}" STREQUAL "otool")
  466. set(gp_cmd_args "-L")
  467. set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
  468. set(gp_regex_error "")
  469. set(gp_regex_fallback "")
  470. set(gp_regex_cmp_count 3)
  471. set(gp_tool_known 1)
  472. endif("${gp_tool}" STREQUAL "otool")
  473. if("${gp_tool}" STREQUAL "dumpbin")
  474. set(gp_cmd_args "/dependents")
  475. set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
  476. set(gp_regex_error "")
  477. set(gp_regex_fallback "")
  478. set(gp_regex_cmp_count 1)
  479. set(gp_tool_known 1)
  480. set(ENV{VS_UNICODE_OUTPUT} "") # Block extra output from inside VS IDE.
  481. endif("${gp_tool}" STREQUAL "dumpbin")
  482. if(NOT gp_tool_known)
  483. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  484. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  485. message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
  486. return()
  487. endif(NOT gp_tool_known)
  488. set(gp_cmd_paths ${gp_cmd_paths}
  489. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  490. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  491. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  492. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  493. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  494. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  495. "/usr/local/bin"
  496. "/usr/bin"
  497. )
  498. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  499. if(NOT gp_cmd)
  500. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  501. return()
  502. endif(NOT gp_cmd)
  503. if("${gp_tool}" STREQUAL "dumpbin")
  504. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  505. # PATH. It will already be in the PATH if being run from a Visual Studio
  506. # command prompt. Add it to the PATH here in case we are running from a
  507. # different command prompt.
  508. #
  509. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  510. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  511. if(EXISTS "${gp_cmd_dlls_dir}")
  512. # only add to the path if it is not already in the path
  513. if(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
  514. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  515. endif(NOT "$ENV{PATH}" MATCHES "${gp_cmd_dlls_dir}")
  516. endif(EXISTS "${gp_cmd_dlls_dir}")
  517. endif("${gp_tool}" STREQUAL "dumpbin")
  518. #
  519. # </setup-gp_tool-vars>
  520. if("${gp_tool}" STREQUAL "ldd")
  521. set(old_ld_env "$ENV{LD_LIBRARY_PATH}")
  522. foreach(dir ${exepath} ${dirs})
  523. set(ENV{LD_LIBRARY_PATH} "${dir}:$ENV{LD_LIBRARY_PATH}")
  524. endforeach(dir)
  525. endif("${gp_tool}" STREQUAL "ldd")
  526. # Track new prerequisites at each new level of recursion. Start with an
  527. # empty list at each level:
  528. #
  529. set(unseen_prereqs)
  530. # Run gp_cmd on the target:
  531. #
  532. execute_process(
  533. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  534. OUTPUT_VARIABLE gp_cmd_ov
  535. )
  536. if("${gp_tool}" STREQUAL "ldd")
  537. set(ENV{LD_LIBRARY_PATH} "${old_ld_env}")
  538. endif("${gp_tool}" STREQUAL "ldd")
  539. if(verbose)
  540. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  541. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  542. message(STATUS "</RawOutput>")
  543. endif(verbose)
  544. get_filename_component(target_dir "${target}" PATH)
  545. # Convert to a list of lines:
  546. #
  547. string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
  548. string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
  549. # Analyze each line for file names that match the regular expression:
  550. #
  551. foreach(candidate ${candidates})
  552. if("${candidate}" MATCHES "${gp_regex}")
  553. # Extract information from each candidate:
  554. if(gp_regex_error AND "${candidate}" MATCHES "${gp_regex_error}")
  555. string(REGEX REPLACE "${gp_regex_fallback}" "\\1" raw_item "${candidate}")
  556. else(gp_regex_error AND "${candidate}" MATCHES "${gp_regex_error}")
  557. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  558. endif(gp_regex_error AND "${candidate}" MATCHES "${gp_regex_error}")
  559. if(gp_regex_cmp_count GREATER 1)
  560. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  561. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  562. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  563. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  564. endif(gp_regex_cmp_count GREATER 1)
  565. if(gp_regex_cmp_count GREATER 2)
  566. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  567. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  568. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  569. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  570. endif(gp_regex_cmp_count GREATER 2)
  571. # Use the raw_item as the list entries returned by this function. Use the
  572. # gp_resolve_item function to resolve it to an actual full path file if
  573. # necessary.
  574. #
  575. set(item "${raw_item}")
  576. # Add each item unless it is excluded:
  577. #
  578. set(add_item 1)
  579. if(${exclude_system})
  580. set(type "")
  581. gp_resolved_file_type("${target}" "${item}" "${exepath}" "${dirs}" type)
  582. if("${type}" STREQUAL "system")
  583. set(add_item 0)
  584. endif("${type}" STREQUAL "system")
  585. endif(${exclude_system})
  586. if(add_item)
  587. list(LENGTH ${prerequisites_var} list_length_before_append)
  588. gp_append_unique(${prerequisites_var} "${item}")
  589. list(LENGTH ${prerequisites_var} list_length_after_append)
  590. if(${recurse})
  591. # If item was really added, this is the first time we have seen it.
  592. # Add it to unseen_prereqs so that we can recursively add *its*
  593. # prerequisites...
  594. #
  595. # But first: resolve its name to an absolute full path name such
  596. # that the analysis tools can simply accept it as input.
  597. #
  598. if(NOT list_length_before_append EQUAL list_length_after_append)
  599. gp_resolve_item("${target}" "${item}" "${exepath}" "${dirs}" resolved_item)
  600. set(unseen_prereqs ${unseen_prereqs} "${resolved_item}")
  601. endif(NOT list_length_before_append EQUAL list_length_after_append)
  602. endif(${recurse})
  603. endif(add_item)
  604. else("${candidate}" MATCHES "${gp_regex}")
  605. if(verbose)
  606. message(STATUS "ignoring non-matching line: '${candidate}'")
  607. endif(verbose)
  608. endif("${candidate}" MATCHES "${gp_regex}")
  609. endforeach(candidate)
  610. list(LENGTH ${prerequisites_var} prerequisites_var_length)
  611. if(prerequisites_var_length GREATER 0)
  612. list(SORT ${prerequisites_var})
  613. endif(prerequisites_var_length GREATER 0)
  614. if(${recurse})
  615. set(more_inputs ${unseen_prereqs})
  616. foreach(input ${more_inputs})
  617. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse} "${exepath}" "${dirs}")
  618. endforeach(input)
  619. endif(${recurse})
  620. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  621. endfunction(get_prerequisites)
  622. # list_prerequisites target all exclude_system verbose
  623. #
  624. # ARGV0 (target) is the full path to an executable file
  625. #
  626. # optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
  627. # 1 for all prerequisites recursively
  628. #
  629. # optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
  630. # prerequisites , 1 to exclude them
  631. #
  632. # optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
  633. # names of prerequisites, 1 to print extra information
  634. #
  635. function(list_prerequisites target)
  636. if("${ARGV1}" STREQUAL "")
  637. set(all 1)
  638. else("${ARGV1}" STREQUAL "")
  639. set(all "${ARGV1}")
  640. endif("${ARGV1}" STREQUAL "")
  641. if("${ARGV2}" STREQUAL "")
  642. set(exclude_system 0)
  643. else("${ARGV2}" STREQUAL "")
  644. set(exclude_system "${ARGV2}")
  645. endif("${ARGV2}" STREQUAL "")
  646. if("${ARGV3}" STREQUAL "")
  647. set(verbose 0)
  648. else("${ARGV3}" STREQUAL "")
  649. set(verbose "${ARGV3}")
  650. endif("${ARGV3}" STREQUAL "")
  651. set(count 0)
  652. set(count_str "")
  653. set(print_count "${verbose}")
  654. set(print_prerequisite_type "${verbose}")
  655. set(print_target "${verbose}")
  656. set(type_str "")
  657. get_filename_component(exepath "${target}" PATH)
  658. set(prereqs "")
  659. get_prerequisites("${target}" prereqs ${exclude_system} ${all} "${exepath}" "")
  660. if(print_target)
  661. message(STATUS "File '${target}' depends on:")
  662. endif(print_target)
  663. foreach(d ${prereqs})
  664. math(EXPR count "${count} + 1")
  665. if(print_count)
  666. set(count_str "${count}. ")
  667. endif(print_count)
  668. if(print_prerequisite_type)
  669. gp_file_type("${target}" "${d}" type)
  670. set(type_str " (${type})")
  671. endif(print_prerequisite_type)
  672. message(STATUS "${count_str}${d}${type_str}")
  673. endforeach(d)
  674. endfunction(list_prerequisites)
  675. # list_prerequisites_by_glob glob_arg glob_exp
  676. #
  677. # glob_arg is GLOB or GLOB_RECURSE
  678. #
  679. # glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
  680. # of matching files. If a matching file is executable, its prerequisites are
  681. # listed.
  682. #
  683. # Any additional (optional) arguments provided are passed along as the
  684. # optional arguments to the list_prerequisites calls.
  685. #
  686. function(list_prerequisites_by_glob glob_arg glob_exp)
  687. message(STATUS "=============================================================================")
  688. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  689. message(STATUS "")
  690. file(${glob_arg} file_list ${glob_exp})
  691. foreach(f ${file_list})
  692. is_file_executable("${f}" is_f_executable)
  693. if(is_f_executable)
  694. message(STATUS "=============================================================================")
  695. list_prerequisites("${f}" ${ARGN})
  696. message(STATUS "")
  697. endif(is_f_executable)
  698. endforeach(f)
  699. endfunction(list_prerequisites_by_glob)