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
8b7d50
#include <stdlib.h>
f0d5f3
#include <stdio.h>
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"
50b6ef
#include "slibtool_readlink_impl.h"
6529aa
#include "slibtool_symlink_impl.h"
2bd749
8b7d50
struct slbt_deps_meta {
8b7d50
	char ** altv;
8b7d50
	char *	args;
8b7d50
	int	depscnt;
8b7d50
	int	infolen;
8b7d50
};
8b7d50
5cc3b3
/*******************************************************************/
5cc3b3
/*                                                                 */
5cc3b3
/* -o <ltlib>  switches              input   result                */
5cc3b3
/* ----------  --------------------- -----   ------                */
b07789
/* libfoo.a    [-shared|-static]     bar.lo  libfoo.a              */
5cc3b3
/*                                                                 */
6d048d
/* ar cru libfoo.a bar.o                                           */
5cc3b3
/* ranlib libfoo.a                                                 */
5cc3b3
/*                                                                 */
5cc3b3
/*******************************************************************/
5cc3b3
a0c318
/*******************************************************************/
a0c318
/*                                                                 */
a0c318
/* -o <ltlib>  switches              input   result                */
a0c318
/* ----------  --------------------- -----   ------                */
a0c318
/* libfoo.la   -shared               bar.lo  libfoo.la             */
a0c318
/*                                           .libs/libfoo.a        */
a0c318
/*                                           .libs/libfoo.la (lnk) */
a0c318
/*                                                                 */
a0c318
/* ar cru .libs/libfoo.a .libs/bar.o                               */
a0c318
/* ranlib .libs/libfoo.a                                           */
a0c318
/* (generate libfoo.la)                                            */
a0c318
/* ln -s ../libfoo.la .libs/libfoo.la                              */
f730ac
/*                                                                 */
f730ac
/*******************************************************************/
f730ac
f730ac
/*******************************************************************/
f730ac
/*                                                                 */
f730ac
/* -o <ltlib>  switches              input   result                */
f730ac
/* ----------  --------------------- -----   ------                */
f730ac
/* libfoo.la   -static               bar.lo  libfoo.la             */
f730ac
/*                                           .libs/libfoo.a        */
f730ac
/*                                           .libs/libfoo.la (lnk) */
f730ac
/*                                                                 */
f730ac
/* ar cru .libs/libfoo.a bar.o                                     */
f730ac
/* ranlib .libs/libfoo.a                                           */
f730ac
/* (generate libfoo.la)                                            */
f730ac
/* ln -s ../libfoo.la .libs/libfoo.la                              */
a0c318
/*                                                                 */
a0c318
/*******************************************************************/
a0c318
3be47d
static int slbt_exec_link_exit(
3be47d
	struct slbt_deps_meta *	depsmeta,
3be47d
	int			ret)
3be47d
{
3be47d
	if (depsmeta->altv)
3be47d
		free(depsmeta->altv);
3be47d
3be47d
	if (depsmeta->args)
3be47d
		free(depsmeta->args);
3be47d
3be47d
	return ret;
3be47d
}
3be47d
8b7d50
static int slbt_get_deps_meta(
8b7d50
	char *			libfilename,
8b7d50
	struct slbt_deps_meta *	depsmeta)
8b7d50
{
8b7d50
	int		ret;
8b7d50
	FILE *		fdeps;
8b7d50
	struct stat	st;
8b7d50
	char		depfile[4*PATH_MAX];
8b7d50
	char *		deplibs = depfile;
8b7d50
0f5ca7
	/* -rpath */
0f5ca7
	if ((size_t)snprintf(depfile,sizeof(depfile),"%s.slibtool.rpath",
0f5ca7
				libfilename)
0f5ca7
			>= sizeof(depfile))
0f5ca7
		return -1;
0f5ca7
0f5ca7
	if (!(lstat(depfile,&st))) {
0f5ca7
		/* -Wl,%s */
0f5ca7
		depsmeta->infolen += st.st_size + 4;
0f5ca7
		depsmeta->infolen++;
0f5ca7
	}
0f5ca7
0f5ca7
	/* .deps */
8b7d50
	if ((size_t)snprintf(depfile,sizeof(depfile),"%s.slibtool.deps",
8b7d50
				libfilename)
8b7d50
			>= sizeof(depfile))
8b7d50
		return -1;
8b7d50
8b7d50
	if ((stat(depfile,&st)))
8b7d50
		return -1;
8b7d50
8b7d50
	if (!(fdeps = fopen(depfile,"r")))
8b7d50
		return -1;
8b7d50
8b7d50
	if ((size_t)st.st_size >= sizeof(depfile))
8b7d50
		if (!(deplibs = malloc(st.st_size+1))) {
8b7d50
			fclose(fdeps);
8b7d50
			return -1;
8b7d50
		}
8b7d50
8b7d50
	depsmeta->infolen += st.st_size;
8b7d50
	depsmeta->infolen++;
8b7d50
8b7d50
	while (fscanf(fdeps,"%s\n",deplibs) == 1)
8b7d50
		depsmeta->depscnt++;
8b7d50
085f3a
	if (deplibs != depfile)
085f3a
		free(deplibs);
085f3a
8b7d50
	ret = ferror(fdeps) ? -1 : 0;
8b7d50
	fclose(fdeps);
8b7d50
8b7d50
	return ret;
8b7d50
}
8b7d50
14d60a
static bool slbt_adjust_input_argument(
14d60a
	char *		arg,
14d60a
	const char *	osuffix,
14d60a
	const char *	asuffix,
14d60a
	bool		fpic)
5cc3b3
{
53aa85
	char *	slash;
5cc3b3
	char *	dot;
53aa85
	char	base[PATH_MAX];
5cc3b3
5cc3b3
	if (*arg == '-')
5cc3b3
		return false;
5cc3b3
5cc3b3
	if (!(dot = strrchr(arg,'.')))
5cc3b3
		return false;
5cc3b3
14d60a
	if (strcmp(dot,osuffix))
5cc3b3
		return false;
5cc3b3
5cc3b3
	if (fpic) {
53aa85
		if ((slash = strrchr(arg,'/')))
53aa85
			slash++;
53aa85
		else
53aa85
			slash = arg;
53aa85
53aa85
		if ((size_t)snprintf(base,sizeof(base),"%s",
53aa85
				slash) >= sizeof(base))
53aa85
			return false;
53aa85
53aa85
		sprintf(slash,".libs/%s",base);
53aa85
		dot = strrchr(arg,'.');
5cc3b3
	}
53aa85
14d60a
	strcpy(dot,asuffix);
53aa85
	return true;
5cc3b3
}
5cc3b3
afaba2
static int slbt_adjust_linker_argument(
b3940a
	char *		arg,
b3940a
	bool		fpic,
b3940a
	const char *	dsosuffix,
8b7d50
	const char *	arsuffix,
8b7d50
	struct slbt_deps_meta * depsmeta)
b3940a
{
b3940a
	int	fdlib;
b3940a
	char *	slash;
b3940a
	char *	dot;
b3940a
	char	base[PATH_MAX];
50b6ef
	char	slnk[PATH_MAX];
b3940a
b3940a
	if (*arg == '-')
afaba2
		return 0;
b3940a
b3940a
	if (!(dot = strrchr(arg,'.')))
afaba2
		return 0;
b3940a
b3940a
	if (strcmp(dot,".la"))
afaba2
		return 0;
b3940a
b3940a
	if (fpic) {
b3940a
		if ((slash = strrchr(arg,'/')))
b3940a
			slash++;
b3940a
		else
b3940a
			slash = arg;
b3940a
b3940a
		if ((size_t)snprintf(base,sizeof(base),"%s",
b3940a
				slash) >= sizeof(base))
afaba2
			return 0;
b3940a
b3940a
		sprintf(slash,".libs/%s",base);
b3940a
		dot = strrchr(arg,'.');
b3940a
	}
b3940a
b3940a
	/* shared library dependency? */
b3940a
	if (fpic) {
b3940a
		sprintf(dot,"%s",dsosuffix);
b3940a
50b6ef
		if (!slbt_readlink(arg,slnk,sizeof(slnk))
50b6ef
				&& !(strcmp(slnk,"/dev/null")))
50b6ef
			sprintf(dot,"%s",arsuffix);
50b6ef
		else if ((fdlib = open(arg,O_RDONLY)) >= 0)
b3940a
			close(fdlib);
b3940a
		else
b3940a
			sprintf(dot,"%s",arsuffix);
b3940a
8b7d50
		return slbt_get_deps_meta(arg,depsmeta);
b3940a
	}
b3940a
b3940a
	/* input archive */
b3940a
	sprintf(dot,"%s",arsuffix);
afaba2
	return 0;
b3940a
}
b3940a
2ef5be
static int slbt_exec_link_adjust_argument_vector(
2ef5be
	const struct slbt_driver_ctx *	dctx,
2ef5be
	struct slbt_exec_ctx *		ectx,
3be47d
	struct slbt_deps_meta *		depsmeta,
79c501
	const char *			cwd,
2ef5be
	bool				flibrary)
2ef5be
{
2ef5be
	char ** carg;
2ef5be
	char ** aarg;
2ef5be
	char *	slash;
2ef5be
	char *	mark;
3be47d
	char *	darg;
2ef5be
	char *	dot;
3be47d
	FILE *	fdeps;
3be47d
	char *	dpath;
94ae74
	bool	freqd;
3be47d
	int	argc;
2ef5be
	char	arg[PATH_MAX];
3be47d
	char	lib[PATH_MAX];
0f5ca7
	char	rpathdir[PATH_MAX];
0f5ca7
	char	rpathlnk[PATH_MAX];
2ef5be
	bool	fwholearchive = false;
0f5ca7
	struct	stat st;
2ef5be
3be47d
	for (argc=0,carg=ectx->cargv; *carg; carg++)
3be47d
		argc++;
3be47d
3be47d
	if (!(depsmeta->args = calloc(1,depsmeta->infolen)))
3be47d
		return -1;
3be47d
3be47d
	argc *= 3;
3be47d
	argc += depsmeta->depscnt;
3be47d
3be47d
	if (!(depsmeta->altv = calloc(argc,sizeof(char *))))
3be47d
		return -1;
3be47d
2ef5be
	carg = ectx->cargv;
3be47d
	aarg = depsmeta->altv;
3be47d
	darg = depsmeta->args;
2ef5be
2ef5be
	for (; *carg; ) {
3be47d
		dpath = 0;
94ae74
		freqd = false;
3be47d
2ef5be
		if (!strcmp(*carg,"-Wl,--whole-archive"))
2ef5be
			fwholearchive = true;
2ef5be
		else if (!strcmp(*carg,"-Wl,--no-whole-archive"))
2ef5be
			fwholearchive = false;
2ef5be
2ef5be
2ef5be
df07fb
		/* output annotation */
df07fb
		if (carg == ectx->lout[0]) {
df07fb
			ectx->mout[0] = &aarg[0];
df07fb
			ectx->mout[1] = &aarg[1];
df07fb
		}
df07fb
df07fb
		/* argument translation */
2ef5be
		if (**carg == '-') {
2ef5be
			*aarg++ = *carg++;
2ef5be
2ef5be
		} else if (!(dot = strrchr(*carg,'.'))) {
2ef5be
			*aarg++ = *carg++;
2ef5be
2ef5be
		} else if (!(strcmp(dot,".a"))) {
2ef5be
			if (flibrary && !fwholearchive)
2ef5be
				*aarg++ = "-Wl,--whole-archive";
2ef5be
3be47d
			dpath = lib;
3be47d
			sprintf(lib,"%s.slibtool.deps",*carg);
2ef5be
			*aarg++ = *carg++;
2ef5be
2ef5be
			if (flibrary && !fwholearchive)
2ef5be
				*aarg++ = "-Wl,--no-whole-archive";
2ef5be
2ef5be
		} else if (strcmp(dot,dctx->cctx->settings.dsosuffix)) {
2ef5be
			*aarg++ = *carg++;
2ef5be
867d1e
		} else if (carg == ectx->lout[1]) {
867d1e
			/* ^^^hoppla^^^ */
867d1e
			*aarg++ = *carg++;
2ef5be
		} else {
0f5ca7
			/* -rpath */
0f5ca7
			sprintf(rpathlnk,"%s.slibtool.rpath",*carg);
0f5ca7
0f5ca7
			if (!(lstat(rpathlnk,&st))) {
0f5ca7
				if (slbt_readlink(
0f5ca7
						rpathlnk,\
0f5ca7
						rpathdir,
0f5ca7
						sizeof(rpathdir)))
0f5ca7
					return -1;
0f5ca7
0f5ca7
				sprintf(darg,"-Wl,%s",rpathdir);
0f5ca7
				*aarg++ = "-Wl,-rpath";
0f5ca7
				*aarg++ = darg;
0f5ca7
				darg   += strlen(darg);
0f5ca7
				darg++;
0f5ca7
			}
0f5ca7
3be47d
			dpath = lib;
94ae74
			freqd = true;
3be47d
			sprintf(lib,"%s.slibtool.deps",*carg);
3be47d
2ef5be
			/* account for {'-','L','-','l'} */
2ef5be
			if ((size_t)snprintf(arg,sizeof(arg),"%s",
2ef5be
					*carg) >= (sizeof(arg) - 4))
2ef5be
				return -1;
2ef5be
2ef5be
			if ((slash = strrchr(arg,'/'))) {
2ef5be
				sprintf(*carg,"-L%s",arg);
2ef5be
2ef5be
				mark   = strrchr(*carg,'/');
5b792c
				*mark  = 0;
2ef5be
5ee0d1
				if (ectx->fwrapper) {
5b792c
					*slash = 0;
5ee0d1
5ee0d1
					if (fprintf(ectx->fwrapper,
79c501
							"DL_PATH=\"$DL_PATH$COLON%s/%s\"\n"
5ee0d1
							"COLON=':'\n\n",
79c501
							cwd,arg) < 0)
5ee0d1
						return -1;
5ee0d1
				}
5ee0d1
2ef5be
				*aarg++ = *carg++;
2ef5be
				*aarg++ = ++mark;
2ef5be
2ef5be
				++slash;
2ef5be
				slash += strlen(dctx->cctx->settings.dsoprefix);
2ef5be
2ef5be
				sprintf(mark,"-l%s",slash);
2ef5be
				dot  = strrchr(mark,'.');
5b792c
				*dot = 0;
2ef5be
			} else {
2ef5be
				*aarg++ = *carg++;
2ef5be
			}
2ef5be
		}
3be47d
3be47d
		if (dpath) {
3be47d
			*aarg = darg;
3be47d
94ae74
			if ((fdeps = fopen(dpath,"r"))) {
94ae74
				while (fscanf(fdeps,"%s\n",darg) == 1) {
94ae74
					*aarg++ = darg;
94ae74
					darg   += strlen(darg);
94ae74
					darg++;
94ae74
				}
3be47d
94ae74
				if (ferror(fdeps)) {
94ae74
					free(depsmeta->altv);
94ae74
					free(depsmeta->args);
94ae74
					fclose(fdeps);
94ae74
					return -1;
94ae74
				} else {
94ae74
					fclose(fdeps);
94ae74
				}
94ae74
			} else if (freqd) {
3be47d
				free(depsmeta->altv);
3be47d
				free(depsmeta->args);
3be47d
				return -1;
3be47d
			}
3be47d
		}
2ef5be
	}
2ef5be
2ef5be
	return 0;
2ef5be
}
2ef5be
628cde
static int slbt_exec_link_remove_file(
628cde
	const struct slbt_driver_ctx *	dctx,
628cde
	struct slbt_exec_ctx *		ectx,
628cde
	const char *			target)
628cde
{
628cde
	/* remove target (if any) */
628cde
	if (!(unlink(target)) || (errno == ENOENT))
628cde
		return 0;
628cde
628cde
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
628cde
		strerror(errno);
628cde
628cde
	return -1;
628cde
}
628cde
0913c3
static int slbt_exec_link_create_dep_file(
0913c3
	struct slbt_exec_ctx *	ectx,
0913c3
	char **			altv,
0913c3
	const char *		libfilename)
0913c3
{
0913c3
	char **	parg;
0913c3
	char *	popt;
0913c3
	char *	plib;
0913c3
	char	depfile[PATH_MAX];
0913c3
0913c3
	if (ectx->fdeps)
0913c3
		fclose(ectx->fdeps);
0913c3
0913c3
	if ((size_t)snprintf(depfile,sizeof(depfile),"%s.slibtool.deps",
0913c3
				libfilename)
0913c3
			>= sizeof(depfile))
0913c3
		return -1;
0913c3
0913c3
	if (!(ectx->fdeps = fopen(depfile,"w")))
0913c3
		return -1;
0913c3
0913c3
	for (parg=altv; *parg; parg++) {
0913c3
		popt = 0;
0913c3
		plib = 0;
0913c3
0913c3
		if (!strcmp(*parg,"-l")) {
0913c3
			popt = *parg++;
0913c3
			plib = *parg;
0913c3
		} else if (!strcmp(*parg,"--library")) {
0913c3
			popt = *parg++;
0913c3
			plib = *parg;
0913c3
		} else if (!strncmp(*parg,"-l",2)) {
0913c3
			popt = *parg;
0913c3
			plib = popt + 2;
0913c3
		} else if (!strncmp(*parg,"--library=",10)) {
0913c3
			popt = *parg;
0913c3
			plib = popt + 10;
0913c3
		}
0913c3
0913c3
		if (plib)
0913c3
			if (fprintf(ectx->fdeps,"-l%s\n",plib) < 0)
0913c3
				return -1;
0913c3
	}
0913c3
0913c3
	if (fflush(ectx->fdeps))
0913c3
		return -1;
0913c3
0913c3
	return 0;
0913c3
}
0913c3
9112ac
static int slbt_exec_link_create_import_library(
9112ac
	const struct slbt_driver_ctx *	dctx,
9112ac
	struct slbt_exec_ctx *		ectx,
9112ac
	char *				impfilename,
9112ac
	char *				deffilename,
4fdf35
	char *				soname,
4fdf35
	bool				ftag)
9112ac
{
4fdf35
	char *	slash;
9112ac
	char *	dlltool[8];
9112ac
	char	program[PATH_MAX];
4fdf35
	char	hosttag[PATH_MAX];
4fdf35
	char	hostlnk[PATH_MAX];
4fdf35
4fdf35
	/* libfoo.so.def.{flavor} */
4fdf35
	if (ftag) {
4fdf35
		if ((size_t)snprintf(hosttag,sizeof(hosttag),"%s.%s",
4fdf35
				deffilename,
4fdf35
				dctx->cctx->host.flavor) >= sizeof(hosttag))
4fdf35
			return -1;
4fdf35
4fdf35
		if ((size_t)snprintf(hostlnk,sizeof(hostlnk),"%s.host",
4fdf35
				deffilename) >= sizeof(hostlnk))
4fdf35
			return -1;
4fdf35
4fdf35
		/* libfoo.so.def is under .libs/ */
4fdf35
		if (!(slash = strrchr(deffilename,'/')))
4fdf35
			return -1;
4fdf35
4fdf35
		if (slbt_create_symlink(
4fdf35
				dctx,ectx,
4fdf35
				deffilename,
4fdf35
				hosttag,
4fdf35
				false))
4fdf35
			return -1;
4fdf35
4fdf35
		/* libfoo.so.def.{flavor} is under .libs/ */
4fdf35
		if (!(slash = strrchr(hosttag,'/')))
4fdf35
			return -1;
4fdf35
4fdf35
		if (slbt_create_symlink(
4fdf35
				dctx,ectx,
4fdf35
				++slash,
4fdf35
				hostlnk,
4fdf35
				false))
4fdf35
			return -1;
4fdf35
	}
9112ac
9112ac
	/* dlltool argv */
9112ac
	if ((size_t)snprintf(program,sizeof(program),"%s",
9112ac
			dctx->cctx->host.dlltool) >= sizeof(program))
9112ac
		return -1;
9112ac
9112ac
	dlltool[0] = program;
9112ac
	dlltool[1] = "-l";
9112ac
	dlltool[2] = impfilename;
9112ac
	dlltool[3] = "-d";
9112ac
	dlltool[4] = deffilename;
9112ac
	dlltool[5] = "-D";
9112ac
	dlltool[6] = soname;
9112ac
	dlltool[7] = 0;
9112ac
9112ac
	/* alternate argument vector */
9112ac
	ectx->argv    = dlltool;
9112ac
	ectx->program = program;
9112ac
9112ac
	/* step output */
9112ac
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
9112ac
		if (slbt_output_link(dctx,ectx))
9112ac
			return -1;
9112ac
9112ac
	/* dlltool spawn */
9112ac
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
9112ac
		return -1;
9112ac
9112ac
	return 0;
9112ac
}
9112ac
3b5e3d
static int slbt_exec_link_create_archive(
5cc3b3
	const struct slbt_driver_ctx *	dctx,
a51ace
	struct slbt_exec_ctx *		ectx,
3b5e3d
	const char *			arfilename,
02ae4d
	bool				fpic,
02ae4d
	bool				fprimary)
5cc3b3
{
5cc3b3
	char ** 	aarg;
5cc3b3
	char ** 	parg;
02ae4d
	char *		base;
02ae4d
	char *		mark;
02ae4d
	char *		slash;
5cc3b3
	char *		ranlib[3];
3c594d
	char		program[PATH_MAX];
3c594d
	char		output [PATH_MAX];
02ae4d
	char		arfile [PATH_MAX];
02ae4d
	char		arlink [PATH_MAX];
5cc3b3
e7ddb2
	/* initial state */
e7ddb2
	slbt_reset_arguments(ectx);
e7ddb2
5cc3b3
	/* placeholders */
5cc3b3
	slbt_reset_placeholders(ectx);
5cc3b3
5cc3b3
	/* alternate program (ar, ranlib) */
5cc3b3
	ectx->program = program;
5cc3b3
a51ace
	/* output */
a51ace
	if ((size_t)snprintf(output,sizeof(output),"%s",
a51ace
			arfilename) >= sizeof(output))
a51ace
		return -1;
a51ace
5cc3b3
	/* ar alternate argument vector */
5cc3b3
	if ((size_t)snprintf(program,sizeof(program),"%s",
5cc3b3
			dctx->cctx->host.ar) >= sizeof(program))
5cc3b3
		return -1;
5cc3b3
5cc3b3
	aarg    = ectx->altv;
5cc3b3
	*aarg++ = program;
5cc3b3
	*aarg++ = "cru";
a51ace
	*aarg++ = output;
5cc3b3
5cc3b3
	/* input argument adjustment */
5cc3b3
	for (parg=ectx->cargv; *parg; parg++)
14d60a
		if (slbt_adjust_input_argument(*parg,".lo",".o",fpic))
5cc3b3
			*aarg++ = *parg;
5cc3b3
5cc3b3
	*aarg = 0;
5cc3b3
	ectx->argv = ectx->altv;
5cc3b3
5cc3b3
	/* step output */
5cc3b3
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
5cc3b3
		if (slbt_output_link(dctx,ectx))
5cc3b3
			return -1;
5cc3b3
628cde
	/* remove old archive as needed */
628cde
	if (slbt_exec_link_remove_file(dctx,ectx,output))
628cde
		return -1;
628cde
0913c3
	/* .deps */
0913c3
	if (slbt_exec_link_create_dep_file(ectx,ectx->cargv,arfilename))
0913c3
		return -1;
0913c3
5cc3b3
	/* ar spawn */
5cc3b3
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
5cc3b3
		return -1;
69bc4a
69bc4a
	/* input objects associated with .la archives */
69bc4a
	for (parg=ectx->cargv; *parg; parg++)
69bc4a
		if (slbt_adjust_input_argument(*parg,".la",".a",fpic))
69bc4a
			if (slbt_archive_import(dctx,ectx,output,*parg))
69bc4a
				return -1;
5cc3b3
5cc3b3
	/* ranlib argv */
5cc3b3
	if ((size_t)snprintf(program,sizeof(program),"%s",
5cc3b3
			dctx->cctx->host.ranlib) >= sizeof(program))
5cc3b3
		return -1;
5cc3b3
5cc3b3
	ranlib[0] = program;
a51ace
	ranlib[1] = output;
5cc3b3
	ranlib[2] = 0;
5cc3b3
	ectx->argv = ranlib;
5cc3b3
5cc3b3
	/* step output */
5cc3b3
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
5cc3b3
		if (slbt_output_link(dctx,ectx))
5cc3b3
			return -1;
5cc3b3
5cc3b3
	/* ranlib spawn */
5cc3b3
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
5cc3b3
		return -1;
5cc3b3
02ae4d
	if (fprimary && (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_SHARED)) {
02ae4d
		strcpy(arlink,output);
02ae4d
		mark  = strrchr(arlink,'/');
02ae4d
		*mark = 0;
02ae4d
02ae4d
		base  = output + (mark - arlink);
02ae4d
		base++;
02ae4d
02ae4d
		if ((slash = strrchr(arlink,'/')))
02ae4d
			slash++;
02ae4d
		else
02ae4d
			slash = arlink;
02ae4d
02ae4d
		strcpy(slash,base);
02ae4d
		sprintf(arfile,".libs/%s",base);
02ae4d
02ae4d
		if (slbt_exec_link_remove_file(dctx,ectx,arlink))
02ae4d
			return -1;
02ae4d
02ae4d
		if (symlink(arfile,arlink))
02ae4d
			return -1;
02ae4d
	}
02ae4d
5cc3b3
	return 0;
5cc3b3
}
5cc3b3
b3940a
static int slbt_exec_link_create_library(
b3940a
	const struct slbt_driver_ctx *	dctx,
b3940a
	struct slbt_exec_ctx *		ectx,
112a2b
	const char *			dsofilename,
112a2b
	const char *			relfilename)
b3940a
{
b3940a
	char ** parg;
79c501
	char	cwd    [PATH_MAX];
b3940a
	char	output [PATH_MAX];
0f8591
	char	soname [PATH_MAX];
fbb80b
	char	symfile[PATH_MAX];
bd9cc9
	struct slbt_deps_meta depsmeta = {0,0,0,0};
b3940a
b3940a
	/* initial state */
b3940a
	slbt_reset_arguments(ectx);
b3940a
b3940a
	/* placeholders */
b3940a
	slbt_reset_placeholders(ectx);
b3940a
b3940a
	/* input argument adjustment */
b3940a
	for (parg=ectx->cargv; *parg; parg++)
14d60a
		slbt_adjust_input_argument(*parg,".lo",".o",true);
b3940a
b3940a
	/* linker argument adjustment */
b3940a
	for (parg=ectx->cargv; *parg; parg++)
afaba2
		if (slbt_adjust_linker_argument(
afaba2
				*parg,true,
afaba2
				dctx->cctx->settings.dsosuffix,
8b7d50
				dctx->cctx->settings.arsuffix,
8b7d50
				&depsmeta) < 0)
afaba2
			return -1;
b3940a
b3940a
	/* --no-undefined */
b3940a
	if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
b3940a
		*ectx->noundef = "-Wl,--no-undefined";
b3940a
0f8591
	/* -soname */
b1778b
	if ((dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_MACHO)) {
b1778b
		(void)0;
b1778b
	} else if (!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION)) {
867d1e
		if ((size_t)snprintf(soname,sizeof(soname),"-Wl,%s%s%s.%d",
867d1e
					dctx->cctx->settings.dsoprefix,
867d1e
					dctx->cctx->libname,
867d1e
					dctx->cctx->settings.dsosuffix,
867d1e
					dctx->cctx->verinfo.major)
867d1e
				>= sizeof(soname))
867d1e
			return -1;
0f8591
867d1e
		*ectx->soname  = "-Wl,-soname";
867d1e
		*ectx->lsoname = soname;
112a2b
	} else if (relfilename) {
112a2b
		if ((size_t)snprintf(soname,sizeof(soname),"-Wl,%s%s-%s%s",
112a2b
					dctx->cctx->settings.dsoprefix,
112a2b
					dctx->cctx->libname,
112a2b
					dctx->cctx->release,
112a2b
					dctx->cctx->settings.dsosuffix)
112a2b
				>= sizeof(soname))
112a2b
			return -1;
112a2b
112a2b
		*ectx->soname  = "-Wl,-soname";
112a2b
		*ectx->lsoname = soname;
867d1e
	}
0f8591
fbb80b
	/* PE: --output-def */
fbb80b
	if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
fbb80b
		if ((size_t)snprintf(symfile,sizeof(symfile),"-Wl,%s",
fbb80b
					ectx->deffilename)
fbb80b
				>= sizeof(output))
fbb80b
			return -1;
fbb80b
fbb80b
		*ectx->symdefs = "-Wl,--output-def";
fbb80b
		*ectx->symfile = symfile;
fbb80b
	}
fbb80b
372423
	/* shared/static */
372423
	if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
372423
		*ectx->dpic = "-static";
372423
	} else {
372423
		*ectx->dpic = "-shared";
372423
		*ectx->fpic = "-fPIC";
372423
	}
b3940a
b3940a
	/* output */
112a2b
	if (relfilename) {
112a2b
		strcpy(output,relfilename);
112a2b
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
867d1e
		strcpy(output,dsofilename);
867d1e
	} else {
867d1e
		if ((size_t)snprintf(output,sizeof(output),"%s.%d.%d.%d",
867d1e
					dsofilename,
867d1e
					dctx->cctx->verinfo.major,
867d1e
					dctx->cctx->verinfo.minor,
867d1e
					dctx->cctx->verinfo.revision)
867d1e
				>= sizeof(output))
867d1e
			return -1;
867d1e
	}
b3940a
b3940a
	*ectx->lout[0] = "-o";
b3940a
	*ectx->lout[1] = output;
b3940a
0f5ca7
	/* ldrpath */
0f5ca7
	if (dctx->cctx->host.ldrpath) {
0f5ca7
		if (slbt_exec_link_remove_file(dctx,ectx,ectx->rpathfilename))
0f5ca7
			return -1;
0f5ca7
0f5ca7
		if (symlink(dctx->cctx->host.ldrpath,ectx->rpathfilename))
0f5ca7
			return -1;
0f5ca7
	}
0f5ca7
79c501
	/* cwd */
79c501
	if (!getcwd(cwd,sizeof(cwd)))
79c501
		return -1;
79c501
2ef5be
	/* .libs/libfoo.so --> -L.libs -lfoo */
2ef5be
	if (slbt_exec_link_adjust_argument_vector(
3be47d
			dctx,ectx,&depsmeta,cwd,true))
2ef5be
		return -1;
2ef5be
2ef5be
	/* using alternate argument vector */
3be47d
	ectx->argv    = depsmeta.altv;
3be47d
	ectx->program = depsmeta.altv[0];
2ef5be
b3940a
	/* step output */
b3940a
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
b3940a
		if (slbt_output_link(dctx,ectx))
3be47d
			return slbt_exec_link_exit(&depsmeta,-1);
b3940a
0913c3
	/* .deps */
0913c3
	if (slbt_exec_link_create_dep_file(ectx,ectx->argv,dsofilename))
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
0913c3
b3940a
	/* spawn */
b3940a
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
b3940a
3be47d
	return slbt_exec_link_exit(&depsmeta,0);
b3940a
}
b3940a
f6ccbe
static int slbt_exec_link_create_executable(
f6ccbe
	const struct slbt_driver_ctx *	dctx,
f6ccbe
	struct slbt_exec_ctx *		ectx,
f6ccbe
	const char *			exefilename)
f6ccbe
{
f6ccbe
	char ** parg;
e9dbdf
	char *	base;
5ee0d1
	char	cwd    [PATH_MAX];
f6ccbe
	char	output [PATH_MAX];
5ee0d1
	char	wrapper[PATH_MAX];
5e5804
	char	wraplnk[PATH_MAX];
238670
	bool	fabspath;
372423
	bool	fpic;
dac9cd
	const struct slbt_source_version * verinfo;
bd9cc9
	struct slbt_deps_meta depsmeta = {0,0,0,0};
f6ccbe
f6ccbe
	/* initial state */
f6ccbe
	slbt_reset_arguments(ectx);
f6ccbe
f6ccbe
	/* placeholders */
f6ccbe
	slbt_reset_placeholders(ectx);
f6ccbe
372423
	/* fpic */
372423
	fpic = !(dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC);
372423
f6ccbe
	/* input argument adjustment */
f6ccbe
	for (parg=ectx->cargv; *parg; parg++)
372423
		slbt_adjust_input_argument(*parg,".lo",".o",fpic);
f6ccbe
f6ccbe
	/* linker argument adjustment */
f6ccbe
	for (parg=ectx->cargv; *parg; parg++)
afaba2
		if (slbt_adjust_linker_argument(
afaba2
				*parg,true,
afaba2
				dctx->cctx->settings.dsosuffix,
8b7d50
				dctx->cctx->settings.arsuffix,
8b7d50
				&depsmeta) < 0)
afaba2
			return -1;
f6ccbe
f6ccbe
	/* --no-undefined */
f6ccbe
	if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
f6ccbe
		*ectx->noundef = "-Wl,--no-undefined";
f6ccbe
5ee0d1
	/* executable wrapper: header */
5ee0d1
	if ((size_t)snprintf(wrapper,sizeof(wrapper),"%s.wrapper.tmp",
5ee0d1
				dctx->cctx->output)
5ee0d1
			>= sizeof(wrapper))
5ee0d1
		return -1;
5ee0d1
5ee0d1
	if (!(ectx->fwrapper = fopen(wrapper,"w")))
5ee0d1
		return -1;
5ee0d1
dac9cd
	verinfo = slbt_source_version();
dac9cd
5ee0d1
	if (fprintf(ectx->fwrapper,
5ee0d1
			"#!/bin/sh\n"
821d55
			"# libtool compatible executable wrapper\n"
dac9cd
			"# Generated by %s (slibtool %d.%d.%d)\n"
dac9cd
			"# [commit reference: %s]\n\n"
1f87fd
5ee0d1
			"if [ -z \"$%s\" ]; then\n"
5ee0d1
			"\tDL_PATH=\n"
5ee0d1
			"\tCOLON=\n"
5ee0d1
			"\tLCOLON=\n"
5ee0d1
			"else\n"
5ee0d1
			"\tDL_PATH=\n"
5ee0d1
			"\tCOLON=\n"
5ee0d1
			"\tLCOLON=':'\n"
5ee0d1
			"fi\n\n",
1f87fd
1f87fd
			dctx->program,
dac9cd
			verinfo->major,verinfo->minor,verinfo->revision,
dac9cd
			verinfo->commit,
5ee0d1
			dctx->cctx->settings.ldpathenv) < 0)
5ee0d1
		return -1;
5ee0d1
f6ccbe
	/* output */
f6ccbe
	if ((size_t)snprintf(output,sizeof(output),"%s",
f6ccbe
				exefilename)
f6ccbe
			>= sizeof(output))
f6ccbe
		return -1;
f6ccbe
f6ccbe
	*ectx->lout[0] = "-o";
f6ccbe
	*ectx->lout[1] = output;
f6ccbe
79c501
	/* cwd */
79c501
	if (!getcwd(cwd,sizeof(cwd)))
79c501
		return -1;
79c501
2ef5be
	/* .libs/libfoo.so --> -L.libs -lfoo */
2ef5be
	if (slbt_exec_link_adjust_argument_vector(
3be47d
			dctx,ectx,&depsmeta,cwd,false))
2ef5be
		return -1;
2ef5be
2ef5be
	/* using alternate argument vector */
3be47d
	ectx->argv    = depsmeta.altv;
3be47d
	ectx->program = depsmeta.altv[0];
2ef5be
e9dbdf
	/* executable wrapper: base name */
e9dbdf
	if ((base = strrchr(dctx->cctx->output,'/')))
e9dbdf
		base++;
e9dbdf
	else
e9dbdf
		base = wrapper;
e9dbdf
5ee0d1
	/* executable wrapper: footer */
238670
	fabspath = (exefilename[0] == '/');
238670
5ee0d1
	if (fprintf(ectx->fwrapper,
5ee0d1
			"DL_PATH=\"$DL_PATH$LCOLON$%s\"\n\n"
5ee0d1
			"export %s=$DL_PATH\n\n"
e9dbdf
			"if [ `basename \"$0\"` = \"%s.exe.wrapper\" ]; then\n"
e9dbdf
			"\tprogram=\"$1\"; shift\n"
e9dbdf
			"\texec \"$program\" \"$@\"\n"
e9dbdf
			"fi\n\n"
5ee0d1
			"exec %s/%s \"$@\"\n",
5ee0d1
			dctx->cctx->settings.ldpathenv,
5ee0d1
			dctx->cctx->settings.ldpathenv,
e9dbdf
			base,
238670
			fabspath ? "" : cwd,
238670
			fabspath ? &exefilename[1] : exefilename) < 0)
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
5ee0d1
f6ccbe
	/* step output */
f6ccbe
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
f6ccbe
		if (slbt_output_link(dctx,ectx))
3be47d
			return slbt_exec_link_exit(&depsmeta,-1);
f6ccbe
f6ccbe
	/* spawn */
f6ccbe
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
f6ccbe
5ee0d1
	/* executable wrapper: finalize */
5ee0d1
	fclose(ectx->fwrapper);
5ee0d1
	ectx->fwrapper = 0;
5ee0d1
5e5804
	if ((size_t)snprintf(wraplnk,sizeof(wraplnk),"%s.exe.wrapper",
5e5804
			dctx->cctx->output) >= sizeof(wraplnk))
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
5e5804
5e5804
	if (slbt_create_symlink(
5e5804
			dctx,ectx,
5e5804
			dctx->cctx->output,wraplnk,
5e5804
			false))
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
5e5804
5ee0d1
	if (rename(wrapper,dctx->cctx->output))
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
5ee0d1
5ee0d1
	if (chmod(dctx->cctx->output,0755))
3be47d
		return slbt_exec_link_exit(&depsmeta,-1);
5ee0d1
3be47d
	return slbt_exec_link_exit(&depsmeta,0);
f6ccbe
}
f6ccbe
b3940a
static int slbt_exec_link_create_library_symlink(
b3940a
	const struct slbt_driver_ctx *	dctx,
b3940a
	struct slbt_exec_ctx *		ectx,
b3940a
	bool				fmajor)
b3940a
{
b3940a
	char	target[PATH_MAX];
b3940a
	char	lnkname[PATH_MAX];
b3940a
112a2b
	if (ectx->relfilename) {
112a2b
		strcpy(target,ectx->relfilename);
112a2b
		sprintf(lnkname,"%s.release",ectx->dsofilename);
112a2b
112a2b
		if (slbt_create_symlink(
112a2b
				dctx,ectx,
112a2b
				target,lnkname,
112a2b
				false))
112a2b
			return -1;
112a2b
	} else
112a2b
		sprintf(target,"%s.%d.%d.%d",
112a2b
			ectx->dsofilename,
112a2b
			dctx->cctx->verinfo.major,
112a2b
			dctx->cctx->verinfo.minor,
112a2b
			dctx->cctx->verinfo.revision);
b3940a
b3940a
	if (fmajor)
b3940a
		sprintf(lnkname,"%s.%d",
b3940a
			ectx->dsofilename,
b3940a
			dctx->cctx->verinfo.major);
b3940a
b3940a
	else
b3940a
		strcpy(lnkname,ectx->dsofilename);
b3940a
dbd229
	if (fmajor && (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE))
dbd229
		return slbt_copy_file(
dbd229
			dctx,ectx,
dbd229
			target,lnkname);
dbd229
	else
dbd229
		return slbt_create_symlink(
dbd229
			dctx,ectx,
dbd229
			target,lnkname,
dbd229
			false);
b3940a
}
b3940a
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;
a51ace
	const char *		output;
2bd749
	char *			dot;
2bd749
	FILE *			fout;
2bd749
	struct slbt_exec_ctx *	actx;
372423
	bool			fpic;
372423
	bool			fstaticonly;
4469ed
	bool			fnover;
4469ed
	bool			fvernum;
4469ed
	int			current;
4469ed
	int			revision;
4469ed
	int			age;
9112ac
	char			soname[PATH_MAX];
4469ed
	char			soxyz [PATH_MAX];
4469ed
	char			solnk [PATH_MAX];
4469ed
	char			arname[PATH_MAX];
dac9cd
	const struct slbt_source_version * verinfo;
4469ed
4469ed
	/* libfoo.so.x.y.z */
4469ed
	if ((size_t)snprintf(soxyz,sizeof(soxyz),"%s%s%s.%d.%d.%d",
4469ed
				dctx->cctx->settings.dsoprefix,
4469ed
				dctx->cctx->libname,
4469ed
				dctx->cctx->settings.dsosuffix,
4469ed
				dctx->cctx->verinfo.major,
4469ed
				dctx->cctx->verinfo.minor,
4469ed
				dctx->cctx->verinfo.revision)
4469ed
			>= sizeof(soxyz))
4469ed
		return -1;
4469ed
4469ed
	/* libfoo.so.x */
4469ed
	sprintf(soname,"%s%s%s.%d",
4469ed
		dctx->cctx->settings.dsoprefix,
4469ed
		dctx->cctx->libname,
4469ed
		dctx->cctx->settings.dsosuffix,
4469ed
		dctx->cctx->verinfo.major);
4469ed
4469ed
	/* libfoo.so */
4469ed
	sprintf(solnk,"%s%s%s",
a71530
		(dctx->cctx->drvflags & SLBT_DRIVER_MODULE)
a71530
			? ""
a71530
			: dctx->cctx->settings.dsoprefix,
4469ed
		dctx->cctx->libname,
4469ed
		dctx->cctx->settings.dsosuffix);
4469ed
4469ed
	/* libfoo.a */
4469ed
	sprintf(arname,"%s%s%s",
4469ed
		dctx->cctx->settings.arprefix,
4469ed
		dctx->cctx->libname,
4469ed
		dctx->cctx->settings.arsuffix);
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
5cc3b3
	/* output suffix */
a51ace
	output = dctx->cctx->output;
a51ace
	dot    = strrchr(output,'.');
5cc3b3
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
5cc3b3
	/* non-pic libfoo.a */
5cc3b3
	if (dot && !strcmp(dot,".a"))
02ae4d
		if (slbt_exec_link_create_archive(dctx,ectx,output,false,false)) {
5cc3b3
			slbt_free_exec_ctx(actx);
5cc3b3
			return -1;
5cc3b3
		}
5cc3b3
372423
	/* fpic, fstaticonly */
372423
	if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
372423
		fstaticonly = true;
372423
		fpic        = false;
f782a1
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_SHARED) {
f782a1
		fstaticonly = true;
f782a1
		fpic        = false;
372423
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_SHARED) {
372423
		fstaticonly = false;
372423
		fpic        = true;
372423
	} else {
372423
		fstaticonly = false;
372423
		fpic        = false;
372423
	}
372423
a0c318
	/* pic libfoo.a */
a0c318
	if (dot && !strcmp(dot,".la"))
a0c318
		if (slbt_exec_link_create_archive(
a0c318
				dctx,ectx,
a0c318
				ectx->arfilename,
02ae4d
				fpic,true)) {
a0c318
			slbt_free_exec_ctx(actx);
a0c318
			return -1;
a0c318
		}
a0c318
372423
	/* -all-static library */
372423
	if (fstaticonly && dctx->cctx->libname)
372423
		if (slbt_create_symlink(
372423
				dctx,ectx,
372423
				"/dev/null",
372423
				ectx->dsofilename,
372423
				false))
372423
			return -1;
372423
b3940a
	/* dynamic library */
372423
	if (dot && !strcmp(dot,".la") && dctx->cctx->rpath && !fstaticonly) {
b3940a
		/* linking: libfoo.so.x.y.z */
b3940a
		if (slbt_exec_link_create_library(
b3940a
				dctx,ectx,
112a2b
				ectx->dsofilename,
112a2b
				ectx->relfilename)) {
b3940a
			slbt_free_exec_ctx(actx);
b3940a
			return -1;
b3940a
		}
b3940a
867d1e
		if (!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION)) {
867d1e
			/* symlink: libfoo.so.x --> libfoo.so.x.y.z */
867d1e
			if (slbt_exec_link_create_library_symlink(
867d1e
					dctx,ectx,
867d1e
					true)) {
867d1e
				slbt_free_exec_ctx(actx);
867d1e
				return -1;
867d1e
			}
b3940a
867d1e
			/* symlink: libfoo.so --> libfoo.so.x.y.z */
867d1e
			if (slbt_exec_link_create_library_symlink(
867d1e
					dctx,ectx,
867d1e
					false)) {
867d1e
				slbt_free_exec_ctx(actx);
867d1e
				return -1;
867d1e
			}
112a2b
		} else if (ectx->relfilename) {
112a2b
			/* symlink: libfoo.so --> libfoo-x.y.z.so */
112a2b
			if (slbt_exec_link_create_library_symlink(
112a2b
					dctx,ectx,
112a2b
					false)) {
112a2b
				slbt_free_exec_ctx(actx);
112a2b
				return -1;
112a2b
			}
b3940a
		}
9112ac
9112ac
		/* PE import libraries */
9112ac
		if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
ff4b97
			/* libfoo.x.lib.a */
9112ac
			if (slbt_exec_link_create_import_library(
9112ac
					dctx,ectx,
ff4b97
					ectx->pimpfilename,
9112ac
					ectx->deffilename,
4fdf35
					soname,
4fdf35
					true))
9112ac
				return -1;
9112ac
ff4b97
			/* symlink: libfoo.lib.a --> libfoo.x.lib.a */
ff4b97
			if (slbt_create_symlink(
9112ac
					dctx,ectx,
9112ac
					ectx->pimpfilename,
ff4b97
					ectx->dimpfilename,
ff4b97
					false))
9112ac
				return -1;
9112ac
9112ac
			/* libfoo.x.y.z.lib.a */
9112ac
			if (slbt_exec_link_create_import_library(
9112ac
					dctx,ectx,
9112ac
					ectx->vimpfilename,
9112ac
					ectx->deffilename,
4469ed
					soxyz,
4fdf35
					false))
9112ac
				return -1;
9112ac
		}
b3940a
	}
b3940a
f6ccbe
	/* executable */
f6ccbe
	if (!dctx->cctx->rpath && !dctx->cctx->libname) {
f6ccbe
		/* linking: .libs/exefilename */
f6ccbe
		if (slbt_exec_link_create_executable(
f6ccbe
				dctx,ectx,
f6ccbe
				ectx->exefilename)) {
f6ccbe
			slbt_free_exec_ctx(actx);
f6ccbe
			return -1;
f6ccbe
		}
f6ccbe
	}
f6ccbe
2bd749
	/* no wrapper? */
5cc3b3
	if (!dot || strcmp(dot,".la")) {
2bd749
		slbt_free_exec_ctx(actx);
2bd749
		return 0;
2bd749
	}
2bd749
2bd749
	/* hey, yo, let's rap it up */
687567
	if (!(fout = fopen(output,"w"))) {
2bd749
		slbt_free_exec_ctx(actx);
2bd749
		return -1;
2bd749
	}
2bd749
4469ed
	/* compatible library wrapper */
4469ed
	current  = 0;
4469ed
	age      = 0;
4469ed
	revision = 0;
4469ed
4469ed
	if (dctx->cctx->verinfo.verinfo)
4469ed
		sscanf(dctx->cctx->verinfo.verinfo,"%d:%d:%d",
4469ed
			&current,&revision,&age;;
4469ed
4469ed
	fnover  = !!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION);
4469ed
	fvernum = !!(dctx->cctx->verinfo.vernumber);
dac9cd
	verinfo = slbt_source_version();
4469ed
4469ed
	ret = fprintf(fout,
4469ed
		"# libtool compatible library wrapper\n"
dac9cd
		"# Generated by %s (slibtool %d.%d.%d)\n"
dac9cd
		"# [commit reference: %s]\n\n"
4469ed
4469ed
		"dlname='%s'\n"
4469ed
		"library_names='%s %s %s'\n"
4469ed
		"old_library='%s'\n\n"
4469ed
4469ed
		"inherited_linker_flags='%s'\n"
4469ed
		"dependency_libs='%s'\n"
4469ed
		"weak_library_names='%s'\n\n"
4469ed
4469ed
		"current=%d\n"
4469ed
		"age=%d\n"
4469ed
		"revision=%d\n\n"
4469ed
4469ed
		"installed=%s\n"
4469ed
		"shouldnotlink=%s\n\n"
4469ed
4469ed
		"dlopen='%s'\n"
4469ed
		"dlpreopen='%s'\n\n"
4469ed
4469ed
		"libdir='%s'\n",
4469ed
dac9cd
		/* nickname, verinfo */
1f87fd
		dctx->program,
dac9cd
		verinfo->major,verinfo->minor,verinfo->revision,
dac9cd
		verinfo->commit,
1f87fd
4469ed
		/* dlname */
4469ed
		fnover ? solnk : soxyz,
4469ed
4469ed
		/* library_names */
4469ed
		fnover ? solnk : soxyz,
4469ed
		fnover ? solnk : soname,
4469ed
		solnk,
4469ed
4469ed
		/* old_library */
4469ed
		arname,
4469ed
4469ed
		/* inherited_linker_flags, dependency_libs, weak_library_names */
4469ed
		"","","",
4469ed
4469ed
		/* current, age, revision */
4469ed
		fvernum ? dctx->cctx->verinfo.major : current,
4469ed
		fvernum ? dctx->cctx->verinfo.minor : age,
4469ed
		fvernum ? dctx->cctx->verinfo.major : revision,
4469ed
4469ed
		/* installed, shouldnotlink */
4469ed
		"no","no",
4469ed
4469ed
		/* dlopen, dlpreopen */
4469ed
		"","",
4469ed
4469ed
		/* libdir */
4469ed
		dctx->cctx->rpath);
4469ed
4c76f7
	/* wrapper symlink */
4469ed
	if (ret > 0)
4469ed
		ret = (slbt_create_symlink(
4469ed
				dctx,ectx,
4469ed
				output,
4469ed
				ectx->lafilename,
4469ed
				true));
4c76f7
26ea30
	/* .lai wrapper symlink */
26ea30
	if (ret == 0)
26ea30
		ret = (slbt_create_symlink(
26ea30
				dctx,ectx,
26ea30
				output,
26ea30
				ectx->laifilename,
26ea30
				true));
26ea30
2bd749
	/* all done */
2bd749
	fclose(fout);
2bd749
	slbt_free_exec_ctx(actx);
2bd749
4469ed
	return ret;
2bd749
}