Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / ada / mlib-prj.adb
blob03ca2d0ee96be79ffd903f8af08783fb43cf0cc6
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M L I B . P R J --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2005, Ada Core Technologies, Inc. --
10 -- --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with ALI; use ALI;
28 with Gnatvsn; use Gnatvsn;
29 with Hostparm;
30 with MLib.Fil; use MLib.Fil;
31 with MLib.Tgt; use MLib.Tgt;
32 with MLib.Utl; use MLib.Utl;
33 with Namet; use Namet;
34 with Opt;
35 with Osint; use Osint;
36 with Output; use Output;
37 with Prj.Com; use Prj.Com;
38 with Prj.Env; use Prj.Env;
39 with Prj.Util; use Prj.Util;
40 with Sinput.P;
41 with Snames; use Snames;
42 with Switch; use Switch;
43 with Table;
44 with Types; use Types;
46 with Ada.Characters.Handling;
48 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
49 with GNAT.HTable;
50 with GNAT.OS_Lib; use GNAT.OS_Lib;
51 with Interfaces.C_Streams; use Interfaces.C_Streams;
52 with System; use System;
53 with System.Case_Util; use System.Case_Util;
55 package body MLib.Prj is
57 Prj_Add_Obj_Files : Types.Int;
58 pragma Import (C, Prj_Add_Obj_Files, "__gnat_prj_add_obj_files");
59 Add_Object_Files : constant Boolean := Prj_Add_Obj_Files /= 0;
60 -- Indicates if object files in pragmas Linker_Options (found in the
61 -- binder generated file) should be taken when linking aq stand-alone
62 -- library.
63 -- False for Windows, True for other platforms.
65 ALI_Suffix : constant String := ".ali";
66 B_Start : String := "b~";
68 S_Osinte_Ads : Name_Id := No_Name;
69 -- Name_Id for "s-osinte.ads"
71 S_Dec_Ads : Name_Id := No_Name;
72 -- Name_Id for "dec.ads"
74 G_Trasym_Ads : Name_Id := No_Name;
75 -- Name_Id for "g-trasym.ads"
77 No_Argument_List : aliased String_List := (1 .. 0 => null);
78 No_Argument : constant String_List_Access := No_Argument_List'Access;
80 Arguments : String_List_Access := No_Argument;
81 -- Used to accumulate arguments for the invocation of gnatbind and of
82 -- the compiler. Also used to collect the interface ALI when copying
83 -- the ALI files to the library directory.
85 Argument_Number : Natural := 0;
86 -- Index of the last argument in Arguments
88 Initial_Argument_Max : constant := 10;
90 No_Main_String : aliased String := "-n";
91 No_Main : constant String_Access := No_Main_String'Access;
93 Output_Switch_String : aliased String := "-o";
94 Output_Switch : constant String_Access := Output_Switch_String'Access;
96 Compile_Switch_String : aliased String := "-c";
97 Compile_Switch : constant String_Access := Compile_Switch_String'Access;
99 -- List of objects to put inside the library
101 Object_Files : Argument_List_Access;
103 package Objects is new Table.Table
104 (Table_Name => "Mlib.Prj.Objects",
105 Table_Component_Type => String_Access,
106 Table_Index_Type => Natural,
107 Table_Low_Bound => 1,
108 Table_Initial => 50,
109 Table_Increment => 100);
111 package Objects_Htable is new GNAT.HTable.Simple_HTable
112 (Header_Num => Header_Num,
113 Element => Boolean,
114 No_Element => False,
115 Key => Name_Id,
116 Hash => Hash,
117 Equal => "=");
119 -- List of non-Ada object files
121 Foreign_Objects : Argument_List_Access;
123 package Foreigns is new Table.Table
124 (Table_Name => "Mlib.Prj.Foreigns",
125 Table_Component_Type => String_Access,
126 Table_Index_Type => Natural,
127 Table_Low_Bound => 1,
128 Table_Initial => 20,
129 Table_Increment => 100);
131 -- List of ALI files
133 Ali_Files : Argument_List_Access;
135 package ALIs is new Table.Table
136 (Table_Name => "Mlib.Prj.Alis",
137 Table_Component_Type => String_Access,
138 Table_Index_Type => Natural,
139 Table_Low_Bound => 1,
140 Table_Initial => 50,
141 Table_Increment => 100);
143 -- List of options set in the command line.
145 Options : Argument_List_Access;
147 package Opts is new Table.Table
148 (Table_Name => "Mlib.Prj.Opts",
149 Table_Component_Type => String_Access,
150 Table_Index_Type => Natural,
151 Table_Low_Bound => 1,
152 Table_Initial => 5,
153 Table_Increment => 100);
155 -- All the ALI file in the library
157 package Library_ALIs is new GNAT.HTable.Simple_HTable
158 (Header_Num => Header_Num,
159 Element => Boolean,
160 No_Element => False,
161 Key => Name_Id,
162 Hash => Hash,
163 Equal => "=");
165 -- The ALI files in the interface sets
167 package Interface_ALIs is new GNAT.HTable.Simple_HTable
168 (Header_Num => Header_Num,
169 Element => Boolean,
170 No_Element => False,
171 Key => Name_Id,
172 Hash => Hash,
173 Equal => "=");
175 -- The ALI files that have been processed to check if the corresponding
176 -- library unit is in the interface set.
178 package Processed_ALIs is new GNAT.HTable.Simple_HTable
179 (Header_Num => Header_Num,
180 Element => Boolean,
181 No_Element => False,
182 Key => Name_Id,
183 Hash => Hash,
184 Equal => "=");
186 -- The projects imported directly or indirectly.
188 package Processed_Projects is new GNAT.HTable.Simple_HTable
189 (Header_Num => Header_Num,
190 Element => Boolean,
191 No_Element => False,
192 Key => Name_Id,
193 Hash => Hash,
194 Equal => "=");
196 -- The library projects imported directly or indirectly.
198 package Library_Projs is new Table.Table (
199 Table_Component_Type => Project_Id,
200 Table_Index_Type => Integer,
201 Table_Low_Bound => 1,
202 Table_Initial => 10,
203 Table_Increment => 10,
204 Table_Name => "Make.Library_Projs");
206 type Build_Mode_State is (None, Static, Dynamic, Relocatable);
208 procedure Add_Argument (S : String);
209 -- Add one argument to the array Arguments.
210 -- If Arguments is full, double its size.
212 function ALI_File_Name (Source : String) return String;
213 -- Return the ALI file name corresponding to a source.
215 procedure Check (Filename : String);
216 -- Check if filename is a regular file. Fail if it is not.
218 procedure Check_Context;
219 -- Check each object files in table Object_Files
220 -- Fail if any of them is not a regular file
222 procedure Clean (Directory : Name_Id);
223 -- Attempt to delete all files in Directory, but not subdirectories
225 procedure Copy_Interface_Sources
226 (For_Project : Project_Id;
227 Interfaces : Argument_List;
228 To_Dir : Name_Id);
229 -- Copy the interface sources of a SAL to directory To_Dir
231 procedure Display (Executable : String);
232 -- Display invocation of gnatbind and of the compiler with the arguments
233 -- in Arguments, except when Quiet_Output is True.
235 procedure Process_Binder_File (Name : String);
236 -- For Stand-Alone libraries, get the Linker Options in the binder
237 -- generated file.
239 procedure Reset_Tables;
240 -- Make sure that all the above tables are empty
241 -- (Objects, Foreign_Objects, Ali_Files, Options).
243 ------------------
244 -- Add_Argument --
245 ------------------
247 procedure Add_Argument (S : String) is
248 begin
249 if Argument_Number = Arguments'Last then
250 declare
251 New_Args : constant String_List_Access :=
252 new String_List (1 .. 2 * Arguments'Last);
254 begin
255 -- Copy the String_Accesses and set them to null in Arguments
256 -- so that they will not be deallocated by the call to
257 -- Free (Arguments).
259 New_Args (Arguments'Range) := Arguments.all;
260 Arguments.all := (others => null);
261 Free (Arguments);
262 Arguments := New_Args;
263 end;
264 end if;
266 Argument_Number := Argument_Number + 1;
267 Arguments (Argument_Number) := new String'(S);
268 end Add_Argument;
270 -------------------
271 -- ALI_File_Name --
272 -------------------
274 function ALI_File_Name (Source : String) return String is
275 begin
276 -- If the source name has an extension, then replace it with
277 -- the ALI suffix.
279 for Index in reverse Source'First + 1 .. Source'Last loop
280 if Source (Index) = '.' then
281 return Source (Source'First .. Index - 1) & ALI_Suffix;
282 end if;
283 end loop;
285 -- If there is no dot, or if it is the first character, just add the
286 -- ALI suffix.
288 return Source & ALI_Suffix;
289 end ALI_File_Name;
291 -------------------
292 -- Build_Library --
293 -------------------
295 procedure Build_Library
296 (For_Project : Project_Id;
297 Gnatbind : String;
298 Gnatbind_Path : String_Access;
299 Gcc : String;
300 Gcc_Path : String_Access;
301 Bind : Boolean := True;
302 Link : Boolean := True)
304 Warning_For_Library : Boolean := False;
305 -- Set to True for the first warning about a unit missing from the
306 -- interface set.
308 Libgnarl_Needed : Boolean := False;
309 -- Set to True if library needs to be linked with libgnarl
311 Libdecgnat_Needed : Boolean := False;
312 -- On OpenVMS, set to True if library needs to be linked with libdecgnat
314 Gtrasymobj_Needed : Boolean := False;
315 -- On OpenVMS, set to True if library needs to be linked with
316 -- g-trasym.obj.
318 Data : Project_Data := Projects.Table (For_Project);
320 Object_Directory_Path : constant String :=
321 Get_Name_String (Data.Object_Directory);
323 Standalone : constant Boolean := Data.Standalone_Library;
325 Project_Name : constant String := Get_Name_String (Data.Name);
327 Current_Dir : constant String := Get_Current_Dir;
329 Lib_Filename : String_Access;
330 Lib_Dirpath : String_Access;
331 Lib_Version : String_Access := new String'("");
333 The_Build_Mode : Build_Mode_State := None;
335 Success : Boolean := False;
337 Library_Options : Variable_Value := Nil_Variable_Value;
339 Library_GCC : Variable_Value := Nil_Variable_Value;
341 Driver_Name : Name_Id := No_Name;
343 In_Main_Object_Directory : Boolean := True;
345 Rpath : String_Access := null;
346 -- Allocated only if Path Option is supported
348 Rpath_Last : Natural := 0;
349 -- Index of last valid character of Rpath
351 Initial_Rpath_Length : constant := 200;
352 -- Initial size of Rpath, when first allocated
354 Path_Option : String_Access := Linker_Library_Path_Option;
355 -- If null, Path Option is not supported.
356 -- Not a constant so that it can be deallocated.
358 Copy_Dir : Name_Id;
359 -- Directory where to copy ALI files and possibly interface sources
361 First_ALI : Name_Id := No_Name;
362 -- Store the ALI file name of a source of the library (the first found)
364 procedure Add_ALI_For (Source : Name_Id);
365 -- Add the name of the ALI file corresponding to Source to the
366 -- Arguments.
368 procedure Add_Rpath (Path : String);
369 -- Add a path name to Rpath
371 function Check_Project (P : Project_Id) return Boolean;
372 -- Returns True if P is For_Project or a project extended by For_Project
374 procedure Check_Libs (ALI_File : String);
375 -- Set Libgnarl_Needed if the ALI_File indicates that there is a need
376 -- to link with -lgnarl (this is the case when there is a dependency
377 -- on s-osinte.ads). On OpenVMS, set Libdecgnat_Needed if the ALI file
378 -- indicates that there is a need to link with -ldecgnat (this is the
379 -- case when there is a dependency on dec.ads), and set
380 -- Gtrasymobj_Needed if there is a dependency on g-trasym.ads.
382 procedure Process (The_ALI : File_Name_Type);
383 -- Check if the closure of a library unit which is or should be in the
384 -- interface set is also in the interface set. Issue a warning for each
385 -- missing library unit.
387 procedure Process_Imported_Libraries;
388 -- Add the -L and -l switches for the imported Library Project Files,
389 -- and, if Path Option is supported, the library directory path names
390 -- to Rpath.
392 -----------------
393 -- Add_ALI_For --
394 -----------------
396 procedure Add_ALI_For (Source : Name_Id) is
397 ALI : constant String := ALI_File_Name (Get_Name_String (Source));
398 ALI_Id : Name_Id;
400 begin
401 if Bind then
402 Add_Argument (ALI);
403 end if;
405 Name_Len := 0;
406 Add_Str_To_Name_Buffer (S => ALI);
407 ALI_Id := Name_Find;
409 -- Add the ALI file name to the library ALIs
411 if Bind then
412 Library_ALIs.Set (ALI_Id, True);
413 end if;
415 -- Set First_ALI, if not already done
417 if First_ALI = No_Name then
418 First_ALI := ALI_Id;
419 end if;
420 end Add_ALI_For;
422 ---------------
423 -- Add_Rpath --
424 ---------------
426 procedure Add_Rpath (Path : String) is
428 procedure Double;
429 -- Double Rpath size
431 ------------
432 -- Double --
433 ------------
435 procedure Double is
436 New_Rpath : constant String_Access :=
437 new String (1 .. 2 * Rpath'Length);
438 begin
439 New_Rpath (1 .. Rpath_Last) := Rpath (1 .. Rpath_Last);
440 Free (Rpath);
441 Rpath := New_Rpath;
442 end Double;
444 -- Start of processing for Add_Rpath
446 begin
447 -- If firt path, allocate initial Rpath
449 if Rpath = null then
450 Rpath := new String (1 .. Initial_Rpath_Length);
451 Rpath_Last := 0;
453 else
454 -- Otherwise, add a path separator between two path names
456 if Rpath_Last = Rpath'Last then
457 Double;
458 end if;
460 Rpath_Last := Rpath_Last + 1;
461 Rpath (Rpath_Last) := Path_Separator;
462 end if;
464 -- Increase Rpath size until it is large enough
466 while Rpath_Last + Path'Length > Rpath'Last loop
467 Double;
468 end loop;
470 -- Add the path name
472 Rpath (Rpath_Last + 1 .. Rpath_Last + Path'Length) := Path;
473 Rpath_Last := Rpath_Last + Path'Length;
474 end Add_Rpath;
476 -------------------
477 -- Check_Project --
478 -------------------
480 function Check_Project (P : Project_Id) return Boolean is
481 begin
482 if P = For_Project then
483 return True;
485 elsif P /= No_Project then
486 declare
487 Data : Project_Data := Projects.Table (For_Project);
489 begin
490 while Data.Extends /= No_Project loop
491 if P = Data.Extends then
492 return True;
493 end if;
495 Data := Projects.Table (Data.Extends);
496 end loop;
497 end;
498 end if;
500 return False;
501 end Check_Project;
503 ----------------
504 -- Check_Libs --
505 ----------------
507 procedure Check_Libs (ALI_File : String) is
508 Lib_File : Name_Id;
509 Text : Text_Buffer_Ptr;
510 Id : ALI.ALI_Id;
512 begin
513 if not Libgnarl_Needed or
514 (Hostparm.OpenVMS and then
515 ((not Libdecgnat_Needed) or
516 (not Gtrasymobj_Needed)))
517 then
518 -- Scan the ALI file
520 Name_Len := ALI_File'Length;
521 Name_Buffer (1 .. Name_Len) := ALI_File;
522 Lib_File := Name_Find;
523 Text := Read_Library_Info (Lib_File, True);
525 Id := ALI.Scan_ALI
526 (F => Lib_File,
527 T => Text,
528 Ignore_ED => False,
529 Err => True,
530 Read_Lines => "D");
531 Free (Text);
533 -- Look for s-osinte.ads in the dependencies
535 for Index in ALI.ALIs.Table (Id).First_Sdep ..
536 ALI.ALIs.Table (Id).Last_Sdep
537 loop
538 if ALI.Sdep.Table (Index).Sfile = S_Osinte_Ads then
539 Libgnarl_Needed := True;
541 elsif Hostparm.OpenVMS then
542 if ALI.Sdep.Table (Index).Sfile = S_Dec_Ads then
543 Libdecgnat_Needed := True;
545 elsif ALI.Sdep.Table (Index).Sfile = G_Trasym_Ads then
546 Gtrasymobj_Needed := True;
547 end if;
548 end if;
549 end loop;
550 end if;
551 end Check_Libs;
553 -------------
554 -- Process --
555 -------------
557 procedure Process (The_ALI : File_Name_Type) is
558 Text : Text_Buffer_Ptr;
559 Idread : ALI_Id;
560 First_Unit : ALI.Unit_Id;
561 Last_Unit : ALI.Unit_Id;
562 Unit_Data : Unit_Record;
563 Afile : File_Name_Type;
565 begin
566 -- Nothing to do if the ALI file has already been processed.
567 -- This happens if an interface imports another interface.
569 if not Processed_ALIs.Get (The_ALI) then
570 Processed_ALIs.Set (The_ALI, True);
571 Text := Read_Library_Info (The_ALI);
573 if Text /= null then
574 Idread :=
575 Scan_ALI
576 (F => The_ALI,
577 T => Text,
578 Ignore_ED => False,
579 Err => True);
580 Free (Text);
582 if Idread /= No_ALI_Id then
583 First_Unit := ALI.ALIs.Table (Idread).First_Unit;
584 Last_Unit := ALI.ALIs.Table (Idread).Last_Unit;
586 -- Process both unit (spec and body) if the body is needed
587 -- by the spec (inline or generic). Otherwise, just process
588 -- the spec.
590 if First_Unit /= Last_Unit and then
591 not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
592 then
593 First_Unit := Last_Unit;
594 end if;
596 for Unit in First_Unit .. Last_Unit loop
597 Unit_Data := ALI.Units.Table (Unit);
599 -- Check if each withed unit which is in the library is
600 -- also in the interface set, if it has not yet been
601 -- processed.
603 for W in Unit_Data.First_With .. Unit_Data.Last_With loop
604 Afile := Withs.Table (W).Afile;
606 if Afile /= No_Name and then Library_ALIs.Get (Afile)
607 and then not Processed_ALIs.Get (Afile)
608 then
609 if not Interface_ALIs.Get (Afile) then
610 if not Warning_For_Library then
611 Write_Str ("Warning: In library project """);
612 Get_Name_String (Data.Name);
613 To_Mixed (Name_Buffer (1 .. Name_Len));
614 Write_Str (Name_Buffer (1 .. Name_Len));
615 Write_Line ("""");
616 Warning_For_Library := True;
617 end if;
619 Write_Str (" Unit """);
620 Get_Name_String (Withs.Table (W).Uname);
621 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
622 Write_Str (Name_Buffer (1 .. Name_Len - 2));
623 Write_Line (""" is not in the interface set");
624 Write_Str (" but it is needed by ");
626 case Unit_Data.Utype is
627 when Is_Spec =>
628 Write_Str ("the spec of ");
630 when Is_Body =>
631 Write_Str ("the body of ");
633 when others =>
634 null;
635 end case;
637 Write_Str ("""");
638 Get_Name_String (Unit_Data.Uname);
639 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
640 Write_Str (Name_Buffer (1 .. Name_Len - 2));
641 Write_Line ("""");
642 end if;
644 -- Now, process this unit
646 Process (Afile);
647 end if;
648 end loop;
649 end loop;
650 end if;
651 end if;
652 end if;
653 end Process;
655 --------------------------------
656 -- Process_Imported_Libraries --
657 --------------------------------
659 procedure Process_Imported_Libraries is
660 Current : Project_Id;
662 procedure Process_Project (Project : Project_Id);
663 -- Process Project and its imported projects recursively.
664 -- Add any library projects to table Library_Projs.
666 ---------------------
667 -- Process_Project --
668 ---------------------
670 procedure Process_Project (Project : Project_Id) is
671 Data : constant Project_Data := Projects.Table (Project);
672 Imported : Project_List := Data.Imported_Projects;
673 Element : Project_Element;
675 begin
676 -- Nothing to do if process has already been processed
678 if not Processed_Projects.Get (Data.Name) then
679 Processed_Projects.Set (Data.Name, True);
681 -- Call Process_Project recursively for any imported project.
682 -- We first process the imported projects to guarantee that
683 -- we have a proper reverse order for the libraries.
685 while Imported /= Empty_Project_List loop
686 Element := Project_Lists.Table (Imported);
688 if Element.Project /= No_Project then
689 Process_Project (Element.Project);
690 end if;
692 Imported := Element.Next;
693 end loop;
695 -- If it is a library project, add it to Library_Projs
697 if Project /= For_Project and then Data.Library then
698 Library_Projs.Increment_Last;
699 Library_Projs.Table (Library_Projs.Last) := Project;
700 end if;
702 end if;
703 end Process_Project;
705 -- Start of processing for Process_Imported_Libraries
707 begin
708 -- Build list of library projects imported directly or indirectly,
709 -- in the reverse order.
711 Process_Project (For_Project);
713 -- Add the -L and -l switches and, if the Rpath option is supported,
714 -- add the directory to the Rpath.
715 -- As the library projects are in the wrong order, process from the
716 -- last to the first.
718 for Index in reverse 1 .. Library_Projs.Last loop
719 Current := Library_Projs.Table (Index);
721 Get_Name_String (Projects.Table (Current).Library_Dir);
722 Opts.Increment_Last;
723 Opts.Table (Opts.Last) :=
724 new String'("-L" & Name_Buffer (1 .. Name_Len));
726 if Path_Option /= null then
727 Add_Rpath (Name_Buffer (1 .. Name_Len));
728 end if;
730 Opts.Increment_Last;
731 Opts.Table (Opts.Last) :=
732 new String'
733 ("-l" &
734 Get_Name_String
735 (Projects.Table (Current).Library_Name));
736 end loop;
737 end Process_Imported_Libraries;
739 -- Start of processing for Build_Library
741 begin
742 Reset_Tables;
744 -- Fail if project is not a library project
746 if not Data.Library then
747 Com.Fail ("project """, Project_Name, """ has no library");
748 end if;
750 -- If this is the first time Build_Library is called, get the Name_Id
751 -- of "s-osinte.ads".
753 if S_Osinte_Ads = No_Name then
754 Name_Len := 0;
755 Add_Str_To_Name_Buffer ("s-osinte.ads");
756 S_Osinte_Ads := Name_Find;
757 end if;
759 if S_Dec_Ads = No_Name then
760 Name_Len := 0;
761 Add_Str_To_Name_Buffer ("dec.ads");
762 S_Dec_Ads := Name_Find;
763 end if;
765 if G_Trasym_Ads = No_Name then
766 Name_Len := 0;
767 Add_Str_To_Name_Buffer ("g-trasym.ads");
768 G_Trasym_Ads := Name_Find;
769 end if;
771 -- We work in the object directory
773 Change_Dir (Object_Directory_Path);
775 if Standalone then
776 -- Call gnatbind only if Bind is True
778 if Bind then
779 if Gnatbind_Path = null then
780 Com.Fail ("unable to locate ", Gnatbind);
781 end if;
783 if Gcc_Path = null then
784 Com.Fail ("unable to locate ", Gcc);
785 end if;
787 -- Allocate Arguments, if it is the first time we see a standalone
788 -- library.
790 if Arguments = No_Argument then
791 Arguments := new String_List (1 .. Initial_Argument_Max);
792 end if;
794 -- Add "-n -o b~<lib>.adb (b$<lib>.adb on VMS) -L<lib>"
796 Argument_Number := 2;
797 Arguments (1) := No_Main;
798 Arguments (2) := Output_Switch;
800 if Hostparm.OpenVMS then
801 B_Start (B_Start'Last) := '$';
802 end if;
804 Add_Argument
805 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
806 Add_Argument ("-L" & Get_Name_String (Data.Library_Name));
808 -- Check if Binder'Default_Switches ("Ada) is defined. If it is,
809 -- add these switches to call gnatbind.
811 declare
812 Binder_Package : constant Package_Id :=
813 Value_Of
814 (Name => Name_Binder,
815 In_Packages => Data.Decl.Packages);
817 begin
818 if Binder_Package /= No_Package then
819 declare
820 Defaults : constant Array_Element_Id :=
821 Value_Of
822 (Name => Name_Default_Switches,
823 In_Arrays =>
824 Packages.Table
825 (Binder_Package).Decl.Arrays);
826 Switches : Variable_Value := Nil_Variable_Value;
828 Switch : String_List_Id := Nil_String;
830 begin
831 if Defaults /= No_Array_Element then
832 Switches :=
833 Value_Of
834 (Index => Name_Ada,
835 Src_Index => 0,
836 In_Array => Defaults);
838 if not Switches.Default then
839 Switch := Switches.Values;
841 while Switch /= Nil_String loop
842 Add_Argument
843 (Get_Name_String
844 (String_Elements.Table (Switch).Value));
845 Switch := String_Elements.Table (Switch).Next;
846 end loop;
847 end if;
848 end if;
849 end;
850 end if;
851 end;
852 end if;
854 -- Get all the ALI files of the project file. We do that even if
855 -- Bind is False, so that First_ALI is set.
857 declare
858 Unit : Unit_Data;
860 begin
861 Library_ALIs.Reset;
862 Interface_ALIs.Reset;
863 Processed_ALIs.Reset;
865 for Source in 1 .. Com.Units.Last loop
866 Unit := Com.Units.Table (Source);
868 if Unit.File_Names (Body_Part).Name /= No_Name
869 and then Unit.File_Names (Body_Part).Path /= Slash
870 then
872 Check_Project (Unit.File_Names (Body_Part).Project)
873 then
874 if Unit.File_Names (Specification).Name = No_Name then
875 declare
876 Src_Ind : Source_File_Index;
878 begin
879 Src_Ind := Sinput.P.Load_Project_File
880 (Get_Name_String
881 (Unit.File_Names
882 (Body_Part).Path));
884 -- Add the ALI file only if it is not a subunit
887 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
888 then
889 Add_ALI_For
890 (Unit.File_Names (Body_Part).Name);
891 exit when not Bind;
892 end if;
893 end;
895 else
896 Add_ALI_For (Unit.File_Names (Body_Part).Name);
897 exit when not Bind;
898 end if;
899 end if;
901 elsif Unit.File_Names (Specification).Name /= No_Name
902 and then Unit.File_Names (Specification).Path /= Slash
903 and then Check_Project
904 (Unit.File_Names (Specification).Project)
905 then
906 Add_ALI_For (Unit.File_Names (Specification).Name);
907 exit when not Bind;
908 end if;
909 end loop;
910 end;
912 -- Continue setup and call gnatbind if Bind is True
914 if Bind then
916 -- Get an eventual --RTS from the ALI file
918 if First_ALI /= No_Name then
919 declare
920 use Types;
921 T : Text_Buffer_Ptr;
922 A : ALI_Id;
924 begin
925 -- Load the ALI file
927 T := Read_Library_Info (First_ALI, True);
929 -- Read it
931 A := Scan_ALI
932 (First_ALI, T, Ignore_ED => False, Err => False);
934 if A /= No_ALI_Id then
935 for Index in
936 ALI.Units.Table
937 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
938 ALI.Units.Table
939 (ALI.ALIs.Table (A).First_Unit).Last_Arg
940 loop
941 -- Look for --RTS. If found, add the switch to call
942 -- gnatbind.
944 declare
945 Arg : String_Ptr renames Args.Table (Index);
946 begin
948 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
949 then
950 Add_Argument (Arg.all);
951 exit;
952 end if;
953 end;
954 end loop;
955 end if;
956 end;
957 end if;
959 -- Set the paths
961 Set_Ada_Paths
962 (Project => For_Project, Including_Libraries => True);
964 -- Display the gnatbind command, if not in quiet output
966 Display (Gnatbind);
968 -- Invoke gnatbind
970 GNAT.OS_Lib.Spawn
971 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
973 if not Success then
974 Com.Fail ("could not bind standalone library ",
975 Get_Name_String (Data.Library_Name));
976 end if;
977 end if;
979 -- Compile the binder generated file only if Link is true
981 if Link then
982 -- Set the paths
984 Set_Ada_Paths
985 (Project => For_Project, Including_Libraries => True);
987 -- Invoke <gcc> -c b$$<lib>.adb
989 -- Allocate Arguments, if it is the first time we see a standalone
990 -- library.
992 if Arguments = No_Argument then
993 Arguments := new String_List (1 .. Initial_Argument_Max);
994 end if;
996 Argument_Number := 1;
997 Arguments (1) := Compile_Switch;
999 if Hostparm.OpenVMS then
1000 B_Start (B_Start'Last) := '$';
1001 end if;
1003 Add_Argument
1004 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
1006 -- If necessary, add the PIC option
1008 if PIC_Option /= "" then
1009 Add_Argument (PIC_Option);
1010 end if;
1012 -- Get the back-end switches and --RTS from the ALI file
1014 if First_ALI /= No_Name then
1015 declare
1016 use Types;
1017 T : Text_Buffer_Ptr;
1018 A : ALI_Id;
1020 begin
1021 -- Load the ALI file
1023 T := Read_Library_Info (First_ALI, True);
1025 -- Read it
1027 A := Scan_ALI
1028 (First_ALI, T, Ignore_ED => False, Err => False);
1030 if A /= No_ALI_Id then
1031 for Index in
1032 ALI.Units.Table
1033 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
1034 ALI.Units.Table
1035 (ALI.ALIs.Table (A).First_Unit).Last_Arg
1036 loop
1037 -- Do not compile with the front end switches except
1038 -- for --RTS.
1040 declare
1041 Arg : String_Ptr renames Args.Table (Index);
1042 begin
1043 if not Is_Front_End_Switch (Arg.all)
1044 or else
1045 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1046 then
1047 Add_Argument (Arg.all);
1048 end if;
1049 end;
1050 end loop;
1051 end if;
1052 end;
1053 end if;
1055 -- Now that all the arguments are set, compile the binder
1056 -- generated file.
1058 Display (Gcc);
1059 GNAT.OS_Lib.Spawn
1060 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
1062 if not Success then
1063 Com.Fail
1064 ("could not compile binder generated file for library ",
1065 Get_Name_String (Data.Library_Name));
1066 end if;
1068 -- Process binder generated file for pragmas Linker_Options
1070 Process_Binder_File (Arguments (2).all & ASCII.NUL);
1071 end if;
1072 end if;
1074 -- Build the library only if Link is True
1076 if Link then
1077 -- If attribute Library_GCC was specified, get the driver name
1079 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
1081 if not Library_GCC.Default then
1082 Driver_Name := Library_GCC.Value;
1083 end if;
1085 -- If attribute Library_Options was specified, add these additional
1086 -- options.
1088 Library_Options :=
1089 Value_Of (Name_Library_Options, Data.Decl.Attributes);
1091 if not Library_Options.Default then
1092 declare
1093 Current : String_List_Id := Library_Options.Values;
1094 Element : String_Element;
1096 begin
1097 while Current /= Nil_String loop
1098 Element := String_Elements.Table (Current);
1099 Get_Name_String (Element.Value);
1101 if Name_Len /= 0 then
1102 Opts.Increment_Last;
1103 Opts.Table (Opts.Last) :=
1104 new String'(Name_Buffer (1 .. Name_Len));
1105 end if;
1107 Current := Element.Next;
1108 end loop;
1109 end;
1110 end if;
1112 Lib_Dirpath := new String'(Get_Name_String (Data.Library_Dir));
1113 Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
1115 case Data.Library_Kind is
1116 when Static =>
1117 The_Build_Mode := Static;
1119 when Dynamic =>
1120 The_Build_Mode := Dynamic;
1122 when Relocatable =>
1123 The_Build_Mode := Relocatable;
1125 if PIC_Option /= "" then
1126 Opts.Increment_Last;
1127 Opts.Table (Opts.Last) := new String'(PIC_Option);
1128 end if;
1129 end case;
1131 -- Get the library version, if any
1133 if Data.Lib_Internal_Name /= No_Name then
1134 Lib_Version :=
1135 new String'(Get_Name_String (Data.Lib_Internal_Name));
1136 end if;
1138 -- Add the objects found in the object directory and the object
1139 -- directories of the extended files, if any, except for generated
1140 -- object files (b~.. or B$..) from extended projects.
1141 -- When there are one or more extended files, only add an object file
1142 -- if no object file with the same name have already been added.
1144 In_Main_Object_Directory := True;
1146 loop
1147 declare
1148 Object_Dir_Path : constant String :=
1149 Get_Name_String (Data.Object_Directory);
1150 Object_Dir : Dir_Type;
1151 Filename : String (1 .. 255);
1152 Last : Natural;
1153 Id : Name_Id;
1155 begin
1156 Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1158 -- For all entries in the object directory
1160 loop
1161 Read (Object_Dir, Filename, Last);
1163 exit when Last = 0;
1165 -- Check if it is an object file
1167 if Is_Obj (Filename (1 .. Last)) then
1168 declare
1169 Object_Path : String :=
1170 Normalize_Pathname
1171 (Object_Dir_Path & Directory_Separator &
1172 Filename (1 .. Last));
1174 begin
1175 Canonical_Case_File_Name (Object_Path);
1176 Canonical_Case_File_Name (Filename (1 .. Last));
1178 -- If in the object directory of an extended project,
1179 -- do not consider generated object files.
1181 if In_Main_Object_Directory
1182 or else Last < 5
1183 or else Filename (1 .. B_Start'Length) /= B_Start
1184 then
1185 Name_Len := Last;
1186 Name_Buffer (1 .. Name_Len) := Filename (1 .. Last);
1187 Id := Name_Find;
1189 if not Objects_Htable.Get (Id) then
1191 -- Record this object file
1193 Objects_Htable.Set (Id, True);
1194 Objects.Increment_Last;
1195 Objects.Table (Objects.Last) :=
1196 new String'(Object_Path);
1198 declare
1199 ALI_File : constant String :=
1200 Ext_To (Object_Path, "ali");
1202 begin
1203 if Is_Regular_File (ALI_File) then
1205 -- Record the ALI file
1207 ALIs.Increment_Last;
1208 ALIs.Table (ALIs.Last) :=
1209 new String'(ALI_File);
1211 -- Find out if for this ALI file,
1212 -- libgnarl or libdecgnat or g-trasym.obj
1213 -- (on OpenVMS) is necessary.
1215 Check_Libs (ALI_File);
1217 else
1218 -- Object file is a foreign object file
1220 Foreigns.Increment_Last;
1221 Foreigns.Table (Foreigns.Last) :=
1222 new String'(Object_Path);
1223 end if;
1224 end;
1225 end if;
1226 end if;
1227 end;
1228 end if;
1229 end loop;
1231 Close (Dir => Object_Dir);
1233 exception
1234 when Directory_Error =>
1235 Com.Fail ("cannot find object directory """,
1236 Get_Name_String (Data.Object_Directory),
1237 """");
1238 end;
1240 exit when Data.Extends = No_Project;
1242 In_Main_Object_Directory := False;
1243 Data := Projects.Table (Data.Extends);
1244 end loop;
1246 -- Add the -L and -l switches for the imported Library Project Files,
1247 -- and, if Path Option is supported, the library directory path names
1248 -- to Rpath.
1250 Process_Imported_Libraries;
1252 -- Link with libgnat and possibly libgnarl
1254 Opts.Increment_Last;
1255 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1257 -- If Path Option is supported, add libgnat directory path name to
1258 -- Rpath.
1260 if Path_Option /= null then
1261 Add_Rpath (Lib_Directory);
1262 end if;
1264 if Libgnarl_Needed then
1265 Opts.Increment_Last;
1267 if The_Build_Mode = Static then
1268 Opts.Table (Opts.Last) := new String'("-lgnarl");
1269 else
1270 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnarl"));
1271 end if;
1272 end if;
1274 if Gtrasymobj_Needed then
1275 Opts.Increment_Last;
1276 Opts.Table (Opts.Last) :=
1277 new String'(Lib_Directory & "/g-trasym.obj");
1278 end if;
1280 if Libdecgnat_Needed then
1281 Opts.Increment_Last;
1282 Opts.Table (Opts.Last) :=
1283 new String'("-L" & Lib_Directory & "/../declib");
1284 Opts.Increment_Last;
1285 Opts.Table (Opts.Last) := new String'("-ldecgnat");
1286 end if;
1288 Opts.Increment_Last;
1290 if The_Build_Mode = Static then
1291 Opts.Table (Opts.Last) := new String'("-lgnat");
1292 else
1293 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnat"));
1294 end if;
1296 -- If Path Option is supported, add the necessary switch with the
1297 -- content of Rpath. As Rpath contains at least libgnat directory
1298 -- path name, it is guaranteed that it is not null.
1300 if Path_Option /= null then
1301 Opts.Increment_Last;
1302 Opts.Table (Opts.Last) :=
1303 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1304 Free (Path_Option);
1305 Free (Rpath);
1306 end if;
1308 Object_Files :=
1309 new Argument_List'
1310 (Argument_List (Objects.Table (1 .. Objects.Last)));
1312 Foreign_Objects :=
1313 new Argument_List'(Argument_List
1314 (Foreigns.Table (1 .. Foreigns.Last)));
1316 Ali_Files :=
1317 new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1319 Options :=
1320 new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1322 -- We fail if there are no object to put in the library
1323 -- (Ada or foreign objects).
1325 if Object_Files'Length = 0 then
1326 Com.Fail ("no object files for library """ &
1327 Lib_Filename.all & '"');
1328 end if;
1330 if not Opt.Quiet_Output then
1331 Write_Eol;
1332 Write_Str ("building ");
1333 Write_Str (Ada.Characters.Handling.To_Lower
1334 (Build_Mode_State'Image (The_Build_Mode)));
1335 Write_Str (" library for project ");
1336 Write_Line (Project_Name);
1338 Write_Eol;
1340 Write_Line ("object files:");
1342 for Index in Object_Files'Range loop
1343 Write_Str (" ");
1344 Write_Line (Object_Files (Index).all);
1345 end loop;
1347 Write_Eol;
1349 if Ali_Files'Length = 0 then
1350 Write_Line ("NO ALI files");
1352 else
1353 Write_Line ("ALI files:");
1355 for Index in Ali_Files'Range loop
1356 Write_Str (" ");
1357 Write_Line (Ali_Files (Index).all);
1358 end loop;
1359 end if;
1361 Write_Eol;
1362 end if;
1364 -- We check that all object files are regular files
1366 Check_Context;
1368 -- Delete the existing library file, if it exists.
1369 -- Fail if the library file is not writable, or if it is not possible
1370 -- to delete the file.
1372 declare
1373 DLL_Name : aliased String :=
1374 Lib_Dirpath.all & "/lib" &
1375 Lib_Filename.all & "." & DLL_Ext;
1377 Archive_Name : aliased String :=
1378 Lib_Dirpath.all & "/lib" &
1379 Lib_Filename.all & "." & Archive_Ext;
1381 type Str_Ptr is access all String;
1382 -- This type is necessary to meet the accessibility rules of Ada.
1383 -- It is not possible to use String_Access here.
1385 Full_Lib_Name : Str_Ptr;
1386 -- Designates the full library path name. Either DLL_Name or
1387 -- Archive_Name, depending on the library kind.
1389 Success : Boolean := False;
1390 -- Used to call Delete_File
1392 begin
1393 if The_Build_Mode = Static then
1394 Full_Lib_Name := Archive_Name'Access;
1395 else
1396 Full_Lib_Name := DLL_Name'Access;
1397 end if;
1399 if Is_Regular_File (Full_Lib_Name.all) then
1400 if Is_Writable_File (Full_Lib_Name.all) then
1401 Delete_File (Full_Lib_Name.all, Success);
1402 end if;
1404 if Is_Regular_File (Full_Lib_Name.all) then
1405 Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1406 end if;
1407 end if;
1408 end;
1410 Argument_Number := 0;
1412 -- If we have a standalone library, gather all the interface ALI.
1413 -- They are passed to Build_Dynamic_Library, where they are used by
1414 -- some platforms (VMS, for example) to decide what symbols should be
1415 -- exported. They are also flagged as Interface when we copy them to
1416 -- the library directory (by Copy_ALI_Files, below).
1418 if Standalone then
1419 Data := Projects.Table (For_Project);
1421 declare
1422 Iface : String_List_Id := Data.Lib_Interface_ALIs;
1423 ALI : File_Name_Type;
1425 begin
1426 while Iface /= Nil_String loop
1427 ALI := String_Elements.Table (Iface).Value;
1428 Interface_ALIs.Set (ALI, True);
1429 Get_Name_String (String_Elements.Table (Iface).Value);
1430 Add_Argument (Name_Buffer (1 .. Name_Len));
1431 Iface := String_Elements.Table (Iface).Next;
1432 end loop;
1434 Iface := Data.Lib_Interface_ALIs;
1436 if not Opt.Quiet_Output then
1438 -- Check that the interface set is complete: any unit in the
1439 -- library that is needed by an interface should also be an
1440 -- interface. If it is not the case, output a warning.
1442 while Iface /= Nil_String loop
1443 ALI := String_Elements.Table (Iface).Value;
1444 Process (ALI);
1445 Iface := String_Elements.Table (Iface).Next;
1446 end loop;
1447 end if;
1448 end;
1449 end if;
1451 -- Clean the library directory, if it is also the directory where
1452 -- the ALI files are copied, either because there is no interface
1453 -- copy directory or because the interface copy directory is the
1454 -- same as the library directory.
1456 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1457 Clean (Copy_Dir);
1459 -- Call procedure to build the library, depending on the build mode
1461 case The_Build_Mode is
1462 when Dynamic | Relocatable =>
1463 Build_Dynamic_Library
1464 (Ofiles => Object_Files.all,
1465 Foreign => Foreign_Objects.all,
1466 Afiles => Ali_Files.all,
1467 Options => Options.all,
1468 Options_2 => No_Argument_List,
1469 Interfaces => Arguments (1 .. Argument_Number),
1470 Lib_Filename => Lib_Filename.all,
1471 Lib_Dir => Lib_Dirpath.all,
1472 Symbol_Data => Data.Symbol_Data,
1473 Driver_Name => Driver_Name,
1474 Lib_Version => Lib_Version.all,
1475 Auto_Init => Data.Lib_Auto_Init);
1477 when Static =>
1478 MLib.Build_Library
1479 (Object_Files.all,
1480 Ali_Files.all,
1481 Lib_Filename.all,
1482 Lib_Dirpath.all);
1484 when None =>
1485 null;
1486 end case;
1488 -- We need to copy the ALI files from the object directory to
1489 -- the library directory, so that the linker find them there,
1490 -- and does not need to look in the object directory where it
1491 -- would also find the object files; and we don't want that:
1492 -- we want the linker to use the library.
1494 -- Copy the ALI files and make the copies read-only. For interfaces,
1495 -- mark the copies as interfaces.
1497 Copy_ALI_Files
1498 (Files => Ali_Files.all,
1499 To => Copy_Dir,
1500 Interfaces => Arguments (1 .. Argument_Number));
1502 -- Copy interface sources if Library_Src_Dir specified
1504 if Standalone
1505 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1506 then
1507 -- Clean the interface copy directory, if it is not also the
1508 -- library directory. If it is also the library directory, it
1509 -- has already been cleaned before generation of the library.
1511 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1512 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1513 Clean (Copy_Dir);
1514 end if;
1516 Copy_Interface_Sources
1517 (For_Project => For_Project,
1518 Interfaces => Arguments (1 .. Argument_Number),
1519 To_Dir => Copy_Dir);
1520 end if;
1521 end if;
1523 -- Reset the current working directory to its previous value
1525 Change_Dir (Current_Dir);
1526 end Build_Library;
1528 -----------
1529 -- Check --
1530 -----------
1532 procedure Check (Filename : String) is
1533 begin
1534 if not Is_Regular_File (Filename) then
1535 Com.Fail (Filename, " not found.");
1536 end if;
1537 end Check;
1539 -------------------
1540 -- Check_Context --
1541 -------------------
1543 procedure Check_Context is
1544 begin
1545 -- Check that each object file exists
1547 for F in Object_Files'Range loop
1548 Check (Object_Files (F).all);
1549 end loop;
1550 end Check_Context;
1552 -------------------
1553 -- Check_Library --
1554 -------------------
1556 procedure Check_Library (For_Project : Project_Id) is
1557 Data : constant Project_Data := Projects.Table (For_Project);
1559 begin
1560 -- No need to build the library if there is no object directory,
1561 -- hence no object files to build the library.
1563 if Data.Library
1564 and then not Data.Need_To_Build_Lib
1565 and then Data.Object_Directory /= No_Name
1566 then
1567 declare
1568 Current : constant Dir_Name_Str := Get_Current_Dir;
1569 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1570 Lib_TS : Time_Stamp_Type;
1571 Obj_TS : Time_Stamp_Type;
1573 Object_Dir : Dir_Type;
1575 begin
1576 if Hostparm.OpenVMS then
1577 B_Start (B_Start'Last) := '$';
1578 end if;
1580 Change_Dir (Get_Name_String (Data.Library_Dir));
1582 Lib_TS := File_Stamp (Lib_Name);
1584 -- If the library file does not exist, then the time stamp will
1585 -- be Empty_Time_Stamp, earlier than any other time stamp.
1587 Change_Dir (Get_Name_String (Data.Object_Directory));
1588 Open (Dir => Object_Dir, Dir_Name => ".");
1590 -- For all entries in the object directory
1592 loop
1593 Read (Object_Dir, Name_Buffer, Name_Len);
1594 exit when Name_Len = 0;
1596 -- Check if it is an object file, but ignore any binder
1597 -- generated file.
1599 if Is_Obj (Name_Buffer (1 .. Name_Len))
1600 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1601 then
1602 -- Get the object file time stamp
1604 Obj_TS := File_Stamp (Name_Find);
1606 -- If library file time stamp is earlier, set
1607 -- Need_To_Build_Lib and return. String comparaison is used,
1608 -- otherwise time stamps may be too close and the
1609 -- comparaison would return True, which would trigger
1610 -- an unnecessary rebuild of the library.
1612 if String (Lib_TS) < String (Obj_TS) then
1614 -- Library must be rebuilt
1616 Projects.Table (For_Project).Need_To_Build_Lib := True;
1617 exit;
1618 end if;
1619 end if;
1620 end loop;
1622 Change_Dir (Current);
1623 end;
1624 end if;
1625 end Check_Library;
1627 -----------
1628 -- Clean --
1629 -----------
1631 procedure Clean (Directory : Name_Id) is
1632 Current : constant Dir_Name_Str := Get_Current_Dir;
1634 Dir : Dir_Type;
1636 Name : String (1 .. 200);
1637 Last : Natural;
1639 Disregard : Boolean;
1641 begin
1642 Get_Name_String (Directory);
1644 -- Change the working directory to the directory to clean
1646 begin
1647 Change_Dir (Name_Buffer (1 .. Name_Len));
1649 exception
1650 when others =>
1651 Com.Fail
1652 ("unable to access directory """,
1653 Name_Buffer (1 .. Name_Len),
1654 """");
1655 end;
1657 Open (Dir, ".");
1659 -- For each regular file in the directory, make it writable and
1660 -- delete the file.
1662 loop
1663 Read (Dir, Name, Last);
1664 exit when Last = 0;
1666 if Is_Regular_File (Name (1 .. Last)) then
1667 Set_Writable (Name (1 .. Last));
1668 Delete_File (Name (1 .. Last), Disregard);
1669 end if;
1670 end loop;
1672 Close (Dir);
1674 -- Restore the initial working directory
1676 Change_Dir (Current);
1677 end Clean;
1679 ----------------------------
1680 -- Copy_Interface_Sources --
1681 ----------------------------
1683 procedure Copy_Interface_Sources
1684 (For_Project : Project_Id;
1685 Interfaces : Argument_List;
1686 To_Dir : Name_Id)
1688 Current : constant Dir_Name_Str := Get_Current_Dir;
1689 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1691 Text : Text_Buffer_Ptr;
1692 The_ALI : ALI.ALI_Id;
1693 Lib_File : Name_Id;
1695 First_Unit : ALI.Unit_Id;
1696 Second_Unit : ALI.Unit_Id;
1698 Data : Unit_Data;
1700 Copy_Subunits : Boolean := False;
1702 procedure Copy (File_Name : Name_Id);
1703 -- Copy one source of the project to the target directory
1705 ----------
1706 -- Copy --
1707 ----------
1709 procedure Copy (File_Name : Name_Id) is
1710 Success : Boolean := False;
1712 begin
1713 Unit_Loop :
1714 for Index in 1 .. Com.Units.Last loop
1715 Data := Com.Units.Table (Index);
1717 for J in Data.File_Names'Range loop
1718 if Data.File_Names (J).Project = For_Project
1719 and then Data.File_Names (J).Name = File_Name
1720 then
1721 Copy_File
1722 (Get_Name_String (Data.File_Names (J).Path),
1723 Target,
1724 Success,
1725 Mode => Overwrite,
1726 Preserve => Preserve);
1727 exit Unit_Loop;
1728 end if;
1729 end loop;
1730 end loop Unit_Loop;
1731 end Copy;
1733 use ALI;
1735 -- Start of processing for Copy_Interface_Sources
1737 begin
1738 -- Change the working directory to the object directory
1740 Change_Dir
1741 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1743 for Index in Interfaces'Range loop
1745 -- First, load the ALI file
1747 Name_Len := 0;
1748 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1749 Lib_File := Name_Find;
1750 Text := Read_Library_Info (Lib_File);
1751 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1752 Free (Text);
1754 Second_Unit := No_Unit_Id;
1755 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1756 Copy_Subunits := True;
1758 -- If there is both a spec and a body, check if they are both needed
1760 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1761 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1763 -- If the body is not needed, then reset First_Unit
1765 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1766 First_Unit := No_Unit_Id;
1767 Copy_Subunits := False;
1768 end if;
1770 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1771 Copy_Subunits := False;
1772 end if;
1774 -- Copy the file(s) that need to be copied
1776 if First_Unit /= No_Unit_Id then
1777 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1778 end if;
1780 if Second_Unit /= No_Unit_Id then
1781 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1782 end if;
1784 -- Copy all the separates, if any
1786 if Copy_Subunits then
1787 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1788 ALI.ALIs.Table (The_ALI).Last_Sdep
1789 loop
1790 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1791 Copy (File_Name => Sdep.Table (Dep).Sfile);
1792 end if;
1793 end loop;
1794 end if;
1795 end loop;
1797 -- Restore the initial working directory
1799 Change_Dir (Current);
1800 end Copy_Interface_Sources;
1802 -------------
1803 -- Display --
1804 -------------
1806 procedure Display (Executable : String) is
1807 begin
1808 if not Opt.Quiet_Output then
1809 Write_Str (Executable);
1811 for Index in 1 .. Argument_Number loop
1812 Write_Char (' ');
1813 Write_Str (Arguments (Index).all);
1814 end loop;
1816 Write_Eol;
1817 end if;
1818 end Display;
1820 -------------------------
1821 -- Process_Binder_File --
1822 -------------------------
1824 procedure Process_Binder_File (Name : String) is
1825 Fd : FILEs;
1826 -- Binder file's descriptor
1828 Read_Mode : constant String := "r" & ASCII.Nul;
1829 -- For fopen
1831 Status : Interfaces.C_Streams.int;
1832 pragma Unreferenced (Status);
1833 -- For fclose
1835 Begin_Info : constant String := "-- BEGIN Object file/option list";
1836 End_Info : constant String := "-- END Object file/option list ";
1838 Next_Line : String (1 .. 1000);
1839 -- Current line value
1840 -- Where does this odd constant 1000 come from, looks suspicious ???
1842 Nlast : Integer;
1843 -- End of line slice (the slice does not contain the line terminator)
1845 procedure Get_Next_Line;
1846 -- Read the next line from the binder file without the line terminator
1848 -------------------
1849 -- Get_Next_Line --
1850 -------------------
1852 procedure Get_Next_Line is
1853 Fchars : chars;
1855 begin
1856 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
1858 if Fchars = System.Null_Address then
1859 Fail ("Error reading binder output");
1860 end if;
1862 Nlast := 1;
1863 while Nlast <= Next_Line'Last
1864 and then Next_Line (Nlast) /= ASCII.LF
1865 and then Next_Line (Nlast) /= ASCII.CR
1866 loop
1867 Nlast := Nlast + 1;
1868 end loop;
1870 Nlast := Nlast - 1;
1871 end Get_Next_Line;
1873 -- Start of processing for Process_Binder_File
1875 begin
1876 Fd := fopen (Name'Address, Read_Mode'Address);
1878 if Fd = NULL_Stream then
1879 Fail ("Failed to open binder output");
1880 end if;
1882 -- Skip up to the Begin Info line
1884 loop
1885 Get_Next_Line;
1886 exit when Next_Line (1 .. Nlast) = Begin_Info;
1887 end loop;
1889 -- Find the first switch
1891 loop
1892 Get_Next_Line;
1894 exit when Next_Line (1 .. Nlast) = End_Info;
1896 -- As the binder generated file is in Ada, remove the first eight
1897 -- characters " -- ".
1899 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1900 Nlast := Nlast - 8;
1902 -- Stop when the first switch is found
1904 exit when Next_Line (1) = '-';
1905 end loop;
1907 if Next_Line (1 .. Nlast) /= End_Info then
1908 loop
1909 -- Ignore -static and -shared, since -shared will be used
1910 -- in any case.
1912 -- Ignore -lgnat, -lgnarl and -ldecgnat as they will be added
1913 -- later, because they are also needed for non Stand-Alone shared
1914 -- libraries.
1916 -- Also ignore the shared libraries which are :
1918 -- UNIX / Windows VMS
1919 -- -lgnat-<version> -lgnat_<version> (7 + version'length chars)
1920 -- -lgnarl-<version> -lgnarl_<version> (8 + version'length chars)
1922 if Next_Line (1 .. Nlast) /= "-static" and then
1923 Next_Line (1 .. Nlast) /= "-shared" and then
1924 Next_Line (1 .. Nlast) /= "-ldecgnat" and then
1925 Next_Line (1 .. Nlast) /= "-lgnarl" and then
1926 Next_Line (1 .. Nlast) /= "-lgnat" and then
1927 Next_Line
1928 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) /=
1929 Shared_Lib ("gnarl") and then
1930 Next_Line
1931 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) /=
1932 Shared_Lib ("gnat")
1933 then
1934 if Next_Line (1) /= '-' then
1936 -- This is not an option, should we add it?
1938 if Add_Object_Files then
1939 Opts.Increment_Last;
1940 Opts.Table (Opts.Last) :=
1941 new String'(Next_Line (1 .. Nlast));
1942 end if;
1944 else
1945 -- Add all other options
1947 Opts.Increment_Last;
1948 Opts.Table (Opts.Last) :=
1949 new String'(Next_Line (1 .. Nlast));
1950 end if;
1951 end if;
1953 -- Next option, if any
1955 Get_Next_Line;
1956 exit when Next_Line (1 .. Nlast) = End_Info;
1958 -- Remove first eight characters " -- "
1960 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1961 Nlast := Nlast - 8;
1962 end loop;
1963 end if;
1965 Status := fclose (Fd);
1966 -- Is it really right to ignore any close error ???
1967 end Process_Binder_File;
1969 ------------------
1970 -- Reset_Tables --
1971 ------------------
1973 procedure Reset_Tables is
1974 begin
1975 Objects.Init;
1976 Objects_Htable.Reset;
1977 Foreigns.Init;
1978 ALIs.Init;
1979 Opts.Init;
1980 Processed_Projects.Reset;
1981 Library_Projs.Init;
1982 end Reset_Tables;
1984 end MLib.Prj;