summaryrefslogtreecommitdiffhomepage
path: root/cmake/opentrack-boilerplate.cmake
blob: 54643c423ea6af0449bef36e637f8316a1cf04c8 (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
include(opentrack-hier)

set(new-hier-path "#pragma once
#define OPENTRACK_LIBRARY_PATH \"${opentrack-hier-path}\"
")

set(hier-path-filename "${CMAKE_BINARY_DIR}/opentrack-library-path.h")
set(orig-hier-path "")
if(EXISTS ${hier-path-filename})
    file(READ ${hier-path-filename} orig-hier-path)
endif()
if(NOT (orig-hier-path STREQUAL new-hier-path))
    file(WRITE ${hier-path-filename} "${new-hier-path}")
endif()

function(opentrack_set_globs n)
    set(dir ${PROJECT_SOURCE_DIR})
    file(GLOB ${n}-c ${dir}/*.cpp ${dir}/*.c ${dir}/*.h ${dir}/*.hpp)
    file(GLOB ${n}-res ${dir}/*.rc)
    foreach(f ${n}-res)
        set_source_files_properties(${f} PROPERTIES LANGUAGE RC)
    endforeach()
    file(GLOB ${n}-ui ${dir}/*.ui)
    file(GLOB ${n}-rc ${dir}/*.qrc)
    foreach(i c res ui rc)
        set(${n}-${i} ${${n}-${i}} PARENT_SCOPE)
    endforeach()
endfunction()

function(opentrack_qt n)
    qt5_wrap_cpp(${n}-moc ${${n}-c} OPTIONS --no-notes)
    QT5_WRAP_UI(${n}-uih ${${n}-ui})
    QT5_ADD_RESOURCES(${n}-rcc ${${n}-rc})
    set(${n}-all ${${n}-c} ${${n}-rc} ${${n}-rcc} ${${n}-uih} ${${n}-moc} ${${n}-res})
    foreach(i moc uih rcc all)
        set(${n}-${i} ${${n}-${i}} PARENT_SCOPE)
    endforeach()
endfunction()

function(opentrack_compat target)
    if(MSVC)
        set(msvc-subsystem "/VERSION:5.1 /SUBSYSTEM:WINDOWS,5.01")
        set_target_properties(${target} PROPERTIES LINK_FLAGS "${msvc-subsystem} /DEBUG /OPT:ICF")
    endif()
    if(NOT MSVC)
        set_property(SOURCE ${${target}-moc} APPEND_STRING PROPERTY COMPILE_FLAGS "-w -Wno-error")
    endif()
    if(WIN32)
        target_link_libraries(${target} dinput8 dxguid strmiids)
    endif()
endfunction()

function(opentrack_boilerplate__ n files_ no-library_ static_ no-compat_ compile_ link_ stage2_)
    if((NOT no-library_) AND (NOT stage2_))
        set(link-mode SHARED)
        if (static_)
            set(link-mode STATIC)
        endif()
        add_library(${n} ${link-mode} ${files_})
        message(STATUS "module ${n}")
    endif()
    if(NOT no-library_)
        opentrack_compat(${n})
    endif()
    if(NOT no-compat_)
        target_link_libraries(${n} opentrack-api opentrack-compat)
    endif()
    target_link_libraries(${n} ${MY_QT_LIBS})
    if(CMAKE_COMPILER_IS_GNUCXX)
        set(c-props "-fvisibility=hidden -fuse-cxa-atexit")
    endif()
    if(CMAKE_COMPILER_IS_GNUCXX AND NOT APPLE)
        set(l-props "-Wl,--as-needed")
    endif()
    if(MSVC)
        set(l-props "${msvc-subsystem} /DEBUG /OPT:ICF")
    endif()
    get_target_property(orig-compile ${n} COMPILE_FLAGS)
    if(NOT orig-compile)
        set(orig-compile "")
    endif()
    get_target_property(orig-link ${n} LINK_FLAGS)
    if(NOT orig-link)
        set(orig-link "")
    endif()
    set_target_properties(${n} PROPERTIES
        COMPILE_FLAGS "${c-props} ${orig-compile} ${compile_}"
        LINK_FLAGS "${l-props} ${orig-link} ${link_}"
    )
    string(REGEX REPLACE "^opentrack-" "" n_ ${n})
    string(REPLACE "-" "_" n_ ${n_})
    target_compile_definitions(${n} PRIVATE "BUILD_${n_}")
    if((NOT static_) AND (NOT no-library_))
        install(TARGETS ${n} ${opentrack-hier-str})
    endif()
endfunction()

macro(opentrack_boilerplate n)
    cmake_parse_arguments(${n}-args
        "NO-LIBRARY;STATIC;NO-COMPAT;STAGE2"
        "LINK;COMPILE"
        ""
        ${ARGN}
    )
    if(NOT "${${n}-args_UNPARSED_ARGUMENTS}" STREQUAL "")
        message(FATAL_ERROR "opentrack_boilerplate bad formals ${${n}-args_UNPARSED_ARGUMENTS}")
    endif()
    if(NOT ${n}-args_STAGE2)
        project(${n})
        opentrack_set_globs(${n})
        opentrack_qt(${n})
    endif()
    if((NOT ${n}-args_NO-LIBRARY) OR ${n}-args_STAGE2)
        opentrack_boilerplate__("${n}" "${${n}-all}"
                                       "${${n}-args_NO-LIBRARY}"
                                       "${${n}-args_STATIC}"
                                       "${${n}-args_NO-COMPAT}"
                                       "${${n}-args_COMPILE}"
                                       "${${n}-args_LINK}"
                                       "${${n}-args_STAGE2}")
    endif()
endmacro()