1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2016, 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
155 -- All switches that don't start with -gnat stay as is,
156 -- except -pg, -Wall, -k8, -w
158 if Switch_Chars = "-pg" or else Switch_Chars = "-p" then
160 -- The gcc driver converts -pg to -p, so that is what
161 -- is stored in the ALI file.
163 Add_Switch_Component ("-p");
165 elsif Switch_Chars = "-Wall" then
167 -- The gcc driver adds -gnatwa when -Wall is used
169 Add_Switch_Component ("-gnatwa");
170 Add_Switch_Component ("-Wall");
172 elsif Switch_Chars = "-k8" then
174 -- The gcc driver transforms -k8 into -gnatk8
176 Add_Switch_Component ("-gnatk8");
178 elsif Switch_Chars = "-w" then
180 -- The gcc driver adds -gnatws when -w is used
182 Add_Switch_Component ("-gnatws");
183 Add_Switch_Component ("-w");
185 elsif Switch_Chars'Length > 6
187 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 5)
190 Add_Switch_Component (Switch_Chars);
192 -- When --RTS=mtp is used, the gcc driver adds -mrtp
194 if Switch_Chars = "--RTS=mtp" then
195 Add_Switch_Component ("-mrtp");
198 -- Special case for -fstack-check (alias for
199 -- -fstack-check=specific)
201 elsif Switch_Chars = "-fstack-check" then
202 Add_Switch_Component ("-fstack-check=specific");
204 -- Take only into account switches that are transmitted to
205 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
209 when 'O' | 'W' | 'w' | 'f' | 'd' | 'g' | 'm' =>
210 Add_Switch_Component (Switch_Chars);
222 -- One-letter switches
224 when 'a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'E' | 'f' | 'F'
225 | 'g' | 'h' | 'H' | 'I' | 'L' | 'N' | 'p' | 'P' | 'q'
226 | 'Q' | 'r' | 's' | 'S' | 't' | 'u' | 'U' | 'v' | 'x'
229 Storing (First_Stored) := C;
231 (Storing (Storing'First .. First_Stored));
234 -- One-letter switches followed by a positive number
236 when 'D' | 'G' | 'j' | 'k' | 'm' | 'T' =>
237 Storing (First_Stored) := C;
238 Last_Stored := First_Stored;
240 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
247 or else Switch_Chars (Ptr) not in '0' .. '9';
248 Last_Stored := Last_Stored + 1;
249 Storing (Last_Stored) := Switch_Chars (Ptr);
253 (Storing (Storing'First .. Last_Stored));
256 Storing (First_Stored) := 'd';
260 C := Switch_Chars (Ptr);
261 exit when C = ASCII.NUL or else C = '/'
264 if C in '1' .. '9' or else
265 C in 'a' .. 'z' or else
268 Storing (First_Stored + 1) := C;
270 (Storing (Storing'First .. First_Stored + 1));
282 -- Some of the gnate... switches are not stored
284 Storing (First_Stored) := 'e';
292 case Switch_Chars (Ptr) is
295 Add_Switch_Component ("-gnateA");
298 Storing (First_Stored + 1 ..
299 First_Stored + Max - Ptr + 1) :=
300 Switch_Chars (Ptr .. Max);
302 (Storing (Storing'First ..
303 First_Stored + Max - Ptr + 1));
306 when 'E' | 'F' | 'G' | 'S' | 'u' | 'V' | 'Y' =>
308 ("-gnate" & Switch_Chars (Ptr));
313 First : constant Positive := Ptr;
318 if Ptr <= Max and then
319 Switch_Chars (Ptr) = '='
324 while Ptr <= Max and then
325 Switch_Chars (Ptr) in '0' .. '9'
330 Storing (First_Stored + 1 ..
331 First_Stored + Ptr - First) :=
332 Switch_Chars (First .. Ptr - 1);
334 (Storing (Storing'First ..
335 First_Stored + Ptr - First));
340 Add_Switch_Component ("-gnatel");
344 Add_Switch_Component ("-gnateL");
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);
380 Storing (First_Stored) := 'i';
389 C := Switch_Chars (Ptr);
398 Storing (First_Stored + 1) := C;
400 (Storing (Storing'First .. First_Stored + 1));
408 -- -gnatl may be -gnatl=<file name>
413 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
414 Add_Switch_Component ("-gnatl");
418 ("-gnatl" & Switch_Chars (Ptr .. Max));
422 -- -gnatn may be -gnatn, -gnatn1, or -gnatn2
425 Last_Stored := First_Stored;
426 Storing (Last_Stored) := 'n';
430 and then Switch_Chars (Ptr) in '1' .. '2'
432 Last_Stored := Last_Stored + 1;
433 Storing (Last_Stored) := Switch_Chars (Ptr);
438 (Storing (Storing'First .. Last_Stored));
440 -- -gnato may be -gnatox or -gnatoxx, with x=0/1/2/3
443 Last_Stored := First_Stored;
444 Storing (Last_Stored) := 'o';
448 and then Switch_Chars (Ptr) in '0' .. '3'
450 Last_Stored := Last_Stored + 1;
451 Storing (Last_Stored) := Switch_Chars (Ptr);
455 and then Switch_Chars (Ptr) in '0' .. '3'
457 Last_Stored := Last_Stored + 1;
458 Storing (Last_Stored) := Switch_Chars (Ptr);
464 (Storing (Storing'First .. Last_Stored));
466 -- -gnatR may be followed by '0', '1', '2' or '3',
470 Last_Stored := First_Stored;
471 Storing (Last_Stored) := 'R';
475 and then Switch_Chars (Ptr) in '0' .. '9'
477 C := Switch_Chars (Ptr);
479 if C in '4' .. '9' then
484 Last_Stored := Last_Stored + 1;
485 Storing (Last_Stored) := C;
489 and then Switch_Chars (Ptr) = 's'
491 Last_Stored := Last_Stored + 1;
492 Storing (Last_Stored) := 's';
499 (Storing (Storing'First .. Last_Stored));
501 -- -gnatWx, x = 'h'. 'u', 's', 'e', '8' or 'b'
504 Storing (First_Stored) := 'W';
508 case Switch_Chars (Ptr) is
509 when 'h' | 'u' | 's' | 'e' | '8' | 'b' =>
510 Storing (First_Stored + 1) := Switch_Chars (Ptr);
512 (Storing (Storing'First .. First_Stored + 1));
523 when 'V' | 'w' | 'y' =>
524 Storing (First_Stored) := C;
530 (Storing (Storing'First .. First_Stored));
538 -- Loop through remaining switch characters in string
540 while Ptr <= Max loop
541 C := Switch_Chars (Ptr);
546 if C = 'M' and then Storing (First_Stored) = 'y' then
547 Last_Stored := First_Stored + 1;
548 Storing (Last_Stored) := 'M';
549 while Ptr <= Max loop
550 C := Switch_Chars (Ptr);
551 exit when C not in '0' .. '9';
552 Last_Stored := Last_Stored + 1;
553 Storing (Last_Stored) := C;
557 -- If there is no digit after -gnatyM,
558 -- the switch is invalid.
560 if Last_Stored = First_Stored + 1 then
566 (Storing (Storing'First .. Last_Stored));
571 elsif C = '.' and then Ptr <= Max then
572 Storing (First_Stored + 1) := '.';
573 Storing (First_Stored + 2) := Switch_Chars (Ptr);
576 (Storing (Storing'First .. First_Stored + 2));
578 -- All other switches are -gnatxx
581 Storing (First_Stored + 1) := C;
583 (Storing (Storing'First .. First_Stored + 1));
590 Last_Stored := First_Stored;
591 Storing (Last_Stored) := C;
594 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
602 Last_Stored := Last_Stored + 1;
603 Storing (Last_Stored) := '5';
605 (Storing (Storing'First .. Last_Stored));
612 Last_Stored := First_Stored;
613 Storing (Last_Stored) := C;
616 if Ptr /= Max or else Switch_Chars (Ptr) /= '2' then
624 Last_Stored := Last_Stored + 1;
625 Storing (Last_Stored) := '2';
627 (Storing (Storing'First .. Last_Stored));
631 -- -gnat2005 -gnat2012
634 if Ptr + 3 /= Max then
638 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "005" then
639 Last_Stored := First_Stored + 3;
640 Storing (First_Stored .. Last_Stored) := "2005";
642 (Storing (Storing'First .. Last_Stored));
645 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "012" then
646 Last_Stored := First_Stored + 3;
647 Storing (First_Stored .. Last_Stored) := "2012";
649 (Storing (Storing'First .. Last_Stored));
664 Last_Stored := First_Stored;
665 Storing (Last_Stored) := '8';
668 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
676 Last_Stored := Last_Stored + 1;
677 Storing (Last_Stored) := '3';
679 (Storing (Storing'First .. Last_Stored));
683 -- Not a valid switch
691 end Normalize_Compiler_Switches;
693 function Normalize_Compiler_Switches
694 (Switch_Chars : String) return Argument_List
699 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
702 return (1 .. 0 => null);
704 return Global_Switches (Global_Switches'First .. Last);
706 end Normalize_Compiler_Switches;
708 ------------------------
709 -- Scan_Make_Switches --
710 ------------------------
712 procedure Scan_Make_Switches
713 (Env : in out Prj.Tree.Environment;
714 Switch_Chars : String;
715 Success : out Boolean)
717 Ptr : Integer := Switch_Chars'First;
718 Max : constant Integer := Switch_Chars'Last;
719 C : Character := ' ';
722 -- Assume a good switch
726 -- Skip past the initial character (must be the switch character)
729 Bad_Switch (Switch_Chars);
735 -- A little check, "gnat" at the start of a switch is for the compiler
737 if Switch_Chars'Length >= Ptr + 3
738 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
744 C := Switch_Chars (Ptr);
746 -- Multiple character switches
748 if Switch_Chars'Length > 2 then
749 if Switch_Chars = "--create-missing-dirs" then
750 Setup_Projects := True;
752 elsif Switch_Chars'Length > Subdirs_Option'Length
755 (Switch_Chars'First ..
756 Switch_Chars'First + Subdirs_Option'Length - 1) =
762 (Switch_Chars'First + Subdirs_Option'Length ..
765 elsif Switch_Chars = Makeutl.Unchecked_Shared_Lib_Imports then
766 Opt.Unchecked_Shared_Lib_Imports := True;
768 elsif Switch_Chars = Makeutl.Single_Compile_Per_Obj_Dir_Switch then
769 Opt.One_Compilation_Per_Obj_Dir := True;
771 elsif Switch_Chars = Makeutl.No_Exit_Message_Option then
772 Opt.No_Exit_Message := True;
774 elsif Switch_Chars = Makeutl.Keep_Temp_Files_Option then
775 Opt.Keep_Temporary_Files := True;
777 elsif Switch_Chars (Ptr) = '-' then
778 Bad_Switch (Switch_Chars);
780 elsif Switch_Chars'Length > 3
781 and then Switch_Chars (Ptr .. Ptr + 1) = "aP"
785 Switch_Chars (Ptr + 2 .. Switch_Chars'Last));
787 elsif C = 'v' and then Switch_Chars'Length = 3 then
789 Verbose_Mode := True;
791 case Switch_Chars (Ptr) is
792 when 'l' => Verbosity_Level := Opt.Low;
793 when 'm' => Verbosity_Level := Opt.Medium;
794 when 'h' => Verbosity_Level := Opt.High;
795 when others => Success := False;
800 -- Note: for the debug switch, the remaining characters in this
801 -- switch field must all be debug flags, since all valid switch
802 -- characters are also valid debug characters. This switch is not
803 -- documented on purpose because it is only used by the
806 -- Loop to scan out debug flags
810 C := Switch_Chars (Ptr);
812 if C in 'a' .. 'z' or else C in 'A' .. 'Z' then
815 Bad_Switch (Switch_Chars);
822 case Switch_Chars (Ptr) is
824 -- Processing for eI switch
828 Scan_Pos (Switch_Chars, Max, Ptr, Main_Index, C);
831 Bad_Switch (Switch_Chars);
834 -- Processing for eL switch
838 Bad_Switch (Switch_Chars);
841 Follow_Links_For_Files := True;
842 Follow_Links_For_Dirs := True;
845 -- Processing for eS switch
849 Bad_Switch (Switch_Chars);
852 Commands_To_Stdout := True;
856 Bad_Switch (Switch_Chars);
866 Scan_Nat (Switch_Chars, Max, Ptr, Max_Proc, C);
869 Bad_Switch (Switch_Chars);
873 Max_Proc := Nat (Number_Of_CPUs);
880 Maximum_Processes := Positive (Max_Proc);
884 elsif C = 'w' and then Switch_Chars'Length = 3 then
887 if Switch_Chars = "-we" then
888 Warning_Mode := Treat_As_Error;
890 elsif Switch_Chars = "-wn" then
891 Warning_Mode := Normal;
893 elsif Switch_Chars = "-ws" then
894 Warning_Mode := Suppress;
904 -- Single-character switches
910 Check_Readonly_Files := True;
912 -- Processing for b switch
918 -- Processing for B switch
921 Build_Bind_And_Link_Full_Project := True;
923 -- Processing for c switch
926 Compile_Only := True;
929 -- Processing for C switch
932 Opt.Create_Mapping_File := True;
934 -- Processing for D switch
937 if Object_Directory_Present then
938 Osint.Fail ("duplicate -D switch");
941 Object_Directory_Present := True;
944 -- Processing for f switch
947 Force_Compilations := True;
949 -- Processing for F switch
952 Full_Path_Name_For_Brief_Errors := True;
954 -- Processing for h switch
957 Usage_Requested := True;
959 -- Processing for i switch
962 In_Place_Mode := True;
964 -- Processing for j switch
967 -- -j not followed by a number is an error
969 Bad_Switch (Switch_Chars);
971 -- Processing for k switch
976 -- Processing for l switch
982 -- Processing for M switch
985 List_Dependencies := True;
987 -- Processing for n switch
990 Do_Not_Execute := True;
992 -- Processing for o switch
995 if Output_File_Name_Present then
996 Osint.Fail ("duplicate -o switch");
998 Output_File_Name_Present := True;
1001 -- Processing for p switch
1004 Setup_Projects := True;
1006 -- Processing for q switch
1009 Quiet_Output := True;
1011 -- Processing for R switch
1014 Run_Path_Option := False;
1016 -- Processing for s switch
1020 Check_Switches := True;
1022 -- Processing for v switch
1025 Verbose_Mode := True;
1026 Verbosity_Level := Opt.High;
1028 -- Processing for x switch
1031 External_Unit_Compilation_Allowed := True;
1032 Use_Include_Path_File := True;
1034 -- Processing for z switch
1037 No_Main_Subprogram := True;
1039 -- Any other small letter is an illegal switch
1042 if C in 'a' .. 'z' then
1043 Bad_Switch (Switch_Chars);
1051 end Scan_Make_Switches;