[cage] Add some karma aliases for myself
[parrot.git] / tools / dev / cc_flags.pl
blob4b147869c66b802a62ea031d4170471e3d28d5d4
1 #! perl
2 ################################################################################
3 # Copyright (C) 2001-2003, Parrot Foundation.
4 # $Id$
5 ################################################################################
7 =head1 NAME
9 tools/dev/cc_flags.pl - Process compiler flags
11 =head1 SYNOPSIS
13 % perl tools/dev/cc_flags.pl transform compiler flags
15 =head1 DESCRIPTION
17 This script is used in a F<Makefile> to process the flags to pass to the
18 compiler for each C file.
20 See F<config/gen/makefiles/CFLAGS.in> for the transformation file format.
22 =head1 SEE ALSO
24 F<config/gen/cflags/root.in>.
26 =cut
28 ################################################################################
30 use strict;
31 use warnings;
33 my $return_only;
34 my $verbose;
36 if ($ARGV[0] eq '--return-only') {
37 $return_only = 1;
38 shift;
41 if ($ARGV[0] eq '-v') {
42 $verbose = 1;
43 shift;
46 my $cflags = shift;
48 open my $F, '<', $cflags or die "open $cflags: $!\n";
50 my @options;
52 while (<$F>) {
53 chomp;
54 s/#.*//;
55 next unless /\S/;
57 my $regex;
58 if (s/^\{(.*?)\}\s*//) {
59 next unless $1;
60 $regex = qr/$1/;
62 elsif (s/^(\S+)\s*//) {
63 $regex = qr/^\Q$1\E$/;
65 else {
66 die "syntax error in $cflags: line $., $_\n";
69 for ( ; ; ) {
70 if (s/^([-+])\{(.*?)\}\s*//) {
71 next unless $2;
72 my ( $sign, $options ) = ( $1, $2 );
73 foreach my $option ( split ' ', $options ) {
74 push @options, [ $regex, $sign, $option ];
77 elsif (s{s(.)(.*?)\1(.*?)\1([imsx]*)\s*}{}) {
78 my $mod = "";
79 $mod = "(?$4)" if $4;
81 push @options, [ $regex, 's', "$mod$2", $3 ];
83 elsif (/\S/) {
84 die "syntax error in $cflags: line $., $_\n";
86 else {
87 last;
92 my ($cfile) = grep /\.c$/, @ARGV;
94 my ( $inject_point, $where );
96 foreach (@ARGV) {
97 last if $_ eq '';
98 ++$where;
100 if ($where) {
102 # Found a "" - remove it
103 splice @ARGV, $where, 1;
104 $inject_point = $where;
106 else {
107 $inject_point = 1;
110 if ($cfile) {
111 foreach my $option (@options) {
112 if ( $cfile =~ $option->[0] ) {
113 if ( $option->[1] eq '+' ) {
114 splice @ARGV, $inject_point, 0, $option->[2];
116 elsif ( $option->[1] eq '-' ) {
117 @ARGV = grep { $_ ne $option->[2] } @ARGV;
119 else {
120 foreach my $arg (@ARGV) {
121 $arg =~ s/$option->[2]/$option->[3]/;
127 # print "@ARGV\n";
129 # Visual C++ already prints the source file name...
130 if ( $ARGV[0] =~ /cl(?:\.exe)?/i ) {
132 # ...but only the file name, so we print the path
133 # to the directory first
134 if ( $cfile =~ /(.*[\/\\])/ ) {
135 print $1;
138 else {
139 print "$cfile\n";
143 if ($return_only) {
144 print join ' ', @ARGV;
145 exit;
148 if ($verbose) {
149 print join ' ', @ARGV;
152 exit system(@ARGV) / 256;
154 # Local Variables:
155 # mode: cperl
156 # cperl-indent-level: 4
157 # fill-column: 100
158 # End:
159 # vim: expandtab shiftwidth=4: