Blame project/pyexts.c

b90775
/*******************************************************************/
b90775
/*  sbpython3: external configurable build project for Python-3.x. */
b90775
/*  Copyright (C) 2018  Z. Gilboa                                  */
b90775
/*  Released under the Standard MIT License; see COPYING.SBPYTHON3.*/
b90775
/*******************************************************************/
b90775
b90775
/*******************************************************************/
b90775
/* pyexts.c: a simple development-time utility for generating      */
b90775
/* make rules for python's extension modules. The utility is       */
b90775
/* included with the sbpython3 project for reference only.         */
b90775
/*                                                                 */
b90775
/* cc -std=c99 -D_XOPEN_SOURCE=700 pyexts.c                        */
b90775
/*******************************************************************/
b90775
b90775
#include <stdio.h>
b90775
#include <string.h>
feb6bd
#include "pyexts.h"
b90775
b90775
static void pyext_uppercase(char * buf, const char * name)
b90775
{
b90775
	const char *	src;
b90775
	char *		dst;
b90775
b90775
	src = name;
b90775
	dst = buf;
b90775
b90775
	for (; *src; )
b90775
		*dst++ = ((*src >= 'a') && (*src <= 'z'))
b90775
			? 'A' - 'a' + *src++ : *src++;
b90775
b90775
	*dst = 0;
b90775
}
b90775
b90775
int main(int argc, char ** argv)
b90775
{
b90775
	const struct pyext_meta *	p;
b90775
	size_t				nlen;
b90775
	const char **			parg;
b90775
	const char *			dtab;
b90775
	const char *			otab;
b90775
	const char *			htab;
b90775
	const char *			stab;
b90775
	const char *			ltab;
b90775
	const char *			rtab;
b90775
	const char *			vtab;
b90775
	const char *			name;
b90775
	char				fmt[64];
b90775
	char				uname[64];
b90775
b90775
	if ((argc == 2) && !strcmp(argv[1],"--py-init-func")) {
b90775
		for (p=pyexts; p->name; p++)
b90775
			printf("PY_INIT_FUNC(%s);\n",p->name);
b90775
b90775
		return 0;
b90775
	}
b90775
b90775
	if ((argc == 2) && !strcmp(argv[1],"--init-func-ptr")) {
b90775
		for (p=pyexts; p->name; p++) {
b90775
			nlen = strlen(p->name);
b90775
b90775
			sprintf(fmt,"\t{\"%%s\",%%-%zusPyInit_%%s},%%-%zus\\\n",
b90775
				20-nlen,23-nlen);
b90775
b90775
			printf(fmt,p->name," ",p->name," ");
b90775
		}
b90775
b90775
		return 0;
b90775
	}
b90775
b90775
	for (p=pyexts; p->name; p++) {
b90775
		/* init */
b90775
		name = (p->name[0] == '_') ? &p->name[1] : p->name;
b90775
		nlen = strlen(name);
b90775
b90775
		pyext_uppercase(uname,name);
b90775
b90775
		/* pretty */
b90775
		dtab = (nlen <= 4)
b90775
			? "\t\t\t"
b90775
			: (nlen <= 12)
b90775
				? "\t\t" : "\t";
b90775
b90775
		otab = (nlen <= 3)
b90775
			? "\t\t\t"
b90775
			: (nlen <= 11)
b90775
				? "\t\t" : "\t";
b90775
b90775
		ltab = (nlen <= 2)
b90775
			? "\t\t\t"
b90775
			: (nlen <= 10)
b90775
				? "\t\t" : "\t";
b90775
b90775
		rtab = (nlen <= 1)
b90775
			? "\t\t\t"
b90775
			: (nlen <= 9)
b90775
				? "\t\t" : "\t";
b90775
b90775
		vtab = (nlen <= 6)
b90775
			? "\t\t"
b90775
			: (nlen <= 14)
b90775
				? "\t" : "";
b90775
b90775
		htab = (nlen <= 8)
b90775
			? "\t\t" : "\t";
b90775
b90775
		stab = (nlen <= 9)
b90775
			? "\t\t" : "\t";
b90775
b90775
b90775
		/* output */
b90775
		printf("# %s\n",p->name);
b90775
b90775
		for (parg=p->srcs; *parg; parg++)
b90775
			printf("PYEXT_%s_SRCS%s+= "
b90775
				"pyext/obj/%s.c\n",
b90775
				uname,dtab,*parg);
b90775
b90775
		printf("\n");
b90775
		printf("PYEXT_%s_LOBJS%s= $(PYEXT_%s_SRCS:.c=.lo)\n",uname,otab,uname);
b90775
		printf("PYEXT_%s_OBJS%s= $(PYEXT_%s_SRCS:.c=.o)\n",uname,dtab,uname);
b90775
		printf("\n");
b90775
b90775
		for (parg=p->hdrs; parg && *parg; parg++)
b90775
			printf("$(PYEXT_%s_LOBJS):%s"
b90775
				"CFLAGS_CONFIG += %s\n",
b90775
				uname,htab,*parg);
b90775
b90775
		for (parg=p->hdrs; parg && *parg; parg++)
b90775
			printf("$(PYEXT_%s_OBJS):%s"
b90775
				"CFLAGS_CONFIG += %s\n",
b90775
				uname,stab,*parg);
b90775
b90775
		for (parg=p->cstd; parg && *parg; parg++)
b90775
				printf("$(PYEXT_%s_LOBJS):%s"
b90775
					"CFLAGS_LAST += %s\n",
b90775
					uname,otab,*parg);
b90775
b90775
		for (parg=p->cstd; parg && *parg; parg++)
b90775
			printf("$(PYEXT_%s_OBJS):%s"
b90775
				"CFLAGS_LAST += %s\n",
b90775
				uname,dtab,*parg);
b90775
b90775
		if (p->hdrs || p->cstd)
b90775
			printf("\n");
b90775
b90775
		printf("PYEXT_%s_SHARED%s= pyext/%s$(PYTHON_SOEXT)\n",uname,ltab,p->name);
b90775
		printf("PYEXT_%s_STATIC%s= pyext/%s$(OS_ARCHIVE_EXT)\n",uname,ltab,p->name);
b90775
		printf("\n");
b90775
b90775
		for (parg=p->vars; parg && *parg; parg++)
b90775
			printf("$(PYEXT_%s_SHARED):%s"
b90775
				"LDFLAGS_DYNEXT += %s\n",
b90775
				uname,vtab,*parg);
b90775
b90775
		for (parg=p->deps; parg && *parg; parg++)
b90775
			printf("$(PYEXT_%s_SHARED):%s%s\n",uname,vtab,*parg);
b90775
b90775
		printf("$(PYEXT_%s_SHARED):%s$(PYEXT_%s_LOBJS)\n",uname,vtab,uname);
b90775
		printf("$(PYEXT_%s_STATIC):%s$(PYEXT_%s_OBJS)\n",uname,vtab,uname);
b90775
b90775
		printf("\n");
b90775
		printf("pyext-%s-shared:%s$(PYEXT_%s_SHARED)\n",name,rtab,uname);
b90775
		printf("pyext-%s-static:%s$(PYEXT_%s_STATIC)\n",name,rtab,uname);
b90775
b90775
		printf("\n");
b90775
		printf("pyext-%s-clean:\n",name);
b90775
		printf("\trm -f $(PYEXT_%s_LOBJS)\n",uname);
b90775
		printf("\trm -f $(PYEXT_%s_OBJS)\n",uname);
b90775
		printf("\trm -f $(PYEXT_%s_SHARED)\n",uname);
b90775
		printf("\trm -f $(PYEXT_%s_STATIC)\n",uname);
b90775
b90775
		printf("\n");
b90775
		printf("clean:\tpyext-%s-clean\n",name);
b90775
b90775
		printf("\n");
b90775
		printf(".PHONY:\tpyext-%s-shared\n",name);
b90775
		printf(".PHONY:\tpyext-%s-static\n",name);
b90775
		printf(".PHONY:\tpyext-%s-clean\n",name);
b90775
		printf("\n\n");
b90775
	}
b90775
b90775
	return 0;
b90775
}