summaryrefslogtreecommitdiff
path: root/cmake/ModulePackage.cmake
blob: b963e0de036f545a090191bebe134ac52c4b9f46 (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
option(ENABLE_SHARED "Build shared (dynamic) libraries for all modules" ON)
option(ENABLE_STATIC "Build static libraries for all modules" ON)
option(COMPILE_AS_CXX "Compile all C code as C++ code" OFF)

find_package(PkgConfig REQUIRED)

function(add_c_executable exec)
  if(COMPILE_AS_CXX)
    set_source_files_properties(${ARGN} PROPERTIES LANGUAGE CXX)
  endif()

  add_executable(${exec} ${ARGN})
endfunction()

function(pkg_use_module mod)
  pkg_search_module(${mod} ${ARGN})
  if(${mod}_FOUND)
    link_directories(${${mod}_LIBRARY_DIRS})
    include_directories(${${mod}_INCLUDE_DIRS})
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${${mod}_CFLAGS_OTHER}")
  endif()
endfunction()

function(add_module lib)
  if(COMPILE_AS_CXX)
    set_source_files_properties(${ARGN} PROPERTIES LANGUAGE CXX)
  endif()

  if(ENABLE_SHARED)
    add_library(${lib}_shared SHARED ${ARGN})
    set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib})
    install(TARGETS ${lib}_shared DESTINATION "lib")
  endif()
  if(ENABLE_STATIC)
    add_library(${lib}_static STATIC ${ARGN})
    set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib})
    install(TARGETS ${lib}_static DESTINATION "lib")
  endif()
endfunction()

function(target_link_modules target)
  if(TARGET ${target}_shared)
    set(_targets ${_targets} ${target}_shared)
  endif()
  if(TARGET ${target}_static)
    set(_targets ${_targets} ${target}_static)
  endif()
  if(NOT _targets)
    set(_targets ${_targets} ${target})
  endif()

  foreach(target ${_targets})
    foreach(dep ${ARGN})
      if(TARGET ${dep}_shared)
        target_link_libraries(${target} ${dep}_shared)
      elseif(TARGET ${dep}_static)
        target_link_libraries(${target} ${dep}_static)
      else()
        target_link_libraries(${target} ${dep})
      endif()
    endforeach()
  endforeach()
endfunction()