xref: /openbsd-src/usr.bin/make/generate.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: generate.c,v 1.6 2006/01/20 23:10:19 espie Exp $ */
3 
4 /*
5  * Copyright (c) 2001 Marc Espie.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 
34 #include "stats.h"
35 #include "ohash.h"
36 #include "cond_int.h"
37 #include "var_int.h"
38 
39 #define M(x)	x, #x
40 char *table_var[] = {
41 	M(TARGET),
42 	M(OODATE),
43 	M(ALLSRC),
44 	M(IMPSRC),
45 	M(PREFIX),
46 	M(ARCHIVE),
47 	M(MEMBER),
48 	M(LONGTARGET),
49 	M(LONGOODATE),
50 	M(LONGALLSRC),
51 	M(LONGIMPSRC),
52 	M(LONGPREFIX),
53 	M(LONGARCHIVE),
54 	M(LONGMEMBER),
55 	M(FTARGET),
56 	M(DTARGET),
57 	M(FPREFIX),
58 	M(DPREFIX),
59 	M(FARCHIVE),
60 	M(DARCHIVE),
61 	M(FMEMBER),
62 	M(DMEMBER),
63 	NULL
64 };
65 
66 char *table_cond[] = {
67 	M(COND_IF),
68 	M(COND_IFDEF),
69 	M(COND_IFNDEF),
70 	M(COND_IFMAKE),
71 	M(COND_IFNMAKE),
72 	M(COND_ELSE),
73 	M(COND_ELIF),
74 	M(COND_ELIFDEF),
75 	M(COND_ELIFNDEF),
76 	M(COND_ELIFMAKE),
77 	M(COND_ELIFNMAKE),
78 	M(COND_ENDIF),
79 	M(COND_FOR),
80 	M(COND_ENDFOR),
81 	M(COND_INCLUDE),
82 	M(COND_UNDEF),
83 	NULL
84 };
85 
86 
87 char **table[] = {
88 	table_var,
89 	table_cond
90 };
91 
92 int
93 main(int argc, char *argv[])
94 {
95 	uint32_t i;
96 	uint32_t v;
97 	uint32_t h;
98 	uint32_t slots;
99 	const char *e;
100 	char **occupied;
101 	char **t;
102 	int tn;
103 
104 	Init_Stats();
105 	if (argc != 3)
106 		exit(1);
107 
108 	tn = atoi(argv[1]);
109 	if (!tn)
110 		exit(1);
111 	t = table[tn-1];
112 	slots = atoi(argv[2]);
113 	if (slots) {
114 		occupied = malloc(sizeof(char *) * slots);
115 		if (!occupied)
116 			exit(1);
117 		for (i = 0; i < slots; i++)
118 			occupied[i] = NULL;
119 	} else
120 		occupied = NULL;
121 
122 	printf("/* File created by generate %d %d, do not edit */\n",
123 	    tn, slots);
124 	for (i = 0; t[i] != NULL; i++) {
125 		e = NULL;
126 		v = ohash_interval(t[i], &e);
127 		if (slots) {
128 			h = v % slots;
129 			if (occupied[h]) {
130 				fprintf(stderr,
131 				    "Collision: %s / %s (%d)\n", occupied[h],
132 				    t[i], h);
133 				exit(1);
134 			}
135 			occupied[h] = t[i];
136 		}
137 		i++;
138 		printf("#define K_%s %u\n", t[i], v);
139 	}
140 	if (slots)
141 		printf("#define MAGICSLOTS%d %u\n", tn, slots);
142 	exit(0);
143 }
144