Remove dead code.
[freeradius.git] / scripts / jlibtool.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #if !defined(__MINGW32__)
23 #include <sys/wait.h>
24 #endif
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <assert.h>
29
30 #ifdef __EMX__
31 #  define SHELL_CMD  "sh"
32 #  define GEN_EXPORTS "emxexp"
33 #  define DEF2IMPLIB_CMD "emximp"
34 #  define SHARE_SW   "-Zdll -Zmtd"
35 #  define USE_OMF 1
36 #  define TRUNCATE_DLL_NAME
37 #  define DYNAMIC_LIB_EXT "dll"
38 #  define EXE_EXT ".exe"
39
40 #  if USE_OMF
41      /* OMF is the native format under OS/2 */
42 #    define STATIC_LIB_EXT "lib"
43 #    define OBJECT_EXT     "obj"
44 #    define LIBRARIAN      "emxomfar"
45 #    define LIBRARIAN_OPTS "cr"
46 #  else
47      /* but the alternative, a.out, can fork() which is sometimes necessary */
48 #    define STATIC_LIB_EXT "a"
49 #    define OBJECT_EXT     "o"
50 #    define LIBRARIAN      "ar"
51 #    define LIBRARIAN_OPTS "cr"
52 #  endif
53 #endif
54
55 #if defined(__APPLE__)
56 #  define SHELL_CMD  "/bin/sh"
57 #  define DYNAMIC_LIB_EXT "dylib"
58 #  define MODULE_LIB_EXT  "bundle"
59 #  define STATIC_LIB_EXT "a"
60 #  define OBJECT_EXT     "o"
61 #  define LIBRARIAN      "ar"
62 #  define LIBRARIAN_OPTS "cr"
63 /* man libtool(1) documents ranlib option of -c.  */
64 #  define RANLIB "ranlib"
65 #  define PIC_FLAG "-fPIC -fno-common"
66 #  define SHARED_OPTS "-dynamiclib"
67 #  define MODULE_OPTS "-bundle -dynamic"
68 #  define DYNAMIC_LINK_OPTS "-flat_namespace"
69 #  define DYNAMIC_LINK_UNDEFINED "-undefined suppress"
70 #  define dynamic_link_version_func darwin_dynamic_link_function
71 #  define DYNAMIC_INSTALL_NAME "-install_name"
72 #  define DYNAMIC_LINK_NO_INSTALL "-dylib_file"
73 #  define HAS_REALPATH
74 /*-install_name  /Users/jerenk/apache-2.0-cvs/lib/libapr.0.dylib -compatibility_version 1 -current_version 1.0 */
75 #  define LD_LIBRARY_PATH "DYLD_LIBRARY_PATH"
76 #  define LD_LIBRARY_PATH_LOCAL "DYLD_FALLBACK_LIBRARY_PATH"
77 #endif
78
79 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
80 #  define SHELL_CMD  "/bin/sh"
81 #  define DYNAMIC_LIB_EXT "so"
82 #  define MODULE_LIB_EXT  "so"
83 #  define STATIC_LIB_EXT "a"
84 #  define OBJECT_EXT     "o"
85 #  define LIBRARIAN      "ar"
86 #  define LIBRARIAN_OPTS "cr"
87 #  define RANLIB "ranlib"
88 #  define PIC_FLAG "-fPIC"
89 #  define RPATH "-rpath"
90 #  define SHARED_OPTS "-shared"
91 #  define MODULE_OPTS "-shared"
92 #  define DYNAMIC_LINK_OPTS "-export-dynamic"
93 #  define LINKER_FLAG_PREFIX "-Wl,"
94 #  define ADD_MINUS_L
95 #  define LD_RUN_PATH "LD_RUN_PATH"
96 #  define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
97 #  define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
98 #endif
99
100 #if defined(sun)
101 #  define SHELL_CMD  "/bin/sh"
102 #  define DYNAMIC_LIB_EXT "so"
103 #  define MODULE_LIB_EXT  "so"
104 #  define STATIC_LIB_EXT "a"
105 #  define OBJECT_EXT     "o"
106 #  define LIBRARIAN      "ar"
107 #  define LIBRARIAN_OPTS "cr"
108 #  define RANLIB "ranlib"
109 #  define PIC_FLAG "-KPIC"
110 #  define RPATH "-R"
111 #  define SHARED_OPTS "-G"
112 #  define MODULE_OPTS "-G"
113 #  define DYNAMIC_LINK_OPTS ""
114 #  define LINKER_FLAG_NO_EQUALS
115 #  define ADD_MINUS_L
116 #  define HAS_REALPATH
117 #  define LD_RUN_PATH "LD_RUN_PATH"
118 #  define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
119 #  define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
120 #endif
121
122 #if defined(_OSD_POSIX)
123 #  define SHELL_CMD  "/usr/bin/sh"
124 #  define DYNAMIC_LIB_EXT "so"
125 #  define MODULE_LIB_EXT  "so"
126 #  define STATIC_LIB_EXT "a"
127 #  define OBJECT_EXT     "o"
128 #  define LIBRARIAN      "ar"
129 #  define LIBRARIAN_OPTS "cr"
130 #  define SHARED_OPTS "-G"
131 #  define MODULE_OPTS "-G"
132 #  define LINKER_FLAG_PREFIX "-Wl,"
133 #  define NEED_SNPRINTF
134 #endif
135
136 #if defined(sinix) && defined(mips) && defined(__SNI_TARG_UNIX)
137 #  define SHELL_CMD  "/usr/bin/sh"
138 #  define DYNAMIC_LIB_EXT "so"
139 #  define MODULE_LIB_EXT  "so"
140 #  define STATIC_LIB_EXT "a"
141 #  define OBJECT_EXT     "o"
142 #  define LIBRARIAN      "ar"
143 #  define LIBRARIAN_OPTS "cr"
144 #  define RPATH "-Brpath"
145 #  define SHARED_OPTS "-G"
146 #  define MODULE_OPTS "-G"
147 #  define DYNAMIC_LINK_OPTS "-Wl,-Blargedynsym"
148 #  define LINKER_FLAG_PREFIX "-Wl,"
149 #  define NEED_SNPRINTF
150 #  define LD_RUN_PATH "LD_RUN_PATH"
151 #  define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
152 #  define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
153 #endif
154
155 #if defined(__MINGW32__)
156 #  define SHELL_CMD "sh"
157 #  define DYNAMIC_LIB_EXT "dll"
158 #  define MODULE_LIB_EXT  "dll"
159 #  define STATIC_LIB_EXT "a"
160 #  define OBJECT_EXT     "o"
161 #  define LIBRARIAN      "ar"
162 #  define LIBRARIAN_OPTS "cr"
163 #  define RANLIB "ranlib"
164 #  define LINKER_FLAG_PREFIX "-Wl,"
165 #  define SHARED_OPTS "-shared"
166 #  define MODULE_OPTS "-shared"
167 #  define MKDIR_NO_UMASK
168 #  define EXE_EXT ".exe"
169 #endif
170
171 #ifndef CC
172 #define CC "gcc"
173 #endif
174
175 #ifndef CXX
176 #define CXX "g++"
177 #endif
178
179 #ifndef LINK_c
180 #define LINK_c "gcc"
181 #endif
182
183 #ifndef LINK_cxx
184 #define LINK_cxx "g++"
185 #endif
186
187 #ifndef LIBDIR
188 #define LIBDIR "/usr/local/lib"
189 #endif
190
191 #define OBJDIR ".libs"
192
193 #ifndef SHELL_CMD
194 #error Unsupported platform: Please add defines for SHELL_CMD etc. for your platform.
195 #endif
196
197 #ifdef NEED_SNPRINTF
198 #include <stdarg.h>
199 #endif
200
201 #ifdef __EMX__
202 #include <process.h>
203 #endif
204
205 #ifndef PATH_MAX
206 #define PATH_MAX 1024
207 #endif
208
209
210 /* We want to say we are libtool 1.4 for shlibtool compatibility. */
211 #define VERSION "1.4"
212
213 enum tool_mode_t {
214     mUnknown,
215     mCompile,
216     mLink,
217     mExecute,
218     mInstall,
219 };
220
221 enum output_t {
222     otGeneral,
223     otObject,
224     otProgram,
225     otLibrary,
226     otStaticLibraryOnly,
227     otDynamicLibraryOnly,
228     otModule,
229 };
230
231 enum pic_mode_e {
232     pic_UNKNOWN,
233     pic_PREFER,
234     pic_AVOID,
235 };
236
237 enum shared_mode_e {
238     share_UNSET,
239     share_STATIC,
240     share_SHARED,
241 };
242
243 enum lib_type {
244     type_UNKNOWN,
245     type_STATIC_LIB,
246     type_DYNAMIC_LIB,
247     type_MODULE_LIB,
248     type_OBJECT,
249 };
250
251 typedef struct {
252     const char **vals;
253     int num;
254 } count_chars;
255
256 typedef struct {
257     const char *normal;
258     const char *install;
259 } library_name;
260
261 typedef struct {
262     count_chars *normal;
263     count_chars *install;
264     count_chars *dependencies;
265 } library_opts;
266
267 typedef struct {
268     int silent;
269     int debug;
270     enum shared_mode_e shared;
271     int export_all;
272     int dry_run;
273     enum pic_mode_e pic_mode;
274     int export_dynamic;
275     int no_install;
276 } options_t;
277
278 typedef struct {
279     enum tool_mode_t mode;
280     enum output_t output;
281     options_t options;
282
283     char *output_name;
284     char *fake_output_name;
285     char *basename;
286
287     const char *install_path;
288     const char *compiler;
289     const char *program;
290     count_chars *program_opts;
291
292     count_chars *arglist;
293     count_chars *tmp_dirs;
294     count_chars *obj_files;
295     count_chars *dep_rpaths;
296     count_chars *rpaths;
297
298     library_name static_name;
299     library_name shared_name;
300     library_name module_name;
301
302     library_opts static_opts;
303     library_opts shared_opts;
304
305     const char *version_info;
306     const char *undefined_flag;
307 } command_t;
308
309 #ifdef RPATH
310 static void add_rpath(count_chars *cc, const char *path);
311 #endif
312
313 static void usage(int code)
314 {
315         printf("Usage: jlibtool [OPTIONS...] COMMANDS...\n");
316         printf("jlibtool is a replacement for GNU libtool with similar functionality.\n\n");
317
318         printf("    --config          show all configuration variables\n");
319         printf("    --debug           enable verbose shell tracing\n");
320         printf("    --dry-run         display commands without modifying any files\n");
321         printf("    --help            display this help message and exit\n");
322         printf("    --mode=MODE       use operational mode MODE (you *must* set mode)\n");
323
324         printf("    --silent          don't print informational messages\n");
325         printf("    --tag=TAG         Ignored for libtool compatibility\n");
326         printf("    --version         print version information\n");
327
328
329         printf("    --shared          Build shared libraries when using --mode=link\n");
330         printf("    --export-all      Try to export 'def' file on some platforms\n");
331
332         printf("\nMODE must be one of the following:\n\n");
333         printf("      compile         compile a source file into a jlibtool object\n");
334         printf("      execute         automatically set library path, then run a program\n");
335         printf("      install         install libraries or executables\n");
336         printf("      link            create a library or an executable\n");
337
338         printf("\nMODE-ARGS can be the following:\n\n");
339         printf("      -export-dynamic  accepted and ignored\n");
340         printf("      -module          create a module when linking\n");
341         printf("      -shared          create a shared library when linking\n");
342         printf("      -prefer-pic      prefer position-independent-code when compiling\n");
343         printf("      -prefer-non-pic  prefer non position-independent-code when compiling\n");
344         printf("      -static          create a static library when linking\n");
345         printf("      -no-install      link libraries locally\n");
346         printf("      -rpath arg       Set install path for shared libraries\n");
347         printf("      -l arg           pass '-l arg' to the link stage\n");
348         printf("      -L arg           pass '-L arg' to the link stage\n");
349         printf("      -R dir           add 'dir' to runtime library search path.\n");
350         printf("      -Zexe            accepted and ignored\n");
351         printf("      -avoid-version   accepted and ignored\n");
352
353         exit(code);
354 }
355
356 #if defined(NEED_SNPRINTF)
357 /* Write at most n characters to the buffer in str, return the
358  * number of chars written or -1 if the buffer would have been
359  * overflowed.
360  *
361  * This is portable to any POSIX-compliant system has /dev/null
362  */
363 static FILE *f=NULL;
364 static int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
365 {
366        int res;
367
368        if (f == NULL)
369                f = fopen("/dev/null","w");
370        if (f == NULL)
371                return -1;
372
373        setvbuf( f, str, _IOFBF, n );
374
375        res = vfprintf( f, fmt, ap );
376
377        if ( res > 0 && res < n ) {
378                res = vsprintf( str, fmt, ap );
379        }
380        return res;
381 }
382 static int snprintf( char *str, size_t n, const char *fmt, ... )
383 {
384         va_list ap;
385         int res;
386
387         va_start( ap, fmt );
388         res = vsnprintf( str, n, fmt, ap );
389         va_end( ap );
390         return res;
391 }
392 #endif
393
394 static void init_count_chars(count_chars *cc)
395 {
396     cc->vals = (const char**)malloc(PATH_MAX*sizeof(char*));
397     cc->num = 0;
398 }
399
400 static void clear_count_chars(count_chars *cc)
401 {
402     int i;
403     for (i = 0; i < cc->num; i++) {
404         cc->vals[i] = 0;
405     }
406
407     cc->num = 0;
408 }
409
410 static void push_count_chars(count_chars *cc, const char *newval)
411 {
412     cc->vals[cc->num++] = newval;
413 }
414
415 static void pop_count_chars(count_chars *cc)
416 {
417     cc->num--;
418 }
419
420 static void insert_count_chars(count_chars *cc, const char *newval, int position)
421 {
422     int i;
423
424     for (i = cc->num; i > position; i--) {
425         cc->vals[i] = cc->vals[i-1];
426     }
427
428     cc->vals[position] = newval;
429     cc->num++;
430 }
431
432 static void append_count_chars(count_chars *cc, count_chars *cctoadd)
433 {
434     int i;
435     for (i = 0; i < cctoadd->num; i++) {
436         if (cctoadd->vals[i]) {
437             push_count_chars(cc, cctoadd->vals[i]);
438         }
439     }
440 }
441
442 static const char *flatten_count_chars(count_chars *cc, int space)
443 {
444     int i, size;
445     char *newval;
446
447     size = 0;
448     for (i = 0; i < cc->num; i++) {
449         if (cc->vals[i]) {
450             size += strlen(cc->vals[i]) + 1;
451             if (space) {
452               size++;
453             }
454         }
455     }
456
457     newval = (char*)malloc(size + 1);
458     newval[0] = 0;
459
460     for (i = 0; i < cc->num; i++) {
461         if (cc->vals[i]) {
462             strcat(newval, cc->vals[i]);
463             if (space) {
464                 strcat(newval, " ");
465             }
466         }
467     }
468
469     return newval;
470 }
471
472 static char *shell_esc(const char *str)
473 {
474     int in_quote = 0;
475     char *cmd;
476     unsigned char *d;
477     const unsigned char *s;
478
479     cmd = (char *)malloc(2 * strlen(str) + 3);
480     d = (unsigned char *)cmd;
481     s = (const unsigned char *)str;
482
483 #ifdef __MINGW32__
484     *d++ = '\"';
485 #endif
486
487     for (; *s; ++s) {
488         if (*s == '"') {
489             *d++ = '\\';
490             in_quote++;
491         }
492         else if (*s == '\\' || (*s == ' ' && (in_quote % 2))) {
493             *d++ = '\\';
494         }
495         *d++ = *s;
496     }
497
498 #ifdef __MINGW32__
499     *d++ = '\"';
500 #endif
501
502     *d = '\0';
503     return cmd;
504 }
505
506 static int external_spawn(command_t *cmd, const char *file, const char **argv)
507 {
508     file = file;                /* -Wunused */
509
510     if (!cmd->options.silent) {
511         const char **argument = argv;
512         printf("Executing: ");
513         while (*argument) {
514             printf("%s ", *argument);
515             argument++;
516         }
517         puts("");
518     }
519
520     if (cmd->options.dry_run) {
521         return 0;
522     }
523 #if defined(__EMX__) || defined(__MINGW32__)
524     return spawnvp(P_WAIT, argv[0], argv);
525 #else
526     {
527         pid_t pid;
528         pid = fork();
529         if (pid == 0) {
530             return execvp(argv[0], (char**)argv);
531         }
532         else {
533             int statuscode;
534             waitpid(pid, &statuscode, 0);
535             if (WIFEXITED(statuscode)) {
536                 return WEXITSTATUS(statuscode);
537             }
538             return 0;
539         }
540     }
541 #endif
542 }
543
544 static int run_command(command_t *cmd_data, count_chars *cc)
545 {
546     int ret;
547     char *command;
548     char *tmp;
549     const char *raw;
550     const char *spawn_args[4];
551     count_chars tmpcc;
552
553     init_count_chars(&tmpcc);
554
555     if (cmd_data->program) {
556         push_count_chars(&tmpcc, cmd_data->program);
557     }
558
559     append_count_chars(&tmpcc, cmd_data->program_opts);
560
561     append_count_chars(&tmpcc, cc);
562
563     raw = flatten_count_chars(&tmpcc, 1);
564     command = shell_esc(raw);
565     
566     memcpy(&tmp, &raw, sizeof(tmp));
567     free(tmp);
568
569     spawn_args[0] = SHELL_CMD;
570     spawn_args[1] = "-c";
571     spawn_args[2] = command;
572     spawn_args[3] = NULL;
573     ret = external_spawn(cmd_data, spawn_args[0], spawn_args);
574     
575     free(command);
576     
577     return ret;
578 }
579
580 /*
581  * print configuration
582  * shlibpath_var is used in configure.
583  */
584 #define printc(_x,_y) if (!value || !strcmp(value, _x)) printf(_x "=\"%s\"\n", _y)
585
586 static void print_config(const char *value)
587 {
588 #ifdef LD_RUN_PATH
589     printc("runpath_var", LD_RUN_PATH);
590 #endif
591 #ifdef LD_LIBRARY_PATH
592     printc("shlibpath_var", LD_LIBRARY_PATH);
593 #endif
594 #ifdef LD_LIBRARY_PATH_LOCAL
595     printc("shlocallibpath_var", LD_LIBRARY_PATH_LOCAL);
596 #endif
597 #ifdef SHELL_CMD
598     printc("SHELL", SHELL_CMD);
599 #endif
600 #ifdef OBJECT_EXT
601     printc("objext", OBJECT_EXT);
602 #endif
603 #ifdef OBJDIR
604     printc("objdir", OBJDIR);
605 #endif
606 #ifdef DYNAMIC_LIB_EXT
607     /* add a '.' prefix because libtool does that. */
608     printc("shrext_cmds", "echo ." DYNAMIC_LIB_EXT);
609     /* add a '.' prefix because libtool does that. */
610     printc("shrext", "." DYNAMIC_LIB_EXT);
611 #endif
612 #ifdef EXE_EXT
613     printc("exeext", EXE_EXT);
614 #endif
615 #ifdef STATIC_LIB_EXT
616     printc("libext", STATIC_LIB_EXT);
617 #endif
618 #ifdef LIBRARIAN
619     printc("AR", LIBRARIAN);
620 #endif
621 #ifdef LIBRARIAN_OPTS
622     printc("AR_FLAGS", LIBRARIAN_OPTS);
623 #endif
624 #ifdef LINKER_FLAG_PREFIX
625     printc("wl", LINKER_FLAG_PREFIX);
626 #endif
627 #ifdef RANLIB
628     printc("ranlib", RANLIB);
629 #endif
630
631 }
632 /*
633  * Add a directory to the runtime library search path.
634  */
635 static void add_runtimedirlib(char *arg, command_t *cmd_data)
636 {
637 #ifdef RPATH
638     add_rpath(cmd_data->shared_opts.dependencies, arg);
639 #else
640     arg = arg;                  /* -Wunused */
641     cmd_data = cmd_data;
642 #endif
643 }
644
645 static int parse_long_opt(char *arg, command_t *cmd_data)
646 {
647     char *equal_pos = strchr(arg, '=');
648     char var[50];
649     char value[500];
650
651     if (equal_pos) {
652         strncpy(var, arg, equal_pos - arg);
653         var[equal_pos - arg] = 0;
654         strcpy(value, equal_pos + 1);
655     } else {
656         strncpy(var, arg, sizeof(var) - 1);
657         var[sizeof(var) - 1] = '\0';
658         
659         value[0] = '\0';
660     }
661
662     if (strcmp(var, "silent") == 0) {
663         cmd_data->options.silent = 1;
664     } else if (strcmp(var, "quiet") == 0) {
665         cmd_data->options.silent = 1;
666     } else if (strcmp(var, "debug") == 0) {
667         cmd_data->options.debug = 1;
668     } else if (strcmp(var, "mode") == 0) {
669         if (cmd_data->mode != mUnknown) {
670                 fprintf(stderr, "Cannot set --mode twice\n");
671                 exit(1);
672         }
673
674         if (strcmp(value, "compile") == 0) {
675             cmd_data->mode = mCompile;
676             cmd_data->output = otObject;
677
678         } else if (strcmp(value, "link") == 0) {
679             cmd_data->mode = mLink;
680             cmd_data->output = otLibrary;
681
682         } else if (strcmp(value, "install") == 0) {
683             cmd_data->mode = mInstall;
684
685         } else if (strcmp(value, "execute") == 0) {
686             cmd_data->mode = mExecute;
687
688         } else {
689             printf("Unknown mode \"%s\"\n", value);
690         }
691     } else if (strcmp(var, "shared") == 0) {
692       if ((cmd_data->mode == mLink) &&
693           (cmd_data->output == otGeneral)) {
694             cmd_data->output = otDynamicLibraryOnly;
695         }
696         cmd_data->options.shared = share_SHARED;
697     } else if (strcmp(var, "export-all") == 0) {
698         cmd_data->options.export_all = 1;
699     } else if (strcmp(var, "dry-run") == 0) {
700         printf("Dry-run mode on!\n");
701         cmd_data->options.dry_run = 1;
702     } else if (strcmp(var, "version") == 0) {
703         printf("Version " VERSION "\n");
704     } else if (strcmp(var, "help") == 0) {
705         usage(0);
706     } else if (strcmp(var, "config") == 0) {
707         print_config(value);
708         exit(0);
709     } else if (strcmp(var, "tag") == 0) {
710         if (strcmp(value, "CC") == 0) {
711             /* Do nothing. */
712         }
713         if (strcmp(value, "CXX") == 0) {
714             /* Do nothing. */
715         }
716     } else {
717         return 0;
718     }
719
720     return 1;
721 }
722
723 /* Return 1 if we eat it. */
724 static int parse_short_opt(char *arg, command_t *cmd_data)
725 {
726     if (strcmp(arg, "export-dynamic") == 0) {
727         cmd_data->options.export_dynamic = 1;
728         return 1;
729     }
730
731     if (strcmp(arg, "module") == 0) {
732         cmd_data->output = otModule;
733         return 1;
734     }
735
736     if (strcmp(arg, "shared") == 0) {
737         if (cmd_data->mode == mLink) {
738             cmd_data->output = otDynamicLibraryOnly;
739         }
740         cmd_data->options.shared = share_SHARED;
741         return 1;
742     }
743
744     if (strcmp(arg, "Zexe") == 0) {
745         return 1;
746     }
747
748     if (strcmp(arg, "avoid-version") == 0) {
749         return 1;
750     }
751
752     if (strcmp(arg, "prefer-pic") == 0) {
753         cmd_data->options.pic_mode = pic_PREFER;
754         return 1;
755     }
756
757     if (strcmp(arg, "prefer-non-pic") == 0) {
758         cmd_data->options.pic_mode = pic_AVOID;
759         return 1;
760     }
761
762     if (strcmp(arg, "static") == 0) {
763        if ((cmd_data->mode == mLink) &&
764           (cmd_data->output == otLibrary)) {
765             cmd_data->output = otStaticLibraryOnly;
766         }
767         cmd_data->options.shared = share_STATIC;
768         return 1;
769     }
770
771     if (cmd_data->mode == mLink) {
772         if (strcmp(arg, "no-install") == 0) {
773             cmd_data->options.no_install = 1;
774             return 1;
775         }
776         if (arg[0] == 'L' || arg[0] == 'l') {
777             /* Hack... */
778             arg--;
779             push_count_chars(cmd_data->shared_opts.dependencies, arg);
780             return 1;
781         } else if (arg[0] == 'R' && arg[1]) {
782             /* -Rdir Add dir to runtime library search path. */
783             add_runtimedirlib(&arg[1], cmd_data);
784             return 1;
785         }
786     }
787     return 0;
788 }
789
790 #ifdef TRUNCATE_DLL_NAME
791 static char *truncate_dll_name(char *path)
792 {
793     /* Cut DLL name down to 8 characters after removing any mod_ prefix */
794     char *tmppath = strdup(path);
795     char *newname = strrchr(tmppath, '/') + 1;
796     char *ext = strrchr(newname, '.');
797     int len;
798
799     if (ext == NULL)
800         return tmppath;
801
802     len = ext - newname;
803
804     if (strncmp(newname, "mod_", 4) == 0) {
805         strcpy(newname, newname + 4);
806         len -= 4;
807     }
808
809     if (len > 8) {
810         strcpy(newname + 8, strchr(newname, '.'));
811     }
812
813     return tmppath;
814 }
815 #endif
816
817 static long safe_strtol(const char *nptr, const char **endptr, int base)
818 {
819     long rv;
820
821     errno = 0;
822
823     rv = strtol(nptr, (char**)endptr, 10);
824
825     if (errno == ERANGE) {
826         return 0;
827     }
828
829     return rv;
830 }
831
832 static void safe_mkdir(const char *path)
833 {
834     int status;
835     mode_t old_umask;
836
837     old_umask = umask(0);
838     umask(old_umask);
839
840 #ifdef MKDIR_NO_UMASK
841     status = mkdir(path);
842 #else
843     status = mkdir(path, ~old_umask);
844 #endif
845     if ((status < 0) && (errno != EEXIST)) {
846         printf("Warning: mkdir of %s failed\n", path);
847     }
848 }
849
850 /* returns just a file's name without the path */
851 static const char *jlibtool_basename(const char *fullpath)
852 {
853     const char *name = strrchr(fullpath, '/');
854
855     if (name == NULL) {
856         name = strrchr(fullpath, '\\');
857     }
858
859     if (name == NULL) {
860         name = fullpath;
861     } else {
862         name++;
863     }
864
865     return name;
866 }
867
868 #ifdef GEN_EXPORTS
869 /* returns just a file's name without path or extension */
870 static const char *nameof(const char *fullpath)
871 {
872     const char *name;
873     const char *ext;
874
875     name = jlibtool_basename(fullpath);
876     ext = strrchr(name, '.');
877
878     if (ext) {
879         char *trimmed;
880         trimmed = malloc(ext - name + 1);
881         strncpy(trimmed, name, ext - name);
882         trimmed[ext-name] = 0;
883         return trimmed;
884     }
885
886     return name;
887 }
888 #endif
889
890 /* version_info is in the form of MAJOR:MINOR:PATCH */
891 static const char *darwin_dynamic_link_function(const char *version_info)
892 {
893     char *newarg;
894     long major, minor, patch;
895
896     major = 0;
897     minor = 0;
898     patch = 0;
899
900     if (version_info) {
901         major = safe_strtol(version_info, &version_info, 10);
902
903         if (version_info) {
904             if (version_info[0] == ':') {
905                 version_info++;
906             }
907
908             minor = safe_strtol(version_info, &version_info, 10);
909
910             if (version_info) {
911                 if (version_info[0] == ':') {
912                     version_info++;
913                 }
914
915                 patch = safe_strtol(version_info, &version_info, 10);
916
917             }
918         }
919     }
920
921     /* Avoid -dylib_compatibility_version must be greater than zero errors. */
922     if (major == 0) {
923         major = 1;
924     }
925     newarg = (char*)malloc(100);
926     snprintf(newarg, 99,
927              "-compatibility_version %ld -current_version %ld.%ld",
928              major, major, minor);
929
930     return newarg;
931 }
932
933
934 /*
935  *      Add a '.libs/' to the buffer.  The caller ensures that
936  *      The buffer is large enough to handle 6 extra characters.
937  */
938 static void add_dotlibs(char *buffer)
939 {
940         char *name = strrchr(buffer, '/');
941
942         if (!name) {
943                 if (!buffer[0]) {
944                         strcpy(buffer, ".libs/");
945                         return;
946                 }
947                 name = buffer;
948         } else {
949                 name++;
950         }
951         memmove(name + 6, name, strlen(name));
952         memcpy(name, ".libs/", 6);
953 }
954
955 static char *gen_library_name(const char *name, enum lib_type genlib)
956 {
957     char *newarg, *newext;
958
959     newarg = (char *)calloc(strlen(name) + 11, 1);
960
961     if (genlib == type_MODULE_LIB && strncmp(name, "lib", 3) == 0) {
962         name += 3;
963     }
964
965     if (genlib == type_MODULE_LIB) {
966         strcpy(newarg, jlibtool_basename(name));
967     }
968     else {
969         strcpy(newarg, name);
970     }
971
972     newext = strrchr(newarg, '.') + 1;
973
974     switch (genlib) {
975     case type_STATIC_LIB:
976         strcpy(newext, STATIC_LIB_EXT);
977         break;
978     case type_DYNAMIC_LIB:
979         strcpy(newext, DYNAMIC_LIB_EXT);
980         break;
981     case type_MODULE_LIB:
982         strcpy(newext, MODULE_LIB_EXT);
983         break;
984
985     default:
986         break;
987     }
988
989     add_dotlibs(newarg);
990
991     return newarg;
992 }
993
994 static char *gen_install_name(const char *name, enum lib_type genlib)
995 {
996     char *newname;
997     int rv;
998     struct stat sb;
999
1000     newname = gen_library_name(name, genlib);
1001     if (!newname) return NULL;
1002
1003     /* Check if it exists. If not, return NULL.  */
1004     rv = stat(newname, &sb);
1005
1006     if (rv) {
1007         free(newname);
1008         return NULL;
1009     }
1010
1011     return newname;
1012 }
1013
1014 static const char *check_object_exists(command_t *cmd, const char *arg, int arglen)
1015 {
1016     char *newarg, *ext;
1017     int pass, rv;
1018
1019     newarg = (char *)malloc(arglen + 10);
1020     memcpy(newarg, arg, arglen);
1021     newarg[arglen] = 0;
1022     ext = newarg + arglen;
1023
1024     pass = 0;
1025
1026     do {
1027         struct stat sb;
1028
1029         switch (pass) {
1030         case 0:
1031             strcpy(ext, OBJECT_EXT);
1032             break;
1033 /*
1034         case 1:
1035             strcpy(ext, NO_PIC_EXT);
1036             break;
1037 */
1038         default:
1039             break;
1040         }
1041
1042         if (cmd->options.debug) {
1043             printf("Checking (obj): %s\n", newarg);
1044         }
1045         rv = stat(newarg, &sb);
1046     }
1047     while (rv != 0 && ++pass < 1);
1048
1049     if (rv == 0) {
1050         if (pass == 1) {
1051             cmd->options.pic_mode = pic_AVOID;
1052         }
1053         return newarg;
1054     }
1055     
1056     free(newarg);
1057
1058     return NULL;
1059 }
1060
1061 /* libdircheck values:
1062  * 0 - no .libs suffix
1063  * 1 - .libs suffix
1064  */
1065 static char *check_library_exists(command_t *cmd, const char *arg, int pathlen,
1066                            int libdircheck, enum lib_type *libtype)
1067 {
1068     char *newarg, *ext;
1069     int pass, rv, newpathlen;
1070
1071     newarg = (char *)malloc(strlen(arg) + 10);
1072     strcpy(newarg, arg);
1073     newarg[pathlen] = '\0';
1074
1075     newpathlen = pathlen;
1076     if (libdircheck) {
1077         add_dotlibs(newarg);
1078         newpathlen += sizeof(".libs/") - 1;
1079     }
1080
1081     strcpy(newarg + newpathlen, arg + pathlen);
1082     ext = strrchr(newarg, '.') + 1;
1083     if (!ext) {
1084         printf("Error: Library path does not have an extension");
1085         free(newarg);
1086         
1087         return NULL;
1088     }
1089
1090     pass = 0;
1091
1092     do {
1093         struct stat sb;
1094
1095         switch (pass) {
1096         case 0:
1097             if (cmd->options.pic_mode != pic_AVOID &&
1098                 cmd->options.shared != share_STATIC) {
1099                 strcpy(ext, DYNAMIC_LIB_EXT);
1100                 *libtype = type_DYNAMIC_LIB;
1101                 break;
1102             }
1103             pass = 1;
1104             /* Fall through */
1105         case 1:
1106             strcpy(ext, STATIC_LIB_EXT);
1107             *libtype = type_STATIC_LIB;
1108             break;
1109         case 2:
1110             strcpy(ext, MODULE_LIB_EXT);
1111             *libtype = type_MODULE_LIB;
1112             break;
1113         case 3:
1114             strcpy(ext, OBJECT_EXT);
1115             *libtype = type_OBJECT;
1116             break;
1117         default:
1118             *libtype = type_UNKNOWN;
1119             break;
1120         }
1121
1122         if (cmd->options.debug) {
1123             printf("Checking (lib): %s\n", newarg);
1124         }
1125         rv = stat(newarg, &sb);
1126     }
1127     while (rv != 0 && ++pass < 4);
1128
1129     if (rv == 0) {
1130         return newarg;
1131     }
1132     
1133     free(newarg);
1134
1135     return NULL;
1136 }
1137
1138 static char * load_install_path(const char *arg)
1139 {
1140     FILE *f;
1141     char *path;
1142
1143     f = fopen(arg,"r");
1144     if (f == NULL) {
1145         return NULL;
1146     }
1147     
1148     path = malloc(PATH_MAX);
1149
1150     fgets(path, PATH_MAX, f);
1151     fclose(f);
1152     
1153     if (path[strlen(path)-1] == '\n') {
1154         path[strlen(path)-1] = '\0';
1155     }
1156     
1157     /* Check that we have an absolute path.
1158      * Otherwise the file could be a GNU libtool file.
1159      */
1160     if (path[0] != '/') {
1161         free(path);
1162         
1163         return NULL;
1164     }
1165     return path;
1166 }
1167
1168 static char * load_noinstall_path(const char *arg, int pathlen)
1169 {
1170     char *newarg, *expanded_path;
1171     int newpathlen;
1172
1173     newarg = (char *)malloc(strlen(arg) + 10);
1174     strcpy(newarg, arg);
1175     newarg[pathlen] = 0;
1176
1177     newpathlen = pathlen;
1178     strcat(newarg, ".libs");
1179     newpathlen += sizeof(".libs") - 1;
1180     newarg[newpathlen] = 0;
1181
1182 #ifdef HAS_REALPATH
1183     expanded_path = malloc(PATH_MAX);
1184     expanded_path = realpath(newarg, expanded_path);
1185     /* Uh, oh.  There was an error.  Fall back on our first guess. */
1186     if (!expanded_path) {
1187         expanded_path = newarg;
1188     }
1189 #else
1190     /* We might get ../ or something goofy.  Oh, well. */
1191     expanded_path = newarg;
1192 #endif
1193
1194     return expanded_path;
1195 }
1196
1197 static void add_dynamic_link_opts(command_t *cmd_data, count_chars *args)
1198 {
1199 #ifdef DYNAMIC_LINK_OPTS
1200     if (cmd_data->options.pic_mode != pic_AVOID) {
1201         if (cmd_data->options.debug) {
1202            printf("Adding: %s\n", DYNAMIC_LINK_OPTS);
1203         }
1204         push_count_chars(args, DYNAMIC_LINK_OPTS);
1205         if (cmd_data->undefined_flag) {
1206             push_count_chars(args, "-undefined");
1207 #if defined(__APPLE__)
1208             /* -undefined dynamic_lookup is used by the bundled Python in
1209              * 10.4, but if we don't set MACOSX_DEPLOYMENT_TARGET to 10.3+,
1210              * we'll get a linker error if we pass this flag.
1211              */
1212             if (strcasecmp(cmd_data->undefined_flag,
1213                            "dynamic_lookup") == 0) {
1214                 insert_count_chars(cmd_data->program_opts,
1215                                    "MACOSX_DEPLOYMENT_TARGET=10.3", 0);
1216             }
1217 #endif
1218             push_count_chars(args, cmd_data->undefined_flag);
1219         }
1220         else {
1221 #ifdef DYNAMIC_LINK_UNDEFINED
1222             if (cmd_data->options.debug) {
1223                 printf("Adding: %s\n", DYNAMIC_LINK_UNDEFINED);
1224             }
1225             push_count_chars(args, DYNAMIC_LINK_UNDEFINED);
1226 #endif
1227         }
1228     }
1229 #endif
1230 }
1231
1232 /* Read the final install location and add it to runtime library search path. */
1233 #ifdef RPATH
1234 static void add_rpath(count_chars *cc, const char *path)
1235 {
1236     int size = 0;
1237     char *tmp;
1238
1239 #ifdef LINKER_FLAG_PREFIX
1240     size = strlen(LINKER_FLAG_PREFIX);
1241 #endif
1242     size = size + strlen(path) + strlen(RPATH) + 2;
1243     tmp = malloc(size);
1244     if (tmp == NULL) {
1245         return;
1246     }
1247 #ifdef LINKER_FLAG_PREFIX
1248     strcpy(tmp, LINKER_FLAG_PREFIX);
1249     strcat(tmp, RPATH);
1250 #else
1251     strcpy(tmp, RPATH);
1252 #endif
1253 #ifndef LINKER_FLAG_NO_EQUALS
1254     strcat(tmp, "=");
1255 #endif
1256     strcat(tmp, path);
1257
1258     push_count_chars(cc, tmp);
1259 }
1260
1261 static void add_rpath_file(count_chars *cc, const char *arg)
1262 {
1263     const char *path;
1264
1265     path = load_install_path(arg);
1266     if (path) {
1267         add_rpath(cc, path);
1268     }
1269 }
1270
1271 static void add_rpath_noinstall(count_chars *cc, const char *arg, int pathlen)
1272 {
1273     const char *path;
1274
1275     path = load_noinstall_path(arg, pathlen);
1276     if (path) {
1277         add_rpath(cc, path);
1278         free(path);
1279     }
1280 }
1281 #endif
1282
1283 #ifdef DYNAMIC_LINK_NO_INSTALL
1284 static void add_dylink_noinstall(count_chars *cc, const char *arg, int pathlen,
1285                           int extlen)
1286 {
1287     const char *install_path, *current_path, *name;
1288     char *exp_argument;
1289     int i_p_len, c_p_len, name_len, dyext_len, cur_len;
1290
1291     install_path = load_install_path(arg);
1292     current_path = load_noinstall_path(arg, pathlen);
1293
1294     if (!install_path || !current_path) {
1295         return;
1296     }
1297
1298     push_count_chars(cc, DYNAMIC_LINK_NO_INSTALL);
1299
1300     i_p_len = strlen(install_path);
1301     c_p_len = strlen(current_path);
1302
1303     name = arg+pathlen;
1304     name_len = extlen-pathlen;
1305     dyext_len = sizeof(DYNAMIC_LIB_EXT) - 1;
1306
1307     /* No, we need to replace the extension. */
1308     exp_argument = (char *)malloc(i_p_len + c_p_len + (name_len*2) +
1309                                   (dyext_len*2) + 2);
1310
1311     cur_len = 0;
1312     strcpy(exp_argument, install_path);
1313     cur_len += i_p_len;
1314     exp_argument[cur_len++] = '/';
1315     strncpy(exp_argument+cur_len, name, extlen-pathlen);
1316     cur_len += name_len;
1317     strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1318     cur_len += dyext_len;
1319     exp_argument[cur_len++] = ':';
1320     strcpy(exp_argument+cur_len, current_path);
1321     cur_len += c_p_len;
1322     exp_argument[cur_len++] = '/';
1323     strncpy(exp_argument+cur_len, name, extlen-pathlen);
1324     cur_len += name_len;
1325     strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1326     cur_len += dyext_len;
1327
1328     push_count_chars(cc, exp_argument);
1329 }
1330 #endif
1331
1332 #ifdef ADD_MINUS_L
1333 /* use -L -llibname to allow to use installed libraries */
1334 static void add_minus_l(count_chars *cc, const char *arg)
1335 {
1336     char *newarg;
1337     char *name = strrchr(arg, '/');
1338     char *file = strrchr(arg, '.');
1339
1340     if ((name != NULL) && (file != NULL) &&
1341         (strstr(name, "lib") == (name + 1))) {
1342         *name = '\0';
1343         *file = '\0';
1344         file = name;
1345         file = file+4;
1346         push_count_chars(cc, "-L");
1347         push_count_chars(cc, arg);
1348         /* we need one argument like -lapr-1 */
1349         newarg = malloc(strlen(file) + 3);
1350         strcpy(newarg, "-l");
1351         strcat(newarg, file);
1352         push_count_chars(cc, newarg);
1353     } else {
1354         push_count_chars(cc, arg);
1355     }
1356 }
1357 #endif
1358
1359 #if 0
1360 static void add_linker_flag_prefix(count_chars *cc, const char *arg)
1361 {
1362 #ifndef LINKER_FLAG_PREFIX
1363     push_count_chars(cc, arg);
1364 #else
1365     char *newarg;
1366     newarg = (char*)malloc(strlen(arg) + sizeof(LINKER_FLAG_PREFIX) + 1);
1367     strcpy(newarg, LINKER_FLAG_PREFIX);
1368     strcat(newarg, arg);
1369     push_count_chars(cc, newarg);
1370 #endif
1371 }
1372 #endif
1373
1374 static int explode_static_lib(command_t *cmd_data, const char *lib)
1375 {
1376     count_chars tmpdir_cc, libname_cc;
1377     const char *tmpdir, *libname;
1378     char savewd[PATH_MAX];
1379     const char *name;
1380     DIR *dir;
1381     struct dirent *entry;
1382     const char *lib_args[4];
1383
1384     /* Bah! */
1385     if (cmd_data->options.dry_run) {
1386         return 0;
1387     }
1388
1389     name = jlibtool_basename(lib);
1390
1391     init_count_chars(&tmpdir_cc);
1392     push_count_chars(&tmpdir_cc, ".libs/");
1393     push_count_chars(&tmpdir_cc, name);
1394     push_count_chars(&tmpdir_cc, ".exploded/");
1395     tmpdir = flatten_count_chars(&tmpdir_cc, 0);
1396
1397     if (!cmd_data->options.silent) {
1398         printf("Making: %s\n", tmpdir);
1399     }
1400     safe_mkdir(tmpdir);
1401
1402     push_count_chars(cmd_data->tmp_dirs, tmpdir);
1403
1404     getcwd(savewd, sizeof(savewd));
1405
1406     if (chdir(tmpdir) != 0) {
1407         if (!cmd_data->options.silent) {
1408             printf("Warning: could not explode %s\n", lib);
1409         }
1410         return 1;
1411     }
1412
1413     if (lib[0] == '/') {
1414         libname = lib;
1415     }
1416     else {
1417         init_count_chars(&libname_cc);
1418         push_count_chars(&libname_cc, "../../");
1419         push_count_chars(&libname_cc, lib);
1420         libname = flatten_count_chars(&libname_cc, 0);
1421     }
1422
1423     lib_args[0] = LIBRARIAN;
1424     lib_args[1] = "x";
1425     lib_args[2] = libname;
1426     lib_args[3] = NULL;
1427
1428     external_spawn(cmd_data, LIBRARIAN, lib_args);
1429
1430     chdir(savewd);
1431     dir = opendir(tmpdir);
1432
1433     while ((entry = readdir(dir)) != NULL) {
1434 #if defined(__APPLE__) && defined(RANLIB)
1435         /* Apple inserts __.SYMDEF which isn't needed.
1436          * Leopard (10.5+) can also add '__.SYMDEF SORTED' which isn't
1437          * much fun either.  Just skip them.
1438          */
1439         if (strstr(entry->d_name, "__.SYMDEF") != NULL) {
1440             continue;
1441         }
1442 #endif
1443         if (entry->d_name[0] != '.') {
1444             push_count_chars(&tmpdir_cc, entry->d_name);
1445             name = flatten_count_chars(&tmpdir_cc, 0);
1446             if (cmd_data->options.debug) {
1447                 printf("Adding: %s\n", name);
1448             }
1449             push_count_chars(cmd_data->obj_files, name);
1450             pop_count_chars(&tmpdir_cc);
1451         }
1452     }
1453
1454     closedir(dir);
1455     return 0;
1456 }
1457
1458 static int parse_input_file_name(char *arg, command_t *cmd_data)
1459 {
1460     const char *ext = strrchr(arg, '.');
1461     const char *name;
1462     int pathlen;
1463     enum lib_type libtype;
1464     const char *newarg;
1465
1466     if (!ext) {
1467         return 0;
1468     }
1469
1470     ext++;
1471     name = jlibtool_basename(arg);
1472
1473     pathlen = name - arg;
1474
1475     if (strcmp(ext, "lo") == 0) {
1476         newarg = check_object_exists(cmd_data, arg, ext - arg);
1477         if (!newarg) {
1478             fprintf(stderr, "Can not find suitable object file for %s\n", arg);
1479             exit(1);
1480         }
1481         if (cmd_data->mode != mLink) {
1482             push_count_chars(cmd_data->arglist, newarg);
1483         }
1484         else {
1485             push_count_chars(cmd_data->obj_files, newarg);
1486         }
1487         return 1;
1488     }
1489
1490     if (strcmp(ext, "la") == 0) {
1491         switch (cmd_data->mode) {
1492         case mLink:
1493             /* Try the .libs dir first! */
1494             newarg = check_library_exists(cmd_data, arg, pathlen, 1, &libtype);
1495             if (!newarg) {
1496                 /* Try the normal dir next. */
1497                 newarg = check_library_exists(cmd_data, arg, pathlen, 0, &libtype);
1498                 if (!newarg) {
1499                     fprintf(stderr, "Can not find suitable library for %s\n", arg);
1500                     exit(1);
1501                 }
1502             }
1503
1504             /* It is not ok to just add the file: a library may added with:
1505                1 - -L path library_name. (For *.so in Linux).
1506                2 - library_name.
1507              */
1508 #ifdef ADD_MINUS_L
1509             if (libtype == type_DYNAMIC_LIB) {
1510                  add_minus_l(cmd_data->shared_opts.dependencies, newarg);
1511             } else if (cmd_data->output == otLibrary &&
1512                        libtype == type_STATIC_LIB) {
1513                 explode_static_lib(cmd_data, newarg);
1514             } else {
1515                  push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1516             }
1517 #else
1518             if (cmd_data->output == otLibrary && libtype == type_STATIC_LIB) {
1519                 explode_static_lib(cmd_data, newarg);
1520             }
1521             else {
1522                 push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1523             }
1524 #endif
1525             if (libtype == type_DYNAMIC_LIB) {
1526                 if (cmd_data->options.no_install) {
1527 #ifdef RPATH
1528                     add_rpath_noinstall(cmd_data->shared_opts.dependencies,
1529                                         arg, pathlen);
1530 #endif
1531 #ifdef DYNAMIC_LINK_NO_INSTALL
1532                     /*
1533                      * This doesn't work as Darwin's linker has no way to
1534                      * override at link-time the search paths for a
1535                      * non-installed library.
1536                      */
1537                     /*
1538                     add_dylink_noinstall(cmd_data->shared_opts.dependencies,
1539                                          arg, pathlen, ext - arg);
1540                     */
1541 #endif
1542                 }
1543                 else {
1544 #ifdef RPATH
1545                     add_rpath_file(cmd_data->shared_opts.dependencies, arg);
1546 #endif
1547                 }
1548             }
1549             break;
1550         case mInstall:
1551             /* If we've already recorded a library to install, we're most
1552              * likely getting the .la file that we want to install as.
1553              * The problem is that we need to add it as the directory,
1554              * not the .la file itself.  Otherwise, we'll do odd things.
1555              */
1556             if (cmd_data->output == otLibrary) {
1557                 arg[pathlen] = '\0';
1558                 push_count_chars(cmd_data->arglist, arg);
1559             }
1560             else {
1561                 cmd_data->output = otLibrary;
1562                 cmd_data->output_name = arg;
1563                 cmd_data->static_name.install = gen_install_name(arg, 0);
1564                 cmd_data->shared_name.install = gen_install_name(arg, 1);
1565                 cmd_data->module_name.install = gen_install_name(arg, 2);
1566
1567                 if (!cmd_data->static_name.install &&
1568                     !cmd_data->shared_name.install &&
1569                     !cmd_data->module_name.install) {
1570                   fprintf(stderr, "Files to install do not exist\n");
1571                   exit(1);
1572                 }
1573
1574             }
1575             break;
1576         default:
1577             break;
1578         }
1579         return 1;
1580     }
1581
1582     if (strcmp(ext, "c") == 0) {
1583         /* If we don't already have an idea what our output name will be. */
1584         if (cmd_data->basename == NULL) {
1585             cmd_data->basename = (char *)malloc(strlen(arg) + 4);
1586             strcpy(cmd_data->basename, arg);
1587             strcpy(strrchr(cmd_data->basename, '.') + 1, "lo");
1588
1589             cmd_data->fake_output_name = strrchr(cmd_data->basename, '/');
1590             if (cmd_data->fake_output_name) {
1591                 cmd_data->fake_output_name++;
1592             }
1593             else {
1594                 cmd_data->fake_output_name = cmd_data->basename;
1595             }
1596         }
1597     }
1598
1599     return 0;
1600 }
1601
1602 static int parse_output_file_name(char *arg, command_t *cmd_data)
1603 {
1604     const char *name;
1605     const char *ext;
1606     char *newarg = NULL;
1607     int pathlen;
1608
1609     cmd_data->fake_output_name = arg;
1610
1611     name = jlibtool_basename(arg);
1612     ext = strrchr(name, '.');
1613
1614 #ifdef EXE_EXT
1615     if (!ext || strcmp(ext, EXE_EXT) == 0) {
1616 #else
1617     if (!ext) {
1618 #endif
1619         cmd_data->basename = arg;
1620         cmd_data->output = otProgram;
1621 #if defined(_OSD_POSIX)
1622         cmd_data->options.pic_mode = pic_AVOID;
1623 #endif
1624         newarg = (char *)malloc(strlen(arg) + 5);
1625         strcpy(newarg, arg);
1626 #ifdef EXE_EXT
1627         if (!ext) {
1628           strcat(newarg, EXE_EXT);
1629         }
1630 #endif
1631         cmd_data->output_name = newarg;
1632         return 1;
1633     }
1634
1635     ext++;
1636     pathlen = name - arg;
1637
1638     if (strcmp(ext, "la") == 0) {
1639         assert(cmd_data->mode == mLink);
1640
1641         cmd_data->basename = arg;
1642         cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1643         cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1644         cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1645         cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1646         cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1647         cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1648
1649         if (!cmd_data->options.dry_run) {
1650                 char *newname;
1651                 char *newext;
1652                 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1653
1654                 strcpy(newname, cmd_data->static_name.normal);
1655                 newext = strrchr(newname, '/');
1656                 if (!newext) {
1657                         /* Check first to see if the dir already exists! */
1658                         safe_mkdir(".libs");
1659                 } else {
1660                         *newext = '\0';
1661                         safe_mkdir(newname);
1662                 }
1663                 free(newname);
1664         }
1665
1666 #ifdef TRUNCATE_DLL_NAME
1667         if (shared) {
1668           arg = truncate_dll_name(arg);
1669         }
1670 #endif
1671
1672         cmd_data->output_name = arg;
1673         return 1;
1674     }
1675
1676     if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1677         assert(cmd_data->mode == mLink);
1678
1679         cmd_data->basename = arg;
1680         cmd_data->options.shared = share_STATIC;
1681         cmd_data->output = otStaticLibraryOnly;
1682         cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1683         cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1684
1685         if (!cmd_data->options.dry_run) {
1686                 char *newname;
1687                 char *newext;
1688                 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1689
1690                 strcpy(newname, cmd_data->static_name.normal);
1691                 newext = strrchr(newname, '/');
1692                 if (!newext) {
1693                         /* Check first to see if the dir already exists! */
1694                         safe_mkdir(".libs");
1695                 } else {
1696                         *newext = '\0';
1697                         safe_mkdir(newname);
1698                 }
1699                 free(newname);
1700         }
1701
1702         cmd_data->output_name = arg;
1703         return 1;
1704     }
1705
1706     if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1707         assert(cmd_data->mode == mLink);
1708
1709         cmd_data->basename = arg;
1710         cmd_data->options.shared = share_SHARED;
1711         cmd_data->output = otDynamicLibraryOnly;
1712         cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1713         cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1714         cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1715         cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1716
1717         if (!cmd_data->options.dry_run) {
1718                 char *newname;
1719                 char *newext;
1720                 newname = malloc(strlen(cmd_data->shared_name.normal) + 1);
1721
1722                 strcpy(newname, cmd_data->shared_name.normal);
1723                 newext = strrchr(newname, '/');
1724                 if (!newext) {
1725                         /* Check first to see if the dir already exists! */
1726                         safe_mkdir(".libs");
1727                 } else {
1728                         *newext = '\0';
1729                         safe_mkdir(newname);
1730                 }
1731                 free(newname);
1732         }
1733
1734         cmd_data->output_name = arg;
1735         return 1;
1736     }
1737
1738     if (strcmp(ext, "lo") == 0) {
1739         char *newext;
1740         cmd_data->basename = arg;
1741         cmd_data->output = otObject;
1742         newarg = (char *)malloc(strlen(arg) + 2);
1743         strcpy(newarg, arg);
1744         newext = strrchr(newarg, '.') + 1;
1745         strcpy(newext, OBJECT_EXT);
1746         cmd_data->output_name = newarg;
1747         return 1;
1748     }
1749
1750     if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1751       fprintf(stderr, "Please build libraries with .la target, not ."
1752               DYNAMIC_LIB_EXT "\n");
1753       exit(1);
1754     }
1755
1756     if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1757       fprintf(stderr, "Please build libraries with .la target, not ."
1758               STATIC_LIB_EXT "\n");
1759       exit(1);
1760     }
1761
1762     return 0;
1763 }
1764
1765 static char *automode(char *arg, command_t *cmd_data)
1766 {
1767         if (cmd_data->mode != mUnknown) return arg;
1768
1769         if (strcmp(arg, "CC") == 0) {
1770                 arg = CC;
1771                 cmd_data->mode = mCompile;
1772
1773         } else if (strcmp(arg, "CXX") == 0) {
1774                 arg = CXX;
1775                 cmd_data->mode = mCompile;
1776
1777         } else if (strcmp(arg, "LINK") == 0) {
1778                 arg = LINK_c;
1779                 cmd_data->mode = mLink;
1780
1781         } else if (strcmp(arg, "LINK.c") == 0) {
1782                 arg = LINK_c;
1783                 cmd_data->mode = mLink;
1784
1785         } else if (strcmp(arg, "LINK.cxx") == 0) {
1786                 arg = LINK_cxx;
1787                 cmd_data->mode = mLink;
1788         }
1789         
1790         return arg;
1791 }
1792
1793 static void parse_args(int argc, char *argv[], command_t *cmd_data)
1794 {
1795     int a;
1796     char *arg, *base;
1797     int argused;
1798
1799     /*
1800      *  We now take a major step past libtool.
1801      *
1802      *  IF there's no "--mode=...", AND we recognise
1803      *  the binary as a "special" name, THEN replace it
1804      *  with the correct one, and set the correct mode.
1805      */
1806     base = jlibtool_basename(argv[0]);
1807     arg = automode(base, cmd_data);
1808     
1809     if (arg != base) {
1810             if (cmd_data->options.debug) {
1811                     printf("Adding: %s\n", arg);
1812             }
1813             push_count_chars(cmd_data->arglist, arg);
1814             assert(cmd_data->mode != mUnknown);
1815     }
1816
1817     /*
1818      *  We first pass over the command-line arguments looking for
1819      *  "--mode", etc.  If so, then use the libtool compatibility
1820      *  method for building the software.  Otherwise, auto-detect it
1821      *  via "-o" and the extensions.
1822      */
1823     base = NULL;
1824     if (cmd_data->mode == mUnknown) for (a = 1; a < argc; a++) {
1825         arg = argv[a];
1826
1827         if (strncmp(arg, "--mode=", 7) == 0) {
1828           base = NULL;
1829           break;
1830         }
1831
1832         /*
1833          *      Stop if we get another magic method
1834          */
1835         if ((a == 1) &&
1836             ((strncmp(arg, "LINK", 4) == 0) ||
1837              (strcmp(arg, "CC") == 0) ||
1838              (strcmp(arg, "CXX") == 0))) {
1839           base = NULL;
1840           break;
1841         }
1842
1843         if (strncmp(arg, "-o", 2) == 0) {
1844           base = argv[++a];
1845         }
1846     }
1847
1848     if (base) {
1849             arg = strrchr(base, '.');
1850             if (!arg) {
1851                     cmd_data->mode = mLink;
1852                     push_count_chars(cmd_data->arglist, LINK_c);
1853             }
1854 #ifdef EXE_EXT
1855             else if (strcmp(arg, EXE_EXT) == 0) {
1856                     cmd_data->mode = mLink;
1857                     push_count_chars(cmd_data->arglist, LINK_c);
1858             }
1859 #endif
1860             else if (strcmp(arg + 1, DYNAMIC_LIB_EXT) == 0) {
1861                     cmd_data->mode = mLink;
1862                     push_count_chars(cmd_data->arglist, LINK_c);
1863             }
1864             else if (strcmp(arg + 1, STATIC_LIB_EXT) == 0) {
1865                     cmd_data->mode = mLink;
1866                     push_count_chars(cmd_data->arglist, LINK_c);
1867             }
1868             else if (strcmp(arg + 1, "la") == 0) {
1869                     cmd_data->mode = mLink;
1870                     push_count_chars(cmd_data->arglist, LINK_c);
1871             }
1872             else if ((strcmp(arg + 1, "lo") == 0) ||
1873                      (strcmp(arg + 1, "o") == 0)) {
1874                     cmd_data->mode = mCompile;
1875                     push_count_chars(cmd_data->arglist, CC);
1876             }
1877     }
1878
1879     for (a = 1; a < argc; a++) {
1880         arg = argv[a];
1881         argused = 1;
1882
1883         if (arg[0] == '-') {
1884             if (arg[1] == '-') {
1885                 argused = parse_long_opt(arg + 2, cmd_data);
1886             }
1887             else {
1888                 argused = parse_short_opt(arg + 1, cmd_data);
1889             }
1890
1891             /* We haven't done anything with it yet, try some of the
1892              * more complicated short opts... */
1893             if (argused == 0 && a + 1 < argc) {
1894                 if (arg[1] == 'o' && !arg[2]) {
1895                     arg = argv[++a];
1896                     argused = parse_output_file_name(arg, cmd_data);
1897                 } else if (strcmp(arg+1, "MT") == 0) {
1898                     if (cmd_data->options.debug) {
1899                         printf("Adding: %s\n", arg);
1900                     }
1901                     push_count_chars(cmd_data->arglist, arg);
1902                     arg = argv[++a];
1903                     if (!cmd_data->options.silent) {
1904                         printf(" %s\n", arg);
1905                     }
1906                     push_count_chars(cmd_data->arglist, arg);
1907                     argused = 1;
1908                 } else if (strcmp(arg+1, "rpath") == 0) {
1909                     /* Aha, we should try to link both! */
1910                     cmd_data->install_path = argv[++a];
1911                     argused = 1;
1912                 } else if (strcmp(arg+1, "release") == 0) {
1913                     /* Store for later deciphering */
1914                     cmd_data->version_info = argv[++a];
1915                     argused = 1;
1916                 } else if (strcmp(arg+1, "version-info") == 0) {
1917                     /* Store for later deciphering */
1918                     cmd_data->version_info = argv[++a];
1919                     argused = 1;
1920                 } else if (strcmp(arg+1, "export-symbols-regex") == 0) {
1921                     /* Skip the argument. */
1922                     ++a;
1923                     argused = 1;
1924                 } else if (strcmp(arg+1, "release") == 0) {
1925                     /* Skip the argument. */
1926                     ++a;
1927                     argused = 1;
1928                 } else if (strcmp(arg+1, "undefined") == 0) {
1929                     cmd_data->undefined_flag = argv[++a];
1930                     argused = 1;
1931                 } else if (arg[1] == 'R' && !arg[2]) {
1932                     /* -R dir Add dir to runtime library search path. */
1933                     add_runtimedirlib(argv[++a], cmd_data);
1934                     argused = 1;
1935                 }
1936             }
1937         } else {
1938             argused = parse_input_file_name(arg, cmd_data);
1939         }
1940
1941         if (!argused) {
1942                 /*
1943                  *  If we still don't have a run mode, look for a magic
1944                  *  program name CC, LINK, or whatever.  Then replace that
1945                  *  with the name of the real program we want to run.
1946                  */
1947                 if ((cmd_data->arglist->num == 0) &&
1948                     (cmd_data->mode == mUnknown)) {
1949                         arg = automode(arg, cmd_data);
1950                 }
1951
1952             if (cmd_data->options.debug) {
1953                 printf("Adding: %s\n", arg);
1954             }
1955             push_count_chars(cmd_data->arglist, arg);
1956         }
1957     }
1958
1959 }
1960
1961 #ifdef GEN_EXPORTS
1962 static void generate_def_file(command_t *cmd_data)
1963 {
1964     char def_file[1024];
1965     char implib_file[1024];
1966     char *ext;
1967     FILE *hDef;
1968     char *export_args[1024];
1969     int num_export_args = 0;
1970     char *cmd;
1971     int cmd_size = 0;
1972     int a;
1973
1974     if (cmd_data->output_name) {
1975         strcpy(def_file, cmd_data->output_name);
1976         strcat(def_file, ".def");
1977         hDef = fopen(def_file, "w");
1978
1979         if (hDef != NULL) {
1980             fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", nameof(cmd_data->output_name));
1981             fprintf(hDef, "DATA NONSHARED\n");
1982             fprintf(hDef, "EXPORTS\n");
1983             fclose(hDef);
1984
1985             for (a = 0; a < cmd_data->num_obj_files; a++) {
1986                 cmd_size += strlen(cmd_data->obj_files[a]) + 1;
1987             }
1988
1989             cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
1990             cmd = (char *)malloc(cmd_size);
1991             strcpy(cmd, GEN_EXPORTS);
1992
1993             for (a=0; a < cmd_data->num_obj_files; a++) {
1994                 strcat(cmd, " ");
1995                 strcat(cmd, cmd_data->obj_files[a] );
1996             }
1997
1998             strcat(cmd, ">>");
1999             strcat(cmd, def_file);
2000             puts(cmd);
2001             export_args[num_export_args++] = SHELL_CMD;
2002             export_args[num_export_args++] = "-c";
2003             export_args[num_export_args++] = cmd;
2004             export_args[num_export_args++] = NULL;
2005             external_spawn(cmd_data, export_args[0], (const char**)export_args);
2006             cmd_data->arglist[cmd_data->num_args++] = strdup(def_file);
2007
2008             /* Now make an import library for the dll */
2009             num_export_args = 0;
2010             export_args[num_export_args++] = DEF2IMPLIB_CMD;
2011             export_args[num_export_args++] = "-o";
2012
2013             strcpy(implib_file, ".libs/");
2014             strcat(implib_file, cmd_data->basename);
2015             ext = strrchr(implib_file, '.');
2016
2017             if (ext)
2018                 *ext = 0;
2019
2020             strcat(implib_file, ".");
2021             strcat(implib_file, STATIC_LIB_EXT);
2022
2023             export_args[num_export_args++] = implib_file;
2024             export_args[num_export_args++] = def_file;
2025             export_args[num_export_args++] = NULL;
2026             external_spawn(cmd_data, export_args[0], (const char**)export_args);
2027
2028         }
2029     }
2030 }
2031 #endif
2032
2033 #if 0
2034 static const char* expand_path(const char *relpath)
2035 {
2036     char foo[PATH_MAX], *newpath;
2037
2038     getcwd(foo, PATH_MAX-1);
2039     newpath = (char*)malloc(strlen(foo)+strlen(relpath)+2);
2040     strcpy(newpath, foo);
2041     strcat(newpath, "/");
2042     strcat(newpath, relpath);
2043     return newpath;
2044 }
2045 #endif
2046
2047 static void link_fixup(command_t *c)
2048 {
2049     /* If we were passed an -rpath directive, we need to build
2050      * shared objects too.  Otherwise, we should only create static
2051      * libraries.
2052      */
2053     if (!c->install_path && (c->output == otDynamicLibraryOnly ||
2054         c->output == otModule || c->output == otLibrary)) {
2055         if (c->options.shared == share_SHARED) {
2056                 c->install_path = LIBDIR;
2057         }
2058     }
2059
2060     if (c->output == otDynamicLibraryOnly ||
2061         c->output == otModule ||
2062         c->output == otLibrary) {
2063
2064         push_count_chars(c->shared_opts.normal, "-o");
2065         if (c->output == otModule) {
2066             push_count_chars(c->shared_opts.normal, c->module_name.normal);
2067         }
2068         else {
2069             char *tmp;
2070             push_count_chars(c->shared_opts.normal, c->shared_name.normal);
2071 #ifdef DYNAMIC_INSTALL_NAME
2072             push_count_chars(c->shared_opts.normal, DYNAMIC_INSTALL_NAME);
2073
2074             if (!c->install_path) {
2075                     fprintf(stderr, "Installation mode requires -rpath\n");
2076                     exit(1);
2077             }
2078
2079             tmp = (char*)malloc(PATH_MAX);
2080             strcpy(tmp, c->install_path);
2081             if (c->shared_name.install) {
2082                     strcat(tmp, strrchr(c->shared_name.install, '/'));
2083             } else {
2084                     strcat(tmp, strrchr(c->shared_name.normal, '/'));
2085             }
2086             push_count_chars(c->shared_opts.normal, tmp);
2087 #endif
2088         }
2089
2090         append_count_chars(c->shared_opts.normal, c->obj_files);
2091         append_count_chars(c->shared_opts.normal, c->shared_opts.dependencies);
2092
2093         if (c->options.export_all) {
2094 #ifdef GEN_EXPORTS
2095             generate_def_file(c);
2096 #endif
2097         }
2098     }
2099
2100     if (c->output == otLibrary || c->output == otStaticLibraryOnly) {
2101         push_count_chars(c->static_opts.normal, "-o");
2102         push_count_chars(c->static_opts.normal, c->output_name);
2103     }
2104
2105     if (c->output == otProgram) {
2106         if (c->output_name) {
2107             push_count_chars(c->arglist, "-o");
2108             push_count_chars(c->arglist, c->output_name);
2109             append_count_chars(c->arglist, c->obj_files);
2110             append_count_chars(c->arglist, c->shared_opts.dependencies);
2111             add_dynamic_link_opts(c, c->arglist);
2112         }
2113     }
2114 }
2115
2116 static void post_parse_fixup(command_t *cmd_data)
2117 {
2118     switch (cmd_data->mode)
2119     {
2120     case mCompile:
2121 #ifdef PIC_FLAG
2122         if (cmd_data->options.pic_mode != pic_AVOID) {
2123             push_count_chars(cmd_data->arglist, PIC_FLAG);
2124         }
2125 #endif
2126         if (cmd_data->output_name) {
2127             push_count_chars(cmd_data->arglist, "-o");
2128             push_count_chars(cmd_data->arglist, cmd_data->output_name);
2129         }
2130         break;
2131     case mLink:
2132         link_fixup(cmd_data);
2133         break;
2134     case mInstall:
2135         if (cmd_data->output == otLibrary) {
2136             link_fixup(cmd_data);
2137         }
2138     default:
2139         break;
2140     }
2141
2142 #ifdef USE_OMF
2143     if (cmd_data->output == otObject ||
2144         cmd_data->output == otProgram ||
2145         cmd_data->output == otLibrary ||
2146         cmd_data->output == otDynamicLibraryOnly) {
2147         push_count_chars(cmd_data->arglist, "-Zomf");
2148     }
2149 #endif
2150
2151     if (cmd_data->options.shared &&
2152             (cmd_data->output == otObject ||
2153              cmd_data->output == otLibrary ||
2154              cmd_data->output == otDynamicLibraryOnly)) {
2155 #ifdef SHARE_SW
2156         push_count_chars(cmd_data->arglist, SHARE_SW);
2157 #endif
2158     }
2159 }
2160
2161 static int run_mode(command_t *cmd_data)
2162 {
2163     int rv = 0;
2164     count_chars *cctemp;
2165
2166     cctemp = (count_chars*)malloc(sizeof(count_chars));
2167     init_count_chars(cctemp);
2168
2169     switch (cmd_data->mode)
2170     {
2171     case mCompile:
2172         rv = run_command(cmd_data, cmd_data->arglist);
2173         if (rv) goto finish;
2174         break;
2175     case mInstall:
2176         /* Well, we'll assume it's a file going to a directory... */
2177         /* For brain-dead install-sh based scripts, we have to repeat
2178          * the command N-times.  install-sh should die.
2179          */
2180         if (!cmd_data->output_name) {
2181             rv = run_command(cmd_data, cmd_data->arglist);
2182             if (rv) goto finish;
2183         }
2184         if (cmd_data->output_name) {
2185             append_count_chars(cctemp, cmd_data->arglist);
2186             insert_count_chars(cctemp,
2187                                cmd_data->output_name,
2188                                cctemp->num - 1);
2189             rv = run_command(cmd_data, cctemp);
2190             if (rv) goto finish;
2191             clear_count_chars(cctemp);
2192         }
2193         if (cmd_data->static_name.install) {
2194             append_count_chars(cctemp, cmd_data->arglist);
2195             insert_count_chars(cctemp,
2196                                cmd_data->static_name.install,
2197                                cctemp->num - 1);
2198             rv = run_command(cmd_data, cctemp);
2199             if (rv) goto finish;
2200 #if defined(__APPLE__) && defined(RANLIB)
2201             /* From the Apple libtool(1) manpage on Tiger/10.4:
2202              * ----
2203              * With  the way libraries used to be created, errors were possible
2204              * if the library was modified with ar(1) and  the  table  of
2205              * contents  was  not updated  by  rerunning ranlib(1).  Thus the
2206              * link editor, ld, warns when the modification date of a library
2207              * is more  recent  than  the  creation date  of its table of
2208              * contents.  Unfortunately, this means that you get the warning
2209              * even if you only copy the library.
2210              * ----
2211              *
2212              * This means that when we install the static archive, we need to
2213              * rerun ranlib afterwards.
2214              */
2215             const char *lib_args[3], *static_lib_name;
2216             char *tmp;
2217             size_t len1, len2;
2218             len1 = strlen(cmd_data->arglist->vals[cmd_data->arglist->num - 1]);
2219
2220             static_lib_name = jlibtool_basename(cmd_data->static_name.install);
2221             len2 = strlen(static_lib_name);
2222
2223             tmp = malloc(len1 + len2 + 2);
2224
2225             snprintf(tmp, len1 + len2 + 2, "%s/%s",
2226                     cmd_data->arglist->vals[cmd_data->arglist->num - 1],
2227                     static_lib_name);
2228
2229             lib_args[0] = RANLIB;
2230             lib_args[1] = tmp;
2231             lib_args[2] = NULL;
2232             external_spawn(cmd_data, RANLIB, lib_args);
2233             free(tmp);
2234 #endif
2235             clear_count_chars(cctemp);
2236         }
2237         if (cmd_data->shared_name.install) {
2238             append_count_chars(cctemp, cmd_data->arglist);
2239             insert_count_chars(cctemp,
2240                                cmd_data->shared_name.install,
2241                                cctemp->num - 1);
2242             rv = run_command(cmd_data, cctemp);
2243             if (rv) goto finish;
2244             clear_count_chars(cctemp);
2245         }
2246         if (cmd_data->module_name.install) {
2247             append_count_chars(cctemp, cmd_data->arglist);
2248             insert_count_chars(cctemp,
2249                                cmd_data->module_name.install,
2250                                cctemp->num - 1);
2251             rv = run_command(cmd_data, cctemp);
2252             if (rv) goto finish;
2253             clear_count_chars(cctemp);
2254         }
2255         break;
2256     case mLink:
2257         if (cmd_data->output == otStaticLibraryOnly ||
2258             cmd_data->output == otLibrary) {
2259 #ifdef RANLIB
2260             const char *lib_args[3];
2261 #endif
2262             /* Removes compiler! */
2263             cmd_data->program = LIBRARIAN;
2264             push_count_chars(cmd_data->program_opts, LIBRARIAN_OPTS);
2265             push_count_chars(cmd_data->program_opts,
2266                              cmd_data->static_name.normal);
2267
2268             rv = run_command(cmd_data, cmd_data->obj_files);
2269             if (rv) goto finish;
2270
2271 #ifdef RANLIB
2272             lib_args[0] = RANLIB;
2273             lib_args[1] = cmd_data->static_name.normal;
2274             lib_args[2] = NULL;
2275             external_spawn(cmd_data, RANLIB, lib_args);
2276 #endif
2277         }
2278
2279         if (cmd_data->output == otDynamicLibraryOnly ||
2280             cmd_data->output == otModule ||
2281             cmd_data->output == otLibrary) {
2282             cmd_data->program = NULL;
2283             clear_count_chars(cmd_data->program_opts);
2284
2285             append_count_chars(cmd_data->program_opts, cmd_data->arglist);
2286             if (cmd_data->output == otModule) {
2287 #ifdef MODULE_OPTS
2288                 push_count_chars(cmd_data->program_opts, MODULE_OPTS);
2289 #endif
2290             } else {
2291 #ifdef SHARED_OPTS
2292                 push_count_chars(cmd_data->program_opts, SHARED_OPTS);
2293 #endif
2294 #ifdef dynamic_link_version_func
2295                 push_count_chars(cmd_data->program_opts,
2296                              dynamic_link_version_func(cmd_data->version_info));
2297 #endif
2298             }
2299             add_dynamic_link_opts(cmd_data, cmd_data->program_opts);
2300
2301             rv = run_command(cmd_data, cmd_data->shared_opts.normal);
2302             if (rv) goto finish;
2303         }
2304         if (cmd_data->output == otProgram) {
2305             rv = run_command(cmd_data, cmd_data->arglist);
2306             if (rv) goto finish;
2307         }
2308         break;
2309     case mExecute:
2310     {
2311         char *l, libpath[PATH_MAX];
2312         
2313         if (strlen(cmd_data->arglist->vals[0]) >= PATH_MAX) {
2314                 fprintf(stderr, "Libpath too long no buffer space");
2315                 rv = 1;
2316                 
2317                 goto finish;
2318         }
2319
2320         strcpy(libpath, cmd_data->arglist->vals[0]);
2321         add_dotlibs(libpath);
2322         l = strrchr(libpath, '/');
2323         if (!l) l = strrchr(libpath, '\\');
2324         if (l) {
2325             *l = '\0';
2326             l = libpath;
2327         } else {
2328             l = ".libs/";
2329         }
2330
2331         l = "./build/lib/.libs";
2332         setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
2333         rv = run_command(cmd_data, cmd_data->arglist);
2334         if (rv) goto finish;
2335     }
2336       break;
2337
2338     default:
2339         break;
2340     }
2341
2342     finish:
2343     
2344     free(cctemp);
2345     return rv;
2346 }
2347
2348 static void cleanup_tmp_dir(const char *dirname)
2349 {
2350     DIR *dir;
2351     struct dirent *entry;
2352     char fullname[1024];
2353
2354     dir = opendir(dirname);
2355
2356     if (dir == NULL)
2357         return;
2358
2359     if ((strlen(dirname) + 1 + entry->d_namlen) >= sizeof(fullname)) {
2360           fprintf(stderr, "Dirname too long out of buffer space");
2361           return;
2362     }
2363
2364     while ((entry = readdir(dir)) != NULL) {
2365         if (entry->d_name[0] != '.') {
2366             strcpy(fullname, dirname);
2367             strcat(fullname, "/");
2368             strcat(fullname, entry->d_name);
2369             (void) remove(fullname);
2370         }
2371     }
2372
2373     rmdir(dirname);
2374     
2375     (void) closedir(dir);
2376 }
2377
2378 static void cleanup_tmp_dirs(command_t *cmd_data)
2379 {
2380     int d;
2381
2382     for (d = 0; d < cmd_data->tmp_dirs->num; d++) {
2383         cleanup_tmp_dir(cmd_data->tmp_dirs->vals[d]);
2384     }
2385 }
2386
2387 static int ensure_fake_uptodate(command_t *cmd_data)
2388 {
2389     /* FIXME: could do the stat/touch here, but nah... */
2390     const char *touch_args[3];
2391
2392     if (cmd_data->mode == mInstall) {
2393         return 0;
2394     }
2395     if (!cmd_data->fake_output_name) {
2396         return 0;
2397     }
2398
2399     touch_args[0] = "touch";
2400     touch_args[1] = cmd_data->fake_output_name;
2401     touch_args[2] = NULL;
2402     return external_spawn(cmd_data, "touch", touch_args);
2403 }
2404
2405 /* Store the install path in the *.la file */
2406 static int add_for_runtime(command_t *cmd_data)
2407 {
2408     if (cmd_data->mode == mInstall) {
2409         return 0;
2410     }
2411     if (cmd_data->output == otDynamicLibraryOnly ||
2412         cmd_data->output == otLibrary) {
2413         FILE *f=fopen(cmd_data->fake_output_name,"w");
2414         if (f == NULL) {
2415             return -1;
2416         }
2417         fprintf(f,"%s\n", cmd_data->install_path);
2418         fclose(f);
2419         return(0);
2420     } else {
2421         return(ensure_fake_uptodate(cmd_data));
2422     }
2423 }
2424
2425 int main(int argc, char *argv[])
2426 {
2427     int rc;
2428     command_t cmd_data;
2429
2430     memset(&cmd_data, 0, sizeof(cmd_data));
2431
2432     cmd_data.options.pic_mode = pic_UNKNOWN;
2433
2434     cmd_data.program_opts = (count_chars*)malloc(sizeof(count_chars));
2435     init_count_chars(cmd_data.program_opts);
2436     cmd_data.arglist = (count_chars*)malloc(sizeof(count_chars));
2437     init_count_chars(cmd_data.arglist);
2438     cmd_data.tmp_dirs = (count_chars*)malloc(sizeof(count_chars));
2439     init_count_chars(cmd_data.tmp_dirs);
2440     cmd_data.obj_files = (count_chars*)malloc(sizeof(count_chars));
2441     init_count_chars(cmd_data.obj_files);
2442     cmd_data.dep_rpaths = (count_chars*)malloc(sizeof(count_chars));
2443     init_count_chars(cmd_data.dep_rpaths);
2444     cmd_data.rpaths = (count_chars*)malloc(sizeof(count_chars));
2445     init_count_chars(cmd_data.rpaths);
2446     cmd_data.static_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2447     init_count_chars(cmd_data.static_opts.normal);
2448     cmd_data.shared_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2449     init_count_chars(cmd_data.shared_opts.normal);
2450     cmd_data.shared_opts.dependencies = (count_chars*)malloc(sizeof(count_chars));
2451     init_count_chars(cmd_data.shared_opts.dependencies);
2452
2453     cmd_data.mode = mUnknown;
2454     cmd_data.output = otGeneral;
2455
2456     parse_args(argc, argv, &cmd_data);
2457     post_parse_fixup(&cmd_data);
2458
2459     if (cmd_data.mode == mUnknown) {
2460        usage(1);
2461     }
2462
2463     rc = run_mode(&cmd_data);
2464
2465     if (!rc) {
2466        add_for_runtime(&cmd_data);
2467     }
2468
2469     cleanup_tmp_dirs(&cmd_data);
2470     return rc;
2471 }