1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2010, 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 -- Take only into account switches that are transmitted to
200 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
204 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
205 Add_Switch_Component (Switch_Chars);
218 -- One-letter switches
220 when 'a
' | 'A
' | 'b
' | 'B
' | 'c
' | 'C
' | 'E
' | 'f
' |
221 'F
' | 'g
' | 'h
' | 'H
' | 'I
' | 'L
' | 'n
' | 'N
' |
222 'o
' | 'p
' | 'P
' | 'q
' | 'Q
' | 'r
' | 's
' | 'S
' |
223 't
' | 'u
' | 'U
' | 'v
' | 'x
' | 'X
' | 'Z
' =>
224 Storing (First_Stored) := C;
226 (Storing (Storing'First .. First_Stored));
229 -- One-letter switches followed by a positive number
231 when 'D
' | 'G
' | 'j
' | 'k
' | 'm
' | 'T
' =>
232 Storing (First_Stored) := C;
233 Last_Stored := First_Stored;
235 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
242 or else Switch_Chars (Ptr) not in '0' .. '9';
243 Last_Stored := Last_Stored + 1;
244 Storing (Last_Stored) := Switch_Chars (Ptr);
248 (Storing (Storing'First .. Last_Stored));
251 Storing (First_Stored) := 'd
';
255 C := Switch_Chars (Ptr);
256 exit when C = ASCII.NUL or else C = '/'
259 if C in '1' .. '9' or else
260 C in 'a
' .. 'z
' or else
263 Storing (First_Stored + 1) := C;
265 (Storing (Storing'First .. First_Stored + 1));
277 -- Some of the gnate... switches are not stored
279 Storing (First_Stored) := 'e
';
287 case Switch_Chars (Ptr) is
290 Storing (First_Stored + 1 ..
291 First_Stored + Max - Ptr + 1) :=
292 Switch_Chars (Ptr .. Max);
294 (Storing (Storing'First ..
295 First_Stored + Max - Ptr + 1));
300 Add_Switch_Component ("-gnateG");
306 First : constant Positive := Ptr - 1;
308 if Ptr <= Max and then
309 Switch_Chars (Ptr) = '='
314 while Ptr <= Max and then
315 Switch_Chars (Ptr) in '0' .. '9'
320 Storing (First_Stored + 1 ..
321 First_Stored + Ptr - First) :=
322 Switch_Chars (First .. Ptr - 1);
324 (Storing (Storing'First ..
325 First_Stored + Ptr - First));
336 if Switch_Chars (Ptr) = '=' then
340 -- To normalize, always put a '=' after
341 -- -gnatep. Because that could lengthen the
342 -- switch string, declare a local variable.
345 To_Store : String (1 .. Max - Ptr + 9);
347 To_Store (1 .. 8) := "-gnatep=";
348 To_Store (9 .. Max - Ptr + 9) :=
349 Switch_Chars (Ptr .. Max);
350 Add_Switch_Component (To_Store);
357 Add_Switch_Component ("-gnateS");
366 Storing (First_Stored) := 'i
';
375 C := Switch_Chars (Ptr);
384 Storing (First_Stored + 1) := C;
386 (Storing (Storing'First .. First_Stored + 1));
394 -- -gnatl may be -gnatl=<file name>
399 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
400 Add_Switch_Component ("-gnatl");
404 ("-gnatl" & Switch_Chars (Ptr .. Max));
408 -- -gnatR may be followed by '0', '1', '2' or '3',
412 Last_Stored := First_Stored;
413 Storing (Last_Stored) := 'R
';
417 and then Switch_Chars (Ptr) in '0' .. '9'
419 C := Switch_Chars (Ptr);
421 if C in '4' .. '9' then
426 Last_Stored := Last_Stored + 1;
427 Storing (Last_Stored) := C;
431 and then Switch_Chars (Ptr) = 's
'
433 Last_Stored := Last_Stored + 1;
434 Storing (Last_Stored) := 's
';
441 (Storing (Storing'First .. Last_Stored));
443 -- -gnatWx, x = 'h
'. 'u
', 's
', 'e
', '8' or 'b
'
446 Storing (First_Stored) := 'W
';
450 case Switch_Chars (Ptr) is
451 when 'h
' | 'u
' | 's
' | 'e
' | '8' | 'b
' =>
452 Storing (First_Stored + 1) := Switch_Chars (Ptr);
454 (Storing (Storing'First .. First_Stored + 1));
465 when 'V
' | 'w
' | 'y
' =>
466 Storing (First_Stored) := C;
472 (Storing (Storing'First .. First_Stored));
480 -- Loop through remaining switch characters in string
482 while Ptr <= Max loop
483 C := Switch_Chars (Ptr);
488 if C = 'M
' and then Storing (First_Stored) = 'y
' then
489 Last_Stored := First_Stored + 1;
490 Storing (Last_Stored) := 'M
';
491 while Ptr <= Max loop
492 C := Switch_Chars (Ptr);
493 exit when C not in '0' .. '9';
494 Last_Stored := Last_Stored + 1;
495 Storing (Last_Stored) := C;
499 -- If there is no digit after -gnatyM,
500 -- the switch is invalid.
502 if Last_Stored = First_Stored + 1 then
508 (Storing (Storing'First .. Last_Stored));
513 elsif C = '.' and then Ptr <= Max then
514 Storing (First_Stored + 1) := '.';
515 Storing (First_Stored + 2) := Switch_Chars (Ptr);
518 (Storing (Storing'First .. First_Stored + 2));
520 -- All other switches are -gnatxx
523 Storing (First_Stored + 1) := C;
525 (Storing (Storing'First .. First_Stored + 1));
532 Last_Stored := First_Stored;
533 Storing (Last_Stored) := C;
536 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
544 Last_Stored := Last_Stored + 1;
545 Storing (Last_Stored) := '5';
547 (Storing (Storing'First .. Last_Stored));
554 Last_Stored := First_Stored;
555 Storing (Last_Stored) := '8';
558 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
566 Last_Stored := Last_Stored + 1;
567 Storing (Last_Stored) := '3';
569 (Storing (Storing'First .. Last_Stored));
573 -- Not a valid switch
583 end Normalize_Compiler_Switches;
585 function Normalize_Compiler_Switches
586 (Switch_Chars : String) return Argument_List
591 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
594 return (1 .. 0 => null);
596 return Global_Switches (Global_Switches'First .. Last);
598 end Normalize_Compiler_Switches;
600 ------------------------
601 -- Scan_Make_Switches --
602 ------------------------
604 procedure Scan_Make_Switches
605 (Project_Node_Tree : Prj.Tree.Project_Node_Tree_Ref;
606 Switch_Chars : String;
607 Success : out Boolean)
609 Ptr : Integer := Switch_Chars'First;
610 Max : constant Integer := Switch_Chars'Last;
611 C : Character := ' ';
614 -- Assume a good switch
618 -- Skip past the initial character (must be the switch character)
621 Bad_Switch (Switch_Chars);
627 -- A little check, "gnat" at the start of a switch is for the compiler
629 if Switch_Chars'Length >= Ptr + 3
630 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
636 C := Switch_Chars (Ptr);
638 -- Multiple character switches
640 if Switch_Chars'Length > 2 then
641 if Switch_Chars = "--create-missing-dirs" then
642 Setup_Projects := True;
644 elsif Switch_Chars'Length > Subdirs_Option'Length
647 (Switch_Chars'First ..
648 Switch_Chars'First + Subdirs_Option'Length - 1) =
654 (Switch_Chars
'First + Subdirs_Option
'Length ..
657 elsif Switch_Chars
= Makeutl
.Unchecked_Shared_Lib_Imports
then
658 Opt
.Unchecked_Shared_Lib_Imports
:= True;
660 elsif Switch_Chars
= Makeutl
.Single_Compile_Per_Obj_Dir_Switch
then
661 Opt
.One_Compilation_Per_Obj_Dir
:= True;
663 elsif Switch_Chars
(Ptr
) = '-' then
664 Bad_Switch
(Switch_Chars
);
666 elsif Switch_Chars
'Length > 3
667 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
670 (Project_Node_Tree
.Project_Path
,
671 Switch_Chars
(Ptr
+ 2 .. Switch_Chars
'Last));
673 elsif C
= 'v' and then Switch_Chars
'Length = 3 then
675 Verbose_Mode
:= True;
677 case Switch_Chars
(Ptr
) is
679 Verbosity_Level
:= Opt
.Low
;
682 Verbosity_Level
:= Opt
.Medium
;
685 Verbosity_Level
:= Opt
.High
;
693 -- Note: for the debug switch, the remaining characters in this
694 -- switch field must all be debug flags, since all valid switch
695 -- characters are also valid debug characters. This switch is not
696 -- documented on purpose because it is only used by the
699 -- Loop to scan out debug flags
703 C
:= Switch_Chars
(Ptr
);
705 if C
in 'a' .. 'z' or else C
in 'A' .. 'Z' then
708 Bad_Switch
(Switch_Chars
);
715 case Switch_Chars
(Ptr
) is
717 -- Processing for eI switch
721 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Main_Index
, C
);
724 Bad_Switch
(Switch_Chars
);
727 -- Processing for eL switch
731 Bad_Switch
(Switch_Chars
);
734 Follow_Links_For_Files
:= True;
735 Follow_Links_For_Dirs
:= True;
738 -- Processing for eS switch
742 Bad_Switch
(Switch_Chars
);
745 Commands_To_Stdout
:= True;
749 Bad_Switch
(Switch_Chars
);
759 Scan_Nat
(Switch_Chars
, Max
, Ptr
, Max_Proc
, C
);
762 Bad_Switch
(Switch_Chars
);
766 Max_Proc
:= Nat
(Number_Of_CPUs
);
773 Maximum_Processes
:= Positive (Max_Proc
);
777 elsif C
= 'w' and then Switch_Chars
'Length = 3 then
780 if Switch_Chars
= "-we" then
781 Warning_Mode
:= Treat_As_Error
;
783 elsif Switch_Chars
= "-wn" then
784 Warning_Mode
:= Normal
;
786 elsif Switch_Chars
= "-ws" then
787 Warning_Mode
:= Suppress
;
797 -- Single-character switches
805 Check_Readonly_Files
:= True;
807 -- Processing for b switch
813 -- Processing for B switch
816 Build_Bind_And_Link_Full_Project
:= True;
818 -- Processing for c switch
821 Compile_Only
:= True;
824 -- Processing for C switch
827 Opt
.Create_Mapping_File
:= True;
829 -- Processing for D switch
832 if Object_Directory_Present
then
833 Osint
.Fail
("duplicate -D switch");
836 Object_Directory_Present
:= True;
839 -- Processing for f switch
842 Force_Compilations
:= True;
844 -- Processing for F switch
847 Full_Path_Name_For_Brief_Errors
:= True;
849 -- Processing for h switch
852 Usage_Requested
:= True;
854 -- Processing for i switch
857 In_Place_Mode
:= True;
859 -- Processing for j switch
862 -- -j not followed by a number is an error
864 Bad_Switch
(Switch_Chars
);
866 -- Processing for k switch
871 -- Processing for l switch
877 -- Processing for M switch
880 List_Dependencies
:= True;
882 -- Processing for n switch
885 Do_Not_Execute
:= True;
887 -- Processing for o switch
890 if Output_File_Name_Present
then
891 Osint
.Fail
("duplicate -o switch");
893 Output_File_Name_Present
:= True;
896 -- Processing for p switch
899 Setup_Projects
:= True;
901 -- Processing for q switch
904 Quiet_Output
:= True;
906 -- Processing for R switch
909 Run_Path_Option
:= False;
911 -- Processing for s switch
915 Check_Switches
:= True;
917 -- Processing for v switch
920 Verbose_Mode
:= True;
921 Verbosity_Level
:= Opt
.High
;
923 -- Processing for x switch
926 External_Unit_Compilation_Allowed
:= True;
927 Use_Include_Path_File
:= True;
929 -- Processing for z switch
932 No_Main_Subprogram
:= True;
934 -- Any other small letter is an illegal switch
937 if C
in 'a' .. 'z' then
938 Bad_Switch
(Switch_Chars
);
947 end Scan_Make_Switches
;