1# Copyright (c) 2018-2019 Intel Corporation
2#
3# Permission is hereby granted, free of charge, to any person obtaining a copy
4# of this software and associated documentation files (the "Software"), to deal
5# in the Software without restriction, including without limitation the rights
6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7# copies of the Software, and to permit persons to whom the Software is
8# furnished to do so, subject to the following conditions:
9#
10# The above copyright notice and this permission notice shall be included in all
11# copies or substantial portions of the Software.
12#
13# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19# SOFTWARE.
20
21set( CMAKE_LIB_DIR ${CMAKE_BINARY_DIR}/__lib )
22set( CMAKE_BIN_DIR ${CMAKE_BINARY_DIR}/__bin )
23set_property( GLOBAL PROPERTY PROP_PLUGINS_CFG "" )
24
25# .....................................................
26function( collect_oses )
27  if( CMAKE_SYSTEM_NAME MATCHES Windows )
28    set( Windows    true PARENT_SCOPE )
29    set( NotLinux   true PARENT_SCOPE )
30    set( NotDarwin  true PARENT_SCOPE )
31
32  elseif( CMAKE_SYSTEM_NAME MATCHES Darwin )
33    set( Darwin     true PARENT_SCOPE )
34    set( NotLinux   true PARENT_SCOPE )
35    set( NotWindows true PARENT_SCOPE )
36
37  else()
38    set( Linux      true PARENT_SCOPE )
39    set( NotDarwin  true PARENT_SCOPE )
40    set( NotWindows true PARENT_SCOPE )
41
42  endif()
43endfunction()
44
45# .....................................................
46function( append what where )
47  set(${ARGV1} "${ARGV0} ${${ARGV1}}" PARENT_SCOPE)
48endfunction()
49
50# .....................................................
51function( get_source include sources)
52  file( GLOB_RECURSE include "[^.]*.h" )
53  file( GLOB_RECURSE sources "[^.]*.c" "[^.]*.cpp" )
54
55  set( ${ARGV0} ${include} PARENT_SCOPE )
56  set( ${ARGV1} ${sources} PARENT_SCOPE )
57endfunction()
58
59#
60# Usage: get_target(target name none|<variant>)
61#
62function( get_target target name variant)
63  if( ARGV1 MATCHES shortname )
64    get_filename_component( tname ${CMAKE_CURRENT_SOURCE_DIR} NAME )
65  else()
66    set( tname ${ARGV1} )
67  endif()
68  if( ARGV2 MATCHES none OR ARGV2 MATCHES universal OR DEFINED USE_STRICT_NAME)
69    set( target ${tname} )
70  else()
71   set( target ${tname}_${ARGV2} )
72  endif()
73
74  set( ${ARGV0} ${target} PARENT_SCOPE )
75endfunction()
76
77# .....................................................
78function( get_folder folder )
79  set( folder ${CMAKE_PROJECT_NAME} )
80  set (${ARGV0} ${folder} PARENT_SCOPE)
81endfunction()
82
83function( get_mfx_version mfx_version_major mfx_version_minor )
84  file(STRINGS ${MFX_API_HOME}/include/mfxdefs.h major REGEX "#define MFX_VERSION_MAJOR" LIMIT_COUNT 1)
85  if(major STREQUAL "") # old style version
86     file(STRINGS ${MFX_API_HOME}/include/mfxvideo.h major REGEX "#define MFX_VERSION_MAJOR")
87  endif()
88  file(STRINGS ${MFX_API_HOME}/include/mfxdefs.h minor REGEX "#define MFX_VERSION_MINOR" LIMIT_COUNT 1)
89  if(minor STREQUAL "") # old style version
90     file(STRINGS ${MFX_API_HOME}/include/mfxvideo.h minor REGEX "#define MFX_VERSION_MINOR")
91  endif()
92  string(REPLACE "#define MFX_VERSION_MAJOR " "" major ${major})
93  string(REPLACE "#define MFX_VERSION_MINOR " "" minor ${minor})
94  set(${mfx_version_major} ${major} PARENT_SCOPE)
95  set(${mfx_version_minor} ${minor} PARENT_SCOPE)
96endfunction()
97
98function( gen_plugins_cfg plugin_id guid plugin_name type codecID )
99  get_mfx_version(mfx_version_major mfx_version_minor)
100  math(EXPR api_version "${mfx_version_major}*256 + ${mfx_version_minor}")
101
102  get_property( PLUGINS_CFG GLOBAL PROPERTY PROP_PLUGINS_CFG )
103  set(PLUGINS_CFG "${PLUGINS_CFG}[${plugin_id}_${guid}]\n")
104  set(PLUGINS_CFG "${PLUGINS_CFG}GUID = ${guid}\n")
105  set(PLUGINS_CFG "${PLUGINS_CFG}PluginVersion = 1\n")
106  set(PLUGINS_CFG "${PLUGINS_CFG}APIVersion = ${api_version}\n")
107  set(PLUGINS_CFG "${PLUGINS_CFG}Path = ${MFX_PLUGINS_DIR}/lib${plugin_name}.so\n")
108  set(PLUGINS_CFG "${PLUGINS_CFG}Type = ${type}\n")
109  set(PLUGINS_CFG "${PLUGINS_CFG}CodecID = ${codecID}\n")
110  set(PLUGINS_CFG "${PLUGINS_CFG}Default = 0\n")
111  set_property( GLOBAL PROPERTY PROP_PLUGINS_CFG ${PLUGINS_CFG} )
112
113endfunction()
114
115function( create_plugins_cfg directory )
116  get_property( PLUGINS_CFG GLOBAL PROPERTY PROP_PLUGINS_CFG )
117  file(WRITE ${directory}/plugins.cfg ${PLUGINS_CFG})
118
119  install( FILES ${directory}/plugins.cfg DESTINATION ${MFX_PLUGINS_CONF_DIR} )
120
121endfunction()
122
123# Usage:
124#  make_library(shortname|<name> none|<variant> static|shared)
125#    - shortname|<name>: use folder name as library name or <name> specified by user
126#    - <variant>|none: build library in specified variant (with drm or x11 or wayland support, etc),
127#      universal - special variant which enables compilation flags required for all backends, but
128#      moves dependency to runtime instead of linktime
129#    or without variant if none was specified
130#    - static|shared: build static or shared library
131#
132function( make_library name variant type )
133  get_target( target ${ARGV0} ${ARGV1} )
134  if( ${ARGV0} MATCHES shortname )
135    get_folder( folder )
136  else ()
137    set( folder ${ARGV0} )
138  endif()
139
140  configure_dependencies(${target} "${DEPENDENCIES}" ${variant})
141  if(SKIPPING MATCHES ${target} OR NOT_CONFIGURED MATCHES ${target})
142    return()
143  else()
144    report_add_target(BUILDING ${target})
145  endif()
146
147  if( NOT sources )
148   get_source( include sources )
149  endif()
150
151  if( sources.plus )
152    list( APPEND sources ${sources.plus} )
153  endif()
154
155  if( ARGV2 MATCHES static )
156    add_library( ${target} STATIC ${include} ${sources} )
157    append_property(${target} COMPILE_FLAGS "${SCOPE_CFLAGS}")
158
159  elseif( ARGV2 MATCHES shared )
160    add_library( ${target} SHARED ${include} ${sources} )
161
162    if( Linux )
163      target_link_libraries( ${target} "-Xlinker --start-group" )
164    endif()
165
166    foreach( lib ${LIBS_VARIANT} )
167      if(ARGV1 MATCHES none OR ARGV1 MATCHES universal)
168        add_dependencies( ${target} ${lib} )
169        target_link_libraries( ${target} ${lib} )
170      else()
171        add_dependencies( ${target} ${lib}_${ARGV1} )
172        target_link_libraries( ${target} ${lib}_${ARGV1} )
173      endif()
174    endforeach()
175
176    foreach( lib ${LIBS_NOVARIANT} )
177      add_dependencies( ${target} ${lib} )
178      target_link_libraries( ${target} ${lib} )
179    endforeach()
180
181    append_property(${target} COMPILE_FLAGS "${CFLAGS} ${SCOPE_CFLAGS}")
182    append_property(${target} LINK_FLAGS "${LDFLAGS} ${SCOPE_LINKFLAGS}")
183    foreach(lib ${LIBS} ${SCOPE_LIBS})
184      target_link_libraries( ${target} ${lib} )
185    endforeach()
186
187    if( Linux )
188      target_link_libraries( ${target} "-Xlinker --end-group" )
189    endif()
190
191#    set_target_properties( ${target} PROPERTIES LINK_INTERFACE_LIBRARIES "" )
192  endif()
193
194  configure_build_variant( ${target} ${ARGV1} )
195
196  if( defs )
197    append_property( ${target} COMPILE_FLAGS ${defs} )
198  endif()
199  set_target_properties( ${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BIN_DIR}/${CMAKE_BUILD_TYPE} FOLDER ${folder} )
200  set_target_properties( ${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BIN_DIR}/${CMAKE_BUILD_TYPE} FOLDER ${folder} )
201  set_target_properties( ${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_LIB_DIR}/${CMAKE_BUILD_TYPE} FOLDER ${folder} )
202
203  if( Linux )
204    target_link_libraries( ${target} "-lgcc" )
205  endif()
206
207  set( target ${target} PARENT_SCOPE )
208endfunction()
209
210# Usage:
211#  make_executable(name|<name> none|<variant>)
212#    - name|<name>: use folder name as library name or <name> specified by user
213#    - <variant>|none: build library in specified variant (with drm or x11 or wayland support, etc),
214#      universal - special variant which enables compilation flags required for all backends, but
215#      moves dependency to runtime instead of linktime
216#
217function( make_executable name variant )
218  get_target( target ${ARGV0} ${ARGV1} )
219  get_folder( folder )
220
221  configure_dependencies(${target} "${DEPENDENCIES}" ${variant})
222  if(SKIPPING MATCHES ${target} OR NOT_CONFIGURED MATCHES ${target})
223    return()
224  else()
225    report_add_target(BUILDING ${target})
226  endif()
227
228  if( NOT sources )
229    get_source( include sources )
230  endif()
231
232  if( sources.plus )
233    list( APPEND sources ${sources.plus} )
234  endif()
235
236  add_executable( ${target} ${include} ${sources} )
237
238  if( defs )
239    append_property( ${target} COMPILE_FLAGS ${defs} )
240  endif()
241  set_target_properties( ${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BIN_DIR}/${CMAKE_BUILD_TYPE} FOLDER ${folder} )
242
243  if( Linux )
244    target_link_libraries( ${target} "-Xlinker --start-group" )
245  endif()
246
247  foreach( lib ${LIBS_VARIANT} )
248    if(ARGV1 MATCHES none OR ARGV1 MATCHES universal)
249      add_dependencies( ${target} ${lib} )
250      target_link_libraries( ${target} ${lib} )
251    else()
252      add_dependencies( ${target} ${lib}_${ARGV1} )
253      target_link_libraries( ${target} ${lib}_${ARGV1} )
254    endif()
255  endforeach()
256
257  foreach( lib ${LIBS_NOVARIANT} )
258    add_dependencies( ${target} ${lib} )
259    target_link_libraries( ${target} ${lib} )
260  endforeach()
261
262  if( ${NEED_DISPATCHER} )
263    target_link_libraries( ${target} debug mfx_d )
264    target_link_libraries( ${target} optimized mfx )
265  endif()
266
267  foreach( lib ${LIBS} ${SCOPE_LIBS} )
268    target_link_libraries( ${target} ${lib} )
269  endforeach()
270
271  append_property(${target} COMPILE_FLAGS "${CFLAGS} ${SCOPE_CFLAGS}")
272  append_property(${target} LINK_FLAGS "${LDFLAGS} ${SCOPE_LINKFLAGS}")
273
274  configure_build_variant( ${target} ${ARGV1} )
275
276  foreach( lib ${LIBS_SUFFIX} )
277    target_link_libraries( ${target} ${lib} )
278  endforeach()
279
280  if( Linux )
281    target_link_libraries( ${target} "-Xlinker --end-group -lgcc" )
282  endif()
283
284  set( target ${target} PARENT_SCOPE )
285endfunction()
286
287function( set_file_and_product_version input_version version_defs )
288  if( Linux OR Darwin )
289    execute_process(
290      COMMAND echo
291      COMMAND cut -f 1 -d.
292      COMMAND date -r 1640076943 "+.%-y.%-m.%-d"
293      OUTPUT_VARIABLE cur_date
294      OUTPUT_STRIP_TRAILING_WHITESPACE
295      )
296    string( SUBSTRING ${input_version} 0 1 ver )
297
298    set( version_defs " -DMFX_PLUGIN_FILE_VERSION=\"\\\"${ver}${cur_date}\"\\\"" )
299
300    set( git_commit "intel-mediasdk-22.1.0" )
301    git_describe( git_commit )
302
303    set( version_defs " -DMFX_PLUGIN_FILE_VERSION=\"\\\"${ver}${cur_date}${git_commit}\"\\\"" )
304    set( version_defs "${version_defs} -DMFX_PLUGIN_PRODUCT_VERSION=\"\\\"${input_version}\"\\\"" PARENT_SCOPE )
305
306  endif()
307endfunction()
308
309function( git_describe git_commit )
310  execute_process(
311    COMMAND git rev-parse --short HEAD
312    OUTPUT_VARIABLE git_commit
313    OUTPUT_STRIP_TRAILING_WHITESPACE
314    WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
315  )
316  if( NOT ${git_commit} MATCHES "^$" )
317    set( git_commit "intel-mediasdk-22.1.0" PARENT_SCOPE )
318  endif()
319endfunction()
320
321function(msdk_install target dir)
322  if( Windows )
323    install( TARGETS ${target} RUNTIME DESTINATION ${dir} )
324  else()
325    install( TARGETS ${target} LIBRARY DESTINATION ${dir} )
326  endif()
327endfunction()
328
329