1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2018, 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 Osint
; use Osint
;
31 with System
.Multiprocessors
; use System
.Multiprocessors
;
33 package body Switch
.M
is
35 package Normalized_Switches
is new Table
.Table
36 (Table_Component_Type
=> String_Access
,
37 Table_Index_Type
=> Integer,
40 Table_Increment
=> 100,
41 Table_Name
=> "Switch.M.Normalized_Switches");
42 -- This table is used to keep the normalized switches, so that they may be
43 -- reused for subsequent invocations of Normalize_Compiler_Switches with
46 Initial_Number_Of_Switches
: constant := 10;
48 Global_Switches
: Argument_List_Access
:= null;
49 -- Used by function Normalize_Compiler_Switches
51 Subdirs_Option
: constant String := "--subdirs=";
53 ---------------------------------
54 -- Normalize_Compiler_Switches --
55 ---------------------------------
57 procedure Normalize_Compiler_Switches
58 (Switch_Chars
: String;
59 Switches
: in out Argument_List_Access
;
62 Switch_Starts_With_Gnat
: Boolean;
64 Ptr
: Integer := Switch_Chars
'First;
65 Max
: constant Integer := Switch_Chars
'Last;
68 Storing
: String := Switch_Chars
;
69 First_Stored
: Positive := Ptr
+ 1;
70 Last_Stored
: Positive := First_Stored
;
72 procedure Add_Switch_Component
(S
: String);
73 -- Add a new String_Access component in Switches. If a string equal
74 -- to S is already stored in the table Normalized_Switches, use it.
75 -- Otherwise add a new component to the table.
77 --------------------------
78 -- Add_Switch_Component --
79 --------------------------
81 procedure Add_Switch_Component
(S
: String) is
83 -- If Switches is null, allocate a new array
85 if Switches
= null then
86 Switches
:= new Argument_List
(1 .. Initial_Number_Of_Switches
);
88 -- Otherwise, if Switches is full, extend it
90 elsif Last
= Switches
'Last then
92 New_Switches
: constant Argument_List_Access
:=
94 (1 .. Switches
'Length + Switches
'Length);
96 New_Switches
(1 .. Switches
'Length) := Switches
.all;
97 Last
:= Switches
'Length;
98 Switches
:= New_Switches
;
102 -- If this is the first switch, Last designates the first component
105 Last
:= Switches
'First;
110 -- Look into the table Normalized_Switches for a similar string.
111 -- If one is found, put it at the added component, and return.
113 for Index
in 1 .. Normalized_Switches
.Last
loop
114 if S
= Normalized_Switches
.Table
(Index
).all then
115 Switches
(Last
) := Normalized_Switches
.Table
(Index
);
120 -- No string equal to S was found in the table Normalized_Switches.
121 -- Add a new component in the table.
123 Switches
(Last
) := new String'(S);
124 Normalized_Switches.Append (Switches (Last));
125 end Add_Switch_Component;
127 -- Start of processing for Normalize_Compiler_Switches
132 if Ptr = Max or else Switch_Chars (Ptr) /= '-' then
138 Switch_Starts_With_Gnat :=
139 Ptr + 3 <= Max and then Switch_Chars (Ptr .. Ptr + 3) = "gnat";
141 if Switch_Starts_With_Gnat then
146 while Ptr <= Max loop
147 C := Switch_Chars (Ptr);
149 -- Processing for a switch
151 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 -- Special case for -fstack-check (alias for
198 -- -fstack-check=specific)
200 elsif Switch_Chars = "-fstack-check" then
201 Add_Switch_Component ("-fstack-check=specific");
203 -- Take only into account switches that are transmitted to
204 -- gnat1 by the gcc driver and stored by gnat1 in the ALI file.
208 when 'O
' | 'W
' | 'w
' | 'f
' | 'd
' | 'g
' | 'm
' =>
209 Add_Switch_Component (Switch_Chars);
221 -- One-letter switches
223 when 'a
' | 'A
' | 'b
' | 'B
' | 'c
' | 'C
' | 'E
' | 'f
' | 'F
'
224 | 'g
' | 'h
' | 'H
' | 'I
' | 'L
' | 'N
' | 'p
' | 'P
' | 'q
'
225 | 'Q
' | 'r
' | 's
' | 'S
' | 't
' | 'u
' | 'U
' | 'v
' | 'x
'
228 Storing (First_Stored) := C;
230 (Storing (Storing'First .. First_Stored));
233 -- One-letter switches followed by a positive number
235 when 'D
' | 'G
' | 'j
' | 'k
' | 'm
' | 'T
' =>
236 Storing (First_Stored) := C;
237 Last_Stored := First_Stored;
239 if Ptr <= Max and then Switch_Chars (Ptr) = '=' then
246 or else Switch_Chars (Ptr) not in '0' .. '9';
247 Last_Stored := Last_Stored + 1;
248 Storing (Last_Stored) := Switch_Chars (Ptr);
252 (Storing (Storing'First .. Last_Stored));
255 Storing (First_Stored) := 'd
';
259 C := Switch_Chars (Ptr);
260 exit when C = ASCII.NUL or else C = '/'
263 if C in '1' .. '9' or else
264 C in 'a
' .. 'z
' or else
267 Storing (First_Stored + 1) := C;
269 (Storing (Storing'First .. First_Stored + 1));
281 -- Some of the gnate... switches are not stored
283 Storing (First_Stored) := 'e
';
291 case Switch_Chars (Ptr) is
294 Add_Switch_Component ("-gnateA");
297 Storing (First_Stored + 1 ..
298 First_Stored + Max - Ptr + 1) :=
299 Switch_Chars (Ptr .. Max);
301 (Storing (Storing'First ..
302 First_Stored + Max - Ptr + 1));
305 when 'E
' | 'F
' | 'G
' | 'S
' | 'u
' | 'V
' | 'Y
' =>
307 ("-gnate" & Switch_Chars (Ptr));
312 First : constant Positive := Ptr;
317 if Ptr <= Max and then
318 Switch_Chars (Ptr) = '='
323 while Ptr <= Max and then
324 Switch_Chars (Ptr) in '0' .. '9'
329 Storing (First_Stored + 1 ..
330 First_Stored + Ptr - First) :=
331 Switch_Chars (First .. Ptr - 1);
333 (Storing (Storing'First ..
334 First_Stored + Ptr - First));
339 Add_Switch_Component ("-gnatel");
343 Add_Switch_Component ("-gnateL");
353 if Switch_Chars (Ptr) = '=' then
357 -- To normalize, always put a '=' after
358 -- -gnatep. Because that could lengthen the
359 -- switch string, declare a local variable.
362 To_Store : String (1 .. Max - Ptr + 9);
364 To_Store (1 .. 8) := "-gnatep=";
365 To_Store (9 .. Max - Ptr + 9) :=
366 Switch_Chars (Ptr .. Max);
367 Add_Switch_Component (To_Store);
379 Storing (First_Stored) := 'i
';
388 C := Switch_Chars (Ptr);
397 Storing (First_Stored + 1) := C;
399 (Storing (Storing'First .. First_Stored + 1));
407 -- -gnatl may be -gnatl=<file name>
412 if Ptr > Max or else Switch_Chars (Ptr) /= '=' then
413 Add_Switch_Component ("-gnatl");
417 ("-gnatl" & Switch_Chars (Ptr .. Max));
421 -- -gnatn may be -gnatn, -gnatn1, or -gnatn2
424 Last_Stored := First_Stored;
425 Storing (Last_Stored) := 'n
';
429 and then Switch_Chars (Ptr) in '1' .. '2'
431 Last_Stored := Last_Stored + 1;
432 Storing (Last_Stored) := Switch_Chars (Ptr);
437 (Storing (Storing'First .. Last_Stored));
439 -- -gnato may be -gnatox or -gnatoxx, with x=0/1/2/3
442 Last_Stored := First_Stored;
443 Storing (Last_Stored) := 'o
';
447 and then Switch_Chars (Ptr) in '0' .. '3'
449 Last_Stored := Last_Stored + 1;
450 Storing (Last_Stored) := Switch_Chars (Ptr);
454 and then Switch_Chars (Ptr) in '0' .. '3'
456 Last_Stored := Last_Stored + 1;
457 Storing (Last_Stored) := Switch_Chars (Ptr);
463 (Storing (Storing'First .. Last_Stored));
465 -- -gnatR may be followed by '0', '1', '2' or '3',
469 Last_Stored := First_Stored;
470 Storing (Last_Stored) := 'R
';
474 and then Switch_Chars (Ptr) in '0' .. '9'
476 C := Switch_Chars (Ptr);
478 if C in '4' .. '9' then
483 Last_Stored := Last_Stored + 1;
484 Storing (Last_Stored) := C;
488 and then Switch_Chars (Ptr) = 's
'
490 Last_Stored := Last_Stored + 1;
491 Storing (Last_Stored) := 's
';
498 (Storing (Storing'First .. Last_Stored));
500 -- -gnatWx, x = 'h
'. 'u
', 's
', 'e
', '8' or 'b
'
503 Storing (First_Stored) := 'W
';
507 case Switch_Chars (Ptr) is
508 when 'h
' | 'u
' | 's
' | 'e
' | '8' | 'b
' =>
509 Storing (First_Stored + 1) := Switch_Chars (Ptr);
511 (Storing (Storing'First .. First_Stored + 1));
522 when 'V
' | 'w
' | 'y
' =>
523 Storing (First_Stored) := C;
529 (Storing (Storing'First .. First_Stored));
537 -- Loop through remaining switch characters in string
539 while Ptr <= Max loop
540 C := Switch_Chars (Ptr);
545 if C = 'M
' and then Storing (First_Stored) = 'y
' then
546 Last_Stored := First_Stored + 1;
547 Storing (Last_Stored) := 'M
';
548 while Ptr <= Max loop
549 C := Switch_Chars (Ptr);
550 exit when C not in '0' .. '9';
551 Last_Stored := Last_Stored + 1;
552 Storing (Last_Stored) := C;
556 -- If there is no digit after -gnatyM,
557 -- the switch is invalid.
559 if Last_Stored = First_Stored + 1 then
565 (Storing (Storing'First .. Last_Stored));
570 elsif C = '.' and then Ptr <= Max then
571 Storing (First_Stored + 1) := '.';
572 Storing (First_Stored + 2) := Switch_Chars (Ptr);
575 (Storing (Storing'First .. First_Stored + 2));
577 -- All other switches are -gnatxx
580 Storing (First_Stored + 1) := C;
582 (Storing (Storing'First .. First_Stored + 1));
589 Last_Stored := First_Stored;
590 Storing (Last_Stored) := C;
593 if Ptr /= Max or else Switch_Chars (Ptr) /= '5' then
601 Last_Stored := Last_Stored + 1;
602 Storing (Last_Stored) := '5';
604 (Storing (Storing'First .. Last_Stored));
611 Last_Stored := First_Stored;
612 Storing (Last_Stored) := C;
615 if Ptr /= Max or else Switch_Chars (Ptr) /= '2' then
623 Last_Stored := Last_Stored + 1;
624 Storing (Last_Stored) := '2';
626 (Storing (Storing'First .. Last_Stored));
630 -- -gnat2005 -gnat2012
633 if Ptr + 3 /= Max then
637 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "005" then
638 Last_Stored := First_Stored + 3;
639 Storing (First_Stored .. Last_Stored) := "2005";
641 (Storing (Storing'First .. Last_Stored));
644 elsif Switch_Chars (Ptr + 1 .. Ptr + 3) = "012" then
645 Last_Stored := First_Stored + 3;
646 Storing (First_Stored .. Last_Stored) := "2012";
648 (Storing (Storing'First .. Last_Stored));
663 Last_Stored := First_Stored;
664 Storing (Last_Stored) := '8';
667 if Ptr /= Max or else Switch_Chars (Ptr) /= '3' then
675 Last_Stored := Last_Stored + 1;
676 Storing (Last_Stored) := '3';
678 (Storing (Storing'First .. Last_Stored));
682 -- Not a valid switch
690 end Normalize_Compiler_Switches;
692 function Normalize_Compiler_Switches
693 (Switch_Chars : String) return Argument_List
698 Normalize_Compiler_Switches (Switch_Chars, Global_Switches, Last);
701 return (1 .. 0 => null);
703 return Global_Switches (Global_Switches'First .. Last);
705 end Normalize_Compiler_Switches;
707 ------------------------
708 -- Scan_Make_Switches --
709 ------------------------
711 procedure Scan_Make_Switches
712 (Switch_Chars : String;
713 Success : out Boolean)
715 Ptr : Integer := Switch_Chars'First;
716 Max : constant Integer := Switch_Chars'Last;
717 C : Character := ' ';
720 -- Assume a good switch
724 -- Skip past the initial character (must be the switch character)
727 Bad_Switch (Switch_Chars);
733 -- A little check, "gnat" at the start of a switch is for the compiler
735 if Switch_Chars'Length >= Ptr + 3
736 and then Switch_Chars (Ptr .. Ptr + 3) = "gnat"
742 C := Switch_Chars (Ptr);
744 -- Multiple character switches
746 -- To preserve building gnat_util, it is not possible to use the
747 -- constant Strings declare in Make_Util, as Make_Util is not in
750 if Switch_Chars'Length > 2 then
751 if Switch_Chars = "--create-missing-dirs" then
752 Setup_Projects := True;
754 elsif Switch_Chars'Length > Subdirs_Option'Length
757 (Switch_Chars'First ..
758 Switch_Chars'First + Subdirs_Option'Length - 1) =
762 new String'(Switch_Chars
763 (Switch_Chars
'First + Subdirs_Option
'Length ..
766 elsif Switch_Chars
= "--unchecked-shared-lib-imports" then
767 Opt
.Unchecked_Shared_Lib_Imports
:= True;
769 elsif Switch_Chars
= "--single-compile-per-obj-dir" then
770 Opt
.One_Compilation_Per_Obj_Dir
:= True;
772 elsif Switch_Chars
= "--no-exit-message" then
773 Opt
.No_Exit_Message
:= True;
775 elsif Switch_Chars
= "--keep-temp-files" then
776 Opt
.Keep_Temporary_Files
:= True;
778 elsif Switch_Chars
(Ptr
) = '-' then
779 Bad_Switch
(Switch_Chars
);
781 elsif Switch_Chars
'Length > 3
782 and then Switch_Chars
(Ptr
.. Ptr
+ 1) = "aP"
785 -- This is only used by gprbuild
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
;