source: calamares/trunk/fuentes/CMakeModules/CalamaresAddLibrary.cmake @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 5.3 KB
Line 
1# === This file is part of Calamares - <https://github.com/calamares> ===
2#
3#   Calamares is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7#
8#   Calamares is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11#   GNU General Public License for more details.
12#
13#   You should have received a copy of the GNU General Public License
14#   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
15#
16#   SPDX-License-Identifier: GPL-3.0+
17#   License-Filename: LICENSE
18#
19###
20#
21# Support functions for building plugins.
22
23include( CMakeParseArguments )
24
25function(calamares_add_library)
26    # parse arguments (name needs to be saved before passing ARGN into the macro)
27    set(NAME ${ARGV0})
28    set(options NO_INSTALL NO_VERSION)
29    set(oneValueArgs NAME TYPE EXPORT_MACRO TARGET TARGET_TYPE EXPORT VERSION SOVERSION INSTALL_BINDIR RESOURCES)
30    set(multiValueArgs SOURCES UI LINK_LIBRARIES LINK_PRIVATE_LIBRARIES COMPILE_DEFINITIONS QT5_MODULES)
31    cmake_parse_arguments(LIBRARY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
32    set(LIBRARY_NAME ${NAME})
33
34
35#    message("*** Arguments for ${LIBRARY_NAME}")
36#    message("Sources: ${LIBRARY_SOURCES}")
37#    message("Link libraries: ${LIBRARY_LINK_LIBRARIES}")
38#    message("UI: ${LIBRARY_UI}")
39#    message("TARGET_TYPE: ${LIBRARY_TARGET_TYPE}")
40#    message("EXPORT_MACRO: ${LIBRARY_EXPORT_MACRO}")
41#    message("NO_INSTALL: ${LIBRARY_NO_INSTALL}")
42
43    set(target ${LIBRARY_NAME})
44
45    # qt stuff
46    include_directories(${CMAKE_CURRENT_LIST_DIR})
47    include_directories(${CMAKE_CURRENT_BINARY_DIR})
48
49    if(LIBRARY_UI)
50        qt5_wrap_ui(LIBRARY_UI_SOURCES ${LIBRARY_UI})
51        list(APPEND LIBRARY_SOURCES ${LIBRARY_UI_SOURCES})
52    endif()
53
54    # add resources from current dir
55    if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${LIBRARY_RESOURCES}")
56        qt5_add_resources(LIBRARY_RC_SOURCES "${LIBRARY_RESOURCES}")
57        list(APPEND LIBRARY_SOURCES ${LIBRARY_RC_SOURCES})
58        unset(LIBRARY_RC_SOURCES)
59    endif()
60
61    # add target
62    if(LIBRARY_TARGET_TYPE STREQUAL "STATIC")
63        add_library(${target} STATIC ${LIBRARY_SOURCES})
64    elseif(LIBRARY_TARGET_TYPE STREQUAL "MODULE")
65        add_library(${target} MODULE ${LIBRARY_SOURCES})
66    else() # default
67        add_library(${target} SHARED ${LIBRARY_SOURCES})
68    endif()
69
70    # definitions - can this be moved into set_target_properties below?
71    add_definitions(${QT_DEFINITIONS})
72    set_target_properties(${target} PROPERTIES AUTOMOC TRUE)
73
74    if(LIBRARY_EXPORT_MACRO)
75        set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS ${LIBRARY_EXPORT_MACRO})
76    endif()
77
78    if(LIBRARY_COMPILE_DEFINITIONS)
79        # Dear CMake, i hate you! Sincerely, domme
80        # At least in CMake 2.8.8, you CANNOT set more than one COMPILE_DEFINITIONS value
81        # only takes the first one if called multiple times or bails out with wrong number of arguments
82        # when passing in a list, thus i redefine the export macro here in hope it won't mess up other targets
83        add_definitions( "-D${LIBRARY_EXPORT_MACRO}" )
84
85        set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS ${LIBRARY_COMPILE_DEFINITIONS})
86    endif()
87
88    # add link targets
89    target_link_libraries(${target}
90        LINK_PUBLIC ${CALAMARES_LIBRARIES}
91        Qt5::Core
92        Qt5::Gui
93        Qt5::Widgets
94        ${LIBRARY_QT5_MODULES}
95    )
96    if(LIBRARY_LINK_LIBRARIES)
97        target_link_libraries(${target} LINK_PUBLIC ${LIBRARY_LINK_LIBRARIES})
98    endif()
99    if(LIBRARY_LINK_PRIVATE_LIBRARIES)
100        target_link_libraries(${target} LINK_PRIVATE ${LIBRARY_LINK_PRIVATE_LIBRARIES})
101    endif()
102
103    # add soversion
104    if(NOT LIBRARY_NO_VERSION)
105        set_target_properties(${target} PROPERTIES VERSION ${LIBRARY_VERSION})
106
107        if(NOT LIBRARY_SOVERSION)
108            set(LIBRARY_SOVERSION ${LIBRARY_VERSION})
109        endif()
110
111        set_target_properties(${target} PROPERTIES SOVERSION ${LIBRARY_SOVERSION})
112    endif()
113
114
115    if(NOT LIBRARY_INSTALL_BINDIR)
116        set(LIBRARY_INSTALL_BINDIR "${CMAKE_INSTALL_BINDIR}")
117        set(LIBRARY_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
118    else()
119        set(LIBRARY_INSTALL_LIBDIR "${LIBRARY_INSTALL_BINDIR}")
120    endif()
121
122    #message("INSTALL_BINDIR: ${LIBRARY_INSTALL_BINDIR}")
123    #message("INSTALL_LIBDIR: ${LIBRARY_INSTALL_LIBDIR}")
124
125    # make installation optional, maybe useful for dummy plugins one day
126    if(NOT LIBRARY_NO_INSTALL)
127        include(GNUInstallDirs)
128        if(NOT LIBRARY_EXPORT)
129            install( TARGETS ${target}
130                RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
131                LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
132                ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
133            )
134        else()
135            install( TARGETS ${target}
136                EXPORT ${LIBRARY_EXPORT}
137                RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
138                LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
139                ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
140            )
141        endif()
142    endif()
143endfunction()
Note: See TracBrowser for help on using the repository browser.