GetPrerequisites.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. endif("${gp_tool}" STREQUAL "dumpbin")
  222. if(NOT gp_tool_known)
  223. message(STATUS "warning: gp_tool='${gp_tool}' is an unknown tool...")
  224. message(STATUS "CMake function get_prerequisites needs more code to handle '${gp_tool}'")
  225. message(STATUS "Valid gp_tool values are dumpbin, ldd and otool.")
  226. return()
  227. endif(NOT gp_tool_known)
  228. set(gp_cmd_paths ${gp_cmd_paths}
  229. "C:/Program Files/Microsoft Visual Studio 9.0/VC/bin"
  230. "C:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/bin"
  231. "C:/Program Files/Microsoft Visual Studio 8/VC/BIN"
  232. "C:/Program Files (x86)/Microsoft Visual Studio 8/VC/BIN"
  233. "C:/Program Files/Microsoft Visual Studio .NET 2003/VC7/BIN"
  234. "C:/Program Files (x86)/Microsoft Visual Studio .NET 2003/VC7/BIN"
  235. "/usr/local/bin"
  236. "/usr/bin"
  237. )
  238. find_program(gp_cmd ${gp_tool} PATHS ${gp_cmd_paths})
  239. if(NOT gp_cmd)
  240. message(STATUS "warning: could not find '${gp_tool}' - cannot analyze prerequisites...")
  241. return()
  242. endif(NOT gp_cmd)
  243. if("${gp_tool}" STREQUAL "dumpbin")
  244. # When running dumpbin, it also needs the "Common7/IDE" directory in the
  245. # PATH. It will already be in the PATH if being run from a Visual Studio
  246. # command prompt. Add it to the PATH here in case we are running from a
  247. # different command prompt.
  248. #
  249. get_filename_component(gp_cmd_dir "${gp_cmd}" PATH)
  250. get_filename_component(gp_cmd_dlls_dir "${gp_cmd_dir}/../../Common7/IDE" ABSOLUTE)
  251. if(EXISTS "${gp_cmd_dlls_dir}")
  252. set(ENV{PATH} "$ENV{PATH};${gp_cmd_dlls_dir}")
  253. endif(EXISTS "${gp_cmd_dlls_dir}")
  254. endif("${gp_tool}" STREQUAL "dumpbin")
  255. #
  256. # </setup-gp_tool-vars>
  257. # Track new prerequisites at each new level of recursion. Start with an
  258. # empty list at each level:
  259. #
  260. set(unseen_prereqs)
  261. # Run gp_cmd on the target:
  262. #
  263. execute_process(
  264. COMMAND ${gp_cmd} ${gp_cmd_args} ${target}
  265. OUTPUT_VARIABLE gp_cmd_ov
  266. )
  267. if(verbose)
  268. message(STATUS "<RawOutput cmd='${gp_cmd} ${gp_cmd_args} ${target}'>")
  269. message(STATUS "gp_cmd_ov='${gp_cmd_ov}'")
  270. message(STATUS "</RawOutput>")
  271. endif(verbose)
  272. get_filename_component(target_dir "${target}" PATH)
  273. # Convert to a list of lines:
  274. #
  275. string(REGEX REPLACE ";" "\\\\;" candidates "${gp_cmd_ov}")
  276. string(REGEX REPLACE "\n" "${eol_char};" candidates "${candidates}")
  277. # Analyze each line for file names that match the regular expression:
  278. #
  279. foreach(candidate ${candidates})
  280. if("${candidate}" MATCHES "${gp_regex}")
  281. # Extract information from each candidate:
  282. string(REGEX REPLACE "${gp_regex}" "\\1" raw_item "${candidate}")
  283. if(gp_regex_cmp_count GREATER 1)
  284. string(REGEX REPLACE "${gp_regex}" "\\2" raw_compat_version "${candidate}")
  285. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" compat_major_version "${raw_compat_version}")
  286. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" compat_minor_version "${raw_compat_version}")
  287. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" compat_patch_version "${raw_compat_version}")
  288. endif(gp_regex_cmp_count GREATER 1)
  289. if(gp_regex_cmp_count GREATER 2)
  290. string(REGEX REPLACE "${gp_regex}" "\\3" raw_current_version "${candidate}")
  291. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\1" current_major_version "${raw_current_version}")
  292. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\2" current_minor_version "${raw_current_version}")
  293. string(REGEX REPLACE "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$" "\\3" current_patch_version "${raw_current_version}")
  294. endif(gp_regex_cmp_count GREATER 2)
  295. # Using find_program on Windows will find dll files that are in the PATH.
  296. # (Converting simple file names into full path names if found.)
  297. #
  298. set(item "item-NOTFOUND")
  299. find_program(item "${raw_item}" PATHS "${target_dir}")
  300. if(NOT item)
  301. set(item "${raw_item}")
  302. endif(NOT item)
  303. if(verbose)
  304. message(STATUS "raw_item='${raw_item}'")
  305. message(STATUS "item='${item}'")
  306. endif(verbose)
  307. # Add each item unless it is excluded:
  308. #
  309. set(add_item 1)
  310. if(${exclude_system})
  311. set(type "")
  312. gp_file_type("${target}" "${item}" type)
  313. if("${type}" STREQUAL "system")
  314. set(add_item 0)
  315. endif("${type}" STREQUAL "system")
  316. endif(${exclude_system})
  317. if(add_item)
  318. list(LENGTH ${prerequisites_var} list_length_before_append)
  319. gp_append_unique(${prerequisites_var} "${item}")
  320. list(LENGTH ${prerequisites_var} list_length_after_append)
  321. if(${recurse})
  322. # If item was really added, this is the first time we have seen it.
  323. # Add it to unseen_prereqs so that we can recursively add *its*
  324. # prerequisites...
  325. #
  326. if(NOT list_length_before_append EQUAL list_length_after_append)
  327. set(unseen_prereqs ${unseen_prereqs} "${item}")
  328. endif(NOT list_length_before_append EQUAL list_length_after_append)
  329. endif(${recurse})
  330. endif(add_item)
  331. else("${candidate}" MATCHES "${gp_regex}")
  332. if(verbose)
  333. message(STATUS "ignoring non-matching line: '${candidate}'")
  334. endif(verbose)
  335. endif("${candidate}" MATCHES "${gp_regex}")
  336. endforeach(candidate)
  337. list(SORT ${prerequisites_var})
  338. if(${recurse})
  339. set(more_inputs ${unseen_prereqs})
  340. foreach(input ${more_inputs})
  341. get_prerequisites("${input}" ${prerequisites_var} ${exclude_system} ${recurse})
  342. endforeach(input)
  343. endif(${recurse})
  344. set(${prerequisites_var} ${${prerequisites_var}} PARENT_SCOPE)
  345. endfunction(get_prerequisites)
  346. # list_prerequisites target all exclude_system verbose
  347. #
  348. # ARGV0 (target) is the full path to an executable file
  349. #
  350. # optional ARGV1 (all) is 0 or 1: 0 for direct prerequisites only,
  351. # 1 for all prerequisites recursively
  352. #
  353. # optional ARGV2 (exclude_system) is 0 or 1: 0 to include "system"
  354. # prerequisites , 1 to exclude them
  355. #
  356. # optional ARGV3 (verbose) is 0 or 1: 0 to print only full path
  357. # names of prerequisites, 1 to print extra information
  358. #
  359. function(list_prerequisites target)
  360. if("${ARGV1}" STREQUAL "")
  361. set(all 1)
  362. else("${ARGV1}" STREQUAL "")
  363. set(all "${ARGV1}")
  364. endif("${ARGV1}" STREQUAL "")
  365. if("${ARGV2}" STREQUAL "")
  366. set(exclude_system 0)
  367. else("${ARGV2}" STREQUAL "")
  368. set(exclude_system "${ARGV2}")
  369. endif("${ARGV2}" STREQUAL "")
  370. if("${ARGV3}" STREQUAL "")
  371. set(verbose 0)
  372. else("${ARGV3}" STREQUAL "")
  373. set(verbose "${ARGV3}")
  374. endif("${ARGV3}" STREQUAL "")
  375. set(count 0)
  376. set(count_str "")
  377. set(print_count "${verbose}")
  378. set(print_prerequisite_type "${verbose}")
  379. set(print_target "${verbose}")
  380. set(type_str "")
  381. set(prereqs "")
  382. get_prerequisites("${target}" prereqs ${exclude_system} ${all})
  383. if(print_target)
  384. message(STATUS "File '${target}' depends on:")
  385. endif(print_target)
  386. foreach(d ${prereqs})
  387. math(EXPR count "${count} + 1")
  388. if(print_count)
  389. set(count_str "${count}. ")
  390. endif(print_count)
  391. if(print_prerequisite_type)
  392. gp_file_type("${target}" "${d}" type)
  393. set(type_str " (${type})")
  394. endif(print_prerequisite_type)
  395. message(STATUS "${count_str}${d}${type_str}")
  396. endforeach(d)
  397. endfunction(list_prerequisites)
  398. # list_prerequisites_by_glob glob_arg glob_exp
  399. #
  400. # glob_arg is GLOB or GLOB_RECURSE
  401. #
  402. # glob_exp is a globbing expression used with "file(GLOB" to retrieve a list
  403. # of matching files. If a matching file is executable, its prerequisites are
  404. # listed.
  405. #
  406. # Any additional (optional) arguments provided are passed along as the
  407. # optional arguments to the list_prerequisites calls.
  408. #
  409. function(list_prerequisites_by_glob glob_arg glob_exp)
  410. message(STATUS "=============================================================================")
  411. message(STATUS "List prerequisites of executables matching ${glob_arg} '${glob_exp}'")
  412. message(STATUS "")
  413. file(${glob_arg} file_list ${glob_exp})
  414. foreach(f ${file_list})
  415. is_file_executable("${f}" is_f_executable)
  416. if(is_f_executable)
  417. message(STATUS "=============================================================================")
  418. list_prerequisites("${f}" ${ARGN})
  419. message(STATUS "")
  420. endif(is_f_executable)
  421. endforeach(f)
  422. endfunction(list_prerequisites_by_glob)