orbea / cross / slibtool

Forked from cross/slibtool 3 years ago
Clone

Blame src/logic/slbt_exec_link.c

2bd749
/*******************************************************************/
2bd749
/*  slibtool: a skinny libtool implementation, written in C        */
2bd749
/*  Copyright (C) 2016  Z. Gilboa                                  */
2bd749
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
2bd749
/*******************************************************************/
2bd749
2bd749
#include <string.h>
2bd749
#include <stdbool.h>
2bd749
#include <fcntl.h>
2bd749
#include <errno.h>
2bd749
#include <sys/stat.h>
2bd749
2bd749
#include <slibtool/slibtool.h>
2bd749
#include "slibtool_spawn_impl.h"
2bd749
2bd749
2bd749
int slbt_exec_link(
2bd749
	const struct slbt_driver_ctx *	dctx,
2bd749
	struct slbt_exec_ctx *		ectx)
2bd749
{
2bd749
	int			ret;
2bd749
	int			fdlibs;
2bd749
	char *			dot;
2bd749
	FILE *			fout;
2bd749
	struct slbt_exec_ctx *	actx;
2bd749
2bd749
	/* context */
2bd749
	if (ectx)
2bd749
		actx = 0;
2bd749
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
2bd749
		return ret;
2bd749
	else
2bd749
		actx = ectx;
2bd749
2bd749
	/* .libs directory */
2bd749
	if (dctx->cctx->drvflags & SLBT_DRIVER_SHARED) {
2bd749
		if ((fdlibs = open(ectx->ldirname,O_DIRECTORY)) >= 0)
2bd749
			close(fdlibs);
2bd749
		else if ((errno != ENOENT) || mkdir(ectx->ldirname,0777)) {
2bd749
			slbt_free_exec_ctx(actx);
2bd749
			return -1;
2bd749
		}
2bd749
	}
2bd749
2bd749
	/* no wrapper? */
2bd749
	if (!(dot = strrchr(dctx->cctx->output,'.')) || strcmp(dot,".la")) {
2bd749
		slbt_free_exec_ctx(actx);
2bd749
		return 0;
2bd749
	}
2bd749
2bd749
	/* hey, yo, let's rap it up */
2bd749
	if (!(fout = fopen(ectx->ltobjname,"w"))) {
2bd749
		slbt_free_exec_ctx(actx);
2bd749
		return -1;
2bd749
	}
2bd749
2bd749
	ret = fprintf(fout,
2bd749
		"# slibtool (pre-alphe) generated file\n\n");
2bd749
2bd749
	/* all done */
2bd749
	fclose(fout);
2bd749
	slbt_free_exec_ctx(actx);
2bd749
2bd749
	return (ret > 0) ? 0 : -1;
2bd749
}