1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2006, 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;
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.Increment_Last;
122 Normalized_Switches.Table (Normalized_Switches.Last) :=
124 end Add_Switch_Component;
126 -- Start of processing for Normalize_Compiler_Switches
131 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
137 Switch_Starts_With_Gnat :=
138 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
140 if Switch_Starts_With_Gnat then
145 while Ptr <= Max loop
146 C := Switch_Chars (Ptr);
148 -- Processing for a switch
150 case Switch_Starts_With_Gnat is
154 -- All switches that don't start with -gnat stay as is,
157 if Switch_Chars = "-pg" then
159 -- The gcc driver converts -pg to -p, so that is what
160 -- is stored in the ALI file.
162 Add_Switch_Component ("-p");
165 Add_Switch_Component (Switch_Chars);
174 -- One-letter switches
176 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
177 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
178 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
179 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
180 Storing (First_Stored) := C;
182 (Storing (Storing'First .. First_Stored));
185 -- One-letter switches followed by a positive number
188 Storing (First_Stored) := C;
189 Last_Stored := First_Stored;
194 or else Switch_Chars (Ptr) not in '0' .. '9';
195 Last_Stored := Last_Stored + 1;
196 Storing (Last_Stored) := Switch_Chars (Ptr);
200 (Storing (Storing'First .. Last_Stored));
203 Storing (First_Stored) := 'd';
207 C := Switch_Chars (Ptr);
208 exit when C = ASCII.NUL or else C = '/'
211 if C in '1' .. '9' or else
212 C in 'a' .. 'z' or else
215 Storing (First_Stored + 1) := C;
217 (Storing (Storing'First .. First_Stored + 1));
229 -- Only -gnateD and -gnatep= need storing in ALI file
231 Storing (First_Stored) := 'e';
235 or else (Switch_Chars (Ptr) /= 'D'
236 and then Switch_Chars (Ptr) /= 'p')
242 -- Processing for -gnateD
244 if Switch_Chars (Ptr) = 'D' then
245 Storing (First_Stored + 1 ..
246 First_Stored + Max - Ptr + 1) :=
247 Switch_Chars (Ptr .. Max);
249 (Storing (Storing'First ..
250 First_Stored + Max - Ptr + 1));
252 -- Processing for -gnatep=
262 if Switch_Chars (Ptr) = '=' then
266 -- To normalize, always put a '=' after -gnatep.
267 -- Because that could lengthen the switch string,
268 -- declare a local variable.
271 To_Store : String (1 .. Max - Ptr + 9);
273 To_Store (1 .. 8) := "-gnatep=";
274 To_Store (9 .. Max - Ptr + 9) :=
275 Switch_Chars (Ptr .. Max);
276 Add_Switch_Component (To_Store);
283 Storing (First_Stored) := 'i';
292 C := Switch_Chars (Ptr);
301 Storing (First_Stored + 1) := C;
303 (Storing (Storing'First .. First_Stored + 1));
311 -- -gnatR may be followed by '0', '1', '2' or '3',
315 Last_Stored := First_Stored;
316 Storing (Last_Stored) := 'R';
320 and then Switch_Chars (Ptr) in '0' .. '9'
322 C := Switch_Chars (Ptr);
324 if C in '4' .. '9' then
329 Last_Stored := Last_Stored + 1;
330 Storing (Last_Stored) := C;
334 and then Switch_Chars (Ptr) = 's' then
335 Last_Stored := Last_Stored + 1;
336 Storing (Last_Stored) := 's';
343 (Storing (Storing'First .. Last_Stored));
347 when 'V' | 'w' | 'y' =>
348 Storing (First_Stored) := C;
354 (Storing (Storing'First .. First_Stored));
362 while Ptr <= Max loop
363 C := Switch_Chars (Ptr);
369 Storing (First_Stored) = 'y'
371 Last_Stored := First_Stored + 1;
372 Storing (Last_Stored) := 'M';
374 while Ptr <= Max loop
375 C := Switch_Chars (Ptr);
376 exit when C not in '0' .. '9';
377 Last_Stored := Last_Stored + 1;
378 Storing (Last_Stored) := C;
382 -- If there is no digit after -gnatyM,
383 -- the switch is invalid.
385 if Last_Stored = First_Stored + 1 then
391 (Storing (Storing'First .. Last_Stored));
394 -- All other switches are -gnatxx
397 Storing (First_Stored + 1) := C;
399 (Storing (Storing'First .. First_Stored + 1));
406 Last_Stored := First_Stored;
407 Storing (Last_Stored) := C;
410 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
418 Last_Stored := Last_Stored + 1;
419 Storing (Last_Stored) := '5';
421 (Storing (Storing'First .. Last_Stored));
428 Last_Stored := First_Stored;
429 Storing (Last_Stored) := '8';
432 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
440 Last_Stored := Last_Stored + 1;
441 Storing (Last_Stored) := '3';
443 (Storing (Storing'First .. Last_Stored));
447 -- Not a valid switch
457 end Normalize_Compiler_Switches;
459 function Normalize_Compiler_Switches
460 (Switch_Chars : String)
466 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
469 return (1 .. 0 => null);
471 return Global_Switches (Global_Switches'First .. Last);
474 end Normalize_Compiler_Switches;
476 ------------------------
477 -- Scan_Make_Switches --
478 ------------------------
480 procedure Scan_Make_Switches
481 (Switch_Chars : String;
482 Success : out Boolean)
484 Ptr : Integer := Switch_Chars'First;
485 Max : constant Integer := Switch_Chars'Last;
486 C : Character := ' ';
489 -- Assume a good switch
493 -- Skip past the initial character (must be the switch character)
496 Bad_Switch (Switch_Chars);
502 -- A little check, "gnat" at the start of a switch is for the compiler
504 if Switch_Chars'Length >= Ptr + 3
505 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
511 C := Switch_Chars (Ptr);
513 -- Multiple character switches
515 if Switch_Chars'Length > 2 then
516 if Switch_Chars = "--create-missing-dirs" then
517 Setup_Projects := True;
519 elsif C = 'v' and then Switch_Chars'Length = 3 then
521 Verbose_Mode := True;
523 case Switch_Chars (Ptr) is
525 Verbosity_Level := Opt.Low;
528 Verbosity_Level := Opt.Medium;
531 Verbosity_Level := Opt.High;
539 -- Note: for the debug switch, the remaining characters in this
540 -- switch field must all be debug flags, since all valid switch
541 -- characters are also valid debug characters. This switch is not
542 -- documented on purpose because it is only used by the
545 -- Loop to scan out debug flags
549 C := Switch_Chars (Ptr);
551 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
554 Bad_Switch (Switch_Chars);
561 case Switch_Chars (Ptr) is
563 -- Processing for eI switch
567 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
570 Bad_Switch (Switch_Chars);
573 -- Processing for eL switch
577 Bad_Switch (Switch_Chars);
580 Follow_Links := True;
583 -- Processing for eS switch
587 Bad_Switch (Switch_Chars);
590 Commands_To_Stdout := True;
594 Bad_Switch (Switch_Chars);
603 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
606 Bad_Switch (Switch_Chars);
609 Maximum_Processes := Positive (Max_Proc);
613 elsif C = 'w' and then Switch_Chars'Length = 3 then
616 if Switch_Chars = "-we" then
617 Warning_Mode := Treat_As_Error;
619 elsif Switch_Chars = "-wn" then
620 Warning_Mode := Normal;
622 elsif Switch_Chars = "-ws" then
623 Warning_Mode := Suppress;
633 -- Single-character switches
641 Check_Readonly_Files := True;
643 -- Processing for b switch
649 -- Processing for B switch
652 Build_Bind_And_Link_Full_Project := True;
654 -- Processing for c switch
657 Compile_Only := True;
660 -- Processing for C switch
663 Create_Mapping_File := True;
665 -- Processing for D switch
668 if Object_Directory_Present then
669 Osint.Fail ("duplicate -D switch");
672 Object_Directory_Present := True;
675 -- Processing for f switch
678 Force_Compilations := True;
680 -- Processing for F switch
683 Full_Path_Name_For_Brief_Errors := True;
685 -- Processing for h switch
688 Usage_Requested := True;
690 -- Processing for i switch
693 In_Place_Mode := True;
695 -- Processing for j switch
698 -- -j not followed by a number is an error
700 Bad_Switch (Switch_Chars);
702 -- Processing for k switch
707 -- Processing for l switch
713 -- Processing for M switch
716 List_Dependencies := True;
718 -- Processing for n switch
721 Do_Not_Execute := True;
723 -- Processing for o switch
726 if Output_File_Name_Present then
727 Osint.Fail ("duplicate -o switch");
729 Output_File_Name_Present := True;
732 -- Processing for p switch
735 Setup_Projects := True;
737 -- Processing for q switch
740 Quiet_Output := True;
742 -- Processing for R switch
745 Run_Path_Option := False;
747 -- Processing for s switch
751 Check_Switches := True;
753 -- Processing for v switch
756 Verbose_Mode := True;
757 Verbosity_Level := Opt.High;
759 -- Processing for x switch
762 External_Unit_Compilation_Allowed := True;
764 -- Processing for z switch
767 No_Main_Subprogram := True;
769 -- Any other small letter is an illegal switch
772 if C in 'a' .. 'z' then
773 Bad_Switch (Switch_Chars);
782 end Scan_Make_Switches;