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 -- Store -gnateD, -gnatep= and -gnateG in the ALI file.
271 -- The other -gnate switches do not need to be stored.
273 Storing (First_Stored) := 'e';
277 or else (Switch_Chars (Ptr) /= 'D'
278 and then Switch_Chars (Ptr) /= 'G'
279 and then Switch_Chars (Ptr) /= 'p')
285 -- Processing for -gnateD
287 if Switch_Chars (Ptr) = 'D' then
288 Storing (First_Stored + 1 ..
289 First_Stored + Max - Ptr + 1) :=
290 Switch_Chars (Ptr .. Max);
292 (Storing (Storing'First ..
293 First_Stored + Max - Ptr + 1));
295 -- Processing for -gnatep=
297 elsif Switch_Chars (Ptr) = 'p' then
305 if Switch_Chars (Ptr) = '=' then
309 -- To normalize, always put a '=' after -gnatep.
310 -- Because that could lengthen the switch string,
311 -- declare a local variable.
314 To_Store : String (1 .. Max - Ptr + 9);
316 To_Store (1 .. 8) := "-gnatep=";
317 To_Store (9 .. Max - Ptr + 9) :=
318 Switch_Chars (Ptr .. Max);
319 Add_Switch_Component (To_Store);
322 elsif Switch_Chars (Ptr) = 'G' then
323 Add_Switch_Component ("-gnateG");
329 Storing (First_Stored) := 'i';
338 C := Switch_Chars (Ptr);
347 Storing (First_Stored + 1) := C;
349 (Storing (Storing'First .. First_Stored + 1));
357 -- -gnatR may be followed by '0', '1', '2' or '3',
361 Last_Stored := First_Stored;
362 Storing (Last_Stored) := 'R';
366 and then Switch_Chars (Ptr) in '0' .. '9'
368 C := Switch_Chars (Ptr);
370 if C in '4' .. '9' then
375 Last_Stored := Last_Stored + 1;
376 Storing (Last_Stored) := C;
380 and then Switch_Chars (Ptr) = 's'
382 Last_Stored := Last_Stored + 1;
383 Storing (Last_Stored) := 's';
390 (Storing (Storing'First .. Last_Stored));
394 when 'V' | 'w' | 'y' =>
395 Storing (First_Stored) := C;
401 (Storing (Storing'First .. First_Stored));
409 while Ptr <= Max loop
410 C := Switch_Chars (Ptr);
415 if C = 'M' and then Storing (First_Stored) = 'y' then
416 Last_Stored := First_Stored + 1;
417 Storing (Last_Stored) := 'M';
418 while Ptr <= Max loop
419 C := Switch_Chars (Ptr);
420 exit when C not in '0' .. '9';
421 Last_Stored := Last_Stored + 1;
422 Storing (Last_Stored) := C;
426 -- If there is no digit after -gnatyM,
427 -- the switch is invalid.
429 if Last_Stored = First_Stored + 1 then
435 (Storing (Storing'First .. Last_Stored));
438 -- All other switches are -gnatxx
441 Storing (First_Stored + 1) := C;
443 (Storing (Storing'First .. First_Stored + 1));
450 Last_Stored := First_Stored;
451 Storing (Last_Stored) := C;
454 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
462 Last_Stored := Last_Stored + 1;
463 Storing (Last_Stored) := '5';
465 (Storing (Storing'First .. Last_Stored));
472 Last_Stored := First_Stored;
473 Storing (Last_Stored) := '8';
476 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
484 Last_Stored := Last_Stored + 1;
485 Storing (Last_Stored) := '3';
487 (Storing (Storing'First .. Last_Stored));
491 -- Not a valid switch
501 end Normalize_Compiler_Switches;
503 function Normalize_Compiler_Switches
504 (Switch_Chars : String)
510 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
513 return (1 .. 0 => null);
515 return Global_Switches (Global_Switches'First .. Last);
518 end Normalize_Compiler_Switches;
520 ------------------------
521 -- Scan_Make_Switches --
522 ------------------------
524 procedure Scan_Make_Switches
525 (Switch_Chars : String;
526 Success : out Boolean)
528 Ptr : Integer := Switch_Chars'First;
529 Max : constant Integer := Switch_Chars'Last;
530 C : Character := ' ';
533 -- Assume a good switch
537 -- Skip past the initial character (must be the switch character)
540 Bad_Switch (Switch_Chars);
546 -- A little check, "gnat" at the start of a switch is for the compiler
548 if Switch_Chars'Length >= Ptr + 3
549 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
555 C := Switch_Chars (Ptr);
557 -- Multiple character switches
559 if Switch_Chars'Length > 2 then
560 if Switch_Chars = "--create-missing-dirs" then
561 Setup_Projects := True;
563 elsif Switch_Chars'Length > Subdirs_Option'Length
566 (Switch_Chars'First ..
567 Switch_Chars'First + Subdirs_Option'Length - 1) =
573 (Switch_Chars'First + Subdirs_Option'Length ..
576 elsif Switch_Chars (Ptr) = '-' then
577 Bad_Switch (Switch_Chars);
579 elsif Switch_Chars'Length > 3
580 and then Switch_Chars (Ptr .. Ptr + 1) = "aP"
582 Add_Search_Project_Directory
583 (Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
585 elsif C = 'v' and then Switch_Chars'Length = 3 then
587 Verbose_Mode := True;
589 case Switch_Chars (Ptr) is
591 Verbosity_Level := Opt.Low;
594 Verbosity_Level := Opt.Medium;
597 Verbosity_Level := Opt.High;
605 -- Note: for the debug switch, the remaining characters in this
606 -- switch field must all be debug flags, since all valid switch
607 -- characters are also valid debug characters. This switch is not
608 -- documented on purpose because it is only used by the
611 -- Loop to scan out debug flags
615 C := Switch_Chars (Ptr);
617 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
620 Bad_Switch (Switch_Chars);
627 case Switch_Chars (Ptr) is
629 -- Processing for eI switch
633 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
636 Bad_Switch (Switch_Chars);
639 -- Processing for eL switch
643 Bad_Switch (Switch_Chars);
646 Follow_Links_For_Files := True;
649 -- Processing for eS switch
653 Bad_Switch (Switch_Chars);
656 Commands_To_Stdout := True;
660 Bad_Switch (Switch_Chars);
669 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
672 Bad_Switch (Switch_Chars);
675 Maximum_Processes := Positive (Max_Proc);
679 elsif C = 'w' and then Switch_Chars'Length = 3 then
682 if Switch_Chars = "-we" then
683 Warning_Mode := Treat_As_Error;
685 elsif Switch_Chars = "-wn" then
686 Warning_Mode := Normal;
688 elsif Switch_Chars = "-ws" then
689 Warning_Mode := Suppress;
699 -- Single-character switches
707 Check_Readonly_Files := True;
709 -- Processing for b switch
715 -- Processing for B switch
718 Build_Bind_And_Link_Full_Project := True;
720 -- Processing for c switch
723 Compile_Only := True;
726 -- Processing for C switch
729 Create_Mapping_File := True;
731 -- Processing for D switch
734 if Object_Directory_Present then
735 Osint.Fail ("duplicate -D switch");
738 Object_Directory_Present := True;
741 -- Processing for f switch
744 Force_Compilations := True;
746 -- Processing for F switch
749 Full_Path_Name_For_Brief_Errors := True;
751 -- Processing for h switch
754 Usage_Requested := True;
756 -- Processing for i switch
759 In_Place_Mode := True;
761 -- Processing for j switch
764 -- -j not followed by a number is an error
766 Bad_Switch (Switch_Chars);
768 -- Processing for k switch
773 -- Processing for l switch
779 -- Processing for M switch
782 List_Dependencies := True;
784 -- Processing for n switch
787 Do_Not_Execute := True;
789 -- Processing for o switch
792 if Output_File_Name_Present then
793 Osint.Fail ("duplicate -o switch");
795 Output_File_Name_Present := True;
798 -- Processing for p switch
801 Setup_Projects := True;
803 -- Processing for q switch
806 Quiet_Output := True;
808 -- Processing for R switch
811 Run_Path_Option := False;
813 -- Processing for s switch
817 Check_Switches := True;
819 -- Processing for v switch
822 Verbose_Mode := True;
823 Verbosity_Level := Opt.High;
825 -- Processing for x switch
828 External_Unit_Compilation_Allowed := True;
830 -- Processing for z switch
833 No_Main_Subprogram := True;
835 -- Any other small letter is an illegal switch
838 if C in 'a' .. 'z' then
839 Bad_Switch (Switch_Chars);
848 end Scan_Make_Switches;