blob: 9d04ef5631bc2c3c83095e10d1e8c70b49ac7a9b [file] [log] [blame]
Henrik Andersson23029cc2011-06-09 09:13:23 +00001macro(libtool_create_control_file _target)
Henrik Andersson23029cc2011-06-09 09:13:23 +00002 get_target_property(_target_type ${_target} TYPE)
3
4 message("-- Creating static libtool control file for target ${_target}")
DRCa18a88f2011-06-22 00:08:26 +00005 # No support for shared libraries, as TigerVNC only needs libtool config
6 # files for static libraries.
Henrik Andersson23029cc2011-06-09 09:13:23 +00007 if("${_target_type}" MATCHES "^[^STATIC_LIBRARY]$")
DRCa18a88f2011-06-22 00:08:26 +00008 message(ERROR " - trying to use libtool_create_control_file for non-static library target.")
Henrik Andersson23029cc2011-06-09 09:13:23 +00009 endif()
10
11 #
DRCa18a88f2011-06-22 00:08:26 +000012 # Parse the target_LIB_DEPENDS variable to determine which libraries to put
13 # into libtool control file as library dependencies, and handle a few corner
14 # cases.
Henrik Andersson23029cc2011-06-09 09:13:23 +000015 #
Pierre Ossman216d5912014-11-03 14:18:44 +010016
17 # First we need to split up any internal entries
18 set(target_libs "")
Henrik Andersson23029cc2011-06-09 09:13:23 +000019 foreach(library ${${_target}_LIB_DEPENDS})
Pierre Ossman216d5912014-11-03 14:18:44 +010020 if("${library}" MATCHES " ")
21 string(REPLACE " " ";" lib_list "${library}")
22 list(APPEND target_libs ${lib_list})
23 else()
24 list(APPEND target_libs "${library}")
25 endif()
26 endforeach()
27
Pierre Ossman881a8e22014-11-03 14:20:46 +010028 set(STATIC_MODE OFF)
29
Pierre Ossman216d5912014-11-03 14:18:44 +010030 foreach(library ${target_libs})
DRCa18a88f2011-06-22 00:08:26 +000031 # Assume all entries are shared libs if platform-specific static library
32 # extension is not matched.
Henrik Andersson23029cc2011-06-09 09:13:23 +000033 if("${library}" MATCHES "[^.+\\${CMAKE_STATIC_LIBRARY_SUFFIX}]$")
Henrik Andersson23029cc2011-06-09 09:13:23 +000034 if("${library}" MATCHES ".+\\${CMAKE_SHARED_LIBRARY_SUFFIX}$")
DRCa18a88f2011-06-22 00:08:26 +000035 # Shared library extension matched, so extract the path and library
36 # name, then add the result to the libtool dependency libs. This
37 # will always be an absolute path, because that's what CMake uses
38 # internally.
DRCdadfbec2011-06-22 00:01:39 +000039 get_filename_component(_shared_lib ${library} NAME_WE)
40 get_filename_component(_shared_lib_path ${library} PATH)
41 string(REPLACE "lib" "" _shared_lib ${_shared_lib})
Henrik Andersson23029cc2011-06-09 09:13:23 +000042 set(_target_dependency_libs "${_target_dependency_libs} -L${_shared_lib_path} -l${_shared_lib}")
43 else()
DRCa18a88f2011-06-22 00:08:26 +000044 # No shared library extension matched. Check whether target is a CMake
45 # target.
Pierre Ossman26999672017-10-06 14:30:26 +020046 if(TARGET ${library})
Pierre Ossman881a8e22014-11-03 14:20:46 +010047 # Target is a CMake target, so ignore (CMake targets are static
48 # libs in TigerVNC.)
49 elseif(${library} STREQUAL "-Wl,-Bstatic")
50 # All following libraries should be static
51 set(STATIC_MODE ON)
52 elseif(${library} STREQUAL "-Wl,-Bdynamic")
53 # All following libraries should be dynamic
54 set(STATIC_MODE OFF)
55 else()
56 # Normal library, so use find_library() to attempt to locate the
DRCa18a88f2011-06-22 00:08:26 +000057 # library in a system directory.
Pierre Ossman40a5f9e2014-11-03 14:20:01 +010058
59 # Need to remove -l prefix
60 if (${library} MATCHES "^\\${CMAKE_LINK_LIBRARY_FLAG}")
61 string(REPLACE ${CMAKE_LINK_LIBRARY_FLAG} "" library ${library})
62 endif()
63
Pierre Ossman881a8e22014-11-03 14:20:46 +010064 if(STATIC_MODE)
65 set(library ${CMAKE_STATIC_LIBRARY_PREFIX}${library}${CMAKE_STATIC_LIBRARY_SUFFIX})
66 endif()
67
DRCdadfbec2011-06-22 00:01:39 +000068 find_library(FL ${library})
69 if(FL)
Pierre Ossman881a8e22014-11-03 14:20:46 +010070 # Found library. Depending on if it's static or not we might
71 # extract the path and library name, then add the
DRCa18a88f2011-06-22 00:08:26 +000072 # result to the libtool dependency libs.
Pierre Ossman881a8e22014-11-03 14:20:46 +010073 if(STATIC_MODE)
74 set(_target_dependency_libs "${_target_dependency_libs} ${FL}")
75 else()
76 get_filename_component(_shared_lib ${FL} NAME_WE)
77 get_filename_component(_shared_lib_path ${FL} PATH)
78 string(REPLACE "lib" "" _shared_lib ${_shared_lib})
79 set(_target_dependency_libs "${_target_dependency_libs} -L${_shared_lib_path} -l${_shared_lib}")
80 endif()
DRCdadfbec2011-06-22 00:01:39 +000081 else()
Pierre Ossman881a8e22014-11-03 14:20:46 +010082 # No library found, so ignore target.
DRCdadfbec2011-06-22 00:01:39 +000083 endif()
Pierre Ossman818550b2014-11-03 14:17:10 +010084 # Need to clear FL to get new results next loop
85 unset(FL CACHE)
DRCdadfbec2011-06-22 00:01:39 +000086 endif()
Henrik Andersson23029cc2011-06-09 09:13:23 +000087 endif()
88 else()
DRCa18a88f2011-06-22 00:08:26 +000089 # Detected a static library. Check whether the library pathname is
klemens0536d092017-01-28 20:56:56 +010090 # absolute and, if not, use find_library() to get the absolute path.
Henrik Andersson23029cc2011-06-09 09:13:23 +000091 get_filename_component(_name ${library} NAME)
92 string(REPLACE "${_name}" "" _path ${library})
DRC801ef7c2011-06-22 00:11:00 +000093 if(NOT "${_path}" STREQUAL "")
DRCa18a88f2011-06-22 00:08:26 +000094 # Pathname is absolute, so add it to the libtool library dependencies
95 # as-is.
Henrik Andersson23029cc2011-06-09 09:13:23 +000096 set(_target_dependency_libs "${_target_dependency_libs} ${library}")
97 else()
DRCa18a88f2011-06-22 00:08:26 +000098 # Pathname is not absolute, so use find_library() to get the absolute
99 # path.
DRCdadfbec2011-06-22 00:01:39 +0000100 find_library(FL ${library})
101 if(FL)
DRCa18a88f2011-06-22 00:08:26 +0000102 # Absolute pathname found. Add it.
DRCdadfbec2011-06-22 00:01:39 +0000103 set(_target_dependency_libs "${_target_dependency_libs} ${FL}")
104 else()
DRCa18a88f2011-06-22 00:08:26 +0000105 # No absolute pathname found. Ignore it.
DRCdadfbec2011-06-22 00:01:39 +0000106 endif()
Pierre Ossman818550b2014-11-03 14:17:10 +0100107 # Need to clear FL to get new results next loop
108 unset(FL CACHE)
Henrik Andersson23029cc2011-06-09 09:13:23 +0000109 endif()
110 endif()
111 endforeach()
112
DRCa18a88f2011-06-22 00:08:26 +0000113 # Write the libtool control file for the static library
Pierre Ossmanc27f5da2017-10-06 14:29:54 +0200114 set(_lname ${CMAKE_STATIC_LIBRARY_PREFIX}${_target})
Henrik Andersson23029cc2011-06-09 09:13:23 +0000115 set(_laname ${CMAKE_CURRENT_BINARY_DIR}/${_lname}.la)
DRC754ff592011-08-09 02:26:30 +0000116
Henrik Andersson23029cc2011-06-09 09:13:23 +0000117 file(WRITE ${_laname} "# ${_lname}.la - a libtool library file\n# Generated by ltmain.sh (GNU libtool) 2.2.6b\n")
118 file(APPEND ${_laname} "dlname=''\n\n")
119 file(APPEND ${_laname} "library_names=''\n\n")
Henrik Andersson697954f2011-06-09 13:26:05 +0000120 file(APPEND ${_laname} "old_library='${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX}'\n\n")
Henrik Andersson23029cc2011-06-09 09:13:23 +0000121 file(APPEND ${_laname} "inherited_linker_flags=''\n\n")
122 file(APPEND ${_laname} "dependency_libs=' ${_target_dependency_libs}'\n\n")
123 file(APPEND ${_laname} "weak_library_names=''\n\n")
124 file(APPEND ${_laname} "current=\n")
125 file(APPEND ${_laname} "age=\n")
126 file(APPEND ${_laname} "revision=\n\n")
127 file(APPEND ${_laname} "installed=no\n\n")
128 file(APPEND ${_laname} "shouldnotlink=no\n\n")
129 file(APPEND ${_laname} "dlopen=''\n")
130 file(APPEND ${_laname} "dlpreopen=''\n\n")
Pierre Ossmande2df7e2015-04-30 09:28:37 +0200131 file(APPEND ${_laname} "libdir='/usr/lib'\n\n")
Henrik Andersson23029cc2011-06-09 09:13:23 +0000132
Pierre Ossman4495b722016-12-11 14:04:37 +0100133 # Make sure the timestamp is updated to trigger other make invocations
134 add_custom_command(TARGET ${_target} POST_BUILD COMMAND
135 "${CMAKE_COMMAND}" -E touch "${_laname}")
136
Henrik Andersson697954f2011-06-09 13:26:05 +0000137
DRCa18a88f2011-06-22 00:08:26 +0000138 # Add custom command to symlink the static library so that autotools finds
139 # the library in .libs. These are executed after the specified target build.
Henrik Andersson697954f2011-06-09 13:26:05 +0000140 add_custom_command(TARGET ${_target} POST_BUILD COMMAND
Brian Hinz491b9502013-04-27 20:14:50 +0000141 "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/.libs")
Henrik Andersson697954f2011-06-09 13:26:05 +0000142 add_custom_command(TARGET ${_target} POST_BUILD COMMAND
Pierre Ossmanc27f5da2017-10-06 14:29:54 +0200143 "${CMAKE_COMMAND}" -E create_symlink ../${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX} "${CMAKE_CURRENT_BINARY_DIR}/.libs/${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX}")
Henrik Andersson697954f2011-06-09 13:26:05 +0000144
DRC5dc23af2011-06-09 22:06:05 +0000145endmacro()