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>
22 #if !defined(__MINGW32__)
31 # define SHELL_CMD "sh"
32 # define GEN_EXPORTS "emxexp"
33 # define DEF2IMPLIB_CMD "emximp"
34 # define SHARE_SW "-Zdll -Zmtd"
36 # define TRUNCATE_DLL_NAME
37 # define DYNAMIC_LIB_EXT "dll"
38 # define EXE_EXT ".exe"
41 /* OMF is the native format under OS/2 */
42 # define STATIC_LIB_EXT "lib"
43 # define OBJECT_EXT "obj"
44 # define LIBRARIAN "emxomfar"
45 # define LIBRARIAN_OPTS "cr"
47 /* but the alternative, a.out, can fork() which is sometimes necessary */
48 # define STATIC_LIB_EXT "a"
49 # define OBJECT_EXT "o"
50 # define LIBRARIAN "ar"
51 # define LIBRARIAN_OPTS "cr"
55 #if defined(__APPLE__)
56 # define SHELL_CMD "/bin/sh"
57 # define DYNAMIC_LIB_EXT "dylib"
58 # define MODULE_LIB_EXT "bundle"
59 # define STATIC_LIB_EXT "a"
60 # define OBJECT_EXT "o"
61 # define LIBRARIAN "ar"
62 # define LIBRARIAN_OPTS "cr"
63 /* man libtool(1) documents ranlib option of -c. */
64 # define RANLIB "ranlib"
65 # define PIC_FLAG "-fPIC -fno-common"
66 # define SHARED_OPTS "-dynamiclib"
67 # define MODULE_OPTS "-bundle -dynamic"
68 # define DYNAMIC_LINK_OPTS "-flat_namespace"
69 # define DYNAMIC_LINK_UNDEFINED "-undefined suppress"
70 # define dynamic_link_version_func darwin_dynamic_link_function
71 # define DYNAMIC_INSTALL_NAME "-install_name"
72 # define DYNAMIC_LINK_NO_INSTALL "-dylib_file"
74 /*-install_name /Users/jerenk/apache-2.0-cvs/lib/libapr.0.dylib -compatibility_version 1 -current_version 1.0 */
75 # define LD_LIBRARY_PATH "DYLD_LIBRARY_PATH"
76 # define LD_LIBRARY_PATH_LOCAL "DYLD_FALLBACK_LIBRARY_PATH"
79 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
80 # define SHELL_CMD "/bin/sh"
81 # define DYNAMIC_LIB_EXT "so"
82 # define MODULE_LIB_EXT "so"
83 # define STATIC_LIB_EXT "a"
84 # define OBJECT_EXT "o"
85 # define LIBRARIAN "ar"
86 # define LIBRARIAN_OPTS "cr"
87 # define RANLIB "ranlib"
88 # define PIC_FLAG "-fPIC"
89 # define RPATH "-rpath"
90 # define SHARED_OPTS "-shared"
91 # define MODULE_OPTS "-shared"
92 # define DYNAMIC_LINK_OPTS "-export-dynamic"
93 # define LINKER_FLAG_PREFIX "-Wl,"
95 # define LD_RUN_PATH "LD_RUN_PATH"
96 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
97 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
101 # define SHELL_CMD "/bin/sh"
102 # define DYNAMIC_LIB_EXT "so"
103 # define MODULE_LIB_EXT "so"
104 # define STATIC_LIB_EXT "a"
105 # define OBJECT_EXT "o"
106 # define LIBRARIAN "ar"
107 # define LIBRARIAN_OPTS "cr"
108 # define RANLIB "ranlib"
109 # define PIC_FLAG "-KPIC"
111 # define SHARED_OPTS "-G"
112 # define MODULE_OPTS "-G"
113 # define DYNAMIC_LINK_OPTS ""
114 # define LINKER_FLAG_NO_EQUALS
116 # define HAS_REALPATH
117 # define LD_RUN_PATH "LD_RUN_PATH"
118 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
119 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
122 #if defined(_OSD_POSIX)
123 # define SHELL_CMD "/usr/bin/sh"
124 # define DYNAMIC_LIB_EXT "so"
125 # define MODULE_LIB_EXT "so"
126 # define STATIC_LIB_EXT "a"
127 # define OBJECT_EXT "o"
128 # define LIBRARIAN "ar"
129 # define LIBRARIAN_OPTS "cr"
130 # define SHARED_OPTS "-G"
131 # define MODULE_OPTS "-G"
132 # define LINKER_FLAG_PREFIX "-Wl,"
133 # define NEED_SNPRINTF
136 #if defined(sinix) && defined(mips) && defined(__SNI_TARG_UNIX)
137 # define SHELL_CMD "/usr/bin/sh"
138 # define DYNAMIC_LIB_EXT "so"
139 # define MODULE_LIB_EXT "so"
140 # define STATIC_LIB_EXT "a"
141 # define OBJECT_EXT "o"
142 # define LIBRARIAN "ar"
143 # define LIBRARIAN_OPTS "cr"
144 # define RPATH "-Brpath"
145 # define SHARED_OPTS "-G"
146 # define MODULE_OPTS "-G"
147 # define DYNAMIC_LINK_OPTS "-Wl,-Blargedynsym"
148 # define LINKER_FLAG_PREFIX "-Wl,"
149 # define NEED_SNPRINTF
150 # define LD_RUN_PATH "LD_RUN_PATH"
151 # define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
152 # define LD_LIBRARY_PATH_LOCAL LD_LIBRARY_PATH
155 #if defined(__MINGW32__)
156 # define SHELL_CMD "sh"
157 # define DYNAMIC_LIB_EXT "dll"
158 # define MODULE_LIB_EXT "dll"
159 # define STATIC_LIB_EXT "a"
160 # define OBJECT_EXT "o"
161 # define LIBRARIAN "ar"
162 # define LIBRARIAN_OPTS "cr"
163 # define RANLIB "ranlib"
164 # define LINKER_FLAG_PREFIX "-Wl,"
165 # define SHARED_OPTS "-shared"
166 # define MODULE_OPTS "-shared"
167 # define MKDIR_NO_UMASK
168 # define EXE_EXT ".exe"
184 #define LINK_cxx "g++"
188 #define LIBDIR "/usr/local/lib"
191 #define OBJDIR ".libs"
194 #error Unsupported platform: Please add defines for SHELL_CMD etc. for your platform.
206 #define PATH_MAX 1024
210 /* We want to say we are libtool 1.4 for shlibtool compatibility. */
211 #define VERSION "1.4"
227 otDynamicLibraryOnly,
263 count_chars *install;
264 count_chars *dependencies;
270 enum shared_mode_e shared;
273 enum pic_mode_e pic_mode;
279 enum tool_mode_t mode;
280 enum output_t output;
284 char *fake_output_name;
287 const char *install_path;
288 const char *compiler;
290 count_chars *program_opts;
292 count_chars *arglist;
293 count_chars *tmp_dirs;
294 count_chars *obj_files;
295 count_chars *dep_rpaths;
298 library_name static_name;
299 library_name shared_name;
300 library_name module_name;
302 library_opts static_opts;
303 library_opts shared_opts;
305 const char *version_info;
306 const char *undefined_flag;
310 static void add_rpath(count_chars *cc, const char *path);
313 static void usage(int code)
315 printf("Usage: jlibtool [OPTIONS...] COMMANDS...\n");
316 printf("jlibtool is a replacement for GNU libtool with similar functionality.\n\n");
318 printf(" --config show all configuration variables\n");
319 printf(" --debug enable verbose shell tracing\n");
320 printf(" --dry-run display commands without modifying any files\n");
321 printf(" --help display this help message and exit\n");
322 printf(" --mode=MODE use operational mode MODE (you *must* set mode)\n");
324 printf(" --silent don't print informational messages\n");
325 printf(" --tag=TAG Ignored for libtool compatibility\n");
326 printf(" --version print version information\n");
329 printf(" --shared Build shared libraries when using --mode=link\n");
330 printf(" --export-all Try to export 'def' file on some platforms\n");
332 printf("\nMODE must be one of the following:\n\n");
333 printf(" compile compile a source file into a jlibtool object\n");
334 printf(" execute automatically set library path, then run a program\n");
335 printf(" install install libraries or executables\n");
336 printf(" link create a library or an executable\n");
338 printf("\nMODE-ARGS can be the following:\n\n");
339 printf(" -export-dynamic accepted and ignored\n");
340 printf(" -module create a module when linking\n");
341 printf(" -shared create a shared library when linking\n");
342 printf(" -prefer-pic prefer position-independent-code when compiling\n");
343 printf(" -prefer-non-pic prefer non position-independent-code when compiling\n");
344 printf(" -static create a static library when linking\n");
345 printf(" -no-install link libraries locally\n");
346 printf(" -rpath arg Set install path for shared libraries\n");
347 printf(" -l arg pass '-l arg' to the link stage\n");
348 printf(" -L arg pass '-L arg' to the link stage\n");
349 printf(" -R dir add 'dir' to runtime library search path.\n");
350 printf(" -Zexe accepted and ignored\n");
351 printf(" -avoid-version accepted and ignored\n");
356 #if defined(NEED_SNPRINTF)
357 /* Write at most n characters to the buffer in str, return the
358 * number of chars written or -1 if the buffer would have been
361 * This is portable to any POSIX-compliant system has /dev/null
364 static int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
369 f = fopen("/dev/null","w");
373 setvbuf( f, str, _IOFBF, n );
375 res = vfprintf( f, fmt, ap );
377 if ( res > 0 && res < n ) {
378 res = vsprintf( str, fmt, ap );
382 static int snprintf( char *str, size_t n, const char *fmt, ... )
388 res = vsnprintf( str, n, fmt, ap );
394 static void init_count_chars(count_chars *cc)
396 cc->vals = (const char**)malloc(PATH_MAX*sizeof(char*));
400 static void clear_count_chars(count_chars *cc)
403 for (i = 0; i < cc->num; i++) {
410 static void push_count_chars(count_chars *cc, const char *newval)
412 cc->vals[cc->num++] = newval;
415 static void pop_count_chars(count_chars *cc)
420 static void insert_count_chars(count_chars *cc, const char *newval, int position)
424 for (i = cc->num; i > position; i--) {
425 cc->vals[i] = cc->vals[i-1];
428 cc->vals[position] = newval;
432 static void append_count_chars(count_chars *cc, count_chars *cctoadd)
435 for (i = 0; i < cctoadd->num; i++) {
436 if (cctoadd->vals[i]) {
437 push_count_chars(cc, cctoadd->vals[i]);
442 static const char *flatten_count_chars(count_chars *cc, int space)
448 for (i = 0; i < cc->num; i++) {
450 size += strlen(cc->vals[i]) + 1;
457 newval = (char*)malloc(size + 1);
460 for (i = 0; i < cc->num; i++) {
462 strcat(newval, cc->vals[i]);
472 static char *shell_esc(const char *str)
477 const unsigned char *s;
479 cmd = (char *)malloc(2 * strlen(str) + 3);
480 d = (unsigned char *)cmd;
481 s = (const unsigned char *)str;
492 else if (*s == '\\' || (*s == ' ' && (in_quote % 2))) {
506 static int external_spawn(command_t *cmd, const char *file, const char **argv)
508 file = file; /* -Wunused */
510 if (!cmd->options.silent) {
511 const char **argument = argv;
512 printf("Executing: ");
514 printf("%s ", *argument);
520 if (cmd->options.dry_run) {
523 #if defined(__EMX__) || defined(__MINGW32__)
524 return spawnvp(P_WAIT, argv[0], argv);
530 return execvp(argv[0], (char**)argv);
534 waitpid(pid, &statuscode, 0);
535 if (WIFEXITED(statuscode)) {
536 return WEXITSTATUS(statuscode);
544 static int run_command(command_t *cmd_data, count_chars *cc)
547 const char *spawn_args[4];
550 init_count_chars(&tmpcc);
552 if (cmd_data->program) {
553 push_count_chars(&tmpcc, cmd_data->program);
556 append_count_chars(&tmpcc, cmd_data->program_opts);
558 append_count_chars(&tmpcc, cc);
560 command = shell_esc(flatten_count_chars(&tmpcc, 1));
562 spawn_args[0] = SHELL_CMD;
563 spawn_args[1] = "-c";
564 spawn_args[2] = command;
565 spawn_args[3] = NULL;
566 return external_spawn(cmd_data, spawn_args[0], spawn_args);
570 * print configuration
571 * shlibpath_var is used in configure.
573 #define printc(_x,_y) if (!value || !strcmp(value, _x)) printf(_x "=\"%s\"\n", _y)
575 static void print_config(const char *value)
578 printc("runpath_var", LD_RUN_PATH);
580 #ifdef LD_LIBRARY_PATH
581 printc("shlibpath_var", LD_LIBRARY_PATH);
583 #ifdef LD_LIBRARY_PATH_LOCAL
584 printc("shlocallibpath_var", LD_LIBRARY_PATH_LOCAL);
587 printc("SHELL", SHELL_CMD);
590 printc("objext", OBJECT_EXT);
593 printc("objdir", OBJDIR);
595 #ifdef DYNAMIC_LIB_EXT
596 /* add a '.' prefix because libtool does that. */
597 printc("shrext_cmds", "echo ." DYNAMIC_LIB_EXT);
598 /* add a '.' prefix because libtool does that. */
599 printc("shrext", "." DYNAMIC_LIB_EXT);
602 printc("exeext", EXE_EXT);
604 #ifdef STATIC_LIB_EXT
605 printc("libext", STATIC_LIB_EXT);
608 printc("AR", LIBRARIAN);
610 #ifdef LIBRARIAN_OPTS
611 printc("AR_FLAGS", LIBRARIAN_OPTS);
613 #ifdef LINKER_FLAG_PREFIX
614 printc("wl", LINKER_FLAG_PREFIX);
617 printc("ranlib", RANLIB);
622 * Add a directory to the runtime library search path.
624 static void add_runtimedirlib(char *arg, command_t *cmd_data)
627 add_rpath(cmd_data->shared_opts.dependencies, arg);
629 arg = arg; /* -Wunused */
634 static int parse_long_opt(char *arg, command_t *cmd_data)
636 char *equal_pos = strchr(arg, '=');
641 strncpy(var, arg, equal_pos - arg);
642 var[equal_pos - arg] = 0;
643 strcpy(value, equal_pos + 1);
649 if (strcmp(var, "silent") == 0) {
650 cmd_data->options.silent = 1;
651 } else if (strcmp(var, "quiet") == 0) {
652 cmd_data->options.silent = 1;
653 } else if (strcmp(var, "debug") == 0) {
654 cmd_data->options.debug = 1;
655 } else if (strcmp(var, "mode") == 0) {
656 if (cmd_data->mode != mUnknown) {
657 fprintf(stderr, "Cannot set --mode twice\n");
661 if (strcmp(value, "compile") == 0) {
662 cmd_data->mode = mCompile;
663 cmd_data->output = otObject;
665 } else if (strcmp(value, "link") == 0) {
666 cmd_data->mode = mLink;
667 cmd_data->output = otLibrary;
669 } else if (strcmp(value, "install") == 0) {
670 cmd_data->mode = mInstall;
672 } else if (strcmp(value, "execute") == 0) {
673 cmd_data->mode = mExecute;
676 printf("Unknown mode \"%s\"\n", value);
678 } else if (strcmp(var, "shared") == 0) {
679 if ((cmd_data->mode == mLink) &&
680 (cmd_data->output == otGeneral)) {
681 cmd_data->output = otDynamicLibraryOnly;
683 cmd_data->options.shared = share_SHARED;
684 } else if (strcmp(var, "export-all") == 0) {
685 cmd_data->options.export_all = 1;
686 } else if (strcmp(var, "dry-run") == 0) {
687 printf("Dry-run mode on!\n");
688 cmd_data->options.dry_run = 1;
689 } else if (strcmp(var, "version") == 0) {
690 printf("Version " VERSION "\n");
691 } else if (strcmp(var, "help") == 0) {
693 } else if (strcmp(var, "config") == 0) {
696 } else if (strcmp(var, "tag") == 0) {
697 if (strcmp(value, "CC") == 0) {
700 if (strcmp(value, "CXX") == 0) {
710 /* Return 1 if we eat it. */
711 static int parse_short_opt(char *arg, command_t *cmd_data)
713 if (strcmp(arg, "export-dynamic") == 0) {
714 cmd_data->options.export_dynamic = 1;
718 if (strcmp(arg, "module") == 0) {
719 cmd_data->output = otModule;
723 if (strcmp(arg, "shared") == 0) {
724 if (cmd_data->mode == mLink) {
725 cmd_data->output = otDynamicLibraryOnly;
727 cmd_data->options.shared = share_SHARED;
731 if (strcmp(arg, "Zexe") == 0) {
735 if (strcmp(arg, "avoid-version") == 0) {
739 if (strcmp(arg, "prefer-pic") == 0) {
740 cmd_data->options.pic_mode = pic_PREFER;
744 if (strcmp(arg, "prefer-non-pic") == 0) {
745 cmd_data->options.pic_mode = pic_AVOID;
749 if (strcmp(arg, "static") == 0) {
750 if ((cmd_data->mode == mLink) &&
751 (cmd_data->output == otLibrary)) {
752 cmd_data->output = otStaticLibraryOnly;
754 cmd_data->options.shared = share_STATIC;
758 if (cmd_data->mode == mLink) {
759 if (strcmp(arg, "no-install") == 0) {
760 cmd_data->options.no_install = 1;
763 if (arg[0] == 'L' || arg[0] == 'l') {
766 push_count_chars(cmd_data->shared_opts.dependencies, arg);
768 } else if (arg[0] == 'R' && arg[1]) {
769 /* -Rdir Add dir to runtime library search path. */
770 add_runtimedirlib(&arg[1], cmd_data);
777 #ifdef TRUNCATE_DLL_NAME
778 static char *truncate_dll_name(char *path)
780 /* Cut DLL name down to 8 characters after removing any mod_ prefix */
781 char *tmppath = strdup(path);
782 char *newname = strrchr(tmppath, '/') + 1;
783 char *ext = strrchr(newname, '.');
791 if (strncmp(newname, "mod_", 4) == 0) {
792 strcpy(newname, newname + 4);
797 strcpy(newname + 8, strchr(newname, '.'));
804 static long safe_strtol(const char *nptr, const char **endptr, int base)
810 rv = strtol(nptr, (char**)endptr, 10);
812 if (errno == ERANGE) {
819 static void safe_mkdir(const char *path)
824 old_umask = umask(0);
827 #ifdef MKDIR_NO_UMASK
828 status = mkdir(path);
830 status = mkdir(path, ~old_umask);
832 if ((status < 0) && (errno != EEXIST)) {
833 printf("Warning: mkdir of %s failed\n", path);
837 /* returns just a file's name without the path */
838 static const char *jlibtool_basename(const char *fullpath)
840 const char *name = strrchr(fullpath, '/');
843 name = strrchr(fullpath, '\\');
856 /* returns just a file's name without path or extension */
857 static const char *nameof(const char *fullpath)
862 name = jlibtool_basename(fullpath);
863 ext = strrchr(name, '.');
867 trimmed = malloc(ext - name + 1);
868 strncpy(trimmed, name, ext - name);
869 trimmed[ext-name] = 0;
877 /* version_info is in the form of MAJOR:MINOR:PATCH */
878 static const char *darwin_dynamic_link_function(const char *version_info)
881 long major, minor, patch;
888 major = safe_strtol(version_info, &version_info, 10);
891 if (version_info[0] == ':') {
895 minor = safe_strtol(version_info, &version_info, 10);
898 if (version_info[0] == ':') {
902 patch = safe_strtol(version_info, &version_info, 10);
908 /* Avoid -dylib_compatibility_version must be greater than zero errors. */
912 newarg = (char*)malloc(100);
914 "-compatibility_version %ld -current_version %ld.%ld",
915 major, major, minor);
922 * Add a '.libs/' to the buffer. The caller ensures that
923 * The buffer is large enough to handle 6 extra characters.
925 static void add_dotlibs(char *buffer)
927 char *name = strrchr(buffer, '/');
931 strcpy(buffer, ".libs/");
938 memmove(name + 6, name, strlen(name));
939 memcpy(name, ".libs/", 6);
942 static char *gen_library_name(const char *name, enum lib_type genlib)
944 char *newarg, *newext;
946 newarg = (char *)calloc(strlen(name) + 11, 1);
948 if (genlib == type_MODULE_LIB && strncmp(name, "lib", 3) == 0) {
952 if (genlib == type_MODULE_LIB) {
953 strcpy(newarg, jlibtool_basename(name));
956 strcpy(newarg, name);
959 newext = strrchr(newarg, '.') + 1;
966 case type_STATIC_LIB:
967 strcpy(newext, STATIC_LIB_EXT);
969 case type_DYNAMIC_LIB:
970 strcpy(newext, DYNAMIC_LIB_EXT);
972 case type_MODULE_LIB:
973 strcpy(newext, MODULE_LIB_EXT);
985 static char *gen_install_name(const char *name, enum lib_type genlib)
991 newname = gen_library_name(name, genlib);
992 if (!newname) return NULL;
994 /* Check if it exists. If not, return NULL. */
995 rv = stat(newname, &sb);
1005 static const char *check_object_exists(command_t *cmd, const char *arg, int arglen)
1010 newarg = (char *)malloc(arglen + 10);
1011 memcpy(newarg, arg, arglen);
1013 ext = newarg + arglen;
1022 strcpy(ext, OBJECT_EXT);
1026 strcpy(ext, NO_PIC_EXT);
1033 if (cmd->options.debug) {
1034 printf("Checking (obj): %s\n", newarg);
1036 rv = stat(newarg, &sb);
1038 while (rv != 0 && ++pass < 1);
1042 cmd->options.pic_mode = pic_AVOID;
1050 /* libdircheck values:
1051 * 0 - no .libs suffix
1054 static char *check_library_exists(command_t *cmd, const char *arg, int pathlen,
1055 int libdircheck, enum lib_type *libtype)
1058 int pass, rv, newpathlen;
1060 newarg = (char *)malloc(strlen(arg) + 10);
1061 strcpy(newarg, arg);
1062 newarg[pathlen] = '\0';
1064 newpathlen = pathlen;
1066 add_dotlibs(newarg);
1067 newpathlen += sizeof(".libs/") - 1;
1070 strcpy(newarg + newpathlen, arg + pathlen);
1071 ext = strrchr(newarg, '.') + 1;
1073 printf("Error: Library path does not have an extension");
1086 if (cmd->options.pic_mode != pic_AVOID &&
1087 cmd->options.shared != share_STATIC) {
1088 strcpy(ext, DYNAMIC_LIB_EXT);
1089 *libtype = type_DYNAMIC_LIB;
1095 strcpy(ext, STATIC_LIB_EXT);
1096 *libtype = type_STATIC_LIB;
1099 strcpy(ext, MODULE_LIB_EXT);
1100 *libtype = type_MODULE_LIB;
1103 strcpy(ext, OBJECT_EXT);
1104 *libtype = type_OBJECT;
1107 *libtype = type_UNKNOWN;
1111 if (cmd->options.debug) {
1112 printf("Checking (lib): %s\n", newarg);
1114 rv = stat(newarg, &sb);
1116 while (rv != 0 && ++pass < 4);
1127 static char * load_install_path(const char *arg)
1132 path = malloc(PATH_MAX);
1138 fgets(path, PATH_MAX, f);
1140 if (path[strlen(path)-1] == '\n') {
1141 path[strlen(path)-1] = '\0';
1143 /* Check that we have an absolute path.
1144 * Otherwise the file could be a GNU libtool file.
1146 if (path[0] != '/') {
1152 static char * load_noinstall_path(const char *arg, int pathlen)
1154 char *newarg, *expanded_path;
1157 newarg = (char *)malloc(strlen(arg) + 10);
1158 strcpy(newarg, arg);
1159 newarg[pathlen] = 0;
1161 newpathlen = pathlen;
1162 strcat(newarg, ".libs");
1163 newpathlen += sizeof(".libs") - 1;
1164 newarg[newpathlen] = 0;
1167 expanded_path = malloc(PATH_MAX);
1168 expanded_path = realpath(newarg, expanded_path);
1169 /* Uh, oh. There was an error. Fall back on our first guess. */
1170 if (!expanded_path) {
1171 expanded_path = newarg;
1174 /* We might get ../ or something goofy. Oh, well. */
1175 expanded_path = newarg;
1178 return expanded_path;
1181 static void add_dynamic_link_opts(command_t *cmd_data, count_chars *args)
1183 #ifdef DYNAMIC_LINK_OPTS
1184 if (cmd_data->options.pic_mode != pic_AVOID) {
1185 if (cmd_data->options.debug) {
1186 printf("Adding: %s\n", DYNAMIC_LINK_OPTS);
1188 push_count_chars(args, DYNAMIC_LINK_OPTS);
1189 if (cmd_data->undefined_flag) {
1190 push_count_chars(args, "-undefined");
1191 #if defined(__APPLE__)
1192 /* -undefined dynamic_lookup is used by the bundled Python in
1193 * 10.4, but if we don't set MACOSX_DEPLOYMENT_TARGET to 10.3+,
1194 * we'll get a linker error if we pass this flag.
1196 if (strcasecmp(cmd_data->undefined_flag,
1197 "dynamic_lookup") == 0) {
1198 insert_count_chars(cmd_data->program_opts,
1199 "MACOSX_DEPLOYMENT_TARGET=10.3", 0);
1202 push_count_chars(args, cmd_data->undefined_flag);
1205 #ifdef DYNAMIC_LINK_UNDEFINED
1206 if (cmd_data->options.debug) {
1207 printf("Adding: %s\n", DYNAMIC_LINK_UNDEFINED);
1209 push_count_chars(args, DYNAMIC_LINK_UNDEFINED);
1216 /* Read the final install location and add it to runtime library search path. */
1218 static void add_rpath(count_chars *cc, const char *path)
1223 #ifdef LINKER_FLAG_PREFIX
1224 size = strlen(LINKER_FLAG_PREFIX);
1226 size = size + strlen(path) + strlen(RPATH) + 2;
1231 #ifdef LINKER_FLAG_PREFIX
1232 strcpy(tmp, LINKER_FLAG_PREFIX);
1237 #ifndef LINKER_FLAG_NO_EQUALS
1242 push_count_chars(cc, tmp);
1245 static void add_rpath_file(count_chars *cc, const char *arg)
1249 path = load_install_path(arg);
1251 add_rpath(cc, path);
1255 static void add_rpath_noinstall(count_chars *cc, const char *arg, int pathlen)
1259 path = load_noinstall_path(arg, pathlen);
1261 add_rpath(cc, path);
1267 #ifdef DYNAMIC_LINK_NO_INSTALL
1268 static void add_dylink_noinstall(count_chars *cc, const char *arg, int pathlen,
1271 const char *install_path, *current_path, *name;
1273 int i_p_len, c_p_len, name_len, dyext_len, cur_len;
1275 install_path = load_install_path(arg);
1276 current_path = load_noinstall_path(arg, pathlen);
1278 if (!install_path || !current_path) {
1282 push_count_chars(cc, DYNAMIC_LINK_NO_INSTALL);
1284 i_p_len = strlen(install_path);
1285 c_p_len = strlen(current_path);
1288 name_len = extlen-pathlen;
1289 dyext_len = sizeof(DYNAMIC_LIB_EXT) - 1;
1291 /* No, we need to replace the extension. */
1292 exp_argument = (char *)malloc(i_p_len + c_p_len + (name_len*2) +
1296 strcpy(exp_argument, install_path);
1298 exp_argument[cur_len++] = '/';
1299 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1300 cur_len += name_len;
1301 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1302 cur_len += dyext_len;
1303 exp_argument[cur_len++] = ':';
1304 strcpy(exp_argument+cur_len, current_path);
1306 exp_argument[cur_len++] = '/';
1307 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1308 cur_len += name_len;
1309 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1310 cur_len += dyext_len;
1312 push_count_chars(cc, exp_argument);
1317 /* use -L -llibname to allow to use installed libraries */
1318 static void add_minus_l(count_chars *cc, const char *arg)
1321 char *name = strrchr(arg, '/');
1322 char *file = strrchr(arg, '.');
1324 if ((name != NULL) && (file != NULL) &&
1325 (strstr(name, "lib") == (name + 1))) {
1330 push_count_chars(cc, "-L");
1331 push_count_chars(cc, arg);
1332 /* we need one argument like -lapr-1 */
1333 newarg = malloc(strlen(file) + 3);
1334 strcpy(newarg, "-l");
1335 strcat(newarg, file);
1336 push_count_chars(cc, newarg);
1338 push_count_chars(cc, arg);
1344 static void add_linker_flag_prefix(count_chars *cc, const char *arg)
1346 #ifndef LINKER_FLAG_PREFIX
1347 push_count_chars(cc, arg);
1350 newarg = (char*)malloc(strlen(arg) + sizeof(LINKER_FLAG_PREFIX) + 1);
1351 strcpy(newarg, LINKER_FLAG_PREFIX);
1352 strcat(newarg, arg);
1353 push_count_chars(cc, newarg);
1358 static int explode_static_lib(command_t *cmd_data, const char *lib)
1360 count_chars tmpdir_cc, libname_cc;
1361 const char *tmpdir, *libname;
1362 char savewd[PATH_MAX];
1365 struct dirent *entry;
1366 const char *lib_args[4];
1369 if (cmd_data->options.dry_run) {
1373 name = jlibtool_basename(lib);
1375 init_count_chars(&tmpdir_cc);
1376 push_count_chars(&tmpdir_cc, ".libs/");
1377 push_count_chars(&tmpdir_cc, name);
1378 push_count_chars(&tmpdir_cc, ".exploded/");
1379 tmpdir = flatten_count_chars(&tmpdir_cc, 0);
1381 if (!cmd_data->options.silent) {
1382 printf("Making: %s\n", tmpdir);
1386 push_count_chars(cmd_data->tmp_dirs, tmpdir);
1388 getcwd(savewd, sizeof(savewd));
1390 if (chdir(tmpdir) != 0) {
1391 if (!cmd_data->options.silent) {
1392 printf("Warning: could not explode %s\n", lib);
1397 if (lib[0] == '/') {
1401 init_count_chars(&libname_cc);
1402 push_count_chars(&libname_cc, "../../");
1403 push_count_chars(&libname_cc, lib);
1404 libname = flatten_count_chars(&libname_cc, 0);
1407 lib_args[0] = LIBRARIAN;
1409 lib_args[2] = libname;
1412 external_spawn(cmd_data, LIBRARIAN, lib_args);
1415 dir = opendir(tmpdir);
1417 while ((entry = readdir(dir)) != NULL) {
1418 #if defined(__APPLE__) && defined(RANLIB)
1419 /* Apple inserts __.SYMDEF which isn't needed.
1420 * Leopard (10.5+) can also add '__.SYMDEF SORTED' which isn't
1421 * much fun either. Just skip them.
1423 if (strstr(entry->d_name, "__.SYMDEF") != NULL) {
1427 if (entry->d_name[0] != '.') {
1428 push_count_chars(&tmpdir_cc, entry->d_name);
1429 name = flatten_count_chars(&tmpdir_cc, 0);
1430 if (cmd_data->options.debug) {
1431 printf("Adding: %s\n", name);
1433 push_count_chars(cmd_data->obj_files, name);
1434 pop_count_chars(&tmpdir_cc);
1442 static int parse_input_file_name(char *arg, command_t *cmd_data)
1444 const char *ext = strrchr(arg, '.');
1447 enum lib_type libtype;
1455 name = jlibtool_basename(arg);
1457 pathlen = name - arg;
1459 if (strcmp(ext, "lo") == 0) {
1460 newarg = check_object_exists(cmd_data, arg, ext - arg);
1462 fprintf(stderr, "Can not find suitable object file for %s\n", arg);
1465 if (cmd_data->mode != mLink) {
1466 push_count_chars(cmd_data->arglist, newarg);
1469 push_count_chars(cmd_data->obj_files, newarg);
1474 if (strcmp(ext, "la") == 0) {
1475 switch (cmd_data->mode) {
1477 /* Try the .libs dir first! */
1478 newarg = check_library_exists(cmd_data, arg, pathlen, 1, &libtype);
1480 /* Try the normal dir next. */
1481 newarg = check_library_exists(cmd_data, arg, pathlen, 0, &libtype);
1483 fprintf(stderr, "Can not find suitable library for %s\n", arg);
1488 /* It is not ok to just add the file: a library may added with:
1489 1 - -L path library_name. (For *.so in Linux).
1493 if (libtype == type_DYNAMIC_LIB) {
1494 add_minus_l(cmd_data->shared_opts.dependencies, newarg);
1495 } else if (cmd_data->output == otLibrary &&
1496 libtype == type_STATIC_LIB) {
1497 explode_static_lib(cmd_data, newarg);
1499 push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1502 if (cmd_data->output == otLibrary && libtype == type_STATIC_LIB) {
1503 explode_static_lib(cmd_data, newarg);
1506 push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1509 if (libtype == type_DYNAMIC_LIB) {
1510 if (cmd_data->options.no_install) {
1512 add_rpath_noinstall(cmd_data->shared_opts.dependencies,
1515 #ifdef DYNAMIC_LINK_NO_INSTALL
1517 * This doesn't work as Darwin's linker has no way to
1518 * override at link-time the search paths for a
1519 * non-installed library.
1522 add_dylink_noinstall(cmd_data->shared_opts.dependencies,
1523 arg, pathlen, ext - arg);
1529 add_rpath_file(cmd_data->shared_opts.dependencies, arg);
1535 /* If we've already recorded a library to install, we're most
1536 * likely getting the .la file that we want to install as.
1537 * The problem is that we need to add it as the directory,
1538 * not the .la file itself. Otherwise, we'll do odd things.
1540 if (cmd_data->output == otLibrary) {
1541 arg[pathlen] = '\0';
1542 push_count_chars(cmd_data->arglist, arg);
1545 cmd_data->output = otLibrary;
1546 cmd_data->output_name = arg;
1547 cmd_data->static_name.install = gen_install_name(arg, 0);
1548 cmd_data->shared_name.install = gen_install_name(arg, 1);
1549 cmd_data->module_name.install = gen_install_name(arg, 2);
1551 if (!cmd_data->static_name.install &&
1552 !cmd_data->shared_name.install &&
1553 !cmd_data->module_name.install) {
1554 fprintf(stderr, "Files to install do not exist\n");
1566 if (strcmp(ext, "c") == 0) {
1567 /* If we don't already have an idea what our output name will be. */
1568 if (cmd_data->basename == NULL) {
1569 cmd_data->basename = (char *)malloc(strlen(arg) + 4);
1570 strcpy(cmd_data->basename, arg);
1571 strcpy(strrchr(cmd_data->basename, '.') + 1, "lo");
1573 cmd_data->fake_output_name = strrchr(cmd_data->basename, '/');
1574 if (cmd_data->fake_output_name) {
1575 cmd_data->fake_output_name++;
1578 cmd_data->fake_output_name = cmd_data->basename;
1586 static int parse_output_file_name(char *arg, command_t *cmd_data)
1590 char *newarg = NULL;
1593 cmd_data->fake_output_name = arg;
1595 name = jlibtool_basename(arg);
1596 ext = strrchr(name, '.');
1599 if (!ext || strcmp(ext, EXE_EXT) == 0) {
1603 cmd_data->basename = arg;
1604 cmd_data->output = otProgram;
1605 #if defined(_OSD_POSIX)
1606 cmd_data->options.pic_mode = pic_AVOID;
1608 newarg = (char *)malloc(strlen(arg) + 5);
1609 strcpy(newarg, arg);
1612 strcat(newarg, EXE_EXT);
1615 cmd_data->output_name = newarg;
1620 pathlen = name - arg;
1622 if (strcmp(ext, "la") == 0) {
1623 assert(cmd_data->mode == mLink);
1625 cmd_data->basename = arg;
1626 cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1627 cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1628 cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1629 cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1630 cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1631 cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1633 if (!cmd_data->options.dry_run) {
1636 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1638 strcpy(newname, cmd_data->static_name.normal);
1639 newext = strrchr(newname, '/');
1641 /* Check first to see if the dir already exists! */
1642 safe_mkdir(".libs");
1645 safe_mkdir(newname);
1650 #ifdef TRUNCATE_DLL_NAME
1652 arg = truncate_dll_name(arg);
1656 cmd_data->output_name = arg;
1660 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1661 assert(cmd_data->mode == mLink);
1663 cmd_data->basename = arg;
1664 cmd_data->options.shared = share_STATIC;
1665 cmd_data->output = otStaticLibraryOnly;
1666 cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1667 cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1669 if (!cmd_data->options.dry_run) {
1672 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1674 strcpy(newname, cmd_data->static_name.normal);
1675 newext = strrchr(newname, '/');
1677 /* Check first to see if the dir already exists! */
1678 safe_mkdir(".libs");
1681 safe_mkdir(newname);
1686 cmd_data->output_name = arg;
1690 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1691 assert(cmd_data->mode == mLink);
1693 cmd_data->basename = arg;
1694 cmd_data->options.shared = share_SHARED;
1695 cmd_data->output = otDynamicLibraryOnly;
1696 cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1697 cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1698 cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1699 cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1701 if (!cmd_data->options.dry_run) {
1704 newname = malloc(strlen(cmd_data->shared_name.normal) + 1);
1706 strcpy(newname, cmd_data->shared_name.normal);
1707 newext = strrchr(newname, '/');
1709 /* Check first to see if the dir already exists! */
1710 safe_mkdir(".libs");
1713 safe_mkdir(newname);
1718 cmd_data->output_name = arg;
1722 if (strcmp(ext, "lo") == 0) {
1724 cmd_data->basename = arg;
1725 cmd_data->output = otObject;
1726 newarg = (char *)malloc(strlen(arg) + 2);
1727 strcpy(newarg, arg);
1728 newext = strrchr(newarg, '.') + 1;
1729 strcpy(newext, OBJECT_EXT);
1730 cmd_data->output_name = newarg;
1734 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1735 fprintf(stderr, "Please build libraries with .la target, not ."
1736 DYNAMIC_LIB_EXT "\n");
1740 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1741 fprintf(stderr, "Please build libraries with .la target, not ."
1742 STATIC_LIB_EXT "\n");
1749 static char *automode(char *arg, command_t *cmd_data)
1751 if (cmd_data->mode != mUnknown) return arg;
1753 if (strcmp(arg, "CC") == 0) {
1755 cmd_data->mode = mCompile;
1757 } else if (strcmp(arg, "CXX") == 0) {
1759 cmd_data->mode = mCompile;
1761 } else if (strcmp(arg, "LINK") == 0) {
1763 cmd_data->mode = mLink;
1765 } else if (strcmp(arg, "LINK.c") == 0) {
1767 cmd_data->mode = mLink;
1769 } else if (strcmp(arg, "LINK.cxx") == 0) {
1771 cmd_data->mode = mLink;
1777 static void parse_args(int argc, char *argv[], command_t *cmd_data)
1784 * We now take a major step past libtool.
1786 * IF there's no "--mode=...", AND we recognise
1787 * the binary as a "special" name, THEN replace it
1788 * with the correct one, and set the correct mode.
1790 base = jlibtool_basename(argv[0]);
1791 arg = automode(base, cmd_data);
1794 if (cmd_data->options.debug) {
1795 printf("Adding: %s\n", arg);
1797 push_count_chars(cmd_data->arglist, arg);
1798 assert(cmd_data->mode != mUnknown);
1802 * We first pass over the command-line arguments looking for
1803 * "--mode", etc. If so, then use the libtool compatibility
1804 * method for building the software. Otherwise, auto-detect it
1805 * via "-o" and the extensions.
1808 if (cmd_data->mode == mUnknown) for (a = 1; a < argc; a++) {
1811 if (strncmp(arg, "--mode=", 7) == 0) {
1817 * Stop if we get another magic method
1820 ((strncmp(arg, "LINK", 4) == 0) ||
1821 (strcmp(arg, "CC") == 0) ||
1822 (strcmp(arg, "CXX") == 0))) {
1827 if (strncmp(arg, "-o", 2) == 0) {
1833 arg = strrchr(base, '.');
1835 cmd_data->mode = mLink;
1836 push_count_chars(cmd_data->arglist, LINK_c);
1839 else if (strcmp(arg, EXE_EXT) == 0) {
1840 cmd_data->mode = mLink;
1841 push_count_chars(cmd_data->arglist, LINK_c);
1844 else if (strcmp(arg + 1, DYNAMIC_LIB_EXT) == 0) {
1845 cmd_data->mode = mLink;
1846 push_count_chars(cmd_data->arglist, LINK_c);
1848 else if (strcmp(arg + 1, STATIC_LIB_EXT) == 0) {
1849 cmd_data->mode = mLink;
1850 push_count_chars(cmd_data->arglist, LINK_c);
1852 else if (strcmp(arg + 1, "la") == 0) {
1853 cmd_data->mode = mLink;
1854 push_count_chars(cmd_data->arglist, LINK_c);
1856 else if ((strcmp(arg + 1, "lo") == 0) ||
1857 (strcmp(arg + 1, "o") == 0)) {
1858 cmd_data->mode = mCompile;
1859 push_count_chars(cmd_data->arglist, CC);
1863 for (a = 1; a < argc; a++) {
1867 if (arg[0] == '-') {
1868 if (arg[1] == '-') {
1869 argused = parse_long_opt(arg + 2, cmd_data);
1872 argused = parse_short_opt(arg + 1, cmd_data);
1875 /* We haven't done anything with it yet, try some of the
1876 * more complicated short opts... */
1877 if (argused == 0 && a + 1 < argc) {
1878 if (arg[1] == 'o' && !arg[2]) {
1880 argused = parse_output_file_name(arg, cmd_data);
1881 } else if (strcmp(arg+1, "MT") == 0) {
1882 if (cmd_data->options.debug) {
1883 printf("Adding: %s\n", arg);
1885 push_count_chars(cmd_data->arglist, arg);
1887 if (!cmd_data->options.silent) {
1888 printf(" %s\n", arg);
1890 push_count_chars(cmd_data->arglist, arg);
1892 } else if (strcmp(arg+1, "rpath") == 0) {
1893 /* Aha, we should try to link both! */
1894 cmd_data->install_path = argv[++a];
1896 } else if (strcmp(arg+1, "release") == 0) {
1897 /* Store for later deciphering */
1898 cmd_data->version_info = argv[++a];
1900 } else if (strcmp(arg+1, "version-info") == 0) {
1901 /* Store for later deciphering */
1902 cmd_data->version_info = argv[++a];
1904 } else if (strcmp(arg+1, "export-symbols-regex") == 0) {
1905 /* Skip the argument. */
1908 } else if (strcmp(arg+1, "release") == 0) {
1909 /* Skip the argument. */
1912 } else if (strcmp(arg+1, "undefined") == 0) {
1913 cmd_data->undefined_flag = argv[++a];
1915 } else if (arg[1] == 'R' && !arg[2]) {
1916 /* -R dir Add dir to runtime library search path. */
1917 add_runtimedirlib(argv[++a], cmd_data);
1922 argused = parse_input_file_name(arg, cmd_data);
1927 * If we still don't have a run mode, look for a magic
1928 * program name CC, LINK, or whatever. Then replace that
1929 * with the name of the real program we want to run.
1931 if ((cmd_data->arglist->num == 0) &&
1932 (cmd_data->mode == mUnknown)) {
1933 arg = automode(arg, cmd_data);
1936 if (cmd_data->options.debug) {
1937 printf("Adding: %s\n", arg);
1939 push_count_chars(cmd_data->arglist, arg);
1946 static void generate_def_file(command_t *cmd_data)
1948 char def_file[1024];
1949 char implib_file[1024];
1952 char *export_args[1024];
1953 int num_export_args = 0;
1958 if (cmd_data->output_name) {
1959 strcpy(def_file, cmd_data->output_name);
1960 strcat(def_file, ".def");
1961 hDef = fopen(def_file, "w");
1964 fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", nameof(cmd_data->output_name));
1965 fprintf(hDef, "DATA NONSHARED\n");
1966 fprintf(hDef, "EXPORTS\n");
1969 for (a = 0; a < cmd_data->num_obj_files; a++) {
1970 cmd_size += strlen(cmd_data->obj_files[a]) + 1;
1973 cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
1974 cmd = (char *)malloc(cmd_size);
1975 strcpy(cmd, GEN_EXPORTS);
1977 for (a=0; a < cmd_data->num_obj_files; a++) {
1979 strcat(cmd, cmd_data->obj_files[a] );
1983 strcat(cmd, def_file);
1985 export_args[num_export_args++] = SHELL_CMD;
1986 export_args[num_export_args++] = "-c";
1987 export_args[num_export_args++] = cmd;
1988 export_args[num_export_args++] = NULL;
1989 external_spawn(cmd_data, export_args[0], (const char**)export_args);
1990 cmd_data->arglist[cmd_data->num_args++] = strdup(def_file);
1992 /* Now make an import library for the dll */
1993 num_export_args = 0;
1994 export_args[num_export_args++] = DEF2IMPLIB_CMD;
1995 export_args[num_export_args++] = "-o";
1997 strcpy(implib_file, ".libs/");
1998 strcat(implib_file, cmd_data->basename);
1999 ext = strrchr(implib_file, '.');
2004 strcat(implib_file, ".");
2005 strcat(implib_file, STATIC_LIB_EXT);
2007 export_args[num_export_args++] = implib_file;
2008 export_args[num_export_args++] = def_file;
2009 export_args[num_export_args++] = NULL;
2010 external_spawn(cmd_data, export_args[0], (const char**)export_args);
2018 static const char* expand_path(const char *relpath)
2020 char foo[PATH_MAX], *newpath;
2022 getcwd(foo, PATH_MAX-1);
2023 newpath = (char*)malloc(strlen(foo)+strlen(relpath)+2);
2024 strcpy(newpath, foo);
2025 strcat(newpath, "/");
2026 strcat(newpath, relpath);
2031 static void link_fixup(command_t *c)
2033 /* If we were passed an -rpath directive, we need to build
2034 * shared objects too. Otherwise, we should only create static
2037 if (!c->install_path && (c->output == otDynamicLibraryOnly ||
2038 c->output == otModule || c->output == otLibrary)) {
2039 if (c->options.shared == share_SHARED) {
2040 c->install_path = LIBDIR;
2044 if (c->output == otDynamicLibraryOnly ||
2045 c->output == otModule ||
2046 c->output == otLibrary) {
2048 push_count_chars(c->shared_opts.normal, "-o");
2049 if (c->output == otModule) {
2050 push_count_chars(c->shared_opts.normal, c->module_name.normal);
2054 push_count_chars(c->shared_opts.normal, c->shared_name.normal);
2055 #ifdef DYNAMIC_INSTALL_NAME
2056 push_count_chars(c->shared_opts.normal, DYNAMIC_INSTALL_NAME);
2058 if (!c->install_path) {
2059 fprintf(stderr, "Installation mode requires -rpath\n");
2063 tmp = (char*)malloc(PATH_MAX);
2064 strcpy(tmp, c->install_path);
2065 if (c->shared_name.install) {
2066 strcat(tmp, strrchr(c->shared_name.install, '/'));
2068 strcat(tmp, strrchr(c->shared_name.normal, '/'));
2070 push_count_chars(c->shared_opts.normal, tmp);
2074 append_count_chars(c->shared_opts.normal, c->obj_files);
2075 append_count_chars(c->shared_opts.normal, c->shared_opts.dependencies);
2077 if (c->options.export_all) {
2079 generate_def_file(c);
2084 if (c->output == otLibrary || c->output == otStaticLibraryOnly) {
2085 push_count_chars(c->static_opts.normal, "-o");
2086 push_count_chars(c->static_opts.normal, c->output_name);
2089 if (c->output == otProgram) {
2090 if (c->output_name) {
2091 push_count_chars(c->arglist, "-o");
2092 push_count_chars(c->arglist, c->output_name);
2093 append_count_chars(c->arglist, c->obj_files);
2094 append_count_chars(c->arglist, c->shared_opts.dependencies);
2095 add_dynamic_link_opts(c, c->arglist);
2100 static void post_parse_fixup(command_t *cmd_data)
2102 switch (cmd_data->mode)
2106 if (cmd_data->options.pic_mode != pic_AVOID) {
2107 push_count_chars(cmd_data->arglist, PIC_FLAG);
2110 if (cmd_data->output_name) {
2111 push_count_chars(cmd_data->arglist, "-o");
2112 push_count_chars(cmd_data->arglist, cmd_data->output_name);
2116 link_fixup(cmd_data);
2119 if (cmd_data->output == otLibrary) {
2120 link_fixup(cmd_data);
2127 if (cmd_data->output == otObject ||
2128 cmd_data->output == otProgram ||
2129 cmd_data->output == otLibrary ||
2130 cmd_data->output == otDynamicLibraryOnly) {
2131 push_count_chars(cmd_data->arglist, "-Zomf");
2135 if (cmd_data->options.shared &&
2136 (cmd_data->output == otObject ||
2137 cmd_data->output == otLibrary ||
2138 cmd_data->output == otDynamicLibraryOnly)) {
2140 push_count_chars(cmd_data->arglist, SHARE_SW);
2145 static int run_mode(command_t *cmd_data)
2148 count_chars *cctemp;
2150 cctemp = (count_chars*)malloc(sizeof(count_chars));
2151 init_count_chars(cctemp);
2153 switch (cmd_data->mode)
2156 rv = run_command(cmd_data, cmd_data->arglist);
2162 /* Well, we'll assume it's a file going to a directory... */
2163 /* For brain-dead install-sh based scripts, we have to repeat
2164 * the command N-times. install-sh should die.
2166 if (!cmd_data->output_name) {
2167 rv = run_command(cmd_data, cmd_data->arglist);
2172 if (cmd_data->output_name) {
2173 append_count_chars(cctemp, cmd_data->arglist);
2174 insert_count_chars(cctemp,
2175 cmd_data->output_name,
2177 rv = run_command(cmd_data, cctemp);
2181 clear_count_chars(cctemp);
2183 if (cmd_data->static_name.install) {
2184 append_count_chars(cctemp, cmd_data->arglist);
2185 insert_count_chars(cctemp,
2186 cmd_data->static_name.install,
2188 rv = run_command(cmd_data, cctemp);
2192 #if defined(__APPLE__) && defined(RANLIB)
2193 /* From the Apple libtool(1) manpage on Tiger/10.4:
2195 * With the way libraries used to be created, errors were possible
2196 * if the library was modified with ar(1) and the table of
2197 * contents was not updated by rerunning ranlib(1). Thus the
2198 * link editor, ld, warns when the modification date of a library
2199 * is more recent than the creation date of its table of
2200 * contents. Unfortunately, this means that you get the warning
2201 * even if you only copy the library.
2204 * This means that when we install the static archive, we need to
2205 * rerun ranlib afterwards.
2207 const char *lib_args[3], *static_lib_name;
2210 len1 = strlen(cmd_data->arglist->vals[cmd_data->arglist->num - 1]);
2212 static_lib_name = jlibtool_basename(cmd_data->static_name.install);
2213 len2 = strlen(static_lib_name);
2215 tmp = malloc(len1 + len2 + 2);
2217 snprintf(tmp, len1 + len2 + 2, "%s/%s",
2218 cmd_data->arglist->vals[cmd_data->arglist->num - 1],
2221 lib_args[0] = RANLIB;
2224 external_spawn(cmd_data, RANLIB, lib_args);
2227 clear_count_chars(cctemp);
2229 if (cmd_data->shared_name.install) {
2230 append_count_chars(cctemp, cmd_data->arglist);
2231 insert_count_chars(cctemp,
2232 cmd_data->shared_name.install,
2234 rv = run_command(cmd_data, cctemp);
2238 clear_count_chars(cctemp);
2240 if (cmd_data->module_name.install) {
2241 append_count_chars(cctemp, cmd_data->arglist);
2242 insert_count_chars(cctemp,
2243 cmd_data->module_name.install,
2245 rv = run_command(cmd_data, cctemp);
2249 clear_count_chars(cctemp);
2253 if (cmd_data->output == otStaticLibraryOnly ||
2254 cmd_data->output == otLibrary) {
2256 const char *lib_args[3];
2258 /* Removes compiler! */
2259 cmd_data->program = LIBRARIAN;
2260 push_count_chars(cmd_data->program_opts, LIBRARIAN_OPTS);
2261 push_count_chars(cmd_data->program_opts,
2262 cmd_data->static_name.normal);
2264 rv = run_command(cmd_data, cmd_data->obj_files);
2270 lib_args[0] = RANLIB;
2271 lib_args[1] = cmd_data->static_name.normal;
2273 external_spawn(cmd_data, RANLIB, lib_args);
2277 if (cmd_data->output == otDynamicLibraryOnly ||
2278 cmd_data->output == otModule ||
2279 cmd_data->output == otLibrary) {
2280 cmd_data->program = NULL;
2281 clear_count_chars(cmd_data->program_opts);
2283 append_count_chars(cmd_data->program_opts, cmd_data->arglist);
2284 if (cmd_data->output == otModule) {
2286 push_count_chars(cmd_data->program_opts, MODULE_OPTS);
2290 push_count_chars(cmd_data->program_opts, SHARED_OPTS);
2292 #ifdef dynamic_link_version_func
2293 push_count_chars(cmd_data->program_opts,
2294 dynamic_link_version_func(cmd_data->version_info));
2297 add_dynamic_link_opts(cmd_data, cmd_data->program_opts);
2299 rv = run_command(cmd_data, cmd_data->shared_opts.normal);
2304 if (cmd_data->output == otProgram) {
2305 rv = run_command(cmd_data, cmd_data->arglist);
2313 char *l, libpath[PATH_MAX];
2315 strcpy(libpath, cmd_data->arglist->vals[0]);
2316 add_dotlibs(libpath);
2317 l = strrchr(libpath, '/');
2318 if (!l) l = strrchr(libpath, '\\');
2326 l = "./build/lib/.libs";
2327 setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
2328 rv = run_command(cmd_data, cmd_data->arglist);
2342 static void cleanup_tmp_dir(const char *dirname)
2345 struct dirent *entry;
2346 char fullname[1024];
2348 dir = opendir(dirname);
2353 while ((entry = readdir(dir)) != NULL) {
2354 if (entry->d_name[0] != '.') {
2355 strcpy(fullname, dirname);
2356 strcat(fullname, "/");
2357 strcat(fullname, entry->d_name);
2358 (void) remove(fullname);
2365 static void cleanup_tmp_dirs(command_t *cmd_data)
2369 for (d = 0; d < cmd_data->tmp_dirs->num; d++) {
2370 cleanup_tmp_dir(cmd_data->tmp_dirs->vals[d]);
2374 static int ensure_fake_uptodate(command_t *cmd_data)
2376 /* FIXME: could do the stat/touch here, but nah... */
2377 const char *touch_args[3];
2379 if (cmd_data->mode == mInstall) {
2382 if (!cmd_data->fake_output_name) {
2386 touch_args[0] = "touch";
2387 touch_args[1] = cmd_data->fake_output_name;
2388 touch_args[2] = NULL;
2389 return external_spawn(cmd_data, "touch", touch_args);
2392 /* Store the install path in the *.la file */
2393 static int add_for_runtime(command_t *cmd_data)
2395 if (cmd_data->mode == mInstall) {
2398 if (cmd_data->output == otDynamicLibraryOnly ||
2399 cmd_data->output == otLibrary) {
2400 FILE *f=fopen(cmd_data->fake_output_name,"w");
2404 fprintf(f,"%s\n", cmd_data->install_path);
2408 return(ensure_fake_uptodate(cmd_data));
2412 int main(int argc, char *argv[])
2417 memset(&cmd_data, 0, sizeof(cmd_data));
2419 cmd_data.options.pic_mode = pic_UNKNOWN;
2421 cmd_data.program_opts = (count_chars*)malloc(sizeof(count_chars));
2422 init_count_chars(cmd_data.program_opts);
2423 cmd_data.arglist = (count_chars*)malloc(sizeof(count_chars));
2424 init_count_chars(cmd_data.arglist);
2425 cmd_data.tmp_dirs = (count_chars*)malloc(sizeof(count_chars));
2426 init_count_chars(cmd_data.tmp_dirs);
2427 cmd_data.obj_files = (count_chars*)malloc(sizeof(count_chars));
2428 init_count_chars(cmd_data.obj_files);
2429 cmd_data.dep_rpaths = (count_chars*)malloc(sizeof(count_chars));
2430 init_count_chars(cmd_data.dep_rpaths);
2431 cmd_data.rpaths = (count_chars*)malloc(sizeof(count_chars));
2432 init_count_chars(cmd_data.rpaths);
2433 cmd_data.static_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2434 init_count_chars(cmd_data.static_opts.normal);
2435 cmd_data.shared_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2436 init_count_chars(cmd_data.shared_opts.normal);
2437 cmd_data.shared_opts.dependencies = (count_chars*)malloc(sizeof(count_chars));
2438 init_count_chars(cmd_data.shared_opts.dependencies);
2440 cmd_data.mode = mUnknown;
2441 cmd_data.output = otGeneral;
2443 parse_args(argc, argv, &cmd_data);
2444 post_parse_fixup(&cmd_data);
2446 if (cmd_data.mode == mUnknown) {
2450 rc = run_mode(&cmd_data);
2453 add_for_runtime(&cmd_data);
2456 cleanup_tmp_dirs(&cmd_data);