Blame src/host/slbt_host_flavor.c

47a74a
/*******************************************************************/
47a74a
/*  slibtool: a skinny libtool implementation, written in C        */
47a74a
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
47a74a
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
47a74a
/*******************************************************************/
47a74a
47a74a
#include <slibtool/slibtool.h>
47a74a
#include "slibtool_driver_impl.h"
47a74a
47a74a
/* elf rpath */
47a74a
static const char * ldrpath_elf[] = {
47a74a
	"/lib",
47a74a
	"/lib/64",
47a74a
	"/usr/lib",
47a74a
	"/usr/lib64",
47a74a
	"/usr/local/lib",
47a74a
	"/usr/local/lib64",
47a74a
	0};
47a74a
47a74a
/* flavor settings */
47a74a
#define SLBT_FLAVOR_SETTINGS(flavor,          \
47a74a
		bfmt,pic,                     \
47a74a
		arp,ars,dsop,dsos,osds,osdf,  \
98dca8
		exep,exes,impp,imps,maps,     \
47a74a
		ldenv)                        \
47a74a
	static const struct slbt_flavor_settings flavor = {  \
47a74a
		bfmt,arp,ars,dsop,dsos,osds,osdf,           \
98dca8
		exep,exes,impp,imps,maps,                  \
47a74a
		ldenv,pic}
47a74a
47a74a
SLBT_FLAVOR_SETTINGS(host_flavor_default,       \
47a74a
	"elf","-fPIC",                          \
47a74a
	"lib",".a","lib",".so",".so","",        \
752c02
	"","","","",".expsyms.ver",             \
47a74a
	"LD_LIBRARY_PATH");
47a74a
47a74a
SLBT_FLAVOR_SETTINGS(host_flavor_midipix,       \
47a74a
	"pe","-fPIC",                           \
47a74a
	"lib",".a","lib",".so",".so","",        \
98dca8
	"","","lib",".lib.a",".expsyms.def",    \
47a74a
	"LD_LIBRARY_PATH");
47a74a
47a74a
SLBT_FLAVOR_SETTINGS(host_flavor_mingw,         \
47a74a
	"pe",0,                                 \
47a74a
	"lib",".a","lib",".dll","",".dll",      \
98dca8
	"",".exe","lib",".dll.a",".expsyms.def",\
47a74a
	"PATH");
47a74a
47a74a
SLBT_FLAVOR_SETTINGS(host_flavor_cygwin,        \
47a74a
	"pe",0,                                 \
47a74a
	"lib",".a","lib",".dll","",".dll",      \
98dca8
	"",".exe","lib",".dll.a",".expsyms.def",\
47a74a
	"PATH");
47a74a
a8b6dd
SLBT_FLAVOR_SETTINGS(host_flavor_msys,          \
a8b6dd
	"pe",0,                                 \
a8b6dd
	"lib",".a","lib",".dll","",".dll",      \
98dca8
	"",".exe","lib",".dll.a",".expsyms.def",\
a8b6dd
	"PATH");
a8b6dd
47a74a
SLBT_FLAVOR_SETTINGS(host_flavor_darwin,        \
47a74a
	"macho","-fPIC",                        \
47a74a
	"lib",".a","lib",".dylib","",".dylib",  \
752c02
	"","","","",".expsyms.exp",             \
47a74a
	"DYLD_LIBRARY_PATH");
47a74a
47a74a
47a74a
slbt_hidden int slbt_init_ldrpath(
47a74a
	struct slbt_common_ctx *  cctx,
47a74a
	struct slbt_host_params * host)
47a74a
{
47a74a
	char *         buf;
47a74a
	const char **  ldrpath;
47a74a
47a74a
	if (!cctx->rpath || !(cctx->drvflags & SLBT_DRIVER_IMAGE_ELF)) {
47a74a
		host->ldrpath = 0;
47a74a
		return 0;
47a74a
	}
47a74a
47a74a
	/* common? */
47a74a
	for (ldrpath=ldrpath_elf; *ldrpath; ldrpath ++)
47a74a
		if (!(strcmp(cctx->rpath,*ldrpath))) {
47a74a
			host->ldrpath = 0;
47a74a
			return 0;
47a74a
		}
47a74a
47a74a
	/* buf */
47a74a
	if (!(buf = malloc(12 + strlen(cctx->host.host))))
47a74a
		return -1;
47a74a
47a74a
	/* /usr/{host}/lib */
47a74a
	sprintf(buf,"/usr/%s/lib",cctx->host.host);
47a74a
47a74a
	if (!(strcmp(cctx->rpath,buf))) {
47a74a
		host->ldrpath = 0;
47a74a
		free(buf);
47a74a
		return 0;
47a74a
	}
47a74a
47a74a
	/* /usr/{host}/lib64 */
47a74a
	sprintf(buf,"/usr/%s/lib64",cctx->host.host);
47a74a
47a74a
	if (!(strcmp(cctx->rpath,buf))) {
47a74a
		host->ldrpath = 0;
47a74a
		free(buf);
47a74a
		return 0;
47a74a
	}
47a74a
47a74a
	host->ldrpath = cctx->rpath;
47a74a
47a74a
	free(buf);
47a74a
	return 0;
47a74a
}
47a74a
47a74a
47a74a
slbt_hidden void slbt_init_flavor_settings(
47a74a
	struct slbt_common_ctx *	cctx,
47a74a
	const struct slbt_host_params * ahost,
47a74a
	struct slbt_flavor_settings *	psettings)
47a74a
{
47a74a
	const struct slbt_host_params *     host;
47a74a
	const struct slbt_flavor_settings * settings;
47a74a
47a74a
	host = ahost ? ahost : &cctx->host;
47a74a
47a74a
	if (!strcmp(host->flavor,"midipix"))
47a74a
		settings = &host_flavor_midipix;
47a74a
	else if (!strcmp(host->flavor,"mingw"))
47a74a
		settings = &host_flavor_mingw;
47a74a
	else if (!strcmp(host->flavor,"cygwin"))
47a74a
		settings = &host_flavor_cygwin;
a8b6dd
	else if (!strcmp(host->flavor,"msys"))
a8b6dd
		settings = &host_flavor_msys;
47a74a
	else if (!strcmp(host->flavor,"darwin"))
47a74a
		settings = &host_flavor_darwin;
47a74a
	else
47a74a
		settings = &host_flavor_default;
47a74a
47a74a
	if (!ahost) {
47a74a
		if (!strcmp(settings->imagefmt,"elf"))
47a74a
			cctx->drvflags |= SLBT_DRIVER_IMAGE_ELF;
47a74a
		else if (!strcmp(settings->imagefmt,"pe"))
47a74a
			cctx->drvflags |= SLBT_DRIVER_IMAGE_PE;
47a74a
		else if (!strcmp(settings->imagefmt,"macho"))
47a74a
			cctx->drvflags |= SLBT_DRIVER_IMAGE_MACHO;
47a74a
	}
47a74a
47a74a
	memcpy(psettings,settings,sizeof(*settings));
47a74a
}
47a74a
47a74a
47a74a
int slbt_host_flavor_settings(
47a74a
	const char *                            flavor,
47a74a
	const struct slbt_flavor_settings **    settings)
47a74a
{
47a74a
	if (!strcmp(flavor,"midipix"))
47a74a
		*settings = &host_flavor_midipix;
47a74a
	else if (!strcmp(flavor,"mingw"))
47a74a
		*settings = &host_flavor_mingw;
47a74a
	else if (!strcmp(flavor,"cygwin"))
47a74a
		*settings = &host_flavor_cygwin;
a8b6dd
	else if (!strcmp(flavor,"msys"))
a8b6dd
		*settings = &host_flavor_msys;
47a74a
	else if (!strcmp(flavor,"darwin"))
47a74a
		*settings = &host_flavor_darwin;
47a74a
	else if (!strcmp(flavor,"default"))
47a74a
		*settings = &host_flavor_default;
47a74a
	else
47a74a
		*settings = 0;
47a74a
47a74a
	return *settings ? 0 : -1;
47a74a
}
f248f3
f248f3
f248f3
int slbt_host_objfmt_is_coff(const struct slbt_driver_ctx * dctx)
f248f3
{
f248f3
	const char * flavor = dctx->cctx->host.flavor;
f248f3
f248f3
	return !strcmp(flavor,"midipix")
f248f3
		|| !strcmp(flavor,"mingw")
f248f3
		|| !strcmp(flavor,"cygwin")
f248f3
		|| !strcmp(flavor,"msys");
f248f3
}
f248f3
f248f3
f248f3
int slbt_host_objfmt_is_macho(const struct slbt_driver_ctx * dctx)
f248f3
{
f248f3
	const char * flavor = dctx->cctx->host.flavor;
f248f3
f248f3
	return !strcmp(flavor,"darwin");
f248f3
}
f248f3
f248f3
f248f3
int slbt_host_group_is_winnt(const struct slbt_driver_ctx * dctx)
f248f3
{
f248f3
	return slbt_host_objfmt_is_coff(dctx);
f248f3
}
f248f3
f248f3
f248f3
int slbt_host_group_is_darwin(const struct slbt_driver_ctx * dctx)
f248f3
{
f248f3
	return slbt_host_objfmt_is_macho(dctx);
f248f3
}