0

I want to generate a libtool file for a library that I'm building with CMake 3. I found this macro, which would have helped with an older version of CMake: https://gitlab.kitware.com/cmake/community/-/wikis/contrib/macros/LibtoolFile

Here's the code that works with CMake 2.8:

 MACRO(GET_TARGET_PROPERTY_WITH_DEFAULT _variable _target _property _default_value)
   GET_TARGET_PROPERTY (${_variable} ${_target} ${_property})
   IF (${_variable} MATCHES NOTFOUND)
     SET (${_variable} ${_default_value})
   ENDIF (${_variable} MATCHES NOTFOUND)
 ENDMACRO (GET_TARGET_PROPERTY_WITH_DEFAULT)

 MACRO(CREATE_LIBTOOL_FILE _target _install_DIR)
   GET_TARGET_PROPERTY(_target_location ${_target} LOCATION)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_static_lib ${_target} STATIC_LIB "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dependency_libs ${_target} LT_DEPENDENCY_LIBS "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_current ${_target} LT_VERSION_CURRENT 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_age ${_target} LT_VERSION_AGE 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_revision ${_target} LT_VERSION_REVISION 0)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_installed ${_target} LT_INSTALLED yes)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_shouldnotlink ${_target} LT_SHOULDNOTLINK yes)
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlopen ${_target} LT_DLOPEN "")
   GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlpreopen ${_target} LT_DLPREOPEN "")
   GET_FILENAME_COMPONENT(_laname ${_target_location} NAME_WE)
   GET_FILENAME_COMPONENT(_soname ${_target_location} NAME)
   SET(_laname ${PROJECT_BINARY_DIR}/${_laname}.la)
   FILE(WRITE ${_laname} "# ${_laname} - a libtool library file\n")
   FILE(WRITE ${_laname} "# Generated by CMake ${CMAKE_VERSION} (like GNU libtool)\n")
   FILE(WRITE ${_laname} "\n# Please DO NOT delete this file!\n# It is necessary for linking the library with libtool.\n\n" )
   FILE(APPEND ${_laname} "# The name that we can dlopen(3).\n")
   FILE(APPEND ${_laname} "dlname='${_soname}'\n\n")
   FILE(APPEND ${_laname} "# Names of this library.\n")
   FILE(APPEND ${_laname} "library_names='${_soname}.${_target_current}.${_target_age}.${_target_revision} ${_soname}.${_target_current} ${_soname}'\n\n")
   FILE(APPEND ${_laname} "# The name of the static archive.\n")
   FILE(APPEND ${_laname} "old_library='${_target_static_lib}'\n\n")
   FILE(APPEND ${_laname} "# Libraries that this one depends upon.\n")
   FILE(APPEND ${_laname} "dependency_libs='${_target_dependency_libs}'\n\n")
   FILE(APPEND ${_laname} "# Names of additional weak libraries provided by this library\n")
   FILE(APPEND ${_laname} "weak_library_names=''\n\n")
   FILE(APPEND ${_laname} "# Version information for ${_laname}.\n")
   FILE(APPEND ${_laname} "current=${_target_current}\n")
   FILE(APPEND ${_laname} "age=${_target_age}\n")
   FILE(APPEND ${_laname} "revision=${_target_revision}\n\n")
   FILE(APPEND ${_laname} "# Is this an already installed library?\n")
   FILE(APPEND ${_laname} "installed=${_target_installed}\n\n")
   FILE(APPEND ${_laname} "# Should we warn about portability when linking against -modules?\n")
   FILE(APPEND ${_laname} "shouldnotlink=${_target_shouldnotlink}\n\n")
   FILE(APPEND ${_laname} "# Files to dlopen/dlpreopen\n")
   FILE(APPEND ${_laname} "dlopen='${_target_dlopen}'\n")
   FILE(APPEND ${_laname} "dlpreopen='${_target_dlpreopen}'\n\n")
   FILE(APPEND ${_laname} "# Directory that this library needs to be installed in:\n")
   FILE(APPEND ${_laname} "libdir='${CMAKE_INSTALL_PREFIX}${_install_DIR}'\n")
   INSTALL( FILES ${_laname} DESTINATION ${CMAKE_INSTALL_PREFIX}${_install_DIR})
 ENDMACRO(CREATE_LIBTOOL_FILE)

Unfortunately, that won't work for me now, because recent versions of cmake no longer support GET_TARGET_PROPERTY(... LOCATION). I see that there's a generator expression you can use ($<TARGET_FILE_NAME:target_name>) in its place, but the rules under which you can use generator expressions are different, and my experience with CMake is still very limited.

Can anyone give me hints as to how I might adapt the above macro for recent versions of CMake, or possibly point me to an alternate solution if one already exists?

EDIT: this question was closed because it was "the same" as this: How can I remove the "the Location property may not be read from target" error in CMake?

The content is similar, but not the same. The linked question above explains how to replace a specific line of code, while my question is asking for guidance on how to come up with an alternate solution to the code found here: https://gitlab.kitware.com/cmake/community/-/wikis/contrib/macros/LibtoolFile

Crankycyclops
  • 552
  • 5
  • 25
  • "but the rules under which you can use generator expressions are different, and my experience with CMake is still very limited." - Don't you find that it is the right time to **try** something and improve your CMake experience? We won't write the code **for you** neither we won't adapt some other code **for you**. We could help you in writing a code or adapting it, but we expect you to do something. Asking every time "Can you fix the code for me because generator expressions are too difficult for me" is not very productive. – Tsyvarev Jun 25 '21 at 17:53
  • Just a hint: The code you refers to creates a file via `file(WRITE)`. That way you cannot write generator expressions to the file. But for create a file you could use [file(GENERATE)](https://cmake.org/cmake/help/latest/command/file.html#generate) command, so generator expressions will be properly expanded in it. – Tsyvarev Jun 25 '21 at 17:56
  • I asked for help, not for you to write the code for me. Your second comment would have been enough. – Crankycyclops Jun 25 '21 at 18:32
  • "my question is asking for guidance on how to come up with an alternate solution to the code found here:" - Yes, that would legitimate request for help with the code, but for it you need to add the code into your **question post**, link is not sufficient. (Imagine Kitware will find that the code your refer to is staled and will remove the page contained that code. In that case your link will be dead, and without the code your question will be unclear for future visitors. Note, that the main purpose of Stack Overflow is to have question/answers useful for future visitors.) – Tsyvarev Jun 25 '21 at 19:23
  • Understood. I updated the question to include the code and to make the nature of my question more clear in terms of what I'm asking for. Let me know if it meets with your approval. – Crankycyclops Jun 25 '21 at 20:15
  • Ok, so you are aware that reading LOCATION property should be replaced with generator expressions. Please, add to the question post your **attempt** of using them (What have you tried?) – Tsyvarev Jun 25 '21 at 20:18

0 Answers0