3 * Generate module.c from module.in and Makefile.am or Makefile.
5 * Copyright (C) 2004-2007 Peter Johnson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
33 #include "compat-queue.h"
35 #define OUTPUT "module.c"
38 #define MAXMODULES 128
39 #define MAXINCLUDES 256
41 typedef struct include {
42 STAILQ_ENTRY(include) link;
47 main(int argc, char *argv[])
54 char *modules[MAXMODULES];
56 STAILQ_HEAD(includehead, include) includes =
57 STAILQ_HEAD_INITIALIZER(includes);
63 fprintf(stderr, "Usage: %s <module.in> <Makefile[.am]>\n", argv[0]);
67 str = malloc(MAXLINE);
69 /* Starting with initial input Makefile, look for include <file> or
70 * YASM_MODULES += <module>. Note this currently doesn't handle
71 * a relative starting path.
73 len = strlen(argv[2]);
74 inc = malloc(sizeof(include));
75 inc->filename = malloc(len+1);
76 strcpy(inc->filename, argv[2]);
77 STAILQ_INSERT_TAIL(&includes, inc, link);
79 isam = argv[2][len-2] == 'a' && argv[2][len-1] == 'm';
81 while (!STAILQ_EMPTY(&includes)) {
82 inc = STAILQ_FIRST(&includes);
83 STAILQ_REMOVE_HEAD(&includes, link);
84 in = fopen(inc->filename, "rt");
86 fprintf(stderr, "Could not open `%s'.\n", inc->filename);
92 while (fgets(str, MAXLINE, in)) {
93 /* Strip off any trailing whitespace */
97 while (len > 0 && isspace(*strp)) {
105 /* Skip whitespace */
106 while (isspace(*strp))
113 /* If line continuation, skip to continue copy */
117 /* Check for include if original input is .am file */
118 if (isam && strncmp(strp, "include", 7) == 0 && isspace(strp[7])) {
120 while (isspace(*strp))
122 /* Build new include and add to end of list */
123 inc = malloc(sizeof(include));
124 inc->filename = malloc(strlen(strp)+1);
125 strcpy(inc->filename, strp);
126 STAILQ_INSERT_TAIL(&includes, inc, link);
130 /* Check for YASM_MODULES = or += */
131 if (strncmp(strp, "YASM_MODULES", 12) != 0)
134 while (isspace(*strp))
136 if (strncmp(strp, "+=", 2) != 0 && *strp != '=')
141 while (isspace(*strp))
145 /* Check for continuation */
146 if (len > 0 && str[len-1] == '\\') {
148 while (isspace(*strp))
154 while (*strp != '\0') {
155 /* Copy module name */
156 modules[num_modules] = malloc(MAXNAME);
158 while (*strp != '\0' && !isspace(*strp))
159 modules[num_modules][len++] = *strp++;
160 modules[num_modules][len] = '\0';
163 while (isspace(*strp))
170 out = fopen(OUTPUT, "wt");
173 fprintf(stderr, "Could not open `%s'.\n", OUTPUT);
177 fprintf(out, "/* This file auto-generated by genmodule.c"
178 " - don't edit it */\n\n");
180 in = fopen(argv[1], "rt");
182 fprintf(stderr, "Could not open `%s'.\n", argv[1]);
189 while (fgets(str, MAXLINE, in)) {
190 if (strncmp(str, "MODULES_", 8) == 0) {
193 while (*strp != '\0' && *strp != '_') {
199 for (i=0; i<num_modules; i++) {
200 if (strncmp(modules[i], str+8, len) == 0) {
201 fprintf(out, " {\"%s\", &yasm_%s_LTX_%s},\n",
202 modules[i]+len+1, modules[i]+len+1, str+8);
205 } else if (strncmp(str, "EXTERN_LIST", 11) == 0) {
206 for (i=0; i<num_modules; i++) {
207 strcpy(str, modules[i]);
209 while (*strp != '\0' && *strp != '_')
213 fprintf(out, "extern yasm_%s_module yasm_%s_LTX_%s;\n",
223 for (i=0; i<num_modules; i++)