# This macro creates a libtool .la file for shared libraries or plugins. The
# first parameter is the target name of the library, the second parameter is
# the directory where it will be installed to. E.g. for a KDE 3.x module named
# "kfoo" the usage would be as follows:
#
# The macro GET_TARGET_PROPERTY_WITH_DEFAULT is helpful to handle properties
# with default values.
#
# ADD_LIBRARY(foo SHARED kfoo1.cpp kfoo2.cpp)
# CREATE_LIBTOOL_FILE(foo /lib/kde3)
#
# Original macro taken from
#    http://www.cmake.org/Wiki/CMakeMacroLibtoolFile
# licensed under Creative Commons Attribution 2.5 Generic
#    (see http://creativecommons.org/licenses/by/2.5/)
# modified by Alberto Griggio

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_shared_lib ${_target} LT_SHARED_LIB "")
  GET_TARGET_PROPERTY_WITH_DEFAULT(_target_static_lib ${_target} LT_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 no)
  GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlopen ${_target} LT_DLOPEN "")
  GET_TARGET_PROPERTY_WITH_DEFAULT(_target_dlpreopen ${_target} LT_DLPREOPEN "")
  GET_FILENAME_COMPONENT(_laname_default ${_target_location} NAME_WE)
  GET_TARGET_PROPERTY_WITH_DEFAULT(_laname ${_target} LT_LANAME ${_laname_default})
  SET(_soname ${_target_shared_lib})
  SET(_laname ${_laname}.la)
  SET(_outlaname ${PROJECT_BINARY_DIR}/${_laname})
  FILE(WRITE ${_outlaname} "# ${_laname} - a libtool library file\n")
  FILE(APPEND ${_outlaname} "# Generated by CMake ${CMAKE_VERSION} (like GNU libtool)\n")
  FILE(APPEND ${_outlaname} "\n# Please DO NOT delete this file!\n# It is necessary for linking the library with libtool.\n\n" )
  FILE(APPEND ${_outlaname} "# The name that we can dlopen(3).\n")
  FILE(APPEND ${_outlaname} "dlname='${_soname}'\n\n")
  FILE(APPEND ${_outlaname} "# Names of this library.\n")
  IF("${_soname}" STREQUAL "")
      FILE(APPEND ${_outlaname} "library_names=''\n\n")
  ELSE("${_soname}" STREQUAL "")
      FILE(APPEND ${_outlaname} "library_names='${_soname}.${_target_current}.${_target_age}.${_target_revision} ${_soname}.${_target_current} ${_soname}'\n\n")
  ENDIF("${_soname}" STREQUAL "")
  
  FILE(APPEND ${_outlaname} "# The name of the static archive.\n")
  FILE(APPEND ${_outlaname} "old_library='${_target_static_lib}'\n\n")
  FILE(APPEND ${_outlaname} "# Libraries that this one depends upon.\n")
  FILE(APPEND ${_outlaname} "dependency_libs='${_target_dependency_libs}'\n\n")
  FILE(APPEND ${_outlaname} "# Names of additional weak libraries provided by this library\n")
  FILE(APPEND ${_outlaname} "weak_library_names=\n\n")
  FILE(APPEND ${_outlaname} "# Version information for ${_laname}.\n")
  FILE(APPEND ${_outlaname} "current=${_target_current}\n")
  FILE(APPEND ${_outlaname} "age=${_target_age}\n")
  FILE(APPEND ${_outlaname} "revision=${_target_revision}\n\n")
  FILE(APPEND ${_outlaname} "# Is this an already installed library?\n")
  FILE(APPEND ${_outlaname} "installed=${_target_installed}\n\n")
  FILE(APPEND ${_outlaname} "# Should we warn about portability when linking against -modules?\n")
  FILE(APPEND ${_outlaname} "shouldnotlink=${_target_shouldnotlink}\n\n")
  FILE(APPEND ${_outlaname} "# Files to dlopen/dlpreopen\n")
  FILE(APPEND ${_outlaname} "dlopen='${_target_dlopen}'\n")
  FILE(APPEND ${_outlaname} "dlpreopen='${_target_dlpreopen}'\n\n")
  FILE(APPEND ${_outlaname} "# Directory that this library needs to be installed in:\n")
  FILE(APPEND ${_outlaname} "libdir='${CMAKE_INSTALL_PREFIX}/${_install_DIR}'\n")
#  INSTALL( FILES ${_outlaname} DESTINATION ${CMAKE_INSTALL_PREFIX}${_install_DIR})
ENDMACRO(CREATE_LIBTOOL_FILE)
