1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2008, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Debug
; use Debug
;
27 with Osint
; use Osint
;
30 with Prj
.Ext
; use Prj
.Ext
;
33 package body Switch
.M
is
35 package Normalized_Switches
is new Table
.Table
36 (Table_Component_Type
=> String_Access
,
37 Table_Index_Type
=> Integer,
40 Table_Increment
=> 100,
41 Table_Name
=> "Switch.M.Normalized_Switches");
42 -- This table is used to keep the normalized switches, so that they may be
43 -- reused for subsequent invocations of Normalize_Compiler_Switches with
46 Initial_Number_Of_Switches
: constant := 10;
48 Global_Switches
: Argument_List_Access
:= null;
49 -- Used by function Normalize_Compiler_Switches
51 ---------------------------------
52 -- Normalize_Compiler_Switches --
53 ---------------------------------
55 procedure Normalize_Compiler_Switches
56 (Switch_Chars
: String;
57 Switches
: in out Argument_List_Access
;
60 Switch_Starts_With_Gnat
: Boolean;
62 Ptr
: Integer := Switch_Chars
'First;
63 Max
: constant Integer := Switch_Chars
'Last;
66 Storing
: String := Switch_Chars
;
67 First_Stored
: Positive := Ptr
+ 1;
68 Last_Stored
: Positive := First_Stored
;
70 procedure Add_Switch_Component
(S
: String);
71 -- Add a new String_Access component in Switches. If a string equal
72 -- to S is already stored in the table Normalized_Switches, use it.
73 -- Other wise add a new component to the table.
75 --------------------------
76 -- Add_Switch_Component --
77 --------------------------
79 procedure Add_Switch_Component
(S
: String) is
81 -- If Switches is null, allocate a new array
83 if Switches
= null then
84 Switches
:= new Argument_List
(1 .. Initial_Number_Of_Switches
);
86 -- otherwise, if Switches is full, extend it
88 elsif Last
= Switches
'Last then
90 New_Switches
: constant Argument_List_Access
:=
92 (1 .. Switches
'Length + Switches
'Length);
94 New_Switches
(1 .. Switches
'Length) := Switches
.all;
95 Last
:= Switches
'Length;
96 Switches
:= New_Switches
;
100 -- If this is the first switch, Last designates the first component
103 Last
:= Switches
'First;
108 -- Look into the table Normalized_Switches for a similar string.
109 -- If one is found, put it at the added component, and return.
111 for Index
in 1 .. Normalized_Switches
.Last
loop
112 if S
= Normalized_Switches
.Table
(Index
).all then
113 Switches
(Last
) := Normalized_Switches
.Table
(Index
);
118 -- No string equal to S was found in the table Normalized_Switches.
119 -- Add a new component in the table.
121 Switches
(Last
) := new String'(S);
122 Normalized_Switches.Append (Switches (Last));
123 end Add_Switch_Component;
125 -- Start of processing for Normalize_Compiler_Switches
130 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
136 Switch_Starts_With_Gnat :=
137 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
139 if Switch_Starts_With_Gnat then
144 while Ptr <= Max loop
145 C := Switch_Chars (Ptr);
147 -- Processing for a switch
149 case Switch_Starts_With_Gnat is
153 -- All switches that don't start with -gnat stay as is,
154 -- except -pg, -Wall, -k8, -w
156 if Switch_Chars = "-pg" or else Switch_Chars = "-p" then
158 -- The gcc driver converts -pg to -p, so that is what
159 -- is stored in the ALI file.
161 Add_Switch_Component ("-p");
163 elsif Switch_Chars = "-Wall" then
165 -- The gcc driver adds -gnatwa when -Wall is used
167 Add_Switch_Component ("-gnatwa");
168 Add_Switch_Component ("-Wall");
170 elsif Switch_Chars = "-k8" then
172 -- The gcc driver transforms -k8 into -gnatk8
174 Add_Switch_Component ("-gnatk8");
176 elsif Switch_Chars = "-w" then
178 -- The gcc driver adds -gnatws when -w is used
180 Add_Switch_Component ("-gnatws");
181 Add_Switch_Component ("-w");
183 elsif Switch_Chars'Length > 6
185 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 5)
188 Add_Switch_Component (Switch_Chars);
190 -- When --RTS=mtp is used, the gcc driver adds -mrtp
192 if Switch_Chars = "--RTS=mtp" then
193 Add_Switch_Component ("-mrtp");
196 -- Take only into account switches that are transmitted to
197 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
201 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
202 Add_Switch_Component (Switch_Chars);
215 -- One-letter switches
217 when 'a
' | 'A
' | 'b
' | 'c
' | 'D
' | 'E
' | 'f
' |
218 'F
' | 'g
' | 'h
' | 'H
' | 'k
' | 'l
' | 'L
' | 'n
' | 'N
' |
219 'o
' | 'O
' | 'p
' | 'P
' | 'q
' | 'Q
' | 'r
' | 's
' | 't
' |
220 'u
' | 'U
' | 'v
' | 'x
' | 'X
' | 'Z
' =>
221 Storing (First_Stored) := C;
223 (Storing (Storing'First .. First_Stored));
226 -- One-letter switches followed by a positive number
229 Storing (First_Stored) := C;
230 Last_Stored := First_Stored;
235 or else Switch_Chars (Ptr) not in '0' .. '9';
236 Last_Stored := Last_Stored + 1;
237 Storing (Last_Stored) := Switch_Chars (Ptr);
241 (Storing (Storing'First .. Last_Stored));
244 Storing (First_Stored) := 'd
';
248 C := Switch_Chars (Ptr);
249 exit when C = ASCII.NUL or else C = '/'
252 if C in '1' .. '9' or else
253 C in 'a
' .. 'z
' or else
256 Storing (First_Stored + 1) := C;
258 (Storing (Storing'First .. First_Stored + 1));
270 -- Only -gnateD and -gnatep= need storing in ALI file
272 Storing (First_Stored) := 'e
';
276 or else (Switch_Chars (Ptr) /= 'D
'
277 and then Switch_Chars (Ptr) /= 'p
')
283 -- Processing for -gnateD
285 if Switch_Chars (Ptr) = 'D
' then
286 Storing (First_Stored + 1 ..
287 First_Stored + Max - Ptr + 1) :=
288 Switch_Chars (Ptr .. Max);
290 (Storing (Storing'First ..
291 First_Stored + Max - Ptr + 1));
293 -- Processing for -gnatep=
303 if Switch_Chars (Ptr) = '=' then
307 -- To normalize, always put a '=' after -gnatep.
308 -- Because that could lengthen the switch string,
309 -- declare a local variable.
312 To_Store : String (1 .. Max - Ptr + 9);
314 To_Store (1 .. 8) := "-gnatep=";
315 To_Store (9 .. Max - Ptr + 9) :=
316 Switch_Chars (Ptr .. Max);
317 Add_Switch_Component (To_Store);
324 Storing (First_Stored) := 'i
';
333 C := Switch_Chars (Ptr);
342 Storing (First_Stored + 1) := C;
344 (Storing (Storing'First .. First_Stored + 1));
352 -- -gnatR may be followed by '0', '1', '2' or '3',
356 Last_Stored := First_Stored;
357 Storing (Last_Stored) := 'R
';
361 and then Switch_Chars (Ptr) in '0' .. '9'
363 C := Switch_Chars (Ptr);
365 if C in '4' .. '9' then
370 Last_Stored := Last_Stored + 1;
371 Storing (Last_Stored) := C;
375 and then Switch_Chars (Ptr) = 's
'
377 Last_Stored := Last_Stored + 1;
378 Storing (Last_Stored) := 's
';
385 (Storing (Storing'First .. Last_Stored));
389 when 'V
' | 'w
' | 'y
' =>
390 Storing (First_Stored) := C;
396 (Storing (Storing'First .. First_Stored));
404 while Ptr <= Max loop
405 C := Switch_Chars (Ptr);
410 if C = 'M
' and then Storing (First_Stored) = 'y
' then
411 Last_Stored := First_Stored + 1;
412 Storing (Last_Stored) := 'M
';
413 while Ptr <= Max loop
414 C := Switch_Chars (Ptr);
415 exit when C not in '0' .. '9';
416 Last_Stored := Last_Stored + 1;
417 Storing (Last_Stored) := C;
421 -- If there is no digit after -gnatyM,
422 -- the switch is invalid.
424 if Last_Stored = First_Stored + 1 then
430 (Storing (Storing'First .. Last_Stored));
433 -- All other switches are -gnatxx
436 Storing (First_Stored + 1) := C;
438 (Storing (Storing'First .. First_Stored + 1));
445 Last_Stored := First_Stored;
446 Storing (Last_Stored) := C;
449 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
457 Last_Stored := Last_Stored + 1;
458 Storing (Last_Stored) := '5';
460 (Storing (Storing'First .. Last_Stored));
467 Last_Stored := First_Stored;
468 Storing (Last_Stored) := '8';
471 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
479 Last_Stored := Last_Stored + 1;
480 Storing (Last_Stored) := '3';
482 (Storing (Storing'First .. Last_Stored));
486 -- Not a valid switch
496 end Normalize_Compiler_Switches;
498 function Normalize_Compiler_Switches
499 (Switch_Chars : String)
505 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
508 return (1 .. 0 => null);
510 return Global_Switches (Global_Switches'First .. Last);
513 end Normalize_Compiler_Switches;
515 ------------------------
516 -- Scan_Make_Switches --
517 ------------------------
519 procedure Scan_Make_Switches
520 (Switch_Chars : String;
521 Success : out Boolean)
523 Ptr : Integer := Switch_Chars'First;
524 Max : constant Integer := Switch_Chars'Last;
525 C : Character := ' ';
528 -- Assume a good switch
532 -- Skip past the initial character (must be the switch character)
535 Bad_Switch (Switch_Chars);
541 -- A little check, "gnat" at the start of a switch is for the compiler
543 if Switch_Chars'Length >= Ptr + 3
544 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
550 C := Switch_Chars (Ptr);
552 -- Multiple character switches
554 if Switch_Chars'Length > 2 then
555 if Switch_Chars = "--create-missing-dirs" then
556 Setup_Projects := True;
558 elsif Switch_Chars'Length > Subdirs_Option'Length
561 (Switch_Chars'First ..
562 Switch_Chars'First + Subdirs_Option'Length - 1) =
568 (Switch_Chars
'First + Subdirs_Option
'Length ..
571 elsif Switch_Chars
(Ptr
) = '-' then
572 Bad_Switch
(Switch_Chars
);
574 elsif Switch_Chars
'Length > 3
575 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
577 Add_Search_Project_Directory
578 (Switch_Chars
(Ptr
+ 2 .. Switch_Chars
'Last));
580 elsif C
= 'v' and then Switch_Chars
'Length = 3 then
582 Verbose_Mode
:= True;
584 case Switch_Chars
(Ptr
) is
586 Verbosity_Level
:= Opt
.Low
;
589 Verbosity_Level
:= Opt
.Medium
;
592 Verbosity_Level
:= Opt
.High
;
600 -- Note: for the debug switch, the remaining characters in this
601 -- switch field must all be debug flags, since all valid switch
602 -- characters are also valid debug characters. This switch is not
603 -- documented on purpose because it is only used by the
606 -- Loop to scan out debug flags
610 C
:= Switch_Chars
(Ptr
);
612 if C
in 'a' .. 'z' or else C
in 'A' .. 'Z' then
615 Bad_Switch
(Switch_Chars
);
622 case Switch_Chars
(Ptr
) is
624 -- Processing for eI switch
628 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Main_Index
, C
);
631 Bad_Switch
(Switch_Chars
);
634 -- Processing for eL switch
638 Bad_Switch
(Switch_Chars
);
641 Follow_Links_For_Files
:= True;
644 -- Processing for eS switch
648 Bad_Switch
(Switch_Chars
);
651 Commands_To_Stdout
:= True;
655 Bad_Switch
(Switch_Chars
);
664 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Max_Proc
, C
);
667 Bad_Switch
(Switch_Chars
);
670 Maximum_Processes
:= Positive (Max_Proc
);
674 elsif C
= 'w' and then Switch_Chars
'Length = 3 then
677 if Switch_Chars
= "-we" then
678 Warning_Mode
:= Treat_As_Error
;
680 elsif Switch_Chars
= "-wn" then
681 Warning_Mode
:= Normal
;
683 elsif Switch_Chars
= "-ws" then
684 Warning_Mode
:= Suppress
;
694 -- Single-character switches
702 Check_Readonly_Files
:= True;
704 -- Processing for b switch
710 -- Processing for B switch
713 Build_Bind_And_Link_Full_Project
:= True;
715 -- Processing for c switch
718 Compile_Only
:= True;
721 -- Processing for C switch
724 Create_Mapping_File
:= True;
726 -- Processing for D switch
729 if Object_Directory_Present
then
730 Osint
.Fail
("duplicate -D switch");
733 Object_Directory_Present
:= True;
736 -- Processing for f switch
739 Force_Compilations
:= True;
741 -- Processing for F switch
744 Full_Path_Name_For_Brief_Errors
:= True;
746 -- Processing for h switch
749 Usage_Requested
:= True;
751 -- Processing for i switch
754 In_Place_Mode
:= True;
756 -- Processing for j switch
759 -- -j not followed by a number is an error
761 Bad_Switch
(Switch_Chars
);
763 -- Processing for k switch
768 -- Processing for l switch
774 -- Processing for M switch
777 List_Dependencies
:= True;
779 -- Processing for n switch
782 Do_Not_Execute
:= True;
784 -- Processing for o switch
787 if Output_File_Name_Present
then
788 Osint
.Fail
("duplicate -o switch");
790 Output_File_Name_Present
:= True;
793 -- Processing for p switch
796 Setup_Projects
:= True;
798 -- Processing for q switch
801 Quiet_Output
:= True;
803 -- Processing for R switch
806 Run_Path_Option
:= False;
808 -- Processing for s switch
812 Check_Switches
:= True;
814 -- Processing for v switch
817 Verbose_Mode
:= True;
818 Verbosity_Level
:= Opt
.High
;
820 -- Processing for x switch
823 External_Unit_Compilation_Allowed
:= True;
825 -- Processing for z switch
828 No_Main_Subprogram
:= True;
830 -- Any other small letter is an illegal switch
833 if C
in 'a' .. 'z' then
834 Bad_Switch
(Switch_Chars
);
843 end Scan_Make_Switches
;