Add credits for Marek
[trojita.git] / cmake / TrojitaOption.cmake
blobed99e952a6d5e2842dd00df55a7619cb63e9c202
1 # Based on CMakeDependentOption.cmake
2 #=============================================================================
3 # Copyright 2006-2009 Kitware, Inc.
4 # Copyright 2013 Pali Rohár <pali.rohar@gmail.com>
6 # Distributed under the OSI-approved BSD License (the "License");
7 # see accompanying file Copyright.txt for details.
9 # This software is distributed WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # See the License for more information.
12 #=============================================================================
13 # (To distribute this file outside of CMake, substitute the full
14 #  License text for the above reference.)
16 include(FeatureSummary)
18 # trojita_string_option(option description default depends force [options...])
19 macro(trojita_string_option option description default depends force)
20     if(${option}_ISSET MATCHES "^${option}_ISSET$")
21         set(${option}_AVAILABLE 1)
22         foreach(d ${depends})
23             string(REGEX REPLACE " +" ";" CMAKE_DEPENDENT_OPTION_DEP "${d}")
24             if(NOT (${CMAKE_DEPENDENT_OPTION_DEP}))
25                 set(${option}_AVAILABLE 0)
26                 message(STATUS "Disabling ${option} because of ${CMAKE_DEPENDENT_OPTION_DEP}")
27             endif()
28         endforeach()
29         if(${option}_AVAILABLE)
30             if(${option} MATCHES "^${option}$")
31                 set(${option} "${default}" CACHE STRING "${description}" FORCE)
32             else()
33                 set(${option} "${${option}}" CACHE STRING "${description}" FORCE)
34             endif()
35             set_property(CACHE ${option} PROPERTY STRINGS ${ARGN})
36         else()
37             if(NOT ${option} MATCHES "^${option}$")
38                 set(${option} "${${option}}" CACHE INTERNAL "${description}")
39             endif()
40             set(${option} ${force})
41         endif()
42     else()
43         set(${option} "${${option}_ISSET}")
44     endif()
45 endmacro()
47 # trojita_option(option description default [depends])
48 macro(trojita_option option description default)
49     set(depends ${ARGN})
50     trojita_string_option(${option} "${description}" ${default} "${depends}" OFF AUTO ON OFF)
51     add_feature_info(${option} ${option} "${description}")
52 endmacro()
54 # trojita_plugin_option(option description [depends] [STATIC])
55 macro(trojita_plugin_option option description)
56     set(depends)
57     set(default "AUTO")
58     foreach(arg ${ARGN})
59         if("${arg}" STREQUAL "STATIC")
60             set(default "STATIC")
61         else()
62             list(APPEND depends "${arg}")
63         endif()
64     endforeach()
65     trojita_string_option(${option} "${description}" ${default} "${depends}" OFF AUTO STATIC ON OFF)
66     if(NOT WITH_SHARED_PLUGINS)
67         if("${${option}}" STREQUAL "AUTO")
68             if(NOT ${option} MATCHES "^${option}$")
69                 set(${option} "${${option}}" CACHE INTERNAL "${description}")
70             endif()
71             set(${option} "STATIC")
72         elseif(NOT "${${option}}" STREQUAL "STATIC")
73             message(STATUS "Disabling ${option} because of NOT WITH_SHARED_PLUGINS")
74             if("${${option}}" STREQUAL "ON")
75                 message(FATAL_ERROR "Plugin ${option} is disabled")
76             endif()
77             if(NOT ${option} MATCHES "^${option}$")
78                 set(${option} "${${option}}" CACHE INTERNAL "${description}")
79             endif()
80             set(${option} OFF)
81         endif()
82     endif()
83     add_feature_info(${option} ${option} "${description}")
84 endmacro()
86 # trojita_add_plugin(target type [sources...])
87 macro(trojita_add_plugin target type)
88     if("${${type}}" STREQUAL "STATIC")
89         message(STATUS "Building static plugin ${target}")
90         add_library(${target} STATIC ${ARGN})
91         set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS QT_STATICPLUGIN)
92         set_property(GLOBAL APPEND PROPERTY TROJITA_STATIC_PLUGINS ${target})
93     else()
94         message(STATUS "Building shared plugin ${target}")
95         add_library(${target} MODULE ${ARGN})
96         install(TARGETS ${target} DESTINATION ${CMAKE_INSTALL_PLUGIN_DIR})
97         set_property(GLOBAL APPEND PROPERTY TROJITA_SHARED_PLUGINS ${target})
98     endif()
99     set_target_properties(${target} PROPERTIES PREFIX "")
100     set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS BUILD_PLUGIN)
101     target_link_libraries(${target} Plugins)
102     if (WITH_QT5)
103         qt5_use_modules(${target} Core)
104     else()
105         target_link_libraries(${target} ${QT_QTCORE_LIBRARY})
106     endif()
107 endmacro()
109 # trojita_find_package(package version url description purpose [options...])
110 macro(trojita_find_package package version url description purpose)
111     set(type OPTIONAL)
112     foreach(arg ${ARGN})
113         if("${${arg}}" STREQUAL "ON" OR "${arg}" STREQUAL REQUIRED)
114             message(STATUS "Package ${package} is required because of ${arg}")
115             set(type REQUIRED)
116         endif()
117     endforeach()
118     if ("${type}" STREQUAL REQUIRED)
119         find_package(${package} ${version} REQUIRED)
120     else()
121         find_package(${package} ${version})
122     endif()
123     set_package_properties(${package} PROPERTIES URL "${url}" DESCRIPTION "${description}" TYPE ${type} PURPOSE "${purpose}")
124     if(NOT ${package}_FOUND)
125         foreach(arg ${ARGN})
126             if("${${arg}}" STREQUAL "AUTO")
127                 message(STATUS "Disabling ${arg} because package ${package} was not found")
128                 if(NOT ${arg} MATCHES "^${arg}$")
129                     set(${arg} "${${arg}}" CACHE INTERNAL "")
130                 endif()
131                 set(${arg} OFF)
132                 get_property(features GLOBAL PROPERTY ENABLED_FEATURES)
133                 list(REMOVE_ITEM features ${arg})
134                 set_property(GLOBAL PROPERTY ENABLED_FEATURES "${features}")
135                 get_property(features GLOBAL PROPERTY DISABLED_FEATURES)
136                 list(FIND features ${arg} id)
137                 if(id EQUAL -1)
138                     set_property(GLOBAL APPEND PROPERTY DISABLED_FEATURES "${arg}")
139                 endif()
140             endif()
141         endforeach()
142     endif()
143 endmacro()