1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2014, 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 -- Special case for -fstack-check (alias for
218 -- -fstack-check=specific)
220 elsif Switch_Chars = "-fstack-check" then
221 Add_Switch_Component ("-fstack-check=specific");
223 -- Take only into account switches that are transmitted to
224 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
228 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
229 Add_Switch_Component (Switch_Chars);
242 -- One-letter switches
244 when 'a
' | 'A
' | 'b
' | 'B
' | 'c
' | 'C
' | 'E
' | 'f
' |
245 'F
' | 'g
' | 'h
' | 'H
' | 'I
' | 'L
' | 'N
' | 'p
' |
246 'P
' | 'q
' | 'Q
' | 'r
' | 's
' | 'S
' | 't
' | 'u
' |
247 'U
' | 'v
' | 'x
' | 'X
' | 'Z
' =>
248 Storing (First_Stored) := C;
250 (Storing (Storing'First .. First_Stored));
253 -- One-letter switches followed by a positive number
255 when 'D
' | 'G
' | 'j
' | 'k
' | 'm
' | 'T
' =>
256 Storing (First_Stored) := C;
257 Last_Stored := First_Stored;
259 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
266 or else Switch_Chars (Ptr) not in '0' .. '9';
267 Last_Stored := Last_Stored + 1;
268 Storing (Last_Stored) := Switch_Chars (Ptr);
272 (Storing (Storing'First .. Last_Stored));
275 Storing (First_Stored) := 'd
';
279 C := Switch_Chars (Ptr);
280 exit when C = ASCII.NUL or else C = '/'
283 if C in '1' .. '9' or else
284 C in 'a
' .. 'z
' or else
287 Storing (First_Stored + 1) := C;
289 (Storing (Storing'First .. First_Stored + 1));
301 -- Some of the gnate... switches are not stored
303 Storing (First_Stored) := 'e
';
311 case Switch_Chars (Ptr) is
315 Add_Switch_Component ("-gnateA");
318 Storing (First_Stored + 1 ..
319 First_Stored + Max - Ptr + 1) :=
320 Switch_Chars (Ptr .. Max);
322 (Storing (Storing'First ..
323 First_Stored + Max - Ptr + 1));
326 when 'E
' | 'F
' | 'G
' | 'S
' | 'u
' | 'V
' | 'Y
' =>
328 ("-gnate" & Switch_Chars (Ptr));
333 First : constant Positive := Ptr;
338 if Ptr <= Max and then
339 Switch_Chars (Ptr) = '='
344 while Ptr <= Max and then
345 Switch_Chars (Ptr) in '0' .. '9'
350 Storing (First_Stored + 1 ..
351 First_Stored + Ptr - First) :=
352 Switch_Chars (First .. Ptr - 1);
354 (Storing (Storing'First ..
355 First_Stored + Ptr - First));
360 Add_Switch_Component ("-gnatel");
364 Add_Switch_Component ("-gnateL");
374 if Switch_Chars (Ptr) = '=' then
378 -- To normalize, always put a '=' after
379 -- -gnatep. Because that could lengthen the
380 -- switch string, declare a local variable.
383 To_Store : String (1 .. Max - Ptr + 9);
385 To_Store (1 .. 8) := "-gnatep=";
386 To_Store (9 .. Max - Ptr + 9) :=
387 Switch_Chars (Ptr .. Max);
388 Add_Switch_Component (To_Store);
400 Storing (First_Stored) := 'i
';
409 C := Switch_Chars (Ptr);
418 Storing (First_Stored + 1) := C;
420 (Storing (Storing'First .. First_Stored + 1));
428 -- -gnatl may be -gnatl=<file name>
433 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
434 Add_Switch_Component ("-gnatl");
438 ("-gnatl" & Switch_Chars (Ptr .. Max));
442 -- -gnatn may be -gnatn, -gnatn1, or -gnatn2
445 Last_Stored := First_Stored;
446 Storing (Last_Stored) := 'n
';
450 and then Switch_Chars (Ptr) in '1' .. '2'
452 Last_Stored := Last_Stored + 1;
453 Storing (Last_Stored) := Switch_Chars (Ptr);
458 (Storing (Storing'First .. Last_Stored));
460 -- -gnato may be -gnatox or -gnatoxx, with x=0/1/2/3
463 Last_Stored := First_Stored;
464 Storing (Last_Stored) := 'o
';
468 and then Switch_Chars (Ptr) in '0' .. '3'
470 Last_Stored := Last_Stored + 1;
471 Storing (Last_Stored) := Switch_Chars (Ptr);
475 and then Switch_Chars (Ptr) in '0' .. '3'
477 Last_Stored := Last_Stored + 1;
478 Storing (Last_Stored) := Switch_Chars (Ptr);
484 (Storing (Storing'First .. Last_Stored));
486 -- -gnatR may be followed by '0', '1', '2' or '3',
490 Last_Stored := First_Stored;
491 Storing (Last_Stored) := 'R
';
495 and then Switch_Chars (Ptr) in '0' .. '9'
497 C := Switch_Chars (Ptr);
499 if C in '4' .. '9' then
504 Last_Stored := Last_Stored + 1;
505 Storing (Last_Stored) := C;
509 and then Switch_Chars (Ptr) = 's
'
511 Last_Stored := Last_Stored + 1;
512 Storing (Last_Stored) := 's
';
519 (Storing (Storing'First .. Last_Stored));
521 -- -gnatWx, x = 'h
'. 'u
', 's
', 'e
', '8' or 'b
'
524 Storing (First_Stored) := 'W
';
528 case Switch_Chars (Ptr) is
529 when 'h
' | 'u
' | 's
' | 'e
' | '8' | 'b
' =>
530 Storing (First_Stored + 1) := Switch_Chars (Ptr);
532 (Storing (Storing'First .. First_Stored + 1));
543 when 'V
' | 'w
' | 'y
' =>
544 Storing (First_Stored) := C;
550 (Storing (Storing'First .. First_Stored));
558 -- Loop through remaining switch characters in string
560 while Ptr <= Max loop
561 C := Switch_Chars (Ptr);
566 if C = 'M
' and then Storing (First_Stored) = 'y
' then
567 Last_Stored := First_Stored + 1;
568 Storing (Last_Stored) := 'M
';
569 while Ptr <= Max loop
570 C := Switch_Chars (Ptr);
571 exit when C not in '0' .. '9';
572 Last_Stored := Last_Stored + 1;
573 Storing (Last_Stored) := C;
577 -- If there is no digit after -gnatyM,
578 -- the switch is invalid.
580 if Last_Stored = First_Stored + 1 then
586 (Storing (Storing'First .. Last_Stored));
591 elsif C = '.' and then Ptr <= Max then
592 Storing (First_Stored + 1) := '.';
593 Storing (First_Stored + 2) := Switch_Chars (Ptr);
596 (Storing (Storing'First .. First_Stored + 2));
598 -- All other switches are -gnatxx
601 Storing (First_Stored + 1) := C;
603 (Storing (Storing'First .. First_Stored + 1));
610 Last_Stored := First_Stored;
611 Storing (Last_Stored) := C;
614 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
622 Last_Stored := Last_Stored + 1;
623 Storing (Last_Stored) := '5';
625 (Storing (Storing'First .. Last_Stored));
632 Last_Stored := First_Stored;
633 Storing (Last_Stored) := C;
636 if Ptr /= Max or else Switch_Chars (Ptr) /= '2' then
644 Last_Stored := Last_Stored + 1;
645 Storing (Last_Stored) := '2';
647 (Storing (Storing'First .. Last_Stored));
651 -- -gnat2005 -gnat2012
654 if Ptr + 3 /= Max then
658 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "005" then
659 Last_Stored := First_Stored + 3;
660 Storing (First_Stored .. Last_Stored) := "2005";
662 (Storing (Storing'First .. Last_Stored));
665 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "012" then
666 Last_Stored := First_Stored + 3;
667 Storing (First_Stored .. Last_Stored) := "2012";
669 (Storing (Storing'First .. Last_Stored));
684 Last_Stored := First_Stored;
685 Storing (Last_Stored) := '8';
688 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
696 Last_Stored := Last_Stored + 1;
697 Storing (Last_Stored) := '3';
699 (Storing (Storing'First .. Last_Stored));
703 -- Not a valid switch
713 end Normalize_Compiler_Switches;
715 function Normalize_Compiler_Switches
716 (Switch_Chars : String) return Argument_List
721 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
724 return (1 .. 0 => null);
726 return Global_Switches (Global_Switches'First .. Last);
728 end Normalize_Compiler_Switches;
730 ------------------------
731 -- Scan_Make_Switches --
732 ------------------------
734 procedure Scan_Make_Switches
735 (Env : in out Prj.Tree.Environment;
736 Switch_Chars : String;
737 Success : out Boolean)
739 Ptr : Integer := Switch_Chars'First;
740 Max : constant Integer := Switch_Chars'Last;
741 C : Character := ' ';
744 -- Assume a good switch
748 -- Skip past the initial character (must be the switch character)
751 Bad_Switch (Switch_Chars);
757 -- A little check, "gnat" at the start of a switch is for the compiler
759 if Switch_Chars'Length >= Ptr + 3
760 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
766 C := Switch_Chars (Ptr);
768 -- Multiple character switches
770 if Switch_Chars'Length > 2 then
771 if Switch_Chars = "--create-missing-dirs" then
772 Setup_Projects := True;
774 elsif Switch_Chars'Length > Subdirs_Option'Length
777 (Switch_Chars'First ..
778 Switch_Chars'First + Subdirs_Option'Length - 1) =
784 (Switch_Chars
'First + Subdirs_Option
'Length ..
787 elsif Switch_Chars
= Makeutl
.Unchecked_Shared_Lib_Imports
then
788 Opt
.Unchecked_Shared_Lib_Imports
:= True;
790 elsif Switch_Chars
= Makeutl
.Single_Compile_Per_Obj_Dir_Switch
then
791 Opt
.One_Compilation_Per_Obj_Dir
:= True;
793 elsif Switch_Chars
= Makeutl
.No_Exit_Message_Option
then
794 Opt
.No_Exit_Message
:= True;
796 elsif Switch_Chars
= Makeutl
.Keep_Temp_Files_Option
then
797 Opt
.Keep_Temporary_Files
:= True;
799 elsif Switch_Chars
(Ptr
) = '-' then
800 Bad_Switch
(Switch_Chars
);
802 elsif Switch_Chars
'Length > 3
803 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
807 Switch_Chars
(Ptr
+ 2 .. Switch_Chars
'Last));
809 elsif C
= 'v' and then Switch_Chars
'Length = 3 then
811 Verbose_Mode
:= True;
813 case Switch_Chars
(Ptr
) is
815 Verbosity_Level
:= Opt
.Low
;
818 Verbosity_Level
:= Opt
.Medium
;
821 Verbosity_Level
:= Opt
.High
;
829 -- Note: for the debug switch, the remaining characters in this
830 -- switch field must all be debug flags, since all valid switch
831 -- characters are also valid debug characters. This switch is not
832 -- documented on purpose because it is only used by the
835 -- Loop to scan out debug flags
839 C
:= Switch_Chars
(Ptr
);
841 if C
in 'a' .. 'z' or else C
in 'A' .. 'Z' then
844 Bad_Switch
(Switch_Chars
);
851 case Switch_Chars
(Ptr
) is
853 -- Processing for eI switch
857 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Main_Index
, C
);
860 Bad_Switch
(Switch_Chars
);
863 -- Processing for eL switch
867 Bad_Switch
(Switch_Chars
);
870 Follow_Links_For_Files
:= True;
871 Follow_Links_For_Dirs
:= True;
874 -- Processing for eS switch
878 Bad_Switch
(Switch_Chars
);
881 Commands_To_Stdout
:= True;
885 Bad_Switch
(Switch_Chars
);
895 Scan_Nat
(Switch_Chars
, Max
, Ptr
, Max_Proc
, C
);
898 Bad_Switch
(Switch_Chars
);
902 Max_Proc
:= Nat
(Number_Of_CPUs
);
909 Maximum_Processes
:= Positive (Max_Proc
);
913 elsif C
= 'w' and then Switch_Chars
'Length = 3 then
916 if Switch_Chars
= "-we" then
917 Warning_Mode
:= Treat_As_Error
;
919 elsif Switch_Chars
= "-wn" then
920 Warning_Mode
:= Normal
;
922 elsif Switch_Chars
= "-ws" then
923 Warning_Mode
:= Suppress
;
933 -- Single-character switches
941 Check_Readonly_Files
:= True;
943 -- Processing for b switch
949 -- Processing for B switch
952 Build_Bind_And_Link_Full_Project
:= True;
954 -- Processing for c switch
957 Compile_Only
:= True;
960 -- Processing for C switch
963 Opt
.Create_Mapping_File
:= True;
965 -- Processing for D switch
968 if Object_Directory_Present
then
969 Osint
.Fail
("duplicate -D switch");
972 Object_Directory_Present
:= True;
975 -- Processing for f switch
978 Force_Compilations
:= True;
980 -- Processing for F switch
983 Full_Path_Name_For_Brief_Errors
:= True;
985 -- Processing for h switch
988 Usage_Requested
:= True;
990 -- Processing for i switch
993 In_Place_Mode
:= True;
995 -- Processing for j switch
998 -- -j not followed by a number is an error
1000 Bad_Switch
(Switch_Chars
);
1002 -- Processing for k switch
1007 -- Processing for l switch
1013 -- Processing for M switch
1016 List_Dependencies
:= True;
1018 -- Processing for n switch
1021 Do_Not_Execute
:= True;
1023 -- Processing for o switch
1026 if Output_File_Name_Present
then
1027 Osint
.Fail
("duplicate -o switch");
1029 Output_File_Name_Present
:= True;
1032 -- Processing for p switch
1035 Setup_Projects
:= True;
1037 -- Processing for q switch
1040 Quiet_Output
:= True;
1042 -- Processing for R switch
1045 Run_Path_Option
:= False;
1047 -- Processing for s switch
1051 Check_Switches
:= True;
1053 -- Processing for v switch
1056 Verbose_Mode
:= True;
1057 Verbosity_Level
:= Opt
.High
;
1059 -- Processing for x switch
1062 External_Unit_Compilation_Allowed
:= True;
1063 Use_Include_Path_File
:= True;
1065 -- Processing for z switch
1068 No_Main_Subprogram
:= True;
1070 -- Any other small letter is an illegal switch
1073 if C
in 'a' .. 'z' then
1074 Bad_Switch
(Switch_Chars
);
1083 end Scan_Make_Switches
;