-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiledbtool.pl
executable file
·613 lines (472 loc) · 14.3 KB
/
compiledbtool.pl
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
#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(GetOptionsFromArray);
use Term::ANSIColor;
use File::Spec::Functions qw(canonpath file_name_is_absolute);
use File::Basename;
use List::MoreUtils qw(first_index);
eval 'use JSON';
if ($@) {
die "Cannot load JSON library ($@), install it: \$ cpan JSON";
}
my %COMMANDS_MAP = (
'help' => \&helpcommand,
'addflag' => \&addflagcommand,
'rmflag' => \&rmflagcommand,
'lsflag' => \&lsflagcommand,
'abspath' => \&abspathcommand,
'join' => \&joincommand,
'dumpcmd' => \&dumpcmdcommand,
'gcccrosscompile' => \&gcccrosscompilecommand,
# 'globalflags' => \&globalflagscommand
);
my $COMMANDS = "
help - get help for a specific command
addflag - add a compile flag to the database
rmflag - remove a compile flag from the database
lsflag - lookup flags matching a regex
abspath - make flags with paths absolute
join - join multiple compile databases together
dumpcmd - dump the command for files matching a regex
gcccrosscompile - add flags for cross compilation
";
# globalflags - create a global set of flags
if(not @ARGV) {
usage();
exit 1;
} else {
my $cmd = shift @ARGV;
if (defined($COMMANDS_MAP{$cmd})) {
&{$COMMANDS_MAP{$cmd}}(@ARGV);
exit 0;
} else {
print STDERR colored("Unknown command: $cmd\n", "red")
unless($cmd eq "-h" or $cmd eq "--help");
usage();
exit 1;
}
}
sub is_help {
for(@_) {
if ($_ eq "-h" or $_ eq "--help") {
return 1;
}
}
return 0;
}
sub usage {
print STDERR "
Clang compilation database manipulation tool
Can be used to modify a compile_commands.json database
Usage:
compiledbtool.pl <command> [<args to command..>]
Commands:
$COMMANDS
";
}
sub helpcommand {
my @args = @_;
if(@args < 1 or is_help(@args)) {
print STDERR "
Usage:
compiledbtool.pl help <command>
Commands:
$COMMANDS
";
} elsif (defined($COMMANDS_MAP{$args[0]})) {
&{$COMMANDS_MAP{$args[0]}}('--help');
exit 0;
}
}
sub addflagcommand {
my @args = @_;
if (is_help(@args)) {
print STDERR "
Add the flag to all files in the database.
Usage:
compiledbtool.pl addflag [<options>] <flag>
Options:
None so far...
";
exit 0;
}
die "A flag is required" if (@args < 1);
my $flag = $args[0];
my $cdb = readcdb();
for my $command (@$cdb) {
my $curfile = $command->{"file"};
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
print STDERR colored($curfile, "yellow"), ": Adding $flag\n";
push @{$command->{"arguments"}}, $flag;
}
writecdb($cdb);
}
sub rmflagcommand {
my @args = @_;
if (is_help(@args)) {
print STDERR "
Remove all flags matching <regex>
Usage:
compiledbtool.pl rmflag [<options>] <regex>
Options:
None so far...
";
exit 0;
}
die "A regex is required" if (@args < 1);
my $regex = qr/($args[0])/;
my $cdb = readcdb();
for my $command (@$cdb) {
my $curfile = $command->{"file"};
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
my @remlist;
for my $idx (0 .. @{$command->{"arguments"}} - 1) {
if($command->{"arguments"}[$idx] =~ $regex) {
print STDERR colored($curfile, "yellow"), ": Removing $1\n";
push @remlist, $idx;
}
}
# Must do this in reverse so indexs don't get invalidated
for my $idx (reverse @remlist) {
splice @{$command->{"arguments"}}, $idx, 1;
}
}
writecdb($cdb);
}
sub lsflagcommand {
my @args = @_;
if (is_help(@args)) {
print STDERR "
List all flags matching <regex>
Usage:
compiledbtool.pl lsflag [<options>] <regex>
Options:
--before N - Number of arguments to display before matched argument
--after N - Number of arguments to display after matched argument
";
exit 0;
}
die "A regex is required" if (@args < 1);
my $regex = qr/$args[0]/;
shift @args;
# TODO: implement this
my $nbefore = '0';
my $nafter = '0';
GetOptionsFromArray(\@args,
'before=i' => \$nbefore,
'after=i' => \$nafter
) or die "Failed to parse options";
my $cdb = readcdb();
for my $command (@$cdb) {
my $curfile = $command->{"file"};
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
for my $arg (@{$command->{"arguments"}}) {
if($arg =~ $regex) {
print colored($curfile, "yellow"), ": $arg\n";
}
}
}
}
sub abspathcommand {
my @args = @_;
if (is_help(@_)) {
print STDERR "
Change relative paths to absolute paths.
if --working-directory is given don't modify flags but just
add -working-directory for all files.
Usage:
compiledbtool.pl abspath [<options>]
Options:
--working-directory - use Clang's -working-directory flag
";
exit 0;
}
my $useworkdir = 0;
GetOptionsFromArray(\@args,
'working-directory' => \$useworkdir
) or die "Failed to parse options";
my $cdb = readcdb();
for my $command (@$cdb) {
my $curfile = $command->{"file"};
my $curdir = $command->{"directory"};
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
if($useworkdir) {
push @{$command->{"arguments"}}, "-working-directory";
push @{$command->{"arguments"}}, $curdir;
print STDERR colored($curfile, "yellow"), ": Adding -working-directory $curdir\n";
} else {
my $next_is_path = 0;
for my $idx (0 .. @{$command->{"arguments"}} - 1) {
if($next_is_path) {
my $path = $command->{"arguments"}[$idx];
if(!file_name_is_absolute($path)) {
$command->{"arguments"}[$idx] = canonpath($curdir . "/" . $path);
}
$next_is_path = 0;
} else {
my $arg = $command->{"arguments"}[$idx];
my $flag;
if($arg =~ /^-I(.*)/) {
$flag = "-I";
} elsif($arg =~ /^-isystem(.*)/) {
$flag = "-isystem";
} elsif($arg =~ /^-iquote(.*)/) {
$flag = "-iquote";
} elsif($arg =~ /^--sysroot=(.*)/) {
$flag = "--sysroot";
} elsif($arg =~ /^-include(.*)/) {
$flag = "-include";
} else {
next;
}
if(length($1) > 0) {
if(!file_name_is_absolute($1)) {
$command->{"arguments"}[$idx] = $flag . canonpath($curdir . "/" . $1);
}
} else {
$next_is_path = 1;
}
print STDERR colored($curfile, "yellow"), ": Making $flag absolute\n";
}
}
}
}
writecdb($cdb);
}
sub joincommand {
my @args = @_;
if (is_help(@_)) {
print STDERR "
Join multiple compile_commands.json together
The last filename is the output.
Usage:
compiledbtool.pl join [<options>] <cdb1> <cdb2> [<cdb3> ...] <cdbout>
Options:
None so far...
";
exit 0;
}
if(@args < 2) {
die "Specify atleast 2 files";
}
my @joinedcdb;
for my $cdbfile (@args[0..-2]) {
push @joinedcdb, @{readcdb($cdbfile)};
}
if(-f $args[-1]) {
push @joinedcdb, @{readcdb($args[-1])};
}
writecdb(\@joinedcdb);
}
sub globalflagscommand {
my @args = @_;
if (is_help(@_)) {
print STDERR "
Find all compile flags in database
Usage:
compiledbtool.pl globalflags
Options:
None so far...
";
exit 0;
}
my %flaghash;
my $cdb = readcdb();
for my $command (@$cdb) {
my $flags = $command->{"arguments"};
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
for my $flag (@$flags) {
if (!defined($flaghash{$flag})) {
$flaghash{$flag} = 0;
}
$flaghash{$flag} += 1;
}
}
for my $flag (keys %flaghash) {
print "$flag\t\t$flaghash{$flag}";
}
}
sub dumpcmdcommand {
my @args = @_;
if (is_help(@_)) {
print STDERR "
Dump the full command used for compiling.
This can be useful to test compile a source file to see the errors.
Usage:
compiledbtool.pl dumpcmd <source file regex>
Options:
None
";
exit 0;
}
if (@args < 1) {
print STDERR "Please specify a regex\n";
exit 1;
}
my $regex = qr/$args[0]/;
my $cdb = readcdb();
my @match_commands;
for my $command (@$cdb) {
my $file = $command->{"file"};
if ($file =~ $regex) {
push @match_commands, $command;
}
}
if (@match_commands == 1) {
print join(' ', @{$match_commands[0]->{"arguments"}}), "\n";
} elsif (@match_commands == 0) {
print STDERR colored("No files match the regex\n", "red");
} else {
for my $command (@match_commands) {
print colored($command->{"file"}, "yellow"), ": ", join(' ', @{$command->{"arguments"}}), "\n";
}
}
}
sub gcccrosscompilecommand {
my @args = @_;
if (is_help(@_)) {
print STDERR "
Add common libclang flags for cross compiling
this tries to run the cross compiler and checks the filesystem to determine
extra flags and includes needed for cross compiling.
It is important that the generated database contains the path to the cross compiler as the first argument.
Usage:
compiledbtool.pl gcccrosscompile
Options:
None
";
exit 0;
}
my $cdb = readcdb();
my %compiler_cache;
for my $command (@$cdb) {
if(not defined $command->{"arguments"}) {
my $commandstr = $command->{"command"};
my @commandarr = split(/[ \t]+/, $commandstr);
$command->{"arguments"} = \@commandarr;
delete $command->{"command"};
}
my $arguments = $command->{"arguments"};
my $compiler = $arguments->[0];
if ($compiler =~ qr/^[^-].*g?[c+][c+]/) {
print STDERR colored($command->{"file"}, "yellow"), ": Compiler = $compiler\n";
if (not defined $compiler_cache{$compiler}) {
my @flags = cross_compile_flags($compiler, $command);
$compiler_cache{$compiler} = \@flags;
}
print STDERR colored($command->{"file"}, "yellow"), ": Flags = ", join(",", @{$compiler_cache{$compiler}}), "\n";
push @{$command->{"arguments"}}, @{$compiler_cache{$compiler}};
}
}
writecdb($cdb);
}
sub cross_compile_flags {
my $compiler = $_[0];
my $command = $_[1];
my @flags;
`$compiler --version`;
if ($? == 0) {
my $arch = `$compiler -dumpmachine`;
push @flags, "--target=$arch" unless ($arch eq "");
my $libgcc = `$compiler -print-libgcc-file-name`;
my $toolchain = dirname(dirname(dirname(dirname(dirname($libgcc)))));
push @flags, "--gcc-toolchain=$toolchain";
my $sysroot = `$compiler -print-sysroot`;
push @flags, "--sysroot=$sysroot";
my ($stdinc, $stdinc_cpp) = command_stdinc($command);
if ($stdinc) {
# Don't use -x c since we may have a g++ compiler in some cases
my $xcpp_flag = $stdinc_cpp ? "-xc++" : "";
my $is_system_inc = 0;
for my $inc_line (split /^/m, `$compiler -E $xcpp_flag -Wp,-v /dev/null 2>&1`) {
if ($inc_line =~ /End of search list\./) {
$is_system_inc = 0;
} elsif ($is_system_inc) {
# Remove leading/trailing whitespace
$inc_line =~ s/^[ \t]+//;
$inc_line =~ s/[ \t]+$//;
push @flags, "-isystem$inc_line";
} elsif ($inc_line =~ /#include <...> search starts here:/) {
$is_system_inc = 1;
}
}
}
}
chomp @flags;
return @flags;
}
sub command_is_cpp {
my $command = $_[0];
my $is_cpp = ($command->{"file"} =~ /\.(cpp|cc|cxx|c\+\+)$/);
my $found_x = 0;
for my $arg (@{$command->{"arguments"}}) {
$arg =~ s/^\s+//;
$arg =~ s/\s+$//;
if ($arg eq "-x") {
$found_x = 1;
} elsif ($found_x) {
$is_cpp = ($arg =~ /[Cc]\+\+/);
$found_x = 0;
}
}
return $is_cpp;
}
sub command_stdinc {
my $command = $_[0];
my $stdinc = 1;
my $stdinc_cpp = command_is_cpp($command);
for my $arg (@{$command->{"arguments"}}) {
$arg =~ s/^\s+//;
$arg =~ s/\s+$//;
if ($arg eq "-nostdinc") {
$stdinc = 0;
} elsif ($arg eq "-nostdinc++") {
$stdinc_cpp = 0;
}
}
return ($stdinc, $stdinc_cpp);
}
sub readcdb {
my $filename = "compile_commands.json";
if(@_ > 0) {
($filename) = @_;
}
my $cdbstr = do {
open(my $cdb, "<", $filename) or
die "$filename does not exist!";
local $/ = undef;
<$cdb>;
};
decode_json($cdbstr) or die "Failed to code JSON";
}
sub writecdb {
my ($cdbref) = @_;
my $json_str = to_json($cdbref, {utf8 => 1, pretty => 1});
print STDOUT $json_str;
}