Blame src/slibtool.c

9ca8c4
/*******************************************************************/
9ca8c4
/*  slibtool: a skinny libtool implementation, written in C        */
9ca8c4
/*  Copyright (C) 2016  Z. Gilboa                                  */
9ca8c4
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
9ca8c4
/*******************************************************************/
9ca8c4
9ca8c4
#include <stdio.h>
9ca8c4
#include <unistd.h>
0c2489
#include <string.h>
9ca8c4
#include <slibtool/slibtool.h>
9ca8c4
#include "slibtool_driver_impl.h"
9ca8c4
9ca8c4
#ifndef SLBT_DRIVER_FLAGS
9ca8c4
#define SLBT_DRIVER_FLAGS	SLBT_DRIVER_VERBOSITY_ERRORS \
9ca8c4
				| SLBT_DRIVER_VERBOSITY_USAGE
9ca8c4
#endif
9ca8c4
6cc0dd
static const char vermsg[] = "%s%s%s (git://midipix.org/slibtool): "
6cc0dd
			     "version %s%d.%d.%d%s.\n"
6cc0dd
			     "[commit reference: %s%s%s]\n";
6cc0dd
6cc0dd
static const char * const slbt_ver_color[6] = {
520f08
		"\x1b[1m\x1b[35m","\x1b[0m",
520f08
		"\x1b[1m\x1b[32m","\x1b[0m",
520f08
		"\x1b[1m\x1b[34m","\x1b[0m"
6cc0dd
};
6cc0dd
6cc0dd
static const char * const slbt_ver_plain[6] = {
6cc0dd
		"","",
6cc0dd
		"","",
6cc0dd
		"",""
6cc0dd
};
9ca8c4
9ca8c4
static ssize_t slibtool_version(struct slbt_driver_ctx * dctx)
9ca8c4
{
e39fca
	const struct slbt_source_version * verinfo;
6cc0dd
	const char * const * verclr;
e39fca
e39fca
	verinfo = slbt_source_version();
6cc0dd
	verclr  = isatty(STDOUT_FILENO) ? slbt_ver_color : slbt_ver_plain;
e39fca
6cc0dd
	return fprintf(stdout,vermsg,
6cc0dd
			verclr[0],dctx->program,verclr[1],
6cc0dd
			verclr[2],verinfo->major,verinfo->minor,
6cc0dd
			verinfo->revision,verclr[3],
6cc0dd
			verclr[4],verinfo->commit,verclr[5]);
9ca8c4
}
9ca8c4
980491
static void slibtool_perform_driver_actions(struct slbt_driver_ctx * dctx)
980491
{
980491
	if (dctx->cctx->drvflags & SLBT_DRIVER_CONFIG)
980491
		dctx->nerrors += (slbt_output_config(dctx) < 0);
980491
980491
	if (dctx->cctx->mode == SLBT_MODE_COMPILE)
980491
		dctx->nerrors += (slbt_exec_compile(dctx,0) < 0);
583c0a
87ab8b
	if (dctx->cctx->mode == SLBT_MODE_EXECUTE)
87ab8b
		dctx->nerrors += (slbt_exec_execute(dctx,0) < 0);
87ab8b
258073
	if (dctx->cctx->mode == SLBT_MODE_INSTALL)
258073
		dctx->nerrors += (slbt_exec_install(dctx,0) < 0);
258073
583c0a
	if (dctx->cctx->mode == SLBT_MODE_LINK)
583c0a
		dctx->nerrors += (slbt_exec_link(dctx,0) < 0);
980491
}
980491
9ca8c4
static void slibtool_perform_unit_actions(struct slbt_unit_ctx * uctx)
9ca8c4
{
9ca8c4
}
9ca8c4
9ca8c4
static int slibtool_exit(struct slbt_driver_ctx * dctx, int nerrors)
9ca8c4
{
fd1cb8
	if (nerrors && errno)
fd1cb8
		strerror(errno);
fd1cb8
9ca8c4
	slbt_free_driver_ctx(dctx);
9ca8c4
	return nerrors ? 2 : 0;
9ca8c4
}
9ca8c4
9ca8c4
int slibtool_main(int argc, char ** argv, char ** envp)
9ca8c4
{
9ca8c4
	int				ret;
0c2489
	uint64_t			flags;
9ca8c4
	struct slbt_driver_ctx *	dctx;
9ca8c4
	struct slbt_unit_ctx *		uctx;
9ca8c4
	const char **			unit;
0c2489
	char *				program;
0c2489
	char *				dash;
7c05d6
	char *				sargv[5];
7c05d6
7c05d6
	/* --version only? */
10063d
	if ((argc == 2) && (!strcmp(argv[1],"--version")
10063d
				|| !strcmp(argv[1],"--help-all")
10063d
				|| !strcmp(argv[1],"--help")
10063d
				|| !strcmp(argv[1],"-h"))) {
7c05d6
		sargv[0] = argv[0];
7c05d6
		sargv[1] = argv[1];
7c05d6
		sargv[2] = "--mode=compile";
7c05d6
		sargv[3] = "<compiler>";
7c05d6
		sargv[4] = 0;
7c05d6
7c05d6
		return (slbt_get_driver_ctx(sargv,envp,SLBT_DRIVER_FLAGS,&dctx))
7c05d6
			? 2 : (slibtool_version(dctx) < 0)
7c05d6
				? slibtool_exit(dctx,2)
7c05d6
				: slibtool_exit(dctx,0);
7c05d6
	}
9ca8c4
0c2489
	/* program */
0c2489
	if ((program = strrchr(argv[0],'/')))
0c2489
		program++;
0c2489
	else
0c2489
		program = argv[0];
0c2489
0c2489
	/* dash */
0c2489
	if ((dash = strrchr(program,'-')))
0c2489
		dash++;
0c2489
0c2489
	/* flags */
0c2489
	if (dash == 0)
0c2489
		flags = SLBT_DRIVER_FLAGS;
0c2489
0c2489
	else if (!(strcmp(dash,"shared")))
0c2489
		flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_STATIC;
0c2489
0c2489
	else if (!(strcmp(dash,"static")))
0c2489
		flags = SLBT_DRIVER_FLAGS | SLBT_DRIVER_DISABLE_SHARED;
0c2489
ed2780
	else
ed2780
		flags = SLBT_DRIVER_FLAGS;
ed2780
e4f933
	/* debug */
e4f933
	if (!(strcmp(program,"dlibtool")))
e4f933
		flags |= SLBT_DRIVER_DEBUG;
e4f933
e4f933
	else if (!(strncmp(program,"dlibtool",8)))
e4f933
		if ((program[8] == '-') || (program[8] == '.'))
e4f933
			flags |= SLBT_DRIVER_DEBUG;
e4f933
0c2489
	/* driver context */
0c2489
	if ((ret = slbt_get_driver_ctx(argv,envp,flags,&dctx)))
9ca8c4
		return (ret == SLBT_USAGE) ? !--argc : 2;
9ca8c4
9ca8c4
	if (dctx->cctx->drvflags & SLBT_DRIVER_VERSION)
9ca8c4
		if ((slibtool_version(dctx)) < 0)
9ca8c4
			return slibtool_exit(dctx,2);
9ca8c4
980491
	slibtool_perform_driver_actions(dctx);
980491
	ret += dctx->nerrors;
980491
9ca8c4
	for (unit=dctx->units; *unit; unit++) {
9ca8c4
		if (!(slbt_get_unit_ctx(dctx,*unit,&uctx))) {
9ca8c4
			slibtool_perform_unit_actions(uctx);
9ca8c4
			ret += uctx->nerrors;
9ca8c4
			slbt_free_unit_ctx(uctx);
9ca8c4
		}
9ca8c4
	}
9ca8c4
9ca8c4
	return slibtool_exit(dctx,ret);
9ca8c4
}
9ca8c4
9ca8c4
#ifndef SLIBTOOL_IN_A_BOX
9ca8c4
9ca8c4
int main(int argc, char ** argv, char ** envp)
9ca8c4
{
9ca8c4
	return slibtool_main(argc,argv,envp);
9ca8c4
}
9ca8c4
9ca8c4
#endif