summaryrefslogtreecommitdiffstats
path: root/Modules/GenerateExportHeader.cmake
blob: 1578365d134f7153e5479b6ef0f67e96f72a5a03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

# - Function for generation of export macros for libraries
#
# This module provides the function GENERATE_EXPORT_HEADER() and the
# accompanying ADD_COMPILER_EXPORT_FLAGS() function.
#
# The GENERATE_EXPORT_HEADER function can be used to generate a file suitable
# for preprocessor inclusion which contains EXPORT macros to be used in
# library classes.
#
# GENERATE_EXPORT_HEADER( LIBRARY_TARGET
#             [BASE_NAME <base_name>]
#             [EXPORT_MACRO_NAME <export_macro_name>]
#             [EXPORT_FILE_NAME <export_file_name>]
#             [DEPRECATED_NAME <deprecated_name>]
#             [NO_EXPORT_MACRO_NAME <no_export_macro_name>]
#             [STATIC_DEFINE <static_define>]
# )
#
# ADD_COMPILER_EXPORT_FLAGS( [FATAL_WARNINGS] )
#
# By default GENERATE_EXPORT_HEADER() generates macro names in a file name
# determined by the name of the library. The ADD_COMPILER_EXPORT_FLAGS macro adds
# -fvisibility=hidden to CMAKE_CXX_FLAGS if supported, and is a no-op on Windows
# which does not need extra compiler flags for exporting support.
#
# This means that in the simplest case, users of these functions will be equivalent to:
#
#   add_compiler_export_flags()
#
#   add_library(somelib someclass.cpp)
#
#   generate_export_header(somelib)
#
#   install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
#
#   install(FILES
#    someclass.h
#    ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
#   )
#
# And in the ABI header files:
#
#   \code
#   #include "somelib_export.h"
#
#   class SOMELIB_EXPORT SomeClass {
#
#   };
#   \endcode
#
# The CMake fragment will generate a file in the ${CMAKE_CURRENT_BUILD_DIR} called
# somelib_export.h containing the macros SOMELIB_EXPORT, SOMELIB_NO_EXPORT,
# SOMELIB_DEPRECATED, SOMELIB_DEPRECATED_EXPORT and SOMELIB_DEPRECATED_NO_EXPORT.
# The resulting file should be installed with other headers in the library.
#
# The BASE_NAME argument can be used to override the file name and the names
# used for the macros
#
#   add_library(somelib someclass.cpp)
#   generate_export_header(somelib
#     BASE_NAME other_name
#   )
#
# Generates a file called other_name_export.h containing the macros
# OTHER_NAME_EXPORT, OTHER_NAME_NO_EXPORT and OTHER_NAME_DEPRECATED etc.
#
# The BASE_NAME may be overridden by specifiying other options in the function.
# For example:
#
#   add_library(somelib someclass.cpp)
#   generate_export_header(somelib
#     EXPORT_MACRO_NAME OTHER_NAME_EXPORT
#   )
#
# creates the macro OTHER_NAME_EXPORT instead of SOMELIB_EXPORT, but other macros
# and the generated file name is as default.
#
#   add_library(somelib someclass.cpp)
#   generate_export_header(somelib
#     DEPRECATED_NAME KDE_DEPRECATED
#   )
#
# creates the macro KDE_DEPRECATED instead of SOMELIB_DEPRECATED.
#
# If LIBRARY_TARGET is a static library, macros are defined without values.
#
# If the same sources are used to create both a shared and a static library, the
# uppercased symbol ${BASE_NAME}_STATIC_DEFINE should be used when building the
# static library
#
#   add_library(shared_variant SHARED ${lib_SRCS})
#   add_library(static_variant ${lib_SRCS})
#
#   generate_export_header(shared_variant BASE_NAME libshared_and_static)
#
#   set_target_properties(static_variant PROPERTIES COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
#
# This will cause the export macros to expand to nothing when building the static library.


#=============================================================================
# Copyright 2011 Stephen Kelly <steveire@gmail.com>
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)

include(CMakeParseArguments)
include(CheckCXXCompilerFlag)

macro(_test_compiler_hidden_visibility)

  if (CMAKE_COMPILER_IS_GNUCXX)
    exec_program(${CMAKE_C_COMPILER} ARGS --version OUTPUT_VARIABLE     _gcc_version_info)
    string (REGEX MATCH "[345]\\.[0-9]\\.[0-9]" _gcc_version "${_gcc_version_info}")
    # gcc on mac just reports: "gcc (GCC) 3.3 20030304 ..." without the
    # patch level, handle this here:
    if(NOT _gcc_version)
      string (REGEX REPLACE ".*\\(GCC\\).* ([34]\\.[0-9]) .*" "\\1.0" _gcc_version "${_gcc_version_info}")
    endif()

    if(${_gcc_version} VERSION_LESS "4.2")
      set(GCC_TOO_OLD TRUE)
      message(WARNING "GCC version older than 4.2")
    endif()
  endif()

  if (NOT GCC_TOO_OLD)
    check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
    check_cxx_compiler_flag(-fvisibility-inlines-hidden COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
    option(USE_COMPILER_HIDDEN_VISIBILITY "Use HIDDEN visibility support if available." ON)
    mark_as_advanced(USE_COMPILER_HIDDEN_VISIBILITY)
  endif()
endmacro()

set(myDir ${CMAKE_CURRENT_LIST_DIR})

macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
  set(DEFINE_DEPRECATED)
  set(DEFINE_EXPORT)
  set(DEFINE_IMPORT)
  set(DEFINE_NO_EXPORT)

  if(WIN32)
    if (${CMAKE_CXX_COMPILER_ID} MATCHES Borland)
      set(DEFINE_DEPRECATED "__declspec(deprecated)")
    endif()
  else()
    set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
  endif()

  get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)

  if(NOT ${type} STREQUAL "STATIC_LIBRARY")
    if(WIN32)
      if (NOT (${CMAKE_CXX_COMPILER_ID} MATCHES Borland))
        set(DEFINE_EXPORT "__declspec(dllexport)")
        set(DEFINE_IMPORT "__declspec(dllimport)")
      endif()
    elseif(COMPILER_HAS_HIDDEN_VISIBILITY AND USE_COMPILER_HIDDEN_VISIBILITY)
      set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
      set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
      set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
    endif()
  endif()
endmacro()

macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  # Option overrides
  set(options)
  set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME DEPRECATED_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE)
  set(multiValueArgs)

  cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

  set(BASE_NAME "${TARGET_LIBRARY}")

  if(_GEH_BASE_NAME)
    set(BASE_NAME ${_GEH_BASE_NAME})
  endif()

  string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
  string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)

  # Default options
  set(EXPORT_MACRO_NAME "${PREFIX}${BASE_NAME_UPPER}_EXPORT")
  set(NO_EXPORT_MACRO_NAME "${PREFIX}${BASE_NAME_UPPER}_NO_EXPORT")
  set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
  set(DEPRECATED_NAME "${PREFIX}${BASE_NAME_UPPER}_DEPRECATED")
  set(STATIC_DEFINE "${PREFIX}${BASE_NAME_UPPER}_STATIC_DEFINE")

  if(_GEH_UNPARSED_ARGUMENTS)
    message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
  endif()

  if(_GEH_EXPORT_MACRO_NAME)
    set(EXPORT_MACRO_NAME ${PREFIX}${_GEH_EXPORT_MACRO_NAME})
  endif()
  if(_GEH_EXPORT_FILE_NAME)
    if(IS_ABSOLUTE _GEH_EXPORT_FILE_NAME)
      set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
    else()
      set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
    endif()
  endif()
  if(_GEH_DEPRECATED_NAME)
    set(DEPRECATED_NAME ${PREFIX}${_GEH_DEPRECATED_NAME})
  endif()
  if(_GEH_NO_EXPORT_MACRO_NAME)
    set(NO_EXPORT_MACRO_NAME ${PREFIX}${_GEH_NO_EXPORT_MACRO_NAME})
  endif()
  if(_GEH_STATIC_DEFINE)
    set(STATIC_DEFINE ${PREFIX}${_GEH_STATIC_DEFINE})
  endif()

  set(INCLUDE_GUARD_NAME "${PREFIX}${EXPORT_MACRO_NAME}_H")

  get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)

  if (NOT EXPORT_IMPORT_CONDITION)
    set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
  endif()

  configure_file(${myDir}/exportheader.cmake.in ${EXPORT_FILE_NAME} @ONLY)
endmacro()

function(GENERATE_EXPORT_HEADER TARGET_LIBRARY)
  get_property(type TARGET ${TARGET_LIBRARY} PROPERTY TYPE)
  if(${type} STREQUAL "MODULE")
    message(WARNING "This macro should not be used with libraries of type MODULE")
    return()
  endif()
  if(NOT ${type} STREQUAL "STATIC_LIBRARY" AND NOT ${type} STREQUAL "SHARED_LIBRARY")
    message(WARNING "This macro can only be used with libraries")
    return()
  endif()
  _test_compiler_hidden_visibility()
  _do_set_macro_values(${TARGET_LIBRARY})
  _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
endfunction()

function(add_compiler_export_flags)

  _test_compiler_hidden_visibility()

  if(NOT (USE_COMPILER_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_VISIBILITY) OR MINGW)
    message(WARNING "Compiler doesn't have hidden visibility")
    return()
  endif()

  set (EXTRA_FLAGS "-fvisibility=hidden")

  if(COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
    set (EXTRA_FLAGS "${EXTRA_FLAGS} -fvisibility-inlines-hidden")
  endif()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}" PARENT_SCOPE)
endfunction()