summaryrefslogtreecommitdiff
path: root/project/pyexts.c
blob: 7871b4a77247347e6120152edfb2d0f904f7b28d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*******************************************************************/
/*  sbpython3: external configurable build project for Python-3.x. */
/*  Copyright (C) 2018--2021  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SBPYTHON3.*/
/*******************************************************************/

/*******************************************************************/
/* pyexts.c: a simple development-time utility for generating      */
/* make rules for python's extension modules. The utility is       */
/* included with the sbpython3 project for reference only.         */
/*                                                                 */
/* cc -std=c99 -D_XOPEN_SOURCE=700 pyexts.c                        */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include "pyexts.h"

static void pyext_uppercase(char * buf, const char * name)
{
	const char *	src;
	char *		dst;

	src = name;
	dst = buf;

	for (; *src; )
		*dst++ = ((*src >= 'a') && (*src <= 'z'))
			? 'A' - 'a' + *src++ : *src++;

	*dst = 0;
}

int main(int argc, char ** argv)
{
	const struct pyext_meta *	p;
	size_t				nlen;
	const char **			parg;
	const char *			dtab;
	const char *			otab;
	const char *			htab;
	const char *			stab;
	const char *			ltab;
	const char *			rtab;
	const char *			vtab;
	const char *			name;
	char				fmt[64];
	char				uname[64];

	if ((argc == 2) && !strcmp(argv[1],"--py-init-func")) {
		for (p=pyexts; p->name; p++)
			printf("PY_INIT_FUNC(%s);\n",p->name);

		return 0;
	}

	if ((argc == 2) && !strcmp(argv[1],"--init-func-ptr")) {
		for (p=pyexts; p->name; p++) {
			nlen = strlen(p->name);

			sprintf(fmt,"\t{\"%%s\",%%-%zusPyInit_%%s},%%-%zus\\\n",
				20-nlen,23-nlen);

			printf(fmt,p->name," ",p->name," ");
		}

		return 0;
	}

	for (p=pyexts; p->name; p++) {
		/* init */
		name = (p->name[0] == '_') ? &p->name[1] : p->name;
		nlen = strlen(name);

		pyext_uppercase(uname,name);

		/* pretty */
		dtab = (nlen <= 4)
			? "\t\t\t"
			: (nlen <= 12)
				? "\t\t" : "\t";

		otab = (nlen <= 3)
			? "\t\t\t"
			: (nlen <= 11)
				? "\t\t" : "\t";

		ltab = (nlen <= 2)
			? "\t\t\t"
			: (nlen <= 10)
				? "\t\t" : "\t";

		rtab = (nlen <= 1)
			? "\t\t\t"
			: (nlen <= 9)
				? "\t\t" : "\t";

		vtab = (nlen <= 6)
			? "\t\t"
			: (nlen <= 14)
				? "\t" : "";

		htab = (nlen <= 8)
			? "\t\t" : "\t";

		stab = (nlen <= 9)
			? "\t\t" : "\t";


		/* output */
		printf("# %s\n",p->name);

		for (parg=p->srcs; *parg; parg++)
			printf("PYEXT_%s_SRCS%s+= "
				"pyext/obj/%s.c\n",
				uname,dtab,*parg);

		printf("\n");
		printf("PYEXT_%s_LOBJS%s= $(PYEXT_%s_SRCS:.c=.lo)\n",uname,otab,uname);
		printf("PYEXT_%s_OBJS%s= $(PYEXT_%s_SRCS:.c=.o)\n",uname,dtab,uname);
		printf("\n");

		for (parg=p->hdrs; parg && *parg; parg++)
			printf("$(PYEXT_%s_LOBJS):%s"
				"CFLAGS_CONFIG += %s\n",
				uname,htab,*parg);

		for (parg=p->hdrs; parg && *parg; parg++)
			printf("$(PYEXT_%s_OBJS):%s"
				"CFLAGS_CONFIG += %s\n",
				uname,stab,*parg);

		for (parg=p->cstd; parg && *parg; parg++)
				printf("$(PYEXT_%s_LOBJS):%s"
					"CFLAGS_LAST += %s\n",
					uname,otab,*parg);

		for (parg=p->cstd; parg && *parg; parg++)
			printf("$(PYEXT_%s_OBJS):%s"
				"CFLAGS_LAST += %s\n",
				uname,dtab,*parg);

		if (p->hdrs || p->cstd)
			printf("\n");

		printf("PYEXT_%s_SHARED%s= pyext/%s$(PYTHON_SOEXT)\n",uname,ltab,p->name);
		printf("PYEXT_%s_STATIC%s= pyext/%s$(OS_ARCHIVE_EXT)\n",uname,ltab,p->name);
		printf("\n");

		for (parg=p->vars; parg && *parg; parg++)
			printf("$(PYEXT_%s_SHARED):%s"
				"LDFLAGS_DYNEXT += %s\n",
				uname,vtab,*parg);

		for (parg=p->deps; parg && *parg; parg++)
			printf("$(PYEXT_%s_SHARED):%s%s\n",uname,vtab,*parg);

		printf("$(PYEXT_%s_SHARED):%s$(PYEXT_%s_LOBJS)\n",uname,vtab,uname);
		printf("$(PYEXT_%s_STATIC):%s$(PYEXT_%s_OBJS)\n",uname,vtab,uname);

		printf("\n");
		printf("pyext-%s-shared:%s$(PYEXT_%s_SHARED)\n",name,rtab,uname);
		printf("pyext-%s-static:%s$(PYEXT_%s_STATIC)\n",name,rtab,uname);

		printf("\n");
		printf("pyext-%s-clean:\n",name);
		printf("\trm -f $(PYEXT_%s_LOBJS)\n",uname);
		printf("\trm -f $(PYEXT_%s_OBJS)\n",uname);
		printf("\trm -f $(PYEXT_%s_SHARED)\n",uname);
		printf("\trm -f $(PYEXT_%s_STATIC)\n",uname);

		printf("\n");
		printf("clean:\tpyext-%s-clean\n",name);

		printf("\n");
		printf(".PHONY:\tpyext-%s-shared\n",name);
		printf(".PHONY:\tpyext-%s-static\n",name);
		printf(".PHONY:\tpyext-%s-clean\n",name);
		printf("\n\n");
	}

	return 0;
}