1# Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with GCC; see the file COPYING3.  If not see
15# <http://www.gnu.org/licenses/>.
16
17# This file was contributed by John David Anglin (dave.anglin@nrc-cnrc.gc.ca)
18
19set orig_environment_saved 0
20set orig_ld_library_path_saved 0
21set orig_ld_run_path_saved 0
22set orig_shlib_path_saved 0
23set orig_ld_libraryn32_path_saved 0
24set orig_ld_library64_path_saved 0
25set orig_ld_library_path_32_saved 0
26set orig_ld_library_path_64_saved 0
27set orig_dyld_library_path_saved 0
28
29
30#######################################
31# proc set_ld_library_path_env_vars { }
32#######################################
33
34proc set_ld_library_path_env_vars { } {
35  global ld_library_path
36  global orig_environment_saved
37  global orig_ld_library_path_saved
38  global orig_ld_run_path_saved
39  global orig_shlib_path_saved
40  global orig_ld_libraryn32_path_saved
41  global orig_ld_library64_path_saved
42  global orig_ld_library_path_32_saved
43  global orig_ld_library_path_64_saved
44  global orig_dyld_library_path_saved
45  global orig_ld_library_path
46  global orig_ld_run_path
47  global orig_shlib_path
48  global orig_ld_libraryn32_path
49  global orig_ld_library64_path
50  global orig_ld_library_path_32
51  global orig_ld_library_path_64
52  global orig_dyld_library_path
53  global GCC_EXEC_PREFIX
54
55  # Set the relocated compiler prefix, but only if the user hasn't specified one.
56  if { [info exists GCC_EXEC_PREFIX] && ![info exists env(GCC_EXEC_PREFIX)] } {
57    setenv GCC_EXEC_PREFIX "$GCC_EXEC_PREFIX"
58  }
59
60  # Setting the ld library path causes trouble when testing cross-compilers.
61  if { [is_remote target] } {
62    return
63  }
64
65  if { $orig_environment_saved == 0 } {
66    global env
67
68    set orig_environment_saved 1
69
70    # Save the original environment.
71    if [info exists env(LD_LIBRARY_PATH)] {
72      set orig_ld_library_path "$env(LD_LIBRARY_PATH)"
73      set orig_ld_library_path_saved 1
74    }
75    if [info exists env(LD_RUN_PATH)] {
76      set orig_ld_run_path "$env(LD_RUN_PATH)"
77      set orig_ld_run_path_saved 1
78    }
79    if [info exists env(SHLIB_PATH)] {
80      set orig_shlib_path "$env(SHLIB_PATH)"
81      set orig_shlib_path_saved 1
82    }
83    if [info exists env(LD_LIBRARYN32_PATH)] {
84      set orig_ld_libraryn32_path "$env(LD_LIBRARYN32_PATH)"
85      set orig_ld_libraryn32_path_saved 1
86    }
87    if [info exists env(LD_LIBRARY64_PATH)] {
88      set orig_ld_library64_path "$env(LD_LIBRARY64_PATH)"
89      set orig_ld_library64_path_saved 1
90    }
91    if [info exists env(LD_LIBRARY_PATH_32)] {
92      set orig_ld_library_path_32 "$env(LD_LIBRARY_PATH_32)"
93      set orig_ld_library_path_32_saved 1
94    }
95    if [info exists env(LD_LIBRARY_PATH_64)] {
96      set orig_ld_library_path_64 "$env(LD_LIBRARY_PATH_64)"
97      set orig_ld_library_path_64_saved 1
98    }
99    if [info exists env(DYLD_LIBRARY_PATH)] {
100      set orig_dyld_library_path "$env(DYLD_LIBRARY_PATH)"
101      set orig_dyld_library_path_saved 1
102    }
103  }
104
105  # We need to set ld library path in the environment.  Currently,
106  # unix.exp doesn't set the environment correctly for all systems.
107  # It only sets SHLIB_PATH and LD_LIBRARY_PATH when it executes a
108  # program.  We also need the environment set for compilations, etc.
109  #
110  # On IRIX 6, we have to set variables akin to LD_LIBRARY_PATH, but
111  # called LD_LIBRARYN32_PATH (for the N32 ABI) and LD_LIBRARY64_PATH
112  # (for the 64-bit ABI).  The same applies to Darwin (DYLD_LIBRARY_PATH),
113  # Solaris 32 bit (LD_LIBRARY_PATH_32), Solaris 64 bit (LD_LIBRARY_PATH_64),
114  # and HP-UX (SHLIB_PATH).  In some cases, the variables are independent
115  # of LD_LIBRARY_PATH, and in other cases LD_LIBRARY_PATH is used if the
116  # variable is not defined.
117  #
118  # Doing this is somewhat of a hack as ld_library_path gets repeated in
119  # SHLIB_PATH and LD_LIBRARY_PATH when unix_load sets these variables.
120  if { $orig_ld_library_path_saved } {
121    setenv LD_LIBRARY_PATH "$ld_library_path:$orig_ld_library_path"
122  } else {
123    setenv LD_LIBRARY_PATH "$ld_library_path"
124  }
125  if { $orig_ld_run_path_saved } {
126    setenv LD_RUN_PATH "$ld_library_path:$orig_ld_run_path"
127  } else {
128    setenv LD_RUN_PATH "$ld_library_path"
129  }
130  # The default shared library dynamic path search for 64-bit
131  # HP-UX executables searches LD_LIBRARY_PATH before SHLIB_PATH.
132  # LD_LIBRARY_PATH isn't used for 32-bit executables.  Thus, we
133  # set LD_LIBRARY_PATH and SHLIB_PATH as if they were independent.
134  if { $orig_shlib_path_saved } {
135    setenv SHLIB_PATH "$ld_library_path:$orig_shlib_path"
136  } else {
137    setenv SHLIB_PATH "$ld_library_path"
138  }
139  if { $orig_ld_libraryn32_path_saved } {
140    setenv LD_LIBRARYN32_PATH "$ld_library_path:$orig_ld_libraryn32_path"
141  } elseif { $orig_ld_library_path_saved } {
142    setenv LD_LIBRARYN32_PATH "$ld_library_path:$orig_ld_library_path"
143  } else {
144    setenv LD_LIBRARYN32_PATH "$ld_library_path"
145  }
146  if { $orig_ld_library64_path_saved } {
147    setenv LD_LIBRARY64_PATH "$ld_library_path:$orig_ld_library64_path"
148  } elseif { $orig_ld_library_path_saved } {
149    setenv LD_LIBRARY64_PATH "$ld_library_path:$orig_ld_library_path"
150  } else {
151    setenv LD_LIBRARY64_PATH "$ld_library_path"
152  }
153  if { $orig_ld_library_path_32_saved } {
154    setenv LD_LIBRARY_PATH_32 "$ld_library_path:$orig_ld_library_path_32"
155  } elseif { $orig_ld_library_path_saved } {
156    setenv LD_LIBRARY_PATH_32 "$ld_library_path:$orig_ld_library_path"
157  } else {
158    setenv LD_LIBRARY_PATH_32 "$ld_library_path"
159  }
160  if { $orig_ld_library_path_64_saved } {
161    setenv LD_LIBRARY_PATH_64 "$ld_library_path:$orig_ld_library_path_64"
162  } elseif { $orig_ld_library_path_saved } {
163    setenv LD_LIBRARY_PATH_64 "$ld_library_path:$orig_ld_library_path"
164  } else {
165    setenv LD_LIBRARY_PATH_64 "$ld_library_path"
166  }
167  if { $orig_dyld_library_path_saved } {
168    setenv DYLD_LIBRARY_PATH "$ld_library_path:$orig_dyld_library_path"
169  } else {
170    setenv DYLD_LIBRARY_PATH "$ld_library_path"
171  }
172
173  verbose -log "set_ld_library_path_env_vars: ld_library_path=$ld_library_path"
174}
175
176#######################################
177# proc restore_ld_library_path_env_vars { }
178#######################################
179
180proc restore_ld_library_path_env_vars { } {
181  global orig_environment_saved
182  global orig_ld_library_path_saved
183  global orig_ld_run_path_saved
184  global orig_shlib_path_saved
185  global orig_ld_libraryn32_path_saved
186  global orig_ld_library64_path_saved
187  global orig_ld_library_path_32_saved
188  global orig_ld_library_path_64_saved
189  global orig_dyld_library_path_saved
190  global orig_ld_library_path
191  global orig_ld_run_path
192  global orig_shlib_path
193  global orig_ld_libraryn32_path
194  global orig_ld_library64_path
195  global orig_ld_library_path_32
196  global orig_ld_library_path_64
197  global orig_dyld_library_path
198
199  if { $orig_environment_saved == 0 } {
200    return
201  }
202
203  if { $orig_ld_library_path_saved } {
204    setenv LD_LIBRARY_PATH "$orig_ld_library_path"
205  } elseif [info exists env(LD_LIBRARY_PATH)] {
206    unsetenv LD_LIBRARY_PATH
207  }
208  if { $orig_ld_run_path_saved } {
209    setenv LD_RUN_PATH "$orig_ld_run_path"
210  } elseif [info exists env(LD_RUN_PATH)] {
211    unsetenv LD_RUN_PATH
212  }
213  if { $orig_shlib_path_saved } {
214    setenv SHLIB_PATH "$orig_shlib_path"
215  } elseif [info exists env(SHLIB_PATH)] {
216    unsetenv SHLIB_PATH
217  }
218  if { $orig_ld_libraryn32_path_saved } {
219    setenv LD_LIBRARYN32_PATH "$orig_ld_libraryn32_path"
220  } elseif [info exists env(LD_LIBRARYN32_PATH)] {
221    unsetenv LD_LIBRARYN32_PATH
222  }
223  if { $orig_ld_library64_path_saved } {
224    setenv LD_LIBRARY64_PATH "$orig_ld_library64_path"
225  } elseif [info exists env(LD_LIBRARY64_PATH)] {
226    unsetenv LD_LIBRARY64_PATH
227  }
228  if { $orig_ld_library_path_32_saved } {
229    setenv LD_LIBRARY_PATH_32 "$orig_ld_library_path_32"
230  } elseif [info exists env(LD_LIBRARY_PATH_32)] {
231    unsetenv LD_LIBRARY_PATH_32
232  }
233  if { $orig_ld_library_path_64_saved } {
234    setenv LD_LIBRARY_PATH_64 "$orig_ld_library_path_64"
235  } elseif [info exists env(LD_LIBRARY_PATH_64)] {
236    unsetenv LD_LIBRARY_PATH_64
237  }
238  if { $orig_dyld_library_path_saved } {
239    setenv DYLD_LIBRARY_PATH "$orig_dyld_library_path"
240  } elseif [info exists env(DYLD_LIBRARY_PATH)] {
241    unsetenv DYLD_LIBRARY_PATH
242  }
243}
244
245#######################################
246# proc get_shlib_extension { }
247#######################################
248
249proc get_shlib_extension { } {
250    global shlib_ext
251
252    if { [ istarget *-*-darwin* ] } {
253	set shlib_ext "dylib"
254    } elseif { [ istarget *-*-cygwin* ] || [ istarget *-*-mingw* ] } {
255	set shlib_ext "dll"
256    } elseif { [ istarget hppa*-*-hpux* ] } {
257	set shlib_ext "sl"
258    } else {
259	set shlib_ext "so"
260    }
261    return $shlib_ext
262}
263
264