/[gentoo-alt]/trunk/toolchain-prefix-wrapper/ld/gnuplugin.c
Gentoo

Contents of /trunk/toolchain-prefix-wrapper/ld/gnuplugin.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1591 - (show annotations) (download) (as text)
Fri Nov 2 10:58:02 2007 UTC (6 years, 8 months ago) by haubi
File MIME type: text/x-csrc
File size: 4668 byte(s)
initial import of toolchain-prefix-wrapper

1 /*
2 * Copyright 2007 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2
4 * Author: Michael Haubenwallner <haubi@gentoo.org>
5 */
6
7 #include <config.h>
8
9 #include "gnuplugin.h"
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20
21 StringList* gnuplugin(StringList *argList, StringList *userLibpathList, StringList *sysLibpathList)
22 {
23 static const char rpathArg_rpath[] = "-rpath";
24 static int rpathArg_rpath_len = 6;
25 static const char rpathArg_R[] = "-R";
26 static int rpathArg_R_len = 2;
27
28 int argc = 0;
29 int err;
30 StringList *newArgList = NULL;
31 StringList *runpathList = NULL;
32 String *argString;
33 String *newString = NULL;
34 char const *argBuffer;
35 int argLength;
36 char const *rpathArg = NULL;
37 int rpathArgLen = 0;
38
39 do { /* dummy loop */
40 err = 1;
41
42 /* keep argv[0] */
43 newArgList = StringListCreate(argList, 0, 1);
44 if (newArgList == NULL)
45 break;
46
47 runpathList = StringListCreate(NULL, 0, 0);
48 if (runpathList == NULL)
49 break;
50
51 err = 0;
52 for(argc = 1; !err && argc < StringListGetSize(argList); argc++) {
53 err = 1;
54
55 argString = StringListGetString(argList, argc);
56 argBuffer = StringGetBuffer(argString);
57 argLength = StringGetLength(argString);
58
59 rpathArg = NULL;
60
61 if (strncmp(argBuffer, rpathArg_rpath, rpathArg_rpath_len) == 0) {
62 rpathArg = rpathArg_rpath;
63 rpathArgLen = rpathArg_rpath_len;
64 if (argBuffer[rpathArgLen] == '=') {
65 ++ rpathArgLen;
66 } else
67 if (argBuffer[rpathArgLen] != '\0') {
68 /* is not "-rpath=" */
69 rpathArg = NULL;
70 }
71 } else
72 if (strncmp(argBuffer, rpathArg_R, rpathArg_R_len) == 0) {
73 rpathArg = rpathArg_R;
74 rpathArgLen = rpathArg_R_len;
75 }
76 if (rpathArg != NULL) {
77 /* collect -rpath path arguments */
78 char const *curr, *next;
79
80 argBuffer += rpathArgLen;
81 argLength -= rpathArgLen;
82
83 if (*argBuffer == 0 && argc < StringListGetSize(argList)) {
84 argc++;
85 argString = StringListGetString(argList, argc);
86 argBuffer = StringGetBuffer(argString);
87 argLength = StringGetLength(argString);
88 }
89
90 err = 0;
91 for(curr = next = argBuffer; !err && *next != '\0'; curr = next+1) {
92 err = 1;
93
94 for(next = curr; *next != '\0' && *next != ':'; next++);
95
96 if (curr == argBuffer && rpathArg == rpathArg_R) {
97 struct stat sbuf;
98 int rv = stat(curr, &sbuf);
99 /* if argument to "-R" is not a directory, then it is not a rpath */
100 if (rv != 0 || (sbuf.st_mode & S_IFDIR) == 0) {
101 if (StringListAppendConcat(newArgList
102 , rpathArg_R, rpathArg_R_len
103 , curr, next - curr
104 , NULL) < 0) {
105 /* failed to append arg */
106 break;
107 }
108 /* done */
109 err = 0;
110 break;
111 }
112 }
113
114 if (next - curr <= 1) {
115 /* skip empty path */
116 err = 0;
117 continue;
118 }
119
120 if (StringListContains(sysLibpathList, curr, next - curr)) {
121 /* skip default path */
122 err = 0;
123 continue;
124 }
125
126 if (StringListContains(runpathList, curr, next - curr)) {
127 /* skip already recorded runpath */
128 err = 0;
129 continue;
130 }
131
132 /* record this path into runpath list */
133 if (StringListAppendConcat(runpathList, curr, next - curr, NULL) < 0)
134 break;
135
136 err = 0;
137 }
138 if (err)
139 break;
140
141 /* end "-rpath" handling */
142 continue;
143 }
144
145 /* keep other arguments on commandline */
146 if (StringListAppendString(newArgList, argString) < 0)
147 break;
148
149 err = 0;
150 }
151 if (argc < StringListGetSize(argList)) {
152 /* error during argument parsing */
153 break;
154 }
155
156 /* add sys libpath's to runpath list */
157 if (StringListAppendList(runpathList, sysLibpathList, 0, -1) < 0)
158 break;
159
160 if (StringListGetSize(runpathList) > 0) {
161 /* put runpath arg on commandline */
162 if (StringListAppendConcat(newArgList, "-rpath", 6, NULL) < 0) break;
163
164 /* create string containing libpath1:libpathN */
165 newString = StringListJoin(runpathList, NULL, 0, ":", 1, NULL, 0);
166 if (newString == NULL) {
167 break;
168 }
169
170 /* put runpath on commandline */
171 if (StringListAppendString(newArgList, newString) < 0) break;
172 }
173
174 /* append sys libpath's with "-L" */
175 if (StringListAppendListModify(newArgList, sysLibpathList, 0, -1, "-L", 2, NULL, 0) < 0)
176 break;
177
178 err = 0;
179 } while(0); /* end dummy loop */
180
181 if (newString != NULL) newString = StringDestroy(newString);
182 if (runpathList != NULL) runpathList = StringListDestroy(runpathList);
183
184 if (err && newArgList != NULL) newArgList = StringListDestroy(newArgList);
185
186 return newArgList;
187 }
188
189 #if defined(__cplusplus)
190 }
191 #endif

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.20