1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
3 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
29 #include "coretypes.h"
42 /* Possible flag values. */
46 /* We keep linked lists of directory names. A ``directory'' can be
47 either an ordinary directory or a .zip file. */
55 static void free_entry (struct entry
**);
56 static void append_entry (struct entry
**, struct entry
*);
57 static void add_entry (struct entry
**, const char *, int);
58 static void add_path (struct entry
**, const char *, int);
60 /* We support several different ways to set the class path.
62 built-in system directory (only libgcj.jar)
63 CLASSPATH environment variable
64 -classpath option overrides $CLASSPATH
65 -CLASSPATH option is a synonym for -classpath (for compatibility)
66 -bootclasspath overrides built-in
67 -extdirs sets the extensions directory path (overrides built-in)
68 -I prepends path to list
70 We implement this by keeping several path lists, and then simply
71 ignoring the ones which are not relevant. */
73 /* This holds all the -I directories. */
74 static struct entry
*include_dirs
;
76 /* This holds the CLASSPATH environment variable. */
77 static struct entry
*classpath_env
;
79 /* This holds the -classpath command-line option. */
80 static struct entry
*classpath_user
;
82 /* This holds the default directories. Some of these will have the
84 static struct entry
*sys_dirs
;
86 /* This holds the extensions path entries. */
87 static struct entry
*extensions
;
89 /* This is the sealed list. It is just a combination of other lists. */
90 static struct entry
*sealed
;
92 /* We keep track of the longest path we've seen. */
93 static int longest_path
= 0;
98 free_entry (struct entry
**entp
)
102 for (e
= *entp
; e
; e
= n
)
112 append_entry (struct entry
**entp
, struct entry
*ent
)
114 /* It doesn't matter if this is slow, since it is run only at
115 startup, and then infrequently. */
118 /* Find end of list. */
119 for (e
= *entp
; e
&& e
->next
; e
= e
->next
)
129 add_entry (struct entry
**entp
, const char *filename
, int is_system
)
134 n
= XNEW (struct entry
);
135 n
->flags
= is_system
? FLAG_SYSTEM
: 0;
138 len
= strlen (filename
);
140 if (len
> 4 && (FILENAME_CMP (filename
+ len
- 4, ".zip") == 0
141 || FILENAME_CMP (filename
+ len
- 4, ".jar") == 0))
143 n
->flags
|= FLAG_ZIP
;
144 /* If the user uses -classpath then he'll have to include
145 libgcj.jar in the value. We check for this in a simplistic
146 way. Symlinks will fool this test. This is only used for
147 -MM and -MMD, so it probably isn't terribly important. */
148 if (! FILENAME_CMP (filename
, LIBGCJ_ZIP_FILE
))
149 n
->flags
|= FLAG_SYSTEM
;
152 /* Note that we add a trailing separator to `.zip' names as well.
153 This is a little hack that lets the searching code in jcf-io.c
154 work more easily. Eww. */
155 if (! IS_DIR_SEPARATOR (filename
[len
- 1]))
157 char *f2
= (char *) alloca (len
+ 2);
158 strcpy (f2
, filename
);
159 f2
[len
] = DIR_SEPARATOR
;
161 n
->name
= xstrdup (f2
);
165 n
->name
= xstrdup (filename
);
167 if (len
> longest_path
)
170 append_entry (entp
, n
);
174 add_path (struct entry
**entp
, const char *cp
, int is_system
)
176 const char *startp
, *endp
;
180 char *buf
= (char *) alloca (strlen (cp
) + 3);
184 if (! *endp
|| *endp
== PATH_SEPARATOR
)
189 buf
[1] = DIR_SEPARATOR
;
194 strncpy (buf
, startp
, endp
- startp
);
195 buf
[endp
- startp
] = '\0';
197 add_entry (entp
, buf
, is_system
);
209 static int init_done
= 0;
211 /* Initialize the path module. */
216 char *attempt
, sep
[2];
224 sep
[0] = DIR_SEPARATOR
;
227 GET_ENVIRONMENT (cp
, "GCC_EXEC_PREFIX");
230 attempt
= (char *) alloca (strlen (cp
) + 50);
231 /* The exec prefix can be something like
232 /usr/local/bin/../lib/gcc-lib/. We want to change this
233 into a pointer to the share/java directory. We support two
234 configurations: one where prefix and exec-prefix are the
235 same, and one where exec-prefix is `prefix/SOMETHING'. */
236 strcpy (attempt
, cp
);
237 strcat (attempt
, DIR_UP
);
238 strcat (attempt
, sep
);
239 strcat (attempt
, DIR_UP
);
240 strcat (attempt
, sep
);
241 len
= strlen (attempt
);
243 strcpy (attempt
+ len
, "share");
244 strcat (attempt
, sep
);
245 strcat (attempt
, "java");
246 strcat (attempt
, sep
);
247 strcat (attempt
, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
248 if (! stat (attempt
, &stat_b
))
250 add_entry (&sys_dirs
, attempt
, 1);
252 strcpy (&attempt
[strlen (attempt
)
253 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
255 strcat (attempt
, "ext");
256 strcat (attempt
, sep
);
257 if (! stat (attempt
, &stat_b
))
258 jcf_path_extdirs_arg (attempt
);
262 strcpy (attempt
+ len
, DIR_UP
);
263 strcat (attempt
, sep
);
264 strcat (attempt
, "share");
265 strcat (attempt
, sep
);
266 strcat (attempt
, "java");
267 strcat (attempt
, sep
);
268 strcat (attempt
, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
269 if (! stat (attempt
, &stat_b
))
271 add_entry (&sys_dirs
, attempt
, 1);
273 strcpy (&attempt
[strlen (attempt
)
274 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
276 strcat (attempt
, "ext");
277 strcat (attempt
, sep
);
278 if (! stat (attempt
, &stat_b
))
279 jcf_path_extdirs_arg (attempt
);
285 /* Desperation: use the installed one. */
287 add_entry (&sys_dirs
, LIBGCJ_ZIP_FILE
, 1);
288 extdirs
= (char *) alloca (strlen (LIBGCJ_ZIP_FILE
) + 1);
289 strcpy (extdirs
, LIBGCJ_ZIP_FILE
);
290 strcpy (&extdirs
[strlen (LIBGCJ_ZIP_FILE
)
291 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
293 strcat (extdirs
, sep
);
294 if (! stat (extdirs
, &stat_b
))
295 jcf_path_extdirs_arg (extdirs
);
298 GET_ENVIRONMENT (cp
, "CLASSPATH");
299 add_path (&classpath_env
, cp
, 0);
302 /* Call this when -classpath is seen on the command line.
303 This overrides only the $CLASSPATH environment variable.
306 jcf_path_classpath_arg (const char *path
)
308 free_entry (&classpath_user
);
309 add_path (&classpath_user
, path
, 0);
312 /* Call this when -bootclasspath is seen on the command line.
315 jcf_path_bootclasspath_arg (const char *path
)
317 free_entry (&sys_dirs
);
318 add_path (&sys_dirs
, path
, 1);
321 /* Call this when -extdirs is seen on the command line.
324 jcf_path_extdirs_arg (const char *cp
)
326 const char *startp
, *endp
;
328 free_entry (&extensions
);
332 char *buf
= (char *) alloca (strlen (cp
) + 3);
336 if (! *endp
|| *endp
== PATH_SEPARATOR
)
341 strncpy (buf
, startp
, endp
- startp
);
342 buf
[endp
- startp
] = '\0';
346 int dirname_length
= strlen (buf
);
348 dirp
= opendir (buf
);
354 struct dirent
*direntp
= readdir (dirp
);
359 if (direntp
->d_name
[0] != '.')
361 char *name
= (char *) alloca (dirname_length
362 + strlen (direntp
->d_name
) + 2);
364 if (! IS_DIR_SEPARATOR (name
[dirname_length
-1]))
366 name
[dirname_length
] = DIR_SEPARATOR
;
367 name
[dirname_length
+1] = 0;
369 strcat (name
, direntp
->d_name
);
370 add_entry (&extensions
, name
, 0);
388 /* Call this when -I is seen on the command line. */
390 jcf_path_include_arg (const char *path
)
392 add_entry (&include_dirs
, path
, 0);
395 /* We `seal' the path by linking everything into one big list. Then
396 we provide a way to iterate through the sealed list. If PRINT is
397 true then we print the final class path to stderr. */
399 jcf_path_seal (int print
)
401 struct entry
*secondary
;
403 sealed
= include_dirs
;
408 secondary
= classpath_user
;
409 classpath_user
= NULL
;
414 add_entry (&classpath_env
, ".", 0);
416 secondary
= classpath_env
;
417 classpath_env
= NULL
;
421 free_entry (&classpath_user
);
422 free_entry (&classpath_env
);
424 append_entry (&sealed
, secondary
);
425 append_entry (&sealed
, sys_dirs
);
426 append_entry (&sealed
, extensions
);
433 fprintf (stderr
, "Class path starts here:\n");
434 for (ent
= sealed
; ent
; ent
= ent
->next
)
436 fprintf (stderr
, " %s", ent
->name
);
437 if ((ent
->flags
& FLAG_SYSTEM
))
438 fprintf (stderr
, " (system)");
439 if ((ent
->flags
& FLAG_ZIP
))
440 fprintf (stderr
, " (zip)");
441 fprintf (stderr
, "\n");
447 jcf_path_start (void)
449 return (void *) sealed
;
453 jcf_path_next (void *x
)
455 struct entry
*ent
= (struct entry
*) x
;
456 return (void *) ent
->next
;
460 PATH_SEPARATOR_STR
[] = {PATH_SEPARATOR
, '\0'};
463 jcf_path_compute (const char *prefix
)
467 int length
= strlen (prefix
) + 1;
470 for (iter
= sealed
; iter
!= NULL
; iter
= iter
->next
)
471 length
+= strlen (iter
->name
) + 1;
473 result
= (char *) xmalloc (length
);
474 strcpy (result
, prefix
);
476 for (iter
= sealed
; iter
!= NULL
; iter
= iter
->next
)
479 strcat (result
, PATH_SEPARATOR_STR
);
481 strcat (result
, iter
->name
);
482 /* Ugly: we want to strip the '/' from zip entries when
483 computing a string classpath. */
484 if ((iter
->flags
& FLAG_ZIP
) != 0)
485 result
[strlen (result
) - 1] = '\0';
491 /* We guarantee that the return path will either be a zip file, or it
492 will end with a directory separator. */
494 jcf_path_name (void *x
)
496 struct entry
*ent
= (struct entry
*) x
;
501 jcf_path_is_zipfile (void *x
)
503 struct entry
*ent
= (struct entry
*) x
;
504 return (ent
->flags
& FLAG_ZIP
);
508 jcf_path_is_system (void *x
)
510 struct entry
*ent
= (struct entry
*) x
;
511 return (ent
->flags
& FLAG_SYSTEM
);
515 jcf_path_max_len (void)