Skip to content

Commit

Permalink
CMake: ability to install plugin binary / library to different places.
Browse files Browse the repository at this point in the history
In order to properly support Windows where DLLs should be in bin/ and
*.lib files in lib/.
  • Loading branch information
mosra committed Oct 11, 2016
1 parent d85e71c commit 8c5dcb6
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 37 deletions.
50 changes: 37 additions & 13 deletions doc/corrade-cmake.dox
Expand Up @@ -199,36 +199,60 @@ used for compiling library / executable. Example usage:
@anchor corrade-cmake-add-plugin

corrade_add_plugin(<plugin name>
<debug install dir> <release install dir>
"<debug binary install dir>;<debug library install dir>"
"<release binary install dir>;<release library install dir>"
<metadata file>
<sources>...)

The macro adds preprocessor directive `CORRADE_DYNAMIC_PLUGIN`. Additional
libraries can be linked in via `target_link_libraries(plugin_name ...)`. If
`<debug install dir>` is set to `CMAKE_CURRENT_BINARY_DIR` (e.g. for testing
purposes), the files are copied directly, without the need to perform install
step. Note that the files are actually put into configuration-based
subdirectory, i.e. `${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}`. See
documentation of `CMAKE_CFG_INTDIR` variable for more information.
libraries can be linked in via `target_link_libraries(plugin_name ...)`. On DLL
platforms, the plugin DLLs and metadata files are put into `<debug binary install dir>`
/`<release binary install dir>` and the `*.lib` files into `<debug library install dir>`
/`<release library install dir>`. On non-DLL platforms everything is put into
`<debug library install dir>`/`<release library install dir>`.

corrade_add_plugin(<plugin name>
<debug install dir>
<release install dir>
<metadata file>
<sources>...)

Unline the above version this puts everything into `<debug install dir>` on
both DLL and non-DLL platforms. If `<debug install dir>` is set to
`CMAKE_CURRENT_BINARY_DIR` (e.g. for testing purposes), the files are copied
directly, without the need to perform install step. Note that the files are
actually put into configuration-based subdirectory, i.e.
`${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}`. See documentation of
`CMAKE_CFG_INTDIR` variable for more information.

### Add static plugin
@anchor corrade-cmake-add-static-plugin

corrade_add_static_plugin(<plugin name>
"<binary install dir>;<library install dir>"
<metadata file>
<sources>...)

The macro adds preprocessor directive `CORRADE_STATIC_PLUGIN`. Additional
libraries can be linked in via `target_link_libraries(plugin_name ...)`. The
`<binary install dir>` is ignored and included just for compatibility with the
@ref corrade-cmake-add-plugin "corrade_add_plugin()" command, everything is
installed into `<library install dir>`. Note that plugins built in debug
configuration (e.g. with `CMAKE_BUILD_TYPE` set to `Debug`) have `-d` suffix to
make it possible to have both debug and release plugins installed alongside
each other.

corrade_add_static_plugin(<plugin name>
<install dir>
<metadata file>
<sources>...)

The macro adds preprocessor directive `CORRADE_STATIC_PLUGIN`. Additional
libraries can be linked in via `target_link_libraries(plugin_name ...)`. If
Equivalent to the above with `<library install dir>` set to `<install dir>`. If
`<install dir>` is set to `CMAKE_CURRENT_BINARY_DIR` (e.g. for testing
purposes), no installation rules are added.
@attention Plugins built in debug configuration (e.g. with `CMAKE_BUILD_TYPE`
set to `Debug`) have `-d` suffix to make it possible to have both debug and
release plugins installed alongside each other.

### Find corresponding DLLs for library files
@anchor corrade-find-dlls-for-libs
@anchor corrade-cmake-find-dlls-for-libs

corrade_find_dlls_for_libs(<output variable> <libs>...)

Expand Down
47 changes: 35 additions & 12 deletions modules/FindCorrade.cmake
Expand Up @@ -163,37 +163,60 @@
# Add dynamic plugin::
#
# corrade_add_plugin(<plugin name>
# <debug install dir> <release install dir>
# "<debug binary install dir>;<debug library install dir>"
# "<release binary install dir>;<release library install dir>"
# <metadata file>
# <sources>...)
#
# The macro adds preprocessor directive ``CORRADE_DYNAMIC_PLUGIN``. Additional
# libraries can be linked in via :command:`target_link_libraries(plugin_name ...) <target_link_libraries>`.
# If ``<debug install dir>`` is set to :variable:`CMAKE_CURRENT_BINARY_DIR`
# (e.g. for testing purposes), the files are copied directly, without the need
# to perform install step. Note that the files are actually put into
# configuration-based subdirectory, i.e. ``${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}``.
# See documentation of :variable:`CMAKE_CFG_INTDIR` variable for more
# information.
# On DLL platforms, the plugin DLLs and metadata files are put into
# ``<debug binary install dir>``/``<release binary install dir>`` and the
# ``*.lib`` files into ``<debug library install dir>``/``<release library install dir>``.
# On non-DLL platforms everything is put into ``<debug library install dir>``/
# ``<release library install dir>``.
#
# corrade_add_plugin(<plugin name>
# <debug install dir>
# <release install dir>
# <metadata file>
# <sources>...)
#
# Unline the above version this puts everything into ``<debug install dir>`` on
# both DLL and non-DLL platforms. If ``<debug install dir>`` is set to
# :variable:`CMAKE_CURRENT_BINARY_DIR` (e.g. for testing purposes), the files
# are copied directly, without the need to perform install step. Note that the
# files are actually put into configuration-based subdirectory, i.e.
# ``${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}``. See documentation of
# :variable:`CMAKE_CFG_INTDIR` variable for more information.
#
# .. command:: corrade_add_static_plugin
#
# Add static plugin::
#
# corrade_add_static_plugin(<plugin name>
# <install dir>
# "<binary install dir>;<library install dir>"
# <metadata file>
# <sources>...)
#
# The macro adds preprocessor directive ``CORRADE_STATIC_PLUGIN``. Additional
# libraries can be linked in via :command:`target_link_libraries(plugin_name ...) <target_link_libraries>`.
# The ``<binary install dir>`` is ignored and included just for compatibility
# with the :command:`corrade_add_plugin` command, everything is installed into
# ``<library install dir>``. Note that plugins built in debug configuration
# (e.g. with :variable:`CMAKE_BUILD_TYPE` set to ``Debug``) have ``"-d"``
# suffix to make it possible to have both debug and release plugins installed
# alongside each other.
#
# corrade_add_static_plugin(<plugin name>
# <install dir>
# <metadata file>
# <sources>...)
#
# Equivalent to the above with ``<library install dir>`` set to ``<install dir>``.
# If ``<install dir>`` is set to :variable:`CMAKE_CURRENT_BINARY_DIR` (e.g. for
# testing purposes), no installation rules are added.
#
# Note that plugins built in debug configuration (e.g. with :variable:`CMAKE_BUILD_TYPE`
# set to ``Debug``) have ``"-d"`` suffix to make it possible to have both debug
# and release plugins installed alongside each other.
#
# .. command:: corrade_find_dlls_for_libs
#
# Find corresponding DLLs for library files::
Expand Down
72 changes: 60 additions & 12 deletions modules/UseCorrade.cmake
Expand Up @@ -343,9 +343,38 @@ function(corrade_add_resource name configurationFile)
set(${name} "${out}" PARENT_SCOPE)
endfunction()

function(corrade_add_plugin plugin_name debug_install_dir release_install_dir metadata_file)
function(corrade_add_plugin plugin_name debug_install_dirs release_install_dirs metadata_file)
if(CORRADE_TARGET_NACL_NEWLIB OR CORRADE_TARGET_EMSCRIPTEN OR CORRADE_TARGET_WINDOWS_RT OR CORRADE_TARGET_IOS)
message(SEND_ERROR "Dynamic plugins are not available on this platform, use corrade_add_static_plugin() instead")
message(SEND_ERROR "corrade_add_plugin(): dynamic plugins are not available on this platform, use corrade_add_static_plugin() instead")
endif()

# Populate {debug,release}_{binary,library,conf}_install_dir variables
if(NOT debug_install_dirs STREQUAL CMAKE_CURRENT_BINARY_DIR)
list(LENGTH debug_install_dirs debug_install_dir_count)
list(LENGTH release_install_dirs release_install_dir_count)
if(NOT debug_install_dir_count EQUAL release_install_dir_count)
message(FATAL_ERROR "corrade_add_plugin(): either none or both install dirs must contain binary location")
elseif(debug_install_dir_count EQUAL 1)
set(debug_binary_install_dir ${debug_install_dirs})
set(debug_library_install_dir ${debug_install_dirs})
set(release_binary_install_dir ${release_install_dirs})
set(release_library_install_dir ${release_install_dirs})
elseif(debug_install_dir_count EQUAL 2)
list(GET debug_install_dirs 0 debug_binary_install_dir)
list(GET debug_install_dirs 1 debug_library_install_dir)
list(GET release_install_dirs 0 release_binary_install_dir)
list(GET release_install_dirs 1 release_library_install_dir)
else()
message(FATAL_ERROR "corrade_add_plugin(): install dirs must contain either just library location or both binary and library location")
endif()

if(CORRADE_TARGET_WINDOWS)
set(debug_conf_install_dir ${debug_binary_install_dir})
set(release_conf_install_dir ${release_binary_install_dir})
else()
set(debug_conf_install_dir ${debug_library_install_dir})
set(release_conf_install_dir ${release_library_install_dir})
endif()
endif()

# Create dynamic library and bring all needed options along
Expand All @@ -369,7 +398,7 @@ function(corrade_add_plugin plugin_name debug_install_dir release_install_dir me

# Copy metadata next to the binary for testing purposes or install it both
# somewhere
if(${debug_install_dir} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
if(debug_install_dirs STREQUAL CMAKE_CURRENT_BINARY_DIR)
add_custom_command(
OUTPUT ${plugin_name}.conf
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${metadata_file} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${plugin_name}.conf
Expand All @@ -382,20 +411,39 @@ function(corrade_add_plugin plugin_name debug_install_dir release_install_dir me
# Force IDEs display also the metadata file in project view
add_custom_target(${plugin_name}-metadata SOURCES ${metadata_file})

install(TARGETS ${plugin_name} DESTINATION "${debug_install_dir}"
CONFIGURATIONS Debug)
install(TARGETS ${plugin_name} DESTINATION "${release_install_dir}"
CONFIGURATIONS "" None Release RelWithDebInfo MinSizeRel)
install(FILES ${metadata_file} DESTINATION "${debug_install_dir}"
# CONFIGURATIONS must be first in order to not be ignored when having
# multiple destinations.
# https://gitlab.kitware.com/cmake/cmake/issues/16361
install(TARGETS ${plugin_name}
CONFIGURATIONS Debug
RUNTIME DESTINATION ${debug_binary_install_dir}
LIBRARY DESTINATION ${debug_library_install_dir}
ARCHIVE DESTINATION ${debug_library_install_dir})
install(TARGETS ${plugin_name}
CONFIGURATIONS "" None Release RelWithDebInfo MinSizeRel
RUNTIME DESTINATION ${release_binary_install_dir}
LIBRARY DESTINATION ${release_library_install_dir}
ARCHIVE DESTINATION ${release_library_install_dir})
install(FILES ${metadata_file} DESTINATION ${debug_conf_install_dir}
RENAME "${plugin_name}.conf"
CONFIGURATIONS Debug)
install(FILES ${metadata_file} DESTINATION "${release_install_dir}"
install(FILES ${metadata_file} DESTINATION ${release_conf_install_dir}
RENAME "${plugin_name}.conf"
CONFIGURATIONS "" None Release RelWithDebInfo MinSizeRel)
endif()
endfunction()

function(corrade_add_static_plugin plugin_name install_dir metadata_file)
function(corrade_add_static_plugin plugin_name install_dirs metadata_file)
# Populate library_install_dir variable
list(LENGTH install_dirs install_dir_count)
if(install_dir_count EQUAL 1)
set(library_install_dir ${install_dirs})
elseif(install_dir_count EQUAL 2)
list(GET install_dirs 1 library_install_dir)
else()
message(FATAL_ERROR "corrade_add_static_plugin(): install dir must contain either just library location or both library and binary location")
endif()

# Compile resources
set(resource_file "${CMAKE_CURRENT_BINARY_DIR}/resources_${plugin_name}.conf")
file(WRITE "${resource_file}" "group=CorradeStaticPlugin_${plugin_name}\n[file]\nfilename=\"${CMAKE_CURRENT_SOURCE_DIR}/${metadata_file}\"\nalias=${plugin_name}.conf")
Expand All @@ -410,8 +458,8 @@ function(corrade_add_static_plugin plugin_name install_dir metadata_file)
set_target_properties(${plugin_name} PROPERTIES DEBUG_POSTFIX "-d")

# Install, if not into the same place
if(NOT ${install_dir} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
install(TARGETS ${plugin_name} DESTINATION "${install_dir}")
if(NOT install_dirs STREQUAL CMAKE_CURRENT_BINARY_DIR)
install(TARGETS ${plugin_name} DESTINATION ${library_install_dir})
endif()
endfunction()

Expand Down

0 comments on commit 8c5dcb6

Please sign in to comment.