aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/prj-ext.adb
blob: 5f134008b1cec0d496edbe15f03022aa05a78eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                              P R J . E X T                               --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 2000-2013, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

with Osint;    use Osint;

with Ada.Unchecked_Deallocation;

package body Prj.Ext is

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (Self      : out External_References;
      Copy_From : External_References := No_External_Refs)
   is
      N  : Name_To_Name_Ptr;
      N2 : Name_To_Name_Ptr;
   begin
      if Self.Refs = null then
         Self.Refs := new Name_To_Name_HTable.Instance;

         if Copy_From.Refs /= null then
            N := Name_To_Name_HTable.Get_First (Copy_From.Refs.all);
            while N /= null loop
               N2 := new Name_To_Name'
                           (Key    => N.Key,
                            Value  => N.Value,
                            Source => N.Source,
                            Next   => null);
               Name_To_Name_HTable.Set (Self.Refs.all, N2);
               N := Name_To_Name_HTable.Get_Next (Copy_From.Refs.all);
            end loop;
         end if;
      end if;
   end Initialize;

   ---------
   -- Add --
   ---------

   procedure Add
     (Self          : External_References;
      External_Name : String;
      Value         : String;
      Source        : External_Source := External_Source'First;
      Silent        : Boolean := False)
   is
      Key : Name_Id;
      N   : Name_To_Name_Ptr;

   begin
      --  For external attribute, set the environment variable

      if Source = From_External_Attribute and then External_Name /= "" then
         declare
            Env_Var : String_Access := Getenv (External_Name);

         begin
            if Env_Var = null or else Env_Var.all = "" then
               Setenv (Name => External_Name, Value => Value);

               if not Silent then
                  Debug_Output
                    ("Environment variable """ & External_Name
                     & """ = """ & Value & '"');
               end if;

            elsif not Silent then
               Debug_Output
                 ("Not overriding existing environment variable """
                  & External_Name & """, value is """ & Env_Var.all & '"');
            end if;

            Free (Env_Var);
         end;
      end if;

      Name_Len := External_Name'Length;
      Name_Buffer (1 .. Name_Len) := External_Name;
      Canonical_Case_Env_Var_Name (Name_Buffer (1 .. Name_Len));
      Key := Name_Find;

      --  Check whether the value is already defined, to properly respect the
      --  overriding order.

      if Source /= External_Source'First then
         N := Name_To_Name_HTable.Get (Self.Refs.all, Key);

         if N /= null then
            if External_Source'Pos (N.Source) <
               External_Source'Pos (Source)
            then
               if not Silent then
                  Debug_Output
                    ("Not overridding existing external reference '"
                     & External_Name & "', value was defined in "
                     & N.Source'Img);
               end if;

               return;
            end if;
         end if;
      end if;

      Name_Len := Value'Length;
      Name_Buffer (1 .. Name_Len) := Value;
      N := new Name_To_Name'
                 (Key    => Key,
                  Source => Source,
                  Value  => Name_Find,
                  Next   => null);

      if not Silent then
         Debug_Output ("Add external (" & External_Name & ") is", N.Value);
      end if;

      Name_To_Name_HTable.Set (Self.Refs.all, N);
   end Add;

   -----------
   -- Check --
   -----------

   function Check
     (Self        : External_References;
      Declaration : String) return Boolean
   is
   begin
      for Equal_Pos in Declaration'Range loop
         if Declaration (Equal_Pos) = '=' then
            exit when Equal_Pos = Declaration'First;
            Add
              (Self          => Self,
               External_Name =>
                 Declaration (Declaration'First .. Equal_Pos - 1),
               Value         =>
                 Declaration (Equal_Pos + 1 .. Declaration'Last),
               Source        => From_Command_Line);
            return True;
         end if;
      end loop;

      return False;
   end Check;

   -----------
   -- Reset --
   -----------

   procedure Reset (Self : External_References) is
   begin
      if Self.Refs /= null then
         Debug_Output ("Reset external references");
         Name_To_Name_HTable.Reset (Self.Refs.all);
      end if;
   end Reset;

   --------------
   -- Value_Of --
   --------------

   function Value_Of
     (Self          : External_References;
      External_Name : Name_Id;
      With_Default  : Name_Id := No_Name)
      return          Name_Id
   is
      Value : Name_To_Name_Ptr;
      Val   : Name_Id;
      Name  : String := Get_Name_String (External_Name);

   begin
      Canonical_Case_Env_Var_Name (Name);

      if Self.Refs /= null then
         Name_Len := Name'Length;
         Name_Buffer (1 .. Name_Len) := Name;
         Value := Name_To_Name_HTable.Get (Self.Refs.all, Name_Find);

         if Value /= null then
            Debug_Output ("Value_Of (" & Name & ") is in cache", Value.Value);
            return Value.Value;
         end if;
      end if;

      --  Find if it is an environment, if it is, put value in the hash table

      declare
         Env_Value : String_Access := Getenv (Name);

      begin
         if Env_Value /= null and then Env_Value'Length > 0 then
            Name_Len := Env_Value'Length;
            Name_Buffer (1 .. Name_Len) := Env_Value.all;
            Val := Name_Find;

            if Current_Verbosity = High then
               Debug_Output ("Value_Of (" & Name & ") is", Val);
            end if;

            if Self.Refs /= null then
               Value := new Name_To_Name'
                 (Key    => External_Name,
                  Value  => Val,
                  Source => From_Environment,
                  Next   => null);
               Name_To_Name_HTable.Set (Self.Refs.all, Value);
            end if;

            Free (Env_Value);
            return Val;

         else
            if Current_Verbosity = High then
               Debug_Output
                 ("Value_Of (" & Name & ") is default", With_Default);
            end if;

            Free (Env_Value);
            return With_Default;
         end if;
      end;
   end Value_Of;

   ----------
   -- Free --
   ----------

   procedure Free (Self : in out External_References) is
      procedure Unchecked_Free is new Ada.Unchecked_Deallocation
        (Name_To_Name_HTable.Instance, Instance_Access);
   begin
      if Self.Refs /= null then
         Reset (Self);
         Unchecked_Free (Self.Refs);
      end if;
   end Free;

   --------------
   -- Set_Next --
   --------------

   procedure Set_Next (E : Name_To_Name_Ptr; Next : Name_To_Name_Ptr) is
   begin
      E.Next := Next;
   end Set_Next;

   ----------
   -- Next --
   ----------

   function Next (E : Name_To_Name_Ptr) return Name_To_Name_Ptr is
   begin
      return E.Next;
   end Next;

   -------------
   -- Get_Key --
   -------------

   function Get_Key (E : Name_To_Name_Ptr) return Name_Id is
   begin
      return E.Key;
   end Get_Key;

end Prj.Ext;