VTK  9.0.1
vtkDetectLibraryType.cmake
Go to the documentation of this file.
1 include(CMakeParseArguments)
2 
3 #[==[
4 @brief Detect library type of a library
5 
6 Sometimes it needs to be known whether a library is shared or static on a
7 system in order to change the usage requirements of an imported target
8 representing that library. This commonly occurs between static and shared
9 builds that share a set of installed headers. This function returns one of
10 `SHARED`, `STATIC`, or `UNKNOWN` into the variable passed as the first
11 argument.
12 
13 ~~~
14 vtk_detect_library_type(<variable>
15  PATH <path>)
16 ~~~
17 #]==]
18 function (vtk_detect_library_type output)
19  cmake_parse_arguments(vdlt
20  ""
21  "PATH"
22  ""
23  ${ARGN})
24 
25  if (NOT DEFINED vdlt_PATH)
26  message(FATAL_ERROR
27  "The `PATH` argument is required.")
28  endif ()
29 
30  if (DEFINED vdlt_UNPARSED_ARGUMENTS)
31  message(FATAL_ERROR
32  "Unparsed arguments for vtk_detect_library_type: "
33  "${vdlt_UNPARSED_ARGUMENTS}")
34  endif ()
35 
36  if (NOT vdlt_PATH)
37  message(FATAL_ERROR
38  "The `PATH` argument is empty.")
39  endif ()
40 
41  set(vdlt_type UNKNOWN)
42  # Windows libraries all end with `.lib`. We need to detect the type based on
43  # the contents of the library. However, MinGW does use different extensions.
44  if (WIN32 AND NOT MINGW)
45  find_program(DUMPBIN_EXECUTABLE
46  NAMES dumpbin
47  DOC "Path to the dumpbin executable")
48  mark_as_advanced(DUMPBIN_EXECUTABLE)
49  execute_process(
50  COMMAND "${DUMPBIN_EXECUTABLE}"
51  /HEADERS
52  "${vdlt_PATH}"
53  OUTPUT_VARIABLE vdlt_out
54  ERROR_VARIABLE vdlt_err
55  RESULT_VARIABLE vdlt_res)
56  if (vdlt_res)
57  message(WARNING
58  "Failed to run `dumpbin` on ${vdlt_PATH}. Cannot determine "
59  "shared/static library type: ${vdlt_err}")
60  else ()
61  if (vdlt_out MATCHES "DLL name :")
62  set(vdlt_type SHARED)
63  else ()
64  set(vdlt_type STATIC)
65  endif ()
66  endif ()
67  else ()
68  string(LENGTH "${vdlt_PATH}" vdlt_path_len)
69 
70  string(LENGTH "${CMAKE_SHARED_LIBRARY_SUFFIX}" vdlt_shared_suffix_len)
71  math(EXPR vdlt_shared_idx "${vdlt_path_len} - ${vdlt_shared_suffix_len}")
72  string(SUBSTRING "${vdlt_PATH}" "${vdlt_shared_idx}" -1 vdlt_shared_check)
73 
74  string(LENGTH "${CMAKE_STATIC_LIBRARY_SUFFIX}" vdlt_static_suffix_len)
75  math(EXPR vdlt_static_idx "${vdlt_path_len} - ${vdlt_static_suffix_len}")
76  string(SUBSTRING "${vdlt_PATH}" "${vdlt_static_idx}" -1 vdlt_static_check)
77 
78  if (vdlt_shared_check STREQUAL CMAKE_SHARED_LIBRARY_SUFFIX)
79  set(vdlt_type SHARED)
80  elseif (vdlt_static_check STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
81  set(vdlt_type STATIC)
82  endif ()
83 
84  # when import suffix != static suffix, we can disambiguate static and import
85  if (WIN32 AND NOT CMAKE_IMPORT_LIBRARY_SUFFIX STREQUAL CMAKE_STATIC_LIBRARY_SUFFIX)
86  string(LENGTH "${CMAKE_IMPORT_LIBRARY_SUFFIX}" vdlt_import_suffix_len)
87  math(EXPR vdlt_import_idx "${vdlt_path_len} - ${vdlt_import_suffix_len}")
88  string(SUBSTRING "${vdlt_PATH}" "${vdlt_import_idx}" -1 vdlt_import_check)
89  if (vdlt_import_check STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
90  set(vdlt_type SHARED)
91  endif ()
92  endif ()
93  endif ()
94 
95  set("${output}"
96  "${vdlt_type}"
97  PARENT_SCOPE)
98 endfunction ()
99 
100 #[==[
101 @brief Detect whether an imported target is shared or not
102 
103 This is intended for use with modules using
104 @ref vtk_module_third_party_external to detect whether that module is shared or
105 not. Generally, this should be replaced with the `Find` module providing this
106 information and modifying the usage requirements as necessary instead, but it
107 is not always possible.
108 
109 ~~~
110 vtk_detect_library_shared(<name> <target>)
111 ~~~
112 
113 Sets `<name>_is_shared` in the caller's scope if `<target>` is a shared
114 library. If it is an `UNKNOWN_LIBRARY`, a cache variable is exposed to allow
115 the user to provide the information if it ends up breaking something.
116 #]==]
117 function (vtk_detect_library_shared name target)
118  if (VTK_MODULE_USE_EXTERNAL_${name})
119  get_property(library_type
120  TARGET "${target}"
121  PROPERTY TYPE)
122  if (library_type STREQUAL "SHARED_LIBRARY")
123  set(is_shared 1)
124  elseif (library_type STREQUAL "UNKNOWN_LIBRARY")
125  option("VTK_MODULE_${name}_IS_SHARED" "Whether the ${name} in use is shared or not" ON)
126  mark_as_advanced("VTK_MODULE_${name}_IS_SHARED")
127  set(is_shared "${VTK_MODULE_${name}_IS_SHARED}")
128  else ()
129  set(is_shared 0)
130  endif ()
131  else ()
132  set(is_shared "${BUILD_SHARED_LIBS}")
133  endif ()
134 
135  set("${name}_is_shared"
136  "${is_shared}"
137  PARENT_SCOPE)
138 endfunction ()
vtk_detect_library_type
function vtk_detect_library_type(output)
Detect library type of a library.
Definition: vtkDetectLibraryType.cmake:18
vtkX3D::type
@ type
Definition: vtkX3D.h:522
vtkX3D::on
@ on
Definition: vtkX3D.h:445
target
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:965
vtkX3D::name
@ name
Definition: vtkX3D.h:225
vtkX3D::order
@ order
Definition: vtkX3D.h:446