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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <sys/types.h>
24 #if !defined(__MINGW32__)
25 # include <sys/wait.h>
34 # define SHELL_CMD "sh"
35 # define GEN_EXPORTS "emxexp"
36 # define DEF2IMPLIB_CMD "emximp"
37 # define SHARE_SW "-Zdll -Zmtd"
39 # define TRUNCATE_DLL_NAME
40 # define DYNAMIC_LIB_EXT "dll"
41 # define EXE_EX ".exe"
42 /* OMF is the native format under OS/2 */
45 # define STATIC_LIB_EXT "lib"
46 # define OBJECT_EXT "obj"
47 # define LIBRARIAN "emxomfar"
48 # define LIBRARIAN_OPTS "cr"
50 /* but the alternative, a.out, can fork() which is sometimes necessary */
51 # define STATIC_LIB_EXT "a"
52 # define OBJECT_EXT "o"
53 # define LIBRARIAN "ar"
54 # define LIBRARIAN_OPTS "cr"
58 #if defined(__APPLE__)
59 # define SHELL_CMD "/bin/sh"
60 # define DYNAMIC_LIB_EXT "dylib"
61 # define MODULE_LIB_EXT "bundle"
62 # define STATIC_LIB_EXT "a"
63 # define OBJECT_EXT "o"
64 # define LIBRARIAN "ar"
65 # define LIBRARIAN_OPTS "cr"
66 /* man libtool(1) documents ranlib option of -c. */
67 # define RANLIB "ranlib"
68 # define PIC_FLAG "-fPIC -fno-common"
69 # define SHARED_OPTS "-dynamiclib"
70 # define MODULE_OPTS "-bundle -dynamic"
71 # define DYNAMIC_LINK_OPTS "-flat_namespace"
72 # define DYNAMIC_LINK_UNDEFINED "-undefined suppress"
73 # define dynamic_link_version_func darwin_dynamic_link_function
74 # define DYNAMIC_INSTALL_NAME "-install_name"
75 # define DYNAMIC_LINK_NO_INSTALL "-dylib_file"
77 /*-install_name /Users/jerenk/apache-2.0-cvs/lib/libapr.0.dylib -compatibility_version 1 -current_version 1.0 */
78 # define LD_LIBRARY_PATH "DYLD_LIBRARY_PATH"
79 # define LD_LIBRARY_PATH_LOCAL "DYLD_FALLBACK_LIBRARY_PATH"
82 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__sun) && defined(__GNUC__))
83 # define SHELL_CMD "/bin/sh"
84 # define DYNAMIC_LIB_EXT "so"
85 # define MODULE_LIB_EXT "so"
86 # define STATIC_LIB_EXT "a"
87 # define OBJECT_EXT "o"
88 # define LIBRARIAN "ar"
89 # define LIBRARIAN_OPTS "cr"
90 # define RANLIB "ranlib"
91 # define PIC_FLAG "-fPIC"
92 # define RPATH "-rpath"
93 # define SHARED_OPTS "-shared"
94 # define MODULE_OPTS "-shared"
95 # define LINKER_FLAG_PREFIX "-Wl,"
97 # define DYNAMIC_LINK_OPTS LINKER_FLAG_PREFIX "-export-dynamic"
99 # define DYNAMIC_LINK_OPTS ""
102 # define LD_RUN_PATH "LD_RUN_PATH"
103 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
104 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
107 #if defined(__sun) && !defined(__GNUC__)
108 # define SHELL_CMD "/bin/sh"
109 # define DYNAMIC_LIB_EXT "so"
110 # define MODULE_LIB_EXT "so"
111 # define STATIC_LIB_EXT "a"
112 # define OBJECT_EXT "o"
113 # define LIBRARIAN "ar"
114 # define LIBRARIAN_OPTS "cr"
115 # define RANLIB "ranlib"
116 # define PIC_FLAG "-fPIC"
118 # define SHARED_OPTS "-G"
119 # define MODULE_OPTS "-G"
120 # define DYNAMIC_LINK_OPTS ""
121 # define LINKER_FLAG_NO_EQUALS
123 # define HAS_REALPATH
124 # define LD_RUN_PATH "LD_RUN_PATH"
125 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
126 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
129 #if defined(_OSD_POSIX)
130 # define SHELL_CMD "/usr/bin/sh"
131 # define DYNAMIC_LIB_EXT "so"
132 # define MODULE_LIB_EXT "so"
133 # define STATIC_LIB_EXT "a"
134 # define OBJECT_EXT "o"
135 # define LIBRARIAN "ar"
136 # define LIBRARIAN_OPTS "cr"
137 # define SHARED_OPTS "-G"
138 # define MODULE_OPTS "-G"
139 # define LINKER_FLAG_PREFIX "-Wl,"
140 # define NEED_SNPRINTF
143 #if defined(sinix) && defined(mips) && defined(__SNI_TARG_UNIX)
144 # define SHELL_CMD "/usr/bin/sh"
145 # define DYNAMIC_LIB_EXT "so"
146 # define MODULE_LIB_EXT "so"
147 # define STATIC_LIB_EXT "a"
148 # define OBJECT_EXT "o"
149 # define LIBRARIAN "ar"
150 # define LIBRARIAN_OPTS "cr"
151 # define RPATH "-Brpath"
152 # define SHARED_OPTS "-G"
153 # define MODULE_OPTS "-G"
154 # define LINKER_FLAG_PREFIX "-Wl,"
155 # define DYNAMIC_LINK_OPTS LINKER_FLAG_PREFIX "-Blargedynsym"
157 # define NEED_SNPRINTF
158 # define LD_RUN_PATH "LD_RUN_PATH"
159 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
160 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
163 #if defined(__MINGW32__)
164 # define SHELL_CMD "sh"
165 # define DYNAMIC_LIB_EXT "dll"
166 # define MODULE_LIB_EXT "dll"
167 # define STATIC_LIB_EXT "a"
168 # define OBJECT_EXT "o"
169 # define LIBRARIAN "ar"
170 # define LIBRARIAN_OPTS "cr"
171 # define RANLIB "ranlib"
172 # define LINKER_FLAG_PREFIX "-Wl,"
173 # define SHARED_OPTS "-shared"
174 # define MODULE_OPTS "-shared"
175 # define MKDIR_NO_UMASK
176 # define EXE_EXT ".exe"
192 #define LINK_CXX "g++"
196 #define LIBDIR "/usr/local/lib"
199 #define OBJDIR ".libs"
202 #error Unsupported platform: Please add defines for SHELL_CMD etc. for your platform.
214 #define PATH_MAX 1024
218 /* We want to say we are libtool 1.4 for shlibtool compatibility. */
219 #define VERSION "1.4"
221 #define DEBUG(fmt, ...) if(cmd->options.debug) printf(fmt, ## __VA_ARGS__)
222 #define NOTICE(fmt, ...) if(!cmd->options.silent) printf(fmt, ## __VA_ARGS__)
223 #define ERROR(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
239 OUT_DYNAMIC_LIB_ONLY,
275 count_chars *install;
276 count_chars *dependencies;
282 enum shared_mode shared;
285 enum pic_mode pic_mode;
292 enum output_type output;
295 char const *output_name;
296 char const *fake_output_name;
297 char const *basename;
299 char const *install_path;
300 char const *compiler;
302 count_chars *program_opts;
304 count_chars *arglist;
305 count_chars *tmp_dirs;
306 count_chars *obj_files;
307 count_chars *dep_rpaths;
310 library_name static_name;
311 library_name shared_name;
312 library_name module_name;
314 library_opts static_opts;
315 library_opts shared_opts;
317 char const *version_info;
318 char const *undefined_flag;
322 static void add_rpath(count_chars *cc, char const *path);
325 static void usage(int code)
327 printf("Usage: jlibtool [OPTIONS...] COMMANDS...\n");
328 printf("jlibtool is a replacement for GNU libtool with similar functionality.\n\n");
330 printf(" --config show all configuration variables\n");
331 printf(" --debug enable verbose shell tracing\n");
332 printf(" --dry-run display commands without modifying any files\n");
333 printf(" --help display this help message and exit\n");
334 printf(" --mode=MODE use operational mode MODE (you *must* set mode)\n");
336 printf(" --silent don't print informational messages\n");
337 printf(" --tag=TAG Ignored for libtool compatibility\n");
338 printf(" --version print version information\n");
341 printf(" --shared Build shared libraries when using --mode=link\n");
342 printf(" --export-all Try to export 'def' file on some platforms\n");
344 printf("\nMODE must be one of the following:\n\n");
345 printf(" compile compile a source file into a jlibtool object\n");
346 printf(" execute automatically set library path, then run a program\n");
347 printf(" install install libraries or executables\n");
348 printf(" link create a library or an executable\n");
350 printf("\nMODE-ARGS can be the following:\n\n");
351 printf(" -export-dynamic accepted and ignored\n");
352 printf(" -module create a module when linking\n");
353 printf(" -shared create a shared library when linking\n");
354 printf(" -prefer-pic prefer position-independent-code when compiling\n");
355 printf(" -prefer-non-pic prefer non position-independent-code when compiling\n");
356 printf(" -static create a static library when linking\n");
357 printf(" -no-install link libraries locally\n");
358 printf(" -rpath arg Set install path for shared libraries\n");
359 printf(" -l arg pass '-l arg' to the link stage\n");
360 printf(" -L arg pass '-L arg' to the link stage\n");
361 printf(" -R dir add 'dir' to runtime library search path.\n");
362 printf(" -Zexe accepted and ignored\n");
363 printf(" -avoid-version accepted and ignored\n");
368 #if defined(NEED_SNPRINTF)
369 /* Write at most n characters to the buffer in str, return the
370 * number of chars written or -1 if the buffer would have been
373 * This is portable to any POSIX-compliant system has /dev/null
375 static FILE *f = NULL;
376 static int vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
381 f = fopen("/dev/null","w");
388 setvbuf(f, str, _IOFBF, n);
390 res = vfprintf(f, fmt, ap);
392 if ((res > 0) && (res < n)) {
393 res = vsprintf( str, fmt, ap );
398 static int snprintf(char *str, size_t n, char const *fmt, ...)
404 res = vsnprintf(str, n, fmt, ap);
410 static void *lt_malloc(size_t size)
416 ERROR("Failed allocating %zu bytes, OOM", size);
423 static void lt_const_free(const void *ptr)
427 memcpy(&tmp, &ptr, sizeof(tmp));
431 static void init_count_chars(count_chars *cc)
433 cc->vals = (char const**) lt_malloc(PATH_MAX*sizeof(char*));
437 static count_chars *alloc_countchars(void)
440 out = lt_malloc(sizeof(count_chars));
444 init_count_chars(out);
449 static void clear_count_chars(count_chars *cc)
452 for (i = 0; i < cc->num; i++) {
459 static void push_count_chars(count_chars *cc, char const *newval)
461 cc->vals[cc->num++] = newval;
464 static char const *pop_count_chars(count_chars *cc)
469 return cc->vals[--cc->num];
472 static void insert_count_chars(count_chars *cc, char const *newval, int position)
476 for (i = cc->num; i > position; i--) {
477 cc->vals[i] = cc->vals[i-1];
480 cc->vals[position] = newval;
484 static void append_count_chars(count_chars *cc, count_chars *cctoadd)
487 for (i = 0; i < cctoadd->num; i++) {
488 if (cctoadd->vals[i]) {
489 push_count_chars(cc, cctoadd->vals[i]);
494 static char const *flatten_count_chars(count_chars *cc, char delim)
500 for (i = 0; i < cc->num; i++) {
502 size += strlen(cc->vals[i]) + 1;
509 newval = (char*)lt_malloc(size + 1);
512 for (i = 0; i < cc->num; i++) {
514 strcat(newval, cc->vals[i]);
516 size_t len = strlen(newval);
518 newval[len + 1] = '\0';
526 static char *shell_esc(char const *str)
533 cmd = (char *)lt_malloc(2 * strlen(str) + 3);
534 d = (unsigned char *)cmd;
535 s = (const unsigned char *)str;
546 else if (*s == '\\' || (*s == ' ' && (in_quote % 2))) {
560 static int external_spawn(command_t *cmd, char const *file, char const **argv)
562 file = file; /* -Wunused */
564 if (!cmd->options.silent) {
565 char const **argument = argv;
566 NOTICE("Executing: ");
568 NOTICE("%s ", *argument);
574 if (cmd->options.dry_run) {
577 #if defined(__EMX__) || defined(__MINGW32__)
578 return spawnvp(P_WAIT, argv[0], argv);
584 return execvp(argv[0], (char**)argv);
588 waitpid(pid, &status, 0);
591 * Exited via exit(status)
593 if (WIFEXITED(status)) {
594 return WEXITSTATUS(status);
598 if (WIFSIGNALED(status)) {
599 return WTERMSIG(status);
604 * Some other failure.
612 static int run_command(command_t *cmd, count_chars *cc)
618 char const *spawn_args[4];
621 init_count_chars(&tmpcc);
624 push_count_chars(&tmpcc, cmd->program);
627 append_count_chars(&tmpcc, cmd->program_opts);
629 append_count_chars(&tmpcc, cc);
631 raw = flatten_count_chars(&tmpcc, ' ');
632 command = shell_esc(raw);
634 memcpy(&tmp, &raw, sizeof(tmp));
637 spawn_args[0] = SHELL_CMD;
638 spawn_args[1] = "-c";
639 spawn_args[2] = command;
640 spawn_args[3] = NULL;
641 ret = external_spawn(cmd, spawn_args[0], spawn_args);
649 * print configuration
650 * shlibpath_var is used in configure.
652 #define printc(_x,_y) if (!value || !strcmp(value, _x)) printf(_x "=\"%s\"\n", _y)
654 static void print_config(char const *value)
657 printc("runpath_var", LD_RUN_PATH);
659 #ifdef LD_LIBRARY_PATH
660 printc("shlibpath_var", LD_LIBRARY_PATH);
662 #ifdef LD_LIBRARY_PATH_LOCAL
663 printc("shlocallibpath_var", LD_LIBRARY_PATH_LOCAL);
666 printc("SHELL", SHELL_CMD);
669 printc("objext", OBJECT_EXT);
672 printc("objdir", OBJDIR);
674 #ifdef DYNAMIC_LIB_EXT
675 /* add a '.' prefix because libtool does that. */
676 printc("shrext_cmds", "echo ." DYNAMIC_LIB_EXT);
677 /* add a '.' prefix because libtool does that. */
678 printc("shrext", "." DYNAMIC_LIB_EXT);
681 printc("exeext", EXE_EXT);
683 #ifdef STATIC_LIB_EXT
684 printc("libext", STATIC_LIB_EXT);
687 printc("AR", LIBRARIAN);
689 #ifdef LIBRARIAN_OPTS
690 printc("AR_FLAGS", LIBRARIAN_OPTS);
692 #ifdef LINKER_FLAG_PREFIX
693 printc("wl", LINKER_FLAG_PREFIX);
696 printc("ranlib", RANLIB);
701 * Add a directory to the runtime library search path.
703 static void add_runtime_dir_lib(char const *arg, command_t *cmd)
706 add_rpath(cmd->shared_opts.dependencies, arg);
708 (void) arg; /* -Wunused */
713 static int parse_long_opt(char const *arg, command_t *cmd)
715 char *equal_pos = strchr(arg, '=');
720 strncpy(var, arg, equal_pos - arg);
721 var[equal_pos - arg] = 0;
722 if (strlen(equal_pos + 1) >= sizeof(var)) {
725 strcpy(value, equal_pos + 1);
727 strncpy(var, arg, sizeof(var) - 1);
728 var[sizeof(var) - 1] = '\0';
733 if (strcmp(var, "silent") == 0) {
734 cmd->options.silent = 1;
735 } else if (strcmp(var, "quiet") == 0) {
736 cmd->options.silent = 1;
737 } else if (strcmp(var, "debug") == 0) {
738 cmd->options.debug = 1;
739 } else if (strcmp(var, "mode") == 0) {
740 if (cmd->mode != MODE_UNKNOWN) {
741 ERROR("Cannot set --mode twice\n");
745 if (strcmp(value, "compile") == 0) {
746 cmd->mode = MODE_COMPILE;
747 cmd->output = OUT_OBJECT;
749 } else if (strcmp(value, "link") == 0) {
750 cmd->mode = MODE_LINK;
751 cmd->output = OUT_LIB;
753 } else if (strcmp(value, "install") == 0) {
754 cmd->mode = MODE_INSTALL;
756 } else if (strcmp(value, "execute") == 0) {
757 cmd->mode = MODE_EXECUTE;
760 ERROR("Unknown mode \"%s\"\n", value);
764 } else if (strcmp(var, "shared") == 0) {
765 if ((cmd->mode == MODE_LINK) && (cmd->output == OUT_GENERAL)) {
766 cmd->output = OUT_DYNAMIC_LIB_ONLY;
768 cmd->options.shared = SHARE_SHARED;
770 } else if (strcmp(var, "export-all") == 0) {
771 cmd->options.export_all = 1;
773 } else if (strcmp(var, "dry-run") == 0) {
774 NOTICE("Dry-run mode on!\n");
775 cmd->options.dry_run = 1;
777 } else if (strcmp(var, "version") == 0) {
778 NOTICE("Version " VERSION "\n");
780 } else if (strcmp(var, "help") == 0) {
783 } else if (strcmp(var, "config") == 0) {
794 /* Return 1 if we eat it. */
795 static int parse_short_opt(char const *arg, command_t *cmd)
797 if (strcmp(arg, "export-dynamic") == 0) {
798 cmd->options.export_dynamic = 1;
802 if (strcmp(arg, "module") == 0) {
803 cmd->output = OUT_MODULE;
807 if (strcmp(arg, "shared") == 0) {
808 if (cmd->mode == MODE_LINK) {
809 cmd->output = OUT_DYNAMIC_LIB_ONLY;
811 cmd->options.shared = SHARE_SHARED;
815 if (strcmp(arg, "Zexe") == 0) {
819 if (strcmp(arg, "avoid-version") == 0) {
823 if (strcmp(arg, "prefer-pic") == 0) {
824 cmd->options.pic_mode = PIC_PREFER;
828 if (strcmp(arg, "prefer-non-pic") == 0) {
829 cmd->options.pic_mode = PIC_AVOID;
833 if (strcmp(arg, "static") == 0) {
834 if ((cmd->mode == MODE_LINK) && (cmd->output == OUT_LIB)) {
835 cmd->output = OUT_STATIC_LIB_ONLY;
837 cmd->options.shared = SHARE_STATIC;
841 if (cmd->mode == MODE_LINK) {
842 if (strcmp(arg, "no-install") == 0) {
843 cmd->options.no_install = 1;
846 if (arg[0] == 'L' || arg[0] == 'l') {
849 push_count_chars(cmd->shared_opts.dependencies, arg);
851 } else if (arg[0] == 'R' && arg[1]) {
852 /* -Rdir Add dir to runtime library search path. */
853 add_runtime_dir_lib(&arg[1], cmd);
860 #ifdef TRUNCATE_DLL_NAME
861 static char *truncate_dll_name(char *path)
863 /* Cut DLL name down to 8 characters after removing any mod_ prefix */
864 char *tmppath = strdup(path);
865 char *newname = strrchr(tmppath, '/') + 1;
866 char *ext = strrchr(newname, '.');
875 if (strncmp(newname, "mod_", 4) == 0) {
876 strcpy(newname, newname + 4);
881 strcpy(newname + 8, strchr(newname, '.'));
888 static long safe_strtol(char const *nptr, char const **endptr, int base)
894 rv = strtol(nptr, (char**)endptr, 10);
896 if (errno == ERANGE) {
903 static void safe_mkdir(command_t *cmd, char const *path)
908 old_umask = umask(0);
911 #ifdef MKDIR_NO_UMASK
912 status = mkdir(path);
914 status = mkdir(path, ~old_umask);
916 if ((status < 0) && (errno != EEXIST)) {
917 NOTICE("Warning: mkdir of %s failed\n", path);
921 /** Returns a file's name without the path
923 * @param path to break apart.
924 * @return pointer in path.
926 static char const *file_name(char const *path)
930 name = strrchr(path, '/');
932 name = strrchr(path, '\\'); /* eww windows? */
945 /** Returns a file's name without path or extension
947 * @param path to check
948 * @return pointer in path.
950 static char const *file_name_stripped(char const *path)
955 name = file_name(path);
956 ext = strrchr(name, '.');
961 trimmed = lt_malloc(ext - name + 1);
962 strncpy(trimmed, name, ext - name);
963 trimmed[ext-name] = 0;
972 /* version_info is in the form of MAJOR:MINOR:PATCH */
973 static char const *darwin_dynamic_link_function(char const *version_info)
976 long major, minor, patch;
983 major = safe_strtol(version_info, &version_info, 10);
986 if (version_info[0] == ':') {
990 minor = safe_strtol(version_info, &version_info, 10);
993 if (version_info[0] == ':') {
997 patch = safe_strtol(version_info, &version_info, 10);
1003 /* Avoid -dylib_compatibility_version must be greater than zero errors. */
1007 newarg = (char*)lt_malloc(100);
1008 snprintf(newarg, 99,
1009 "-compatibility_version %ld -current_version %ld.%ld",
1010 major, major, minor);
1017 * Add a '.libs/' to the buffer. The caller ensures that
1018 * The buffer is large enough to handle 6 extra characters.
1020 static void add_dotlibs(char *buffer)
1022 char *name = strrchr(buffer, '/');
1026 strcpy(buffer, ".libs/");
1033 memmove(name + 6, name, strlen(name));
1034 memcpy(name, ".libs/", 6);
1037 static char *gen_library_name(char const *name, enum lib_type genlib)
1039 char *newarg, *newext;
1041 newarg = (char *)calloc(strlen(name) + 11, 1);
1043 if (genlib == TYPE_MODULE_LIB && strncmp(name, "lib", 3) == 0) {
1047 if (genlib == TYPE_MODULE_LIB) {
1048 strcpy(newarg, file_name(name));
1051 strcpy(newarg, name);
1054 newext = strrchr(newarg, '.');
1056 ERROR("Library path does not have an extension");
1064 case TYPE_STATIC_LIB:
1065 strcpy(newext, STATIC_LIB_EXT);
1067 case TYPE_DYNAMIC_LIB:
1068 strcpy(newext, DYNAMIC_LIB_EXT);
1070 case TYPE_MODULE_LIB:
1071 strcpy(newext, MODULE_LIB_EXT);
1078 add_dotlibs(newarg);
1083 static char *gen_install_name(char const *name, enum lib_type genlib)
1089 newname = gen_library_name(name, genlib);
1090 if (!newname) return NULL;
1092 /* Check if it exists. If not, return NULL. */
1093 rv = stat(newname, &sb);
1103 static char const *check_object_exists(command_t *cmd, char const *arg, int arglen)
1108 newarg = (char *)lt_malloc(arglen + 10);
1109 memcpy(newarg, arg, arglen);
1111 ext = newarg + arglen;
1113 strcpy(ext, OBJECT_EXT);
1115 DEBUG("Checking (obj): %s\n", newarg);
1116 if (stat(newarg, &sb) == 0) {
1125 /* libdircheck values:
1126 * 0 - no .libs suffix
1129 static char *check_library_exists(command_t *cmd, char const *arg, int pathlen,
1130 int libdircheck, enum lib_type *libtype)
1133 int pass, rv, newpathlen;
1135 newarg = (char *)lt_malloc(strlen(arg) + 10);
1136 strcpy(newarg, arg);
1137 newarg[pathlen] = '\0';
1139 newpathlen = pathlen;
1141 add_dotlibs(newarg);
1142 newpathlen += sizeof(".libs/") - 1;
1145 strcpy(newarg + newpathlen, arg + pathlen);
1146 ext = strrchr(newarg, '.');
1148 ERROR("Error: Library path does not have an extension");
1162 if (cmd->options.pic_mode != PIC_AVOID &&
1163 cmd->options.shared != SHARE_STATIC) {
1164 strcpy(ext, DYNAMIC_LIB_EXT);
1165 *libtype = TYPE_DYNAMIC_LIB;
1171 strcpy(ext, STATIC_LIB_EXT);
1172 *libtype = TYPE_STATIC_LIB;
1175 strcpy(ext, MODULE_LIB_EXT);
1176 *libtype = TYPE_MODULE_LIB;
1179 strcpy(ext, OBJECT_EXT);
1180 *libtype = TYPE_OBJECT;
1183 *libtype = TYPE_UKNOWN;
1187 DEBUG("Checking (lib): %s\n", newarg);
1188 rv = stat(newarg, &sb);
1190 while (rv != 0 && ++pass < 4);
1201 static char * load_install_path(char const *arg)
1211 path = lt_malloc(PATH_MAX);
1213 fgets(path, PATH_MAX, f);
1216 if (path[strlen(path)-1] == '\n') {
1217 path[strlen(path)-1] = '\0';
1220 /* Check that we have an absolute path.
1221 * Otherwise the file could be a GNU libtool file.
1223 if (path[0] != '/') {
1231 static char *load_noinstall_path(char const *arg, int pathlen)
1233 char *newarg, *expanded_path;
1236 newarg = (char *)lt_malloc(strlen(arg) + 10);
1237 strcpy(newarg, arg);
1238 newarg[pathlen] = 0;
1240 newpathlen = pathlen;
1241 strcat(newarg, ".libs");
1242 newpathlen += sizeof(".libs") - 1;
1243 newarg[newpathlen] = 0;
1246 expanded_path = lt_malloc(PATH_MAX);
1247 expanded_path = realpath(newarg, expanded_path);
1248 /* Uh, oh. There was an error. Fall back on our first guess. */
1249 if (!expanded_path) {
1250 expanded_path = newarg;
1253 /* We might get ../ or something goofy. Oh, well. */
1254 expanded_path = newarg;
1257 return expanded_path;
1260 static void add_dynamic_link_opts(command_t *cmd, count_chars *args)
1262 #ifdef DYNAMIC_LINK_OPTS
1263 if (cmd->options.pic_mode != PIC_AVOID) {
1264 DEBUG("Adding linker opt: %s\n", DYNAMIC_LINK_OPTS);
1266 push_count_chars(args, DYNAMIC_LINK_OPTS);
1267 if (cmd->undefined_flag) {
1268 push_count_chars(args, "-undefined");
1269 #if defined(__APPLE__)
1270 /* -undefined dynamic_lookup is used by the bundled Python in
1271 * 10.4, but if we don't set MACOSX_DEPLOYMENT_TARGET to 10.3+,
1272 * we'll get a linker error if we pass this flag.
1274 if (strcasecmp(cmd->undefined_flag, "dynamic_lookup") == 0) {
1275 insert_count_chars(cmd->program_opts, "MACOSX_DEPLOYMENT_TARGET=10.3", 0);
1278 push_count_chars(args, cmd->undefined_flag);
1281 #ifdef DYNAMIC_LINK_UNDEFINED
1282 DEBUG("Adding linker opt: %s\n", DYNAMIC_LINK_UNDEFINED);
1284 push_count_chars(args, DYNAMIC_LINK_UNDEFINED);
1291 /* Read the final install location and add it to runtime library search path. */
1293 static void add_rpath(count_chars *cc, char const *path)
1298 #ifdef LINKER_FLAG_PREFIX
1299 size = strlen(LINKER_FLAG_PREFIX);
1301 size = size + strlen(path) + strlen(RPATH) + 2;
1302 tmp = lt_malloc(size);
1304 #ifdef LINKER_FLAG_PREFIX
1305 strcpy(tmp, LINKER_FLAG_PREFIX);
1310 #ifndef LINKER_FLAG_NO_EQUALS
1315 push_count_chars(cc, tmp);
1318 static void add_rpath_file(count_chars *cc, char const *arg)
1322 path = load_install_path(arg);
1324 add_rpath(cc, path);
1325 lt_const_free(path);
1329 static void add_rpath_noinstall(count_chars *cc, char const *arg, int pathlen)
1333 path = load_noinstall_path(arg, pathlen);
1335 add_rpath(cc, path);
1336 lt_const_free(path);
1341 #ifdef DYNAMIC_LINK_NO_INSTALL
1342 static void add_dylink_noinstall(count_chars *cc, char const *arg, int pathlen,
1345 char const *install_path, *current_path, *name;
1347 int i_p_len, c_p_len, name_len, dyext_len, cur_len;
1349 install_path = load_install_path(arg);
1350 current_path = load_noinstall_path(arg, pathlen);
1352 if (!install_path || !current_path) {
1356 push_count_chars(cc, DYNAMIC_LINK_NO_INSTALL);
1358 i_p_len = strlen(install_path);
1359 c_p_len = strlen(current_path);
1362 name_len = extlen-pathlen;
1363 dyext_len = sizeof(DYNAMIC_LIB_EXT) - 1;
1365 /* No, we need to replace the extension. */
1366 exp_argument = (char *)lt_malloc(i_p_len + c_p_len + (name_len*2) +
1370 strcpy(exp_argument, install_path);
1372 exp_argument[cur_len++] = '/';
1373 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1374 cur_len += name_len;
1375 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1376 cur_len += dyext_len;
1377 exp_argument[cur_len++] = ':';
1378 strcpy(exp_argument+cur_len, current_path);
1380 exp_argument[cur_len++] = '/';
1381 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1382 cur_len += name_len;
1383 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1384 cur_len += dyext_len;
1386 push_count_chars(cc, exp_argument);
1391 /* use -L -llibname to allow to use installed libraries */
1392 static void add_minus_l(count_chars *cc, char const *arg)
1395 char *name = strrchr(arg, '/');
1396 char *file = strrchr(arg, '.');
1398 if ((name != NULL) && (file != NULL) &&
1399 (strstr(name, "lib") == (name + 1))) {
1404 push_count_chars(cc, "-L");
1405 push_count_chars(cc, arg);
1406 /* we need one argument like -lapr-1 */
1407 newarg = lt_malloc(strlen(file) + 3);
1408 strcpy(newarg, "-l");
1409 strcat(newarg, file);
1410 push_count_chars(cc, newarg);
1412 push_count_chars(cc, arg);
1418 static void add_linker_flag_prefix(count_chars *cc, char const *arg)
1420 #ifndef LINKER_FLAG_PREFIX
1421 push_count_chars(cc, arg);
1424 newarg = (char*)lt_malloc(strlen(arg) + sizeof(LINKER_FLAG_PREFIX) + 1);
1425 strcpy(newarg, LINKER_FLAG_PREFIX);
1426 strcat(newarg, arg);
1427 push_count_chars(cc, newarg);
1432 static int explode_static_lib(command_t *cmd, char const *lib)
1434 count_chars tmpdir_cc, libname_cc;
1435 char const *tmpdir, *libname;
1436 char savewd[PATH_MAX];
1439 struct dirent *entry;
1440 char const *lib_args[4];
1443 if (cmd->options.dry_run) {
1447 name = file_name(lib);
1449 init_count_chars(&tmpdir_cc);
1450 push_count_chars(&tmpdir_cc, ".libs/");
1451 push_count_chars(&tmpdir_cc, name);
1452 push_count_chars(&tmpdir_cc, ".exploded/");
1453 tmpdir = flatten_count_chars(&tmpdir_cc, 0);
1455 NOTICE("Making: %s\n", tmpdir);
1457 safe_mkdir(cmd, tmpdir);
1459 push_count_chars(cmd->tmp_dirs, tmpdir);
1461 getcwd(savewd, sizeof(savewd));
1463 if (chdir(tmpdir) != 0) {
1464 NOTICE("Warning: could not explode %s\n", lib);
1469 if (lib[0] == '/') {
1473 init_count_chars(&libname_cc);
1474 push_count_chars(&libname_cc, "../../");
1475 push_count_chars(&libname_cc, lib);
1476 libname = flatten_count_chars(&libname_cc, 0);
1479 lib_args[0] = LIBRARIAN;
1481 lib_args[2] = libname;
1484 external_spawn(cmd, LIBRARIAN, lib_args);
1487 dir = opendir(tmpdir);
1489 while ((entry = readdir(dir)) != NULL) {
1490 #if defined(__APPLE__) && defined(RANLIB)
1491 /* Apple inserts __.SYMDEF which isn't needed.
1492 * Leopard (10.5+) can also add '__.SYMDEF SORTED' which isn't
1493 * much fun either. Just skip them.
1495 if (strstr(entry->d_name, "__.SYMDEF") != NULL) {
1499 if (entry->d_name[0] != '.') {
1500 push_count_chars(&tmpdir_cc, entry->d_name);
1501 name = flatten_count_chars(&tmpdir_cc, 0);
1503 DEBUG("Adding object: %s\n", name);
1504 push_count_chars(cmd->obj_files, name);
1505 pop_count_chars(&tmpdir_cc);
1513 static int parse_input_file_name(char const *arg, command_t *cmd)
1515 char const *ext = strrchr(arg, '.');
1518 enum lib_type libtype;
1521 /* Can't guess the extension */
1527 name = file_name(arg);
1528 pathlen = name - arg;
1531 * Were linking and have an archived object or object file
1532 * push it onto the list of object files which'll get used
1533 * to create the input files list for the linker.
1535 * We assume that these are outside of the project were building,
1536 * as there's no reason to create .a files as part of the build
1539 if (!strcmp(ext, STATIC_LIB_EXT) && (cmd->mode == MODE_LINK)) {
1542 if (!stat(arg, &sb)) {
1543 DEBUG("Adding object: %s\n", arg);
1545 push_count_chars(cmd->obj_files, arg);
1552 * More object files, if were linking they get set as input
1555 if (!strcmp(ext, "lo") || !strcmp(ext, OBJECT_EXT)) {
1556 newarg = check_object_exists(cmd, arg, ext - arg);
1558 ERROR("Can not find suitable object file for %s\n", arg);
1562 if (cmd->mode == MODE_LINK) {
1563 DEBUG("Adding object: %s\n", newarg);
1565 push_count_chars(cmd->obj_files, newarg);
1567 push_count_chars(cmd->arglist, newarg);
1573 if (!strcmp(ext, "la")) {
1574 switch (cmd->mode) {
1576 /* Try the .libs dir first! */
1577 newarg = check_library_exists(cmd, arg, pathlen, 1, &libtype);
1579 /* Try the normal dir next. */
1580 newarg = check_library_exists(cmd, arg, pathlen, 0, &libtype);
1582 ERROR("Can not find suitable library for %s\n", arg);
1587 /* It is not ok to just add the file: a library may added with:
1588 1 - -L path library_name. (For *.so in Linux).
1592 if (libtype == TYPE_DYNAMIC_LIB) {
1593 add_minus_l(cmd->shared_opts.dependencies, newarg);
1594 } else if (cmd->output == OUT_LIB &&
1595 libtype == TYPE_STATIC_LIB) {
1596 explode_static_lib(cmd, newarg);
1598 push_count_chars(cmd->shared_opts.dependencies, newarg);
1601 if (cmd->output == OUT_LIB && libtype == TYPE_STATIC_LIB) {
1602 explode_static_lib(cmd, newarg);
1605 push_count_chars(cmd->shared_opts.dependencies, newarg);
1608 if (libtype == TYPE_DYNAMIC_LIB) {
1609 if (cmd->options.no_install) {
1611 add_rpath_noinstall(cmd->shared_opts.dependencies,
1617 add_rpath_file(cmd->shared_opts.dependencies, arg);
1624 * If we've already recorded a library to
1625 * install, we're most likely getting the .la
1626 * file that we want to install as.
1628 * The problem is that we need to add it as the
1629 * directory, not the .la file itself.
1630 * Otherwise, we'll do odd things.
1632 if (cmd->output == OUT_LIB) {
1636 tmp[pathlen] = '\0';
1637 push_count_chars(cmd->arglist, tmp);
1640 cmd->output = OUT_LIB;
1641 cmd->output_name = arg;
1642 cmd->static_name.install = gen_install_name(arg, 0);
1643 cmd->shared_name.install = gen_install_name(arg, 1);
1644 cmd->module_name.install = gen_install_name(arg, 2);
1646 if (!cmd->static_name.install &&
1647 !cmd->shared_name.install &&
1648 !cmd->module_name.install) {
1649 ERROR("Files to install do not exist\n");
1662 if (!strcmp(ext, "c")) {
1663 /* If we don't already have an idea what our output name will be. */
1664 if (!cmd->basename) {
1665 char *tmp = lt_malloc(strlen(arg) + 4);
1667 strcpy(strrchr(tmp, '.') + 1, "lo");
1669 cmd->basename = tmp;
1671 cmd->fake_output_name = strrchr(cmd->basename, '/');
1672 if (cmd->fake_output_name) {
1673 cmd->fake_output_name++;
1675 cmd->fake_output_name = cmd->basename;
1683 static int parse_output_file_name(char const *arg, command_t *cmd)
1687 char *newarg = NULL;
1690 cmd->fake_output_name = arg;
1692 name = file_name(arg);
1693 ext = strrchr(name, '.');
1696 if (!ext || strcmp(ext, EXE_EXT) == 0) {
1700 cmd->basename = arg;
1701 cmd->output = OUT_PROGRAM;
1702 #if defined(_OSD_POSIX)
1703 cmd->options.pic_mode = PIC_AVOID;
1705 newarg = (char *)lt_malloc(strlen(arg) + 5);
1706 strcpy(newarg, arg);
1709 strcat(newarg, EXE_EXT);
1712 cmd->output_name = newarg;
1717 pathlen = name - arg;
1719 if (strcmp(ext, "la") == 0) {
1720 assert(cmd->mode == MODE_LINK);
1722 cmd->basename = arg;
1723 cmd->static_name.normal = gen_library_name(arg, TYPE_STATIC_LIB);
1724 cmd->shared_name.normal = gen_library_name(arg, TYPE_DYNAMIC_LIB);
1725 cmd->module_name.normal = gen_library_name(arg, TYPE_MODULE_LIB);
1726 cmd->static_name.install = gen_install_name(arg, TYPE_STATIC_LIB);
1727 cmd->shared_name.install = gen_install_name(arg, TYPE_DYNAMIC_LIB);
1728 cmd->module_name.install = gen_install_name(arg, TYPE_MODULE_LIB);
1730 if (!cmd->options.dry_run) {
1733 newname = lt_malloc(strlen(cmd->static_name.normal) + 1);
1735 strcpy(newname, cmd->static_name.normal);
1736 newext = strrchr(newname, '/');
1738 /* Check first to see if the dir already exists! */
1739 safe_mkdir(cmd, ".libs");
1742 safe_mkdir(cmd, newname);
1747 #ifdef TRUNCATE_DLL_NAME
1749 arg = truncate_dll_name(arg);
1753 cmd->output_name = arg;
1757 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1758 assert(cmd->mode == MODE_LINK);
1760 cmd->basename = arg;
1761 cmd->options.shared = SHARE_STATIC;
1762 cmd->output = OUT_STATIC_LIB_ONLY;
1763 cmd->static_name.normal = gen_library_name(arg, TYPE_STATIC_LIB);
1764 cmd->static_name.install = gen_install_name(arg, TYPE_STATIC_LIB);
1766 if (!cmd->options.dry_run) {
1769 newname = lt_malloc(strlen(cmd->static_name.normal) + 1);
1771 strcpy(newname, cmd->static_name.normal);
1772 newext = strrchr(newname, '/');
1774 /* Check first to see if the dir already exists! */
1775 safe_mkdir(cmd, ".libs");
1778 safe_mkdir(cmd, newname);
1783 cmd->output_name = arg;
1787 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1788 assert(cmd->mode == MODE_LINK);
1790 cmd->basename = arg;
1791 cmd->options.shared = SHARE_SHARED;
1792 cmd->output = OUT_DYNAMIC_LIB_ONLY;
1793 cmd->shared_name.normal = gen_library_name(arg, TYPE_DYNAMIC_LIB);
1794 cmd->module_name.normal = gen_library_name(arg, TYPE_MODULE_LIB);
1795 cmd->shared_name.install = gen_install_name(arg, TYPE_DYNAMIC_LIB);
1796 cmd->module_name.install = gen_install_name(arg, TYPE_MODULE_LIB);
1798 if (!cmd->options.dry_run) {
1801 newname = lt_malloc(strlen(cmd->shared_name.normal) + 1);
1803 strcpy(newname, cmd->shared_name.normal);
1804 newext = strrchr(newname, '/');
1806 /* Check first to see if the dir already exists! */
1807 safe_mkdir(cmd, ".libs");
1810 safe_mkdir(cmd, newname);
1815 cmd->output_name = arg;
1819 if (strcmp(ext, "lo") == 0) {
1821 cmd->basename = arg;
1822 cmd->output = OUT_OBJECT;
1823 newarg = (char *)lt_malloc(strlen(arg) + 2);
1824 strcpy(newarg, arg);
1825 newext = strrchr(newarg, '.') + 1;
1826 strcpy(newext, OBJECT_EXT);
1827 cmd->output_name = newarg;
1831 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1832 ERROR("Please build libraries with .la target, not ."
1833 DYNAMIC_LIB_EXT "\n");
1838 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1839 ERROR("Please build libraries with .la target, not ."
1840 STATIC_LIB_EXT "\n");
1848 static char const *automode(char const *arg, command_t *cmd)
1850 if (cmd->mode != MODE_UNKNOWN) return arg;
1852 if (!strcmp(arg, "CC") ||
1853 !strcmp(arg, "CXX")) {
1854 DEBUG("Now in compile mode, guessed from: %s\n", arg);
1856 cmd->mode = MODE_COMPILE;
1858 } else if (!strcmp(arg, "LINK") ||
1859 !strcmp(arg, "LINK.c") ||
1860 !strcmp(arg, "LINK.cxx")) {
1861 DEBUG("Now in linker mode, guessed from: %s\n", arg);
1863 cmd->mode = MODE_LINK;
1871 static void generate_def_file(command_t *cmd)
1873 char def_file[1024];
1874 char implib_file[1024];
1877 char *export_args[1024];
1878 int num_export_args = 0;
1883 if (cmd->output_name) {
1884 strcpy(def_file, cmd->output_name);
1885 strcat(def_file, ".def");
1886 hDef = fopen(def_file, "w");
1889 fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", file_name_stripped(cmd->output_name));
1890 fprintf(hDef, "DATA NONSHARED\n");
1891 fprintf(hDef, "EXPORTS\n");
1894 for (a = 0; a < cmd->num_obj_files; a++) {
1895 cmd_size += strlen(cmd->obj_files[a]) + 1;
1898 cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
1899 cmd = (char *)lt_malloc(cmd_size);
1900 strcpy(cmd, GEN_EXPORTS);
1902 for (a=0; a < cmd->num_obj_files; a++) {
1904 strcat(cmd, cmd->obj_files[a] );
1908 strcat(cmd, def_file);
1910 export_args[num_export_args++] = SHELL_CMD;
1911 export_args[num_export_args++] = "-c";
1912 export_args[num_export_args++] = cmd;
1913 export_args[num_export_args++] = NULL;
1914 external_spawn(cmd, export_args[0], (char const**)export_args);
1915 cmd->arglist[cmd->num_args++] = strdup(def_file);
1917 /* Now make an import library for the dll */
1918 num_export_args = 0;
1919 export_args[num_export_args++] = DEF2IMPLIB_CMD;
1920 export_args[num_export_args++] = "-o";
1922 strcpy(implib_file, ".libs/");
1923 strcat(implib_file, cmd->basename);
1924 ext = strrchr(implib_file, '.');
1930 strcat(implib_file, ".");
1931 strcat(implib_file, STATIC_LIB_EXT);
1933 export_args[num_export_args++] = implib_file;
1934 export_args[num_export_args++] = def_file;
1935 export_args[num_export_args++] = NULL;
1936 external_spawn(cmd, export_args[0], (char const**)export_args);
1944 static char const* expand_path(char const *relpath)
1946 char foo[PATH_MAX], *newpath;
1948 getcwd(foo, PATH_MAX-1);
1949 newpath = (char*)lt_malloc(strlen(foo)+strlen(relpath)+2);
1950 strcpy(newpath, foo);
1951 strcat(newpath, "/");
1952 strcat(newpath, relpath);
1957 static void link_fixup(command_t *cmd)
1959 /* If we were passed an -rpath directive, we need to build
1960 * shared objects too. Otherwise, we should only create static
1963 if (!cmd->install_path && (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
1964 cmd->output == OUT_MODULE || cmd->output == OUT_LIB)) {
1965 if (cmd->options.shared == SHARE_SHARED) {
1966 cmd->install_path = LIBDIR;
1970 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
1971 cmd->output == OUT_MODULE ||
1972 cmd->output == OUT_LIB) {
1974 push_count_chars(cmd->shared_opts.normal, "-o");
1975 if (cmd->output == OUT_MODULE) {
1976 push_count_chars(cmd->shared_opts.normal, cmd->module_name.normal);
1978 push_count_chars(cmd->shared_opts.normal, cmd->shared_name.normal);
1979 #ifdef DYNAMIC_INSTALL_NAME
1980 push_count_chars(cmd->shared_opts.normal, DYNAMIC_INSTALL_NAME);
1982 if (!cmd->install_path) {
1983 ERROR("Installation mode requires -rpath\n");
1988 char *tmp = lt_malloc(PATH_MAX);
1989 strcpy(tmp, cmd->install_path);
1991 if (cmd->shared_name.install) {
1992 strcat(tmp, strrchr(cmd->shared_name.install, '/'));
1994 strcat(tmp, strrchr(cmd->shared_name.normal, '/'));
1997 push_count_chars(cmd->shared_opts.normal, tmp);
2002 append_count_chars(cmd->shared_opts.normal, cmd->obj_files);
2003 append_count_chars(cmd->shared_opts.normal, cmd->shared_opts.dependencies);
2005 if (cmd->options.export_all) {
2007 generate_def_file(cmd);
2012 if (cmd->output == OUT_LIB || cmd->output == OUT_STATIC_LIB_ONLY) {
2013 push_count_chars(cmd->static_opts.normal, "-o");
2014 push_count_chars(cmd->static_opts.normal, cmd->output_name);
2017 if (cmd->output == OUT_PROGRAM) {
2018 if (cmd->output_name) {
2019 push_count_chars(cmd->arglist, "-o");
2020 push_count_chars(cmd->arglist, cmd->output_name);
2021 append_count_chars(cmd->arglist, cmd->obj_files);
2022 append_count_chars(cmd->arglist, cmd->shared_opts.dependencies);
2023 add_dynamic_link_opts(cmd, cmd->arglist);
2028 static void post_parse_fixup(command_t *cmd)
2030 switch (cmd->mode) {
2033 if (cmd->options.pic_mode != PIC_AVOID) {
2034 push_count_chars(cmd->arglist, PIC_FLAG);
2037 if (cmd->output_name) {
2038 push_count_chars(cmd->arglist, "-o");
2039 push_count_chars(cmd->arglist, cmd->output_name);
2046 if (cmd->output == OUT_LIB) {
2054 if (cmd->output == OUT_OBJECT ||
2055 cmd->output == OUT_PROGRAM ||
2056 cmd->output == OUT_LIB ||
2057 cmd->output == OUT_DYNAMIC_LIB_ONLY) {
2058 push_count_chars(cmd->arglist, "-Zomf");
2062 if (cmd->options.shared &&
2063 (cmd->output == OUT_OBJECT ||
2064 cmd->output == OUT_LIB ||
2065 cmd->output == OUT_DYNAMIC_LIB_ONLY)) {
2067 push_count_chars(cmd->arglist, SHARE_SW);
2072 static int run_mode(command_t *cmd)
2075 count_chars *cctemp;
2077 cctemp = (count_chars*)lt_malloc(sizeof(count_chars));
2078 init_count_chars(cctemp);
2080 switch (cmd->mode) {
2082 rv = run_command(cmd, cmd->arglist);
2083 if (rv) goto finish;
2086 /* Well, we'll assume it's a file going to a directory... */
2087 /* For brain-dead install-sh based scripts, we have to repeat
2088 * the command N-times. install-sh should die.
2090 if (!cmd->output_name) {
2091 rv = run_command(cmd, cmd->arglist);
2092 if (rv) goto finish;
2094 if (cmd->output_name) {
2095 append_count_chars(cctemp, cmd->arglist);
2096 insert_count_chars(cctemp,
2099 rv = run_command(cmd, cctemp);
2100 if (rv) goto finish;
2101 clear_count_chars(cctemp);
2103 if (cmd->static_name.install) {
2104 append_count_chars(cctemp, cmd->arglist);
2105 insert_count_chars(cctemp,
2106 cmd->static_name.install,
2108 rv = run_command(cmd, cctemp);
2109 if (rv) goto finish;
2110 #if defined(__APPLE__) && defined(RANLIB)
2111 /* From the Apple libtool(1) manpage on Tiger/10.4:
2113 * With the way libraries used to be created, errors were possible
2114 * if the library was modified with ar(1) and the table of
2115 * contents was not updated by rerunning ranlib(1). Thus the
2116 * link editor, ld, warns when the modification date of a library
2117 * is more recent than the creation date of its table of
2118 * contents. Unfortunately, this means that you get the warning
2119 * even if you only copy the library.
2122 * This means that when we install the static archive, we need to
2123 * rerun ranlib afterwards.
2125 char const *lib_args[3], *static_lib_name;
2131 len1 = strlen(cmd->arglist->vals[cmd->arglist->num - 1]);
2133 static_lib_name = file_name(cmd->static_name.install);
2134 len2 = strlen(static_lib_name);
2136 tmp = lt_malloc(len1 + len2 + 2);
2138 snprintf(tmp, len1 + len2 + 2, "%s/%s",
2139 cmd->arglist->vals[cmd->arglist->num - 1],
2142 lib_args[0] = RANLIB;
2146 external_spawn(cmd, RANLIB, lib_args);
2151 clear_count_chars(cctemp);
2153 if (cmd->shared_name.install) {
2154 append_count_chars(cctemp, cmd->arglist);
2155 insert_count_chars(cctemp, cmd->shared_name.install,
2157 rv = run_command(cmd, cctemp);
2158 if (rv) goto finish;
2159 clear_count_chars(cctemp);
2161 if (cmd->module_name.install) {
2162 append_count_chars(cctemp, cmd->arglist);
2163 insert_count_chars(cctemp, cmd->module_name.install,
2165 rv = run_command(cmd, cctemp);
2166 if (rv) goto finish;
2167 clear_count_chars(cctemp);
2171 if (cmd->output == OUT_STATIC_LIB_ONLY ||
2172 cmd->output == OUT_LIB) {
2174 char const *lib_args[3];
2176 /* Removes compiler! */
2177 cmd->program = LIBRARIAN;
2178 push_count_chars(cmd->program_opts, LIBRARIAN_OPTS);
2179 push_count_chars(cmd->program_opts, cmd->static_name.normal);
2181 rv = run_command(cmd, cmd->obj_files);
2182 if (rv) goto finish;
2185 lib_args[0] = RANLIB;
2186 lib_args[1] = cmd->static_name.normal;
2188 external_spawn(cmd, RANLIB, lib_args);
2192 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
2193 cmd->output == OUT_MODULE ||
2194 cmd->output == OUT_LIB) {
2195 cmd->program = NULL;
2196 clear_count_chars(cmd->program_opts);
2198 append_count_chars(cmd->program_opts, cmd->arglist);
2199 if (cmd->output == OUT_MODULE) {
2201 push_count_chars(cmd->program_opts, MODULE_OPTS);
2205 push_count_chars(cmd->program_opts, SHARED_OPTS);
2207 #ifdef dynamic_link_version_func
2208 push_count_chars(cmd->program_opts,
2209 dynamic_link_version_func(cmd->version_info));
2212 add_dynamic_link_opts(cmd, cmd->program_opts);
2214 rv = run_command(cmd, cmd->shared_opts.normal);
2215 if (rv) goto finish;
2217 if (cmd->output == OUT_PROGRAM) {
2218 rv = run_command(cmd, cmd->arglist);
2219 if (rv) goto finish;
2224 char *l, libpath[PATH_MAX];
2226 if (strlen(cmd->arglist->vals[0]) >= PATH_MAX) {
2227 ERROR("Libpath too long no buffer space");
2233 strcpy(libpath, cmd->arglist->vals[0]);
2234 add_dotlibs(libpath);
2235 l = strrchr(libpath, '/');
2236 if (!l) l = strrchr(libpath, '\\');
2244 l = "./build/lib/.libs";
2245 setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
2247 setenv("DYLD_FALLBACK_LIBRARY_PATH", l, 1);
2249 setenv("FR_LIBRARY_PATH", "./build/lib/local/.libs", 1);
2250 rv = run_command(cmd, cmd->arglist);
2251 if (rv) goto finish;
2265 static void cleanup_tmp_dir(char const *dirname)
2268 struct dirent *entry;
2269 char fullname[1024];
2271 dir = opendir(dirname);
2276 if ((strlen(dirname) + 1 + sizeof(entry->d_name)) >= sizeof(fullname)) {
2277 ERROR("Dirname too long, out of buffer space");
2279 (void) closedir(dir);
2283 while ((entry = readdir(dir)) != NULL) {
2284 if (entry->d_name[0] != '.') {
2285 strcpy(fullname, dirname);
2286 strcat(fullname, "/");
2287 strcat(fullname, entry->d_name);
2288 (void) remove(fullname);
2294 (void) closedir(dir);
2297 static void cleanup_tmp_dirs(command_t *cmd)
2301 for (d = 0; d < cmd->tmp_dirs->num; d++) {
2302 cleanup_tmp_dir(cmd->tmp_dirs->vals[d]);
2306 static int ensure_fake_uptodate(command_t *cmd)
2308 /* FIXME: could do the stat/touch here, but nah... */
2309 char const *touch_args[3];
2311 if (cmd->mode == MODE_INSTALL) {
2314 if (!cmd->fake_output_name) {
2318 touch_args[0] = "touch";
2319 touch_args[1] = cmd->fake_output_name;
2320 touch_args[2] = NULL;
2321 return external_spawn(cmd, "touch", touch_args);
2324 /* Store the install path in the *.la file */
2325 static int add_for_runtime(command_t *cmd)
2327 if (cmd->mode == MODE_INSTALL) {
2330 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
2331 cmd->output == OUT_LIB) {
2332 FILE *f=fopen(cmd->fake_output_name,"w");
2336 fprintf(f,"%s\n", cmd->install_path);
2340 return(ensure_fake_uptodate(cmd));
2344 static void parse_args(int argc, char *argv[], command_t *cmd)
2347 char const *arg, *base;
2351 * We now take a major step past libtool.
2353 * IF there's no "--mode=...", AND we recognise
2354 * the binary as a "special" name, THEN replace it
2355 * with the correct one, and set the correct mode.
2357 * For example if were called 'CC' then we know we should
2358 * probably be compiling stuff.
2360 base = file_name(argv[0]);
2361 arg = automode(base, cmd);
2363 push_count_chars(cmd->arglist, arg);
2365 assert(cmd->mode != MODE_UNKNOWN);
2369 * We first pass over the command-line arguments looking for
2370 * "--mode", etc. If so, then use the libtool compatibility
2371 * method for building the software. Otherwise, auto-detect it
2372 * via "-o" and the extensions.
2375 if (cmd->mode == MODE_UNKNOWN) for (a = 1; a < argc; a++) {
2378 if (strncmp(arg, "--mode=", 7) == 0) {
2384 * Stop if we get another magic method
2387 ((strncmp(arg, "LINK", 4) == 0) ||
2388 (strcmp(arg, "CC") == 0) ||
2389 (strcmp(arg, "CXX") == 0))) {
2394 if (strncmp(arg, "-o", 2) == 0) {
2400 * There were no magic args or an explicit --mode= but we did
2401 * find an output file, so guess what mode were meant to be in
2402 * from its extension.
2405 arg = strrchr(base, '.');
2407 cmd->mode = MODE_LINK;
2408 push_count_chars(cmd->arglist, LINK_C);
2411 else if (strcmp(arg, EXE_EXT) == 0) {
2412 cmd->mode = MODE_LINK;
2413 push_count_chars(cmd->arglist, LINK_C);
2416 else if (strcmp(arg + 1, DYNAMIC_LIB_EXT) == 0) {
2417 cmd->mode = MODE_LINK;
2418 push_count_chars(cmd->arglist, LINK_C);
2420 else if (strcmp(arg + 1, STATIC_LIB_EXT) == 0) {
2421 cmd->mode = MODE_LINK;
2422 push_count_chars(cmd->arglist, LINK_C);
2424 else if (strcmp(arg + 1, "la") == 0) {
2425 cmd->mode = MODE_LINK;
2426 push_count_chars(cmd->arglist, LINK_C);
2428 else if ((strcmp(arg + 1, "lo") == 0) ||
2429 (strcmp(arg + 1, "o") == 0)) {
2430 cmd->mode = MODE_COMPILE;
2431 push_count_chars(cmd->arglist, CC);
2435 for (a = 1; a < argc; a++) {
2439 if (cmd->mode == MODE_EXECUTE) {
2440 push_count_chars(cmd->arglist, arg);
2444 if (arg[0] == '-') {
2446 * Double dashed (long) single dash (short)
2448 arg_used = (arg[1] == '-') ?
2449 parse_long_opt(arg + 2, cmd) :
2450 parse_short_opt(arg + 1, cmd);
2452 if (arg_used) continue;
2455 * Ignore all options after the '--execute'
2457 if (cmd->mode == MODE_EXECUTE) continue;
2460 * We haven't done anything with it yet, but
2461 * there are still some arg/value pairs.
2463 * Try some of the more complicated short opts...
2467 * We found an output file!
2469 if ((arg[1] == 'o') && (arg[2] == '\0')) {
2471 arg_used = parse_output_file_name(arg,
2474 * -MT literal dependency
2476 } else if (!strcmp(arg + 1, "MT")) {
2477 DEBUG("Adding: %s\n", arg);
2479 push_count_chars(cmd->arglist, arg);
2482 NOTICE(" %s\n", arg);
2484 push_count_chars(cmd->arglist, arg);
2487 * Runtime library search path
2489 } else if (!strcmp(arg + 1, "rpath")) {
2490 /* Aha, we should try to link both! */
2491 cmd->install_path = argv[++a];
2494 } else if (!strcmp(arg + 1, "release")) {
2495 /* Store for later deciphering */
2496 cmd->version_info = argv[++a];
2499 } else if (!strcmp(arg + 1, "version-info")) {
2500 /* Store for later deciphering */
2501 cmd->version_info = argv[++a];
2504 } else if (!strcmp(arg + 1,
2505 "export-symbols-regex")) {
2506 /* Skip the argument. */
2510 } else if (!strcmp(arg + 1, "undefined")) {
2511 cmd->undefined_flag = argv[++a];
2514 * Add dir to runtime library search path.
2516 } else if ((arg[1] == 'R') && !arg[2]) {
2518 add_runtime_dir_lib(argv[++a], cmd);
2523 * Ok.. the argument doesn't begin with a dash
2524 * maybe it's an input file.
2526 * Check its extension to see if it's a known input
2527 * file and verify it exists.
2530 arg_used = parse_input_file_name(arg, cmd);
2534 * If we still don't have a run mode, look for a magic
2535 * program name CC, LINK, or whatever. Then replace that
2536 * with the name of the real program we want to run.
2539 if ((cmd->arglist->num == 0) &&
2540 (cmd->mode == MODE_UNKNOWN)) {
2541 arg = automode(arg, cmd);
2544 DEBUG("Adding: %s\n", arg);
2546 push_count_chars(cmd->arglist, arg);
2552 int main(int argc, char *argv[])
2557 memset(&cmd, 0, sizeof(cmd));
2559 cmd.options.pic_mode = PIC_UNKNOWN;
2560 cmd.mode = MODE_UNKNOWN;
2561 cmd.output = OUT_GENERAL;
2564 * Initialise the various argument lists
2566 cmd.program_opts = alloc_countchars();
2567 cmd.arglist = alloc_countchars();
2568 cmd.tmp_dirs = alloc_countchars();
2569 cmd.obj_files = alloc_countchars();
2570 cmd.dep_rpaths = alloc_countchars();
2571 cmd.rpaths = alloc_countchars();
2572 cmd.static_opts.normal = alloc_countchars();
2573 cmd.shared_opts.normal = alloc_countchars();
2574 cmd.shared_opts.dependencies = alloc_countchars();
2577 * Fill up the various argument lists
2579 parse_args(argc, argv, &cmd);
2580 post_parse_fixup(&cmd);
2583 * We couldn't figure out which mode to operate in
2585 if (cmd.mode == MODE_UNKNOWN) {
2589 rc = run_mode(&cmd);
2591 add_for_runtime(&cmd);
2594 cleanup_tmp_dirs(&cmd);