orbea / cross / slibtool

Forked from cross/slibtool 3 years ago
Clone

Blame src/driver/slbt_amain.c

74159a
/*******************************************************************/
74159a
/*  slibtool: a skinny libtool implementation, written in C        */
74159a
/*  Copyright (C) 2016  Z. Gilboa                                  */
74159a
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
74159a
/*******************************************************************/
74159a
74159a
#include <stdio.h>
74159a
#include <unistd.h>
74159a
#include <string.h>
61daf8
#include <errno.h>
74159a
#include <slibtool/slibtool.h>
74159a
#include "slibtool_driver_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
74159a
static const char vermsg[] = "%s%s%s (git://midipix.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
74159a
static ssize_t slbt_version(struct slbt_driver_ctx * dctx)
74159a
{
74159a
	const struct slbt_source_version * verinfo;
74159a
	const char * const * verclr;
74159a
	bool gitver;
74159a
74159a
	verinfo = slbt_source_version();
74159a
	verclr  = isatty(STDOUT_FILENO) ? slbt_ver_color : slbt_ver_plain;
74159a
	gitver  = strcmp(verinfo->commit,"unknown");
74159a
74159a
	return fprintf(stdout,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)
74159a
		dctx->nerrors += (slbt_output_config(dctx) < 0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_COMPILE)
74159a
		dctx->nerrors += (slbt_exec_compile(dctx,0) < 0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_EXECUTE)
74159a
		dctx->nerrors += (slbt_exec_execute(dctx,0) < 0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_INSTALL)
74159a
		dctx->nerrors += (slbt_exec_install(dctx,0) < 0);
74159a
74159a
	if (dctx->cctx->mode == SLBT_MODE_LINK)
74159a
		dctx->nerrors += (slbt_exec_link(dctx,0) < 0);
74159a
}
74159a
74159a
static void slbt_perform_unit_actions(struct slbt_unit_ctx * uctx)
74159a
{
74159a
	(void)uctx;
74159a
}
74159a
74159a
static int slbt_exit(struct slbt_driver_ctx * dctx, int nerrors)
74159a
{
74159a
	if (nerrors && errno)
74159a
		strerror(errno);
74159a
74159a
	slbt_free_driver_ctx(dctx);
74159a
	return nerrors ? 2 : 0;
74159a
}
74159a
74159a
int slbt_main(int argc, char ** argv, char ** envp)
74159a
{
74159a
	int				ret;
74159a
	uint64_t			flags;
74159a
	struct slbt_driver_ctx *	dctx;
74159a
	struct slbt_unit_ctx *		uctx;
74159a
	const char **			unit;
74159a
	char *				program;
74159a
	char *				dash;
74159a
	char *				sargv[5];
74159a
74159a
	/* --version only? */
74159a
	if ((argc == 2) && (!strcmp(argv[1],"--version")
74159a
				|| !strcmp(argv[1],"--help-all")
74159a
				|| !strcmp(argv[1],"--help")
74159a
				|| !strcmp(argv[1],"-h"))) {
74159a
		sargv[0] = argv[0];
74159a
		sargv[1] = argv[1];
74159a
		sargv[2] = "--mode=compile";
74159a
		sargv[3] = "<compiler>";
74159a
		sargv[4] = 0;
74159a
74159a
		return (slbt_get_driver_ctx(sargv,envp,SLBT_DRIVER_FLAGS,&dctx))
74159a
			? 2 : (slbt_version(dctx) < 0)
74159a
				? slbt_exit(dctx,2)
74159a
				: slbt_exit(dctx,0);
74159a
	}
74159a
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
	else
74159a
		flags = SLBT_DRIVER_FLAGS;
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
74159a
	/* driver context */
74159a
	if ((ret = slbt_get_driver_ctx(argv,envp,flags,&dctx)))
74159a
		return (ret == SLBT_USAGE) ? !--argc : 2;
74159a
74159a
	if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
74159a
		if ((slbt_version(dctx)) < 0)
74159a
			return slbt_exit(dctx,2);
74159a
74159a
	slbt_perform_driver_actions(dctx);
74159a
	ret += dctx->nerrors;
74159a
74159a
	for (unit=dctx->units; *unit; unit++) {
74159a
		if (!(slbt_get_unit_ctx(dctx,*unit,&uctx))) {
74159a
			slbt_perform_unit_actions(uctx);
74159a
			ret += uctx->nerrors;
74159a
			slbt_free_unit_ctx(uctx);
74159a
		}
74159a
	}
74159a
74159a
	return slbt_exit(dctx,ret);
74159a
}