PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/V_34_11/libpfe/src/tool/pfe/Make-H.py

#
Python | 154 lines | 148 code | 5 blank | 1 comment | 6 complexity | 1e26cc011e1e96c2cf331777663a51c2 MD5 | raw file
Possible License(s): Zlib, LGPL-2.0, AGPL-3.0, LGPL-2.1
  1. #! /usr/bin/env python
  2. import sys
  3. import re
  4. import os
  5. import os.path
  6. import string
  7. import time
  8. def m(obj, regex):
  9. return re.search(regex, obj)
  10. def s(obj, regex,replace):
  11. return re.sub(regex, replace, obj)
  12. def test_s(path):
  13. return os.path.isfile(path) and os.path.getsize(path)
  14. helpstring = """ <*.c file>
  15. \t scan the C file in the pfe/* source directory and regenerate
  16. \t the header file derived from them. The script will look for:
  17. \t - a global comment block at the start of the file to be copied
  18. \t - FXCode(name) definitions unless they are marked 'static'
  19. \t - a preceding comment block before that FXCode extern
  20. \t - any C declaration preceded by '_extern' copied to 'extern'
  21. \t - a preceding comment block before that _extern prototope
  22. \t - multiple lines between #ifndef _extern...#endif\\n with the
  23. \t ifndef removed. Header #includes are moved to the top of the.
  24. \t generated output file right after the initial #include
  25. \t The body of each regenerated header file is wrapped in the
  26. \t usual #ifndef once_declare...#endif derived from the filename
  27. \t of the input/output file. The output file has just the .h ext
  28. \t replaced with a .c extension. Additionally each header file has
  29. \t an 'initial include' file required to typedef the most basic
  30. \t pfe idioms including the #define FXCode(). In pfe each of the
  31. \t c source files has a two part stem worset-ext.c and the part
  32. \t after the last hyphen determines the variant of the initial
  33. \t include file - pfe-sub.h, pfe-ext.h, pfe-mix.h etc.
  34. """
  35. if len(sys.argv) == 1: print sys.argv[0], helpstring ; sys.exit(1)
  36. file = sys.argv[1]
  37. pfe = os.path.dirname(file)
  38. if not len(pfe): pfe="../pfe"
  39. inc = ""
  40. x = m(file,r"-(\w\w\w).c$")
  41. if x:
  42. if test_s(pfe+"/incl-"+x.group(1)+".h"):
  43. inc = "<pfe/incl-"+x.group(1)+".h>"
  44. if test_s(pfe+"/pfe-"+x.group(1)+".h"):
  45. inc = "<pfe/pfe-"+x.group(1)+".h>"
  46. #
  47. x = m(file,r"-(\w\w\w\w).c$")
  48. if x:
  49. if test_s(pfe+"/def-"+x.group(1)+".h"):
  50. inc = "<pfe/def-"+x.group(1)+".h>"
  51. if test_s(pfe+"/pfe-"+x.group(1)+".h"):
  52. inc = "<pfe/pfe-"+x.group(1)+".h>"
  53. #
  54. if not len(inc):
  55. print >>sys.stderr, file, " " * (27-len(file))," skipped (pfe='"+pfe+"')"
  56. sys.exit()
  57. tr_cpp = string.maketrans("./-"+string.lowercase, "___"+string.uppercase)
  58. O = s(file, r"\.c",r".h")
  59. once = s(string.translate(O,tr_cpp), r"_+",r"_")
  60. print once,
  61. if len(once) < 27 : print " " * (27 - len(once)),
  62. prg = s(sys.argv[0], r".*/([^/]*)$", r"\1")
  63. out = ""
  64. out += "#ifndef "+once+"\n"
  65. out += "#define "+once+" "+"%i"%time.time()+"\n"
  66. out += "/* generated"+time.strftime(" %Y-%m%d-%H%M ", time.localtime())+\
  67. prg+" "+str(sys.argv[1:])+" */\n\n"
  68. T = ""
  69. try:
  70. F = open(sys.argv[1],"r")
  71. except IOError, x:
  72. print "no input text:",sys.argv[1],":",str(x)
  73. sys.exit()
  74. else:
  75. T = F.read()
  76. F.close()
  77. ext = ""
  78. def ext_append(text): global ext; ext += text ; return ""
  79. def out_append(text): global out; out += text ; return ""
  80. T = s(T, r"(?sx) (\#ifn?def\s+_export\s+ (?:\#define\s+_export\s+)?) "
  81. r" ((?:.(?!\#endif))*.) "
  82. r" (\#endif) ",
  83. lambda x : ext_append(x.group(2)))
  84. if not m(ext, r"^\s*\#\s*include\s*[\<\"]"):
  85. out_append("#include "+inc+"\n")
  86. ext = s(ext, r"(?x) ( \#\s*include\s*[\<\"][^<>\"]*[\"\>]\s* ) ",
  87. lambda x : out_append(x.group(1)))
  88. T = s(T, r"(?sx) ^ ( \/\*\*\s (?:.(?!\*\/))* .\*\/ ) ",
  89. lambda x : out_append("\n"+x.group(1)+"\n"))
  90. out += "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"
  91. out += "\n"+ext+"\n"
  92. def q1_(a,b,c,d):
  93. global out
  94. if a is None: a = ""
  95. if not m(b, r"(static|extern)"):
  96. b = s(b, r"_export", r"_extern")
  97. if not m(b, r"extern\s*$"): b += " extern "
  98. b = s(b, r"^\s*", r"\n")
  99. b = s(b, r"\s*$", r" ")
  100. c = "P4_CODE"
  101. d = s(d, r"(?s)\s+", r" ")
  102. if a: a = "\n"+a
  103. out += a+b+c+d+";\n"
  104. return ""
  105. T = s(T, r"(?sx) (\/\*\*\s(?:.(?!\*\/))*.\*\/)?"
  106. r" (\s*(?:_export|static|extern)?\s) \w+ \s"
  107. r" \b (FXCode|FXCode_XE|FXCode_RT|P4_CODE) (\s*\(\s* \w+ \))",
  108. lambda x : q1_(x.group(1), x.group(2), x.group(3), x.group(4)))
  109. def q2_(a,b,c,d):
  110. global out
  111. if a is None: a = ""
  112. if not m(b, r"(static|extern)$"):
  113. b = s(b, r"_export", r"_extern")
  114. if not m(b, r"extern\s*$"): b += " extern "
  115. b = s(b, r"^\s*", r"\n")
  116. b = s(b, r"\s*$", r" ")
  117. c = s(c, r"(?s)\s+", r" ")
  118. d = "; /*"+d+"*/"
  119. if a : a = "\n"+a
  120. out += a+b+c+d+"\n"
  121. return ""
  122. T = s(T, r"(?sx) (/\*\*\s(?:.(?!\*\/))*.\*\/)? "
  123. r" (\s*_export) \b ([^;{}=]+) ([;{}=]) ",
  124. lambda x : q2_(x.group(1), x.group(2), x.group(3), x.group(4)))
  125. out += "\n#ifdef __cplusplus\n} /* extern \"C\" */\n#endif\n\n"
  126. out += "\n#endif\n"
  127. try:
  128. F = open (O, "w")
  129. except IOError, x:
  130. print "could open output file:", str(x)
  131. sys.exit()
  132. else:
  133. F.write(out)
  134. F.close()
  135. count = 0
  136. def counting(): global count ; count += 1 ; return ""
  137. s(out, r"extern", lambda x : counting() )
  138. print " " * (4 - len("%i"%count)),count," extern ", inc