test: Bundle GTest/GMock 1.8.1 sources and provide a find script
[quassel.git] / cmake / QuasselMacros.cmake
index e282506..3af589a 100644 (file)
@@ -7,22 +7,37 @@
 ###################################################################################################
 
 include(CMakeParseArguments)
+include(GenerateExportHeader)
 include(QuasselCompileFeatures)
 
 ###################################################################################################
 # Adds a library target for a Quassel module.
 #
-# It expects the (CamelCased) module name as a parameter, and derives various
+# quassel_add_module(Module [STATIC] [EXPORT])
+#
+# The function expects the (CamelCased) module name as a parameter, and derives various
 # strings from it. For example, quassel_add_module(Client) produces
 #  - a library target named quassel_client with output name (lib)quassel-client(.so)
 #  - an alias target named Quassel::Client in global scope
 #
+# If the optional argument STATIC is given, a static library is built; otherwise, on
+# platforms other than Windows, a shared library is created. For shared libraries, also
+# an install rule is added.
+#
+# To generate an export header for the library, specify EXPORT. The header will be named
+# ${module}-export.h (where ${module} is the lower-case name of the module).
+#
 # The function exports the TARGET variable which can be used in the current scope
 # for setting source files, properties, link dependencies and so on.
 # To refer to the target outside of the current scope, e.g. for linking, use
 # the alias name.
 #
 function(quassel_add_module _module)
+    set(options EXPORT STATIC)
+    set(oneValueArgs )
+    set(multiValueArgs )
+    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+
     # Derive target, alias target, output name from the given module name
     set(alias "Quassel::${_module}")
     set(target ${alias})
@@ -30,9 +45,7 @@ function(quassel_add_module _module)
     string(REPLACE "::" "_" target ${target})
     string(REPLACE "_" "-" output_name ${target})
 
-    # On Windows, building shared libraries requires export headers.
-    # Let's bother with that later.
-    if (WIN32)
+    if (ARG_STATIC)
         set(buildmode STATIC)
     else()
         set(buildmode SHARED)
@@ -54,7 +67,23 @@ function(quassel_add_module _module)
     )
 
     if (buildmode STREQUAL "SHARED")
-        install(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_LIBDIR})
+        install(TARGETS ${target}
+            RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+            LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+            ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        )
+    endif()
+
+    if (ARG_EXPORT)
+        string(TOLOWER ${_module} lower_module)
+        string(TOUPPER ${_module} upper_module)
+        string(REPLACE "::" "-" header_base ${lower_module})
+        string(REPLACE "::" "_" macro_base ${upper_module})
+        generate_export_header(${target}
+            BASE_NAME ${macro_base}
+            EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/export/${header_base}-export.h
+        )
+        target_include_directories(${target} PUBLIC ${CMAKE_BINARY_DIR}/export)
     endif()
 
     # Export the target name for further use
@@ -129,8 +158,9 @@ function(quassel_add_resource _name)
         WORKING_DIRECTORY ${basedir}
     )
 
-    # Generate library target that can be referenced elsewhere
-    quassel_add_module(Resource::${_name})
+    # Generate library target that can be referenced elsewhere. Force static, because
+    # we can't easily export symbols from the generated sources.
+    quassel_add_module(Resource::${_name} STATIC)
     target_sources(${TARGET} PRIVATE ${qrc_srcpath})
     set_target_properties(${TARGET} PROPERTIES AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)