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
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 -- Special case for -fstack-check (alias for
200 -- -fstack-check=specific)
202 elsif Switch_Chars = "-fstack-check" then
203 Add_Switch_Component ("-fstack-check=specific");
205 -- Take only into account switches that are transmitted to
206 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
210 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
211 Add_Switch_Component (Switch_Chars);
224 -- One-letter switches
226 when 'a
' | 'A
' | 'b
' | 'B
' | 'c
' | 'C
' | 'E
' | 'f
' |
227 'F
' | 'g
' | 'h
' | 'H
' | 'I
' | 'L
' | 'N
' | 'p
' |
228 'P
' | 'q
' | 'Q
' | 'r
' | 's
' | 'S
' | 't
' | 'u
' |
229 'U
' | 'v
' | 'x
' | 'X
' | 'Z
' =>
230 Storing (First_Stored) := C;
232 (Storing (Storing'First .. First_Stored));
235 -- One-letter switches followed by a positive number
237 when 'D
' | 'G
' | 'j
' | 'k
' | 'm
' | 'T
' =>
238 Storing (First_Stored) := C;
239 Last_Stored := First_Stored;
241 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
248 or else Switch_Chars (Ptr) not in '0' .. '9';
249 Last_Stored := Last_Stored + 1;
250 Storing (Last_Stored) := Switch_Chars (Ptr);
254 (Storing (Storing'First .. Last_Stored));
257 Storing (First_Stored) := 'd
';
261 C := Switch_Chars (Ptr);
262 exit when C = ASCII.NUL or else C = '/'
265 if C in '1' .. '9' or else
266 C in 'a
' .. 'z
' or else
269 Storing (First_Stored + 1) := C;
271 (Storing (Storing'First .. First_Stored + 1));
283 -- Some of the gnate... switches are not stored
285 Storing (First_Stored) := 'e
';
293 case Switch_Chars (Ptr) is
297 Add_Switch_Component ("-gnateA");
300 Storing (First_Stored + 1 ..
301 First_Stored + Max - Ptr + 1) :=
302 Switch_Chars (Ptr .. Max);
304 (Storing (Storing'First ..
305 First_Stored + Max - Ptr + 1));
308 when 'E
' | 'F
' | 'G
' | 'S
' | 'u
' | 'V
' | 'Y
' =>
310 ("-gnate" & Switch_Chars (Ptr));
315 First : constant Positive := Ptr;
320 if Ptr <= Max and then
321 Switch_Chars (Ptr) = '='
326 while Ptr <= Max and then
327 Switch_Chars (Ptr) in '0' .. '9'
332 Storing (First_Stored + 1 ..
333 First_Stored + Ptr - First) :=
334 Switch_Chars (First .. Ptr - 1);
336 (Storing (Storing'First ..
337 First_Stored + Ptr - First));
342 Add_Switch_Component ("-gnatel");
346 Add_Switch_Component ("-gnateL");
356 if Switch_Chars (Ptr) = '=' then
360 -- To normalize, always put a '=' after
361 -- -gnatep. Because that could lengthen the
362 -- switch string, declare a local variable.
365 To_Store : String (1 .. Max - Ptr + 9);
367 To_Store (1 .. 8) := "-gnatep=";
368 To_Store (9 .. Max - Ptr + 9) :=
369 Switch_Chars (Ptr .. Max);
370 Add_Switch_Component (To_Store);
382 Storing (First_Stored) := 'i
';
391 C := Switch_Chars (Ptr);
400 Storing (First_Stored + 1) := C;
402 (Storing (Storing'First .. First_Stored + 1));
410 -- -gnatl may be -gnatl=<file name>
415 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
416 Add_Switch_Component ("-gnatl");
420 ("-gnatl" & Switch_Chars (Ptr .. Max));
424 -- -gnatn may be -gnatn, -gnatn1, or -gnatn2
427 Last_Stored := First_Stored;
428 Storing (Last_Stored) := 'n
';
432 and then Switch_Chars (Ptr) in '1' .. '2'
434 Last_Stored := Last_Stored + 1;
435 Storing (Last_Stored) := Switch_Chars (Ptr);
440 (Storing (Storing'First .. Last_Stored));
442 -- -gnato may be -gnatox or -gnatoxx, with x=0/1/2/3
445 Last_Stored := First_Stored;
446 Storing (Last_Stored) := 'o
';
450 and then Switch_Chars (Ptr) in '0' .. '3'
452 Last_Stored := Last_Stored + 1;
453 Storing (Last_Stored) := Switch_Chars (Ptr);
457 and then Switch_Chars (Ptr) in '0' .. '3'
459 Last_Stored := Last_Stored + 1;
460 Storing (Last_Stored) := Switch_Chars (Ptr);
466 (Storing (Storing'First .. Last_Stored));
468 -- -gnatR may be followed by '0', '1', '2' or '3',
472 Last_Stored := First_Stored;
473 Storing (Last_Stored) := 'R
';
477 and then Switch_Chars (Ptr) in '0' .. '9'
479 C := Switch_Chars (Ptr);
481 if C in '4' .. '9' then
486 Last_Stored := Last_Stored + 1;
487 Storing (Last_Stored) := C;
491 and then Switch_Chars (Ptr) = 's
'
493 Last_Stored := Last_Stored + 1;
494 Storing (Last_Stored) := 's
';
501 (Storing (Storing'First .. Last_Stored));
503 -- -gnatWx, x = 'h
'. 'u
', 's
', 'e
', '8' or 'b
'
506 Storing (First_Stored) := 'W
';
510 case Switch_Chars (Ptr) is
511 when 'h
' | 'u
' | 's
' | 'e
' | '8' | 'b
' =>
512 Storing (First_Stored + 1) := Switch_Chars (Ptr);
514 (Storing (Storing'First .. First_Stored + 1));
525 when 'V
' | 'w
' | 'y
' =>
526 Storing (First_Stored) := C;
532 (Storing (Storing'First .. First_Stored));
540 -- Loop through remaining switch characters in string
542 while Ptr <= Max loop
543 C := Switch_Chars (Ptr);
548 if C = 'M
' and then Storing (First_Stored) = 'y
' then
549 Last_Stored := First_Stored + 1;
550 Storing (Last_Stored) := 'M
';
551 while Ptr <= Max loop
552 C := Switch_Chars (Ptr);
553 exit when C not in '0' .. '9';
554 Last_Stored := Last_Stored + 1;
555 Storing (Last_Stored) := C;
559 -- If there is no digit after -gnatyM,
560 -- the switch is invalid.
562 if Last_Stored = First_Stored + 1 then
568 (Storing (Storing'First .. Last_Stored));
573 elsif C = '.' and then Ptr <= Max then
574 Storing (First_Stored + 1) := '.';
575 Storing (First_Stored + 2) := Switch_Chars (Ptr);
578 (Storing (Storing'First .. First_Stored + 2));
580 -- All other switches are -gnatxx
583 Storing (First_Stored + 1) := C;
585 (Storing (Storing'First .. First_Stored + 1));
592 Last_Stored := First_Stored;
593 Storing (Last_Stored) := C;
596 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
604 Last_Stored := Last_Stored + 1;
605 Storing (Last_Stored) := '5';
607 (Storing (Storing'First .. Last_Stored));
614 Last_Stored := First_Stored;
615 Storing (Last_Stored) := C;
618 if Ptr /= Max or else Switch_Chars (Ptr) /= '2' then
626 Last_Stored := Last_Stored + 1;
627 Storing (Last_Stored) := '2';
629 (Storing (Storing'First .. Last_Stored));
633 -- -gnat2005 -gnat2012
636 if Ptr + 3 /= Max then
640 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "005" then
641 Last_Stored := First_Stored + 3;
642 Storing (First_Stored .. Last_Stored) := "2005";
644 (Storing (Storing'First .. Last_Stored));
647 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "012" then
648 Last_Stored := First_Stored + 3;
649 Storing (First_Stored .. Last_Stored) := "2012";
651 (Storing (Storing'First .. Last_Stored));
666 Last_Stored := First_Stored;
667 Storing (Last_Stored) := '8';
670 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
678 Last_Stored := Last_Stored + 1;
679 Storing (Last_Stored) := '3';
681 (Storing (Storing'First .. Last_Stored));
685 -- Not a valid switch
695 end Normalize_Compiler_Switches;
697 function Normalize_Compiler_Switches
698 (Switch_Chars : String) return Argument_List
703 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
706 return (1 .. 0 => null);
708 return Global_Switches (Global_Switches'First .. Last);
710 end Normalize_Compiler_Switches;
712 ------------------------
713 -- Scan_Make_Switches --
714 ------------------------
716 procedure Scan_Make_Switches
717 (Env : in out Prj.Tree.Environment;
718 Switch_Chars : String;
719 Success : out Boolean)
721 Ptr : Integer := Switch_Chars'First;
722 Max : constant Integer := Switch_Chars'Last;
723 C : Character := ' ';
726 -- Assume a good switch
730 -- Skip past the initial character (must be the switch character)
733 Bad_Switch (Switch_Chars);
739 -- A little check, "gnat" at the start of a switch is for the compiler
741 if Switch_Chars'Length >= Ptr + 3
742 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
748 C := Switch_Chars (Ptr);
750 -- Multiple character switches
752 if Switch_Chars'Length > 2 then
753 if Switch_Chars = "--create-missing-dirs" then
754 Setup_Projects := True;
756 elsif Switch_Chars'Length > Subdirs_Option'Length
759 (Switch_Chars'First ..
760 Switch_Chars'First + Subdirs_Option'Length - 1) =
766 (Switch_Chars
'First + Subdirs_Option
'Length ..
769 elsif Switch_Chars
= Makeutl
.Unchecked_Shared_Lib_Imports
then
770 Opt
.Unchecked_Shared_Lib_Imports
:= True;
772 elsif Switch_Chars
= Makeutl
.Single_Compile_Per_Obj_Dir_Switch
then
773 Opt
.One_Compilation_Per_Obj_Dir
:= True;
775 elsif Switch_Chars
= Makeutl
.No_Exit_Message_Option
then
776 Opt
.No_Exit_Message
:= True;
778 elsif Switch_Chars
= Makeutl
.Keep_Temp_Files_Option
then
779 Opt
.Keep_Temporary_Files
:= True;
781 elsif Switch_Chars
(Ptr
) = '-' then
782 Bad_Switch
(Switch_Chars
);
784 elsif Switch_Chars
'Length > 3
785 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
789 Switch_Chars
(Ptr
+ 2 .. Switch_Chars
'Last));
791 elsif C
= 'v' and then Switch_Chars
'Length = 3 then
793 Verbose_Mode
:= True;
795 case Switch_Chars
(Ptr
) is
797 Verbosity_Level
:= Opt
.Low
;
800 Verbosity_Level
:= Opt
.Medium
;
803 Verbosity_Level
:= Opt
.High
;
811 -- Note: for the debug switch, the remaining characters in this
812 -- switch field must all be debug flags, since all valid switch
813 -- characters are also valid debug characters. This switch is not
814 -- documented on purpose because it is only used by the
817 -- Loop to scan out debug flags
821 C
:= Switch_Chars
(Ptr
);
823 if C
in 'a' .. 'z' or else C
in 'A' .. 'Z' then
826 Bad_Switch
(Switch_Chars
);
833 case Switch_Chars
(Ptr
) is
835 -- Processing for eI switch
839 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Main_Index
, C
);
842 Bad_Switch
(Switch_Chars
);
845 -- Processing for eL switch
849 Bad_Switch
(Switch_Chars
);
852 Follow_Links_For_Files
:= True;
853 Follow_Links_For_Dirs
:= True;
856 -- Processing for eS switch
860 Bad_Switch
(Switch_Chars
);
863 Commands_To_Stdout
:= True;
867 Bad_Switch
(Switch_Chars
);
877 Scan_Nat
(Switch_Chars
, Max
, Ptr
, Max_Proc
, C
);
880 Bad_Switch
(Switch_Chars
);
884 Max_Proc
:= Nat
(Number_Of_CPUs
);
891 Maximum_Processes
:= Positive (Max_Proc
);
895 elsif C
= 'w' and then Switch_Chars
'Length = 3 then
898 if Switch_Chars
= "-we" then
899 Warning_Mode
:= Treat_As_Error
;
901 elsif Switch_Chars
= "-wn" then
902 Warning_Mode
:= Normal
;
904 elsif Switch_Chars
= "-ws" then
905 Warning_Mode
:= Suppress
;
915 -- Single-character switches
923 Check_Readonly_Files
:= True;
925 -- Processing for b switch
931 -- Processing for B switch
934 Build_Bind_And_Link_Full_Project
:= True;
936 -- Processing for c switch
939 Compile_Only
:= True;
942 -- Processing for C switch
945 Opt
.Create_Mapping_File
:= True;
947 -- Processing for D switch
950 if Object_Directory_Present
then
951 Osint
.Fail
("duplicate -D switch");
954 Object_Directory_Present
:= True;
957 -- Processing for f switch
960 Force_Compilations
:= True;
962 -- Processing for F switch
965 Full_Path_Name_For_Brief_Errors
:= True;
967 -- Processing for h switch
970 Usage_Requested
:= True;
972 -- Processing for i switch
975 In_Place_Mode
:= True;
977 -- Processing for j switch
980 -- -j not followed by a number is an error
982 Bad_Switch
(Switch_Chars
);
984 -- Processing for k switch
989 -- Processing for l switch
995 -- Processing for M switch
998 List_Dependencies
:= True;
1000 -- Processing for n switch
1003 Do_Not_Execute
:= True;
1005 -- Processing for o switch
1008 if Output_File_Name_Present
then
1009 Osint
.Fail
("duplicate -o switch");
1011 Output_File_Name_Present
:= True;
1014 -- Processing for p switch
1017 Setup_Projects
:= True;
1019 -- Processing for q switch
1022 Quiet_Output
:= True;
1024 -- Processing for R switch
1027 Run_Path_Option
:= False;
1029 -- Processing for s switch
1033 Check_Switches
:= True;
1035 -- Processing for v switch
1038 Verbose_Mode
:= True;
1039 Verbosity_Level
:= Opt
.High
;
1041 -- Processing for x switch
1044 External_Unit_Compilation_Allowed
:= True;
1045 Use_Include_Path_File
:= True;
1047 -- Processing for z switch
1050 No_Main_Subprogram
:= True;
1052 -- Any other small letter is an illegal switch
1055 if C
in 'a' .. 'z' then
1056 Bad_Switch
(Switch_Chars
);
1065 end Scan_Make_Switches
;