summaryrefslogtreecommitdiff
path: root/cmake/scripts/SquirrelIncludes.cmake
blob: 672ad09531ecbc97ed5b739b28a14b764c175ce7 (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
cmake_minimum_required(VERSION 3.5)

if(NOT INCLUDES_SOURCE_FILE)
    message(FATAL_ERROR "Script needs INCLUDES_SOURCE_FILE defined")
endif()
if(NOT INCLUDES_BINARY_FILE)
    message(FATAL_ERROR "Script needs INCLUDES_BINARY_FILE defined")
endif()
if(NOT APILC)
    message(FATAL_ERROR "Script needs APILC defined")
endif()
if(NOT APIUC)
    message(FATAL_ERROR "Script needs APIUC defined")
endif()

set(ARGC 1)
set(ARG_READ NO)

# For MSVC CMake runs this script from a batch file using || to detect errors,
# depending on source path it may quote args, and cause cmd to not understand ||
# and pass it as argument to ourself.
# Read all the arguments given to CMake; we are looking for -- and everything
# that follows, until ||. Those are our api files.
while(ARGC LESS CMAKE_ARGC)
    set(ARG ${CMAKE_ARGV${ARGC}})

    if(ARG STREQUAL "||")
        set(ARG_READ NO)
    endif()

    if(ARG_READ)
        list(APPEND SCRIPT_API_BINARY_FILES "${ARG}")
    endif()

    if(ARG STREQUAL "--")
        set(ARG_READ YES)
    endif()

    math(EXPR ARGC "${ARGC} + 1")
endwhile()

foreach(FILE IN LISTS SCRIPT_API_BINARY_FILES)
    file(STRINGS "${FILE}" LINES REGEX "^void SQ${APIUC}.*_Register\\(Squirrel \\*engine\\)$")
    if(LINES)
        string(REGEX REPLACE ".*api/${APILC}/(.*)" "#include \"\\1\"" FILE "${FILE}")
        list(APPEND SQUIRREL_INCLUDES "${FILE}")
        foreach(LINE IN LISTS LINES)
            if("${LINE}" MATCHES "SQ${APIUC}(List|Controller)_Register")
                continue()
            endif()
            string(REGEX REPLACE "^.*void " "	" LINE "${LINE}")
            string(REGEX REPLACE "Squirrel \\*" "" LINE "${LINE}")
            list(APPEND SQUIRREL_REGISTER "${LINE}")
        endforeach()
    endif()
endforeach()

list(SORT SQUIRREL_INCLUDES)
string(REPLACE ";" "\n" SQUIRREL_INCLUDES "${SQUIRREL_INCLUDES}")

string(REGEX REPLACE "_Register" "0000Register" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
list(SORT SQUIRREL_REGISTER)
string(REGEX REPLACE "0000Register" "_Register" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
string(REPLACE ";" ";\n" SQUIRREL_REGISTER "${SQUIRREL_REGISTER}")
set(SQUIRREL_REGISTER "	SQ${APIUC}List_Register(engine);\n${SQUIRREL_REGISTER};")

configure_file(${INCLUDES_SOURCE_FILE} ${INCLUDES_BINARY_FILE})