1 cmake_minimum_required(VERSION 3.5)
4 # CMake script to automatically generate the enums in script_window.hpp
6 # The file is scanned for @enum tokens, and the placeholder is filled with an enum from a different file.
9 # // @enum enumname filename@placeholder@
10 # ... content here is replaced ...
13 # The parameter "enumname" specifies the enumeration to extract. This can also be a regular expression.
14 # The parameter "filename" specifies the relative path to the file, where the enumeration is extracted from. This can also be a glob expression.
16 # All files where enumerations are extracted from are automatically added via #include
19 if(NOT GENERATE_SOURCE_FILE)
20 message(FATAL_ERROR "Script needs GENERATE_SOURCE_FILE defined")
22 if(NOT GENERATE_BINARY_FILE)
23 message(FATAL_ERROR "Script needs GENERATE_BINARY_FILE defined")
26 file(STRINGS ${GENERATE_SOURCE_FILE} ENUM_LINES REGEX "@enum")
28 foreach(ENUM IN LISTS ENUM_LINES)
29 string(REGEX REPLACE "^( )// @enum ([^ ]+) ([^ ]+)@([^ ]+)@" "\\4" PLACE_HOLDER "${ENUM}")
30 set(ADD_INDENT "${CMAKE_MATCH_1}")
31 set(ENUM_PATTERN "${CMAKE_MATCH_2}")
33 file(GLOB FILENAMES "${CMAKE_MATCH_3}")
36 foreach(FILE IN LISTS FILENAMES)
41 file(STRINGS ${FILE} SOURCE_LINES)
43 string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" FILE ${FILE})
44 string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}/* automatically generated from ${FILE} */")
45 list(APPEND INCLUDES "#include \"${FILE}\"")
47 foreach(LINE IN LISTS SOURCE_LINES)
48 string(REPLACE "${RM_INDENT}" "" LINE "${LINE}")
50 # Remember possible doxygen comment before enum declaration
51 if((NOT ACTIVE) AND "${LINE}" MATCHES "/\\*\\*")
52 set(COMMENT "${ADD_INDENT}${LINE}")
54 elseif(ACTIVE_COMMENT EQUAL 1)
55 string(APPEND COMMENT "\n${ADD_INDENT}${LINE}")
58 # Check for enum match
59 if("${LINE}" MATCHES "^ *enum *${ENUM_PATTERN}( *: *[^ ]*)? *\{")
60 # REGEX REPLACE does a REGEX MATCHALL and fails if an empty string is matched
61 string(REGEX MATCH "[^ ]*" RESULT "${LINE}")
62 string(REPLACE "${RESULT}" "" RM_INDENT "${LINE}")
64 string(REGEX MATCH " *: *[^ ]*" RESULT "${LINE}")
65 string(REPLACE "${RESULT}" "" LINE "${LINE}")
68 if(ACTIVE_COMMENT GREATER 0)
69 string(APPEND ${PLACE_HOLDER} "\n${COMMENT}")
75 # Forget doxygen comment, if no enum follows
76 if(ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
80 if(ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")
85 if("${LINE}" MATCHES "^ *[A-Za-z0-9_]* *[,=]")
86 # Transform enum values
87 # REGEX REPLACE does a REGEX MATCHALL and replaces too much
88 string(REGEX MATCH " *=[^,]*" RESULT "${LINE}")
89 string(REPLACE "${RESULT}" "" LINE "${LINE}")
91 string(REGEX REPLACE " *//" " //" LINE "${LINE}")
93 string(REGEX MATCH "^( *)([A-Za-z0-9_]+),(.*)" RESULT "${LINE}")
95 string(LENGTH "${CMAKE_MATCH_2}" LEN)
96 math(EXPR LEN "43 - ${LEN}")
98 foreach(i RANGE ${LEN})
99 string(APPEND SPACES " ")
103 string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},${SPACES}${CMAKE_MATCH_3}")
105 string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},")
107 elseif("${LINE}" STREQUAL "")
108 string(APPEND ${PLACE_HOLDER} "\n")
110 string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${LINE}")
114 if("${LINE}" MATCHES "^ *\};")
116 string(APPEND ${PLACE_HOLDER} "\n")
124 list(REMOVE_DUPLICATES INCLUDES)
125 string(REPLACE ";" "\n" INCLUDES "${INCLUDES}")
127 configure_file(${GENERATE_SOURCE_FILE} ${GENERATE_BINARY_FILE})