1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2009, 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 Makeutl; use Makeutl;
28 with Osint; use Osint;
31 with Prj.Ext; use Prj.Ext;
34 package body Switch.M is
36 package Normalized_Switches is new Table.Table
37 (Table_Component_Type => String_Access,
38 Table_Index_Type => Integer,
41 Table_Increment => 100,
42 Table_Name => "Switch.M.Normalized_Switches");
43 -- This table is used to keep the normalized switches, so that they may be
44 -- reused for subsequent invocations of Normalize_Compiler_Switches with
47 Initial_Number_Of_Switches : constant := 10;
49 Global_Switches : Argument_List_Access := null;
50 -- Used by function Normalize_Compiler_Switches
52 ---------------------------------
53 -- Normalize_Compiler_Switches --
54 ---------------------------------
56 procedure Normalize_Compiler_Switches
57 (Switch_Chars : String;
58 Switches : in out Argument_List_Access;
61 Switch_Starts_With_Gnat : Boolean;
63 Ptr : Integer := Switch_Chars'First;
64 Max : constant Integer := Switch_Chars'Last;
67 Storing : String := Switch_Chars;
68 First_Stored : Positive := Ptr + 1;
69 Last_Stored : Positive := First_Stored;
71 procedure Add_Switch_Component (S : String);
72 -- Add a new String_Access component in Switches. If a string equal
73 -- to S is already stored in the table Normalized_Switches, use it.
74 -- Other wise add a new component to the table.
76 --------------------------
77 -- Add_Switch_Component --
78 --------------------------
80 procedure Add_Switch_Component (S : String) is
82 -- If Switches is null, allocate a new array
84 if Switches = null then
85 Switches := new Argument_List (1 .. Initial_Number_Of_Switches);
87 -- Otherwise, if Switches is full, extend it
89 elsif Last = Switches'Last then
91 New_Switches : constant Argument_List_Access :=
93 (1 .. Switches'Length + Switches'Length);
95 New_Switches (1 .. Switches'Length) := Switches.all;
96 Last := Switches'Length;
97 Switches := New_Switches;
101 -- If this is the first switch, Last designates the first component
104 Last := Switches'First;
109 -- Look into the table Normalized_Switches for a similar string.
110 -- If one is found, put it at the added component, and return.
112 for Index in 1 .. Normalized_Switches.Last loop
113 if S = Normalized_Switches.Table (Index).all then
114 Switches (Last) := Normalized_Switches.Table (Index);
119 -- No string equal to S was found in the table Normalized_Switches.
120 -- Add a new component in the table.
122 Switches (Last) := new String'(S);
123 Normalized_Switches.Append (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,
155 -- except -pg, -Wall, -k8, -w
157 if Switch_Chars = "-pg" or else Switch_Chars = "-p" 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");
164 elsif Switch_Chars = "-Wall" then
166 -- The gcc driver adds -gnatwa when -Wall is used
168 Add_Switch_Component ("-gnatwa");
169 Add_Switch_Component ("-Wall");
171 elsif Switch_Chars = "-k8" then
173 -- The gcc driver transforms -k8 into -gnatk8
175 Add_Switch_Component ("-gnatk8");
177 elsif Switch_Chars = "-w" then
179 -- The gcc driver adds -gnatws when -w is used
181 Add_Switch_Component ("-gnatws");
182 Add_Switch_Component ("-w");
184 elsif Switch_Chars'Length > 6
186 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 5)
189 Add_Switch_Component (Switch_Chars);
191 -- When --RTS=mtp is used, the gcc driver adds -mrtp
193 if Switch_Chars = "--RTS=mtp" then
194 Add_Switch_Component ("-mrtp");
197 -- Take only into account switches that are transmitted to
198 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
202 when 'O' | 'W' | 'w' | 'f' | 'd' | 'g' | 'm' =>
203 Add_Switch_Component (Switch_Chars);
216 -- One-letter switches
218 when 'a' | 'A' | 'b' | 'c' | 'D' | 'E' | 'f' |
219 'F' | 'g' | 'h' | 'H' | 'k' | 'l' | 'L' | 'n' | 'N' |
220 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 't' |
221 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
222 Storing (First_Stored) := C;
224 (Storing (Storing'First .. First_Stored));
227 -- One-letter switches followed by a positive number
230 Storing (First_Stored) := C;
231 Last_Stored := First_Stored;
236 or else Switch_Chars (Ptr) not in '0' .. '9';
237 Last_Stored := Last_Stored + 1;
238 Storing (Last_Stored) := Switch_Chars (Ptr);
242 (Storing (Storing'First .. Last_Stored));
245 Storing (First_Stored) := 'd';
249 C := Switch_Chars (Ptr);
250 exit when C = ASCII.NUL or else C = '/'
253 if C in '1' .. '9' or else
254 C in 'a' .. 'z' or else
257 Storing (First_Stored + 1) := C;
259 (Storing (Storing'First .. First_Stored + 1));
271 -- Store -gnateD, -gnatep= and -gnateG in the ALI file.
272 -- The other -gnate switches do not need to be stored.
274 Storing (First_Stored) := 'e';
278 or else (Switch_Chars (Ptr) /= 'D'
279 and then Switch_Chars (Ptr) /= 'G'
280 and then Switch_Chars (Ptr) /= 'p')
286 -- Processing for -gnateD
288 if Switch_Chars (Ptr) = 'D' then
289 Storing (First_Stored + 1 ..
290 First_Stored + Max - Ptr + 1) :=
291 Switch_Chars (Ptr .. Max);
293 (Storing (Storing'First ..
294 First_Stored + Max - Ptr + 1));
296 -- Processing for -gnatep=
298 elsif Switch_Chars (Ptr) = 'p' then
306 if Switch_Chars (Ptr) = '=' then
310 -- To normalize, always put a '=' after -gnatep.
311 -- Because that could lengthen the switch string,
312 -- declare a local variable.
315 To_Store : String (1 .. Max - Ptr + 9);
317 To_Store (1 .. 8) := "-gnatep=";
318 To_Store (9 .. Max - Ptr + 9) :=
319 Switch_Chars (Ptr .. Max);
320 Add_Switch_Component (To_Store);
323 elsif Switch_Chars (Ptr) = 'G' then
324 Add_Switch_Component ("-gnateG");
330 Storing (First_Stored) := 'i';
339 C := Switch_Chars (Ptr);
348 Storing (First_Stored + 1) := C;
350 (Storing (Storing'First .. First_Stored + 1));
358 -- -gnatR may be followed by '0', '1', '2' or '3',
362 Last_Stored := First_Stored;
363 Storing (Last_Stored) := 'R';
367 and then Switch_Chars (Ptr) in '0' .. '9'
369 C := Switch_Chars (Ptr);
371 if C in '4' .. '9' then
376 Last_Stored := Last_Stored + 1;
377 Storing (Last_Stored) := C;
381 and then Switch_Chars (Ptr) = 's'
383 Last_Stored := Last_Stored + 1;
384 Storing (Last_Stored) := 's';
391 (Storing (Storing'First .. Last_Stored));
395 when 'V' | 'w' | 'y' =>
396 Storing (First_Stored) := C;
402 (Storing (Storing'First .. First_Stored));
410 -- Loop through remaining switch characters in string
412 while Ptr <= Max loop
413 C := Switch_Chars (Ptr);
418 if C = 'M' and then Storing (First_Stored) = 'y' then
419 Last_Stored := First_Stored + 1;
420 Storing (Last_Stored) := 'M';
421 while Ptr <= Max loop
422 C := Switch_Chars (Ptr);
423 exit when C not in '0' .. '9';
424 Last_Stored := Last_Stored + 1;
425 Storing (Last_Stored) := C;
429 -- If there is no digit after -gnatyM,
430 -- the switch is invalid.
432 if Last_Stored = First_Stored + 1 then
438 (Storing (Storing'First .. Last_Stored));
443 elsif C = '.' and then Ptr <= Max then
444 Storing (First_Stored + 1) := '.';
445 Storing (First_Stored + 2) := Switch_Chars (Ptr);
448 (Storing (Storing'First .. First_Stored + 2));
450 -- All other switches are -gnatxx
453 Storing (First_Stored + 1) := C;
455 (Storing (Storing'First .. First_Stored + 1));
462 Last_Stored := First_Stored;
463 Storing (Last_Stored) := C;
466 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
474 Last_Stored := Last_Stored + 1;
475 Storing (Last_Stored) := '5';
477 (Storing (Storing'First .. Last_Stored));
484 Last_Stored := First_Stored;
485 Storing (Last_Stored) := '8';
488 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
496 Last_Stored := Last_Stored + 1;
497 Storing (Last_Stored) := '3';
499 (Storing (Storing'First .. Last_Stored));
503 -- Not a valid switch
513 end Normalize_Compiler_Switches;
515 function Normalize_Compiler_Switches
516 (Switch_Chars : String) return Argument_List
521 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
524 return (1 .. 0 => null);
526 return Global_Switches (Global_Switches'First .. Last);
528 end Normalize_Compiler_Switches;
530 ------------------------
531 -- Scan_Make_Switches --
532 ------------------------
534 procedure Scan_Make_Switches
535 (Switch_Chars : String;
536 Success : out Boolean)
538 Ptr : Integer := Switch_Chars'First;
539 Max : constant Integer := Switch_Chars'Last;
540 C : Character := ' ';
543 -- Assume a good switch
547 -- Skip past the initial character (must be the switch character)
550 Bad_Switch (Switch_Chars);
556 -- A little check, "gnat" at the start of a switch is for the compiler
558 if Switch_Chars'Length >= Ptr + 3
559 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
565 C := Switch_Chars (Ptr);
567 -- Multiple character switches
569 if Switch_Chars'Length > 2 then
570 if Switch_Chars = "--create-missing-dirs" then
571 Setup_Projects := True;
573 elsif Switch_Chars'Length > Subdirs_Option'Length
576 (Switch_Chars'First ..
577 Switch_Chars'First + Subdirs_Option'Length - 1) =
583 (Switch_Chars'First + Subdirs_Option'Length ..
586 elsif Switch_Chars (Ptr) = '-' then
587 Bad_Switch (Switch_Chars);
589 elsif Switch_Chars'Length > 3
590 and then Switch_Chars (Ptr .. Ptr + 1) = "aP"
592 Add_Search_Project_Directory
593 (Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
595 elsif C = 'v' and then Switch_Chars'Length = 3 then
597 Verbose_Mode := True;
599 case Switch_Chars (Ptr) is
601 Verbosity_Level := Opt.Low;
604 Verbosity_Level := Opt.Medium;
607 Verbosity_Level := Opt.High;
615 -- Note: for the debug switch, the remaining characters in this
616 -- switch field must all be debug flags, since all valid switch
617 -- characters are also valid debug characters. This switch is not
618 -- documented on purpose because it is only used by the
621 -- Loop to scan out debug flags
625 C := Switch_Chars (Ptr);
627 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
630 Bad_Switch (Switch_Chars);
637 case Switch_Chars (Ptr) is
639 -- Processing for eI switch
643 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
646 Bad_Switch (Switch_Chars);
649 -- Processing for eL switch
653 Bad_Switch (Switch_Chars);
656 Follow_Links_For_Files := True;
659 -- Processing for eS switch
663 Bad_Switch (Switch_Chars);
666 Commands_To_Stdout := True;
670 Bad_Switch (Switch_Chars);
679 Scan_Pos (Switch_Chars, Max, Ptr, Max_Proc, C);
682 Bad_Switch (Switch_Chars);
685 Maximum_Processes := Positive (Max_Proc);
689 elsif C = 'w' and then Switch_Chars'Length = 3 then
692 if Switch_Chars = "-we" then
693 Warning_Mode := Treat_As_Error;
695 elsif Switch_Chars = "-wn" then
696 Warning_Mode := Normal;
698 elsif Switch_Chars = "-ws" then
699 Warning_Mode := Suppress;
709 -- Single-character switches
717 Check_Readonly_Files := True;
719 -- Processing for b switch
725 -- Processing for B switch
728 Build_Bind_And_Link_Full_Project := True;
730 -- Processing for c switch
733 Compile_Only := True;
736 -- Processing for C switch
739 Create_Mapping_File := True;
741 -- Processing for D switch
744 if Object_Directory_Present then
745 Osint.Fail ("duplicate -D switch");
748 Object_Directory_Present := True;
751 -- Processing for f switch
754 Force_Compilations := True;
756 -- Processing for F switch
759 Full_Path_Name_For_Brief_Errors := True;
761 -- Processing for h switch
764 Usage_Requested := True;
766 -- Processing for i switch
769 In_Place_Mode := True;
771 -- Processing for j switch
774 -- -j not followed by a number is an error
776 Bad_Switch (Switch_Chars);
778 -- Processing for k switch
783 -- Processing for l switch
789 -- Processing for M switch
792 List_Dependencies := True;
794 -- Processing for n switch
797 Do_Not_Execute := True;
799 -- Processing for o switch
802 if Output_File_Name_Present then
803 Osint.Fail ("duplicate -o switch");
805 Output_File_Name_Present := True;
808 -- Processing for p switch
811 Setup_Projects := True;
813 -- Processing for q switch
816 Quiet_Output := True;
818 -- Processing for R switch
821 Run_Path_Option := False;
823 -- Processing for s switch
827 Check_Switches := True;
829 -- Processing for v switch
832 Verbose_Mode := True;
833 Verbosity_Level := Opt.High;
835 -- Processing for x switch
838 External_Unit_Compilation_Allowed := True;
840 -- Processing for z switch
843 No_Main_Subprogram := True;
845 -- Any other small letter is an illegal switch
848 if C in 'a' .. 'z' then
849 Bad_Switch (Switch_Chars);
858 end Scan_Make_Switches;