Blame src/logic/slbt_exec_compile.c

36de3e
/*******************************************************************/
36de3e
/*  slibtool: a skinny libtool implementation, written in C        */
6803d8
/*  Copyright (C) 2016--2018  Z. Gilboa                            */
36de3e
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
36de3e
/*******************************************************************/
36de3e
36de3e
#include <string.h>
36de3e
#include <stdbool.h>
36de3e
#include <fcntl.h>
36de3e
#include <errno.h>
36de3e
#include <sys/stat.h>
36de3e
36de3e
#include <slibtool/slibtool.h>
36de3e
#include "slibtool_spawn_impl.h"
3895af
#include "slibtool_mkdir_impl.h"
9149bc
#include "slibtool_errinfo_impl.h"
70f645
#include "slibtool_metafile_impl.h"
36de3e
d117fc
static int slbt_exec_compile_remove_file(
d117fc
	const struct slbt_driver_ctx *	dctx,
d117fc
	struct slbt_exec_ctx *		ectx,
d117fc
	const char *			target)
d117fc
{
d3ca02
	(void)ectx;
d3ca02
d117fc
	/* remove target (if any) */
d117fc
	if (!(unlink(target)) || (errno == ENOENT))
d117fc
		return 0;
d117fc
9149bc
	return SLBT_SYSTEM_ERROR(dctx);
d117fc
}
d117fc
4ac23a
static int slbt_exec_compile_finalize_argument_vector(
4ac23a
	const struct slbt_driver_ctx *	dctx,
4ac23a
	struct slbt_exec_ctx *		ectx)
4ac23a
{
4ac23a
	char *		sargv[1024];
4ac23a
	char **		sargvbuf;
4ac23a
	char **		base;
4ac23a
	char **		parg;
4ac23a
	char **		aarg;
4ac23a
	char **		aargv;
4ac23a
	char **		cap;
4ac23a
	char **		src;
4ac23a
	char **		dst;
34988f
	char *		ccwrap;
4ac23a
4ac23a
	/* vector size */
4ac23a
	base = ectx->argv;
4ac23a
	parg = ectx->argv;
4ac23a
4ac23a
	for (; *parg; )
4ac23a
		parg++;
4ac23a
4ac23a
	/* buffer */
4ac23a
	if (parg - base < 1024) {
4ac23a
		aargv    = sargv;
4ac23a
		aarg     = aargv;
4ac23a
		sargvbuf = 0;
4ac23a
4ac23a
	} else if (!(sargvbuf = calloc(parg-base+1,sizeof(char *)))) {
4ac23a
		return SLBT_SYSTEM_ERROR(dctx);
4ac23a
4ac23a
	} else {
4ac23a
		aargv = sargvbuf;
4ac23a
		aarg  = aargv;
4ac23a
	}
4ac23a
4ac23a
	/* (program name) */
4ac23a
	parg = &base[1];
4ac23a
4ac23a
	/* split object args from all other args, record output */
4ac23a
	/* annotation, and remove redundant -l arguments       */
4ac23a
	for (; *parg; ) {
4ac23a
		if (ectx->lout[0] == parg) {
4ac23a
			ectx->lout[0] = &aarg[0];
4ac23a
			ectx->lout[1] = &aarg[1];
4ac23a
		}
4ac23a
4ac23a
		if (ectx->mout[0] == parg) {
4ac23a
			ectx->mout[0] = &aarg[0];
4ac23a
			ectx->mout[1] = &aarg[1];
4ac23a
		}
4ac23a
4ac23a
		/* placeholder argument? */
4ac23a
		if (!strncmp(*parg,"-USLIBTOOL_PLACEHOLDER_",23)) {
4ac23a
			parg++;
4ac23a
		} else {
4ac23a
			*aarg++ = *parg++;
4ac23a
		}
4ac23a
	}
4ac23a
34988f
	/* program name, ccwrap */
34988f
	if ((ccwrap = (char *)dctx->cctx->ccwrap)) {
34988f
		base[1] = base[0];
34988f
		base[0] = ccwrap;
34988f
		base++;
34988f
	}
4ac23a
4ac23a
	/* join all other args */
4ac23a
	src = aargv;
4ac23a
	cap = aarg;
34988f
	dst = &base[1];
4ac23a
4ac23a
	for (; src
4ac23a
		*dst++ = *src++;
4ac23a
4ac23a
	/* properly null-terminate argv, accounting for redundant arguments */
4ac23a
	*dst = 0;
4ac23a
4ac23a
	/* output annotation */
4ac23a
	if (ectx->lout[0]) {
4ac23a
		ectx->lout[0] = &base[1] + (ectx->lout[0] - aargv);
4ac23a
		ectx->lout[1] = ectx->lout[0] + 1;
4ac23a
	}
4ac23a
4ac23a
	if (ectx->mout[0]) {
4ac23a
		ectx->mout[0] = &base[1] + (ectx->mout[0] - aargv);
4ac23a
		ectx->mout[1] = ectx->mout[0] + 1;
4ac23a
	}
4ac23a
4ac23a
	/* all done */
4ac23a
	if (sargvbuf)
4ac23a
		free(sargvbuf);
4ac23a
4ac23a
	return 0;
4ac23a
}
4ac23a
36de3e
int  slbt_exec_compile(
36de3e
	const struct slbt_driver_ctx *	dctx,
36de3e
	struct slbt_exec_ctx *		ectx)
36de3e
{
b5fcb3
	int				ret;
77e6c6
	char *				fpic;
34988f
	char *				ccwrap;
b5fcb3
	struct slbt_exec_ctx *		actx = 0;
b5fcb3
	const struct slbt_common_ctx *	cctx = dctx->cctx;
36de3e
c4a389
	/* dry run */
b5fcb3
	if (cctx->drvflags & SLBT_DRIVER_DRY_RUN)
c4a389
		return 0;
c4a389
36de3e
	/* context */
36de3e
	if (ectx)
36de3e
		slbt_reset_placeholders(ectx);
36de3e
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
36de3e
		return ret;
36de3e
	else
36de3e
		actx = ectx;
36de3e
d117fc
	/* remove old .lo wrapper */
d117fc
	if (slbt_exec_compile_remove_file(dctx,ectx,ectx->ltobjname))
9149bc
		return SLBT_NESTED_ERROR(dctx);
d117fc
36de3e
	/* .libs directory */
b5fcb3
	if (cctx->drvflags & SLBT_DRIVER_SHARED)
267766
		if (slbt_mkdir(dctx,ectx->ldirname)) {
3895af
			slbt_free_exec_ctx(actx);
9149bc
			return SLBT_SYSTEM_ERROR(dctx);
36de3e
		}
36de3e
27a8e1
	/* compile mode */
34988f
	ccwrap        = (char *)cctx->ccwrap;
34988f
	ectx->program = ccwrap ? ccwrap : ectx->compiler;
27a8e1
	ectx->argv    = ectx->cargv;
27a8e1
77e6c6
	/* -fpic */
66dce2
	switch (cctx->tag) {
66dce2
		case SLBT_TAG_CC:
66dce2
		case SLBT_TAG_CXX:
66dce2
		case SLBT_TAG_F77:
66dce2
			fpic = cctx->settings.picswitch
66dce2
				? cctx->settings.picswitch
66dce2
				: *ectx->fpic;
66dce2
			break;
66dce2
66dce2
		default:
66dce2
			fpic = *ectx->fpic;
66dce2
	}
77e6c6
36de3e
	/* shared library object */
b5fcb3
	if (cctx->drvflags & SLBT_DRIVER_SHARED) {
77e6c6
		if (!(cctx->drvflags & SLBT_DRIVER_ANTI_PIC)) {
36de3e
			*ectx->dpic = "-DPIC";
77e6c6
			*ectx->fpic = fpic;
77e6c6
		}
36de3e
36de3e
		*ectx->lout[0] = "-o";
36de3e
		*ectx->lout[1] = ectx->lobjname;
36de3e
4ac23a
		if (slbt_exec_compile_finalize_argument_vector(dctx,ectx))
4ac23a
			return SLBT_NESTED_ERROR(dctx);
4ac23a
b5fcb3
		if (!(cctx->drvflags & SLBT_DRIVER_SILENT)) {
db30c2
			if (slbt_output_compile(dctx,ectx)) {
db30c2
				slbt_free_exec_ctx(actx);
9149bc
				return SLBT_NESTED_ERROR(dctx);
db30c2
			}
db30c2
		}
db30c2
36de3e
		if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) {
36de3e
			slbt_free_exec_ctx(actx);
9149bc
			return SLBT_SYSTEM_ERROR(dctx);
36de3e
		}
4ac23a
4ac23a
		if (cctx->drvflags & SLBT_DRIVER_STATIC)
4ac23a
			slbt_reset_argvector(ectx);
36de3e
	}
36de3e
36de3e
	/* static archive object */
b5fcb3
	if (cctx->drvflags & SLBT_DRIVER_STATIC) {
36de3e
		slbt_reset_placeholders(ectx);
36de3e
77e6c6
		if (cctx->drvflags & SLBT_DRIVER_PRO_PIC) {
36de3e
			*ectx->dpic = "-DPIC";
77e6c6
			*ectx->fpic = fpic;
77e6c6
		}
36de3e
36de3e
		*ectx->lout[0] = "-o";
55c95a
		*ectx->lout[1] = ectx->aobjname;
36de3e
4ac23a
		if (slbt_exec_compile_finalize_argument_vector(dctx,ectx))
4ac23a
			return SLBT_NESTED_ERROR(dctx);
4ac23a
b5fcb3
		if (!(cctx->drvflags & SLBT_DRIVER_SILENT)) {
db30c2
			if (slbt_output_compile(dctx,ectx)) {
db30c2
				slbt_free_exec_ctx(actx);
9149bc
				return SLBT_NESTED_ERROR(dctx);
db30c2
			}
db30c2
		}
db30c2
36de3e
		if (((ret = slbt_spawn(ectx,true)) < 0) || ectx->exitcode) {
36de3e
			slbt_free_exec_ctx(actx);
9149bc
			return SLBT_SYSTEM_ERROR(dctx);
36de3e
		}
36de3e
	}
36de3e
70f645
	ret = slbt_create_object_wrapper(dctx,ectx);
36de3e
	slbt_free_exec_ctx(actx);
36de3e
9149bc
	return ret ? SLBT_NESTED_ERROR(dctx) : 0;
36de3e
}