1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
3 2007, 2008, 2010 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"
41 /* Possible flag values. */
45 /* We keep linked lists of directory names. A ``directory'' can be
46 either an ordinary directory or a .zip file. */
54 static void free_entry (struct entry **);
55 static void append_entry (struct entry **, struct entry *);
56 static void add_entry (struct entry **, const char *, int);
57 static void add_path (struct entry **, const char *, int);
59 /* We support several different ways to set the class path.
61 built-in system directory (only libgcj.jar)
62 CLASSPATH environment variable
63 -classpath option overrides $CLASSPATH
64 -CLASSPATH option is a synonym for -classpath (for compatibility)
65 -bootclasspath overrides built-in
66 -extdirs sets the extensions directory path (overrides built-in)
67 -I prepends path to list
69 We implement this by keeping several path lists, and then simply
70 ignoring the ones which are not relevant. */
72 /* This holds all the -I directories. */
73 static struct entry *include_dirs;
75 /* This holds the CLASSPATH environment variable. */
76 static struct entry *classpath_env;
78 /* This holds the -classpath command-line option. */
79 static struct entry *classpath_user;
81 /* This holds the default directories. Some of these will have the
83 static struct entry *sys_dirs;
85 /* This holds the extensions path entries. */
86 static struct entry *extensions;
88 /* This is the sealed list. It is just a combination of other lists. */
89 static struct entry *sealed;
91 /* We keep track of the longest path we've seen. */
92 static int longest_path = 0;
97 free_entry (struct entry **entp)
101 for (e = *entp; e; e = n)
111 append_entry (struct entry **entp, struct entry *ent)
113 /* It doesn't matter if this is slow, since it is run only at
114 startup, and then infrequently. */
117 /* Find end of list. */
118 for (e = *entp; e && e->next; e = e->next)
128 add_entry (struct entry **entp, const char *filename, int is_system)
133 n = XNEW (struct entry);
134 n->flags = is_system ? FLAG_SYSTEM : 0;
137 len = strlen (filename);
139 if (len > 4 && (FILENAME_CMP (filename + len - 4, ".zip") == 0
140 || FILENAME_CMP (filename + len - 4, ".jar") == 0))
142 n->flags |= FLAG_ZIP;
143 /* If the user uses -classpath then he'll have to include
144 libgcj.jar in the value. We check for this in a simplistic
145 way. Symlinks will fool this test. This is only used for
146 -MM and -MMD, so it probably isn't terribly important. */
147 if (! FILENAME_CMP (filename, LIBGCJ_ZIP_FILE))
148 n->flags |= FLAG_SYSTEM;
151 /* Note that we add a trailing separator to `.zip' names as well.
152 This is a little hack that lets the searching code in jcf-io.c
153 work more easily. Eww. */
154 if (! IS_DIR_SEPARATOR (filename[len - 1]))
156 char *f2 = (char *) alloca (len + 2);
157 strcpy (f2, filename);
158 f2[len] = DIR_SEPARATOR;
160 n->name = xstrdup (f2);
164 n->name = xstrdup (filename);
166 if (len > longest_path)
169 append_entry (entp, n);
173 add_path (struct entry **entp, const char *cp, int is_system)
175 const char *startp, *endp;
179 char *buf = (char *) alloca (strlen (cp) + 3);
183 if (! *endp || *endp == PATH_SEPARATOR)
188 buf[1] = DIR_SEPARATOR;
193 strncpy (buf, startp, endp - startp);
194 buf[endp - startp] = '\0';
196 add_entry (entp, buf, is_system);
208 static int init_done = 0;
210 /* Initialize the path module. */
215 char *attempt, sep[2];
223 sep[0] = DIR_SEPARATOR;
226 cp = getenv ("GCC_EXEC_PREFIX");
229 attempt = (char *) alloca (strlen (cp) + 50);
230 /* The exec prefix can be something like
231 /usr/local/bin/../lib/gcc-lib/. We want to change this
232 into a pointer to the share/java directory. We support two
233 configurations: one where prefix and exec-prefix are the
234 same, and one where exec-prefix is `prefix/SOMETHING'. */
235 strcpy (attempt, cp);
236 strcat (attempt, DIR_UP);
237 strcat (attempt, sep);
238 strcat (attempt, DIR_UP);
239 strcat (attempt, sep);
240 len = strlen (attempt);
242 strcpy (attempt + len, "share");
243 strcat (attempt, sep);
244 strcat (attempt, "java");
245 strcat (attempt, sep);
246 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
247 if (! stat (attempt, &stat_b))
249 add_entry (&sys_dirs, attempt, 1);
251 strcpy (&attempt[strlen (attempt)
252 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
254 strcat (attempt, "ext");
255 strcat (attempt, sep);
256 if (! stat (attempt, &stat_b))
257 jcf_path_extdirs_arg (attempt);
261 strcpy (attempt + len, DIR_UP);
262 strcat (attempt, sep);
263 strcat (attempt, "share");
264 strcat (attempt, sep);
265 strcat (attempt, "java");
266 strcat (attempt, sep);
267 strcat (attempt, "libgcj-" DEFAULT_TARGET_VERSION ".jar");
268 if (! stat (attempt, &stat_b))
270 add_entry (&sys_dirs, attempt, 1);
272 strcpy (&attempt[strlen (attempt)
273 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
275 strcat (attempt, "ext");
276 strcat (attempt, sep);
277 if (! stat (attempt, &stat_b))
278 jcf_path_extdirs_arg (attempt);
284 /* Desperation: use the installed one. */
286 add_entry (&sys_dirs, LIBGCJ_ZIP_FILE, 1);
287 extdirs = (char *) alloca (strlen (LIBGCJ_ZIP_FILE) + 1);
288 strcpy (extdirs, LIBGCJ_ZIP_FILE);
289 strcpy (&extdirs[strlen (LIBGCJ_ZIP_FILE)
290 - strlen ("libgcj-" DEFAULT_TARGET_VERSION ".jar")],
292 strcat (extdirs, sep);
293 if (! stat (extdirs, &stat_b))
294 jcf_path_extdirs_arg (extdirs);
297 cp = getenv ("CLASSPATH");
298 add_path (&classpath_env, cp, 0);
301 /* Call this when -classpath is seen on the command line.
302 This overrides only the $CLASSPATH environment variable.
305 jcf_path_classpath_arg (const char *path)
307 free_entry (&classpath_user);
308 add_path (&classpath_user, path, 0);
311 /* Call this when -bootclasspath is seen on the command line.
314 jcf_path_bootclasspath_arg (const char *path)
316 free_entry (&sys_dirs);
317 add_path (&sys_dirs, path, 1);
320 /* Call this when -extdirs is seen on the command line.
323 jcf_path_extdirs_arg (const char *cp)
325 const char *startp, *endp;
327 free_entry (&extensions);
331 char *buf = (char *) alloca (strlen (cp) + 3);
335 if (! *endp || *endp == PATH_SEPARATOR)
340 strncpy (buf, startp, endp - startp);
341 buf[endp - startp] = '\0';
345 int dirname_length = strlen (buf);
347 dirp = opendir (buf);
353 struct dirent *direntp = readdir (dirp);
358 if (direntp->d_name[0] != '.')
360 char *name = (char *) alloca (dirname_length
361 + strlen (direntp->d_name) + 2);
363 if (! IS_DIR_SEPARATOR (name[dirname_length-1]))
365 name[dirname_length] = DIR_SEPARATOR;
366 name[dirname_length+1] = 0;
368 strcat (name, direntp->d_name);
369 add_entry (&extensions, name, 0);
387 /* Call this when -I is seen on the command line. */
389 jcf_path_include_arg (const char *path)
391 add_entry (&include_dirs, path, 0);
394 /* We `seal' the path by linking everything into one big list. Then
395 we provide a way to iterate through the sealed list. If PRINT is
396 true then we print the final class path to stderr. */
398 jcf_path_seal (int print)
400 struct entry *secondary;
402 sealed = include_dirs;
407 secondary = classpath_user;
408 classpath_user = NULL;
413 add_entry (&classpath_env, ".", 0);
415 secondary = classpath_env;
416 classpath_env = NULL;
420 free_entry (&classpath_user);
421 free_entry (&classpath_env);
423 append_entry (&sealed, secondary);
424 append_entry (&sealed, sys_dirs);
425 append_entry (&sealed, extensions);
432 fprintf (stderr, "Class path starts here:\n");
433 for (ent = sealed; ent; ent = ent->next)
435 fprintf (stderr, " %s", ent->name);
436 if ((ent->flags & FLAG_SYSTEM))
437 fprintf (stderr, " (system)");
438 if ((ent->flags & FLAG_ZIP))
439 fprintf (stderr, " (zip)");
440 fprintf (stderr, "\n");
446 jcf_path_start (void)
448 return (void *) sealed;
452 jcf_path_next (void *x)
454 struct entry *ent = (struct entry *) x;
455 return (void *) ent->next;
459 PATH_SEPARATOR_STR[] = {PATH_SEPARATOR, '\0'};
462 jcf_path_compute (const char *prefix)
466 int length = strlen (prefix) + 1;
469 for (iter = sealed; iter != NULL; iter = iter->next)
470 length += strlen (iter->name) + 1;
472 result = (char *) xmalloc (length);
473 strcpy (result, prefix);
475 for (iter = sealed; iter != NULL; iter = iter->next)
478 strcat (result, PATH_SEPARATOR_STR);
480 strcat (result, iter->name);
481 /* Ugly: we want to strip the '/' from zip entries when
482 computing a string classpath. */
483 if ((iter->flags & FLAG_ZIP) != 0)
484 result[strlen (result) - 1] = '\0';
490 /* We guarantee that the return path will either be a zip file, or it
491 will end with a directory separator. */
493 jcf_path_name (void *x)
495 struct entry *ent = (struct entry *) x;
500 jcf_path_is_zipfile (void *x)
502 struct entry *ent = (struct entry *) x;
503 return (ent->flags & FLAG_ZIP);
507 jcf_path_is_system (void *x)
509 struct entry *ent = (struct entry *) x;
510 return (ent->flags & FLAG_SYSTEM);
514 jcf_path_max_len (void)