1 /* Handle CLASSPATH, -classpath, and path searching.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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"
37 /* By default, colon separates directories in a path. */
38 #ifndef PATH_SEPARATOR
39 #define PATH_SEPARATOR ':'
43 #define DIR_SEPARATOR '/'
52 /* Possible flag values. */
56 /* We keep linked lists of directory names. A ``directory'' can be
57 either an ordinary directory or a .zip file. */
65 static void free_entry
PARAMS ((struct entry
**));
66 static void append_entry
PARAMS ((struct entry
**, struct entry
*));
67 static void add_entry
PARAMS ((struct entry
**, const char *, int));
68 static void add_path
PARAMS ((struct entry
**, const char *, int));
70 /* We support several different ways to set the class path.
72 built-in system directory (only libgcj.jar)
73 CLASSPATH environment variable
74 -classpath option overrides $CLASSPATH
75 -CLASSPATH option is a synonym for -classpath (for compatibility)
76 -bootclasspath overrides built-in
77 -extdirs sets the extensions directory path (overrides built-in)
78 -I prepends path to list
80 We implement this by keeping several path lists, and then simply
81 ignoring the ones which are not relevant. */
83 /* This holds all the -I directories. */
84 static struct entry
*include_dirs
;
86 /* This holds the CLASSPATH environment variable. */
87 static struct entry
*classpath_env
;
89 /* This holds the -classpath command-line option. */
90 static struct entry
*classpath_user
;
92 /* This holds the default directories. Some of these will have the
94 static struct entry
*sys_dirs
;
96 /* This holds the extensions path entries. */
97 static struct entry
*extensions
;
99 /* This is the sealed list. It is just a combination of other lists. */
100 static struct entry
*sealed
;
102 /* We keep track of the longest path we've seen. */
103 static int longest_path
= 0;
113 for (e
= *entp
; e
; e
= n
)
123 append_entry (entp
, ent
)
127 /* It doesn't matter if this is slow, since it is run only at
128 startup, and then infrequently. */
131 /* Find end of list. */
132 for (e
= *entp
; e
&& e
->next
; e
= e
->next
)
142 add_entry (entp
, filename
, is_system
)
144 const char *filename
;
150 n
= ALLOC (sizeof (struct entry
));
151 n
->flags
= is_system
? FLAG_SYSTEM
: 0;
154 len
= strlen (filename
);
155 if (len
> 4 && (strcmp (filename
+ len
- 4, ".zip") == 0
156 || strcmp (filename
+ len
- 4, ".jar") == 0))
158 n
->flags
|= FLAG_ZIP
;
159 /* If the user uses -classpath then he'll have to include
160 libgcj.jar in the value. We check for this in a simplistic
161 way. Symlinks will fool this test. This is only used for
162 -MM and -MMD, so it probably isn't terribly important. */
163 if (! strcmp (filename
, LIBGCJ_ZIP_FILE
))
164 n
->flags
|= FLAG_SYSTEM
;
167 /* Note that we add a trailing separator to `.zip' names as well.
168 This is a little hack that lets the searching code in jcf-io.c
169 work more easily. Eww. */
170 if (filename
[len
- 1] != '/' && filename
[len
- 1] != DIR_SEPARATOR
)
172 char *f2
= alloca (len
+ 2);
173 strcpy (f2
, filename
);
174 f2
[len
] = DIR_SEPARATOR
;
176 n
->name
= xstrdup (f2
);
180 n
->name
= xstrdup (filename
);
182 if (len
> longest_path
)
185 append_entry (entp
, n
);
189 add_path (entp
, cp
, is_system
)
194 const char *startp
, *endp
;
198 char *buf
= alloca (strlen (cp
) + 3);
202 if (! *endp
|| *endp
== PATH_SEPARATOR
)
207 buf
[1] = DIR_SEPARATOR
;
212 strncpy (buf
, startp
, endp
- startp
);
213 buf
[endp
- startp
] = '\0';
215 add_entry (entp
, buf
, is_system
);
227 static int init_done
= 0;
229 /* Initialize the path module. */
242 sep
[0] = DIR_SEPARATOR
;
245 GET_ENVIRONMENT (cp
, "GCC_EXEC_PREFIX");
248 try = alloca (strlen (cp
) + 50);
249 /* The exec prefix can be something like
250 /usr/local/bin/../lib/gcc-lib/. We want to change this
251 into a pointer to the share/java directory. We support two
252 configurations: one where prefix and exec-prefix are the
253 same, and one where exec-prefix is `prefix/SOMETHING'. */
255 strcat (try, DIR_UP
);
257 strcat (try, DIR_UP
);
261 strcpy (try + len
, "share");
263 strcat (try, "java");
265 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
266 if (! stat (try, &stat_b
))
268 add_entry (&sys_dirs
, try, 1);
270 strcpy (&try[strlen (try)
271 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
275 if (! stat (try, &stat_b
))
276 jcf_path_extdirs_arg (try);
280 strcpy (try + len
, DIR_UP
);
282 strcat (try, "share");
284 strcat (try, "java");
286 strcat (try, "libgcj-" DEFAULT_TARGET_VERSION
".jar");
287 if (! stat (try, &stat_b
))
289 add_entry (&sys_dirs
, try, 1);
291 strcpy (&try[strlen (try)
292 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
296 if (! stat (try, &stat_b
))
297 jcf_path_extdirs_arg (try);
303 /* Desperation: use the installed one. */
305 add_entry (&sys_dirs
, LIBGCJ_ZIP_FILE
, 1);
306 extdirs
= alloca (strlen (LIBGCJ_ZIP_FILE
) + 1);
307 strcpy (extdirs
, LIBGCJ_ZIP_FILE
);
308 strcpy (&extdirs
[strlen (LIBGCJ_ZIP_FILE
)
309 - strlen ("libgcj-" DEFAULT_TARGET_VERSION
".jar")],
311 strcat (extdirs
, sep
);
312 if (! stat (extdirs
, &stat_b
))
313 jcf_path_extdirs_arg (extdirs
);
316 GET_ENVIRONMENT (cp
, "CLASSPATH");
317 add_path (&classpath_env
, cp
, 0);
320 /* Call this when -classpath is seen on the command line.
321 This overrides only the $CLASSPATH environment variable.
324 jcf_path_classpath_arg (path
)
327 free_entry (&classpath_user
);
328 add_path (&classpath_user
, path
, 0);
331 /* Call this when -bootclasspath is seen on the command line.
334 jcf_path_bootclasspath_arg (path
)
337 free_entry (&sys_dirs
);
338 add_path (&sys_dirs
, path
, 1);
341 /* Call this when -extdirs is seen on the command line.
344 jcf_path_extdirs_arg (cp
)
347 const char *startp
, *endp
;
349 free_entry (&extensions
);
353 char *buf
= alloca (strlen (cp
) + 3);
357 if (! *endp
|| *endp
== PATH_SEPARATOR
)
362 strncpy (buf
, startp
, endp
- startp
);
363 buf
[endp
- startp
] = '\0';
367 int dirname_length
= strlen (buf
);
369 dirp
= opendir (buf
);
375 struct dirent
*direntp
= readdir (dirp
);
380 if (direntp
->d_name
[0] != '.')
382 char *name
= alloca (dirname_length
383 + strlen (direntp
->d_name
) + 2);
385 if (name
[dirname_length
-1] != DIR_SEPARATOR
)
387 name
[dirname_length
] = DIR_SEPARATOR
;
388 name
[dirname_length
+1] = 0;
390 strcat (name
, direntp
->d_name
);
391 add_entry (&extensions
, name
, 0);
407 /* Call this when -I is seen on the command line. */
409 jcf_path_include_arg (path
)
412 add_entry (&include_dirs
, path
, 0);
415 /* We `seal' the path by linking everything into one big list. Then
416 we provide a way to iterate through the sealed list. If PRINT is
417 true then we print the final class path to stderr. */
419 jcf_path_seal (print
)
422 struct entry
*secondary
;
424 sealed
= include_dirs
;
429 secondary
= classpath_user
;
430 classpath_user
= NULL
;
435 add_entry (&classpath_env
, ".", 0);
437 secondary
= classpath_env
;
438 classpath_env
= NULL
;
442 free_entry (&classpath_user
);
443 free_entry (&classpath_env
);
445 append_entry (&sealed
, secondary
);
446 append_entry (&sealed
, sys_dirs
);
447 append_entry (&sealed
, extensions
);
454 fprintf (stderr
, "Class path starts here:\n");
455 for (ent
= sealed
; ent
; ent
= ent
->next
)
457 fprintf (stderr
, " %s", ent
->name
);
458 if ((ent
->flags
& FLAG_SYSTEM
))
459 fprintf (stderr
, " (system)");
460 if ((ent
->flags
& FLAG_ZIP
))
461 fprintf (stderr
, " (zip)");
462 fprintf (stderr
, "\n");
470 return (void *) sealed
;
477 struct entry
*ent
= (struct entry
*) x
;
478 return (void *) ent
->next
;
481 /* We guarantee that the return path will either be a zip file, or it
482 will end with a directory separator. */
487 struct entry
*ent
= (struct entry
*) x
;
492 jcf_path_is_zipfile (x
)
495 struct entry
*ent
= (struct entry
*) x
;
496 return (ent
->flags
& FLAG_ZIP
);
500 jcf_path_is_system (x
)
503 struct entry
*ent
= (struct entry
*) x
;
504 return (ent
->flags
& FLAG_SYSTEM
);