plugin.cmake 8.46 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
# This module provides the following macro:
#
# openflipper_plugin ( [DIRS dir1 dir2 ...]
#                      [DEPS dep1 dep2 ...]
#                      [OPTDEPS dep1 dep2 ...]
#                      [LDFLAGSADD flag1 flag2 ...]
#                      [CFLAGSADD flag1 flag2 ...]
#                      [LIBRARIES lib1 lib2 ...]
#                      [LIBDIRS dir1 dir2 ...]
10
11
#                      [INCDIRS dir1 dir2 ...]
#                      [ADDSRC file1 file2 ...])
Jan Möbius's avatar
Jan Möbius committed
12
13
14
15
16
17
18
19
20
#
# DIRS       = additional directories with source files
# DEPS       = required dependencies for find_package macro
# OPTDEPS    = optional dependencies for find_package macro
# LDFLAGSADD = flags added to the link command
# CFLAGSADD  = flags added to the compile command
# LIBRARIES  = libraries added to link command
# LIBDIRS    = additional link directories
# INCDIRS    = additional include directories
21
# ADDSRC     = additional source files
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
22
#
Jan Möbius's avatar
   
Jan Möbius committed
23
# Plugins can implement a acg_list_filter ("listname") macro to filter out
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
24
25
26
# unneeded headers, sources and ui files from the autogenerated 
# build sources lists (see common.cmake drop_templates macro)
#
Jan Möbius's avatar
Jan Möbius committed
27

Jan Möbius's avatar
   
Jan Möbius committed
28
include (ACGCommon)
Jan Möbius's avatar
Jan Möbius committed
29
30
31
32
33
34
35
36
37
38

# get plugin name from directory name
macro (_get_plugin_name var)
  string (REGEX MATCH "Plugin-.+[/\\]?$" _dir ${CMAKE_CURRENT_SOURCE_DIR})
  string (REPLACE "Plugin-" "" ${var} ${_dir})
endmacro ()

# parse plugin macro parameter
macro (_get_plugin_parameters _prefix)
    set (_current_var _foo)
39
    set (_supported_var DIRS DEPS OPTDEPS LDFLAGSADD CFLAGSADD LIBRARIES LIBDIRS INCDIRS ADDSRC)
Jan Möbius's avatar
Jan Möbius committed
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
    foreach (_val ${_supported_var})
        set (${_prefix}_${_val})
    endforeach ()
    foreach (_val ${ARGN})
        set (_found FALSE)
        foreach (_find ${_supported_var})
            if ("${_find}" STREQUAL "${_val}")
                set (_found TRUE)
            endif ()
        endforeach ()
        
        if (_found)
            set (_current_var ${_prefix}_${_val})
        else ()
            list (APPEND ${_current_var} ${_val})
        endif ()
    endforeach ()
endmacro ()

# check dependencies
macro (_check_plugin_deps _prefix)
    set (${_prefix}_HAS_DEPS TRUE)
    foreach (_val ${ARGN})
        string (TOUPPER ${_val} _VAL)

        find_package(${_val})

        if (${_val}_FOUND OR ${_VAL}_FOUND)
          foreach (_name ${_val} ${_VAL})
            if (DEFINED ${_name}_INCLUDE_DIRS)
              list (APPEND ${_prefix}_DEPS_INCDIRS "${${_name}_INCLUDE_DIRS}")
            endif ()
Jan Möbius's avatar
Jan Möbius committed
72
73
74
            if (DEFINED ${_name}_INCLUDE_DIR)
              list (APPEND ${_prefix}_DEPS_INCDIRS "${${_name}_INCLUDE_DIR}")
            endif ()
Jan Möbius's avatar
Jan Möbius committed
75
76
77
            if (DEFINED ${_name}_LIBRARY_DIRS)
              list (APPEND ${_prefix}_DEPS_LIBDIRS "${${_name}_LIBRARY_DIRS}")
            endif ()
Jan Möbius's avatar
Jan Möbius committed
78
79
80
            if (DEFINED ${_name}_LIBRARY_DIR)
              list (APPEND ${_prefix}_DEPS_LIBDIRS "${${_name}_LIBRARY_DIR}")
            endif ()
Jan Möbius's avatar
Jan Möbius committed
81
82
83
84
85
86
87
88
89
            if (DEFINED ${_name}_LIBRARIES)
              list (APPEND ${_prefix}_DEPS_LIBRARIES "${${_name}_LIBRARIES}")
            endif ()
            if (DEFINED ${_name}_LINKER_FLAGS)
              list (APPEND ${_prefix}_DEPS_LINKER_FLAGS "${${_name}_LINKER_FLAGS}")
            endif ()
          endforeach ()
        else ()
            set (${_prefix}_HAS_DEPS FALSE)
Jan Möbius's avatar
   
Jan Möbius committed
90
            acg_set (_${_prefix}_MISSING_DEPS "${_${_prefix}_MISSING_DEPS} ${_val}")
Jan Möbius's avatar
Jan Möbius committed
91
92
93
94
95
96
        endif ()
    endforeach ()
endmacro ()

# main function
function (_build_openflipper_plugin plugin)
Jan Möbius's avatar
   
Jan Möbius committed
97

Jan Möbius's avatar
   
Jan Möbius committed
98
  acg_set (OPENFLIPPER_${_PLUGIN}_BUILD "0")
Jan Möbius's avatar
Jan Möbius committed
99

Jan Möbius's avatar
Jan Möbius committed
100
  # get upper plugin name
Jan Möbius's avatar
Jan Möbius committed
101
102
  string (TOUPPER ${plugin} _PLUGIN)

Jan Möbius's avatar
Jan Möbius committed
103
  # parse parameters
Jan Möbius's avatar
Jan Möbius committed
104
105
106
107
  _get_plugin_parameters (${_PLUGIN} ${ARGN})


  # check dependencies
Jan Möbius's avatar
   
Jan Möbius committed
108
  acg_unset (_${_PLUGIN}_MISSING_DEPS)
Jan Möbius's avatar
Jan Möbius committed
109
110
111
  set (${_PLUGIN}_HAS_DEPS)
  _check_plugin_deps (${_PLUGIN} ${${_PLUGIN}_OPTDEPS})

Jan Möbius's avatar
   
Jan Möbius committed
112
  acg_unset (_${_PLUGIN}_MISSING_DEPS)
Jan Möbius's avatar
Jan Möbius committed
113
114
115
116
117
118
119
  set (${_PLUGIN}_HAS_DEPS)
  _check_plugin_deps (${_PLUGIN} ${${_PLUGIN}_DEPS})

  if (${_PLUGIN}_HAS_DEPS)
    include_directories (
      .
      ${CMAKE_SOURCE_DIR}
Jan Möbius's avatar
   
Jan Möbius committed
120
      ${CMAKE_SOURCE_DIR}/OpenMesh/src
Jan Möbius's avatar
Jan Möbius committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      ${CMAKE_CURRENT_SOURCE_DIR}
      ${CMAKE_CURRENT_BINARY_DIR}
      ${${_PLUGIN}_DEPS_INCDIRS}
      ${${_PLUGIN}_INCDIRS}
      ${OPENGL_INCLUDE_DIR}
      ${GLEW_INCLUDE_DIR}
      ${GLUT_INCLUDE_DIR}
    )

    link_directories (
      ${${_PLUGIN}_DEPS_LIBDIRS}
      ${${_PLUGIN}_LIBDIRS}
    )

    set (directories 
      . 
      ${${_PLUGIN}_DIRS}
    )

Jan Möbius's avatar
Jan Möbius committed
140
    # collect all header,source and ui files
Jan Möbius's avatar
   
Jan Möbius committed
141
142
143
144
    acg_append_files (headers "*.hh" ${directories})
    acg_append_files (sources "*.cc" ${directories})
    acg_append_files (sources "*.c" ${directories})
    acg_append_files (ui "*.ui" ${directories})
Jan Möbius's avatar
Jan Möbius committed
145

Jan Möbius's avatar
Jan Möbius committed
146
    # remove template cc files from source file list
Jan Möbius's avatar
   
Jan Möbius committed
147
    acg_drop_templates (sources)
Jan Möbius's avatar
Jan Möbius committed
148

Jan Möbius's avatar
   
Jan Möbius committed
149
150
151
152
153
    # filter header,source and ui files lists if acg_list_filter macro has been defined
    if (COMMAND acg_list_filter)
      acg_list_filter (headers)
      acg_list_filter (sources)
      acg_list_filter (ui)
Jan Möbius's avatar
Dennis:    
Jan Möbius committed
154
155
    endif ()

Jan Möbius's avatar
Jan Möbius committed
156
    # genereate uic and moc targets
Jan Möbius's avatar
   
Jan Möbius committed
157
158
    acg_qt4_autouic (uic_targets ${ui})
    acg_qt4_automoc (moc_targets ${headers})
Jan Möbius's avatar
Jan Möbius committed
159

160
    add_library (Plugin-${plugin} MODULE ${uic_targets} ${sources} ${headers} ${moc_targets} ${${_PLUGIN}_ADDSRC})
Jan Möbius's avatar
Jan Möbius committed
161

Jan Möbius's avatar
Jan Möbius committed
162
    # add this plugin to build plugin list for dependency tracking
Jan Möbius's avatar
   
Jan Möbius committed
163
164
    acg_set (OPENFLIPPER_PLUGINS "${OPENFLIPPER_PLUGINS};Plugin-${plugin}")
    acg_set (OPENFLIPPER_${_PLUGIN}_BUILD "1")
Jan Möbius's avatar
Jan Möbius committed
165

Jan Möbius's avatar
Jan Möbius committed
166
    # append compiler and linker flags from plugin dependencies
Jan Möbius's avatar
Jan Möbius committed
167
168
169
170
171
172
173
    set_target_properties (
      Plugin-${plugin} PROPERTIES
      COMPILE_FLAGS "${${_PLUGIN}_CFLAGSADD}"
      LINK_FLAGS "${${_PLUGIN}_LDFLAGSADD} ${${_PLUGIN}_DEPS_LINKER_FLAGS}"
    )
    
    if (WIN32)
Jan Möbius's avatar
Jan Möbius committed
174
      # Visual studio requires our plugins to link with GLUT
Jan Möbius's avatar
Jan Möbius committed
175
      find_package (GLUT)
Jan Möbius's avatar
Jan Möbius committed
176
      # generate dllinport defines
Jan Möbius's avatar
Jan Möbius committed
177
      add_definitions (-DACGDLL -DUSEACG -DPLUGINLIBDLL -DUSEPLUGINLIBDLL)
Jan Möbius's avatar
   
Jan Möbius committed
178
      target_link_libraries (Plugin-${plugin}
179
180
181
	OpenMeshCore
        OpenMeshTools
        ACG
Jan Möbius's avatar
Jan Möbius committed
182
183
184
        PluginLib
        ${GLUT_LIBRARIES}
      )
Jan Möbius's avatar
Jan Möbius committed
185
186
187
188

      # copy plugin dll file to "Build" directory
      # Visual studio will create this file in a subdirectory so we can't use
      # LIBRARY_OUTPUT_DIRECTORY directly here
Jan Möbius's avatar
   
Jan Möbius committed
189
190
      if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR})
        file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build/${ACG_PROJEC_PLUGINDIR})
Jan Möbius's avatar
Jan Möbius committed
191
192
193
194
195
      endif ()
      add_custom_command (TARGET Plugin-${plugin} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/Plugin-${plugin}.dll
Jan Möbius's avatar
   
Jan Möbius committed
196
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}
Jan Möbius's avatar
Jan Möbius committed
197
                          )
198
199
200
201
202
    elseif (APPLE) 

      target_link_libraries (
         Plugin-${plugin}
	 PluginLib
Jan Möbius's avatar
Jan Möbius committed
203
	 ${QT_LIBRARIES}
204
      )
Jan Möbius's avatar
Jan Möbius committed
205
      # copy plugin so file to application bundle inside "Build" directory
Jan Möbius's avatar
   
Jan Möbius committed
206
207
      if (NOT EXISTS ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR})
        file (MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR})
Jan Möbius's avatar
Jan Möbius committed
208
209
210
211
212
      endif ()
      add_custom_command (TARGET Plugin-${plugin} POST_BUILD
                          COMMAND ${CMAKE_COMMAND} -E
                          copy_if_different
                            ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/libPlugin-${plugin}.so
Jan Möbius's avatar
   
Jan Möbius committed
213
                            ${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}/libPlugin-${plugin}.so
Jan Möbius's avatar
Jan Möbius committed
214
                          )
Jan Möbius's avatar
Jan Möbius committed
215
216
    else ()
      # directly generate plugin in plugin directory
Jan Möbius's avatar
Jan Möbius committed
217
218
      set_target_properties (
        Plugin-${plugin} PROPERTIES
Jan Möbius's avatar
   
Jan Möbius committed
219
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Build/${ACG_PROJECT_PLUGINDIR}"
Jan Möbius's avatar
Jan Möbius committed
220
221
222
223
224
225
226
      )
    endif ()
    
    target_link_libraries (Plugin-${plugin}
      ${${_PLUGIN}_DEPS_LIBRARIES}
      ${${_PLUGIN}_LIBRARIES}
    )
Jan Möbius's avatar
Jan Möbius committed
227

Jan Möbius's avatar
Jan Möbius committed
228
229
    # no install on mac, because the whole bundle will be installed in the
    # toplevel CMakeLists.txt
Jan Möbius's avatar
Jan Möbius committed
230
231
232
    if (NOT APPLE)
      install (
        TARGETS Plugin-${plugin}
Jan Möbius's avatar
   
Jan Möbius committed
233
        DESTINATION ${ACG_PROJECT_PLUGINDIR}
Jan Möbius's avatar
Jan Möbius committed
234
235
      )
    endif ()
Jan Möbius's avatar
Jan Möbius committed
236
237
238
239
240
241
242
243
244
245
246
247

  else ()
    message (STATUS "[WARNING] One or more dependencies for plugin ${plugin} not found. Skipping plugin.")
    message (STATUS "Missing dependencies :${_${_PLUGIN}_MISSING_DEPS}")
  endif ()
endfunction ()

macro (openflipper_plugin)
  _get_plugin_name (_plugin)

  string (TOUPPER ${_plugin} _PLUGIN)

Jan Möbius's avatar
Jan Möbius committed
248
  # add option to disable plugin build
Jan Möbius's avatar
Jan Möbius committed
249
250
251
252
253
254
255
256
257
  option (
    DISABLE_PLUGIN_${_PLUGIN}
    "Disable building of plugin \"${_plugin}\""
        OFF
  )

  if (NOT DISABLE_PLUGIN_${_PLUGIN})
    _build_openflipper_plugin (${_plugin} ${ARGN})
  endif ()
258
endmacro ()