firasuke / cross / slibtool

Forked from cross/slibtool 3 months ago
Clone

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
e39fca
static const char vermsg[] = "%s (git://midipix.org/slibtool): "
e39fca
			     "version %d.%d.%d.\n"
e39fca
			     "[commit reference: %s]\n";
9ca8c4
9ca8c4
static ssize_t slibtool_version(struct slbt_driver_ctx * dctx)
9ca8c4
{
e39fca
	const struct slbt_source_version * verinfo;
e39fca
e39fca
	verinfo = slbt_source_version();
e39fca
e39fca
	return fprintf(stdout,vermsg,dctx->program,
e39fca
			verinfo->major,verinfo->minor,verinfo->revision,
e39fca
			verinfo->commit);
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
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
{
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
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