GetPrerequisites.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. # gp_file_type
  14. # is_file_executable
  15. # get_prerequisites
  16. # list_prerequisites
  17. #
  18. # Requires CMake 2.5 or greater because it uses function, break, return and
  19. # PARENT_SCOPE.
  20. #
  21. cmake_minimum_required(VERSION 2.5 FATAL_ERROR)
  22. # gp_append_unique list_var value
  23. #
  24. # Append value to the list variable ${list_var} only if the value is not
  25. # already in the list.
  26. #
  27. function(gp_append_unique list_var value)
  28. set(contains 0)
  29. foreach(item ${${list_var}})
  30. if("${item}" STREQUAL "${value}")
  31. set(contains 1)
  32. break()
  33. endif("${item}" STREQUAL "${value}")
  34. endforeach(item)
  35. if(NOT contains)
  36. set(${list_var} ${${list_var}} "${value}" PARENT_SCOPE)
  37. endif(NOT contains)
  38. endfunction(gp_append_unique)
  39. # gp_file_type original_file file type_var
  40. #
  41. # Return the type of ${file} with respect to ${original_file}. String
  42. # describing type of prerequisite is returned in variable named ${type_var}.
  43. #
  44. # Possible types are:
  45. # system
  46. # local
  47. # embedded
  48. # other
  49. #
  50. function(gp_file_type original_file file type_var)
  51. set(is_embedded 0)
  52. set(is_local 0)
  53. set(is_system 0)
  54. string(TOLOWER "${original_file}" original_lower)
  55. string(TOLOWER "${file}" lower)
  56. if("${file}" MATCHES "^@(executable|loader)_path")
  57. set(is_embedded 1)
  58. endif("${file}" MATCHES "^@(executable|loader)_path")
  59. if(NOT is_embedded)
  60. if("${file}" MATCHES "^(/System/Library/|/usr/lib/)")
  61. set(is_system 1)
  62. endif("${file}" MATCHES "^(/System/Library/|/usr/lib/)")
  63. if(WIN32)
  64. string(TOLOWER "$ENV{SystemRoot}" sysroot)
  65. string(REGEX REPLACE "\\\\" "/" sysroot "${sysroot}")
  66. string(TOLOWER "$ENV{windir}" windir)
  67. string(REGEX REPLACE "\\\\" "/" windir "${windir}")
  68. if("${lower}" MATCHES "^(${sysroot}/system|${windir}/system|msvc[^/]+dll)")
  69. set(is_system 1)
  70. endif("${lower}" MATCHES "^(${sysroot}/system|${windir}/system|msvc[^/]+dll)")
  71. endif(WIN32)
  72. if(NOT is_system)
  73. get_filename_component(original_path "${original_lower}" PATH)
  74. get_filename_component(path "${lower}" PATH)
  75. if("${original_path}" STREQUAL "${path}")
  76. set(is_local 1)
  77. endif("${original_path}" STREQUAL "${path}")
  78. endif(NOT is_system)
  79. endif(NOT is_embedded)
  80. # Return type string based on computed booleans:
  81. #
  82. set(type "other")
  83. if(is_system)
  84. set(type "system")
  85. else(is_system)
  86. if(is_embedded)
  87. set(type "embedded")
  88. else(is_embedded)
  89. if(is_local)
  90. set(type "local")
  91. endif(is_local)
  92. endif(is_embedded)
  93. endif(is_system)
  94. set(${type_var} "${type}" PARENT_SCOPE)
  95. endfunction(gp_file_type)
  96. # is_file_executable file result_var
  97. #
  98. # Return 1 in ${result_var} if ${file} is a binary executable.
  99. #
  100. # Return 0 in ${result_var} otherwise.
  101. #
  102. function(is_file_executable file result_var)
  103. #
  104. # A file is not executable until proven otherwise:
  105. #
  106. set(${result_var} 0 PARENT_SCOPE)
  107. get_filename_component(file_full "${file}" ABSOLUTE)
  108. string(TOLOWER "${file_full}" file_full_lower)
  109. # If file name ends in .exe or .dll on Windows, *assume* executable:
  110. #
  111. if(WIN32)
  112. if("${file_full_lower}" MATCHES "\\.(exe|dll)$")
  113. set(${result_var} 1 PARENT_SCOPE)
  114. return()
  115. endif("${file_full_lower}" MATCHES "\\.(exe|dll)$")
  116. # A clause could be added here that uses output or return value of dumpbin
  117. # to determine ${result_var}. In 95%+ practical cases, the exe|dll name
  118. # match will be sufficient...
  119. #
  120. endif(WIN32)
  121. # Use the information returned from the Unix shell command "file" to
  122. # determine if ${file_full} should be considered an executable file...
  123. #
  124. # If the file command's output contains "executable" and does *not* contain
  125. # "text" then it is likely an executable suitable for prerequisite analysis
  126. # via the get_prerequisites macro.
  127. #
  128. if(UNIX)
  129. if(NOT file_cmd)
  130. find_program(file_cmd "file")
  131. endif(NOT file_cmd)
  132. if(file_cmd)
  133. execute_process(COMMAND "${file_cmd}" "${file_full}"
  134. OUTPUT_VARIABLE file_ov
  135. OUTPUT_STRIP_TRAILING_WHITESPACE
  136. )
  137. # Replace the name of the file in the output with a placeholder token
  138. # (the string " _file_full_ ") so that just in case the path name of
  139. # the file contains the word "text" or "executable" we are not fooled
  140. # into thinking "the wrong thing" because the file name matches the
  141. # other 'file' command output we are looking for...
  142. #
  143. string(REPLACE "${file_full}" " _file_full_ " file_ov "${file_ov}")
  144. string(TOLOWER "${file_ov}" file_ov)
  145. #message(STATUS "file_ov='${file_ov}'")
  146. if("${file_ov}" MATCHES "executable")
  147. #message(STATUS "executable!")
  148. if("${file_ov}" MATCHES "text")
  149. #message(STATUS "but text, so *not* a binary executable!")
  150. else("${file_ov}" MATCHES "text")
  151. set(${result_var} 1 PARENT_SCOPE)
  152. return()
  153. endif("${file_ov}" MATCHES "text")
  154. endif("${file_ov}" MATCHES "executable")
  155. else(file_cmd)
  156. message(STATUS "warning: No 'file' command, skipping execute_process...")
  157. endif(file_cmd)
  158. endif(UNIX)
  159. endfunction(is_file_executable)
  160. # get_prerequisites target prerequisites_var exclude_system recurse
  161. #
  162. # Get the list of shared library files required by ${target}. The list in
  163. # the variable named ${prerequisites_var} should be empty on first entry to
  164. # this function. On exit, ${prerequisites_var} will contain the list of
  165. # required shared library files.
  166. #
  167. # target is the full path to an executable file
  168. #
  169. # prerequisites_var is the name of a CMake variable to contain the results
  170. #
  171. # exclude_system is 0 or 1: 0 to include "system" prerequisites , 1 to
  172. # exclude them
  173. #
  174. # recurse is 0 or 1: 0 for direct prerequisites only, 1 for all prerequisites
  175. # recursively
  176. #
  177. # optional ARGV4 (verbose) is 0 or 1: 0 to skip informational message output,
  178. # 1 to print it
  179. #
  180. function(get_prerequisites target prerequisites_var exclude_system recurse)
  181. # set(verbose 0)
  182. # if(NOT "${ARGV4}" STREQUAL "")
  183. # message(STATUS "ARGV4='${ARGV4}'")
  184. # set(verbose "${ARGV4}")
  185. # endif(NOT "${ARGV4}" STREQUAL "")
  186. # message(STATUS "verbose='${verbose}'")
  187. set(verbose 0)
  188. set(eol_char "E")
  189. # <setup-gp_tool-vars>
  190. #
  191. # Try to choose the right tool by default. Caller can set gp_tool prior to
  192. # calling this function to force using a different tool.
  193. #
  194. if("${gp_tool}" STREQUAL "")
  195. set(gp_tool "ldd")
  196. if(APPLE)
  197. set(gp_tool "otool")
  198. endif(APPLE)
  199. if(WIN32)
  200. set(gp_tool "dumpbin")
  201. endif(WIN32)
  202. endif("${gp_tool}" STREQUAL "")
  203. set(gp_tool_known 0)
  204. if("${gp_tool}" STREQUAL "ldd")
  205. set(gp_cmd_args "")
  206. set(gp_regex "^\t([\t ]+)[\t ].*${eol_char}$")
  207. set(gp_regex_cmp_count 1)
  208. set(gp_tool_known 1)
  209. endif("${gp_tool}" STREQUAL "ldd")
  210. if("${gp_tool}" STREQUAL "otool")
  211. set(gp_cmd_args "-L")
  212. set(gp_regex "^\t([^\t]+) \\(compatibility version ([0-9]+.[0-9]+.[0-9]+), current version ([0-9]+.[0-9]+.[0-9]+)\\)${eol_char}$")
  213. set(gp_regex_cmp_count 3)
  214. set(gp_tool_known 1)
  215. endif("${gp_tool}" STREQUAL "otool")
  216. if("${gp_tool}" STREQUAL "dumpbin")
  217. set(gp_cmd_args "/dependents")
  218. set(gp_regex "^ ([^ ].*[Dd][Ll][Ll])${eol_char}$")
  219. set(gp_regex_cmp_count 1)
  220. set(gp_tool_known 1)
  221. set(ENV{VS_UNICODE_OUTPUT} "") # Block extra output from inside VS IDE.
  222. endif("${gp_tool}" STREQUAL "dumpbin")
  223. if(NOT gp_tool_known)
  224. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  225. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  226. message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
  227. return()
  228. endif(NOT gp_tool_known)
  229. set(gp_cmd_paths ${gp_cmd_paths}
  230. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  231. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  232. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  233. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  234. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  235. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  236. "/usr/local/bin"
  237. "/usr/bin"
  238. )
  239. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  240. if(NOT gp_cmd)
  241. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  242. return()
  243. endif(NOT gp_cmd)
  244. if("${gp_tool}" STREQUAL "dumpbin")
  245. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  246. # PATH. It will already be in the PATH if being run from a Visual Studio
  247. # command prompt. Add it to the PATH here in case we are running from a
  248. # different command prompt.
  249. #
  250. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  251. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  252. if(EXISTS "${gp_cmd_dlls_dir}")
  253. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  254. endif(EXISTS "${gp_cmd_dlls_dir}")
  255. endif("${gp_tool}" STREQUAL "dumpbin")
  256. #
  257. # </setup-gp_tool-vars>
  258. # Track new prerequisites at each new level of recursion. Start with an
  259. # empty list at each level:
  260. #
  261. set(unseen_prereqs)
  262. # Run gp_cmd on the target:
  263. #
  264. execute_process(
  265. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  266. OUTPUT_VARIABLE gp_cmd_ov
  267. )
  268. if(verbose)
  269. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  270. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  271. message(STATUS "</RawOutput>")
  272. endif(verbose)
  273. get_filename_component(target_dir "${target}" PATH)
  274. # Convert to a list of lines:
  275. #
  276. string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
  277. string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
  278. # Analyze each line for file names that match the regular expression:
  279. #
  280. foreach(candidate ${candidates})
  281. if("${candidate}" MATCHES "${gp_regex}")
  282. # Extract information from each candidate:
  283. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  284. if(gp_regex_cmp_count GREATER 1)
  285. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  286. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  287. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  288. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  289. endif(gp_regex_cmp_count GREATER 1)
  290. if(gp_regex_cmp_count GREATER 2)
  291. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  292. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  293. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  294. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  295. endif(gp_regex_cmp_count GREATER 2)
  296. # Using find_program on Windows will find dll files that are in the PATH.
  297. # (Converting simple file names into full path names if found.)
  298. #
  299. set(item "item-NOTFOUND")
  300. find_program(item "${raw_item}" PATHS "${target_dir}")
  301. if(NOT item)
  302. set(item "${raw_item}")
  303. endif(NOT item)
  304. if(verbose)
  305. message(STATUS "raw_item='${raw_item}'")
  306. message(STATUS "item='${item}'")
  307. endif(verbose)
  308. # Add each item unless it is excluded:
  309. #
  310. set(add_item 1)
  311. if(${exclude_system})
  312. set(type "")
  313. gp_file_type("${target}" "${item}" type)
  314. if("${type}" STREQUAL "system")
  315. set(add_item 0)
  316. endif("${type}" STREQUAL "system")
  317. endif(${exclude_system})
  318. if(add_item)
  319. list(LENGTH ${prerequisites_var} list_length_before_append)
  320. gp_append_unique(${prerequisites_var} "${item}")
  321. list(LENGTH ${prerequisites_var} list_length_after_append)
  322. if(${recurse})
  323. # If item was really added, this is the first time we have seen it.
  324. # Add it to unseen_prereqs so that we can recursively add *its*
  325. # prerequisites...
  326. #
  327. if(NOT list_length_before_append EQUAL list_length_after_append)
  328. set(unseen_prereqs ${unseen_prereqs} "${item}")
  329. endif(NOT list_length_before_append EQUAL list_length_after_append)
  330. endif(${recurse})
  331. endif(add_item)
  332. else("${candidate}" MATCHES "${gp_regex}")
  333. if(verbose)
  334. message(STATUS "ignoring non-matching line: '${candidate}'")
  335. endif(verbose)
  336. endif("${candidate}" MATCHES "${gp_regex}")
  337. endforeach(candidate)
  338. list(SORT ${prerequisites_var})
  339. if(${recurse})
  340. set(more_inputs ${unseen_prereqs})
  341. foreach(input ${more_inputs})
  342. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse})
  343. endforeach(input)
  344. endif(${recurse})
  345. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  346. endfunction(get_prerequisites)
  347. # list_prerequisites target all exclude_system verbose
  348. #
  349. # ARGV0 (target) is the full path to an executable file
  350. #
  351. # optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
  352. # 1 for all prerequisites recursively
  353. #
  354. # optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
  355. # prerequisites , 1 to exclude them
  356. #
  357. # optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
  358. # names of prerequisites, 1 to print extra information
  359. #
  360. function(list_prerequisites target)
  361. if("${ARGV1}" STREQUAL "")
  362. set(all 1)
  363. else("${ARGV1}" STREQUAL "")
  364. set(all "${ARGV1}")
  365. endif("${ARGV1}" STREQUAL "")
  366. if("${ARGV2}" STREQUAL "")
  367. set(exclude_system 0)
  368. else("${ARGV2}" STREQUAL "")
  369. set(exclude_system "${ARGV2}")
  370. endif("${ARGV2}" STREQUAL "")
  371. if("${ARGV3}" STREQUAL "")
  372. set(verbose 0)
  373. else("${ARGV3}" STREQUAL "")
  374. set(verbose "${ARGV3}")
  375. endif("${ARGV3}" STREQUAL "")
  376. set(count 0)
  377. set(count_str "")
  378. set(print_count "${verbose}")
  379. set(print_prerequisite_type "${verbose}")
  380. set(print_target "${verbose}")
  381. set(type_str "")
  382. set(prereqs "")
  383. get_prerequisites("${target}" prereqs ${exclude_system} ${all})
  384. if(print_target)
  385. message(STATUS "File '${target}' depends on:")
  386. endif(print_target)
  387. foreach(d ${prereqs})
  388. math(EXPR count "${count} + 1")
  389. if(print_count)
  390. set(count_str "${count}. ")
  391. endif(print_count)
  392. if(print_prerequisite_type)
  393. gp_file_type("${target}" "${d}" type)
  394. set(type_str " (${type})")
  395. endif(print_prerequisite_type)
  396. message(STATUS "${count_str}${d}${type_str}")
  397. endforeach(d)
  398. endfunction(list_prerequisites)
  399. # list_prerequisites_by_glob glob_arg glob_exp
  400. #
  401. # glob_arg is GLOB or GLOB_RECURSE
  402. #
  403. # glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
  404. # of matching files. If a matching file is executable, its prerequisites are
  405. # listed.
  406. #
  407. # Any additional (optional) arguments provided are passed along as the
  408. # optional arguments to the list_prerequisites calls.
  409. #
  410. function(list_prerequisites_by_glob glob_arg glob_exp)
  411. message(STATUS "=============================================================================")
  412. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  413. message(STATUS "")
  414. file(${glob_arg} file_list ${glob_exp})
  415. foreach(f ${file_list})
  416. is_file_executable("${f}" is_f_executable)
  417. if(is_f_executable)
  418. message(STATUS "=============================================================================")
  419. list_prerequisites("${f}" ${ARGN})
  420. message(STATUS "")
  421. endif(is_f_executable)
  422. endforeach(f)
  423. endfunction(list_prerequisites_by_glob)