-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathEclipseProjectParser.ts
More file actions
824 lines (727 loc) · 32.6 KB
/
EclipseProjectParser.ts
File metadata and controls
824 lines (727 loc) · 32.6 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
import * as fs from 'fs';
import * as NodePath from 'path';
import * as os from 'os';
import * as xml2js from 'xml2js';
import { VirtualFolder } from './EIDETypeDefine';
import { VirtualSource, AbstractProject } from './EIDEProject';
import { isArray } from 'util';
import { ArrayDelRepetition } from '../lib/node-utility/Utility';
import { File } from '../lib/node-utility/File';
import { GlobalEvent } from './GlobalEvents';
export type EclipseProjectType = 'arm' | 'sdcc' | 'riscv' | 'gcc';
export interface EclipseProjectInfo {
name: string;
type: EclipseProjectType;
targets: EclipseProjectTarget[];
virtualSource: VirtualFolder;
sourceEntries: string[];
envs: { [name: string]: string }; // only use to show for user, not use in program
}
export interface EclipseBuilderArgs {
// "level-0", "level-1", "level-2", "level-3", "level-size", "level-size-Oz", "level-fast", "level-debug"
optimization: string;
// "c89", "c90", "c99", "c11", "c17", "c23", "gnu89", "gnu90", "gnu99", "gnu11", "gnu17", "gnu23"
cLanguageStd: string;
// "c++98", "gnu++98", "c++11", "gnu++11", "c++14", "gnu++14", "c++17", "gnu++17", "c++20", "gnu++20", "c++23", "gnu++23"
cppLanguageStd: string;
// Signed Char (-fsigned-char)
signedChar: boolean;
globalArgs: string[];
cIncDirs: string[];
cMacros: string[];
cCompilerArgs: string[];
sIncDirs: string[];
sMacros: string[];
assemblerArgs: string[];
linkerArgs: string[];
linkerLibArgs: string[];
linkerLibSearchDirs: string[];
}
export interface EclipseProjectTarget {
name: string;
excList: string[];
builldArgs: EclipseBuilderArgs;
sourceArgs: { [path: string]: EclipseBuilderArgs };
archName?: string;
linkerScriptPath?: string;
objsOrder: string[];
}
function newEclipseBuilderArgs(): EclipseBuilderArgs {
return {
optimization: 'level-debug',
cLanguageStd: 'c11',
cppLanguageStd: 'c++11',
signedChar: false,
globalArgs: [],
cIncDirs: [],
cMacros: [],
cCompilerArgs: [],
sIncDirs: [],
sMacros: [],
assemblerArgs: [],
linkerArgs: [],
linkerLibArgs: [],
linkerLibSearchDirs: []
};
}
export function formatFilePath(path: string): string {
// %7B..%7D/a.c
// PARENT-2-PROJECT_LOC/nrf_demo/app/main.c
// PARENT-3-PROJECT_LOC/nrf_demo/app/main.c
// "${workspace_loc:/${ProjName}/platform/emdrv/nvm3/inc}"
path = decodeURI(path);
if (/-PROJECT_LOC/.test(path)) {
const mRes = /PARENT-(\d+)-PROJECT_LOC/.exec(path);
if (mRes && mRes.length > 1) {
const pLi: string[] = [];
const pCnt = parseInt(mRes[1]);
for (let i = 0; i < pCnt; i++) pLi.push('..');
const pTxt = pLi.join('/');
path = path
.replace('${' + mRes[0] + '}', pTxt)
.replace(mRes[0], pTxt);
}
}
path = path
.replace('${.}', '.')
.replace('${..}', '..')
.replace('${ProjName}/', '')
.replace('${ProjName}', '.')
.replace('PROJECT_LOC/', '')
.replace('${PROJECT_LOC}/', '');
// remove leading and tailing '"'
if (path.charAt(0) == '"' && path.charAt(path.length - 1) == '"')
path = path.substr(1, path.length - 2);
// conv ${workspace_loc:/xxx} to xxx
if (path.startsWith('${workspace_loc:/') && path.charAt(path.length - 1) == '}')
path = path.substr(17, path.length - 18);
// remove tailing '/'
path = path.replace(/\/+$/, '');
if (path.startsWith('/'))
path = '.' + path;
return path;
}
export async function parseEclipseProject(cprojectPath: string): Promise<EclipseProjectInfo> {
let _prjDom = (await xml2js.parseStringPromise(
fs.readFileSync(NodePath.dirname(cprojectPath) + NodePath.sep + '.project').toString()))['projectDescription'];
let cprjDom = (await xml2js.parseStringPromise(
fs.readFileSync(cprojectPath).toString()))['cproject'];
const cprojectDir = new File(NodePath.dirname(cprojectPath));
const SRC_FILE_FILTER = AbstractProject.getSourceFileFilterWithoutObj().concat(AbstractProject.headerFilter);
const PROJ_INFO: EclipseProjectInfo = {
name: _prjDom.name[0].replace(/\s+/g, '_'),
type: 'gcc',
targets: [],
virtualSource: {
name: VirtualSource.rootName,
files: [],
folders: [],
},
sourceEntries: [],
envs: {}
};
// goto: <storageModule moduleId="org.eclipse.cdt.core.settings">
let node = getChild(toArray(cprjDom.storageModule), m => m.$['moduleId'] == "org.eclipse.cdt.core.settings");
if (!node) throw new Error(`not found: '<storageModule moduleId="org.eclipse.cdt.core.settings">'`);
cprjDom = node;
GlobalEvent.log_info(`[EclipseParser] start parsing ${cprojectPath} ...`);
const root_virtualsrcs: string[] = [];
const root_srcdirs: string[] = [];
const eclipseTargetList: string[] = [];
// parse all project targets
for (const ccfg of toArray(cprjDom['cconfiguration'])) {
// parse all eclipse envs
let eN = getChild(toArray(ccfg['storageModule']), m => m.$['moduleId'] == "org.eclipse.cdt.core.settings");
if (eN && eN.macros) {
toArray(eN.macros[0].stringMacro).forEach(m => {
const key = m.$['name'];
if (key) {
PROJ_INFO.envs[key] = m.$['value'];
}
});
}
// goto: <storageModule moduleId="cdtBuildSystem" version="4.0.0">
let cTarget = getChild(toArray(ccfg['storageModule']), n => n.$['moduleId'] == "cdtBuildSystem");
if (!cTarget || !cTarget['configuration']) continue;
// check cdt version
const cdtVer = (cTarget.$['version'] || 'null').trim();
if (!/^([4-9]|[1-9]\d+)\./.test(cdtVer))
throw new Error(`eclipse project cdt version must >= 4.x.x, but your project is '${cdtVer}' !`);
cTarget = cTarget['configuration'][0];
eclipseTargetList.push(cTarget.$['name']);
const tInfo: EclipseProjectTarget = {
name: cTarget.$['name'].replace(/\s+/g, '_'),
excList: [],
builldArgs: newEclipseBuilderArgs(),
sourceArgs: {},
objsOrder: []
};
PROJ_INFO.targets.push(tInfo);
GlobalEvent.log_info(`[EclipseParser] target: "${cTarget.$['name']}"`);
for (const resourceInfo of toArray(cTarget.folderInfo).concat(toArray(cTarget.fileInfo))) {
const folderPath = formatFilePath(resourceInfo.$['resourcePath'] || '').trim();
const isRootResource = folderPath == '';
let builderArgs = tInfo.builldArgs;
if (folderPath) {
tInfo.sourceArgs[folderPath] = newEclipseBuilderArgs();
builderArgs = tInfo.sourceArgs[folderPath];
}
const toolchainInfo = resourceInfo.toolChain ? resourceInfo.toolChain[0] : resourceInfo;
if (isRootResource && PROJ_INFO.type == 'gcc') {
PROJ_INFO.type = detectProjectType(toolchainInfo) || 'gcc';
}
for (const globOpts of toArray(toolchainInfo['option'])) {
const opVal = parseToolOption(globOpts, PROJ_INFO.type);
if (opVal) {
if (opVal.type == 'linkerScriptPath') {
if (isRootResource)
tInfo.linkerScriptPath = opVal.val.join(',');
} else if (opVal.type == 'archName') {
if (isRootResource)
tInfo.archName = opVal.val[0];
} else if (opVal.type == 'linkerLibSearchDirs') {
if (isRootResource)
builderArgs.linkerLibSearchDirs = opVal.val;
} else if (opVal.type == 'optimization') {
if (isRootResource)
builderArgs.optimization = opVal.val[0];
} else if (opVal.type == 'cLanguageStd') {
if (isRootResource)
builderArgs.cLanguageStd = opVal.val[0];
} else if (opVal.type == 'signedChar') {
if (isRootResource)
builderArgs.signedChar = opVal.val[0] == 'true';
} else if (opVal.type == '<ignored>') {
GlobalEvent.log_info(`[EclipseParser] skip option "${opVal.val[0]}" of path "${folderPath}"`);
} else {
builderArgs.globalArgs = builderArgs.globalArgs.concat(opVal.val);
}
} else {
GlobalEvent.log_info(
`[EclipseParser] unknown option "${globOpts.$['name'] || globOpts.$['id']}" of path "${folderPath}"`);
}
}
for (const toolOpts of toArray(toolchainInfo['tool'])) {
const toolOptName: string = toolOpts.$['name'] || '';
// for c/c++
if (toolOptName.includes('C Compiler')) {
toArray(toolOpts.option).forEach(op => {
const opVal = parseToolOption(op, PROJ_INFO.type);
if (opVal) {
if (opVal.type == 'linkerScriptPath') {
if (isRootResource)
tInfo.linkerScriptPath = opVal.val.join(',');
} else if (opVal.type == 'archName') {
if (isRootResource)
tInfo.archName = opVal.val[0];
} else if (opVal.type == 'definedSymbols') {
builderArgs.cMacros = builderArgs.cMacros.concat(opVal.val);
} else if (opVal.type == 'includePath') {
builderArgs.cIncDirs = builderArgs.cIncDirs.concat(opVal.val);
} else if (opVal.type == 'optimization') {
builderArgs.optimization = opVal.val[0];
} else if (opVal.type == 'cLanguageStd') {
builderArgs.cLanguageStd = opVal.val[0];
} else if (opVal.type == 'signedChar') {
builderArgs.signedChar = opVal.val[0] == 'true';
} else if (opVal.type == '<ignored>') {
GlobalEvent.log_info(`[EclipseParser] skip option "${opVal.val[0]}" of path "${folderPath}"`);
} else {
builderArgs.cCompilerArgs = builderArgs.cCompilerArgs.concat(opVal.val);
}
} else {
GlobalEvent.log_info(
`[EclipseParser] unknown option "${op.$['name'] || op.$['id']}" of path "${folderPath}"`);
}
});
}
// for asm
else if (toolOptName.includes('Assembler')) {
toArray(toolOpts.option).forEach(op => {
const opVal = parseToolOption(op, PROJ_INFO.type);
if (opVal) {
if (opVal.type == 'definedSymbols') {
builderArgs.sMacros = builderArgs.sMacros.concat(opVal.val);
} else if (opVal.type == 'includePath') {
builderArgs.sIncDirs = builderArgs.sIncDirs.concat(opVal.val);
} else if (opVal.type == '<ignored>') {
GlobalEvent.log_info(`[EclipseParser] skip option "${opVal.val[0]}" of path "${folderPath}"`);
} else {
builderArgs.assemblerArgs = builderArgs.assemblerArgs.concat(opVal.val);
}
} else {
GlobalEvent.log_info(
`[EclipseParser] unknown option "${op.$['name'] || op.$['id']}" of path "${folderPath}"`);
}
});
}
// for linker
else if (toolOptName.includes('C Linker')) {
toArray(toolOpts.option).forEach(op => {
const opVal = parseToolOption(op, PROJ_INFO.type);
if (opVal) {
if (opVal.type == 'linkerScriptPath') {
if (isRootResource)
tInfo.linkerScriptPath = opVal.val.join(',');
} else if (opVal.type == 'archName') {
if (isRootResource)
tInfo.archName = opVal.val[0];
} else if (opVal.type == 'objsOrder') {
if (isRootResource)
tInfo.objsOrder = tInfo.objsOrder.concat(opVal.val);
} else if (opVal.type == 'libs' || opVal.type == 'userObjs') {
builderArgs.linkerLibArgs = builderArgs.linkerLibArgs.concat(opVal.val);
} else if (opVal.type == 'linkerLibSearchDirs') {
builderArgs.linkerLibSearchDirs = builderArgs.linkerLibSearchDirs.concat(opVal.val);
} else if (opVal.type == '<ignored>') {
GlobalEvent.log_info(`[EclipseParser] skip option "${opVal.val[0]}" of path "${folderPath}"`);
} else {
builderArgs.linkerArgs = builderArgs.linkerArgs.concat(opVal.val);
}
} else {
GlobalEvent.log_info(
`[EclipseParser] unknown option "${op.$['name'] || op.$['id']}" of path "${folderPath}"`);
}
});
}
else {
// ignore
}
}
}
if (Array.isArray(cTarget.sourceEntries) && typeof cTarget.sourceEntries[0] == 'object') {
toArray(cTarget.sourceEntries[0].entry).forEach(e => {
//<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="src" />
if (<string>e.$['kind'] == 'sourcePath') {
const srcdir = formatFilePath(e.$['name']);
if (srcdir == '.' || srcdir == '') {
const srcs = cprojectDir.GetList(SRC_FILE_FILTER, [/^[^\.].+/]);
srcs.forEach(src => {
if (src.IsFile()) {
root_virtualsrcs.push(src.name);
} else {
root_srcdirs.push(src.name);
}
});
} else {
root_srcdirs.push(srcdir);
}
}
(<string>e.$['excluding'] || '')
.split('|').filter(s => s.trim() != '')
.forEach(p => {
tInfo.excList.push(formatFilePath(p));
});
});
}
}
// setup root sources
ArrayDelRepetition(root_virtualsrcs).forEach(srcpath => {
PROJ_INFO.virtualSource.files.push({ path: srcpath });
});
ArrayDelRepetition(root_srcdirs).forEach(srcdir => {
if (eclipseTargetList.includes(srcdir)) return; // skip eclipse build dir
PROJ_INFO.sourceEntries.push(srcdir);
});
const getVirtualFolder = (rePath: string) => {
rePath = rePath.trim()
.replace(/^\//, '')
.replace(/\/+$/, '')
.replace(/^\.\/?/, '');
if (rePath == '' || rePath == '.')
return PROJ_INFO.virtualSource;
let curFolder = PROJ_INFO.virtualSource;
let pathList = rePath.split(/\\|\//).reverse();
while (pathList.length > 0) {
let p = pathList.pop();
if (!p) continue;
const i = curFolder.folders.findIndex(d => d.name == p);
if (i != -1) {
curFolder = curFolder.folders[i];
} else {
const nDir: VirtualFolder = {
name: p,
files: [],
folders: [],
};
curFolder.folders.push(nDir);
curFolder = nDir;
}
}
return curFolder;
};
const virtualRootList: string[] = [];
const virtualRootMap: { [vpath: string]: string } = {};
// parse external source
if (_prjDom.linkedResources && typeof _prjDom.linkedResources[0] == 'object') {
toArray(_prjDom.linkedResources[0].link).forEach(link => {
if (link.type[0] == '1') { // virtual file
const vPath = link.name[0].trim();
if (vPath == '') return;
const vFolder = getVirtualFolder(NodePath.dirname(vPath));
const locations = link.locationURI || link.location;
if (!Array.isArray(locations)) return;
vFolder.files.push({ path: formatFilePath(locations[0]) });
} else if (link.type[0] == '2') { // virtual folder
const vpath = link.name[0].trim();
const locations: string = link.locationURI || link.location;
if (!Array.isArray(locations)) return;
const location: string = locations[0];
if (typeof location != 'string') return;
virtualRootList.push(vpath);
const rootdirpath_ = formatFilePath(location);
let rootdirfullpath = rootdirpath_;
if (!File.isAbsolute(rootdirpath_)) rootdirfullpath = `${cprojectDir.path}/${rootdirpath_}`;
let vFolder = getVirtualFolder(vpath); // add this folder
if (File.IsDir(rootdirfullpath)) {
virtualRootMap[vpath] = File.ToUnixPath(rootdirpath_);
const files = new File(rootdirfullpath).GetAll(SRC_FILE_FILTER, File.EXCLUDE_ALL_FILTER);
const srcRootDir = new File(rootdirfullpath);
files.forEach(f => {
let subvpath = vpath;
const dirname = srcRootDir.ToRelativePath(f.dir);
if (dirname && dirname != '.') subvpath += '/' + dirname;
vFolder = getVirtualFolder(subvpath);
vFolder.files.push({ path: cprojectDir.ToRelativePath(f.path) || f.path });
});
}
}
});
}
const completeVirtualPaths = (pathlist: string[]): string[] => {
return pathlist.map(p => {
if (virtualRootList.includes(p) || virtualRootList.some(e => p.startsWith(e + '/'))) {
return `${VirtualSource.rootName}/${p}`;
} else {
return p;
}
});
};
const resolveVirtualPaths = (pathlist: string[]): string[] => {
return pathlist.map(p => {
for (const rootpath of virtualRootList) {
if ((rootpath == p || p.startsWith(rootpath + '/')) && virtualRootMap[rootpath]) {
return p.replace(rootpath, virtualRootMap[rootpath]);
}
}
return p;
});
};
// del repeat args for every targets
for (const target of PROJ_INFO.targets) {
// add prefix for virtual exclude paths
target.excList = completeVirtualPaths(ArrayDelRepetition(target.excList));
// resolve virtual include paths
target.builldArgs.cIncDirs = resolveVirtualPaths(target.builldArgs.cIncDirs);
target.builldArgs.sIncDirs = resolveVirtualPaths(target.builldArgs.sIncDirs);
for (const key in target.builldArgs) {
const obj: any = target.builldArgs;
if (isArray(obj[key])) {
obj[key] = ArrayDelRepetition(obj[key]);
}
}
}
GlobalEvent.log_info(`[EclipseParser] parse done.`);
return PROJ_INFO;
}
function parseToolOption(optionObj: any, prjtype: EclipseProjectType): { type: string, val: string[] } | undefined {
if (!optionObj.$['id'])
return { type: '<ignored>', val: [optionObj.$['name'] || 'noid'] };
//--
const VALUE_ID: string = optionObj.$['id'];
const VALUE_NAME: string = optionObj.$['name'] || '';
const VALUE_VALS: string[] = [];
const VALUE_TYPE: string | undefined = optionObj.$['valueType'];
const IGNORED_VAL = { type: '<ignored>', val: [VALUE_NAME || VALUE_ID] };
const makeResult = (value: string | string[], typ?: string): { type: string, val: string[] } | undefined => {
if (value == '') return undefined;
if (isArray(value) && value.length == 0) return undefined;
return {
type: typ || VALUE_TYPE || '',
val: isArray(value) ? value : [value]
};
};
const formatArgs = (fmt: string, arg: string): string => {
if (fmt.includes('[option]'))
return fmt.replace('[option]', arg);
return fmt + arg;
}
const cLangStds = [ "c89", "c90", "c99", "c11", "c17", "c23", "gnu89", "gnu90", "gnu99", "gnu11", "gnu17", "gnu23" ];
// ----
// parse special type values
// ----
if (VALUE_TYPE == 'definedSymbols') {
toArray(optionObj.listOptionValue)
.forEach(item => VALUE_VALS.push(item.$['value']));
return makeResult(VALUE_VALS, 'definedSymbols');
} else if (VALUE_TYPE == 'includePath') {
toArray(optionObj.listOptionValue).forEach(item => {
let p = formatFilePath(item.$['value']);
if (p == '..') p = '.';
if (p.startsWith('../')) p = p.substr(3); // for eclipse, include path is base 'Debug' folder
VALUE_VALS.push(p);
});
return makeResult(VALUE_VALS, 'includePath');
} else if (VALUE_TYPE == 'libPaths') {
toArray(optionObj.listOptionValue).forEach(item => {
let p = formatFilePath(item.$['value']);
if (p == '..') p = '.';
if (p.startsWith('../')) p = p.substr(3); // for eclipse, include path is base 'Debug' folder
VALUE_VALS.push(p);
});
return makeResult(VALUE_VALS, 'linkerLibSearchDirs');
} else if (VALUE_TYPE == 'libs') {
toArray(optionObj.listOptionValue)
.forEach(item => VALUE_VALS.push(`-l${item.$['value']}`));
return makeResult(VALUE_VALS, 'libs');
} else if (VALUE_TYPE == 'userObjs') {
toArray(optionObj.listOptionValue)
.forEach(item => VALUE_VALS.push(formatFilePath(item.$['value'])));
return makeResult(VALUE_VALS, 'userObjs');
}
// ----
// parse generic type values
// ----
if (VALUE_TYPE == 'boolean') {
VALUE_VALS.push(optionObj.$['value'] || 'false');
} else if (VALUE_TYPE == 'stringList') {
toArray(optionObj.listOptionValue)
.forEach(item => VALUE_VALS.push(formatFilePath(item.$['value'])));
} else { // string or enums
const val = optionObj.$['value']?.trim() || '';
VALUE_VALS.push(formatFilePath(val));
}
if (VALUE_VALS.length == 0)
return IGNORED_VAL;
// ----
// Eclipse Generic
// ----
if (VALUE_NAME.includes('(-D)') || /Defined symbols/i.test(VALUE_NAME)) {
const li: string[] = [];
toArray(optionObj.listOptionValue).forEach(item => li.push(item.$['value']));
return makeResult(li, 'definedSymbols');
}
if (VALUE_NAME.includes('(-I)') || VALUE_ID.includes('include.paths')) {
const li: string[] = [];
toArray(optionObj.listOptionValue).forEach(item => {
let p = formatFilePath(item.$['value']);
if (p == '..') p = '.';
if (p.startsWith('../')) p = p.substr(3); // for eclipse, include path is base 'Debug' folder
li.push(p);
});
return makeResult(li, 'includePath');
}
// ilg.gnuarmeclipse.managedbuild.cross.option.c.linker.scriptfile
if (VALUE_ID.includes('linker.script')) {
return makeResult(VALUE_VALS, 'linkerScriptPath');
}
// ilg.gnuarmeclipse.managedbuild.cross.option.optimization.level = optimization.level.debug
if (VALUE_ID.includes('optimization.level')) {
if (/level\.none/.test(VALUE_VALS[0])) {
return makeResult(`level-0`, 'optimization');
} else if (/level\.debug/.test(VALUE_VALS[0])) {
return makeResult(`level-debug`, 'optimization');
} else if (/level\.optimize/.test(VALUE_VALS[0])) {
return makeResult(`level-1`, 'optimization');
} else if (/level\.more/.test(VALUE_VALS[0])) {
return makeResult(`level-2`, 'optimization');
} else if (/level\.most/.test(VALUE_VALS[0])) {
return makeResult(`level-3`, 'optimization');
} else if (/level\.size/.test(VALUE_VALS[0])) {
return makeResult(`level-size`, 'optimization');
} else {
return IGNORED_VAL;
}
}
// linker.nostdlibs = false
if (VALUE_ID.includes('linker.nostdlibs')) {
if (VALUE_VALS[0] === 'true')
return makeResult(`-nostdlib`, 'string');
return IGNORED_VAL;
}
// arm.target.family
if (VALUE_ID.includes('arm.target.family')) {
const m = /\.(cortex-m\w+)/.exec(VALUE_VALS[0]);
if (m && m.length > 1)
return makeResult(m[1], 'archName');
return IGNORED_VAL;
}
// option.c.linker.paths
if (VALUE_ID.includes('option.c.linker.paths')) {
toArray(optionObj.listOptionValue).forEach(item => {
let p = formatFilePath(item.$['value']);
if (p == '..') p = '.';
if (p.startsWith('../')) p = p.substr(3); // for eclipse, include path is base 'Debug' folder
VALUE_VALS.push(p);
});
return makeResult(VALUE_VALS, 'linkerLibSearchDirs');
}
// optimization.signedchar
if (VALUE_ID.includes('optimization.signedchar')) {
if (VALUE_VALS[0] === 'true')
return makeResult('true', 'signedChar');
return IGNORED_VAL;
}
// option.warnings.extrawarn
if (VALUE_ID.includes('option.warnings.extrawarn')) {
if (VALUE_VALS[0] === 'true')
return makeResult(`-Wextra`, 'string');
return IGNORED_VAL;
}
// option.warnings.allwarn
if (VALUE_ID.includes('option.warnings.allwarn')) {
if (VALUE_VALS[0] === 'true')
return makeResult(`-Wall`, 'string');
return IGNORED_VAL;
}
// option.c.compiler.otherwarnings
if (VALUE_ID.includes('option.c.compiler.otherwarnings')) {
if (VALUE_VALS[0])
return makeResult(VALUE_VALS[0], 'string');
return IGNORED_VAL;
}
// option.c.linker.nostart
if (VALUE_ID.includes('option.c.linker.nostart')) {
if (VALUE_VALS[0] === 'true')
return makeResult(`-nostartfiles`, 'string');
return IGNORED_VAL;
}
// ignore these options
if (VALUE_NAME.includes('-fno-rtti') ||
VALUE_NAME.includes('-fno-use-cxa-atexit') ||
VALUE_NAME.includes('-fno-threadsafe-statics') ||
VALUE_NAME.includes('-ffunction-sections') ||
VALUE_NAME.includes('-fdata-sections') ||
VALUE_NAME.includes('--gc-sections')) {
return IGNORED_VAL;
}
// ----
// silabs
// ----
// <Short enums (-fshort-enums)> = false
if (VALUE_ID.includes('optimization.shortenums')) {
const v = VALUE_VALS[0];
if (v === "true")
return makeResult(`-fshort-enums`, 'string');
return IGNORED_VAL;
}
// <Language Standard> = option.std.gnu99
// compiler.misc.dialect = com.silabs.ide.si32.gcc.cdt.managedbuild.tool.gnu.c.compiler.misc.dialect.c99
if (/Language (?:Standard|Dialect)/i.test(VALUE_NAME)) {
let m = /std\.(\w+)/.exec(VALUE_VALS[0]);
if (m && m.length > 1) {
const langStd = m[1];
if (cLangStds.includes(langStd))
return makeResult(langStd, 'cLanguageStd');
}
m = /dialect\.(\w+)/.exec(VALUE_VALS[0]);
if (m && m.length > 1) {
const langStd = m[1];
if (cLangStds.includes(langStd))
return makeResult(langStd, 'cLanguageStd');
}
return IGNORED_VAL;
}
// <Linker input ordering> = ./platform/Device/SiliconLabs/EFR32BG22/Source/GCC/startup_efr32bg22.o;
if (VALUE_NAME.trim() == 'Linker input ordering') {
const paths = VALUE_VALS[0].split(';')
.filter(p => p.trim() !== '')
.map(p => '**/' + formatFilePath(p).replace(/^\.\//, ''));
if (paths.length == 0)
return IGNORED_VAL;
return makeResult(paths.slice(0, Math.min(5, paths.length)), 'objsOrder');
}
if (prjtype == 'arm') {
if (VALUE_NAME.includes('--specs=nano.specs') ||
VALUE_NAME.includes('--specs=nosys.specs'))
return IGNORED_VAL;
}
// ----
// STM32CubeIDE
// ----
// value="STM32F407VGTx" valueType="string"
if (VALUE_ID.includes('com.st.stm32cube.ide.mcu.gnu.managedbuild.option.target_mcu')) {
const mcuName = VALUE_VALS[0].toLowerCase();
if (/stm32f0/.test(mcuName))
return makeResult('cortex-m0', 'archName');
else if (/stm32(g0|c0|l0|u0)/.test(mcuName))
return makeResult('cortex-m0+', 'archName');
else if (/stm32(f1|f2|l1)/.test(mcuName))
return makeResult('cortex-m3', 'archName');
else if (/stm32(f4|g4|f3|l4|wb[0-9]|wl)/.test(mcuName))
return makeResult('cortex-m4', 'archName');
else if (/stm32(h5|u5|u3|l5|wba)/.test(mcuName))
return makeResult('cortex-m33', 'archName');
else if (/stm32(h7|f7)/.test(mcuName))
return makeResult('cortex-m7', 'archName');
return IGNORED_VAL;
}
// value="${workspace_loc:/${ProjName}/STM32F407VGTX_FLASH.ld}" valueType="string"
if (VALUE_ID.includes('com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script')) {
if (VALUE_VALS[0])
return makeResult(formatFilePath(VALUE_VALS[0]), 'linkerScriptPath');
}
// ----
// match (-xxx) options by types
// ----
if (VALUE_TYPE == 'boolean') {
if (VALUE_VALS[0] == 'true') {
const mRes = /\((\-.+)\)/.exec(VALUE_NAME);
if (mRes && mRes.length > 1) {
return makeResult(mRes[1]);
}
}
}
if (VALUE_TYPE == 'string') {
const mRes = /\((\-.+)\)/.exec(VALUE_NAME);
if (mRes && mRes.length > 1) {
const fmt = mRes[1];
const arg = formatArgs(fmt, formatFilePath(VALUE_VALS[0]));
return makeResult(arg);
} else if (VALUE_VALS[0].startsWith('-')) {
return makeResult(VALUE_VALS[0]);
}
}
if (VALUE_TYPE == 'stringList') {
const mRes = /\((\-.+)\)/.exec(VALUE_NAME);
if (mRes && mRes.length > 1) {
const fmt = mRes[1];
const li: string[] = [];
toArray(optionObj.listOptionValue)
.forEach(item => li.push(formatArgs(fmt, formatFilePath(item.$['value']))));
return makeResult(li);
} else {
const li: string[] = [];
toArray(optionObj.listOptionValue)
.forEach(item => li.push(formatFilePath(item.$['value'])));
return makeResult(li);
}
}
return IGNORED_VAL;
}
function toArray(obj: any): any[] {
if (obj == undefined || obj == null) return [];
if (!isArray(obj)) return [obj];
return obj;
}
function detectProjectType(toolChain: any): EclipseProjectType | undefined {
const toolId = toolChain.$['id'];
const toolName = toolChain.$['name'] || toolId;
// <toolChain id="ilg.gnuarmeclipse.managedbuild.cross.toolchain.elf.debug.1201710416" name="ARM Cross GCC"
if (/\bARM\b/.test(toolName) || /gnuarmeclipse/.test(toolId)) {
return 'arm';
}
// <toolChain id="ilg.gnumcueclipse.managedbuild.cross.riscv.toolchain.elf.release.231146001" name="RISC-V Cross GCC…
if (/\b(RISCV|RISC-V)\b/.test(toolName) || /\.riscv\./.test(toolId)) {
return 'riscv';
}
// <toolChain errorParsers="" id="cdt.managedbuild.toolchain.sdcc.exe.release.1956586716" name="SDCC Tool Chain"
if (/\bSDCC\b/.test(toolName) || /\.sdcc\./.test(toolId)) {
return 'sdcc';
}
}
function getChild(chLi: any[], filter: (node: any) => boolean): any {
const i = chLi.findIndex(filter);
if (i == -1) return undefined;
return chLi[i];
}