Blame src/driver/slbt_amain.c

74159a
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
74159a
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
74159a
/*******************************************************************/
74159a
74159a
#include <stdio.h>
d4c3e3
#include <stdlib.h>
74159a
#include <unistd.h>
74159a
#include <string.h>
61daf8
#include <errno.h>
d4c3e3
#include <stdbool.h>
74159a
#include <slibtool/slibtool.h>
74159a
#include "slibtool_driver_impl.h"
a82cc2
#include "slibtool_dprintf_impl.h"
74159a
74159a
#ifndef SLBT_DRIVER_FLAGS
74159a
#define SLBT_DRIVER_FLAGS	SLBT_DRIVER_VERBOSITY_ERRORS \
74159a
				| SLBT_DRIVER_VERBOSITY_USAGE
74159a
#endif
74159a
f5cf77
static const char vermsg[] = "%s%s%s (https://git.foss21.org/slibtool): "
74159a
			     "version %s%d.%d.%d%s.\n"
74159a
			     "%s%s%s%s%s\n";
74159a
74159a
static const char * const slbt_ver_color[6] = {
74159a
		"\x1b[1m\x1b[35m","\x1b[0m",
74159a
		"\x1b[1m\x1b[32m","\x1b[0m",
74159a
		"\x1b[1m\x1b[34m","\x1b[0m"
74159a
};
74159a
74159a
static const char * const slbt_ver_plain[6] = {
74159a
		"","",
74159a
		"","",
74159a
		"",""
74159a
};
74159a
644014
static ssize_t slbt_version(struct slbt_driver_ctx * dctx, int fdout)
74159a
{
74159a
	const struct slbt_source_version * verinfo;
74159a
	const char * const * verclr;
74159a
	bool gitver;
74159a
f0270a
	verinfo = slbt_api_source_version();
a82cc2
	verclr  = isatty(fdout) ? slbt_ver_color : slbt_ver_plain;
74159a
	gitver  = strcmp(verinfo->commit,"unknown");
74159a
d4c3e3
	if (dctx->cctx->drvflags & SLBT_DRIVER_ANNOTATE_NEVER)
d4c3e3
		verclr = slbt_ver_plain;
d4c3e3
a82cc2
	return slbt_dprintf(fdout,vermsg,
74159a
			verclr[0],dctx->program,verclr[1],
74159a
			verclr[2],verinfo->major,verinfo->minor,
74159a
			verinfo->revision,verclr[3],
74159a
			gitver ? "[commit reference: " : "",
74159a
			verclr[4],gitver ? verinfo->commit : "",
74159a
			verclr[5],gitver ? "]" : "");
74159a
}
74159a
805666
static ssize_t slbt_print_aux_dir(int fdout)
805666
{
805666
	return slbt_dprintf(fdout,"%s\n",SLBT_PACKAGE_DATADIR);
805666
}
805666
805666
static ssize_t slbt_print_m4_dir(int fdout)
805666
{
805666
	return slbt_dprintf(fdout,"%s\n",SLBT_PACKAGE_DATADIR);
805666
}
805666
74159a
static void slbt_perform_driver_actions(struct slbt_driver_ctx * dctx)
74159a
{
c57816
	if (dctx->cctx->drvflags & SLBT_DRIVER_INFO)
c57816
		slbt_output_info(dctx);
74159a
23b101
	if (dctx->cctx->drvflags & SLBT_DRIVER_FEATURES)
23b101
		slbt_output_features(dctx);
23b101
c1f216
	if (dctx->cctx->mode == SLBT_MODE_CONFIG)
c1f216
		slbt_output_config(dctx);
c1f216
74159a
	if (dctx->cctx->mode == SLBT_MODE_COMPILE)
2e30eb
		slbt_exec_compile(dctx);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_EXECUTE)
2e30eb
		slbt_exec_execute(dctx);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_INSTALL)
2e30eb
		slbt_exec_install(dctx);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_LINK)
2e30eb
		slbt_exec_link(dctx);
0fb20a
0fb20a
	if (dctx->cctx->mode == SLBT_MODE_UNINSTALL)
2e30eb
		slbt_exec_uninstall(dctx);
11f3c7
11f3c7
	if (dctx->cctx->mode == SLBT_MODE_AR)
2e30eb
		slbt_exec_ar(dctx);
4c3fb7
4c3fb7
	if (dctx->cctx->mode == SLBT_MODE_STOOLIE)
4c3fb7
		slbt_exec_stoolie(dctx);
74159a
}
74159a
66eddb
static int slbt_exit(struct slbt_driver_ctx * dctx, int ret)
74159a
{
66eddb
	slbt_output_error_vector(dctx);
ea4137
	slbt_lib_free_driver_ctx(dctx);
66eddb
	return ret;
74159a
}
74159a
633f4e
int slbt_main(char ** argv, char ** envp, const struct slbt_fd_ctx * fdctx)
74159a
{
74159a
	int				ret;
a82cc2
	int				fdout;
74159a
	uint64_t			flags;
d4c3e3
	uint64_t			noclr;
74159a
	struct slbt_driver_ctx *	dctx;
74159a
	char *				program;
74159a
	char *				dash;
74159a
a82cc2
	flags = SLBT_DRIVER_FLAGS;
a82cc2
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
d4c3e3
	noclr = getenv("NO_COLOR") ? SLBT_DRIVER_ANNOTATE_NEVER : 0;
a82cc2
74159a
	/* program */
74159a
	if ((program = strrchr(argv[0],'/')))
74159a
		program++;
74159a
	else
74159a
		program = argv[0];
74159a
74159a
	/* dash */
74159a
	if ((dash = strrchr(program,'-')))
74159a
		dash++;
74159a
74159a
	/* flags */
74159a
	if (dash == 0)
74159a
		flags = SLBT_DRIVER_FLAGS;
74159a
74159a
	else if (!(strcmp(dash,"shared")))
74159a
		flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_STATIC;
74159a
74159a
	else if (!(strcmp(dash,"static")))
74159a
		flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_SHARED;
74159a
11f3c7
	/* internal ar mode */
11f3c7
	else if (!(strcmp(dash,"ar")))
11f3c7
		flags |= SLBT_DRIVER_MODE_AR;
11f3c7
4c3fb7
	/* slibtoolize (stoolie) mode */
4c3fb7
	if (!(strcmp(program,"stoolie")))
4c3fb7
		flags |= SLBT_DRIVER_MODE_STOOLIE;
4c3fb7
4c3fb7
	else if (!(strcmp(program,"slibtoolize")))
4c3fb7
		flags |= SLBT_DRIVER_MODE_STOOLIE;
4c3fb7
74159a
	/* debug */
74159a
	if (!(strcmp(program,"dlibtool")))
74159a
		flags |= SLBT_DRIVER_DEBUG;
74159a
74159a
	else if (!(strncmp(program,"dlibtool",8)))
74159a
		if ((program[8] == '-') || (program[8] == '.'))
74159a
			flags |= SLBT_DRIVER_DEBUG;
74159a
74159a
	/* legabits */
74159a
	if (!(strcmp(program,"clibtool")))
74159a
		flags |= SLBT_DRIVER_LEGABITS;
74159a
74159a
	else if (!(strncmp(program,"clibtool",8)))
74159a
		if ((program[8] == '-') || (program[8] == '.'))
74159a
			flags |= SLBT_DRIVER_LEGABITS;
74159a
294283
	/* heuristics */
294283
	if (!(strcmp(program,"rlibtool")))
294283
		flags |= SLBT_DRIVER_HEURISTICS;
294283
101627
	/* heuristics + legabits */
101627
	if (!(strcmp(program,"rclibtool")))
101627
		flags |= (SLBT_DRIVER_HEURISTICS
101627
                          | SLBT_DRIVER_LEGABITS);
101627
101627
	/* heuristics + debug */
101627
	if (!(strcmp(program,"rdlibtool")))
101627
		flags |= (SLBT_DRIVER_HEURISTICS
101627
                          | SLBT_DRIVER_DEBUG);
101627
101627
	/* heuristics + debug + legabits */
101627
	if (!(strcmp(program,"rdclibtool")))
101627
		flags |= (SLBT_DRIVER_HEURISTICS
101627
                          | SLBT_DRIVER_DEBUG
101627
                          | SLBT_DRIVER_LEGABITS);
101627
74159a
	/* driver context */
ea4137
	if ((ret = slbt_lib_get_driver_ctx(argv,envp,flags|noclr,fdctx,&dctx)))
0254df
		return (ret == SLBT_USAGE)
50a552
			? !argv || !argv[0] || !argv[1] || !argv[2]
0254df
			: SLBT_ERROR;
74159a
6f4115
	/* --dumpmachine disables all other actions */
6f4115
	if (dctx->cctx->drvflags & SLBT_DRIVER_OUTPUT_MACHINE)
6f4115
		return slbt_output_machine(dctx)
6f4115
			? SLBT_ERROR : SLBT_OK;
6f4115
0b535f
	/* --version must be the first (and only) action */
74159a
	if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
b87f70
		if (dctx->cctx->mode != SLBT_MODE_AR)
4c3fb7
			if (dctx->cctx->mode != SLBT_MODE_STOOLIE)
4c3fb7
				return (slbt_version(dctx,fdout) < 0)
4c3fb7
					? slbt_exit(dctx,SLBT_ERROR)
4c3fb7
					: slbt_exit(dctx,SLBT_OK);
b87f70
805666
	/* -print-aux-dir must be the first (and only) action */
805666
	if (dctx->cctx->drvflags & SLBT_DRIVER_OUTPUT_AUX_DIR)
805666
		return (slbt_print_aux_dir(fdout) < 0)
805666
			? slbt_exit(dctx,SLBT_ERROR)
805666
			: slbt_exit(dctx,SLBT_OK);
805666
805666
	/* -print-m4-dir must be the first (and only) action */
805666
	if (dctx->cctx->drvflags & SLBT_DRIVER_OUTPUT_M4_DIR)
805666
		return (slbt_print_m4_dir(fdout) < 0)
805666
			? slbt_exit(dctx,SLBT_ERROR)
805666
			: slbt_exit(dctx,SLBT_OK);
805666
b87f70
	/* perform all other actions */
b87f70
	slbt_perform_driver_actions(dctx);
b87f70
b87f70
	/* print --version on behalf of a secondary tool as needed */
b87f70
	if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
0b535f
		return (slbt_version(dctx,fdout) < 0)
0b535f
			? slbt_exit(dctx,SLBT_ERROR)
0b535f
			: slbt_exit(dctx,SLBT_OK);
74159a
0254df
	return slbt_exit(dctx,dctx->errv[0] ? SLBT_ERROR : SLBT_OK);
74159a
}