-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeSingleHeader.cmake
115 lines (82 loc) · 3.05 KB
/
makeSingleHeader.cmake
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
function(getLocalIncludes retval currentPath)
file(STRINGS "${currentPath}" lines REGEX "^#include \".*\"")
cmake_path(GET currentPath PARENT_PATH currentFolder)
foreach(line ${lines})
string(REGEX MATCH "\".*\"" includeStatement ${line})
string(REPLACE "\"" "" includeStatement ${includeStatement})
cmake_path(SET includePath NORMALIZE "${currentFolder}/${includeStatement}")
list(APPEND retvals ${includePath})
endforeach()
set(${retval} ${retvals} PARENT_SCOPE)
endfunction()
function(getAllFiles retval initialPath)
set(fileList "${initialPath}")
set(todoList "${initialPath}")
list(LENGTH todoList todoLength)
while(NOT (${todoLength} EQUAL 0))
list(POP_FRONT todoList currentPath)
set(includesFromFile "")
getLocalIncludes(includesFromFile ${currentPath})
foreach(file ${includesFromFile})
if(NOT (${file} IN_LIST fileList))
list(APPEND todoList ${file})
list(APPEND fileList ${file})
endif()
endforeach()
list(LENGTH todoList todoLength)
endwhile()
set(${retval} ${fileList} PARENT_SCOPE)
endfunction()
function(sortByNeededIncludes retval files)
list(LENGTH files fileCount)
while(NOT (fileCount EQUAL 0))
foreach(file ${files})
set(includesFromFile "")
getLocalIncludes(includesFromFile ${file})
foreach(include ${includesFromFile})
if(${include} IN_LIST sortedFilesList)
list(REMOVE_ITEM includesFromFile ${include})
endif()
endforeach()
list(LENGTH includesFromFile unaddedDependencies)
if(unaddedDependencies EQUAL 0)
set(next ${file})
break()
endif()
endforeach()
list(REMOVE_ITEM files ${next})
list(APPEND sortedFilesList ${next})
math(EXPR fileCount "${fileCount} -1")
endwhile()
set(${retval} ${sortedFilesList} PARENT_SCOPE)
endfunction()
function(makeSingleHeader rootDir initialFile targetFileName desiredOrder)
getAllFiles(allFiles "${rootDir}/${initialFile}")
list(SORT allFiles COMPARE FILE_BASENAME)
set(rest ${allFiles})
foreach(orderingTerm ${desiredOrder})
set(yesses ${rest})
list(FILTER yesses INCLUDE REGEX "^${rootDir}/${orderingTerm}")
list(APPEND presorted ${yesses})
list(FILTER rest EXCLUDE REGEX "^${rootDir}/${orderingTerm}")
endforeach()
list(APPEND presorted ${rest})
sortByNeededIncludes(sortedFiles "${presorted}")
file(WRITE ${targetFileName} "#pragma once \n")
foreach(header ${sortedFiles})
file(STRINGS ${header} lines)
cmake_path(RELATIVE_PATH header BASE_DIRECTORY "${rootDir}" OUTPUT_VARIABLE printHeaderName)
file(APPEND ${targetFileName} "\n")
file(APPEND ${targetFileName} "//////////////////////////////////////////\n")
file(APPEND ${targetFileName} "//${printHeaderName}\n")
file(APPEND ${targetFileName} "//////////////////////////////////////////\n")
file(APPEND ${targetFileName} "\n")
foreach(line ${lines})
string(FIND "${line}" "#include \"" includeLocation)
string(FIND "${line}" "#pragma once" pragmaLocation)
if((${includeLocation} EQUAL -1) AND (${pragmaLocation} EQUAL -1))
file(APPEND ${targetFileName} "${line}\n")
endif()
endforeach()
endforeach()
endfunction()