1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 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 2, 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 COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
30 #include "coretypes.h"
43 /* Possible flag values. */
47 /* We keep linked lists of directory names. A ``directory'' can be
48 either an ordinary directory or a .zip file. */
56 static void free_entry (struct entry **);
57 static void append_entry (struct entry **, struct entry *);
58 static void add_entry (struct entry **, const char *, int);
59 static void add_path (struct entry **, const char *, int);
61 /* We support several different ways to set the class path.
63 built-in system directory (only libgcj.jar)
64 CLASSPATH environment variable
65 -classpath option overrides $CLASSPATH
66 -CLASSPATH option is a synonym for -classpath (for compatibility)
67 -bootclasspath overrides built-in
68 -extdirs sets the extensions directory path (overrides built-in)
69 -I prepends path to list
71 We implement this by keeping several path lists, and then simply
72 ignoring the ones which are not relevant. */
74 /* This holds all the -I directories. */
75 static struct entry *include_dirs;
77 /* This holds the CLASSPATH environment variable. */
78 static struct entry *classpath_env;
80 /* This holds the -classpath command-line option. */
81 static struct entry *classpath_user;
83 /* This holds the default directories. Some of these will have the
85 static struct entry *sys_dirs;
87 /* This holds the extensions path entries. */
88 static struct entry *extensions;
90 /* This is the sealed list. It is just a combination of other lists. */
91 static struct entry *sealed;
93 /* We keep track of the longest path we've seen. */
94 static int longest_path = 0;
99 free_entry (struct entry **entp)
103 for (e = *entp; e; e = n)
113 append_entry (struct entry **entp, struct entry *ent)
115 /* It doesn't matter if this is slow, since it is run only at
116 startup, and then infrequently. */
119 /* Find end of list. */
120 for (e = *entp; e && e->next; e = e->next)
130 add_entry (struct entry **entp, const char *filename, int is_system)
135 n = XNEW (struct entry);
136 n->flags = is_system ? FLAG_SYSTEM : 0;
139 len = strlen (filename);
141 if (len > 4 && (FILENAME_CMP (filename + len - 4, ".zip") == 0
142 || FILENAME_CMP (filename + len - 4, ".jar") == 0))
144 n->flags |= FLAG_ZIP;
145 /* If the user uses -classpath then he'll have to include
146 libgcj.jar in the value. We check for this in a simplistic
147 way. Symlinks will fool this test. This is only used for
148 -MM and -MMD, so it probably isn't terribly important. */
149 if (! FILENAME_CMP (filename, LIBGCJ_ZIP_FILE))
150 n->flags |= FLAG_SYSTEM;
153 /* Note that we add a trailing separator to `.zip' names as well.
154 This is a little hack that lets the searching code in jcf-io.c
155 work more easily. Eww. */
156 if (! IS_DIR_SEPARATOR (filename[len - 1]))
158 char *f2 = alloca (len + 2);
159 strcpy (f2, filename);
160 f2[len] = DIR_SEPARATOR;
162 n->name = xstrdup (f2);
166 n->name = xstrdup (filename);
168 if (len > longest_path)
171 append_entry (entp, n);
175 add_path (struct entry **entp, const char *cp, int is_system)
177 const char *startp, *endp;
181 char *buf = alloca (strlen (cp) + 3);
185 if (! *endp || *endp == PATH_SEPARATOR)
190 buf[1] = DIR_SEPARATOR;
195 strncpy (buf, startp, endp - startp);
196 buf[endp - startp] = '\0';
198 add_entry (entp, buf, is_system);
210 static int init_done = 0;
212 /* Initialize the path module. */
225 sep[0] = DIR_SEPARATOR;
228 GET_ENVIRONMENT (cp, "GCC_EXEC_PREFIX");
231 try = alloca (strlen (cp) + 50);
232 /* The exec prefix can be something like
233 /usr/local/bin/../lib/gcc-lib/. We want to change this
234 into a pointer to the share/java directory. We support two
235 configurations: one where prefix and exec-prefix are the
236 same, and one where exec-prefix is `prefix/SOMETHING'. */
238 strcat (try, DIR_UP);
240 strcat (try, DIR_UP);
244 strcpy (try + len, "share");
246 strcat (try, "java");
248 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
249 if (! stat (try, &stat_b))
251 add_entry (&sys_dirs, try, 1);
253 strcpy (&try[strlen (try)
254 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
258 if (! stat (try, &stat_b))
259 jcf_path_extdirs_arg (try);
263 strcpy (try + len, DIR_UP);
265 strcat (try, "share");
267 strcat (try, "java");
269 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
270 if (! stat (try, &stat_b))
272 add_entry (&sys_dirs, try, 1);
274 strcpy (&try[strlen (try)
275 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
279 if (! stat (try, &stat_b))
280 jcf_path_extdirs_arg (try);
286 /* Desperation: use the installed one. */
288 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
289 extdirs = alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
290 strcpy (extdirs, LIBGCJ_ZIP_FILE);
291 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
292 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
294 strcat (extdirs, sep);
295 if (! stat (extdirs, &stat_b))
296 jcf_path_extdirs_arg (extdirs);
299 GET_ENVIRONMENT (cp, "CLASSPATH");
300 add_path (&classpath_env, cp, 0);
303 /* Call this when -classpath is seen on the command line.
304 This overrides only the $CLASSPATH environment variable.
307 jcf_path_classpath_arg (const char *path)
309 free_entry (&classpath_user);
310 add_path (&classpath_user, path, 0);
313 /* Call this when -bootclasspath is seen on the command line.
316 jcf_path_bootclasspath_arg (const char *path)
318 free_entry (&sys_dirs);
319 add_path (&sys_dirs, path, 1);
322 /* Call this when -extdirs is seen on the command line.
325 jcf_path_extdirs_arg (const char *cp)
327 const char *startp, *endp;
329 free_entry (&extensions);
333 char *buf = alloca (strlen (cp) + 3);
337 if (! *endp || *endp == PATH_SEPARATOR)
342 strncpy (buf, startp, endp - startp);
343 buf[endp - startp] = '\0';
347 int dirname_length = strlen (buf);
349 dirp = opendir (buf);
355 struct dirent *direntp = readdir (dirp);
360 if (direntp->d_name[0] != '.')
362 char *name = alloca (dirname_length
363 + strlen (direntp->d_name) + 2);
365 if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
367 name[dirname_length] = DIR_SEPARATOR;
368 name[dirname_length+1] = 0;
370 strcat (name, direntp->d_name);
371 add_entry (&extensions, name, 0);
389 /* Call this when -I is seen on the command line. */
391 jcf_path_include_arg (const char *path)
393 add_entry (&include_dirs, path, 0);
396 /* We `seal' the path by linking everything into one big list. Then
397 we provide a way to iterate through the sealed list. If PRINT is
398 true then we print the final class path to stderr. */
400 jcf_path_seal (int print)
402 struct entry *secondary;
404 sealed = include_dirs;
409 secondary = classpath_user;
410 classpath_user = NULL;
415 add_entry (&classpath_env, ".", 0);
417 secondary = classpath_env;
418 classpath_env = NULL;
422 free_entry (&classpath_user);
423 free_entry (&classpath_env);
425 append_entry (&sealed, secondary);
426 append_entry (&sealed, sys_dirs);
427 append_entry (&sealed, extensions);
434 fprintf (stderr, "Class path starts here:\n");
435 for (ent = sealed; ent; ent = ent->next)
437 fprintf (stderr, " %s", ent->name);
438 if ((ent->flags & FLAG_SYSTEM))
439 fprintf (stderr, " (system)");
440 if ((ent->flags & FLAG_ZIP))
441 fprintf (stderr, " (zip)");
442 fprintf (stderr, "\n");
448 jcf_path_start (void)
450 return (void *) sealed;
454 jcf_path_next (void *x)
456 struct entry *ent = (struct entry *) x;
457 return (void *) ent->next;
460 /* We guarantee that the return path will either be a zip file, or it
461 will end with a directory separator. */
463 jcf_path_name (void *x)
465 struct entry *ent = (struct entry *) x;
470 jcf_path_is_zipfile (void *x)
472 struct entry *ent = (struct entry *) x;
473 return (ent->flags & FLAG_ZIP);
477 jcf_path_is_system (void *x)
479 struct entry *ent = (struct entry *) x;
480 return (ent->flags & FLAG_SYSTEM);
484 jcf_path_max_len (void)