prj-env.adb: (Contains_ALI_Files): New Boolean function
authorVincent Celier <celier@gnat.com>
Wed, 27 Oct 2004 13:38:32 +0000 (15:38 +0200)
committerArnaud Charlet <charlet@gcc.gnu.org>
Wed, 27 Oct 2004 13:38:32 +0000 (15:38 +0200)
2004-10-26  Vincent Celier  <celier@gnat.com>

* prj-env.adb: (Contains_ALI_Files): New Boolean function
(Ada_Objects_Path.Add): For a library project, add to the object path
the library directory only if there is no object directory or if the
library directory contains ALI files.
(Set_Ada_Paths.Add.Recursive_Add): Ditto

From-SVN: r89661

gcc/ada/prj-env.adb

index b751e6be7aa98065470d8f85e786368f2e7a8382..517a2ee57c46fc28672da3fb043bc0545377aaed 100644 (file)
@@ -32,7 +32,8 @@ with Prj.Com;  use Prj.Com;
 with Table;
 with Tempdir;
 
-with GNAT.OS_Lib; use GNAT.OS_Lib;
+with GNAT.Directory_Operations; use GNAT.Directory_Operations;
+with GNAT.OS_Lib;               use GNAT.OS_Lib;
 
 package body Prj.Env is
 
@@ -135,6 +136,9 @@ package body Prj.Env is
    --  Add Object_Dir to object path table. Make sure it is not duplicate
    --  and it is the last one in the current table.
 
+   function Contains_ALI_Files (Dir : Name_Id) return Boolean;
+   --  Return True if there is at least one ALI file in the directory Dir
+
    procedure Create_New_Path_File
      (Path_FD   : out File_Descriptor;
       Path_Name : out Name_Id);
@@ -276,10 +280,18 @@ package body Prj.Env is
                    and then
                    (not Including_Libraries or else not Data.Library))
                then
-                  --  For a library project, add the library directory
+                  --  For a library project, add the library directory,
+                  --  if there is no object directory or if it contains ALI
+                  --  files; otherwise add the object directory.
 
                   if Data.Library then
-                     Add_To_Path (Get_Name_String (Data.Library_Dir));
+                     if Data.Object_Directory = No_Name
+                       or else Contains_ALI_Files (Data.Library_Dir)
+                     then
+                        Add_To_Path (Get_Name_String (Data.Library_Dir));
+                     else
+                        Add_To_Path (Get_Name_String (Data.Object_Directory));
+                     end if;
 
                   else
                      --  For a non library project, add the object directory
@@ -546,6 +558,45 @@ package body Prj.Env is
       return Namet.Get_Name_String (Data.File_Names (Body_Part).Path);
    end Body_Path_Name_Of;
 
+   ------------------------
+   -- Contains_ALI_Files --
+   ------------------------
+
+   function Contains_ALI_Files (Dir : Name_Id) return Boolean is
+      Dir_Name : constant String := Get_Name_String (Dir);
+      Direct : Dir_Type;
+      Name   : String (1 .. 1_000);
+      Last   : Natural;
+      Result : Boolean := False;
+
+   begin
+      Open (Direct, Dir_Name);
+
+      --  For each file in the directory, check if it is an ALI file
+
+      loop
+         Read (Direct, Name, Last);
+         exit when Last = 0;
+         Canonical_Case_File_Name (Name (1 .. Last));
+         Result := Last >= 5 and then Name (Last - 3 .. Last) = ".ali";
+         exit when Result;
+      end loop;
+
+      Close (Direct);
+      return Result;
+
+   exception
+      --  If there is any problem, close the directory if open and return
+      --  True; the library directory will be added to the path.
+
+      when others =>
+         if Is_Open (Direct) then
+            Close (Direct);
+         end if;
+
+         return True;
+   end Contains_ALI_Files;
+
    --------------------------------
    -- Create_Config_Pragmas_File --
    --------------------------------
@@ -1966,9 +2017,18 @@ package body Prj.Env is
                             (not Including_Libraries or else not Data.Library))
                      then
                         --  For a library project, add the library directory
+                        --  if there is no object directory or if the library
+                        --  directory contains ALI files; otherwise add the
+                        --  object directory.
 
                         if Data.Library then
-                           Add_To_Object_Path (Data.Library_Dir);
+                           if Data.Object_Directory = No_Name
+                             or else Contains_ALI_Files (Data.Library_Dir)
+                           then
+                              Add_To_Object_Path (Data.Library_Dir);
+                           else
+                              Add_To_Object_Path (Data.Object_Directory);
+                           end if;
 
                         --  For a non-library project, add the object
                         --  directory, if it is not a virtual project.