FSF GCC merge 02/23/03
[official-gcc.git] / gcc / ada / mlib-utl.adb
blob7b5fa0283fe5f1073cb717930b993c8bbea58a11
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M L I B . U T L --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 2002, Ada Core Technologies, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 with MLib.Fil;
29 with MLib.Tgt;
30 with Namet; use Namet;
31 with Opt;
32 with Osint; use Osint;
33 with Output; use Output;
35 package body MLib.Utl is
37 use GNAT;
39 package Files renames MLib.Fil;
40 package Target renames MLib.Tgt;
42 Initialized : Boolean := False;
44 Gcc_Name : constant String := "gcc";
45 Gcc_Exec : OS_Lib.String_Access;
47 Ar_Name : constant String := "ar";
48 Ar_Exec : OS_Lib.String_Access;
50 Ranlib_Name : constant String := "ranlib";
51 Ranlib_Exec : OS_Lib.String_Access;
53 procedure Initialize;
54 -- Look for the tools in the path and record the full path for each one
56 --------
57 -- Ar --
58 --------
60 procedure Ar (Output_File : String; Objects : Argument_List) is
61 Create_Add_Opt : OS_Lib.String_Access := new String' ("cr");
63 Full_Output_File : constant String :=
64 Files.Ext_To (Output_File, Target.Archive_Ext);
66 Arguments : OS_Lib.Argument_List (1 .. 2 + Objects'Length);
67 Success : Boolean;
69 begin
70 Initialize;
72 Arguments (1) := Create_Add_Opt; -- "ar cr ..."
73 Arguments (2) := new String'(Full_Output_File);
74 Arguments (3 .. Arguments'Last) := Objects;
76 Delete_File (Full_Output_File);
78 if not Opt.Quiet_Output then
79 Write_Str (Ar_Name);
81 for J in Arguments'Range loop
82 Write_Char (' ');
83 Write_Str (Arguments (J).all);
84 end loop;
86 Write_Eol;
87 end if;
89 OS_Lib.Spawn (Ar_Exec.all, Arguments, Success);
91 if not Success then
92 Fail (Ar_Name, " execution error.");
93 end if;
95 -- If we have found ranlib, run it over the library
97 if Ranlib_Exec /= null then
98 if not Opt.Quiet_Output then
99 Write_Str (Ranlib_Name);
100 Write_Char (' ');
101 Write_Line (Arguments (2).all);
102 end if;
104 OS_Lib.Spawn (Ranlib_Exec.all, (1 => Arguments (2)), Success);
106 if not Success then
107 Fail (Ranlib_Name, " execution error.");
108 end if;
109 end if;
110 end Ar;
112 -----------------
113 -- Delete_File --
114 -----------------
116 procedure Delete_File (Filename : in String) is
117 File : constant String := Filename & ASCII.Nul;
118 Success : Boolean;
120 begin
121 OS_Lib.Delete_File (File'Address, Success);
123 if Opt.Verbose_Mode then
124 if Success then
125 Write_Str ("deleted ");
127 else
128 Write_Str ("could not delete ");
129 end if;
131 Write_Line (Filename);
132 end if;
133 end Delete_File;
135 ---------
136 -- Gcc --
137 ---------
139 procedure Gcc
140 (Output_File : String;
141 Objects : Argument_List;
142 Options : Argument_List)
144 Arguments : OS_Lib.Argument_List
145 (1 .. 7 + Objects'Length + Options'Length);
147 A : Natural := 0;
148 Success : Boolean;
149 Out_Opt : OS_Lib.String_Access := new String' ("-o");
150 Out_V : OS_Lib.String_Access := new String' (Output_File);
151 Lib_Dir : OS_Lib.String_Access := new String' ("-L" & Lib_Directory);
152 Lib_Opt : OS_Lib.String_Access := new String' (Target.Dynamic_Option);
154 begin
155 Initialize;
157 if Lib_Opt'Length /= 0 then
158 A := A + 1;
159 Arguments (A) := Lib_Opt;
160 end if;
162 A := A + 1;
163 Arguments (A) := Out_Opt;
165 A := A + 1;
166 Arguments (A) := Out_V;
168 A := A + 1;
169 Arguments (A) := Lib_Dir;
171 A := A + Options'Length;
172 Arguments (A - Options'Length + 1 .. A) := Options;
174 A := A + Objects'Length;
175 Arguments (A - Objects'Length + 1 .. A) := Objects;
177 if not Opt.Quiet_Output then
178 Write_Str (Gcc_Exec.all);
180 for J in 1 .. A loop
181 Write_Char (' ');
182 Write_Str (Arguments (J).all);
183 end loop;
185 Write_Eol;
186 end if;
188 OS_Lib.Spawn (Gcc_Exec.all, Arguments (1 .. A), Success);
190 if not Success then
191 Fail (Gcc_Name, " execution error");
192 end if;
193 end Gcc;
195 ----------------
196 -- Initialize --
197 ----------------
199 procedure Initialize is
200 use type OS_Lib.String_Access;
202 begin
203 if not Initialized then
204 Initialized := True;
206 -- gcc
208 Gcc_Exec := OS_Lib.Locate_Exec_On_Path (Gcc_Name);
210 if Gcc_Exec = null then
212 Fail (Gcc_Name, " not found in path");
214 elsif Opt.Verbose_Mode then
215 Write_Str ("found ");
216 Write_Line (Gcc_Exec.all);
217 end if;
219 -- ar
221 Ar_Exec := OS_Lib.Locate_Exec_On_Path (Ar_Name);
223 if Ar_Exec = null then
225 Fail (Ar_Name, " not found in path");
227 elsif Opt.Verbose_Mode then
228 Write_Str ("found ");
229 Write_Line (Ar_Exec.all);
230 end if;
232 -- ranlib
234 Ranlib_Exec := OS_Lib.Locate_Exec_On_Path (Ranlib_Name);
236 if Ranlib_Exec /= null and then Opt.Verbose_Mode then
237 Write_Str ("found ");
238 Write_Line (Ranlib_Exec.all);
239 end if;
241 end if;
243 end Initialize;
245 -------------------
246 -- Lib_Directory --
247 -------------------
249 function Lib_Directory return String is
250 Libgnat : constant String := Target.Libgnat;
252 begin
253 Name_Len := Libgnat'Length;
254 Name_Buffer (1 .. Name_Len) := Libgnat;
255 Get_Name_String (Find_File (Name_Enter, Library));
257 -- Remove libgnat.a
259 return Name_Buffer (1 .. Name_Len - Libgnat'Length);
260 end Lib_Directory;
262 end MLib.Utl;