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
.Ext
; use Prj
.Ext
;
34 package body Switch
.M
is
36 package Normalized_Switches
is new Table
.Table
37 (Table_Component_Type
=> String_Access
,
38 Table_Index_Type
=> Integer,
41 Table_Increment
=> 100,
42 Table_Name
=> "Switch.M.Normalized_Switches");
43 -- This table is used to keep the normalized switches, so that they may be
44 -- reused for subsequent invocations of Normalize_Compiler_Switches with
47 Initial_Number_Of_Switches
: constant := 10;
49 Global_Switches
: Argument_List_Access
:= null;
50 -- Used by function Normalize_Compiler_Switches
52 ---------------------------------
53 -- Normalize_Compiler_Switches --
54 ---------------------------------
56 procedure Normalize_Compiler_Switches
57 (Switch_Chars
: String;
58 Switches
: in out Argument_List_Access
;
61 Switch_Starts_With_Gnat
: Boolean;
63 Ptr
: Integer := Switch_Chars
'First;
64 Max
: constant Integer := Switch_Chars
'Last;
67 Storing
: String := Switch_Chars
;
68 First_Stored
: Positive := Ptr
+ 1;
69 Last_Stored
: Positive := First_Stored
;
71 procedure Add_Switch_Component
(S
: String);
72 -- Add a new String_Access component in Switches. If a string equal
73 -- to S is already stored in the table Normalized_Switches, use it.
74 -- Otherwise add a new component to the table.
76 --------------------------
77 -- Add_Switch_Component --
78 --------------------------
80 procedure Add_Switch_Component
(S
: String) is
82 -- If Switches is null, allocate a new array
84 if Switches
= null then
85 Switches
:= new Argument_List
(1 .. Initial_Number_Of_Switches
);
87 -- Otherwise, if Switches is full, extend it
89 elsif Last
= Switches
'Last then
91 New_Switches
: constant Argument_List_Access
:=
93 (1 .. Switches
'Length + Switches
'Length);
95 New_Switches
(1 .. Switches
'Length) := Switches
.all;
96 Last
:= Switches
'Length;
97 Switches
:= New_Switches
;
101 -- If this is the first switch, Last designates the first component
104 Last
:= Switches
'First;
109 -- Look into the table Normalized_Switches for a similar string.
110 -- If one is found, put it at the added component, and return.
112 for Index
in 1 .. Normalized_Switches
.Last
loop
113 if S
= Normalized_Switches
.Table
(Index
).all then
114 Switches
(Last
) := Normalized_Switches
.Table
(Index
);
119 -- No string equal to S was found in the table Normalized_Switches.
120 -- Add a new component in the table.
122 Switches
(Last
) := new String'(S);
123 Normalized_Switches.Append (Switches (Last));
124 end Add_Switch_Component;
126 -- Start of processing for Normalize_Compiler_Switches
131 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
137 Switch_Starts_With_Gnat :=
138 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
140 if Switch_Starts_With_Gnat then
145 while Ptr <= Max loop
146 C := Switch_Chars (Ptr);
148 -- Processing for a switch
150 case Switch_Starts_With_Gnat is
154 -- All switches that don't start with -gnat stay as is,
155 -- except -pg, -Wall, -k8, -w
157 if Switch_Chars = "-pg" or else Switch_Chars = "-p" then
159 -- The gcc driver converts -pg to -p, so that is what
160 -- is stored in the ALI file.
162 Add_Switch_Component ("-p");
164 elsif Switch_Chars = "-Wall" then
166 -- The gcc driver adds -gnatwa when -Wall is used
168 Add_Switch_Component ("-gnatwa");
169 Add_Switch_Component ("-Wall");
171 elsif Switch_Chars = "-k8" then
173 -- The gcc driver transforms -k8 into -gnatk8
175 Add_Switch_Component ("-gnatk8");
177 elsif Switch_Chars = "-w" then
179 -- The gcc driver adds -gnatws when -w is used
181 Add_Switch_Component ("-gnatws");
182 Add_Switch_Component ("-w");
184 elsif Switch_Chars'Length > 6
186 Switch_Chars (Switch_Chars'First .. Switch_Chars'First + 5)
189 Add_Switch_Component (Switch_Chars);
191 -- When --RTS=mtp is used, the gcc driver adds -mrtp
193 if Switch_Chars = "--RTS=mtp" then
194 Add_Switch_Component ("-mrtp");
197 -- Take only into account switches that are transmitted to
198 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
202 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
203 Add_Switch_Component (Switch_Chars);
216 -- One-letter switches
218 when 'a
' | 'A
' | 'b
' | 'B
' | 'c
' | 'C
' | 'E
' | 'f
' |
219 'F
' | 'g
' | 'h
' | 'H
' | 'I
' | 'L
' | 'n
' | 'N
' |
220 'o
' | 'p
' | 'P
' | 'q
' | 'Q
' | 'r
' | 's
' | 'S
' |
221 't
' | 'u
' | 'U
' | 'v
' | 'x
' | 'X
' | 'Z
' =>
222 Storing (First_Stored) := C;
224 (Storing (Storing'First .. First_Stored));
227 -- One-letter switches followed by a positive number
229 when 'D
' | 'G
' | 'j
' | 'k
' | 'm
' | 'T
' =>
230 Storing (First_Stored) := C;
231 Last_Stored := First_Stored;
233 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
240 or else Switch_Chars (Ptr) not in '0' .. '9';
241 Last_Stored := Last_Stored + 1;
242 Storing (Last_Stored) := Switch_Chars (Ptr);
246 (Storing (Storing'First .. Last_Stored));
249 Storing (First_Stored) := 'd
';
253 C := Switch_Chars (Ptr);
254 exit when C = ASCII.NUL or else C = '/'
257 if C in '1' .. '9' or else
258 C in 'a
' .. 'z
' or else
261 Storing (First_Stored + 1) := C;
263 (Storing (Storing'First .. First_Stored + 1));
275 -- Some of the gnate... switches are not stored
277 Storing (First_Stored) := 'e
';
285 case Switch_Chars (Ptr) is
288 Storing (First_Stored + 1 ..
289 First_Stored + Max - Ptr + 1) :=
290 Switch_Chars (Ptr .. Max);
292 (Storing (Storing'First ..
293 First_Stored + Max - Ptr + 1));
298 Add_Switch_Component ("-gnateG");
304 First : constant Positive := Ptr - 1;
306 if Ptr <= Max and then
307 Switch_Chars (Ptr) = '='
312 while Ptr <= Max and then
313 Switch_Chars (Ptr) in '0' .. '9'
318 Storing (First_Stored + 1 ..
319 First_Stored + Ptr - First) :=
320 Switch_Chars (First .. Ptr - 1);
322 (Storing (Storing'First ..
323 First_Stored + Ptr - First));
334 if Switch_Chars (Ptr) = '=' then
338 -- To normalize, always put a '=' after
339 -- -gnatep. Because that could lengthen the
340 -- switch string, declare a local variable.
343 To_Store : String (1 .. Max - Ptr + 9);
345 To_Store (1 .. 8) := "-gnatep=";
346 To_Store (9 .. Max - Ptr + 9) :=
347 Switch_Chars (Ptr .. Max);
348 Add_Switch_Component (To_Store);
355 Add_Switch_Component ("-gnateS");
364 Storing (First_Stored) := 'i
';
373 C := Switch_Chars (Ptr);
382 Storing (First_Stored + 1) := C;
384 (Storing (Storing'First .. First_Stored + 1));
392 -- -gnatl may be -gnatl=<file name>
397 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
398 Add_Switch_Component ("-gnatl");
402 ("-gnatl" & Switch_Chars (Ptr .. Max));
406 -- -gnatR may be followed by '0', '1', '2' or '3',
410 Last_Stored := First_Stored;
411 Storing (Last_Stored) := 'R
';
415 and then Switch_Chars (Ptr) in '0' .. '9'
417 C := Switch_Chars (Ptr);
419 if C in '4' .. '9' then
424 Last_Stored := Last_Stored + 1;
425 Storing (Last_Stored) := C;
429 and then Switch_Chars (Ptr) = 's
'
431 Last_Stored := Last_Stored + 1;
432 Storing (Last_Stored) := 's
';
439 (Storing (Storing'First .. Last_Stored));
441 -- -gnatWx, x = 'h
'. 'u
', 's
', 'e
', '8' or 'b
'
444 Storing (First_Stored) := 'W
';
448 case Switch_Chars (Ptr) is
449 when 'h
' | 'u
' | 's
' | 'e
' | '8' | 'b
' =>
450 Storing (First_Stored + 1) := Switch_Chars (Ptr);
452 (Storing (Storing'First .. First_Stored + 1));
463 when 'V
' | 'w
' | 'y
' =>
464 Storing (First_Stored) := C;
470 (Storing (Storing'First .. First_Stored));
478 -- Loop through remaining switch characters in string
480 while Ptr <= Max loop
481 C := Switch_Chars (Ptr);
486 if C = 'M
' and then Storing (First_Stored) = 'y
' then
487 Last_Stored := First_Stored + 1;
488 Storing (Last_Stored) := 'M
';
489 while Ptr <= Max loop
490 C := Switch_Chars (Ptr);
491 exit when C not in '0' .. '9';
492 Last_Stored := Last_Stored + 1;
493 Storing (Last_Stored) := C;
497 -- If there is no digit after -gnatyM,
498 -- the switch is invalid.
500 if Last_Stored = First_Stored + 1 then
506 (Storing (Storing'First .. Last_Stored));
511 elsif C = '.' and then Ptr <= Max then
512 Storing (First_Stored + 1) := '.';
513 Storing (First_Stored + 2) := Switch_Chars (Ptr);
516 (Storing (Storing'First .. First_Stored + 2));
518 -- All other switches are -gnatxx
521 Storing (First_Stored + 1) := C;
523 (Storing (Storing'First .. First_Stored + 1));
530 Last_Stored := First_Stored;
531 Storing (Last_Stored) := C;
534 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
542 Last_Stored := Last_Stored + 1;
543 Storing (Last_Stored) := '5';
545 (Storing (Storing'First .. Last_Stored));
552 Last_Stored := First_Stored;
553 Storing (Last_Stored) := '8';
556 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
564 Last_Stored := Last_Stored + 1;
565 Storing (Last_Stored) := '3';
567 (Storing (Storing'First .. Last_Stored));
571 -- Not a valid switch
581 end Normalize_Compiler_Switches;
583 function Normalize_Compiler_Switches
584 (Switch_Chars : String) return Argument_List
589 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
592 return (1 .. 0 => null);
594 return Global_Switches (Global_Switches'First .. Last);
596 end Normalize_Compiler_Switches;
598 ------------------------
599 -- Scan_Make_Switches --
600 ------------------------
602 procedure Scan_Make_Switches
603 (Project_Node_Tree : Prj.Tree.Project_Node_Tree_Ref;
604 Switch_Chars : String;
605 Success : out Boolean)
607 Ptr : Integer := Switch_Chars'First;
608 Max : constant Integer := Switch_Chars'Last;
609 C : Character := ' ';
612 -- Assume a good switch
616 -- Skip past the initial character (must be the switch character)
619 Bad_Switch (Switch_Chars);
625 -- A little check, "gnat" at the start of a switch is for the compiler
627 if Switch_Chars'Length >= Ptr + 3
628 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
634 C := Switch_Chars (Ptr);
636 -- Multiple character switches
638 if Switch_Chars'Length > 2 then
639 if Switch_Chars = "--create-missing-dirs" then
640 Setup_Projects := True;
642 elsif Switch_Chars'Length > Subdirs_Option'Length
645 (Switch_Chars'First ..
646 Switch_Chars'First + Subdirs_Option'Length - 1) =
652 (Switch_Chars
'First + Subdirs_Option
'Length ..
655 elsif Switch_Chars
= Makeutl
.Unchecked_Shared_Lib_Imports
then
656 Opt
.Unchecked_Shared_Lib_Imports
:= True;
658 elsif Switch_Chars
= Makeutl
.Single_Compile_Per_Obj_Dir_Switch
then
659 Opt
.One_Compilation_Per_Obj_Dir
:= True;
661 elsif Switch_Chars
(Ptr
) = '-' then
662 Bad_Switch
(Switch_Chars
);
664 elsif Switch_Chars
'Length > 3
665 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
667 Add_Search_Project_Directory
669 Switch_Chars
(Ptr
+ 2 .. Switch_Chars
'Last));
671 elsif C
= 'v' and then Switch_Chars
'Length = 3 then
673 Verbose_Mode
:= True;
675 case Switch_Chars
(Ptr
) is
677 Verbosity_Level
:= Opt
.Low
;
680 Verbosity_Level
:= Opt
.Medium
;
683 Verbosity_Level
:= Opt
.High
;
691 -- Note: for the debug switch, the remaining characters in this
692 -- switch field must all be debug flags, since all valid switch
693 -- characters are also valid debug characters. This switch is not
694 -- documented on purpose because it is only used by the
697 -- Loop to scan out debug flags
701 C
:= Switch_Chars
(Ptr
);
703 if C
in 'a' .. 'z' or else C
in 'A' .. 'Z' then
706 Bad_Switch
(Switch_Chars
);
713 case Switch_Chars
(Ptr
) is
715 -- Processing for eI switch
719 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Main_Index
, C
);
722 Bad_Switch
(Switch_Chars
);
725 -- Processing for eL switch
729 Bad_Switch
(Switch_Chars
);
732 Follow_Links_For_Files
:= True;
733 Follow_Links_For_Dirs
:= True;
736 -- Processing for eS switch
740 Bad_Switch
(Switch_Chars
);
743 Commands_To_Stdout
:= True;
747 Bad_Switch
(Switch_Chars
);
756 Scan_Pos
(Switch_Chars
, Max
, Ptr
, Max_Proc
, C
);
759 Bad_Switch
(Switch_Chars
);
762 Maximum_Processes
:= Positive (Max_Proc
);
766 elsif C
= 'w' and then Switch_Chars
'Length = 3 then
769 if Switch_Chars
= "-we" then
770 Warning_Mode
:= Treat_As_Error
;
772 elsif Switch_Chars
= "-wn" then
773 Warning_Mode
:= Normal
;
775 elsif Switch_Chars
= "-ws" then
776 Warning_Mode
:= Suppress
;
786 -- Single-character switches
794 Check_Readonly_Files
:= True;
796 -- Processing for b switch
802 -- Processing for B switch
805 Build_Bind_And_Link_Full_Project
:= True;
807 -- Processing for c switch
810 Compile_Only
:= True;
813 -- Processing for C switch
816 Create_Mapping_File
:= True;
818 -- Processing for D switch
821 if Object_Directory_Present
then
822 Osint
.Fail
("duplicate -D switch");
825 Object_Directory_Present
:= True;
828 -- Processing for f switch
831 Force_Compilations
:= True;
833 -- Processing for F switch
836 Full_Path_Name_For_Brief_Errors
:= True;
838 -- Processing for h switch
841 Usage_Requested
:= True;
843 -- Processing for i switch
846 In_Place_Mode
:= True;
848 -- Processing for j switch
851 -- -j not followed by a number is an error
853 Bad_Switch
(Switch_Chars
);
855 -- Processing for k switch
860 -- Processing for l switch
866 -- Processing for M switch
869 List_Dependencies
:= True;
871 -- Processing for n switch
874 Do_Not_Execute
:= True;
876 -- Processing for o switch
879 if Output_File_Name_Present
then
880 Osint
.Fail
("duplicate -o switch");
882 Output_File_Name_Present
:= True;
885 -- Processing for p switch
888 Setup_Projects
:= True;
890 -- Processing for q switch
893 Quiet_Output
:= True;
895 -- Processing for R switch
898 Run_Path_Option
:= False;
900 -- Processing for s switch
904 Check_Switches
:= True;
906 -- Processing for v switch
909 Verbose_Mode
:= True;
910 Verbosity_Level
:= Opt
.High
;
912 -- Processing for x switch
915 External_Unit_Compilation_Allowed
:= True;
916 Use_Include_Path_File
:= True;
918 -- Processing for z switch
921 No_Main_Subprogram
:= True;
923 -- Any other small letter is an illegal switch
926 if C
in 'a' .. 'z' then
927 Bad_Switch
(Switch_Chars
);
936 end Scan_Make_Switches
;