Codechange: replace "magic" trick with simpler straight forward code
[openttd-github.git] / cmake / scripts / SquirrelIncludes.cmake
blobef81ddf041ec6aa27f5b0fb05234c072bfa03182
1 cmake_minimum_required(VERSION 3.5)
3 if(NOT INCLUDES_SOURCE_FILE)
4     message(FATAL_ERROR "Script needs INCLUDES_SOURCE_FILE defined")
5 endif()
6 if(NOT INCLUDES_BINARY_FILE)
7     message(FATAL_ERROR "Script needs INCLUDES_BINARY_FILE defined")
8 endif()
9 if(NOT APILC)
10     message(FATAL_ERROR "Script needs APILC defined")
11 endif()
12 if(NOT APIUC)
13     message(FATAL_ERROR "Script needs APIUC defined")
14 endif()
15 if(NOT API_FILES)
16     message(FATAL_ERROR "Script needs API_FILES defined")
17 endif()
19 file(READ "${API_FILES}" SCRIPT_API_BINARY_FILES)
21 foreach(FILE IN LISTS SCRIPT_API_BINARY_FILES)
22     file(STRINGS "${FILE}" LINES REGEX "^void SQ${APIUC}.*_Register\\(Squirrel \\*engine\\)$")
23     if(LINES)
24         string(REGEX REPLACE ".*api/${APILC}/(.*)" "#include \"\\1\"" FILE "${FILE}")
25         list(APPEND SQUIRREL_INCLUDES "${FILE}")
26         foreach(LINE IN LISTS LINES)
27             if("${LINE}" MATCHES "SQ${APIUC}(List|Controller)_Register")
28                 continue()
29             endif()
30             string(REGEX REPLACE "^.*void " "   " LINE "${LINE}")
31             string(REGEX REPLACE "Squirrel \\*" "" LINE "${LINE}")
32             list(APPEND SQUIRREL_REGISTER "${LINE}")
33         endforeach()
34     endif()
35 endforeach()
37 list(SORT SQUIRREL_INCLUDES)
38 string(REPLACE ";" "\n" SQUIRREL_INCLUDES "${SQUIRREL_INCLUDES}")
40 string(REGEX REPLACE "_Register" "0000Register" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
41 list(SORT SQUIRREL_REGISTER)
42 string(REGEX REPLACE "0000Register" "_Register" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
43 string(REPLACE ";" ";\n" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
44 set(SQUIRREL_REGISTER " SQ${APIUC}List_Register(engine);\n${SQUIRREL_REGISTER};")
46 configure_file(${INCLUDES_SOURCE_FILE} ${INCLUDES_BINARY_FILE})