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 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;
29 with Prj.Ext; use Prj.Ext;
32 package body Switch.M is
34 package Normalized_Switches is new Table.Table
35 (Table_Component_Type => String_Access,
36 Table_Index_Type => Integer,
39 Table_Increment => 100,
40 Table_Name => "Switch.M.Normalized_Switches");
41 -- This table is used to keep the normalized switches, so that they may be
42 -- reused for subsequent invocations of Normalize_Compiler_Switches with
45 Initial_Number_Of_Switches : constant := 10;
47 Global_Switches : Argument_List_Access := null;
48 -- Used by function Normalize_Compiler_Switches
50 ---------------------------------
51 -- Normalize_Compiler_Switches --
52 ---------------------------------
54 procedure Normalize_Compiler_Switches
55 (Switch_Chars : String;
56 Switches : in out Argument_List_Access;
59 Switch_Starts_With_Gnat : Boolean;
61 Ptr : Integer := Switch_Chars'First;
62 Max : constant Integer := Switch_Chars'Last;
65 Storing : String := Switch_Chars;
66 First_Stored : Positive := Ptr + 1;
67 Last_Stored : Positive := First_Stored;
69 procedure Add_Switch_Component (S : String);
70 -- Add a new String_Access component in Switches. If a string equal
71 -- to S is already stored in the table Normalized_Switches, use it.
72 -- Other wise add a new component to the table.
74 --------------------------
75 -- Add_Switch_Component --
76 --------------------------
78 procedure Add_Switch_Component (S : String) is
80 -- If Switches is null, allocate a new array
82 if Switches = null then
83 Switches := new Argument_List (1 .. Initial_Number_Of_Switches);
85 -- otherwise, if Switches is full, extend it
87 elsif Last = Switches'Last then
89 New_Switches : constant Argument_List_Access :=
91 (1 .. Switches'Length + Switches'Length);
93 New_Switches (1 .. Switches'Length) := Switches.all;
94 Last := Switches'Length;
95 Switches := New_Switches;
99 -- If this is the first switch, Last designates the first component
102 Last := Switches'First;
107 -- Look into the table Normalized_Switches for a similar string.
108 -- If one is found, put it at the added component, and return.
110 for Index in 1 .. Normalized_Switches.Last loop
111 if S = Normalized_Switches.Table (Index).all then
112 Switches (Last) := Normalized_Switches.Table (Index);
117 -- No string equal to S was found in the table Normalized_Switches.
118 -- Add a new component in the table.
120 Switches (Last) := new String'(S);
121 Normalized_Switches.Append (Switches (Last));
122 end Add_Switch_Component;
124 -- Start of processing for Normalize_Compiler_Switches
129 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
135 Switch_Starts_With_Gnat :=
136 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
138 if Switch_Starts_With_Gnat then
143 while Ptr <= Max loop
144 C := Switch_Chars (Ptr);
146 -- Processing for a switch
148 case Switch_Starts_With_Gnat is
152 -- All switches that don't start with -gnat stay as is,
153 -- except -v, -E and -pg
155 if Switch_Chars = "-pg" then
157 -- The gcc driver converts -pg to -p, so that is what
158 -- is stored in the ALI file.
160 Add_Switch_Component ("-p");
162 -- Do not take into account switches that are not transmitted
163 -- to gnat1 by the gcc driver.
165 elsif C /= 'v' and then C /= 'E' then
166 Add_Switch_Component (Switch_Chars);
175 -- One-letter switches
177 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
178 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
179 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
180 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
181 Storing (First_Stored) := C;
183 (Storing (Storing'First .. First_Stored));
186 -- One-letter switches followed by a positive number
189 Storing (First_Stored) := C;
190 Last_Stored := First_Stored;
195 or else Switch_Chars (Ptr) not in '0' .. '9';
196 Last_Stored := Last_Stored + 1;
197 Storing (Last_Stored) := Switch_Chars (Ptr);
201 (Storing (Storing'First .. Last_Stored));
204 Storing (First_Stored) := 'd';
208 C := Switch_Chars (Ptr);
209 exit when C = ASCII.NUL or else C = '/'
212 if C in '1' .. '9' or else
213 C in 'a' .. 'z' or else
216 Storing (First_Stored + 1) := C;
218 (Storing (Storing'First .. First_Stored + 1));
230 -- Only -gnateD and -gnatep= need storing in ALI file
232 Storing (First_Stored) := 'e';
236 or else (Switch_Chars (Ptr) /= 'D'
237 and then Switch_Chars (Ptr) /= 'p')
243 -- Processing for -gnateD
245 if Switch_Chars (Ptr) = 'D' then
246 Storing (First_Stored + 1 ..
247 First_Stored + Max - Ptr + 1) :=
248 Switch_Chars (Ptr .. Max);
250 (Storing (Storing'First ..
251 First_Stored + Max - Ptr + 1));
253 -- Processing for -gnatep=
263 if Switch_Chars (Ptr) = '=' then
267 -- To normalize, always put a '=' after -gnatep.
268 -- Because that could lengthen the switch string,
269 -- declare a local variable.
272 To_Store : String (1 .. Max - Ptr + 9);
274 To_Store (1 .. 8) := "-gnatep=";
275 To_Store (9 .. Max - Ptr + 9) :=
276 Switch_Chars (Ptr .. Max);
277 Add_Switch_Component (To_Store);
284 Storing (First_Stored) := 'i';
293 C := Switch_Chars (Ptr);
302 Storing (First_Stored + 1) := C;
304 (Storing (Storing'First .. First_Stored + 1));
312 -- -gnatR may be followed by '0', '1', '2' or '3',
316 Last_Stored := First_Stored;
317 Storing (Last_Stored) := 'R';
321 and then Switch_Chars (Ptr) in '0' .. '9'
323 C := Switch_Chars (Ptr);
325 if C in '4' .. '9' then
330 Last_Stored := Last_Stored + 1;
331 Storing (Last_Stored) := C;
335 and then Switch_Chars (Ptr) = 's' then
336 Last_Stored := Last_Stored + 1;
337 Storing (Last_Stored) := 's';
344 (Storing (Storing'First .. Last_Stored));
348 when 'V' | 'w' | 'y' =>
349 Storing (First_Stored) := C;
355 (Storing (Storing'First .. First_Stored));
363 while Ptr <= Max loop
364 C := Switch_Chars (Ptr);
370 Storing (First_Stored) = 'y'
372 Last_Stored := First_Stored + 1;
373 Storing (Last_Stored) := 'M';
375 while Ptr <= Max loop
376 C := Switch_Chars (Ptr);
377 exit when C not in '0' .. '9';
378 Last_Stored := Last_Stored + 1;
379 Storing (Last_Stored) := C;
383 -- If there is no digit after -gnatyM,
384 -- the switch is invalid.
386 if Last_Stored = First_Stored + 1 then
392 (Storing (Storing'First .. Last_Stored));
395 -- All other switches are -gnatxx
398 Storing (First_Stored + 1) := C;
400 (Storing (Storing'First .. First_Stored + 1));
407 Last_Stored := First_Stored;
408 Storing (Last_Stored) := C;
411 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
419 Last_Stored := Last_Stored + 1;
420 Storing (Last_Stored) := '5';
422 (Storing (Storing'First .. Last_Stored));
429 Last_Stored := First_Stored;
430 Storing (Last_Stored) := '8';
433 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
441 Last_Stored := Last_Stored + 1;
442 Storing (Last_Stored) := '3';
444 (Storing (Storing'First .. Last_Stored));
448 -- Not a valid switch
458 end Normalize_Compiler_Switches;
460 function Normalize_Compiler_Switches
461 (Switch_Chars : String)
467 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
470 return (1 .. 0 => null);
472 return Global_Switches (Global_Switches'First .. Last);
475 end Normalize_Compiler_Switches;
477 ------------------------
478 -- Scan_Make_Switches --
479 ------------------------
481 procedure Scan_Make_Switches
482 (Switch_Chars : String;
483 Success : out Boolean)
485 Ptr : Integer := Switch_Chars'First;
486 Max : constant Integer := Switch_Chars'Last;
487 C : Character := ' ';
490 -- Assume a good switch
494 -- Skip past the initial character (must be the switch character)
497 Bad_Switch (Switch_Chars);
503 -- A little check, "gnat" at the start of a switch is for the compiler
505 if Switch_Chars'Length >= Ptr + 3
506 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
512 C := Switch_Chars (Ptr);
514 -- Multiple character switches
516 if Switch_Chars'Length > 2 then
517 if Switch_Chars = "--create-missing-dirs" then
518 Setup_Projects := True;
520 elsif Switch_Chars'Length > 3 and then
521 Switch_Chars (Ptr .. Ptr + 1) = "aP"
523 Add_Search_Project_Directory
524 (Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
526 elsif C = 'v' and then Switch_Chars'Length = 3 then
528 Verbose_Mode := True;
530 case Switch_Chars (Ptr) is
532 Verbosity_Level := Opt.Low;
535 Verbosity_Level := Opt.Medium;
538 Verbosity_Level := Opt.High;
546 -- Note: for the debug switch, the remaining characters in this
547 -- switch field must all be debug flags, since all valid switch
548 -- characters are also valid debug characters. This switch is not
549 -- documented on purpose because it is only used by the
552 -- Loop to scan out debug flags
556 C := Switch_Chars (Ptr);
558 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
561 Bad_Switch (Switch_Chars);
568 case Switch_Chars (Ptr) is
570 -- Processing for eI switch
574 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
577 Bad_Switch (Switch_Chars);
580 -- Processing for eL switch
584 Bad_Switch (Switch_Chars);
587 Follow_Links := True;
590 -- Processing for eS switch
594 Bad_Switch (Switch_Chars);
597 Commands_To_Stdout := True;
601 Bad_Switch (Switch_Chars);
610 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
613 Bad_Switch (Switch_Chars);
616 Maximum_Processes := Positive (Max_Proc);
620 elsif C = 'w' and then Switch_Chars'Length = 3 then
623 if Switch_Chars = "-we" then
624 Warning_Mode := Treat_As_Error;
626 elsif Switch_Chars = "-wn" then
627 Warning_Mode := Normal;
629 elsif Switch_Chars = "-ws" then
630 Warning_Mode := Suppress;
640 -- Single-character switches
648 Check_Readonly_Files := True;
650 -- Processing for b switch
656 -- Processing for B switch
659 Build_Bind_And_Link_Full_Project := True;
661 -- Processing for c switch
664 Compile_Only := True;
667 -- Processing for C switch
670 Create_Mapping_File := True;
672 -- Processing for D switch
675 if Object_Directory_Present then
676 Osint.Fail ("duplicate -D switch");
679 Object_Directory_Present := True;
682 -- Processing for f switch
685 Force_Compilations := True;
687 -- Processing for F switch
690 Full_Path_Name_For_Brief_Errors := True;
692 -- Processing for h switch
695 Usage_Requested := True;
697 -- Processing for i switch
700 In_Place_Mode := True;
702 -- Processing for j switch
705 -- -j not followed by a number is an error
707 Bad_Switch (Switch_Chars);
709 -- Processing for k switch
714 -- Processing for l switch
720 -- Processing for M switch
723 List_Dependencies := True;
725 -- Processing for n switch
728 Do_Not_Execute := True;
730 -- Processing for o switch
733 if Output_File_Name_Present then
734 Osint.Fail ("duplicate -o switch");
736 Output_File_Name_Present := True;
739 -- Processing for p switch
742 Setup_Projects := True;
744 -- Processing for q switch
747 Quiet_Output := True;
749 -- Processing for R switch
752 Run_Path_Option := False;
754 -- Processing for s switch
758 Check_Switches := True;
760 -- Processing for v switch
763 Verbose_Mode := True;
764 Verbosity_Level := Opt.High;
766 -- Processing for x switch
769 External_Unit_Compilation_Allowed := True;
771 -- Processing for z switch
774 No_Main_Subprogram := True;
776 -- Any other small letter is an illegal switch
779 if C in 'a' .. 'z' then
780 Bad_Switch (Switch_Chars);
789 end Scan_Make_Switches;