You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CMakeMacroLibtoolFile.cmake 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. macro(libtool_create_control_file _target)
  2. get_target_property(_target_type ${_target} TYPE)
  3. message("-- Creating static libtool control file for target ${_target}")
  4. # No support for shared libraries, as TigerVNC only needs libtool config
  5. # files for static libraries.
  6. if("${_target_type}" MATCHES "^[^STATIC_LIBRARY]$")
  7. message(ERROR " - trying to use libtool_create_control_file for non-static library target.")
  8. endif()
  9. #
  10. # Parse the target_LIB_DEPENDS variable to determine which libraries to put
  11. # into libtool control file as library dependencies, and handle a few corner
  12. # cases.
  13. #
  14. # First we need to split up any internal entries
  15. set(target_libs "")
  16. foreach(library ${${_target}_LIB_DEPENDS})
  17. if("${library}" MATCHES " ")
  18. string(REPLACE " " ";" lib_list "${library}")
  19. list(APPEND target_libs ${lib_list})
  20. else()
  21. list(APPEND target_libs "${library}")
  22. endif()
  23. endforeach()
  24. set(STATIC_MODE OFF)
  25. foreach(library ${target_libs})
  26. # Assume all entries are shared libs if platform-specific static library
  27. # extension is not matched.
  28. if("${library}" MATCHES "[^.+\\${CMAKE_STATIC_LIBRARY_SUFFIX}]$")
  29. if("${library}" MATCHES ".+\\${CMAKE_SHARED_LIBRARY_SUFFIX}$")
  30. # Shared library extension matched, so extract the path and library
  31. # name, then add the result to the libtool dependency libs. This
  32. # will always be an absolute path, because that's what CMake uses
  33. # internally.
  34. get_filename_component(_shared_lib ${library} NAME_WE)
  35. get_filename_component(_shared_lib_path ${library} PATH)
  36. string(REPLACE "lib" "" _shared_lib ${_shared_lib})
  37. set(_target_dependency_libs "${_target_dependency_libs} -L${_shared_lib_path} -l${_shared_lib}")
  38. else()
  39. # No shared library extension matched. Check whether target is a CMake
  40. # target.
  41. if(TARGET ${library})
  42. # Target is a CMake target, so ignore (CMake targets are static
  43. # libs in TigerVNC.)
  44. elseif(${library} STREQUAL "-Wl,-Bstatic")
  45. # All following libraries should be static
  46. set(STATIC_MODE ON)
  47. elseif(${library} STREQUAL "-Wl,-Bdynamic")
  48. # All following libraries should be dynamic
  49. set(STATIC_MODE OFF)
  50. else()
  51. # Normal library, so use find_library() to attempt to locate the
  52. # library in a system directory.
  53. # Need to remove -l prefix
  54. if (${library} MATCHES "^\\${CMAKE_LINK_LIBRARY_FLAG}")
  55. string(REPLACE ${CMAKE_LINK_LIBRARY_FLAG} "" library ${library})
  56. endif()
  57. if(STATIC_MODE)
  58. set(library ${CMAKE_STATIC_LIBRARY_PREFIX}${library}${CMAKE_STATIC_LIBRARY_SUFFIX})
  59. endif()
  60. find_library(FL ${library})
  61. if(FL)
  62. # Found library. Depending on if it's static or not we might
  63. # extract the path and library name, then add the
  64. # result to the libtool dependency libs.
  65. if(STATIC_MODE)
  66. set(_target_dependency_libs "${_target_dependency_libs} ${FL}")
  67. else()
  68. get_filename_component(_shared_lib ${FL} NAME_WE)
  69. get_filename_component(_shared_lib_path ${FL} PATH)
  70. string(REPLACE "lib" "" _shared_lib ${_shared_lib})
  71. set(_target_dependency_libs "${_target_dependency_libs} -L${_shared_lib_path} -l${_shared_lib}")
  72. endif()
  73. else()
  74. # No library found, so ignore target.
  75. endif()
  76. # Need to clear FL to get new results next loop
  77. unset(FL CACHE)
  78. endif()
  79. endif()
  80. else()
  81. # Detected a static library. Check whether the library pathname is
  82. # absolute and, if not, use find_library() to get the absolute path.
  83. get_filename_component(_name ${library} NAME)
  84. string(REPLACE "${_name}" "" _path ${library})
  85. if(NOT "${_path}" STREQUAL "")
  86. # Pathname is absolute, so add it to the libtool library dependencies
  87. # as-is.
  88. set(_target_dependency_libs "${_target_dependency_libs} ${library}")
  89. else()
  90. # Pathname is not absolute, so use find_library() to get the absolute
  91. # path.
  92. find_library(FL ${library})
  93. if(FL)
  94. # Absolute pathname found. Add it.
  95. set(_target_dependency_libs "${_target_dependency_libs} ${FL}")
  96. else()
  97. # No absolute pathname found. Ignore it.
  98. endif()
  99. # Need to clear FL to get new results next loop
  100. unset(FL CACHE)
  101. endif()
  102. endif()
  103. endforeach()
  104. # Write the libtool control file for the static library
  105. set(_lname ${CMAKE_STATIC_LIBRARY_PREFIX}${_target})
  106. set(_laname ${CMAKE_CURRENT_BINARY_DIR}/${_lname}.la)
  107. file(WRITE ${_laname} "# ${_lname}.la - a libtool library file\n# Generated by ltmain.sh (GNU libtool) 2.2.6b\n")
  108. file(APPEND ${_laname} "dlname=''\n\n")
  109. file(APPEND ${_laname} "library_names=''\n\n")
  110. file(APPEND ${_laname} "old_library='${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX}'\n\n")
  111. file(APPEND ${_laname} "inherited_linker_flags=''\n\n")
  112. file(APPEND ${_laname} "dependency_libs=' ${_target_dependency_libs}'\n\n")
  113. file(APPEND ${_laname} "weak_library_names=''\n\n")
  114. file(APPEND ${_laname} "current=\n")
  115. file(APPEND ${_laname} "age=\n")
  116. file(APPEND ${_laname} "revision=\n\n")
  117. file(APPEND ${_laname} "installed=no\n\n")
  118. file(APPEND ${_laname} "shouldnotlink=no\n\n")
  119. file(APPEND ${_laname} "dlopen=''\n")
  120. file(APPEND ${_laname} "dlpreopen=''\n\n")
  121. file(APPEND ${_laname} "libdir='/usr/lib'\n\n")
  122. # Make sure the timestamp is updated to trigger other make invocations
  123. add_custom_command(TARGET ${_target} POST_BUILD COMMAND
  124. "${CMAKE_COMMAND}" -E touch "${_laname}")
  125. # Add custom command to symlink the static library so that autotools finds
  126. # the library in .libs. These are executed after the specified target build.
  127. add_custom_command(TARGET ${_target} POST_BUILD COMMAND
  128. "${CMAKE_COMMAND}" -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/.libs")
  129. add_custom_command(TARGET ${_target} POST_BUILD COMMAND
  130. "${CMAKE_COMMAND}" -E create_symlink ../${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX} "${CMAKE_CURRENT_BINARY_DIR}/.libs/${_lname}${CMAKE_STATIC_LIBRARY_SUFFIX}")
  131. endmacro()