1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2011, 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.Env; use Prj.Env;
34 with System.Multiprocessors; use System.Multiprocessors;
36 package body Switch.M is
38 package Normalized_Switches is new Table.Table
39 (Table_Component_Type => String_Access,
40 Table_Index_Type => Integer,
43 Table_Increment => 100,
44 Table_Name => "Switch.M.Normalized_Switches");
45 -- This table is used to keep the normalized switches, so that they may be
46 -- reused for subsequent invocations of Normalize_Compiler_Switches with
49 Initial_Number_Of_Switches : constant := 10;
51 Global_Switches : Argument_List_Access := null;
52 -- Used by function Normalize_Compiler_Switches
54 ---------------------------------
55 -- Normalize_Compiler_Switches --
56 ---------------------------------
58 procedure Normalize_Compiler_Switches
59 (Switch_Chars : String;
60 Switches : in out Argument_List_Access;
63 Switch_Starts_With_Gnat : Boolean;
65 Ptr : Integer := Switch_Chars'First;
66 Max : constant Integer := Switch_Chars'Last;
69 Storing : String := Switch_Chars;
70 First_Stored : Positive := Ptr + 1;
71 Last_Stored : Positive := First_Stored;
73 procedure Add_Switch_Component (S : String);
74 -- Add a new String_Access component in Switches. If a string equal
75 -- to S is already stored in the table Normalized_Switches, use it.
76 -- Otherwise add a new component to the table.
78 --------------------------
79 -- Add_Switch_Component --
80 --------------------------
82 procedure Add_Switch_Component (S : String) is
84 -- If Switches is null, allocate a new array
86 if Switches = null then
87 Switches := new Argument_List (1 .. Initial_Number_Of_Switches);
89 -- Otherwise, if Switches is full, extend it
91 elsif Last = Switches'Last then
93 New_Switches : constant Argument_List_Access :=
95 (1 .. Switches'Length + Switches'Length);
97 New_Switches (1 .. Switches'Length) := Switches.all;
98 Last := Switches'Length;
99 Switches := New_Switches;
103 -- If this is the first switch, Last designates the first component
106 Last := Switches'First;
111 -- Look into the table Normalized_Switches for a similar string.
112 -- If one is found, put it at the added component, and return.
114 for Index in 1 .. Normalized_Switches.Last loop
115 if S = Normalized_Switches.Table (Index).all then
116 Switches (Last) := Normalized_Switches.Table (Index);
121 -- No string equal to S was found in the table Normalized_Switches.
122 -- Add a new component in the table.
124 Switches (Last) := new String'(S);
125 Normalized_Switches.Append (Switches (Last));
126 end Add_Switch_Component;
128 -- Start of processing for Normalize_Compiler_Switches
133 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
139 Switch_Starts_With_Gnat :=
140 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
142 if Switch_Starts_With_Gnat then
147 while Ptr <= Max loop
148 C := Switch_Chars (Ptr);
150 -- Processing for a switch
152 case Switch_Starts_With_Gnat is
156 -- All switches that don't start with -gnat stay as is,
157 -- except -pg, -Wall, -k8, -w
159 if Switch_Chars = "-pg" or else Switch_Chars = "-p" then
161 -- The gcc driver converts -pg to -p, so that is what
162 -- is stored in the ALI file.
164 Add_Switch_Component ("-p");
166 elsif Switch_Chars = "-Wall" then
168 -- The gcc driver adds -gnatwa when -Wall is used
170 Add_Switch_Component ("-gnatwa");
171 Add_Switch_Component ("-Wall");
173 elsif Switch_Chars = "-k8" then
175 -- The gcc driver transforms -k8 into -gnatk8
177 Add_Switch_Component ("-gnatk8");
179 elsif Switch_Chars = "-w" then
181 -- The gcc driver adds -gnatws when -w is used
183 Add_Switch_Component ("-gnatws");
184 Add_Switch_Component ("-w");
186 elsif Switch_Chars'Length > 6
188 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 5)
191 Add_Switch_Component (Switch_Chars);
193 -- When --RTS=mtp is used, the gcc driver adds -mrtp
195 if Switch_Chars = "--RTS=mtp" then
196 Add_Switch_Component ("-mrtp");
199 -- Switch for universal addressing on AAMP target
201 elsif Switch_Chars'Length >= 5
204 (Switch_Chars'First .. Switch_Chars'First + 4) = "-univ"
206 Add_Switch_Component (Switch_Chars);
208 -- Switch for specifying AAMP target library
210 elsif Switch_Chars'Length > 13
212 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 12)
215 Add_Switch_Component (Switch_Chars);
217 -- Take only into account switches that are transmitted to
218 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
222 when 'O' | 'W' | 'w' | 'f' | 'd' | 'g' | 'm' =>
223 Add_Switch_Component (Switch_Chars);
236 -- One-letter switches
238 when 'a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'E' | 'f' |
239 'F' | 'g' | 'h' | 'H' | 'I' | 'L' | 'n' | 'N' |
240 'o' | 'p' | 'P' | 'q' | 'Q' | 'r' | 's' | 'S' |
241 't' | 'u' | 'U' | 'v' | 'x' | 'X' | 'Z' =>
242 Storing (First_Stored) := C;
244 (Storing (Storing'First .. First_Stored));
247 -- One-letter switches followed by a positive number
249 when 'D' | 'G' | 'j' | 'k' | 'm' | 'T' =>
250 Storing (First_Stored) := C;
251 Last_Stored := First_Stored;
253 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
260 or else Switch_Chars (Ptr) not in '0' .. '9';
261 Last_Stored := Last_Stored + 1;
262 Storing (Last_Stored) := Switch_Chars (Ptr);
266 (Storing (Storing'First .. Last_Stored));
269 Storing (First_Stored) := 'd';
273 C := Switch_Chars (Ptr);
274 exit when C = ASCII.NUL or else C = '/'
277 if C in '1' .. '9' or else
278 C in 'a' .. 'z' or else
281 Storing (First_Stored + 1) := C;
283 (Storing (Storing'First .. First_Stored + 1));
295 -- Some of the gnate... switches are not stored
297 Storing (First_Stored) := 'e';
305 case Switch_Chars (Ptr) is
308 Storing (First_Stored + 1 ..
309 First_Stored + Max - Ptr + 1) :=
310 Switch_Chars (Ptr .. Max);
312 (Storing (Storing'First ..
313 First_Stored + Max - Ptr + 1));
318 Add_Switch_Component ("-gnateG");
324 First : constant Positive := Ptr - 1;
326 if Ptr <= Max and then
327 Switch_Chars (Ptr) = '='
332 while Ptr <= Max and then
333 Switch_Chars (Ptr) in '0' .. '9'
338 Storing (First_Stored + 1 ..
339 First_Stored + Ptr - First) :=
340 Switch_Chars (First .. Ptr - 1);
342 (Storing (Storing'First ..
343 First_Stored + Ptr - First));
354 if Switch_Chars (Ptr) = '=' then
358 -- To normalize, always put a '=' after
359 -- -gnatep. Because that could lengthen the
360 -- switch string, declare a local variable.
363 To_Store : String (1 .. Max - Ptr + 9);
365 To_Store (1 .. 8) := "-gnatep=";
366 To_Store (9 .. Max - Ptr + 9) :=
367 Switch_Chars (Ptr .. Max);
368 Add_Switch_Component (To_Store);
375 Add_Switch_Component ("-gnateS");
384 Storing (First_Stored) := 'i';
393 C := Switch_Chars (Ptr);
402 Storing (First_Stored + 1) := C;
404 (Storing (Storing'First .. First_Stored + 1));
412 -- -gnatl may be -gnatl=<file name>
417 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
418 Add_Switch_Component ("-gnatl");
422 ("-gnatl" & Switch_Chars (Ptr .. Max));
426 -- -gnatR may be followed by '0', '1', '2' or '3',
430 Last_Stored := First_Stored;
431 Storing (Last_Stored) := 'R';
435 and then Switch_Chars (Ptr) in '0' .. '9'
437 C := Switch_Chars (Ptr);
439 if C in '4' .. '9' then
444 Last_Stored := Last_Stored + 1;
445 Storing (Last_Stored) := C;
449 and then Switch_Chars (Ptr) = 's'
451 Last_Stored := Last_Stored + 1;
452 Storing (Last_Stored) := 's';
459 (Storing (Storing'First .. Last_Stored));
461 -- -gnatWx, x = 'h'. 'u', 's', 'e', '8' or 'b'
464 Storing (First_Stored) := 'W';
468 case Switch_Chars (Ptr) is
469 when 'h' | 'u' | 's' | 'e' | '8' | 'b' =>
470 Storing (First_Stored + 1) := Switch_Chars (Ptr);
472 (Storing (Storing'First .. First_Stored + 1));
483 when 'V' | 'w' | 'y' =>
484 Storing (First_Stored) := C;
490 (Storing (Storing'First .. First_Stored));
498 -- Loop through remaining switch characters in string
500 while Ptr <= Max loop
501 C := Switch_Chars (Ptr);
506 if C = 'M' and then Storing (First_Stored) = 'y' then
507 Last_Stored := First_Stored + 1;
508 Storing (Last_Stored) := 'M';
509 while Ptr <= Max loop
510 C := Switch_Chars (Ptr);
511 exit when C not in '0' .. '9';
512 Last_Stored := Last_Stored + 1;
513 Storing (Last_Stored) := C;
517 -- If there is no digit after -gnatyM,
518 -- the switch is invalid.
520 if Last_Stored = First_Stored + 1 then
526 (Storing (Storing'First .. Last_Stored));
531 elsif C = '.' and then Ptr <= Max then
532 Storing (First_Stored + 1) := '.';
533 Storing (First_Stored + 2) := Switch_Chars (Ptr);
536 (Storing (Storing'First .. First_Stored + 2));
538 -- All other switches are -gnatxx
541 Storing (First_Stored + 1) := C;
543 (Storing (Storing'First .. First_Stored + 1));
550 Last_Stored := First_Stored;
551 Storing (Last_Stored) := C;
554 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
562 Last_Stored := Last_Stored + 1;
563 Storing (Last_Stored) := '5';
565 (Storing (Storing'First .. Last_Stored));
572 Last_Stored := First_Stored;
573 Storing (Last_Stored) := C;
576 if Ptr /= Max or else Switch_Chars (Ptr) /= '2' then
584 Last_Stored := Last_Stored + 1;
585 Storing (Last_Stored) := '2';
587 (Storing (Storing'First .. Last_Stored));
591 -- -gnat2005 -gnat2012
594 if Ptr + 3 /= Max then
598 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "005" then
599 Last_Stored := First_Stored + 3;
600 Storing (First_Stored .. Last_Stored) := "2005";
602 (Storing (Storing'First .. Last_Stored));
605 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "012" then
606 Last_Stored := First_Stored + 3;
607 Storing (First_Stored .. Last_Stored) := "2012";
609 (Storing (Storing'First .. Last_Stored));
624 Last_Stored := First_Stored;
625 Storing (Last_Stored) := '8';
628 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
636 Last_Stored := Last_Stored + 1;
637 Storing (Last_Stored) := '3';
639 (Storing (Storing'First .. Last_Stored));
643 -- Not a valid switch
653 end Normalize_Compiler_Switches;
655 function Normalize_Compiler_Switches
656 (Switch_Chars : String) return Argument_List
661 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
664 return (1 .. 0 => null);
666 return Global_Switches (Global_Switches'First .. Last);
668 end Normalize_Compiler_Switches;
670 ------------------------
671 -- Scan_Make_Switches --
672 ------------------------
674 procedure Scan_Make_Switches
675 (Env : in out Prj.Tree.Environment;
676 Switch_Chars : String;
677 Success : out Boolean)
679 Ptr : Integer := Switch_Chars'First;
680 Max : constant Integer := Switch_Chars'Last;
681 C : Character := ' ';
684 -- Assume a good switch
688 -- Skip past the initial character (must be the switch character)
691 Bad_Switch (Switch_Chars);
697 -- A little check, "gnat" at the start of a switch is for the compiler
699 if Switch_Chars'Length >= Ptr + 3
700 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
706 C := Switch_Chars (Ptr);
708 -- Multiple character switches
710 if Switch_Chars'Length > 2 then
711 if Switch_Chars = "--create-missing-dirs" then
712 Setup_Projects := True;
714 elsif Switch_Chars'Length > Subdirs_Option'Length
717 (Switch_Chars'First ..
718 Switch_Chars'First + Subdirs_Option'Length - 1) =
724 (Switch_Chars'First + Subdirs_Option'Length ..
727 elsif Switch_Chars = Makeutl.Unchecked_Shared_Lib_Imports then
728 Opt.Unchecked_Shared_Lib_Imports := True;
730 elsif Switch_Chars = Makeutl.Single_Compile_Per_Obj_Dir_Switch then
731 Opt.One_Compilation_Per_Obj_Dir := True;
733 elsif Switch_Chars (Ptr) = '-' then
734 Bad_Switch (Switch_Chars);
736 elsif Switch_Chars'Length > 3
737 and then Switch_Chars (Ptr .. Ptr + 1) = "aP"
741 Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
743 elsif C = 'v' and then Switch_Chars'Length = 3 then
745 Verbose_Mode := True;
747 case Switch_Chars (Ptr) is
749 Verbosity_Level := Opt.Low;
752 Verbosity_Level := Opt.Medium;
755 Verbosity_Level := Opt.High;
763 -- Note: for the debug switch, the remaining characters in this
764 -- switch field must all be debug flags, since all valid switch
765 -- characters are also valid debug characters. This switch is not
766 -- documented on purpose because it is only used by the
769 -- Loop to scan out debug flags
773 C := Switch_Chars (Ptr);
775 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
778 Bad_Switch (Switch_Chars);
785 case Switch_Chars (Ptr) is
787 -- Processing for eI switch
791 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
794 Bad_Switch (Switch_Chars);
797 -- Processing for eL switch
801 Bad_Switch (Switch_Chars);
804 Follow_Links_For_Files := True;
805 Follow_Links_For_Dirs := True;
808 -- Processing for eS switch
812 Bad_Switch (Switch_Chars);
815 Commands_To_Stdout := True;
819 Bad_Switch (Switch_Chars);
829 Scan_Nat (Switch_Chars, Max, Ptr, Max_Proc, C);
832 Bad_Switch (Switch_Chars);
836 Max_Proc := Nat (Number_Of_CPUs);
843 Maximum_Processes := Positive (Max_Proc);
847 elsif C = 'w' and then Switch_Chars'Length = 3 then
850 if Switch_Chars = "-we" then
851 Warning_Mode := Treat_As_Error;
853 elsif Switch_Chars = "-wn" then
854 Warning_Mode := Normal;
856 elsif Switch_Chars = "-ws" then
857 Warning_Mode := Suppress;
867 -- Single-character switches
875 Check_Readonly_Files := True;
877 -- Processing for b switch
883 -- Processing for B switch
886 Build_Bind_And_Link_Full_Project := True;
888 -- Processing for c switch
891 Compile_Only := True;
894 -- Processing for C switch
897 Opt.Create_Mapping_File := True;
899 -- Processing for D switch
902 if Object_Directory_Present then
903 Osint.Fail ("duplicate -D switch");
906 Object_Directory_Present := True;
909 -- Processing for f switch
912 Force_Compilations := True;
914 -- Processing for F switch
917 Full_Path_Name_For_Brief_Errors := True;
919 -- Processing for h switch
922 Usage_Requested := True;
924 -- Processing for i switch
927 In_Place_Mode := True;
929 -- Processing for j switch
932 -- -j not followed by a number is an error
934 Bad_Switch (Switch_Chars);
936 -- Processing for k switch
941 -- Processing for l switch
947 -- Processing for M switch
950 List_Dependencies := True;
952 -- Processing for n switch
955 Do_Not_Execute := True;
957 -- Processing for o switch
960 if Output_File_Name_Present then
961 Osint.Fail ("duplicate -o switch");
963 Output_File_Name_Present := True;
966 -- Processing for p switch
969 Setup_Projects := True;
971 -- Processing for q switch
974 Quiet_Output := True;
976 -- Processing for R switch
979 Run_Path_Option := False;
981 -- Processing for s switch
985 Check_Switches := True;
987 -- Processing for v switch
990 Verbose_Mode := True;
991 Verbosity_Level := Opt.High;
993 -- Processing for x switch
996 External_Unit_Compilation_Allowed := True;
997 Use_Include_Path_File := True;
999 -- Processing for z switch
1002 No_Main_Subprogram := True;
1004 -- Any other small letter is an illegal switch
1007 if C in 'a' .. 'z' then
1008 Bad_Switch (Switch_Chars);
1017 end Scan_Make_Switches;