1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2007, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Debug; use Debug;
28 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.Increment_Last;
123 Normalized_Switches.Table (Normalized_Switches.Last) :=
125 end Add_Switch_Component;
127 -- Start of processing for Normalize_Compiler_Switches
132 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
138 Switch_Starts_With_Gnat :=
139 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
141 if Switch_Starts_With_Gnat then
146 while Ptr <= Max loop
147 C := Switch_Chars (Ptr);
149 -- Processing for a switch
151 case Switch_Starts_With_Gnat is
155 -- All switches that don't start with -gnat stay as is,
156 -- except -v, -E and -pg
158 if Switch_Chars = "-pg" then
160 -- The gcc driver converts -pg to -p, so that is what
161 -- is stored in the ALI file.
163 Add_Switch_Component ("-p");
165 -- Do not take into account switches that are not transmitted
166 -- to gnat1 by the gcc driver.
168 elsif C /= 'v' and then C /= 'E' then
169 Add_Switch_Component (Switch_Chars);
178 -- One-letter switches
180 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
181 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
182 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
183 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
184 Storing (First_Stored) := C;
186 (Storing (Storing'First .. First_Stored));
189 -- One-letter switches followed by a positive number
192 Storing (First_Stored) := C;
193 Last_Stored := First_Stored;
198 or else Switch_Chars (Ptr) not in '0' .. '9';
199 Last_Stored := Last_Stored + 1;
200 Storing (Last_Stored) := Switch_Chars (Ptr);
204 (Storing (Storing'First .. Last_Stored));
207 Storing (First_Stored) := 'd';
211 C := Switch_Chars (Ptr);
212 exit when C = ASCII.NUL or else C = '/'
215 if C in '1' .. '9' or else
216 C in 'a' .. 'z' or else
219 Storing (First_Stored + 1) := C;
221 (Storing (Storing'First .. First_Stored + 1));
233 -- Only -gnateD and -gnatep= need storing in ALI file
235 Storing (First_Stored) := 'e';
239 or else (Switch_Chars (Ptr) /= 'D'
240 and then Switch_Chars (Ptr) /= 'p')
246 -- Processing for -gnateD
248 if Switch_Chars (Ptr) = 'D' then
249 Storing (First_Stored + 1 ..
250 First_Stored + Max - Ptr + 1) :=
251 Switch_Chars (Ptr .. Max);
253 (Storing (Storing'First ..
254 First_Stored + Max - Ptr + 1));
256 -- Processing for -gnatep=
266 if Switch_Chars (Ptr) = '=' then
270 -- To normalize, always put a '=' after -gnatep.
271 -- Because that could lengthen the switch string,
272 -- declare a local variable.
275 To_Store : String (1 .. Max - Ptr + 9);
277 To_Store (1 .. 8) := "-gnatep=";
278 To_Store (9 .. Max - Ptr + 9) :=
279 Switch_Chars (Ptr .. Max);
280 Add_Switch_Component (To_Store);
287 Storing (First_Stored) := 'i';
296 C := Switch_Chars (Ptr);
305 Storing (First_Stored + 1) := C;
307 (Storing (Storing'First .. First_Stored + 1));
315 -- -gnatR may be followed by '0', '1', '2' or '3',
319 Last_Stored := First_Stored;
320 Storing (Last_Stored) := 'R';
324 and then Switch_Chars (Ptr) in '0' .. '9'
326 C := Switch_Chars (Ptr);
328 if C in '4' .. '9' then
333 Last_Stored := Last_Stored + 1;
334 Storing (Last_Stored) := C;
338 and then Switch_Chars (Ptr) = 's' then
339 Last_Stored := Last_Stored + 1;
340 Storing (Last_Stored) := 's';
347 (Storing (Storing'First .. Last_Stored));
351 when 'V' | 'w' | 'y' =>
352 Storing (First_Stored) := C;
358 (Storing (Storing'First .. First_Stored));
366 while Ptr <= Max loop
367 C := Switch_Chars (Ptr);
373 Storing (First_Stored) = 'y'
375 Last_Stored := First_Stored + 1;
376 Storing (Last_Stored) := 'M';
378 while Ptr <= Max loop
379 C := Switch_Chars (Ptr);
380 exit when C not in '0' .. '9';
381 Last_Stored := Last_Stored + 1;
382 Storing (Last_Stored) := C;
386 -- If there is no digit after -gnatyM,
387 -- the switch is invalid.
389 if Last_Stored = First_Stored + 1 then
395 (Storing (Storing'First .. Last_Stored));
398 -- All other switches are -gnatxx
401 Storing (First_Stored + 1) := C;
403 (Storing (Storing'First .. First_Stored + 1));
410 Last_Stored := First_Stored;
411 Storing (Last_Stored) := C;
414 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
422 Last_Stored := Last_Stored + 1;
423 Storing (Last_Stored) := '5';
425 (Storing (Storing'First .. Last_Stored));
432 Last_Stored := First_Stored;
433 Storing (Last_Stored) := '8';
436 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
444 Last_Stored := Last_Stored + 1;
445 Storing (Last_Stored) := '3';
447 (Storing (Storing'First .. Last_Stored));
451 -- Not a valid switch
461 end Normalize_Compiler_Switches;
463 function Normalize_Compiler_Switches
464 (Switch_Chars : String)
470 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
473 return (1 .. 0 => null);
475 return Global_Switches (Global_Switches'First .. Last);
478 end Normalize_Compiler_Switches;
480 ------------------------
481 -- Scan_Make_Switches --
482 ------------------------
484 procedure Scan_Make_Switches
485 (Switch_Chars : String;
486 Success : out Boolean)
488 Ptr : Integer := Switch_Chars'First;
489 Max : constant Integer := Switch_Chars'Last;
490 C : Character := ' ';
493 -- Assume a good switch
497 -- Skip past the initial character (must be the switch character)
500 Bad_Switch (Switch_Chars);
506 -- A little check, "gnat" at the start of a switch is for the compiler
508 if Switch_Chars'Length >= Ptr + 3
509 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
515 C := Switch_Chars (Ptr);
517 -- Multiple character switches
519 if Switch_Chars'Length > 2 then
520 if Switch_Chars = "--create-missing-dirs" then
521 Setup_Projects := True;
523 elsif Switch_Chars'Length > 3 and then
524 Switch_Chars (Ptr .. Ptr + 1) = "aP"
526 Add_Search_Project_Directory
527 (Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
529 elsif C = 'v' and then Switch_Chars'Length = 3 then
531 Verbose_Mode := True;
533 case Switch_Chars (Ptr) is
535 Verbosity_Level := Opt.Low;
538 Verbosity_Level := Opt.Medium;
541 Verbosity_Level := Opt.High;
549 -- Note: for the debug switch, the remaining characters in this
550 -- switch field must all be debug flags, since all valid switch
551 -- characters are also valid debug characters. This switch is not
552 -- documented on purpose because it is only used by the
555 -- Loop to scan out debug flags
559 C := Switch_Chars (Ptr);
561 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
564 Bad_Switch (Switch_Chars);
571 case Switch_Chars (Ptr) is
573 -- Processing for eI switch
577 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
580 Bad_Switch (Switch_Chars);
583 -- Processing for eL switch
587 Bad_Switch (Switch_Chars);
590 Follow_Links := True;
593 -- Processing for eS switch
597 Bad_Switch (Switch_Chars);
600 Commands_To_Stdout := True;
604 Bad_Switch (Switch_Chars);
613 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
616 Bad_Switch (Switch_Chars);
619 Maximum_Processes := Positive (Max_Proc);
623 elsif C = 'w' and then Switch_Chars'Length = 3 then
626 if Switch_Chars = "-we" then
627 Warning_Mode := Treat_As_Error;
629 elsif Switch_Chars = "-wn" then
630 Warning_Mode := Normal;
632 elsif Switch_Chars = "-ws" then
633 Warning_Mode := Suppress;
643 -- Single-character switches
651 Check_Readonly_Files := True;
653 -- Processing for b switch
659 -- Processing for B switch
662 Build_Bind_And_Link_Full_Project := True;
664 -- Processing for c switch
667 Compile_Only := True;
670 -- Processing for C switch
673 Create_Mapping_File := True;
675 -- Processing for D switch
678 if Object_Directory_Present then
679 Osint.Fail ("duplicate -D switch");
682 Object_Directory_Present := True;
685 -- Processing for f switch
688 Force_Compilations := True;
690 -- Processing for F switch
693 Full_Path_Name_For_Brief_Errors := True;
695 -- Processing for h switch
698 Usage_Requested := True;
700 -- Processing for i switch
703 In_Place_Mode := True;
705 -- Processing for j switch
708 -- -j not followed by a number is an error
710 Bad_Switch (Switch_Chars);
712 -- Processing for k switch
717 -- Processing for l switch
723 -- Processing for M switch
726 List_Dependencies := True;
728 -- Processing for n switch
731 Do_Not_Execute := True;
733 -- Processing for o switch
736 if Output_File_Name_Present then
737 Osint.Fail ("duplicate -o switch");
739 Output_File_Name_Present := True;
742 -- Processing for p switch
745 Setup_Projects := True;
747 -- Processing for q switch
750 Quiet_Output := True;
752 -- Processing for R switch
755 Run_Path_Option := False;
757 -- Processing for s switch
761 Check_Switches := True;
763 -- Processing for v switch
766 Verbose_Mode := True;
767 Verbosity_Level := Opt.High;
769 -- Processing for x switch
772 External_Unit_Compilation_Allowed := True;
774 -- Processing for z switch
777 No_Main_Subprogram := True;
779 -- Any other small letter is an illegal switch
782 if C in 'a' .. 'z' then
783 Bad_Switch (Switch_Chars);
792 end Scan_Make_Switches;