summaryrefslogtreecommitdiff
path: root/cmake/scripts/GenerateWidget.cmake
blob: 0bb349095545b861d2f36cf44023edaa36f6db44 (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
cmake_minimum_required(VERSION 3.5)

#
# CMake script to automatically generate the enums in script_window.hpp
#
# The file is scanned for @enum tokens, and the placeholder is filled with an enum from a different file.
#
# Example:
#   // @enum enumname filename@placeholder@
#   ... content here is replaced ...
#   // @endenum
#
# The parameter "enumname" specifies the enumeration to extract. This can also be a regular expression.
# The parameter "filename" specifies the relative path to the file, where the enumeration is extracted from. This can also be a glob expression.
#
#

if (NOT GENERATE_SOURCE_FILE)
    message(FATAL_ERROR "Script needs GENERATE_SOURCE_FILE defined")
endif (NOT GENERATE_SOURCE_FILE)
if (NOT GENERATE_BINARY_FILE)
    message(FATAL_ERROR "Script needs GENERATE_BINARY_FILE defined")
endif (NOT GENERATE_BINARY_FILE)

file(STRINGS ${GENERATE_SOURCE_FILE} ENUM_LINES REGEX "@enum")

foreach(ENUM IN LISTS ENUM_LINES)
    string(REGEX REPLACE "^(	)// @enum ([^ ]+) ([^ ]+)@([^ ]+)@" "\\4" PLACE_HOLDER "${ENUM}")
    set(ADD_INDENT "${CMAKE_MATCH_1}")
    set(ENUM_PATTERN "${CMAKE_MATCH_2}")

    file(GLOB FILENAMES "${CMAKE_MATCH_3}")
    list(SORT FILENAMES)

    foreach(FILE IN LISTS FILENAMES)
        unset(ACTIVE)
        unset(ACTIVE_COMMENT)
        unset(COMMENT)

        file(STRINGS ${FILE} SOURCE_LINES)

        string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" FILE ${FILE})
        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}/* automatically generated from ${FILE} */")

        foreach(LINE IN LISTS SOURCE_LINES)
            string(REPLACE "${RM_INDENT}" "" LINE "${LINE}")

            # Remember possible doxygen comment before enum declaration
            if ((NOT ACTIVE) AND "${LINE}" MATCHES "/\\*\\*")
                set(COMMENT "${ADD_INDENT}${LINE}")
                set(ACTIVE_COMMENT 1)
            elseif (ACTIVE_COMMENT EQUAL 1)
                string(APPEND COMMENT "\n${ADD_INDENT}${LINE}")
            endif()

            # Check for enum match
            if ("${LINE}" MATCHES "^	*enum *${ENUM_PATTERN} *\{")
                # REGEX REPLACE does a REGEX MATCHALL and fails if an empty string is matched
                string(REGEX MATCH "[^	]*" RESULT "${LINE}")
                string(REPLACE "${RESULT}" "" RM_INDENT "${LINE}")

                set(ACTIVE 1)
                if (ACTIVE_COMMENT GREATER 0)
                     string(APPEND ${PLACE_HOLDER} "\n${COMMENT}")
                endif (ACTIVE_COMMENT GREATER 0)
                unset(ACTIVE_COMMENT)
                unset(COMMENT)
            endif ("${LINE}" MATCHES "^	*enum *${ENUM_PATTERN} *\{")

            # Forget doxygen comment, if no enum follows
            if (ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
                unset(ACTIVE_COMMENT)
                unset(COMMENT)
            endif (ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
            if (ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")
                set(ACTIVE_COMMENT 2)
            endif (ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")

            if (ACTIVE)
                if ("${LINE}" MATCHES "^	*[A-Za-z0-9_]* *[,=]")
                    # Transform enum values
                    # REGEX REPLACE does a REGEX MATCHALL and replaces too much
                    string(REGEX MATCH " *=[^,]*" RESULT "${LINE}")
                    string(REPLACE "${RESULT}" "" LINE "${LINE}")

                    string(REGEX REPLACE " *//" " //" LINE "${LINE}")

                    string(REGEX MATCH "^(	*)([A-Za-z0-9_]+),(.*)" RESULT "${LINE}")

                    string(LENGTH "${CMAKE_MATCH_2}" LEN)
                    math(EXPR LEN "43 - ${LEN}")
                    unset(SPACES)
                    foreach(i RANGE ${LEN})
                        string(APPEND SPACES " ")
                    endforeach(i)

                    if (CMAKE_MATCH_3)
                        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},${SPACES}${CMAKE_MATCH_3}")
                    else (CMAKE_MATCH_3)
                        string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},")
                    endif (CMAKE_MATCH_3)
                elseif ("${LINE}" STREQUAL "")
                    string(APPEND ${PLACE_HOLDER} "\n")
                else ()
                    string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${LINE}")
                endif ()
            endif (ACTIVE)

            if ("${LINE}" MATCHES "^	*\};")
                if (ACTIVE)
                    string(APPEND ${PLACE_HOLDER} "\n")
                endif (ACTIVE)
                unset(ACTIVE)
            endif ("${LINE}" MATCHES "^	*\};")
         endforeach(LINE)
    endforeach(FILE)
 endforeach(ENUM)

configure_file(${GENERATE_SOURCE_FILE} ${GENERATE_BINARY_FILE})