CalamaresAddLibrary.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #
  23. # Usage:
  24. #
  25. # calamares_add_library(
  26. # library-name
  27. # EXPORT_MACRO macro-name
  28. # TARGET_TYPE <STATIC|MODULE|...>
  29. # EXPORT export-name
  30. # VERSION version
  31. # SOVERSION version
  32. # INSTALL_BINDIR dir
  33. # RESOURCES resource-file
  34. # SOURCES source-file...
  35. # UI ui-file...
  36. # LINK_LIBRARIES lib...
  37. # LINK_PRIVATE_LIBRARIES lib...
  38. # COMPILE_DEFINITIONS def...
  39. # [NO_INSTALL]
  40. # [NO_VERSION]
  41. # )
  42. #
  43. # The COMPILE_DEFINITIONS are set on the resulting module with a suitable
  44. # flag (i.e. `-D`) so only state the name (optionally, also the value)
  45. # without a `-D` prefixed to it. Pass in a CMake list as needed.
  46. include( CMakeParseArguments )
  47. include( CalamaresAutomoc )
  48. function(calamares_add_library)
  49. # parse arguments (name needs to be saved before passing ARGN into the macro)
  50. set(NAME ${ARGV0})
  51. set(options NO_INSTALL NO_VERSION)
  52. set(oneValueArgs NAME EXPORT_MACRO TARGET_TYPE EXPORT VERSION SOVERSION INSTALL_BINDIR RESOURCES)
  53. set(multiValueArgs SOURCES UI LINK_LIBRARIES LINK_PRIVATE_LIBRARIES COMPILE_DEFINITIONS)
  54. cmake_parse_arguments(LIBRARY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  55. set(LIBRARY_NAME ${NAME})
  56. set(target ${LIBRARY_NAME})
  57. # qt stuff
  58. include_directories(${CMAKE_CURRENT_LIST_DIR})
  59. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  60. # add resources from current dir
  61. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${LIBRARY_RESOURCES}")
  62. qt5_add_resources(LIBRARY_RC_SOURCES "${LIBRARY_RESOURCES}")
  63. list(APPEND LIBRARY_SOURCES ${LIBRARY_RC_SOURCES})
  64. unset(LIBRARY_RC_SOURCES)
  65. endif()
  66. # add target
  67. if(LIBRARY_TARGET_TYPE STREQUAL "STATIC")
  68. add_library(${target} STATIC ${LIBRARY_SOURCES})
  69. elseif(LIBRARY_TARGET_TYPE STREQUAL "MODULE")
  70. add_library(${target} MODULE ${LIBRARY_SOURCES})
  71. else() # default
  72. add_library(${target} SHARED ${LIBRARY_SOURCES})
  73. endif()
  74. calamares_automoc(${target})
  75. if(LIBRARY_UI)
  76. calamares_autouic(${target} ${LIBRARY_UI})
  77. endif()
  78. if(LIBRARY_EXPORT_MACRO)
  79. set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS ${LIBRARY_EXPORT_MACRO})
  80. endif()
  81. if(LIBRARY_COMPILE_DEFINITIONS)
  82. set( _lib_definitions "${LIBRARY_EXPORT_MACRO}" ${LIBRARY_COMPILE_DEFINITIONS} )
  83. set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS "${_lib_definitions}")
  84. endif()
  85. # add link targets
  86. target_link_libraries(${target}
  87. LINK_PUBLIC ${CALAMARES_LIBRARIES}
  88. Qt5::Core
  89. Qt5::Gui
  90. Qt5::Widgets
  91. ${LIBRARY_QT5_MODULES}
  92. )
  93. if(LIBRARY_LINK_LIBRARIES)
  94. target_link_libraries(${target} LINK_PUBLIC ${LIBRARY_LINK_LIBRARIES})
  95. endif()
  96. if(LIBRARY_LINK_PRIVATE_LIBRARIES)
  97. target_link_libraries(${target} LINK_PRIVATE ${LIBRARY_LINK_PRIVATE_LIBRARIES})
  98. endif()
  99. # add soversion
  100. if(NOT LIBRARY_NO_VERSION)
  101. set_target_properties(${target} PROPERTIES VERSION ${LIBRARY_VERSION})
  102. if(NOT LIBRARY_SOVERSION)
  103. set(LIBRARY_SOVERSION ${LIBRARY_VERSION})
  104. endif()
  105. set_target_properties(${target} PROPERTIES SOVERSION ${LIBRARY_SOVERSION})
  106. endif()
  107. if(NOT LIBRARY_INSTALL_BINDIR)
  108. set(LIBRARY_INSTALL_BINDIR "${CMAKE_INSTALL_BINDIR}")
  109. set(LIBRARY_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
  110. else()
  111. set(LIBRARY_INSTALL_LIBDIR "${LIBRARY_INSTALL_BINDIR}")
  112. endif()
  113. # make installation optional, maybe useful for dummy plugins one day
  114. if(NOT LIBRARY_NO_INSTALL)
  115. include(GNUInstallDirs)
  116. if(NOT LIBRARY_EXPORT)
  117. install( TARGETS ${target}
  118. RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
  119. LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
  120. ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
  121. )
  122. else()
  123. install( TARGETS ${target}
  124. EXPORT ${LIBRARY_EXPORT}
  125. RUNTIME DESTINATION ${LIBRARY_INSTALL_BINDIR}
  126. LIBRARY DESTINATION ${LIBRARY_INSTALL_LIBDIR}
  127. ARCHIVE DESTINATION ${LIBRARY_INSTALL_LIBDIR}
  128. )
  129. endif()
  130. endif()
  131. endfunction()