Blame src/logic/linkcmd/slbt_linkcmd_deps.c

b08054
/*******************************************************************/
b08054
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
b08054
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
b08054
/*******************************************************************/
b08054
b08054
#include <stdlib.h>
b08054
#include <stdio.h>
b08054
#include <string.h>
b08054
#include <fcntl.h>
b08054
#include <errno.h>
b08054
#include <sys/stat.h>
b08054
b08054
#include <slibtool/slibtool.h>
b08054
#include "slibtool_driver_impl.h"
b08054
#include "slibtool_errinfo_impl.h"
b08054
#include "slibtool_linkcmd_impl.h"
b08054
#include "slibtool_mapfile_impl.h"
b08054
#include "slibtool_metafile_impl.h"
b08054
#include "slibtool_snprintf_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
b08054
4b56de
slbt_hidden int slbt_get_deps_meta(
b08054
	const struct slbt_driver_ctx *	dctx,
b08054
	char *				libfilename,
b08054
	int				fexternal,
b08054
	struct slbt_deps_meta *		depsmeta)
b08054
{
b08054
	int			fdcwd;
b08054
	char *			ch;
b08054
	char *			cap;
b08054
	char *			base;
b08054
	size_t			libexlen;
b08054
	struct stat		st;
b08054
	struct slbt_map_info *	mapinfo;
b08054
	char			depfile[PATH_MAX];
b08054
b08054
	/* fdcwd */
b08054
	fdcwd = slbt_driver_fdcwd(dctx);
b08054
b08054
	/* -rpath */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
b08054
				"%s.slibtool.rpath",
b08054
				libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
b08054
	/* -Wl,%s */
b08054
	if (!fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
b08054
		depsmeta->infolen += st.st_size + 4;
b08054
		depsmeta->infolen++;
b08054
	}
b08054
b08054
	/* .deps */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
b08054
				"%s.slibtool.deps",
b08054
				libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
b08054
	/* mapinfo */
b08054
	if (!(mapinfo = slbt_map_file(fdcwd,depfile,SLBT_MAP_INPUT)))
b08054
		return (fexternal && (errno == ENOENT))
b08054
			? 0 : SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
b08054
	/* copied length */
b08054
	depsmeta->infolen += mapinfo->size;
b08054
	depsmeta->infolen++;
b08054
b08054
	/* libexlen */
b08054
	libexlen = (base = strrchr(libfilename,'/'))
b08054
		? strlen(depfile) + 2 + (base - libfilename)
b08054
		: strlen(depfile) + 2;
b08054
b08054
	/* iterate */
b08054
	ch  = mapinfo->addr;
b08054
	cap = mapinfo->cap;
b08054
b08054
	for (; ch
b08054
		if (*ch++ == '\n') {
b08054
			depsmeta->infolen += libexlen;
b08054
			depsmeta->depscnt++;
b08054
		}
b08054
	}
b08054
b08054
	slbt_unmap_file(mapinfo);
b08054
b08054
	return 0;
b08054
}
b08054
b08054
4b56de
slbt_hidden int slbt_exec_link_create_dep_file(
b08054
	const struct slbt_driver_ctx *	dctx,
b08054
	struct slbt_exec_ctx *		ectx,
b08054
	char **				altv,
b08054
	const char *			libfilename,
b08054
	bool				farchive)
b08054
{
b08054
	int			ret;
b08054
	int			deps;
b08054
	int			slen;
b08054
	int			fdcwd;
b08054
	char **			parg;
b08054
	char *			popt;
b08054
	char *			plib;
b08054
	char *			path;
b08054
	char *			mark;
b08054
	char *			base;
b08054
	size_t			size;
b08054
	char			deplib [PATH_MAX];
b08054
	bool			is_reladir;
b08054
	char			reladir[PATH_MAX];
b08054
	char			depfile[PATH_MAX];
b08054
	struct stat		st;
b08054
	int			ldepth;
b08054
	int			fdyndep;
b08054
	struct slbt_map_info *  mapinfo;
b08054
b08054
	/* fdcwd */
b08054
	fdcwd = slbt_driver_fdcwd(dctx);
b08054
b08054
	/* depfile */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
b08054
			"%s.slibtool.deps",
b08054
			libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
b08054
	/* deps */
b08054
	if ((deps = openat(fdcwd,depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
b08054
		return SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
b08054
	/* iterate */
b08054
	for (parg=altv; *parg; parg++) {
b08054
		popt    = 0;
b08054
		plib    = 0;
b08054
		path    = 0;
b08054
		mapinfo = 0;
b08054
b08054
		if (!strncmp(*parg,"-l",2)) {
b08054
			popt = *parg;
b08054
			plib = popt + 2;
b08054
b08054
		} else if (!strncmp(*parg,"-L",2)) {
b08054
			popt = *parg;
b08054
			path = popt + 2;
b08054
b08054
		} else if (!strncmp(*parg,"-f",2)) {
b08054
			(void)0;
b08054
b08054
		} else if ((popt = strrchr(*parg,'.')) && !strcmp(popt,".la")) {
b08054
			/* import dependency list */
b08054
			if ((base = strrchr(*parg,'/')))
b08054
				base++;
b08054
			else
b08054
				base = *parg;
b08054
b08054
			/* [relative .la directory] */
b08054
			if (base > *parg) {
b08054
				slen = slbt_snprintf(
b08054
					reladir,
b08054
					sizeof(reladir),
b08054
					"%s",*parg);
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				is_reladir = true;
b08054
				reladir[base - *parg - 1] = 0;
b08054
			} else {
b08054
				is_reladir = false;
b08054
				reladir[0] = '.';
b08054
				reladir[1] = 0;
b08054
			}
b08054
b08054
b08054
			/* dynamic library dependency? */
b08054
			strcpy(depfile,*parg);
b08054
			mark = depfile + (base - *parg);
b08054
			size = sizeof(depfile) - (base - *parg);
b08054
			slen = slbt_snprintf(mark,size,".libs/%s",base);
b08054
b08054
			if (slen < 0) {
b08054
				close(deps);
b08054
				return SLBT_BUFFER_ERROR(dctx);
b08054
			}
b08054
b08054
			mark = strrchr(mark,'.');
b08054
			strcpy(mark,dctx->cctx->settings.dsosuffix);
b08054
b08054
			fdyndep = !fstatat(fdcwd,depfile,&st,0);
b08054
b08054
			/* [-L... as needed] */
b08054
			if (fdyndep && (ectx->ldirdepth >= 0)) {
b08054
				if (slbt_dprintf(deps,"-L") < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
b08054
				for (ldepth=ectx->ldirdepth; ldepth; ldepth--) {
b08054
					if (slbt_dprintf(deps,"../") < 0) {
b08054
						close(deps);
b08054
						return SLBT_SYSTEM_ERROR(dctx,0);
b08054
					}
b08054
				}
b08054
b08054
				if (slbt_dprintf(deps,"%s%s.libs\n",
b08054
						 (is_reladir ? reladir : ""),
b08054
						 (is_reladir ? "/" : "")) < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			/* -ldeplib */
b08054
			if (fdyndep) {
b08054
				*popt = 0;
b08054
				mark  = base;
b08054
				mark += strlen(dctx->cctx->settings.dsoprefix);
b08054
b08054
				if (slbt_dprintf(deps,"-l%s\n",mark) < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
b08054
				*popt = '.';
b08054
			}
b08054
b08054
			/* [open dependency list] */
b08054
			strcpy(depfile,*parg);
b08054
			mark = depfile + (base - *parg);
b08054
			size = sizeof(depfile) - (base - *parg);
b08054
			slen = slbt_snprintf(mark,size,".libs/%s",base);
b08054
b08054
			if (slen < 0) {
b08054
				close(deps);
b08054
				return SLBT_BUFFER_ERROR(dctx);
b08054
			}
b08054
b08054
			mapinfo = 0;
b08054
b08054
			mark = strrchr(mark,'.');
b08054
			size = sizeof(depfile) - (mark - depfile);
b08054
b08054
			if (!farchive) {
b08054
				slen = slbt_snprintf(mark,size,
b08054
					"%s.slibtool.deps",
b08054
					dctx->cctx->settings.dsosuffix);
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				mapinfo = slbt_map_file(
b08054
					fdcwd,depfile,
b08054
					SLBT_MAP_INPUT);
b08054
b08054
				if (!mapinfo && (errno != ENOENT)) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			if (!mapinfo) {
b08054
				slen = slbt_snprintf(mark,size,
b08054
					".a.slibtool.deps");
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				mapinfo = slbt_map_file(
b08054
					fdcwd,depfile,
b08054
					SLBT_MAP_INPUT);
b08054
b08054
				if (!mapinfo) {
b08054
					strcpy(mark,".a.disabled");
b08054
b08054
					if (fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
b08054
						close(deps);
b08054
						return SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
					}
b08054
				}
b08054
			}
b08054
b08054
			/* [-l... as needed] */
b08054
			while (mapinfo && (mapinfo->mark < mapinfo->cap)) {
b08054
				ret = slbt_mapped_readline(
b08054
					dctx,mapinfo,
b08054
					deplib,sizeof(deplib));
b08054
b08054
				if (ret) {
b08054
					close(deps);
b08054
					return SLBT_NESTED_ERROR(dctx);
b08054
				}
b08054
b08054
				ret = ((deplib[0] == '-')
b08054
						&& (deplib[1] == 'L')
b08054
						&& (deplib[2] != '/'))
b08054
					? slbt_dprintf(
b08054
						deps,"-L%s/%s",
b08054
						reladir,&deplib[2])
b08054
					: slbt_dprintf(
b08054
						deps,"%s",
b08054
						deplib);
b08054
b08054
				if (ret < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			if (mapinfo)
b08054
				slbt_unmap_file(mapinfo);
b08054
		}
b08054
b08054
		if (plib && (slbt_dprintf(deps,"-l%s\n",plib) < 0)) {
b08054
			close(deps);
b08054
			return SLBT_SYSTEM_ERROR(dctx,0);
b08054
		}
b08054
b08054
		if (path && (slbt_dprintf(deps,"-L%s\n",path) < 0)) {
b08054
			close(deps);
b08054
			return SLBT_SYSTEM_ERROR(dctx,0);
b08054
		}
b08054
	}
b08054
b08054
	return 0;
b08054
}