Update: Translations from eints
[openttd-github.git] / cmake / scripts / GenerateWidget.cmake
blobffa5e6edea3d3dfed141a0ba4b3ebb995ff100a2
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.
8 # Example:
9 #   // @enum enumname filename@placeholder@
10 #   ... content here is replaced ...
11 #   // @endenum
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")
21 endif()
22 if(NOT GENERATE_BINARY_FILE)
23     message(FATAL_ERROR "Script needs GENERATE_BINARY_FILE defined")
24 endif()
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}")
34     list(SORT FILENAMES)
36     foreach(FILE IN LISTS FILENAMES)
37         unset(ACTIVE)
38         unset(ACTIVE_COMMENT)
39         unset(COMMENT)
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}")
53                 set(ACTIVE_COMMENT 1)
54             elseif(ACTIVE_COMMENT EQUAL 1)
55                 string(APPEND COMMENT "\n${ADD_INDENT}${LINE}")
56             endif()
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}")
67                 set(ACTIVE 1)
68                 if(ACTIVE_COMMENT GREATER 0)
69                      string(APPEND ${PLACE_HOLDER} "\n${COMMENT}")
70                 endif()
71                 unset(ACTIVE_COMMENT)
72                 unset(COMMENT)
73             endif()
75             # Forget doxygen comment, if no enum follows
76             if(ACTIVE_COMMENT EQUAL 2 AND NOT "${LINE}" STREQUAL "")
77                 unset(ACTIVE_COMMENT)
78                 unset(COMMENT)
79             endif()
80             if(ACTIVE_COMMENT EQUAL 1 AND "${LINE}" MATCHES "\\*/")
81                 set(ACTIVE_COMMENT 2)
82             endif()
84             if(ACTIVE)
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}")
97                     unset(SPACES)
98                     foreach(i RANGE ${LEN})
99                         string(APPEND SPACES " ")
100                     endforeach()
102                     if(CMAKE_MATCH_3)
103                         string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},${SPACES}${CMAKE_MATCH_3}")
104                     else()
105                         string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${CMAKE_MATCH_1}${CMAKE_MATCH_2}${SPACES} = ::${CMAKE_MATCH_2},")
106                     endif()
107                 elseif("${LINE}" STREQUAL "")
108                     string(APPEND ${PLACE_HOLDER} "\n")
109                 else()
110                     string(APPEND ${PLACE_HOLDER} "\n${ADD_INDENT}${LINE}")
111                 endif()
112             endif()
114             if("${LINE}" MATCHES "^     *\};")
115                 if(ACTIVE)
116                     string(APPEND ${PLACE_HOLDER} "\n")
117                 endif()
118                 unset(ACTIVE)
119             endif()
120          endforeach()
121     endforeach()
122  endforeach()
124  list(REMOVE_DUPLICATES INCLUDES)
125  string(REPLACE ";" "\n" INCLUDES "${INCLUDES}")
127 configure_file(${GENERATE_SOURCE_FILE} ${GENERATE_BINARY_FILE})