view cmake/function/MalikaniaDefineLibrary.cmake @ 111:8963c68f023c

CMake: output everything in bin, closes #691
author David Demelier <markand@malikania.fr>
date Tue, 05 Sep 2017 14:24:14 +0200
parents 119bcc5a727e
children 48f7e7277ab6
line wrap: on
line source

#
# MalikaniaDefineLibrary.cmake -- CMake build system for malikania
#
# Copyright (c) 2013-2017 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

#
# malikania_define_library
# ------------------------
#
# malikania_define_library(
#    TARGET            The target name
#    SOURCES           The sources
#    ASSETS            (Optional) Additional assets files
#    FLAGS             (Optional) List of flags
#    PRIVATE_INCLUDES  (Optional) List of includes only for building the library
#    PUBLIC_INCLUDES   (Optional) List of public includes to share with the library users
#    LIBRARIES         (Optional) List of libraries to link against
# )
#
# Create a shared library. Follow the same specification as malikania_define_executable.
# However, additional PRIVATE_INCLUDES and PUBLIC_INCLUDES are available.
#

include(CMakeParseArguments)

include(${CMAKE_CURRENT_LIST_DIR}/MalikaniaBuildAssets.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/MalikaniaVeraCheck.cmake)

function(malikania_define_library)
    set(singleArgs TARGET)
    set(multiArgs ASSETS SOURCES FLAGS PRIVATE_INCLUDES PUBLIC_INCLUDES LIBRARIES)
    set(mandatory TARGET SOURCES)

    cmake_parse_arguments(LIB "" "${singleArgs}" "${multiArgs}" ${ARGN})

    if (NOT LIB_TARGET)
        message(FATAL_ERROR "Missing TARGET parameter")
    endif ()
    if (NOT LIB_SOURCES)
        message(FATAL_ERROR "Missing SOURCES parameter")
    endif ()

    # Enable assets for libraries.
    malikania_build_assets("${LIB_ASSETS}" assets)

    # Create the shared library.
    add_library(${LIB_TARGET} SHARED ${LIB_SOURCES} ${assets})
    target_link_libraries(${LIB_TARGET} ${LIB_LIBRARIES})
    target_include_directories(
        ${LIB_TARGET}
        PRIVATE
            ${CMAKE_CURRENT_BINARY_DIR}/assets
            ${LIB_PRIVATE_INCLUDES}
        PUBLIC
            ${CMAKE_CURRENT_SOURCE_DIR}
            ${LIB_PUBLIC_INCLUDES}
    )
    target_compile_definitions(${LIB_TARGET} PRIVATE ${LIB_FLAGS})

    #
    # Move the library into fakeroot/bin/ directory for Windows and other
    # DLL platforms so executables can be ran directly.
    #
    set_target_properties(
        ${LIB_TARGET}
        PROPERTIES
            PREFIX ""
            RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
    )
    foreach (c ${CMAKE_CONFIGURATION_TYPES})
        string(TOUPPER ${c} cu)
        set_target_properties(
            ${LIB_TARGET}
            PROPERTIES
                RUNTIME_OUTPUT_DIRECTORY_${cu} ${CMAKE_BINARY_DIR}/bin/${c}
        )
    endforeach ()

    malikania_vera_check(${LIB_TARGET} "${LIB_SOURCES}")
endfunction()