summaryrefslogtreecommitdiff
path: root/src/logic/slbt_exec_compile.c
blob: dca6e1edc72f9a8345d19f28f8f6f7bcc8b796d9 (plain)
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
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2018  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"
#include "slibtool_mkdir_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_metafile_impl.h"

static int slbt_exec_compile_remove_file(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	const char *			target)
{
	(void)ectx;

	/* remove target (if any) */
	if (!(unlink(target)) || (errno == ENOENT))
		return 0;

	return SLBT_SYSTEM_ERROR(dctx,0);
}

static int slbt_exec_compile_finalize_argument_vector(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx)
{
	char *		sargv[1024];
	char **		sargvbuf;
	char **		base;
	char **		parg;
	char **		aarg;
	char **		aargv;
	char **		cap;
	char **		src;
	char **		dst;
	char *		ccwrap;

	/* vector size */
	base = ectx->argv;
	parg = ectx->argv;

	for (; *parg; )
		parg++;

	/* buffer */
	if (parg - base < 1024) {
		aargv    = sargv;
		aarg     = aargv;
		sargvbuf = 0;

	} else if (!(sargvbuf = calloc(parg-base+1,sizeof(char *)))) {
		return SLBT_SYSTEM_ERROR(dctx,0);

	} else {
		aargv = sargvbuf;
		aarg  = aargv;
	}

	/* (program name) */
	parg = &base[1];

	/* split object args from all other args, record output */
	/* annotation, and remove redundant -l arguments       */
	for (; *parg; ) {
		if (ectx->lout[0] == parg) {
			ectx->lout[0] = &aarg[0];
			ectx->lout[1] = &aarg[1];
		}

		if (ectx->mout[0] == parg) {
			ectx->mout[0] = &aarg[0];
			ectx->mout[1] = &aarg[1];
		}

		/* placeholder argument? */
		if (!strncmp(*parg,"-USLIBTOOL_PLACEHOLDER_",23)) {
			parg++;
		} else {
			*aarg++ = *parg++;
		}
	}

	/* program name, ccwrap */
	if ((ccwrap = (char *)dctx->cctx->ccwrap)) {
		base[1] = base[0];
		base[0] = ccwrap;
		base++;
	}

	/* join all other args */
	src = aargv;
	cap = aarg;
	dst = &base[1];

	for (; src<cap; )
		*dst++ = *src++;

	/* properly null-terminate argv, accounting for redundant arguments */
	*dst = 0;

	/* output annotation */
	if (ectx->lout[0]) {
		ectx->lout[0] = &base[1] + (ectx->lout[0] - aargv);
		ectx->lout[1] = ectx->lout[0] + 1;
	}

	if (ectx->mout[0]) {
		ectx->mout[0] = &base[1] + (ectx->mout[0] - aargv);
		ectx->mout[1] = ectx->mout[0] + 1;
	}

	/* all done */
	if (sargvbuf)
		free(sargvbuf);

	return 0;
}

int  slbt_exec_compile(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx)
{
	int				ret;
	char *				fpic;
	char *				ccwrap;
	struct slbt_exec_ctx *		actx = 0;
	const struct slbt_common_ctx *	cctx = dctx->cctx;

	/* dry run */
	if (cctx->drvflags & SLBT_DRIVER_DRY_RUN)
		return 0;

	/* context */
	if (ectx)
		slbt_reset_placeholders(ectx);
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
		return ret;
	else
		actx = ectx;

	/* remove old .lo wrapper */
	if (slbt_exec_compile_remove_file(dctx,ectx,ectx->ltobjname))
		return SLBT_NESTED_ERROR(dctx);

	/* .libs directory */
	if (cctx->drvflags & SLBT_DRIVER_SHARED)
		if (slbt_mkdir(dctx,ectx->ldirname)) {
			slbt_free_exec_ctx(actx);
			return SLBT_SYSTEM_ERROR(dctx,ectx->ldirname);
		}

	/* compile mode */
	ccwrap        = (char *)cctx->ccwrap;
	ectx->program = ccwrap ? ccwrap : ectx->compiler;
	ectx->argv    = ectx->cargv;

	/* -fpic */
	switch (cctx->tag) {
		case SLBT_TAG_CC:
		case SLBT_TAG_CXX:
		case SLBT_TAG_F77:
			fpic = cctx->settings.picswitch
				? cctx->settings.picswitch
				: *ectx->fpic;
			break;

		default:
			fpic = *ectx->fpic;
	}

	/* shared library object */
	if (cctx->drvflags & SLBT_DRIVER_SHARED) {
		if (!(cctx->drvflags & SLBT_DRIVER_ANTI_PIC)) {
			*ectx->dpic = "-DPIC";
			*ectx->fpic = fpic;
		}

		*ectx->lout[0] = "-o";
		*ectx->lout[1] = ectx->lobjname;

		if (slbt_exec_compile_finalize_argument_vector(dctx,ectx))
			return SLBT_NESTED_ERROR(dctx);

		if (!(cctx->drvflags & SLBT_DRIVER_SILENT)) {
			if (slbt_output_compile(dctx,ectx)) {
				slbt_free_exec_ctx(actx);
				return SLBT_NESTED_ERROR(dctx);
			}
		}

		if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) {
			slbt_free_exec_ctx(actx);
			return SLBT_SYSTEM_ERROR(dctx,0);
		}

		if (cctx->drvflags & SLBT_DRIVER_STATIC)
			slbt_reset_argvector(ectx);
	}

	/* static archive object */
	if (cctx->drvflags & SLBT_DRIVER_STATIC) {
		slbt_reset_placeholders(ectx);

		if (cctx->drvflags & SLBT_DRIVER_PRO_PIC) {
			*ectx->dpic = "-DPIC";
			*ectx->fpic = fpic;
		}

		*ectx->lout[0] = "-o";
		*ectx->lout[1] = ectx->aobjname;

		if (slbt_exec_compile_finalize_argument_vector(dctx,ectx))
			return SLBT_NESTED_ERROR(dctx);

		if (!(cctx->drvflags & SLBT_DRIVER_SILENT)) {
			if (slbt_output_compile(dctx,ectx)) {
				slbt_free_exec_ctx(actx);
				return SLBT_NESTED_ERROR(dctx);
			}
		}

		if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) {
			slbt_free_exec_ctx(actx);
			return SLBT_SYSTEM_ERROR(dctx,0);
		}
	}

	ret = slbt_create_object_wrapper(dctx,ectx);
	slbt_free_exec_ctx(actx);

	return ret ? SLBT_NESTED_ERROR(dctx) : 0;
}