1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2003 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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 : Argument_List_Access := new Argument_List
90 (1 .. Switches'Length + Switches'Length);
92 New_Switches (1 .. Switches'Length) := Switches.all;
93 Last := Switches'Length;
94 Switches := New_Switches;
98 -- If this is the first switch, Last designates the first component
100 Last := Switches'First;
106 -- Look into the table Normalized_Switches for a similar string.
107 -- If one is found, put it at the added component, and return.
109 for Index in 1 .. Normalized_Switches.Last loop
110 if S = Normalized_Switches.Table (Index).all then
111 Switches (Last) := Normalized_Switches.Table (Index);
116 -- No string equal to S was found in the table Normalized_Switches.
117 -- Add a new component in the table.
119 Switches (Last) := new String'(S);
120 Normalized_Switches.Increment_Last;
121 Normalized_Switches.Table (Normalized_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,
156 if Switch_Chars = "-pg" 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");
164 Add_Switch_Component (Switch_Chars);
173 -- One-letter switches
175 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
176 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
177 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
178 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
179 Storing (First_Stored) := C;
181 (Storing (Storing'First .. First_Stored));
184 -- One-letter switches followed by a positive number
187 Storing (First_Stored) := C;
188 Last_Stored := First_Stored;
193 or else Switch_Chars (Ptr) not in '0' .. '9';
194 Last_Stored := Last_Stored + 1;
195 Storing (Last_Stored) := Switch_Chars (Ptr);
199 (Storing (Storing'First .. Last_Stored));
202 Storing (First_Stored) := 'd';
206 C := Switch_Chars (Ptr);
207 exit when C = ASCII.NUL or else C = '/'
210 if C in '1' .. '9' or else
211 C in 'a' .. 'z' or else
214 Storing (First_Stored + 1) := C;
216 (Storing (Storing'First .. First_Stored + 1));
228 -- Only -gnateD and -gnatep= need to be store in an ALI
231 Storing (First_Stored) := 'e';
235 or else (Switch_Chars (Ptr) /= 'D'
236 and then Switch_Chars (Ptr) /= 'p')
242 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));
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);
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);
367 -- 'w' should be skipped in -gnatw
369 if C /= 'w' or else Storing (First_Stored) /= 'w' then
374 and then Storing (First_Stored) = 'y' then
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));
408 -- Not a valid switch
418 end Normalize_Compiler_Switches;
420 function Normalize_Compiler_Switches
421 (Switch_Chars : String)
427 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
430 return (1 .. 0 => null);
433 return Global_Switches (Global_Switches'First .. Last);
436 end Normalize_Compiler_Switches;
438 ------------------------
439 -- Scan_Make_Switches --
440 ------------------------
442 procedure Scan_Make_Switches (Switch_Chars : String) is
443 Ptr : Integer := Switch_Chars'First;
444 Max : constant Integer := Switch_Chars'Last;
445 C : Character := ' ';
448 -- Skip past the initial character (must be the switch character)
457 -- A little check, "gnat" at the start of a switch is not allowed
458 -- except for the compiler (where it was already removed)
460 if Switch_Chars'Length >= Ptr + 3
461 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
464 ("invalid switch: """, Switch_Chars, """ (gnat not needed here)");
467 -- Loop to scan through switches given in switch string
470 C := Switch_Chars (Ptr);
472 -- Processing for a switch
478 Check_Readonly_Files := True;
480 -- Processing for b switch
487 -- Processing for c switch
491 Compile_Only := True;
494 -- Processing for C switch
498 Create_Mapping_File := True;
500 -- Processing for D switch
505 if Object_Directory_Present then
506 Osint.Fail ("duplicate -D switch");
509 Object_Directory_Present := True;
512 -- Processing for d switch
516 -- Note: for the debug switch, the remaining characters in this
517 -- switch field must all be debug flags, since all valid switch
518 -- characters are also valid debug characters. This switch is not
519 -- documented on purpose because it is only used by the
522 -- Loop to scan out debug flags
526 C := Switch_Chars (Ptr);
527 exit when C = ASCII.NUL or else C = '/' or else C = '-';
529 if C in '1' .. '9' or else
530 C in 'a' .. 'z' or else
539 -- Make sure Zero_Cost_Exceptions is set if gnatdX set. This
540 -- is for backwards compatibility with old versions and usage.
542 if Debug_Flag_XX then
543 Zero_Cost_Exceptions_Set := True;
544 Zero_Cost_Exceptions_Val := True;
549 -- Processing for f switch
553 Force_Compilations := True;
555 -- Processing for F switch
559 Full_Path_Name_For_Brief_Errors := True;
561 -- Processing for h switch
565 Usage_Requested := True;
567 -- Processing for i switch
571 In_Place_Mode := True;
573 -- Processing for j switch
581 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc);
582 Maximum_Processes := Positive (Max_Proc);
585 -- Processing for k switch
591 -- Processing for l switch
600 List_Dependencies := True;
602 -- Processing for n switch
606 Do_Not_Execute := True;
608 -- Processing for o switch
613 if Output_File_Name_Present then
614 raise Too_Many_Output_Files;
616 Output_File_Name_Present := True;
619 -- Processing for q switch
623 Quiet_Output := True;
625 -- Processing for R switch
629 Run_Path_Option := False;
631 -- Processing for s switch
635 Check_Switches := True;
637 -- Processing for v switch
641 Verbose_Mode := True;
643 -- Processing for z switch
647 No_Main_Subprogram := True;
649 -- Ignore extra switch character
654 -- Anything else is an error (illegal switch character)
662 Osint.Fail ("invalid switch: ", Switch_Chars);
669 Osint.Fail ("invalid switch: ", (1 => C));
671 when Bad_Switch_Value =>
672 Osint.Fail ("numeric value out of range for switch: ", (1 => C));
674 when Missing_Switch_Value =>
675 Osint.Fail ("missing numeric value for switch: ", (1 => C));
677 when Too_Many_Output_Files =>
678 Osint.Fail ("duplicate -o switch");
680 end Scan_Make_Switches;