genmodule.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. *
  3. * Generate module.c from module.in and Makefile.am or Makefile.
  4. *
  5. * Copyright (C) 2004-2007 Peter Johnson
  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 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.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include "compat-queue.h"
  33. #define OUTPUT "module.c"
  34. #define MAXNAME 128
  35. #define MAXLINE 1024
  36. #define MAXMODULES 128
  37. #define MAXINCLUDES 256
  38. typedef struct include {
  39. STAILQ_ENTRY(include) link;
  40. char *filename;
  41. } include;
  42. int
  43. main(int argc, char *argv[])
  44. {
  45. FILE *in, *out;
  46. char *str;
  47. int i;
  48. size_t len;
  49. char *strp;
  50. char *modules[MAXMODULES];
  51. int num_modules = 0;
  52. STAILQ_HEAD(includehead, include) includes =
  53. STAILQ_HEAD_INITIALIZER(includes);
  54. include *inc;
  55. int isam = 0;
  56. int linecont = 0;
  57. if (argc != 3) {
  58. fprintf(stderr, "Usage: %s <module.in> <Makefile[.am]>\n", argv[0]);
  59. return EXIT_FAILURE;
  60. }
  61. str = malloc(MAXLINE);
  62. /* Starting with initial input Makefile, look for include <file> or
  63. * YASM_MODULES += <module>. Note this currently doesn't handle
  64. * a relative starting path.
  65. */
  66. len = strlen(argv[2]);
  67. inc = malloc(sizeof(include));
  68. inc->filename = malloc(len+1);
  69. strcpy(inc->filename, argv[2]);
  70. STAILQ_INSERT_TAIL(&includes, inc, link);
  71. isam = argv[2][len-2] == 'a' && argv[2][len-1] == 'm';
  72. while (!STAILQ_EMPTY(&includes)) {
  73. inc = STAILQ_FIRST(&includes);
  74. STAILQ_REMOVE_HEAD(&includes, link);
  75. in = fopen(inc->filename, "rt");
  76. if (!in) {
  77. fprintf(stderr, "Could not open `%s'.\n", inc->filename);
  78. return EXIT_FAILURE;
  79. }
  80. free(inc->filename);
  81. free(inc);
  82. while (fgets(str, MAXLINE, in)) {
  83. /* Strip off any trailing whitespace */
  84. len = strlen(str);
  85. if (len > 0) {
  86. strp = &str[len-1];
  87. while (len > 0 && isspace(*strp)) {
  88. *strp-- = '\0';
  89. len--;
  90. }
  91. }
  92. strp = str;
  93. /* Skip whitespace */
  94. while (isspace(*strp))
  95. strp++;
  96. /* Skip comments */
  97. if (*strp == '#')
  98. continue;
  99. /* If line continuation, skip to continue copy */
  100. if (linecont)
  101. goto keepgoing;
  102. /* Check for include if original input is .am file */
  103. if (isam && strncmp(strp, "include", 7) == 0 && isspace(strp[7])) {
  104. strp += 7;
  105. while (isspace(*strp))
  106. strp++;
  107. /* Build new include and add to end of list */
  108. inc = malloc(sizeof(include));
  109. inc->filename = malloc(strlen(strp)+1);
  110. strcpy(inc->filename, strp);
  111. STAILQ_INSERT_TAIL(&includes, inc, link);
  112. continue;
  113. }
  114. /* Check for YASM_MODULES = or += */
  115. if (strncmp(strp, "YASM_MODULES", 12) != 0)
  116. continue;
  117. strp += 12;
  118. while (isspace(*strp))
  119. strp++;
  120. if (strncmp(strp, "+=", 2) != 0 && *strp != '=')
  121. continue;
  122. if (*strp == '+')
  123. strp++;
  124. strp++;
  125. while (isspace(*strp))
  126. strp++;
  127. keepgoing:
  128. /* Check for continuation */
  129. if (len > 0 && str[len-1] == '\\') {
  130. str[len-1] = '\0';
  131. while (isspace(*strp))
  132. *strp-- = '\0';
  133. linecont = 1;
  134. } else
  135. linecont = 0;
  136. while (*strp != '\0') {
  137. /* Copy module name */
  138. modules[num_modules] = malloc(MAXNAME);
  139. len = 0;
  140. while (*strp != '\0' && !isspace(*strp))
  141. modules[num_modules][len++] = *strp++;
  142. modules[num_modules][len] = '\0';
  143. num_modules++;
  144. while (isspace(*strp))
  145. strp++;
  146. }
  147. }
  148. fclose(in);
  149. }
  150. out = fopen(OUTPUT, "wt");
  151. if (!out) {
  152. fprintf(stderr, "Could not open `%s'.\n", OUTPUT);
  153. return EXIT_FAILURE;
  154. }
  155. fprintf(out, "/* This file auto-generated by genmodule.c"
  156. " - don't edit it */\n\n");
  157. in = fopen(argv[1], "rt");
  158. if (!in) {
  159. fprintf(stderr, "Could not open `%s'.\n", argv[1]);
  160. fclose(out);
  161. remove(OUTPUT);
  162. return EXIT_FAILURE;
  163. }
  164. len = 0;
  165. while (fgets(str, MAXLINE, in)) {
  166. if (strncmp(str, "MODULES_", 8) == 0) {
  167. len = 0;
  168. strp = str+8;
  169. while (*strp != '\0' && *strp != '_') {
  170. len++;
  171. strp++;
  172. }
  173. *strp = '\0';
  174. for (i=0; i<num_modules; i++) {
  175. if (strncmp(modules[i], str+8, len) == 0) {
  176. fprintf(out, " {\"%s\", &yasm_%s_LTX_%s},\n",
  177. modules[i]+len+1, modules[i]+len+1, str+8);
  178. }
  179. }
  180. } else if (strncmp(str, "EXTERN_LIST", 11) == 0) {
  181. for (i=0; i<num_modules; i++) {
  182. strcpy(str, modules[i]);
  183. strp = str;
  184. while (*strp != '\0' && *strp != '_')
  185. strp++;
  186. *strp++ = '\0';
  187. fprintf(out, "extern yasm_%s_module yasm_%s_LTX_%s;\n",
  188. str, strp, str);
  189. }
  190. } else
  191. fputs(str, out);
  192. }
  193. fclose(in);
  194. fclose(out);
  195. for (i=0; i<num_modules; i++)
  196. free(modules[i]);
  197. free(str);
  198. return EXIT_SUCCESS;
  199. }