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
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;
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)
3895af
		if (slbt_mkdir(ectx->ldirname)) {
3895af
			slbt_free_exec_ctx(actx);
9149bc
			return SLBT_SYSTEM_ERROR(dctx);
36de3e
		}
36de3e
27a8e1
	/* compile mode */
27a8e1
	ectx->program = ectx->compiler;
27a8e1
	ectx->argv    = ectx->cargv;
27a8e1
77e6c6
	/* -fpic */
77e6c6
	fpic = *ectx->fpic;
77e6c6
77e6c6
	if ((cctx->tag == SLBT_TAG_CC) || (cctx->tag == SLBT_TAG_CXX))
77e6c6
		if (cctx->settings.picswitch)
77e6c6
			fpic = cctx->settings.picswitch;
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
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
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
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
}