1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/*******************************************************************/
/* slibtool: a skinny libtool implementation, written in C */
/* Copyright (C) 2016 Z. Gilboa */
/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <slibtool/slibtool.h>
#include "slibtool_spawn_impl.h"
/*******************************************************************/
/* */
/* -o <ltlib> switches input result */
/* ---------- --------------------- ----- ------ */
/* libfoo.a [-shared|-static] bar.lo libfoo.a */
/* */
/* ar cru libfoo.a bar.o */
/* ranlib libfoo.a */
/* */
/*******************************************************************/
static bool slbt_adjust_input_argument(char * arg, bool fpic)
{
char * slash;
char * dot;
char base[PATH_MAX];
if (*arg == '-')
return false;
if (!(dot = strrchr(arg,'.')))
return false;
if (strcmp(dot,".lo"))
return false;
if (fpic) {
if ((slash = strrchr(arg,'/')))
slash++;
else
slash = arg;
if ((size_t)snprintf(base,sizeof(base),"%s",
slash) >= sizeof(base))
return false;
sprintf(slash,".libs/%s",base);
dot = strrchr(arg,'.');
}
dot[1] = 'o';
dot[2] = '\0';
return true;
}
static int slbt_exec_link_create_archive(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
const char * arfilename,
bool fpic)
{
char ** aarg;
char ** parg;
char * ranlib[3];
char program[PATH_MAX];
char output [PATH_MAX];
/* initial state */
slbt_reset_arguments(ectx);
/* placeholders */
slbt_reset_placeholders(ectx);
/* alternate program (ar, ranlib) */
ectx->program = program;
/* output */
if ((size_t)snprintf(output,sizeof(output),"%s",
arfilename) >= sizeof(output))
return -1;
/* ar alternate argument vector */
if ((size_t)snprintf(program,sizeof(program),"%s",
dctx->cctx->host.ar) >= sizeof(program))
return -1;
aarg = ectx->altv;
*aarg++ = program;
*aarg++ = "cru";
*aarg++ = output;
/* input argument adjustment */
for (parg=ectx->cargv; *parg; parg++)
if (slbt_adjust_input_argument(*parg,fpic))
*aarg++ = *parg;
*aarg = 0;
ectx->argv = ectx->altv;
/* step output */
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
if (slbt_output_link(dctx,ectx))
return -1;
/* ar spawn */
if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
return -1;
/* ranlib argv */
if ((size_t)snprintf(program,sizeof(program),"%s",
dctx->cctx->host.ranlib) >= sizeof(program))
return -1;
ranlib[0] = program;
ranlib[1] = output;
ranlib[2] = 0;
ectx->argv = ranlib;
/* step output */
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
if (slbt_output_link(dctx,ectx))
return -1;
/* ranlib spawn */
if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
return -1;
return 0;
}
static int slbt_exec_link_create_symlink(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx,
const char * target,
char * lnkname)
{
const char * slash;
char * ln[6];
char atarget[PATH_MAX];
/* atarget */
if ((slash = strrchr(target,'/')))
slash++;
else
slash = target;
if ((size_t)snprintf(atarget,sizeof(atarget),"../%s",
slash) >= sizeof(atarget))
return -1;
/* ln argv (fake) */
ln[0] = "ln";
ln[1] = "-f";
ln[2] = "-s";
ln[3] = atarget;
ln[4] = lnkname;
ectx->argv = ln;
/* step output */
if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
if (slbt_output_link(dctx,ectx))
return -1;
/* remove old symlink (if any) */
unlink(lnkname);
/* create symlink */
return symlink(atarget,lnkname);
}
int slbt_exec_link(
const struct slbt_driver_ctx * dctx,
struct slbt_exec_ctx * ectx)
{
int ret;
int fdlibs;
const char * output;
char * dot;
FILE * fout;
struct slbt_exec_ctx * actx;
/* context */
if (ectx)
actx = 0;
else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
return ret;
else
actx = ectx;
/* output suffix */
output = dctx->cctx->output;
dot = strrchr(output,'.');
/* .libs directory */
if (dctx->cctx->drvflags & SLBT_DRIVER_SHARED) {
if ((fdlibs = open(ectx->ldirname,O_DIRECTORY)) >= 0)
close(fdlibs);
else if ((errno != ENOENT) || mkdir(ectx->ldirname,0777)) {
slbt_free_exec_ctx(actx);
return -1;
}
}
/* non-pic libfoo.a */
if (dot && !strcmp(dot,".a"))
if (slbt_exec_link_create_archive(dctx,ectx,output,false)) {
slbt_free_exec_ctx(actx);
return -1;
}
/* no wrapper? */
if (!dot || strcmp(dot,".la")) {
slbt_free_exec_ctx(actx);
return 0;
}
/* hey, yo, let's rap it up */
if (!(fout = fopen(output,"w"))) {
slbt_free_exec_ctx(actx);
return -1;
}
ret = fprintf(fout,
"# slibtool (pre-alphe) generated file\n\n");
/* wrapper symlink */
if (slbt_exec_link_create_symlink(
dctx,ectx,
output,
ectx->lafilename)) {
slbt_free_exec_ctx(actx);
return -1;
}
/* all done */
fclose(fout);
slbt_free_exec_ctx(actx);
return (ret > 0) ? 0 : -1;
}
|