altomaltes / cross / slibtool

Forked from cross/slibtool 2 years ago
Clone

Blame src/driver/slbt_amain.c

74159a
/*******************************************************************/
74159a
/*  slibtool: a skinny libtool implementation, written in C        */
6803d8
/*  Copyright (C) 2016--2018  Z. Gilboa                            */
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
74159a
	verinfo = slbt_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
74159a
static void slbt_perform_driver_actions(struct slbt_driver_ctx * dctx)
74159a
{
74159a
	if (dctx->cctx->drvflags & SLBT_DRIVER_CONFIG)
66eddb
		slbt_output_config(dctx);
74159a
23b101
	if (dctx->cctx->drvflags & SLBT_DRIVER_FEATURES)
23b101
		slbt_output_features(dctx);
23b101
74159a
	if (dctx->cctx->mode == SLBT_MODE_COMPILE)
66eddb
		slbt_exec_compile(dctx,0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_EXECUTE)
66eddb
		slbt_exec_execute(dctx,0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_INSTALL)
66eddb
		slbt_exec_install(dctx,0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_LINK)
66eddb
		slbt_exec_link(dctx,0);
0fb20a
0fb20a
	if (dctx->cctx->mode == SLBT_MODE_UNINSTALL)
0fb20a
		slbt_exec_uninstall(dctx,0);
74159a
}
74159a
66eddb
static int slbt_exit(struct slbt_driver_ctx * dctx, int ret)
74159a
{
66eddb
	slbt_output_error_vector(dctx);
74159a
	slbt_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
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 */
d4c3e3
	if ((ret = slbt_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
6f4115
	/* --version is always the first action */
74159a
	if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
644014
		if ((slbt_version(dctx,fdout)) < 0)
0254df
			return slbt_exit(dctx,SLBT_ERROR);
74159a
74159a
	slbt_perform_driver_actions(dctx);
74159a
0254df
	return slbt_exit(dctx,dctx->errv[0] ? SLBT_ERROR : SLBT_OK);
74159a
}