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.
21 #include <sys/types.h>
23 #if !defined(__MINGW32__)
24 # include <sys/wait.h>
33 # define SHELL_CMD "sh"
34 # define GEN_EXPORTS "emxexp"
35 # define DEF2IMPLIB_CMD "emximp"
36 # define SHARE_SW "-Zdll -Zmtd"
38 # define TRUNCATE_DLL_NAME
39 # define DYNAMIC_LIB_EXT "dll"
40 # define EXE_EX ".exe"
41 /* OMF is the native format under OS/2 */
44 # define STATIC_LIB_EXT "lib"
45 # define OBJECT_EXT "obj"
46 # define LIBRARIAN "emxomfar"
47 # define LIBRARIAN_OPTS "cr"
49 /* but the alternative, a.out, can fork() which is sometimes necessary */
50 # define STATIC_LIB_EXT "a"
51 # define OBJECT_EXT "o"
52 # define LIBRARIAN "ar"
53 # define LIBRARIAN_OPTS "cr"
57 #if defined(__APPLE__)
58 # define SHELL_CMD "/bin/sh"
59 # define DYNAMIC_LIB_EXT "dylib"
60 # define MODULE_LIB_EXT "bundle"
61 # define STATIC_LIB_EXT "a"
62 # define OBJECT_EXT "o"
63 # define LIBRARIAN "ar"
64 # define LIBRARIAN_OPTS "cr"
65 /* man libtool(1) documents ranlib option of -c. */
66 # define RANLIB "ranlib"
67 # define PIC_FLAG "-fPIC -fno-common"
68 # define SHARED_OPTS "-dynamiclib"
69 # define MODULE_OPTS "-bundle -dynamic"
70 # define DYNAMIC_LINK_OPTS "-flat_namespace"
71 # define DYNAMIC_LINK_UNDEFINED "-undefined suppress"
72 # define dynamic_link_version_func darwin_dynamic_link_function
73 # define DYNAMIC_INSTALL_NAME "-install_name"
74 # define DYNAMIC_LINK_NO_INSTALL "-dylib_file"
76 /*-install_name /Users/jerenk/apache-2.0-cvs/lib/libapr.0.dylib -compatibility_version 1 -current_version 1.0 */
77 # define LD_LIBRARY_PATH "DYLD_LIBRARY_PATH"
78 # define LD_LIBRARY_PATH_LOCAL "DYLD_FALLBACK_LIBRARY_PATH"
81 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
82 # define SHELL_CMD "/bin/sh"
83 # define DYNAMIC_LIB_EXT "so"
84 # define MODULE_LIB_EXT "so"
85 # define STATIC_LIB_EXT "a"
86 # define OBJECT_EXT "o"
87 # define LIBRARIAN "ar"
88 # define LIBRARIAN_OPTS "cr"
89 # define RANLIB "ranlib"
90 # define PIC_FLAG "-fPIC"
91 # define RPATH "-rpath"
92 # define SHARED_OPTS "-shared"
93 # define MODULE_OPTS "-shared"
94 # define DYNAMIC_LINK_OPTS "-export-dynamic"
95 # define LINKER_FLAG_PREFIX "-Wl,"
97 # define LD_RUN_PATH "LD_RUN_PATH"
98 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
99 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
103 # define SHELL_CMD "/bin/sh"
104 # define DYNAMIC_LIB_EXT "so"
105 # define MODULE_LIB_EXT "so"
106 # define STATIC_LIB_EXT "a"
107 # define OBJECT_EXT "o"
108 # define LIBRARIAN "ar"
109 # define LIBRARIAN_OPTS "cr"
110 # define RANLIB "ranlib"
111 # define PIC_FLAG "-KPIC"
113 # define SHARED_OPTS "-G"
114 # define MODULE_OPTS "-G"
115 # define DYNAMIC_LINK_OPTS ""
116 # define LINKER_FLAG_NO_EQUALS
118 # define HAS_REALPATH
119 # define LD_RUN_PATH "LD_RUN_PATH"
120 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
121 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
124 #if defined(_OSD_POSIX)
125 # define SHELL_CMD "/usr/bin/sh"
126 # define DYNAMIC_LIB_EXT "so"
127 # define MODULE_LIB_EXT "so"
128 # define STATIC_LIB_EXT "a"
129 # define OBJECT_EXT "o"
130 # define LIBRARIAN "ar"
131 # define LIBRARIAN_OPTS "cr"
132 # define SHARED_OPTS "-G"
133 # define MODULE_OPTS "-G"
134 # define LINKER_FLAG_PREFIX "-Wl,"
135 # define NEED_SNPRINTF
138 #if defined(sinix) && defined(mips) && defined(__SNI_TARG_UNIX)
139 # define SHELL_CMD "/usr/bin/sh"
140 # define DYNAMIC_LIB_EXT "so"
141 # define MODULE_LIB_EXT "so"
142 # define STATIC_LIB_EXT "a"
143 # define OBJECT_EXT "o"
144 # define LIBRARIAN "ar"
145 # define LIBRARIAN_OPTS "cr"
146 # define RPATH "-Brpath"
147 # define SHARED_OPTS "-G"
148 # define MODULE_OPTS "-G"
149 # define DYNAMIC_LINK_OPTS "-Wl,-Blargedynsym"
150 # define LINKER_FLAG_PREFIX "-Wl,"
151 # define NEED_SNPRINTF
152 # define LD_RUN_PATH "LD_RUN_PATH"
153 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
154 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
157 #if defined(__MINGW32__)
158 # define SHELL_CMD "sh"
159 # define DYNAMIC_LIB_EXT "dll"
160 # define MODULE_LIB_EXT "dll"
161 # define STATIC_LIB_EXT "a"
162 # define OBJECT_EXT "o"
163 # define LIBRARIAN "ar"
164 # define LIBRARIAN_OPTS "cr"
165 # define RANLIB "ranlib"
166 # define LINKER_FLAG_PREFIX "-Wl,"
167 # define SHARED_OPTS "-shared"
168 # define MODULE_OPTS "-shared"
169 # define MKDIR_NO_UMASK
170 # define EXE_EXT ".exe"
186 #define LINK_CXX "g++"
190 #define LIBDIR "/usr/local/lib"
193 #define OBJDIR ".libs"
196 #error Unsupported platform: Please add defines for SHELL_CMD etc. for your platform.
208 #define PATH_MAX 1024
212 /* We want to say we are libtool 1.4 for shlibtool compatibility. */
213 #define VERSION "1.4"
215 #define DEBUG(fmt, ...) if(cmd->options.debug) printf(fmt, ## __VA_ARGS__)
216 #define NOTICE(fmt, ...) if(!cmd->options.silent) printf(fmt, ## __VA_ARGS__)
217 #define ERROR(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
233 OUT_DYNAMIC_LIB_ONLY,
269 count_chars *install;
270 count_chars *dependencies;
276 enum shared_mode shared;
279 enum pic_mode pic_mode;
286 enum output_type output;
289 const char *output_name;
290 const char *fake_output_name;
291 const char *basename;
293 const char *install_path;
294 const char *compiler;
296 count_chars *program_opts;
298 count_chars *arglist;
299 count_chars *tmp_dirs;
300 count_chars *obj_files;
301 count_chars *dep_rpaths;
304 library_name static_name;
305 library_name shared_name;
306 library_name module_name;
308 library_opts static_opts;
309 library_opts shared_opts;
311 const char *version_info;
312 const char *undefined_flag;
316 static void add_rpath(count_chars *cc, const char *path);
319 static void usage(int code)
321 printf("Usage: jlibtool [OPTIONS...] COMMANDS...\n");
322 printf("jlibtool is a replacement for GNU libtool with similar functionality.\n\n");
324 printf(" --config show all configuration variables\n");
325 printf(" --debug enable verbose shell tracing\n");
326 printf(" --dry-run display commands without modifying any files\n");
327 printf(" --help display this help message and exit\n");
328 printf(" --mode=MODE use operational mode MODE (you *must* set mode)\n");
330 printf(" --silent don't print informational messages\n");
331 printf(" --tag=TAG Ignored for libtool compatibility\n");
332 printf(" --version print version information\n");
335 printf(" --shared Build shared libraries when using --mode=link\n");
336 printf(" --export-all Try to export 'def' file on some platforms\n");
338 printf("\nMODE must be one of the following:\n\n");
339 printf(" compile compile a source file into a jlibtool object\n");
340 printf(" execute automatically set library path, then run a program\n");
341 printf(" install install libraries or executables\n");
342 printf(" link create a library or an executable\n");
344 printf("\nMODE-ARGS can be the following:\n\n");
345 printf(" -export-dynamic accepted and ignored\n");
346 printf(" -module create a module when linking\n");
347 printf(" -shared create a shared library when linking\n");
348 printf(" -prefer-pic prefer position-independent-code when compiling\n");
349 printf(" -prefer-non-pic prefer non position-independent-code when compiling\n");
350 printf(" -static create a static library when linking\n");
351 printf(" -no-install link libraries locally\n");
352 printf(" -rpath arg Set install path for shared libraries\n");
353 printf(" -l arg pass '-l arg' to the link stage\n");
354 printf(" -L arg pass '-L arg' to the link stage\n");
355 printf(" -R dir add 'dir' to runtime library search path.\n");
356 printf(" -Zexe accepted and ignored\n");
357 printf(" -avoid-version accepted and ignored\n");
362 #if defined(NEED_SNPRINTF)
363 /* Write at most n characters to the buffer in str, return the
364 * number of chars written or -1 if the buffer would have been
367 * This is portable to any POSIX-compliant system has /dev/null
369 static FILE *f = NULL;
370 static int vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
375 f = fopen("/dev/null","w");
382 setvbuf(f, str, _IOFBF, n);
384 res = vfprintf(f, fmt, ap);
386 if ((res > 0) && (res < n)) {
387 res = vsprintf( str, fmt, ap );
392 static int snprintf(char *str, size_t n, const char *fmt, ...)
398 res = vsnprintf( str, n, fmt, ap );
404 static void *lt_malloc(size_t size)
410 ERROR("Failed allocating %zu bytes, OOM", size);
417 static void lt_const_free(const void *ptr)
421 memcpy(&tmp, &ptr, sizeof(tmp));
425 static void init_count_chars(count_chars *cc)
427 cc->vals = (const char**) lt_malloc(PATH_MAX*sizeof(char*));
431 static count_chars *alloc_countchars()
434 out = lt_malloc(sizeof(count_chars));
438 init_count_chars(out);
443 static void clear_count_chars(count_chars *cc)
446 for (i = 0; i < cc->num; i++) {
453 static void push_count_chars(count_chars *cc, const char *newval)
455 cc->vals[cc->num++] = newval;
458 static void pop_count_chars(count_chars *cc)
463 static void insert_count_chars(count_chars *cc, const char *newval, int position)
467 for (i = cc->num; i > position; i--) {
468 cc->vals[i] = cc->vals[i-1];
471 cc->vals[position] = newval;
475 static void append_count_chars(count_chars *cc, count_chars *cctoadd)
478 for (i = 0; i < cctoadd->num; i++) {
479 if (cctoadd->vals[i]) {
480 push_count_chars(cc, cctoadd->vals[i]);
485 static const char *flatten_count_chars(count_chars *cc, int space)
491 for (i = 0; i < cc->num; i++) {
493 size += strlen(cc->vals[i]) + 1;
500 newval = (char*)lt_malloc(size + 1);
503 for (i = 0; i < cc->num; i++) {
505 strcat(newval, cc->vals[i]);
515 static char *shell_esc(const char *str)
520 const unsigned char *s;
522 cmd = (char *)lt_malloc(2 * strlen(str) + 3);
523 d = (unsigned char *)cmd;
524 s = (const unsigned char *)str;
535 else if (*s == '\\' || (*s == ' ' && (in_quote % 2))) {
549 static int external_spawn(command_t *cmd, const char *file, const char **argv)
551 file = file; /* -Wunused */
553 if (!cmd->options.silent) {
554 const char **argument = argv;
555 NOTICE("Executing: ");
557 NOTICE("%s ", *argument);
563 if (cmd->options.dry_run) {
566 #if defined(__EMX__) || defined(__MINGW32__)
567 return spawnvp(P_WAIT, argv[0], argv);
573 return execvp(argv[0], (char**)argv);
577 waitpid(pid, &statuscode, 0);
578 if (WIFEXITED(statuscode)) {
579 return WEXITSTATUS(statuscode);
587 static int run_command(command_t *cmd, count_chars *cc)
593 const char *spawn_args[4];
596 init_count_chars(&tmpcc);
599 push_count_chars(&tmpcc, cmd->program);
602 append_count_chars(&tmpcc, cmd->program_opts);
604 append_count_chars(&tmpcc, cc);
606 raw = flatten_count_chars(&tmpcc, 1);
607 command = shell_esc(raw);
609 memcpy(&tmp, &raw, sizeof(tmp));
612 spawn_args[0] = SHELL_CMD;
613 spawn_args[1] = "-c";
614 spawn_args[2] = command;
615 spawn_args[3] = NULL;
616 ret = external_spawn(cmd, spawn_args[0], spawn_args);
624 * print configuration
625 * shlibpath_var is used in configure.
627 #define printc(_x,_y) if (!value || !strcmp(value, _x)) printf(_x "=\"%s\"\n", _y)
629 static void print_config(const char *value)
632 printc("runpath_var", LD_RUN_PATH);
634 #ifdef LD_LIBRARY_PATH
635 printc("shlibpath_var", LD_LIBRARY_PATH);
637 #ifdef LD_LIBRARY_PATH_LOCAL
638 printc("shlocallibpath_var", LD_LIBRARY_PATH_LOCAL);
641 printc("SHELL", SHELL_CMD);
644 printc("objext", OBJECT_EXT);
647 printc("objdir", OBJDIR);
649 #ifdef DYNAMIC_LIB_EXT
650 /* add a '.' prefix because libtool does that. */
651 printc("shrext_cmds", "echo ." DYNAMIC_LIB_EXT);
652 /* add a '.' prefix because libtool does that. */
653 printc("shrext", "." DYNAMIC_LIB_EXT);
656 printc("exeext", EXE_EXT);
658 #ifdef STATIC_LIB_EXT
659 printc("libext", STATIC_LIB_EXT);
662 printc("AR", LIBRARIAN);
664 #ifdef LIBRARIAN_OPTS
665 printc("AR_FLAGS", LIBRARIAN_OPTS);
667 #ifdef LINKER_FLAG_PREFIX
668 printc("wl", LINKER_FLAG_PREFIX);
671 printc("ranlib", RANLIB);
676 * Add a directory to the runtime library search path.
678 static void add_runtime_dir_lib(const char *arg, command_t *cmd)
681 add_rpath(cmd->shared_opts.dependencies, arg);
683 arg = arg; /* -Wunused */
688 static int parse_long_opt(const char *arg, command_t *cmd)
690 char *equal_pos = strchr(arg, '=');
695 strncpy(var, arg, equal_pos - arg);
696 var[equal_pos - arg] = 0;
697 if (strlen(equal_pos + 1) >= sizeof(var)) return 0;
698 strcpy(value, equal_pos + 1);
700 strncpy(var, arg, sizeof(var) - 1);
701 var[sizeof(var) - 1] = '\0';
706 if (strcmp(var, "silent") == 0) {
707 cmd->options.silent = 1;
708 } else if (strcmp(var, "quiet") == 0) {
709 cmd->options.silent = 1;
710 } else if (strcmp(var, "debug") == 0) {
711 cmd->options.debug = 1;
712 } else if (strcmp(var, "mode") == 0) {
713 if (cmd->mode != MODE_UNKNOWN) {
714 ERROR("Cannot set --mode twice\n");
718 if (strcmp(value, "compile") == 0) {
719 cmd->mode = MODE_COMPILE;
720 cmd->output = OUT_OBJECT;
722 } else if (strcmp(value, "link") == 0) {
723 cmd->mode = MODE_LINK;
724 cmd->output = OUT_LIB;
726 } else if (strcmp(value, "install") == 0) {
727 cmd->mode = MODE_INSTALL;
729 } else if (strcmp(value, "execute") == 0) {
730 cmd->mode = MODE_EXECUTE;
733 ERROR("Unknown mode \"%s\"\n", value);
737 } else if (strcmp(var, "shared") == 0) {
738 if ((cmd->mode == MODE_LINK) && (cmd->output == OUT_GENERAL)) {
739 cmd->output = OUT_DYNAMIC_LIB_ONLY;
741 cmd->options.shared = SHARE_SHARED;
743 } else if (strcmp(var, "export-all") == 0) {
744 cmd->options.export_all = 1;
746 } else if (strcmp(var, "dry-run") == 0) {
747 NOTICE("Dry-run mode on!\n");
748 cmd->options.dry_run = 1;
750 } else if (strcmp(var, "version") == 0) {
751 NOTICE("Version " VERSION "\n");
753 } else if (strcmp(var, "help") == 0) {
756 } else if (strcmp(var, "config") == 0) {
767 /* Return 1 if we eat it. */
768 static int parse_short_opt(const char *arg, command_t *cmd)
770 if (strcmp(arg, "export-dynamic") == 0) {
771 cmd->options.export_dynamic = 1;
775 if (strcmp(arg, "module") == 0) {
776 cmd->output = OUT_MODULE;
780 if (strcmp(arg, "shared") == 0) {
781 if (cmd->mode == MODE_LINK) {
782 cmd->output = OUT_DYNAMIC_LIB_ONLY;
784 cmd->options.shared = SHARE_SHARED;
788 if (strcmp(arg, "Zexe") == 0) {
792 if (strcmp(arg, "avoid-version") == 0) {
796 if (strcmp(arg, "prefer-pic") == 0) {
797 cmd->options.pic_mode = PIC_PREFER;
801 if (strcmp(arg, "prefer-non-pic") == 0) {
802 cmd->options.pic_mode = PIC_AVOID;
806 if (strcmp(arg, "static") == 0) {
807 if ((cmd->mode == MODE_LINK) && (cmd->output == OUT_LIB)) {
808 cmd->output = OUT_STATIC_LIB_ONLY;
810 cmd->options.shared = SHARE_STATIC;
814 if (cmd->mode == MODE_LINK) {
815 if (strcmp(arg, "no-install") == 0) {
816 cmd->options.no_install = 1;
819 if (arg[0] == 'L' || arg[0] == 'l') {
822 push_count_chars(cmd->shared_opts.dependencies, arg);
824 } else if (arg[0] == 'R' && arg[1]) {
825 /* -Rdir Add dir to runtime library search path. */
826 add_runtime_dir_lib(&arg[1], cmd);
833 #ifdef TRUNCATE_DLL_NAME
834 static char *truncate_dll_name(char *path)
836 /* Cut DLL name down to 8 characters after removing any mod_ prefix */
837 char *tmppath = strdup(path);
838 char *newname = strrchr(tmppath, '/') + 1;
839 char *ext = strrchr(newname, '.');
848 if (strncmp(newname, "mod_", 4) == 0) {
849 strcpy(newname, newname + 4);
854 strcpy(newname + 8, strchr(newname, '.'));
861 static long safe_strtol(const char *nptr, const char **endptr, int base)
867 rv = strtol(nptr, (char**)endptr, 10);
869 if (errno == ERANGE) {
876 static void safe_mkdir(command_t *cmd, const char *path)
881 old_umask = umask(0);
884 #ifdef MKDIR_NO_UMASK
885 status = mkdir(path);
887 status = mkdir(path, ~old_umask);
889 if ((status < 0) && (errno != EEXIST)) {
890 NOTICE("Warning: mkdir of %s failed\n", path);
894 /** Returns a file's name without the path
896 * @param path to break apart.
897 * @return pointer in path.
899 static const char *file_name(const char *path)
903 name = strrchr(path, '/');
905 name = strrchr(path, '\\'); /* eww windows? */
918 /** Returns a file's name without path or extension
920 * @param path to check
921 * @return pointer in path.
923 static const char *file_name_stripped(const char *path)
928 name = file_name(path);
929 ext = strrchr(name, '.');
934 trimmed = lt_malloc(ext - name + 1);
935 strncpy(trimmed, name, ext - name);
936 trimmed[ext-name] = 0;
945 /* version_info is in the form of MAJOR:MINOR:PATCH */
946 static const char *darwin_dynamic_link_function(const char *version_info)
949 long major, minor, patch;
956 major = safe_strtol(version_info, &version_info, 10);
959 if (version_info[0] == ':') {
963 minor = safe_strtol(version_info, &version_info, 10);
966 if (version_info[0] == ':') {
970 patch = safe_strtol(version_info, &version_info, 10);
976 /* Avoid -dylib_compatibility_version must be greater than zero errors. */
980 newarg = (char*)lt_malloc(100);
982 "-compatibility_version %ld -current_version %ld.%ld",
983 major, major, minor);
990 * Add a '.libs/' to the buffer. The caller ensures that
991 * The buffer is large enough to handle 6 extra characters.
993 static void add_dotlibs(char *buffer)
995 char *name = strrchr(buffer, '/');
999 strcpy(buffer, ".libs/");
1006 memmove(name + 6, name, strlen(name));
1007 memcpy(name, ".libs/", 6);
1010 static char *gen_library_name(const char *name, enum lib_type genlib)
1012 char *newarg, *newext;
1014 newarg = (char *)calloc(strlen(name) + 11, 1);
1016 if (genlib == TYPE_MODULE_LIB && strncmp(name, "lib", 3) == 0) {
1020 if (genlib == TYPE_MODULE_LIB) {
1021 strcpy(newarg, file_name(name));
1024 strcpy(newarg, name);
1027 newext = strrchr(newarg, '.');
1029 ERROR("Library path does not have an extension");
1037 case TYPE_STATIC_LIB:
1038 strcpy(newext, STATIC_LIB_EXT);
1040 case TYPE_DYNAMIC_LIB:
1041 strcpy(newext, DYNAMIC_LIB_EXT);
1043 case TYPE_MODULE_LIB:
1044 strcpy(newext, MODULE_LIB_EXT);
1051 add_dotlibs(newarg);
1056 static char *gen_install_name(const char *name, enum lib_type genlib)
1062 newname = gen_library_name(name, genlib);
1063 if (!newname) return NULL;
1065 /* Check if it exists. If not, return NULL. */
1066 rv = stat(newname, &sb);
1076 static const char *check_object_exists(command_t *cmd, const char *arg, int arglen)
1081 newarg = (char *)lt_malloc(arglen + 10);
1082 memcpy(newarg, arg, arglen);
1084 ext = newarg + arglen;
1086 strcpy(ext, OBJECT_EXT);
1088 DEBUG("Checking (obj): %s\n", newarg);
1089 if (stat(newarg, &sb) == 0) {
1098 /* libdircheck values:
1099 * 0 - no .libs suffix
1102 static char *check_library_exists(command_t *cmd, const char *arg, int pathlen,
1103 int libdircheck, enum lib_type*libtype)
1106 int pass, rv, newpathlen;
1108 newarg = (char *)lt_malloc(strlen(arg) + 10);
1109 strcpy(newarg, arg);
1110 newarg[pathlen] = '\0';
1112 newpathlen = pathlen;
1114 add_dotlibs(newarg);
1115 newpathlen += sizeof(".libs/") - 1;
1118 strcpy(newarg + newpathlen, arg + pathlen);
1119 ext = strrchr(newarg, '.');
1121 ERROR("Error: Library path does not have an extension");
1135 if (cmd->options.pic_mode != PIC_AVOID &&
1136 cmd->options.shared != SHARE_STATIC) {
1137 strcpy(ext, DYNAMIC_LIB_EXT);
1138 *libtype = TYPE_DYNAMIC_LIB;
1144 strcpy(ext, STATIC_LIB_EXT);
1145 *libtype = TYPE_STATIC_LIB;
1148 strcpy(ext, MODULE_LIB_EXT);
1149 *libtype = TYPE_MODULE_LIB;
1152 strcpy(ext, OBJECT_EXT);
1153 *libtype = TYPE_OBJECT;
1156 *libtype = TYPE_UKNOWN;
1160 DEBUG("Checking (lib): %s\n", newarg);
1161 rv = stat(newarg, &sb);
1163 while (rv != 0 && ++pass < 4);
1174 static char * load_install_path(const char *arg)
1184 path = lt_malloc(PATH_MAX);
1186 fgets(path, PATH_MAX, f);
1189 if (path[strlen(path)-1] == '\n') {
1190 path[strlen(path)-1] = '\0';
1193 /* Check that we have an absolute path.
1194 * Otherwise the file could be a GNU libtool file.
1196 if (path[0] != '/') {
1204 static char * load_noinstall_path(const char *arg, int pathlen)
1206 char *newarg, *expanded_path;
1209 newarg = (char *)lt_malloc(strlen(arg) + 10);
1210 strcpy(newarg, arg);
1211 newarg[pathlen] = 0;
1213 newpathlen = pathlen;
1214 strcat(newarg, ".libs");
1215 newpathlen += sizeof(".libs") - 1;
1216 newarg[newpathlen] = 0;
1219 expanded_path = lt_malloc(PATH_MAX);
1220 expanded_path = realpath(newarg, expanded_path);
1221 /* Uh, oh. There was an error. Fall back on our first guess. */
1222 if (!expanded_path) {
1223 expanded_path = newarg;
1226 /* We might get ../ or something goofy. Oh, well. */
1227 expanded_path = newarg;
1230 return expanded_path;
1233 static void add_dynamic_link_opts(command_t *cmd, count_chars *args)
1235 #ifdef DYNAMIC_LINK_OPTS
1236 if (cmd->options.pic_mode != PIC_AVOID) {
1237 DEBUG("Adding linker opt: %s\n", DYNAMIC_LINK_OPTS);
1239 push_count_chars(args, DYNAMIC_LINK_OPTS);
1240 if (cmd->undefined_flag) {
1241 push_count_chars(args, "-undefined");
1242 #if defined(__APPLE__)
1243 /* -undefined dynamic_lookup is used by the bundled Python in
1244 * 10.4, but if we don't set MACOSX_DEPLOYMENT_TARGET to 10.3+,
1245 * we'll get a linker error if we pass this flag.
1247 if (strcasecmp(cmd->undefined_flag, "dynamic_lookup") == 0) {
1248 insert_count_chars(cmd->program_opts, "MACOSX_DEPLOYMENT_TARGET=10.3", 0);
1251 push_count_chars(args, cmd->undefined_flag);
1254 #ifdef DYNAMIC_LINK_UNDEFINED
1255 DEBUG("Adding linker opt: %s\n", DYNAMIC_LINK_UNDEFINED);
1257 push_count_chars(args, DYNAMIC_LINK_UNDEFINED);
1264 /* Read the final install location and add it to runtime library search path. */
1266 static void add_rpath(count_chars *cc, const char *path)
1271 #ifdef LINKER_FLAG_PREFIX
1272 size = strlen(LINKER_FLAG_PREFIX);
1274 size = size + strlen(path) + strlen(RPATH) + 2;
1275 tmp = lt_malloc(size);
1277 #ifdef LINKER_FLAG_PREFIX
1278 strcpy(tmp, LINKER_FLAG_PREFIX);
1283 #ifndef LINKER_FLAG_NO_EQUALS
1288 push_count_chars(cc, tmp);
1291 static void add_rpath_file(count_chars *cc, const char *arg)
1295 path = load_install_path(arg);
1297 add_rpath(cc, path);
1298 lt_const_free(path);
1302 static void add_rpath_noinstall(count_chars *cc, const char *arg, int pathlen)
1306 path = load_noinstall_path(arg, pathlen);
1308 add_rpath(cc, path);
1309 lt_const_free(path);
1314 #ifdef DYNAMIC_LINK_NO_INSTALL
1315 static void add_dylink_noinstall(count_chars *cc, const char *arg, int pathlen,
1318 const char *install_path, *current_path, *name;
1320 int i_p_len, c_p_len, name_len, dyext_len, cur_len;
1322 install_path = load_install_path(arg);
1323 current_path = load_noinstall_path(arg, pathlen);
1325 if (!install_path || !current_path) {
1329 push_count_chars(cc, DYNAMIC_LINK_NO_INSTALL);
1331 i_p_len = strlen(install_path);
1332 c_p_len = strlen(current_path);
1335 name_len = extlen-pathlen;
1336 dyext_len = sizeof(DYNAMIC_LIB_EXT) - 1;
1338 /* No, we need to replace the extension. */
1339 exp_argument = (char *)lt_malloc(i_p_len + c_p_len + (name_len*2) +
1343 strcpy(exp_argument, install_path);
1345 exp_argument[cur_len++] = '/';
1346 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1347 cur_len += name_len;
1348 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1349 cur_len += dyext_len;
1350 exp_argument[cur_len++] = ':';
1351 strcpy(exp_argument+cur_len, current_path);
1353 exp_argument[cur_len++] = '/';
1354 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1355 cur_len += name_len;
1356 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1357 cur_len += dyext_len;
1359 push_count_chars(cc, exp_argument);
1364 /* use -L -llibname to allow to use installed libraries */
1365 static void add_minus_l(count_chars *cc, const char *arg)
1368 char *name = strrchr(arg, '/');
1369 char *file = strrchr(arg, '.');
1371 if ((name != NULL) && (file != NULL) &&
1372 (strstr(name, "lib") == (name + 1))) {
1377 push_count_chars(cc, "-L");
1378 push_count_chars(cc, arg);
1379 /* we need one argument like -lapr-1 */
1380 newarg = lt_malloc(strlen(file) + 3);
1381 strcpy(newarg, "-l");
1382 strcat(newarg, file);
1383 push_count_chars(cc, newarg);
1385 push_count_chars(cc, arg);
1391 static void add_linker_flag_prefix(count_chars *cc, const char *arg)
1393 #ifndef LINKER_FLAG_PREFIX
1394 push_count_chars(cc, arg);
1397 newarg = (char*)lt_malloc(strlen(arg) + sizeof(LINKER_FLAG_PREFIX) + 1);
1398 strcpy(newarg, LINKER_FLAG_PREFIX);
1399 strcat(newarg, arg);
1400 push_count_chars(cc, newarg);
1405 static int explode_static_lib(command_t *cmd, const char *lib)
1407 count_chars tmpdir_cc, libname_cc;
1408 const char *tmpdir, *libname;
1409 char savewd[PATH_MAX];
1412 struct dirent *entry;
1413 const char *lib_args[4];
1416 if (cmd->options.dry_run) {
1420 name = file_name(lib);
1422 init_count_chars(&tmpdir_cc);
1423 push_count_chars(&tmpdir_cc, ".libs/");
1424 push_count_chars(&tmpdir_cc, name);
1425 push_count_chars(&tmpdir_cc, ".exploded/");
1426 tmpdir = flatten_count_chars(&tmpdir_cc, 0);
1428 NOTICE("Making: %s\n", tmpdir);
1430 safe_mkdir(cmd, tmpdir);
1432 push_count_chars(cmd->tmp_dirs, tmpdir);
1434 getcwd(savewd, sizeof(savewd));
1436 if (chdir(tmpdir) != 0) {
1437 NOTICE("Warning: could not explode %s\n", lib);
1442 if (lib[0] == '/') {
1446 init_count_chars(&libname_cc);
1447 push_count_chars(&libname_cc, "../../");
1448 push_count_chars(&libname_cc, lib);
1449 libname = flatten_count_chars(&libname_cc, 0);
1452 lib_args[0] = LIBRARIAN;
1454 lib_args[2] = libname;
1457 external_spawn(cmd, LIBRARIAN, lib_args);
1460 dir = opendir(tmpdir);
1462 while ((entry = readdir(dir)) != NULL) {
1463 #if defined(__APPLE__) && defined(RANLIB)
1464 /* Apple inserts __.SYMDEF which isn't needed.
1465 * Leopard (10.5+) can also add '__.SYMDEF SORTED' which isn't
1466 * much fun either. Just skip them.
1468 if (strstr(entry->d_name, "__.SYMDEF") != NULL) {
1472 if (entry->d_name[0] != '.') {
1473 push_count_chars(&tmpdir_cc, entry->d_name);
1474 name = flatten_count_chars(&tmpdir_cc, 0);
1476 DEBUG("Adding object: %s\n", name);
1477 push_count_chars(cmd->obj_files, name);
1478 pop_count_chars(&tmpdir_cc);
1486 static int parse_input_file_name(const char *arg, command_t *cmd)
1488 const char *ext = strrchr(arg, '.');
1491 enum lib_type libtype;
1494 /* Can't guess the extension */
1500 name = file_name(arg);
1501 pathlen = name - arg;
1504 * Were linking and have an archived object or object file
1505 * push it onto the list of object files which'll get used
1506 * to create the input files list for the linker.
1508 * We assume that these are outside of the project were building,
1509 * as there's no reason to create .a files as part of the build
1512 if (!strcmp(ext, STATIC_LIB_EXT) && (cmd->mode == MODE_LINK)) {
1515 if (!stat(arg, &sb)) {
1516 DEBUG("Adding object: %s\n", arg);
1518 push_count_chars(cmd->obj_files, arg);
1525 * More object files, if were linking they get set as input
1528 if (!strcmp(ext, "lo") || !strcmp(ext, OBJECT_EXT)) {
1529 newarg = check_object_exists(cmd, arg, ext - arg);
1531 ERROR("Can not find suitable object file for %s\n", arg);
1535 if (cmd->mode == MODE_LINK) {
1536 DEBUG("Adding object: %s\n", newarg);
1538 push_count_chars(cmd->obj_files, newarg);
1540 push_count_chars(cmd->arglist, newarg);
1546 if (!strcmp(ext, "la")) {
1547 switch (cmd->mode) {
1549 /* Try the .libs dir first! */
1550 newarg = check_library_exists(cmd, arg, pathlen, 1, &libtype);
1552 /* Try the normal dir next. */
1553 newarg = check_library_exists(cmd, arg, pathlen, 0, &libtype);
1555 ERROR("Can not find suitable library for %s\n", arg);
1560 /* It is not ok to just add the file: a library may added with:
1561 1 - -L path library_name. (For *.so in Linux).
1565 if (libtype == TYPE_DYNAMIC_LIB) {
1566 add_minus_l(cmd->shared_opts.dependencies, newarg);
1567 } else if (cmd->output == OUT_LIB &&
1568 libtype == TYPE_STATIC_LIB) {
1569 explode_static_lib(cmd, newarg);
1571 push_count_chars(cmd->shared_opts.dependencies, newarg);
1574 if (cmd->output == OUT_LIB && libtype == TYPE_STATIC_LIB) {
1575 explode_static_lib(cmd, newarg);
1578 push_count_chars(cmd->shared_opts.dependencies, newarg);
1581 if (libtype == TYPE_DYNAMIC_LIB) {
1582 if (cmd->options.no_install) {
1584 add_rpath_noinstall(cmd->shared_opts.dependencies,
1590 add_rpath_file(cmd->shared_opts.dependencies, arg);
1597 * If we've already recorded a library to
1598 * install, we're most likely getting the .la
1599 * file that we want to install as.
1601 * The problem is that we need to add it as the
1602 * directory, not the .la file itself.
1603 * Otherwise, we'll do odd things.
1605 if (cmd->output == OUT_LIB) {
1609 tmp[pathlen] = '\0';
1610 push_count_chars(cmd->arglist, tmp);
1613 cmd->output = OUT_LIB;
1614 cmd->output_name = arg;
1615 cmd->static_name.install = gen_install_name(arg, 0);
1616 cmd->shared_name.install = gen_install_name(arg, 1);
1617 cmd->module_name.install = gen_install_name(arg, 2);
1619 if (!cmd->static_name.install &&
1620 !cmd->shared_name.install &&
1621 !cmd->module_name.install) {
1622 ERROR("Files to install do not exist\n");
1635 if (!strcmp(ext, "c")) {
1636 /* If we don't already have an idea what our output name will be. */
1637 if (!cmd->basename) {
1638 char *tmp = lt_malloc(strlen(arg) + 4);
1640 strcpy(strrchr(tmp, '.') + 1, "lo");
1642 cmd->basename = tmp;
1644 cmd->fake_output_name = strrchr(cmd->basename, '/');
1645 if (cmd->fake_output_name) {
1646 cmd->fake_output_name++;
1648 cmd->fake_output_name = cmd->basename;
1656 static int parse_output_file_name(const char *arg, command_t *cmd)
1660 char *newarg = NULL;
1663 cmd->fake_output_name = arg;
1665 name = file_name(arg);
1666 ext = strrchr(name, '.');
1669 if (!ext || strcmp(ext, EXE_EXT) == 0) {
1673 cmd->basename = arg;
1674 cmd->output = OUT_PROGRAM;
1675 #if defined(_OSD_POSIX)
1676 cmd->options.pic_mode = PIC_AVOID;
1678 newarg = (char *)lt_malloc(strlen(arg) + 5);
1679 strcpy(newarg, arg);
1682 strcat(newarg, EXE_EXT);
1685 cmd->output_name = newarg;
1690 pathlen = name - arg;
1692 if (strcmp(ext, "la") == 0) {
1693 assert(cmd->mode == MODE_LINK);
1695 cmd->basename = arg;
1696 cmd->static_name.normal = gen_library_name(arg, TYPE_STATIC_LIB);
1697 cmd->shared_name.normal = gen_library_name(arg, TYPE_DYNAMIC_LIB);
1698 cmd->module_name.normal = gen_library_name(arg, TYPE_MODULE_LIB);
1699 cmd->static_name.install = gen_install_name(arg, TYPE_STATIC_LIB);
1700 cmd->shared_name.install = gen_install_name(arg, TYPE_DYNAMIC_LIB);
1701 cmd->module_name.install = gen_install_name(arg, TYPE_MODULE_LIB);
1703 if (!cmd->options.dry_run) {
1706 newname = lt_malloc(strlen(cmd->static_name.normal) + 1);
1708 strcpy(newname, cmd->static_name.normal);
1709 newext = strrchr(newname, '/');
1711 /* Check first to see if the dir already exists! */
1712 safe_mkdir(cmd, ".libs");
1715 safe_mkdir(cmd, newname);
1720 #ifdef TRUNCATE_DLL_NAME
1722 arg = truncate_dll_name(arg);
1726 cmd->output_name = arg;
1730 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1731 assert(cmd->mode == MODE_LINK);
1733 cmd->basename = arg;
1734 cmd->options.shared = SHARE_STATIC;
1735 cmd->output = OUT_STATIC_LIB_ONLY;
1736 cmd->static_name.normal = gen_library_name(arg, TYPE_STATIC_LIB);
1737 cmd->static_name.install = gen_install_name(arg, TYPE_STATIC_LIB);
1739 if (!cmd->options.dry_run) {
1742 newname = lt_malloc(strlen(cmd->static_name.normal) + 1);
1744 strcpy(newname, cmd->static_name.normal);
1745 newext = strrchr(newname, '/');
1747 /* Check first to see if the dir already exists! */
1748 safe_mkdir(cmd, ".libs");
1751 safe_mkdir(cmd, newname);
1756 cmd->output_name = arg;
1760 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1761 assert(cmd->mode == MODE_LINK);
1763 cmd->basename = arg;
1764 cmd->options.shared = SHARE_SHARED;
1765 cmd->output = OUT_DYNAMIC_LIB_ONLY;
1766 cmd->shared_name.normal = gen_library_name(arg, TYPE_DYNAMIC_LIB);
1767 cmd->module_name.normal = gen_library_name(arg, TYPE_MODULE_LIB);
1768 cmd->shared_name.install = gen_install_name(arg, TYPE_DYNAMIC_LIB);
1769 cmd->module_name.install = gen_install_name(arg, TYPE_MODULE_LIB);
1771 if (!cmd->options.dry_run) {
1774 newname = lt_malloc(strlen(cmd->shared_name.normal) + 1);
1776 strcpy(newname, cmd->shared_name.normal);
1777 newext = strrchr(newname, '/');
1779 /* Check first to see if the dir already exists! */
1780 safe_mkdir(cmd, ".libs");
1783 safe_mkdir(cmd, newname);
1788 cmd->output_name = arg;
1792 if (strcmp(ext, "lo") == 0) {
1794 cmd->basename = arg;
1795 cmd->output = OUT_OBJECT;
1796 newarg = (char *)lt_malloc(strlen(arg) + 2);
1797 strcpy(newarg, arg);
1798 newext = strrchr(newarg, '.') + 1;
1799 strcpy(newext, OBJECT_EXT);
1800 cmd->output_name = newarg;
1804 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1805 ERROR("Please build libraries with .la target, not ."
1806 DYNAMIC_LIB_EXT "\n");
1811 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1812 ERROR("Please build libraries with .la target, not ."
1813 STATIC_LIB_EXT "\n");
1821 static const char *automode(const char *arg, command_t *cmd)
1823 if (cmd->mode != MODE_UNKNOWN) return arg;
1825 if (!strcmp(arg, "CC") ||
1826 !strcmp(arg, "CXX")) {
1827 DEBUG("Now in compile mode, guessed from: %s\n", arg);
1829 cmd->mode = MODE_COMPILE;
1831 } else if (!strcmp(arg, "LINK") ||
1832 !strcmp(arg, "LINK.c") ||
1833 !strcmp(arg, "LINK.cxx")) {
1834 DEBUG("Now in linker mode, guessed from: %s\n", arg);
1836 cmd->mode = MODE_LINK;
1844 static void generate_def_file(command_t *cmd)
1846 char def_file[1024];
1847 char implib_file[1024];
1850 char *export_args[1024];
1851 int num_export_args = 0;
1856 if (cmd->output_name) {
1857 strcpy(def_file, cmd->output_name);
1858 strcat(def_file, ".def");
1859 hDef = fopen(def_file, "w");
1862 fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", file_name_stripped(cmd->output_name));
1863 fprintf(hDef, "DATA NONSHARED\n");
1864 fprintf(hDef, "EXPORTS\n");
1867 for (a = 0; a < cmd->num_obj_files; a++) {
1868 cmd_size += strlen(cmd->obj_files[a]) + 1;
1871 cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
1872 cmd = (char *)lt_malloc(cmd_size);
1873 strcpy(cmd, GEN_EXPORTS);
1875 for (a=0; a < cmd->num_obj_files; a++) {
1877 strcat(cmd, cmd->obj_files[a] );
1881 strcat(cmd, def_file);
1883 export_args[num_export_args++] = SHELL_CMD;
1884 export_args[num_export_args++] = "-c";
1885 export_args[num_export_args++] = cmd;
1886 export_args[num_export_args++] = NULL;
1887 external_spawn(cmd, export_args[0], (const char**)export_args);
1888 cmd->arglist[cmd->num_args++] = strdup(def_file);
1890 /* Now make an import library for the dll */
1891 num_export_args = 0;
1892 export_args[num_export_args++] = DEF2IMPLIB_CMD;
1893 export_args[num_export_args++] = "-o";
1895 strcpy(implib_file, ".libs/");
1896 strcat(implib_file, cmd->basename);
1897 ext = strrchr(implib_file, '.');
1903 strcat(implib_file, ".");
1904 strcat(implib_file, STATIC_LIB_EXT);
1906 export_args[num_export_args++] = implib_file;
1907 export_args[num_export_args++] = def_file;
1908 export_args[num_export_args++] = NULL;
1909 external_spawn(cmd, export_args[0], (const char**)export_args);
1917 static const char* expand_path(const char *relpath)
1919 char foo[PATH_MAX], *newpath;
1921 getcwd(foo, PATH_MAX-1);
1922 newpath = (char*)lt_malloc(strlen(foo)+strlen(relpath)+2);
1923 strcpy(newpath, foo);
1924 strcat(newpath, "/");
1925 strcat(newpath, relpath);
1930 static void link_fixup(command_t *cmd)
1932 /* If we were passed an -rpath directive, we need to build
1933 * shared objects too. Otherwise, we should only create static
1936 if (!cmd->install_path && (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
1937 cmd->output == OUT_MODULE || cmd->output == OUT_LIB)) {
1938 if (cmd->options.shared == SHARE_SHARED) {
1939 cmd->install_path = LIBDIR;
1943 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
1944 cmd->output == OUT_MODULE ||
1945 cmd->output == OUT_LIB) {
1947 push_count_chars(cmd->shared_opts.normal, "-o");
1948 if (cmd->output == OUT_MODULE) {
1949 push_count_chars(cmd->shared_opts.normal, cmd->module_name.normal);
1951 push_count_chars(cmd->shared_opts.normal, cmd->shared_name.normal);
1952 #ifdef DYNAMIC_INSTALL_NAME
1953 push_count_chars(cmd->shared_opts.normal, DYNAMIC_INSTALL_NAME);
1955 if (!cmd->install_path) {
1956 ERROR("Installation mode requires -rpath\n");
1961 char *tmp = lt_malloc(PATH_MAX);
1962 strcpy(tmp, cmd->install_path);
1964 if (cmd->shared_name.install) {
1965 strcat(tmp, strrchr(cmd->shared_name.install, '/'));
1967 strcat(tmp, strrchr(cmd->shared_name.normal, '/'));
1970 push_count_chars(cmd->shared_opts.normal, tmp);
1975 append_count_chars(cmd->shared_opts.normal, cmd->obj_files);
1976 append_count_chars(cmd->shared_opts.normal, cmd->shared_opts.dependencies);
1978 if (cmd->options.export_all) {
1980 generate_def_file(cmd);
1985 if (cmd->output == OUT_LIB || cmd->output == OUT_STATIC_LIB_ONLY) {
1986 push_count_chars(cmd->static_opts.normal, "-o");
1987 push_count_chars(cmd->static_opts.normal, cmd->output_name);
1990 if (cmd->output == OUT_PROGRAM) {
1991 if (cmd->output_name) {
1992 push_count_chars(cmd->arglist, "-o");
1993 push_count_chars(cmd->arglist, cmd->output_name);
1994 append_count_chars(cmd->arglist, cmd->obj_files);
1995 append_count_chars(cmd->arglist, cmd->shared_opts.dependencies);
1996 add_dynamic_link_opts(cmd, cmd->arglist);
2001 static void post_parse_fixup(command_t *cmd)
2007 if (cmd->options.pic_mode != PIC_AVOID) {
2008 push_count_chars(cmd->arglist, PIC_FLAG);
2011 if (cmd->output_name) {
2012 push_count_chars(cmd->arglist, "-o");
2013 push_count_chars(cmd->arglist, cmd->output_name);
2020 if (cmd->output == OUT_LIB) {
2028 if (cmd->output == OUT_OBJECT ||
2029 cmd->output == OUT_PROGRAM ||
2030 cmd->output == OUT_LIB ||
2031 cmd->output == OUT_DYNAMIC_LIB_ONLY) {
2032 push_count_chars(cmd->arglist, "-Zomf");
2036 if (cmd->options.shared &&
2037 (cmd->output == OUT_OBJECT ||
2038 cmd->output == OUT_LIB ||
2039 cmd->output == OUT_DYNAMIC_LIB_ONLY)) {
2041 push_count_chars(cmd->arglist, SHARE_SW);
2046 static int run_mode(command_t *cmd)
2049 count_chars *cctemp;
2051 cctemp = (count_chars*)lt_malloc(sizeof(count_chars));
2052 init_count_chars(cctemp);
2057 rv = run_command(cmd, cmd->arglist);
2058 if (rv) goto finish;
2061 /* Well, we'll assume it's a file going to a directory... */
2062 /* For brain-dead install-sh based scripts, we have to repeat
2063 * the command N-times. install-sh should die.
2065 if (!cmd->output_name) {
2066 rv = run_command(cmd, cmd->arglist);
2067 if (rv) goto finish;
2069 if (cmd->output_name) {
2070 append_count_chars(cctemp, cmd->arglist);
2071 insert_count_chars(cctemp,
2074 rv = run_command(cmd, cctemp);
2075 if (rv) goto finish;
2076 clear_count_chars(cctemp);
2078 if (cmd->static_name.install) {
2079 append_count_chars(cctemp, cmd->arglist);
2080 insert_count_chars(cctemp,
2081 cmd->static_name.install,
2083 rv = run_command(cmd, cctemp);
2084 if (rv) goto finish;
2085 #if defined(__APPLE__) && defined(RANLIB)
2086 /* From the Apple libtool(1) manpage on Tiger/10.4:
2088 * With the way libraries used to be created, errors were possible
2089 * if the library was modified with ar(1) and the table of
2090 * contents was not updated by rerunning ranlib(1). Thus the
2091 * link editor, ld, warns when the modification date of a library
2092 * is more recent than the creation date of its table of
2093 * contents. Unfortunately, this means that you get the warning
2094 * even if you only copy the library.
2097 * This means that when we install the static archive, we need to
2098 * rerun ranlib afterwards.
2100 const char *lib_args[3], *static_lib_name;
2106 len1 = strlen(cmd->arglist->vals[cmd->arglist->num - 1]);
2108 static_lib_name = file_name(cmd->static_name.install);
2109 len2 = strlen(static_lib_name);
2111 tmp = lt_malloc(len1 + len2 + 2);
2113 snprintf(tmp, len1 + len2 + 2, "%s/%s",
2114 cmd->arglist->vals[cmd->arglist->num - 1],
2117 lib_args[0] = RANLIB;
2121 external_spawn(cmd, RANLIB, lib_args);
2126 clear_count_chars(cctemp);
2128 if (cmd->shared_name.install) {
2129 append_count_chars(cctemp, cmd->arglist);
2130 insert_count_chars(cctemp, cmd->shared_name.install,
2132 rv = run_command(cmd, cctemp);
2133 if (rv) goto finish;
2134 clear_count_chars(cctemp);
2136 if (cmd->module_name.install) {
2137 append_count_chars(cctemp, cmd->arglist);
2138 insert_count_chars(cctemp, cmd->module_name.install,
2140 rv = run_command(cmd, cctemp);
2141 if (rv) goto finish;
2142 clear_count_chars(cctemp);
2146 if (cmd->output == OUT_STATIC_LIB_ONLY ||
2147 cmd->output == OUT_LIB) {
2149 const char *lib_args[3];
2151 /* Removes compiler! */
2152 cmd->program = LIBRARIAN;
2153 push_count_chars(cmd->program_opts, LIBRARIAN_OPTS);
2154 push_count_chars(cmd->program_opts, cmd->static_name.normal);
2156 rv = run_command(cmd, cmd->obj_files);
2157 if (rv) goto finish;
2160 lib_args[0] = RANLIB;
2161 lib_args[1] = cmd->static_name.normal;
2163 external_spawn(cmd, RANLIB, lib_args);
2167 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
2168 cmd->output == OUT_MODULE ||
2169 cmd->output == OUT_LIB) {
2170 cmd->program = NULL;
2171 clear_count_chars(cmd->program_opts);
2173 append_count_chars(cmd->program_opts, cmd->arglist);
2174 if (cmd->output == OUT_MODULE) {
2176 push_count_chars(cmd->program_opts, MODULE_OPTS);
2180 push_count_chars(cmd->program_opts, SHARED_OPTS);
2182 #ifdef dynamic_link_version_func
2183 push_count_chars(cmd->program_opts,
2184 dynamic_link_version_func(cmd->version_info));
2187 add_dynamic_link_opts(cmd, cmd->program_opts);
2189 rv = run_command(cmd, cmd->shared_opts.normal);
2190 if (rv) goto finish;
2192 if (cmd->output == OUT_PROGRAM) {
2193 rv = run_command(cmd, cmd->arglist);
2194 if (rv) goto finish;
2199 char *l, libpath[PATH_MAX];
2201 if (strlen(cmd->arglist->vals[0]) >= PATH_MAX) {
2202 ERROR("Libpath too long no buffer space");
2208 strcpy(libpath, cmd->arglist->vals[0]);
2209 add_dotlibs(libpath);
2210 l = strrchr(libpath, '/');
2211 if (!l) l = strrchr(libpath, '\\');
2219 l = "./build/lib/.libs";
2220 setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
2221 rv = run_command(cmd, cmd->arglist);
2222 if (rv) goto finish;
2236 static void cleanup_tmp_dir(const char *dirname)
2239 struct dirent *entry;
2240 char fullname[1024];
2242 dir = opendir(dirname);
2247 if ((strlen(dirname) + 1 + sizeof(entry->d_name)) >= sizeof(fullname)) {
2248 ERROR("Dirname too long, out of buffer space");
2250 (void) closedir(dir);
2254 while ((entry = readdir(dir)) != NULL) {
2255 if (entry->d_name[0] != '.') {
2256 strcpy(fullname, dirname);
2257 strcat(fullname, "/");
2258 strcat(fullname, entry->d_name);
2259 (void) remove(fullname);
2265 (void) closedir(dir);
2268 static void cleanup_tmp_dirs(command_t *cmd)
2272 for (d = 0; d < cmd->tmp_dirs->num; d++) {
2273 cleanup_tmp_dir(cmd->tmp_dirs->vals[d]);
2277 static int ensure_fake_uptodate(command_t *cmd)
2279 /* FIXME: could do the stat/touch here, but nah... */
2280 const char *touch_args[3];
2282 if (cmd->mode == MODE_INSTALL) {
2285 if (!cmd->fake_output_name) {
2289 touch_args[0] = "touch";
2290 touch_args[1] = cmd->fake_output_name;
2291 touch_args[2] = NULL;
2292 return external_spawn(cmd, "touch", touch_args);
2295 /* Store the install path in the *.la file */
2296 static int add_for_runtime(command_t *cmd)
2298 if (cmd->mode == MODE_INSTALL) {
2301 if (cmd->output == OUT_DYNAMIC_LIB_ONLY ||
2302 cmd->output == OUT_LIB) {
2303 FILE *f=fopen(cmd->fake_output_name,"w");
2307 fprintf(f,"%s\n", cmd->install_path);
2311 return(ensure_fake_uptodate(cmd));
2315 static void parse_args(int argc, char *argv[], command_t *cmd)
2318 const char *arg, *base;
2322 * We now take a major step past libtool.
2324 * IF there's no "--mode=...", AND we recognise
2325 * the binary as a "special" name, THEN replace it
2326 * with the correct one, and set the correct mode.
2328 * For example if were called 'CC' then we know we should
2329 * probably be compiling stuff.
2331 base = file_name(argv[0]);
2332 arg = automode(base, cmd);
2334 push_count_chars(cmd->arglist, arg);
2336 assert(cmd->mode != MODE_UNKNOWN);
2340 * We first pass over the command-line arguments looking for
2341 * "--mode", etc. If so, then use the libtool compatibility
2342 * method for building the software. Otherwise, auto-detect it
2343 * via "-o" and the extensions.
2346 if (cmd->mode == MODE_UNKNOWN) for (a = 1; a < argc; a++) {
2349 if (strncmp(arg, "--mode=", 7) == 0) {
2355 * Stop if we get another magic method
2358 ((strncmp(arg, "LINK", 4) == 0) ||
2359 (strcmp(arg, "CC") == 0) ||
2360 (strcmp(arg, "CXX") == 0))) {
2365 if (strncmp(arg, "-o", 2) == 0) {
2371 * There were no magic args or an explicit --mode= but we did
2372 * find an output file, so guess what mode were meant to be in
2373 * from its extension.
2376 arg = strrchr(base, '.');
2378 cmd->mode = MODE_LINK;
2379 push_count_chars(cmd->arglist, LINK_C);
2382 else if (strcmp(arg, EXE_EXT) == 0) {
2383 cmd->mode = MODE_LINK;
2384 push_count_chars(cmd->arglist, LINK_C);
2387 else if (strcmp(arg + 1, DYNAMIC_LIB_EXT) == 0) {
2388 cmd->mode = MODE_LINK;
2389 push_count_chars(cmd->arglist, LINK_C);
2391 else if (strcmp(arg + 1, STATIC_LIB_EXT) == 0) {
2392 cmd->mode = MODE_LINK;
2393 push_count_chars(cmd->arglist, LINK_C);
2395 else if (strcmp(arg + 1, "la") == 0) {
2396 cmd->mode = MODE_LINK;
2397 push_count_chars(cmd->arglist, LINK_C);
2399 else if ((strcmp(arg + 1, "lo") == 0) ||
2400 (strcmp(arg + 1, "o") == 0)) {
2401 cmd->mode = MODE_COMPILE;
2402 push_count_chars(cmd->arglist, CC);
2406 for (a = 1; a < argc; a++) {
2410 if (arg[0] == '-') {
2412 * Double dashed (long) single dash (short)
2414 arg_used = (arg[1] == '-') ?
2415 parse_long_opt(arg + 2, cmd) :
2416 parse_short_opt(arg + 1, cmd);
2418 if (arg_used) continue;
2421 * We haven't done anything with it yet, but
2422 * there are still some arg/value pairs.
2424 * Try some of the more complicated short opts...
2428 * We found an output file!
2430 if ((arg[1] == 'o') && (arg[2] == '\0')) {
2432 arg_used = parse_output_file_name(arg,
2435 * -MT literal dependency
2437 } else if (!strcmp(arg + 1, "MT")) {
2438 DEBUG("Adding: %s\n", arg);
2440 push_count_chars(cmd->arglist, arg);
2443 NOTICE(" %s\n", arg);
2445 push_count_chars(cmd->arglist, arg);
2448 * Runtime library search path
2450 } else if (!strcmp(arg + 1, "rpath")) {
2451 /* Aha, we should try to link both! */
2452 cmd->install_path = argv[++a];
2455 } else if (!strcmp(arg + 1, "release")) {
2456 /* Store for later deciphering */
2457 cmd->version_info = argv[++a];
2460 } else if (!strcmp(arg + 1, "version-info")) {
2461 /* Store for later deciphering */
2462 cmd->version_info = argv[++a];
2465 } else if (!strcmp(arg + 1,
2466 "export-symbols-regex")) {
2467 /* Skip the argument. */
2471 } else if (!strcmp(arg + 1, "undefined")) {
2472 cmd->undefined_flag = argv[++a];
2475 * Add dir to runtime library search path.
2477 } else if ((arg[1] == 'R') && !arg[2]) {
2479 add_runtime_dir_lib(argv[++a], cmd);
2484 * Ok.. the argument doesn't begin with a dash
2485 * maybe it's an input file.
2487 * Check its extension to see if it's a known input
2488 * file and verify it exists.
2491 arg_used = parse_input_file_name(arg, cmd);
2495 * If we still don't have a run mode, look for a magic
2496 * program name CC, LINK, or whatever. Then replace that
2497 * with the name of the real program we want to run.
2500 if ((cmd->arglist->num == 0) &&
2501 (cmd->mode == MODE_UNKNOWN)) {
2502 arg = automode(arg, cmd);
2505 DEBUG("Adding: %s\n", arg);
2507 push_count_chars(cmd->arglist, arg);
2513 int main(int argc, char *argv[])
2518 memset(&cmd, 0, sizeof(cmd));
2520 cmd.options.pic_mode = PIC_UNKNOWN;
2521 cmd.mode = MODE_UNKNOWN;
2522 cmd.output = OUT_GENERAL;
2525 * Initialise the various argument lists
2527 cmd.program_opts = alloc_countchars();
2528 cmd.arglist = alloc_countchars();
2529 cmd.tmp_dirs = alloc_countchars();
2530 cmd.obj_files = alloc_countchars();
2531 cmd.dep_rpaths = alloc_countchars();
2532 cmd.rpaths = alloc_countchars();
2533 cmd.static_opts.normal = alloc_countchars();
2534 cmd.shared_opts.normal = alloc_countchars();
2535 cmd.shared_opts.dependencies = alloc_countchars();
2538 * Fill up the various argument lists
2540 parse_args(argc, argv, &cmd);
2541 post_parse_fixup(&cmd);
2544 * We couldn't figure out which mode to operate in
2546 if (cmd.mode == MODE_UNKNOWN) {
2550 rc = run_mode(&cmd);
2552 add_for_runtime(&cmd);
2555 cleanup_tmp_dirs(&cmd);