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)
550 const char *spawn_args[4];
553 init_count_chars(&tmpcc);
555 if (cmd_data->program) {
556 push_count_chars(&tmpcc, cmd_data->program);
559 append_count_chars(&tmpcc, cmd_data->program_opts);
561 append_count_chars(&tmpcc, cc);
563 raw = flatten_count_chars(&tmpcc, 1);
564 command = shell_esc(raw);
566 memcpy(&tmp, &raw, sizeof(tmp));
569 spawn_args[0] = SHELL_CMD;
570 spawn_args[1] = "-c";
571 spawn_args[2] = command;
572 spawn_args[3] = NULL;
573 ret = external_spawn(cmd_data, spawn_args[0], spawn_args);
581 * print configuration
582 * shlibpath_var is used in configure.
584 #define printc(_x,_y) if (!value || !strcmp(value, _x)) printf(_x "=\"%s\"\n", _y)
586 static void print_config(const char *value)
589 printc("runpath_var", LD_RUN_PATH);
591 #ifdef LD_LIBRARY_PATH
592 printc("shlibpath_var", LD_LIBRARY_PATH);
594 #ifdef LD_LIBRARY_PATH_LOCAL
595 printc("shlocallibpath_var", LD_LIBRARY_PATH_LOCAL);
598 printc("SHELL", SHELL_CMD);
601 printc("objext", OBJECT_EXT);
604 printc("objdir", OBJDIR);
606 #ifdef DYNAMIC_LIB_EXT
607 /* add a '.' prefix because libtool does that. */
608 printc("shrext_cmds", "echo ." DYNAMIC_LIB_EXT);
609 /* add a '.' prefix because libtool does that. */
610 printc("shrext", "." DYNAMIC_LIB_EXT);
613 printc("exeext", EXE_EXT);
615 #ifdef STATIC_LIB_EXT
616 printc("libext", STATIC_LIB_EXT);
619 printc("AR", LIBRARIAN);
621 #ifdef LIBRARIAN_OPTS
622 printc("AR_FLAGS", LIBRARIAN_OPTS);
624 #ifdef LINKER_FLAG_PREFIX
625 printc("wl", LINKER_FLAG_PREFIX);
628 printc("ranlib", RANLIB);
633 * Add a directory to the runtime library search path.
635 static void add_runtimedirlib(char *arg, command_t *cmd_data)
638 add_rpath(cmd_data->shared_opts.dependencies, arg);
640 arg = arg; /* -Wunused */
645 static int parse_long_opt(char *arg, command_t *cmd_data)
647 char *equal_pos = strchr(arg, '=');
652 strncpy(var, arg, equal_pos - arg);
653 var[equal_pos - arg] = 0;
654 strcpy(value, equal_pos + 1);
656 strncpy(var, arg, sizeof(var) - 1);
657 var[sizeof(var) - 1] = '\0';
662 if (strcmp(var, "silent") == 0) {
663 cmd_data->options.silent = 1;
664 } else if (strcmp(var, "quiet") == 0) {
665 cmd_data->options.silent = 1;
666 } else if (strcmp(var, "debug") == 0) {
667 cmd_data->options.debug = 1;
668 } else if (strcmp(var, "mode") == 0) {
669 if (cmd_data->mode != mUnknown) {
670 fprintf(stderr, "Cannot set --mode twice\n");
674 if (strcmp(value, "compile") == 0) {
675 cmd_data->mode = mCompile;
676 cmd_data->output = otObject;
678 } else if (strcmp(value, "link") == 0) {
679 cmd_data->mode = mLink;
680 cmd_data->output = otLibrary;
682 } else if (strcmp(value, "install") == 0) {
683 cmd_data->mode = mInstall;
685 } else if (strcmp(value, "execute") == 0) {
686 cmd_data->mode = mExecute;
689 printf("Unknown mode \"%s\"\n", value);
691 } else if (strcmp(var, "shared") == 0) {
692 if ((cmd_data->mode == mLink) &&
693 (cmd_data->output == otGeneral)) {
694 cmd_data->output = otDynamicLibraryOnly;
696 cmd_data->options.shared = share_SHARED;
697 } else if (strcmp(var, "export-all") == 0) {
698 cmd_data->options.export_all = 1;
699 } else if (strcmp(var, "dry-run") == 0) {
700 printf("Dry-run mode on!\n");
701 cmd_data->options.dry_run = 1;
702 } else if (strcmp(var, "version") == 0) {
703 printf("Version " VERSION "\n");
704 } else if (strcmp(var, "help") == 0) {
706 } else if (strcmp(var, "config") == 0) {
709 } else if (strcmp(var, "tag") == 0) {
710 if (strcmp(value, "CC") == 0) {
713 if (strcmp(value, "CXX") == 0) {
723 /* Return 1 if we eat it. */
724 static int parse_short_opt(char *arg, command_t *cmd_data)
726 if (strcmp(arg, "export-dynamic") == 0) {
727 cmd_data->options.export_dynamic = 1;
731 if (strcmp(arg, "module") == 0) {
732 cmd_data->output = otModule;
736 if (strcmp(arg, "shared") == 0) {
737 if (cmd_data->mode == mLink) {
738 cmd_data->output = otDynamicLibraryOnly;
740 cmd_data->options.shared = share_SHARED;
744 if (strcmp(arg, "Zexe") == 0) {
748 if (strcmp(arg, "avoid-version") == 0) {
752 if (strcmp(arg, "prefer-pic") == 0) {
753 cmd_data->options.pic_mode = pic_PREFER;
757 if (strcmp(arg, "prefer-non-pic") == 0) {
758 cmd_data->options.pic_mode = pic_AVOID;
762 if (strcmp(arg, "static") == 0) {
763 if ((cmd_data->mode == mLink) &&
764 (cmd_data->output == otLibrary)) {
765 cmd_data->output = otStaticLibraryOnly;
767 cmd_data->options.shared = share_STATIC;
771 if (cmd_data->mode == mLink) {
772 if (strcmp(arg, "no-install") == 0) {
773 cmd_data->options.no_install = 1;
776 if (arg[0] == 'L' || arg[0] == 'l') {
779 push_count_chars(cmd_data->shared_opts.dependencies, arg);
781 } else if (arg[0] == 'R' && arg[1]) {
782 /* -Rdir Add dir to runtime library search path. */
783 add_runtimedirlib(&arg[1], cmd_data);
790 #ifdef TRUNCATE_DLL_NAME
791 static char *truncate_dll_name(char *path)
793 /* Cut DLL name down to 8 characters after removing any mod_ prefix */
794 char *tmppath = strdup(path);
795 char *newname = strrchr(tmppath, '/') + 1;
796 char *ext = strrchr(newname, '.');
804 if (strncmp(newname, "mod_", 4) == 0) {
805 strcpy(newname, newname + 4);
810 strcpy(newname + 8, strchr(newname, '.'));
817 static long safe_strtol(const char *nptr, const char **endptr, int base)
823 rv = strtol(nptr, (char**)endptr, 10);
825 if (errno == ERANGE) {
832 static void safe_mkdir(const char *path)
837 old_umask = umask(0);
840 #ifdef MKDIR_NO_UMASK
841 status = mkdir(path);
843 status = mkdir(path, ~old_umask);
845 if ((status < 0) && (errno != EEXIST)) {
846 printf("Warning: mkdir of %s failed\n", path);
850 /* returns just a file's name without the path */
851 static const char *jlibtool_basename(const char *fullpath)
853 const char *name = strrchr(fullpath, '/');
856 name = strrchr(fullpath, '\\');
869 /* returns just a file's name without path or extension */
870 static const char *nameof(const char *fullpath)
875 name = jlibtool_basename(fullpath);
876 ext = strrchr(name, '.');
880 trimmed = malloc(ext - name + 1);
881 strncpy(trimmed, name, ext - name);
882 trimmed[ext-name] = 0;
890 /* version_info is in the form of MAJOR:MINOR:PATCH */
891 static const char *darwin_dynamic_link_function(const char *version_info)
894 long major, minor, patch;
901 major = safe_strtol(version_info, &version_info, 10);
904 if (version_info[0] == ':') {
908 minor = safe_strtol(version_info, &version_info, 10);
911 if (version_info[0] == ':') {
915 patch = safe_strtol(version_info, &version_info, 10);
921 /* Avoid -dylib_compatibility_version must be greater than zero errors. */
925 newarg = (char*)malloc(100);
927 "-compatibility_version %ld -current_version %ld.%ld",
928 major, major, minor);
935 * Add a '.libs/' to the buffer. The caller ensures that
936 * The buffer is large enough to handle 6 extra characters.
938 static void add_dotlibs(char *buffer)
940 char *name = strrchr(buffer, '/');
944 strcpy(buffer, ".libs/");
951 memmove(name + 6, name, strlen(name));
952 memcpy(name, ".libs/", 6);
955 static char *gen_library_name(const char *name, enum lib_type genlib)
957 char *newarg, *newext;
959 newarg = (char *)calloc(strlen(name) + 11, 1);
961 if (genlib == type_MODULE_LIB && strncmp(name, "lib", 3) == 0) {
965 if (genlib == type_MODULE_LIB) {
966 strcpy(newarg, jlibtool_basename(name));
969 strcpy(newarg, name);
972 newext = strrchr(newarg, '.') + 1;
979 case type_STATIC_LIB:
980 strcpy(newext, STATIC_LIB_EXT);
982 case type_DYNAMIC_LIB:
983 strcpy(newext, DYNAMIC_LIB_EXT);
985 case type_MODULE_LIB:
986 strcpy(newext, MODULE_LIB_EXT);
998 static char *gen_install_name(const char *name, enum lib_type genlib)
1004 newname = gen_library_name(name, genlib);
1005 if (!newname) return NULL;
1007 /* Check if it exists. If not, return NULL. */
1008 rv = stat(newname, &sb);
1018 static const char *check_object_exists(command_t *cmd, const char *arg, int arglen)
1023 newarg = (char *)malloc(arglen + 10);
1024 memcpy(newarg, arg, arglen);
1026 ext = newarg + arglen;
1035 strcpy(ext, OBJECT_EXT);
1039 strcpy(ext, NO_PIC_EXT);
1046 if (cmd->options.debug) {
1047 printf("Checking (obj): %s\n", newarg);
1049 rv = stat(newarg, &sb);
1051 while (rv != 0 && ++pass < 1);
1055 cmd->options.pic_mode = pic_AVOID;
1065 /* libdircheck values:
1066 * 0 - no .libs suffix
1069 static char *check_library_exists(command_t *cmd, const char *arg, int pathlen,
1070 int libdircheck, enum lib_type *libtype)
1073 int pass, rv, newpathlen;
1075 newarg = (char *)malloc(strlen(arg) + 10);
1076 strcpy(newarg, arg);
1077 newarg[pathlen] = '\0';
1079 newpathlen = pathlen;
1081 add_dotlibs(newarg);
1082 newpathlen += sizeof(".libs/") - 1;
1085 strcpy(newarg + newpathlen, arg + pathlen);
1086 ext = strrchr(newarg, '.') + 1;
1088 printf("Error: Library path does not have an extension");
1101 if (cmd->options.pic_mode != pic_AVOID &&
1102 cmd->options.shared != share_STATIC) {
1103 strcpy(ext, DYNAMIC_LIB_EXT);
1104 *libtype = type_DYNAMIC_LIB;
1110 strcpy(ext, STATIC_LIB_EXT);
1111 *libtype = type_STATIC_LIB;
1114 strcpy(ext, MODULE_LIB_EXT);
1115 *libtype = type_MODULE_LIB;
1118 strcpy(ext, OBJECT_EXT);
1119 *libtype = type_OBJECT;
1122 *libtype = type_UNKNOWN;
1126 if (cmd->options.debug) {
1127 printf("Checking (lib): %s\n", newarg);
1129 rv = stat(newarg, &sb);
1131 while (rv != 0 && ++pass < 4);
1142 static char * load_install_path(const char *arg)
1152 path = malloc(PATH_MAX);
1154 fgets(path, PATH_MAX, f);
1157 if (path[strlen(path)-1] == '\n') {
1158 path[strlen(path)-1] = '\0';
1161 /* Check that we have an absolute path.
1162 * Otherwise the file could be a GNU libtool file.
1164 if (path[0] != '/') {
1172 static char * load_noinstall_path(const char *arg, int pathlen)
1174 char *newarg, *expanded_path;
1177 newarg = (char *)malloc(strlen(arg) + 10);
1178 strcpy(newarg, arg);
1179 newarg[pathlen] = 0;
1181 newpathlen = pathlen;
1182 strcat(newarg, ".libs");
1183 newpathlen += sizeof(".libs") - 1;
1184 newarg[newpathlen] = 0;
1187 expanded_path = malloc(PATH_MAX);
1188 expanded_path = realpath(newarg, expanded_path);
1189 /* Uh, oh. There was an error. Fall back on our first guess. */
1190 if (!expanded_path) {
1191 expanded_path = newarg;
1194 /* We might get ../ or something goofy. Oh, well. */
1195 expanded_path = newarg;
1198 return expanded_path;
1201 static void add_dynamic_link_opts(command_t *cmd_data, count_chars *args)
1203 #ifdef DYNAMIC_LINK_OPTS
1204 if (cmd_data->options.pic_mode != pic_AVOID) {
1205 if (cmd_data->options.debug) {
1206 printf("Adding: %s\n", DYNAMIC_LINK_OPTS);
1208 push_count_chars(args, DYNAMIC_LINK_OPTS);
1209 if (cmd_data->undefined_flag) {
1210 push_count_chars(args, "-undefined");
1211 #if defined(__APPLE__)
1212 /* -undefined dynamic_lookup is used by the bundled Python in
1213 * 10.4, but if we don't set MACOSX_DEPLOYMENT_TARGET to 10.3+,
1214 * we'll get a linker error if we pass this flag.
1216 if (strcasecmp(cmd_data->undefined_flag,
1217 "dynamic_lookup") == 0) {
1218 insert_count_chars(cmd_data->program_opts,
1219 "MACOSX_DEPLOYMENT_TARGET=10.3", 0);
1222 push_count_chars(args, cmd_data->undefined_flag);
1225 #ifdef DYNAMIC_LINK_UNDEFINED
1226 if (cmd_data->options.debug) {
1227 printf("Adding: %s\n", DYNAMIC_LINK_UNDEFINED);
1229 push_count_chars(args, DYNAMIC_LINK_UNDEFINED);
1236 /* Read the final install location and add it to runtime library search path. */
1238 static void add_rpath(count_chars *cc, const char *path)
1243 #ifdef LINKER_FLAG_PREFIX
1244 size = strlen(LINKER_FLAG_PREFIX);
1246 size = size + strlen(path) + strlen(RPATH) + 2;
1251 #ifdef LINKER_FLAG_PREFIX
1252 strcpy(tmp, LINKER_FLAG_PREFIX);
1257 #ifndef LINKER_FLAG_NO_EQUALS
1262 push_count_chars(cc, tmp);
1265 static void add_rpath_file(count_chars *cc, const char *arg)
1269 path = load_install_path(arg);
1271 add_rpath(cc, path);
1275 static void add_rpath_noinstall(count_chars *cc, const char *arg, int pathlen)
1279 path = load_noinstall_path(arg, pathlen);
1281 add_rpath(cc, path);
1287 #ifdef DYNAMIC_LINK_NO_INSTALL
1288 static void add_dylink_noinstall(count_chars *cc, const char *arg, int pathlen,
1291 const char *install_path, *current_path, *name;
1293 int i_p_len, c_p_len, name_len, dyext_len, cur_len;
1295 install_path = load_install_path(arg);
1296 current_path = load_noinstall_path(arg, pathlen);
1298 if (!install_path || !current_path) {
1302 push_count_chars(cc, DYNAMIC_LINK_NO_INSTALL);
1304 i_p_len = strlen(install_path);
1305 c_p_len = strlen(current_path);
1308 name_len = extlen-pathlen;
1309 dyext_len = sizeof(DYNAMIC_LIB_EXT) - 1;
1311 /* No, we need to replace the extension. */
1312 exp_argument = (char *)malloc(i_p_len + c_p_len + (name_len*2) +
1316 strcpy(exp_argument, install_path);
1318 exp_argument[cur_len++] = '/';
1319 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1320 cur_len += name_len;
1321 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1322 cur_len += dyext_len;
1323 exp_argument[cur_len++] = ':';
1324 strcpy(exp_argument+cur_len, current_path);
1326 exp_argument[cur_len++] = '/';
1327 strncpy(exp_argument+cur_len, name, extlen-pathlen);
1328 cur_len += name_len;
1329 strcpy(exp_argument+cur_len, DYNAMIC_LIB_EXT);
1330 cur_len += dyext_len;
1332 push_count_chars(cc, exp_argument);
1337 /* use -L -llibname to allow to use installed libraries */
1338 static void add_minus_l(count_chars *cc, const char *arg)
1341 char *name = strrchr(arg, '/');
1342 char *file = strrchr(arg, '.');
1344 if ((name != NULL) && (file != NULL) &&
1345 (strstr(name, "lib") == (name + 1))) {
1350 push_count_chars(cc, "-L");
1351 push_count_chars(cc, arg);
1352 /* we need one argument like -lapr-1 */
1353 newarg = malloc(strlen(file) + 3);
1354 strcpy(newarg, "-l");
1355 strcat(newarg, file);
1356 push_count_chars(cc, newarg);
1358 push_count_chars(cc, arg);
1364 static void add_linker_flag_prefix(count_chars *cc, const char *arg)
1366 #ifndef LINKER_FLAG_PREFIX
1367 push_count_chars(cc, arg);
1370 newarg = (char*)malloc(strlen(arg) + sizeof(LINKER_FLAG_PREFIX) + 1);
1371 strcpy(newarg, LINKER_FLAG_PREFIX);
1372 strcat(newarg, arg);
1373 push_count_chars(cc, newarg);
1378 static int explode_static_lib(command_t *cmd_data, const char *lib)
1380 count_chars tmpdir_cc, libname_cc;
1381 const char *tmpdir, *libname;
1382 char savewd[PATH_MAX];
1385 struct dirent *entry;
1386 const char *lib_args[4];
1389 if (cmd_data->options.dry_run) {
1393 name = jlibtool_basename(lib);
1395 init_count_chars(&tmpdir_cc);
1396 push_count_chars(&tmpdir_cc, ".libs/");
1397 push_count_chars(&tmpdir_cc, name);
1398 push_count_chars(&tmpdir_cc, ".exploded/");
1399 tmpdir = flatten_count_chars(&tmpdir_cc, 0);
1401 if (!cmd_data->options.silent) {
1402 printf("Making: %s\n", tmpdir);
1406 push_count_chars(cmd_data->tmp_dirs, tmpdir);
1408 getcwd(savewd, sizeof(savewd));
1410 if (chdir(tmpdir) != 0) {
1411 if (!cmd_data->options.silent) {
1412 printf("Warning: could not explode %s\n", lib);
1417 if (lib[0] == '/') {
1421 init_count_chars(&libname_cc);
1422 push_count_chars(&libname_cc, "../../");
1423 push_count_chars(&libname_cc, lib);
1424 libname = flatten_count_chars(&libname_cc, 0);
1427 lib_args[0] = LIBRARIAN;
1429 lib_args[2] = libname;
1432 external_spawn(cmd_data, LIBRARIAN, lib_args);
1435 dir = opendir(tmpdir);
1437 while ((entry = readdir(dir)) != NULL) {
1438 #if defined(__APPLE__) && defined(RANLIB)
1439 /* Apple inserts __.SYMDEF which isn't needed.
1440 * Leopard (10.5+) can also add '__.SYMDEF SORTED' which isn't
1441 * much fun either. Just skip them.
1443 if (strstr(entry->d_name, "__.SYMDEF") != NULL) {
1447 if (entry->d_name[0] != '.') {
1448 push_count_chars(&tmpdir_cc, entry->d_name);
1449 name = flatten_count_chars(&tmpdir_cc, 0);
1450 if (cmd_data->options.debug) {
1451 printf("Adding: %s\n", name);
1453 push_count_chars(cmd_data->obj_files, name);
1454 pop_count_chars(&tmpdir_cc);
1462 static int parse_input_file_name(char *arg, command_t *cmd_data)
1464 const char *ext = strrchr(arg, '.');
1467 enum lib_type libtype;
1475 name = jlibtool_basename(arg);
1477 pathlen = name - arg;
1479 if (strcmp(ext, "lo") == 0) {
1480 newarg = check_object_exists(cmd_data, arg, ext - arg);
1482 fprintf(stderr, "Can not find suitable object file for %s\n", arg);
1485 if (cmd_data->mode != mLink) {
1486 push_count_chars(cmd_data->arglist, newarg);
1489 push_count_chars(cmd_data->obj_files, newarg);
1494 if (strcmp(ext, "la") == 0) {
1495 switch (cmd_data->mode) {
1497 /* Try the .libs dir first! */
1498 newarg = check_library_exists(cmd_data, arg, pathlen, 1, &libtype);
1500 /* Try the normal dir next. */
1501 newarg = check_library_exists(cmd_data, arg, pathlen, 0, &libtype);
1503 fprintf(stderr, "Can not find suitable library for %s\n", arg);
1508 /* It is not ok to just add the file: a library may added with:
1509 1 - -L path library_name. (For *.so in Linux).
1513 if (libtype == type_DYNAMIC_LIB) {
1514 add_minus_l(cmd_data->shared_opts.dependencies, newarg);
1515 } else if (cmd_data->output == otLibrary &&
1516 libtype == type_STATIC_LIB) {
1517 explode_static_lib(cmd_data, newarg);
1519 push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1522 if (cmd_data->output == otLibrary && libtype == type_STATIC_LIB) {
1523 explode_static_lib(cmd_data, newarg);
1526 push_count_chars(cmd_data->shared_opts.dependencies, newarg);
1529 if (libtype == type_DYNAMIC_LIB) {
1530 if (cmd_data->options.no_install) {
1532 add_rpath_noinstall(cmd_data->shared_opts.dependencies,
1535 #ifdef DYNAMIC_LINK_NO_INSTALL
1537 * This doesn't work as Darwin's linker has no way to
1538 * override at link-time the search paths for a
1539 * non-installed library.
1542 add_dylink_noinstall(cmd_data->shared_opts.dependencies,
1543 arg, pathlen, ext - arg);
1549 add_rpath_file(cmd_data->shared_opts.dependencies, arg);
1555 /* If we've already recorded a library to install, we're most
1556 * likely getting the .la file that we want to install as.
1557 * The problem is that we need to add it as the directory,
1558 * not the .la file itself. Otherwise, we'll do odd things.
1560 if (cmd_data->output == otLibrary) {
1561 arg[pathlen] = '\0';
1562 push_count_chars(cmd_data->arglist, arg);
1565 cmd_data->output = otLibrary;
1566 cmd_data->output_name = arg;
1567 cmd_data->static_name.install = gen_install_name(arg, 0);
1568 cmd_data->shared_name.install = gen_install_name(arg, 1);
1569 cmd_data->module_name.install = gen_install_name(arg, 2);
1571 if (!cmd_data->static_name.install &&
1572 !cmd_data->shared_name.install &&
1573 !cmd_data->module_name.install) {
1574 fprintf(stderr, "Files to install do not exist\n");
1586 if (strcmp(ext, "c") == 0) {
1587 /* If we don't already have an idea what our output name will be. */
1588 if (cmd_data->basename == NULL) {
1589 cmd_data->basename = (char *)malloc(strlen(arg) + 4);
1590 strcpy(cmd_data->basename, arg);
1591 strcpy(strrchr(cmd_data->basename, '.') + 1, "lo");
1593 cmd_data->fake_output_name = strrchr(cmd_data->basename, '/');
1594 if (cmd_data->fake_output_name) {
1595 cmd_data->fake_output_name++;
1598 cmd_data->fake_output_name = cmd_data->basename;
1606 static int parse_output_file_name(char *arg, command_t *cmd_data)
1610 char *newarg = NULL;
1613 cmd_data->fake_output_name = arg;
1615 name = jlibtool_basename(arg);
1616 ext = strrchr(name, '.');
1619 if (!ext || strcmp(ext, EXE_EXT) == 0) {
1623 cmd_data->basename = arg;
1624 cmd_data->output = otProgram;
1625 #if defined(_OSD_POSIX)
1626 cmd_data->options.pic_mode = pic_AVOID;
1628 newarg = (char *)malloc(strlen(arg) + 5);
1629 strcpy(newarg, arg);
1632 strcat(newarg, EXE_EXT);
1635 cmd_data->output_name = newarg;
1640 pathlen = name - arg;
1642 if (strcmp(ext, "la") == 0) {
1643 assert(cmd_data->mode == mLink);
1645 cmd_data->basename = arg;
1646 cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1647 cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1648 cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1649 cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1650 cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1651 cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1653 if (!cmd_data->options.dry_run) {
1656 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1658 strcpy(newname, cmd_data->static_name.normal);
1659 newext = strrchr(newname, '/');
1661 /* Check first to see if the dir already exists! */
1662 safe_mkdir(".libs");
1665 safe_mkdir(newname);
1670 #ifdef TRUNCATE_DLL_NAME
1672 arg = truncate_dll_name(arg);
1676 cmd_data->output_name = arg;
1680 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1681 assert(cmd_data->mode == mLink);
1683 cmd_data->basename = arg;
1684 cmd_data->options.shared = share_STATIC;
1685 cmd_data->output = otStaticLibraryOnly;
1686 cmd_data->static_name.normal = gen_library_name(arg, type_STATIC_LIB);
1687 cmd_data->static_name.install = gen_install_name(arg, type_STATIC_LIB);
1689 if (!cmd_data->options.dry_run) {
1692 newname = malloc(strlen(cmd_data->static_name.normal) + 1);
1694 strcpy(newname, cmd_data->static_name.normal);
1695 newext = strrchr(newname, '/');
1697 /* Check first to see if the dir already exists! */
1698 safe_mkdir(".libs");
1701 safe_mkdir(newname);
1706 cmd_data->output_name = arg;
1710 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1711 assert(cmd_data->mode == mLink);
1713 cmd_data->basename = arg;
1714 cmd_data->options.shared = share_SHARED;
1715 cmd_data->output = otDynamicLibraryOnly;
1716 cmd_data->shared_name.normal = gen_library_name(arg, type_DYNAMIC_LIB);
1717 cmd_data->module_name.normal = gen_library_name(arg, type_MODULE_LIB);
1718 cmd_data->shared_name.install = gen_install_name(arg, type_DYNAMIC_LIB);
1719 cmd_data->module_name.install = gen_install_name(arg, type_MODULE_LIB);
1721 if (!cmd_data->options.dry_run) {
1724 newname = malloc(strlen(cmd_data->shared_name.normal) + 1);
1726 strcpy(newname, cmd_data->shared_name.normal);
1727 newext = strrchr(newname, '/');
1729 /* Check first to see if the dir already exists! */
1730 safe_mkdir(".libs");
1733 safe_mkdir(newname);
1738 cmd_data->output_name = arg;
1742 if (strcmp(ext, "lo") == 0) {
1744 cmd_data->basename = arg;
1745 cmd_data->output = otObject;
1746 newarg = (char *)malloc(strlen(arg) + 2);
1747 strcpy(newarg, arg);
1748 newext = strrchr(newarg, '.') + 1;
1749 strcpy(newext, OBJECT_EXT);
1750 cmd_data->output_name = newarg;
1754 if (strcmp(ext, DYNAMIC_LIB_EXT) == 0) {
1755 fprintf(stderr, "Please build libraries with .la target, not ."
1756 DYNAMIC_LIB_EXT "\n");
1760 if (strcmp(ext, STATIC_LIB_EXT) == 0) {
1761 fprintf(stderr, "Please build libraries with .la target, not ."
1762 STATIC_LIB_EXT "\n");
1769 static char *automode(char *arg, command_t *cmd_data)
1771 if (cmd_data->mode != mUnknown) return arg;
1773 if (strcmp(arg, "CC") == 0) {
1775 cmd_data->mode = mCompile;
1777 } else if (strcmp(arg, "CXX") == 0) {
1779 cmd_data->mode = mCompile;
1781 } else if (strcmp(arg, "LINK") == 0) {
1783 cmd_data->mode = mLink;
1785 } else if (strcmp(arg, "LINK.c") == 0) {
1787 cmd_data->mode = mLink;
1789 } else if (strcmp(arg, "LINK.cxx") == 0) {
1791 cmd_data->mode = mLink;
1797 static void parse_args(int argc, char *argv[], command_t *cmd_data)
1804 * We now take a major step past libtool.
1806 * IF there's no "--mode=...", AND we recognise
1807 * the binary as a "special" name, THEN replace it
1808 * with the correct one, and set the correct mode.
1810 base = jlibtool_basename(argv[0]);
1811 arg = automode(base, cmd_data);
1814 if (cmd_data->options.debug) {
1815 printf("Adding: %s\n", arg);
1817 push_count_chars(cmd_data->arglist, arg);
1818 assert(cmd_data->mode != mUnknown);
1822 * We first pass over the command-line arguments looking for
1823 * "--mode", etc. If so, then use the libtool compatibility
1824 * method for building the software. Otherwise, auto-detect it
1825 * via "-o" and the extensions.
1828 if (cmd_data->mode == mUnknown) for (a = 1; a < argc; a++) {
1831 if (strncmp(arg, "--mode=", 7) == 0) {
1837 * Stop if we get another magic method
1840 ((strncmp(arg, "LINK", 4) == 0) ||
1841 (strcmp(arg, "CC") == 0) ||
1842 (strcmp(arg, "CXX") == 0))) {
1847 if (strncmp(arg, "-o", 2) == 0) {
1853 arg = strrchr(base, '.');
1855 cmd_data->mode = mLink;
1856 push_count_chars(cmd_data->arglist, LINK_c);
1859 else if (strcmp(arg, EXE_EXT) == 0) {
1860 cmd_data->mode = mLink;
1861 push_count_chars(cmd_data->arglist, LINK_c);
1864 else if (strcmp(arg + 1, DYNAMIC_LIB_EXT) == 0) {
1865 cmd_data->mode = mLink;
1866 push_count_chars(cmd_data->arglist, LINK_c);
1868 else if (strcmp(arg + 1, STATIC_LIB_EXT) == 0) {
1869 cmd_data->mode = mLink;
1870 push_count_chars(cmd_data->arglist, LINK_c);
1872 else if (strcmp(arg + 1, "la") == 0) {
1873 cmd_data->mode = mLink;
1874 push_count_chars(cmd_data->arglist, LINK_c);
1876 else if ((strcmp(arg + 1, "lo") == 0) ||
1877 (strcmp(arg + 1, "o") == 0)) {
1878 cmd_data->mode = mCompile;
1879 push_count_chars(cmd_data->arglist, CC);
1883 for (a = 1; a < argc; a++) {
1887 if (arg[0] == '-') {
1888 if (arg[1] == '-') {
1889 argused = parse_long_opt(arg + 2, cmd_data);
1892 argused = parse_short_opt(arg + 1, cmd_data);
1895 /* We haven't done anything with it yet, try some of the
1896 * more complicated short opts... */
1897 if (argused == 0 && a + 1 < argc) {
1898 if (arg[1] == 'o' && !arg[2]) {
1900 argused = parse_output_file_name(arg, cmd_data);
1901 } else if (strcmp(arg+1, "MT") == 0) {
1902 if (cmd_data->options.debug) {
1903 printf("Adding: %s\n", arg);
1905 push_count_chars(cmd_data->arglist, arg);
1907 if (!cmd_data->options.silent) {
1908 printf(" %s\n", arg);
1910 push_count_chars(cmd_data->arglist, arg);
1912 } else if (strcmp(arg+1, "rpath") == 0) {
1913 /* Aha, we should try to link both! */
1914 cmd_data->install_path = argv[++a];
1916 } else if (strcmp(arg+1, "release") == 0) {
1917 /* Store for later deciphering */
1918 cmd_data->version_info = argv[++a];
1920 } else if (strcmp(arg+1, "version-info") == 0) {
1921 /* Store for later deciphering */
1922 cmd_data->version_info = argv[++a];
1924 } else if (strcmp(arg+1, "export-symbols-regex") == 0) {
1925 /* Skip the argument. */
1928 } else if (strcmp(arg+1, "release") == 0) {
1929 /* Skip the argument. */
1932 } else if (strcmp(arg+1, "undefined") == 0) {
1933 cmd_data->undefined_flag = argv[++a];
1935 } else if (arg[1] == 'R' && !arg[2]) {
1936 /* -R dir Add dir to runtime library search path. */
1937 add_runtimedirlib(argv[++a], cmd_data);
1942 argused = parse_input_file_name(arg, cmd_data);
1947 * If we still don't have a run mode, look for a magic
1948 * program name CC, LINK, or whatever. Then replace that
1949 * with the name of the real program we want to run.
1951 if ((cmd_data->arglist->num == 0) &&
1952 (cmd_data->mode == mUnknown)) {
1953 arg = automode(arg, cmd_data);
1956 if (cmd_data->options.debug) {
1957 printf("Adding: %s\n", arg);
1959 push_count_chars(cmd_data->arglist, arg);
1966 static void generate_def_file(command_t *cmd_data)
1968 char def_file[1024];
1969 char implib_file[1024];
1972 char *export_args[1024];
1973 int num_export_args = 0;
1978 if (cmd_data->output_name) {
1979 strcpy(def_file, cmd_data->output_name);
1980 strcat(def_file, ".def");
1981 hDef = fopen(def_file, "w");
1984 fprintf(hDef, "LIBRARY '%s' INITINSTANCE\n", nameof(cmd_data->output_name));
1985 fprintf(hDef, "DATA NONSHARED\n");
1986 fprintf(hDef, "EXPORTS\n");
1989 for (a = 0; a < cmd_data->num_obj_files; a++) {
1990 cmd_size += strlen(cmd_data->obj_files[a]) + 1;
1993 cmd_size += strlen(GEN_EXPORTS) + strlen(def_file) + 3;
1994 cmd = (char *)malloc(cmd_size);
1995 strcpy(cmd, GEN_EXPORTS);
1997 for (a=0; a < cmd_data->num_obj_files; a++) {
1999 strcat(cmd, cmd_data->obj_files[a] );
2003 strcat(cmd, def_file);
2005 export_args[num_export_args++] = SHELL_CMD;
2006 export_args[num_export_args++] = "-c";
2007 export_args[num_export_args++] = cmd;
2008 export_args[num_export_args++] = NULL;
2009 external_spawn(cmd_data, export_args[0], (const char**)export_args);
2010 cmd_data->arglist[cmd_data->num_args++] = strdup(def_file);
2012 /* Now make an import library for the dll */
2013 num_export_args = 0;
2014 export_args[num_export_args++] = DEF2IMPLIB_CMD;
2015 export_args[num_export_args++] = "-o";
2017 strcpy(implib_file, ".libs/");
2018 strcat(implib_file, cmd_data->basename);
2019 ext = strrchr(implib_file, '.');
2024 strcat(implib_file, ".");
2025 strcat(implib_file, STATIC_LIB_EXT);
2027 export_args[num_export_args++] = implib_file;
2028 export_args[num_export_args++] = def_file;
2029 export_args[num_export_args++] = NULL;
2030 external_spawn(cmd_data, export_args[0], (const char**)export_args);
2038 static const char* expand_path(const char *relpath)
2040 char foo[PATH_MAX], *newpath;
2042 getcwd(foo, PATH_MAX-1);
2043 newpath = (char*)malloc(strlen(foo)+strlen(relpath)+2);
2044 strcpy(newpath, foo);
2045 strcat(newpath, "/");
2046 strcat(newpath, relpath);
2051 static void link_fixup(command_t *c)
2053 /* If we were passed an -rpath directive, we need to build
2054 * shared objects too. Otherwise, we should only create static
2057 if (!c->install_path && (c->output == otDynamicLibraryOnly ||
2058 c->output == otModule || c->output == otLibrary)) {
2059 if (c->options.shared == share_SHARED) {
2060 c->install_path = LIBDIR;
2064 if (c->output == otDynamicLibraryOnly ||
2065 c->output == otModule ||
2066 c->output == otLibrary) {
2068 push_count_chars(c->shared_opts.normal, "-o");
2069 if (c->output == otModule) {
2070 push_count_chars(c->shared_opts.normal, c->module_name.normal);
2074 push_count_chars(c->shared_opts.normal, c->shared_name.normal);
2075 #ifdef DYNAMIC_INSTALL_NAME
2076 push_count_chars(c->shared_opts.normal, DYNAMIC_INSTALL_NAME);
2078 if (!c->install_path) {
2079 fprintf(stderr, "Installation mode requires -rpath\n");
2083 tmp = (char*)malloc(PATH_MAX);
2084 strcpy(tmp, c->install_path);
2085 if (c->shared_name.install) {
2086 strcat(tmp, strrchr(c->shared_name.install, '/'));
2088 strcat(tmp, strrchr(c->shared_name.normal, '/'));
2090 push_count_chars(c->shared_opts.normal, tmp);
2094 append_count_chars(c->shared_opts.normal, c->obj_files);
2095 append_count_chars(c->shared_opts.normal, c->shared_opts.dependencies);
2097 if (c->options.export_all) {
2099 generate_def_file(c);
2104 if (c->output == otLibrary || c->output == otStaticLibraryOnly) {
2105 push_count_chars(c->static_opts.normal, "-o");
2106 push_count_chars(c->static_opts.normal, c->output_name);
2109 if (c->output == otProgram) {
2110 if (c->output_name) {
2111 push_count_chars(c->arglist, "-o");
2112 push_count_chars(c->arglist, c->output_name);
2113 append_count_chars(c->arglist, c->obj_files);
2114 append_count_chars(c->arglist, c->shared_opts.dependencies);
2115 add_dynamic_link_opts(c, c->arglist);
2120 static void post_parse_fixup(command_t *cmd_data)
2122 switch (cmd_data->mode)
2126 if (cmd_data->options.pic_mode != pic_AVOID) {
2127 push_count_chars(cmd_data->arglist, PIC_FLAG);
2130 if (cmd_data->output_name) {
2131 push_count_chars(cmd_data->arglist, "-o");
2132 push_count_chars(cmd_data->arglist, cmd_data->output_name);
2136 link_fixup(cmd_data);
2139 if (cmd_data->output == otLibrary) {
2140 link_fixup(cmd_data);
2147 if (cmd_data->output == otObject ||
2148 cmd_data->output == otProgram ||
2149 cmd_data->output == otLibrary ||
2150 cmd_data->output == otDynamicLibraryOnly) {
2151 push_count_chars(cmd_data->arglist, "-Zomf");
2155 if (cmd_data->options.shared &&
2156 (cmd_data->output == otObject ||
2157 cmd_data->output == otLibrary ||
2158 cmd_data->output == otDynamicLibraryOnly)) {
2160 push_count_chars(cmd_data->arglist, SHARE_SW);
2165 static int run_mode(command_t *cmd_data)
2168 count_chars *cctemp;
2170 cctemp = (count_chars*)malloc(sizeof(count_chars));
2171 init_count_chars(cctemp);
2173 switch (cmd_data->mode)
2176 rv = run_command(cmd_data, cmd_data->arglist);
2177 if (rv) goto finish;
2180 /* Well, we'll assume it's a file going to a directory... */
2181 /* For brain-dead install-sh based scripts, we have to repeat
2182 * the command N-times. install-sh should die.
2184 if (!cmd_data->output_name) {
2185 rv = run_command(cmd_data, cmd_data->arglist);
2186 if (rv) goto finish;
2188 if (cmd_data->output_name) {
2189 append_count_chars(cctemp, cmd_data->arglist);
2190 insert_count_chars(cctemp,
2191 cmd_data->output_name,
2193 rv = run_command(cmd_data, cctemp);
2194 if (rv) goto finish;
2195 clear_count_chars(cctemp);
2197 if (cmd_data->static_name.install) {
2198 append_count_chars(cctemp, cmd_data->arglist);
2199 insert_count_chars(cctemp,
2200 cmd_data->static_name.install,
2202 rv = run_command(cmd_data, cctemp);
2203 if (rv) goto finish;
2204 #if defined(__APPLE__) && defined(RANLIB)
2205 /* From the Apple libtool(1) manpage on Tiger/10.4:
2207 * With the way libraries used to be created, errors were possible
2208 * if the library was modified with ar(1) and the table of
2209 * contents was not updated by rerunning ranlib(1). Thus the
2210 * link editor, ld, warns when the modification date of a library
2211 * is more recent than the creation date of its table of
2212 * contents. Unfortunately, this means that you get the warning
2213 * even if you only copy the library.
2216 * This means that when we install the static archive, we need to
2217 * rerun ranlib afterwards.
2219 const char *lib_args[3], *static_lib_name;
2222 len1 = strlen(cmd_data->arglist->vals[cmd_data->arglist->num - 1]);
2224 static_lib_name = jlibtool_basename(cmd_data->static_name.install);
2225 len2 = strlen(static_lib_name);
2227 tmp = malloc(len1 + len2 + 2);
2229 snprintf(tmp, len1 + len2 + 2, "%s/%s",
2230 cmd_data->arglist->vals[cmd_data->arglist->num - 1],
2233 lib_args[0] = RANLIB;
2236 external_spawn(cmd_data, RANLIB, lib_args);
2239 clear_count_chars(cctemp);
2241 if (cmd_data->shared_name.install) {
2242 append_count_chars(cctemp, cmd_data->arglist);
2243 insert_count_chars(cctemp,
2244 cmd_data->shared_name.install,
2246 rv = run_command(cmd_data, cctemp);
2247 if (rv) goto finish;
2248 clear_count_chars(cctemp);
2250 if (cmd_data->module_name.install) {
2251 append_count_chars(cctemp, cmd_data->arglist);
2252 insert_count_chars(cctemp,
2253 cmd_data->module_name.install,
2255 rv = run_command(cmd_data, cctemp);
2256 if (rv) goto finish;
2257 clear_count_chars(cctemp);
2261 if (cmd_data->output == otStaticLibraryOnly ||
2262 cmd_data->output == otLibrary) {
2264 const char *lib_args[3];
2266 /* Removes compiler! */
2267 cmd_data->program = LIBRARIAN;
2268 push_count_chars(cmd_data->program_opts, LIBRARIAN_OPTS);
2269 push_count_chars(cmd_data->program_opts,
2270 cmd_data->static_name.normal);
2272 rv = run_command(cmd_data, cmd_data->obj_files);
2273 if (rv) goto finish;
2276 lib_args[0] = RANLIB;
2277 lib_args[1] = cmd_data->static_name.normal;
2279 external_spawn(cmd_data, RANLIB, lib_args);
2283 if (cmd_data->output == otDynamicLibraryOnly ||
2284 cmd_data->output == otModule ||
2285 cmd_data->output == otLibrary) {
2286 cmd_data->program = NULL;
2287 clear_count_chars(cmd_data->program_opts);
2289 append_count_chars(cmd_data->program_opts, cmd_data->arglist);
2290 if (cmd_data->output == otModule) {
2292 push_count_chars(cmd_data->program_opts, MODULE_OPTS);
2296 push_count_chars(cmd_data->program_opts, SHARED_OPTS);
2298 #ifdef dynamic_link_version_func
2299 push_count_chars(cmd_data->program_opts,
2300 dynamic_link_version_func(cmd_data->version_info));
2303 add_dynamic_link_opts(cmd_data, cmd_data->program_opts);
2305 rv = run_command(cmd_data, cmd_data->shared_opts.normal);
2306 if (rv) goto finish;
2308 if (cmd_data->output == otProgram) {
2309 rv = run_command(cmd_data, cmd_data->arglist);
2310 if (rv) goto finish;
2315 char *l, libpath[PATH_MAX];
2317 strcpy(libpath, cmd_data->arglist->vals[0]);
2318 add_dotlibs(libpath);
2319 l = strrchr(libpath, '/');
2320 if (!l) l = strrchr(libpath, '\\');
2328 l = "./build/lib/.libs";
2329 setenv(LD_LIBRARY_PATH_LOCAL, l, 1);
2330 rv = run_command(cmd_data, cmd_data->arglist);
2331 if (rv) goto finish;
2345 static void cleanup_tmp_dir(const char *dirname)
2348 struct dirent *entry;
2349 char fullname[1024];
2351 dir = opendir(dirname);
2356 if ((strlen(dirname) + 1 + entry->d_namlen) >= sizeof(fullname)) {
2357 fprintf(stderr, "Dirname too long out of buffer space");
2361 while ((entry = readdir(dir)) != NULL) {
2362 if (entry->d_name[0] != '.') {
2363 strcpy(fullname, dirname);
2364 strcat(fullname, "/");
2365 strcat(fullname, entry->d_name);
2366 (void) remove(fullname);
2372 (void) closedir(dir);
2375 static void cleanup_tmp_dirs(command_t *cmd_data)
2379 for (d = 0; d < cmd_data->tmp_dirs->num; d++) {
2380 cleanup_tmp_dir(cmd_data->tmp_dirs->vals[d]);
2384 static int ensure_fake_uptodate(command_t *cmd_data)
2386 /* FIXME: could do the stat/touch here, but nah... */
2387 const char *touch_args[3];
2389 if (cmd_data->mode == mInstall) {
2392 if (!cmd_data->fake_output_name) {
2396 touch_args[0] = "touch";
2397 touch_args[1] = cmd_data->fake_output_name;
2398 touch_args[2] = NULL;
2399 return external_spawn(cmd_data, "touch", touch_args);
2402 /* Store the install path in the *.la file */
2403 static int add_for_runtime(command_t *cmd_data)
2405 if (cmd_data->mode == mInstall) {
2408 if (cmd_data->output == otDynamicLibraryOnly ||
2409 cmd_data->output == otLibrary) {
2410 FILE *f=fopen(cmd_data->fake_output_name,"w");
2414 fprintf(f,"%s\n", cmd_data->install_path);
2418 return(ensure_fake_uptodate(cmd_data));
2422 int main(int argc, char *argv[])
2427 memset(&cmd_data, 0, sizeof(cmd_data));
2429 cmd_data.options.pic_mode = pic_UNKNOWN;
2431 cmd_data.program_opts = (count_chars*)malloc(sizeof(count_chars));
2432 init_count_chars(cmd_data.program_opts);
2433 cmd_data.arglist = (count_chars*)malloc(sizeof(count_chars));
2434 init_count_chars(cmd_data.arglist);
2435 cmd_data.tmp_dirs = (count_chars*)malloc(sizeof(count_chars));
2436 init_count_chars(cmd_data.tmp_dirs);
2437 cmd_data.obj_files = (count_chars*)malloc(sizeof(count_chars));
2438 init_count_chars(cmd_data.obj_files);
2439 cmd_data.dep_rpaths = (count_chars*)malloc(sizeof(count_chars));
2440 init_count_chars(cmd_data.dep_rpaths);
2441 cmd_data.rpaths = (count_chars*)malloc(sizeof(count_chars));
2442 init_count_chars(cmd_data.rpaths);
2443 cmd_data.static_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2444 init_count_chars(cmd_data.static_opts.normal);
2445 cmd_data.shared_opts.normal = (count_chars*)malloc(sizeof(count_chars));
2446 init_count_chars(cmd_data.shared_opts.normal);
2447 cmd_data.shared_opts.dependencies = (count_chars*)malloc(sizeof(count_chars));
2448 init_count_chars(cmd_data.shared_opts.dependencies);
2450 cmd_data.mode = mUnknown;
2451 cmd_data.output = otGeneral;
2453 parse_args(argc, argv, &cmd_data);
2454 post_parse_fixup(&cmd_data);
2456 if (cmd_data.mode == mUnknown) {
2460 rc = run_mode(&cmd_data);
2463 add_for_runtime(&cmd_data);
2466 cleanup_tmp_dirs(&cmd_data);