forked from Kode/Kinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_single_header_libs.js
144 lines (128 loc) · 4.16 KB
/
create_single_header_libs.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require('fs');
const path = require('path');
const headers = {};
function miniPreprocessor(source) {
const normal = 0;
const lineStart = 1;
const pragma = 2;
let mode = lineStart;
let currentPragma = null;
let processed = '';
for (let i = 0; i < source.length; ++i) {
if (mode === normal) {
if (source.charAt(i) === '\n' || source.charAt(i) === '\r') {
mode = lineStart;
}
processed += source.charAt(i);
}
else if (mode === lineStart) {
if (source.charAt(i) === '#') {
currentPragma = '';
mode = pragma;
}
else if (source.charAt(i) === '\n' || source.charAt(i) === '\r') {
processed += source.charAt(i);
mode = lineStart;
}
else {
processed += source.charAt(i);
mode = normal;
}
}
else if (mode === pragma) {
if (source.charAt(i) === '\n' || source.charAt(i) === '\r') {
if (currentPragma.startsWith('include')) {
let start = currentPragma.indexOf('<');
if (start < 0) {
start = currentPragma.indexOf('"');
}
let end = currentPragma.lastIndexOf('>');
if (end < 0) {
end = currentPragma.lastIndexOf('"');
}
if (start >= 0 && end >= 0 && currentPragma.length > start + 1) {
const headerPath = currentPragma.substring(start + 1, end);
if (currentPragma.substring(start + 1).startsWith('kinc')) {
let filePath = null;
if (headerPath.includes('FileReaderImpl') || headerPath.includes('Android')) {
if (!headers[headerPath]) {
headers[headerPath] = true;
processed += '#' + currentPragma + source.charAt(i);
}
}
else {
if (headerPath.startsWith('kinc/backend')) {
filePath = path.resolve('Backends', 'System', 'Microsoft', 'Sources', headerPath);
}
else {
filePath = path.resolve('Sources', headerPath);
}
if (!headers[headerPath]) {
headers[headerPath] = true;
let header = fs.readFileSync(filePath, {encoding: 'utf8'});
console.log('Preprocessing ' + filePath);
header = miniPreprocessor(header);
processed += header;
}
}
}
else {
if (!headers[headerPath]) {
headers[headerPath] = true;
processed += '#' + currentPragma + source.charAt(i);
}
}
}
else {
processed += '#' + currentPragma + source.charAt(i);
}
}
else if (currentPragma.startsWith('pragma once')) {
// ignore
}
else {
processed += '#' + currentPragma + source.charAt(i);
}
mode = lineStart;
}
else {
currentPragma += source.charAt(i);
}
}
else {
throw 'Unknown mode';
}
}
return processed;
}
function findHeader(headerPath) {
const cachedHeader = headers[headerPath];
if (cachedHeader) {
return cachedHeader;
}
const filePath = path.resolve('Sources', headerPath);
let header = fs.readFileSync(filePath, {encoding: 'utf8'});
header = header.replace('#pragma once', '');
for (const headerPath in headers) {
header = header.replace('#include <' + headerPath + '>', '');
}
header = header.trim();
headers[headerPath] = header;
return header;
}
function insertHeaders(file, headerPaths) {
for (const headerPath of headerPaths) {
file = file.replace('#include <' + headerPath + '>', findHeader(headerPath));
}
return file;
}
const audio2_header = fs.readFileSync(path.resolve('Sources', 'kinc', 'audio2', 'audio.h'), {encoding: 'utf8'});
let lib = '#pragma once\n' + miniPreprocessor(audio2_header);
let windows_backend = fs.readFileSync(path.resolve('Backends', 'Audio2', 'WASAPI', 'Sources', 'kinc', 'backend', 'wasapi.c'), {encoding: 'utf8'});
windows_backend = windows_backend.replace('#include <kinc/audio2/audio.h>', '');
windows_backend = miniPreprocessor(windows_backend);
lib = lib.replace('// BACKENDS-PLACEHOLDER', '#ifdef KINC_WINDOWS\n' + windows_backend + '\n#endif');
if (!fs.existsSync('single_header_libs')) {
fs.mkdirSync('single_header_libs');
}
fs.writeFileSync(path.resolve('single_header_libs', 'kinc_audio2.h'), lib, {encoding: 'utf8'});