boost_mpl_preprocess.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # Copyright Deniz Bahadir 2015
  2. #
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. #
  7. # See http://www.boost.org/libs/mpl for documentation.
  8. # See http://stackoverflow.com/a/20660264/3115457 for further information.
  9. # See http://stackoverflow.com/a/29627158/3115457 for further information.
  10. import fix_boost_mpl_preprocess as fixmpl
  11. import argparse
  12. import sys
  13. import os
  14. import os.path
  15. import re
  16. import fileinput
  17. import shutil
  18. def create_more_container_files(sourceDir, suffix, maxElements, containers, containers2):
  19. """Creates additional files for the individual MPL-containers."""
  20. # Create files for each MPL-container with 20 to 'maxElements' elements
  21. # which will be used during generation.
  22. for container in containers:
  23. for i in range(20, maxElements, 10):
  24. # Create copy of "template"-file.
  25. newFile = os.path.join( sourceDir, container, container + str(i+10) + suffix )
  26. shutil.copyfile( os.path.join( sourceDir, container, container + "20" + suffix ), newFile )
  27. # Adjust copy of "template"-file accordingly.
  28. for line in fileinput.input( newFile, inplace=1, mode="rU" ):
  29. line = re.sub(r'20', '%TWENTY%', line.rstrip())
  30. line = re.sub(r'11', '%ELEVEN%', line.rstrip())
  31. line = re.sub(r'10(?![0-9])', '%TEN%', line.rstrip())
  32. line = re.sub(r'%TWENTY%', re.escape(str(i+10)), line.rstrip())
  33. line = re.sub(r'%ELEVEN%', re.escape(str(i + 1)), line.rstrip())
  34. line = re.sub(r'%TEN%', re.escape(str(i)), line.rstrip())
  35. print(line)
  36. for container in containers2:
  37. for i in range(20, maxElements, 10):
  38. # Create copy of "template"-file.
  39. newFile = os.path.join( sourceDir, container, container + str(i+10) + "_c" + suffix )
  40. shutil.copyfile( os.path.join( sourceDir, container, container + "20_c" + suffix ), newFile )
  41. # Adjust copy of "template"-file accordingly.
  42. for line in fileinput.input( newFile, inplace=1, mode="rU" ):
  43. line = re.sub(r'20', '%TWENTY%', line.rstrip())
  44. line = re.sub(r'11', '%ELEVEN%', line.rstrip())
  45. line = re.sub(r'10(?![0-9])', '%TEN%', line.rstrip())
  46. line = re.sub(r'%TWENTY%', re.escape(str(i+10)), line.rstrip())
  47. line = re.sub(r'%ELEVEN%', re.escape(str(i + 1)), line.rstrip())
  48. line = re.sub(r'%TEN%', re.escape(str(i)), line.rstrip())
  49. print(line)
  50. def create_input_for_numbered_sequences(headerDir, sourceDir, containers, maxElements):
  51. """Creates additional source- and header-files for the numbered sequence MPL-containers."""
  52. # Create additional container-list without "map".
  53. containersWithoutMap = containers[:]
  54. try:
  55. containersWithoutMap.remove('map')
  56. except ValueError:
  57. # We can safely ignore if "map" is not contained in 'containers'!
  58. pass
  59. # Create header/source-files.
  60. create_more_container_files(headerDir, ".hpp", maxElements, containers, containersWithoutMap)
  61. create_more_container_files(sourceDir, ".cpp", maxElements, containers, containersWithoutMap)
  62. def adjust_container_limits_for_variadic_sequences(headerDir, containers, maxElements):
  63. """Adjusts the limits of variadic sequence MPL-containers."""
  64. for container in containers:
  65. headerFile = os.path.join( headerDir, "limits", container + ".hpp" )
  66. regexMatch = r'(define\s+BOOST_MPL_LIMIT_' + container.upper() + r'_SIZE\s+)[0-9]+'
  67. regexReplace = r'\g<1>' + re.escape( str(maxElements) )
  68. for line in fileinput.input( headerFile, inplace=1, mode="rU" ):
  69. line = re.sub(regexMatch, regexReplace, line.rstrip())
  70. print(line)
  71. def current_boost_dir():
  72. """Returns the (relative) path to the Boost source-directory this file is located in (if any)."""
  73. # Path to directory containing this script.
  74. path = os.path.dirname( os.path.realpath(__file__) )
  75. # Making sure it is located in "${boost-dir}/libs/mpl/preprocessed".
  76. for directory in reversed( ["libs", "mpl", "preprocessed"] ):
  77. (head, tail) = os.path.split(path)
  78. if tail == directory:
  79. path = head
  80. else:
  81. return None
  82. return os.path.relpath( path )
  83. def to_positive_multiple_of_10(string):
  84. """Converts a string into its encoded positive integer (greater zero) or throws an exception."""
  85. try:
  86. value = int(string)
  87. except ValueError:
  88. msg = '"%r" is not a positive multiple of 10 (greater zero).' % string
  89. raise argparse.ArgumentTypeError(msg)
  90. if value <= 0 or value % 10 != 0:
  91. msg = '"%r" is not a positive multiple of 10 (greater zero).' % string
  92. raise argparse.ArgumentTypeError(msg)
  93. return value
  94. def to_existing_absolute_path(string):
  95. """Converts a path into its absolute path and verifies that it exists or throws an exception."""
  96. value = os.path.abspath(string)
  97. if not os.path.exists( value ) or not os.path.isdir( value ):
  98. msg = '"%r" is not a valid path to a directory.' % string
  99. raise argparse.ArgumentTypeError(msg)
  100. return value
  101. def main():
  102. """The main function."""
  103. # Find the current Boost source-directory in which this script is located.
  104. sourceDir = current_boost_dir()
  105. if sourceDir == None:
  106. sourceDir = ""
  107. # Prepare and run cmdline-parser.
  108. cmdlineParser = argparse.ArgumentParser(description="A generator-script for pre-processed Boost.MPL headers.")
  109. cmdlineParser.add_argument("-v", "--verbose", dest='verbose', action='store_true',
  110. help="Be a little bit more verbose.")
  111. cmdlineParser.add_argument("-s", "--sequence-type", dest='seqType', choices=['variadic', 'numbered', 'both'],
  112. default='both',
  113. help="Only update pre-processed headers for the selected sequence types, "
  114. "either 'numbered' sequences, 'variadic' sequences or 'both' sequence "
  115. "types. (Default=both)")
  116. cmdlineParser.add_argument("--no-vector", dest='want_vector', action='store_false',
  117. help="Do not update pre-processed headers for Boost.MPL Vector.")
  118. cmdlineParser.add_argument("--no-list", dest='want_list', action='store_false',
  119. help="Do not update pre-processed headers for Boost.MPL List.")
  120. cmdlineParser.add_argument("--no-set", dest='want_set', action='store_false',
  121. help="Do not update pre-processed headers for Boost.MPL Set.")
  122. cmdlineParser.add_argument("--no-map", dest='want_map', action='store_false',
  123. help="Do not update pre-processed headers for Boost.MPL Map.")
  124. cmdlineParser.add_argument("--num-elements", dest='numElements', metavar="<num-elements>",
  125. type=to_positive_multiple_of_10, default=100,
  126. help="The maximal number of elements per container sequence. (Default=100)")
  127. cmdlineParser.add_argument(dest='sourceDir', metavar="<source-dir>", default=current_boost_dir(), nargs='?',
  128. type=to_existing_absolute_path,
  129. help="The source-directory of Boost. (Default=\"" + sourceDir + "\")")
  130. args = cmdlineParser.parse_args()
  131. # Some verbose debug output.
  132. if args.verbose:
  133. print "Arguments extracted from command-line:"
  134. print " verbose = ", args.verbose
  135. print " source directory = ", args.sourceDir
  136. print " num elements = ", args.numElements
  137. print " sequence type = ", args.seqType
  138. print " want: vector = ", args.want_vector
  139. print " want: list = ", args.want_list
  140. print " want: set = ", args.want_set
  141. print " want: map = ", args.want_map
  142. # Verify that we received any source-directory.
  143. if args.sourceDir == None:
  144. print "You should specify a valid path to the Boost source-directory."
  145. sys.exit(0)
  146. # The directories for header- and source files of Boost.MPL.
  147. # NOTE: Assuming 'args.sourceDir' is the source-directory of the entire boost project.
  148. headerDir = os.path.join( args.sourceDir, "boost", "mpl" )
  149. sourceDir = os.path.join( args.sourceDir, "libs", "mpl", "preprocessed" )
  150. # Check that the header/source-directories exist.
  151. if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
  152. # Maybe 'args.sourceDir' is not the source-directory of the entire boost project
  153. # but instead of the Boost.MPL git-directory, only?
  154. headerDir = os.path.join( args.sourceDir, "include", "boost", "mpl" )
  155. sourceDir = os.path.join( args.sourceDir, "preprocessed" )
  156. if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
  157. cmdlineParser.print_usage()
  158. print "error: Cannot find Boost.MPL header/source files in given Boost source-directory!"
  159. sys.exit(0)
  160. # Some verbose debug output.
  161. if args.verbose:
  162. print "Chosen header-directory: ", headerDir
  163. print "Chosen source-directory: ", sourceDir
  164. # Create list of containers for which files shall be pre-processed.
  165. containers = []
  166. if args.want_vector:
  167. containers.append('vector')
  168. if args.want_list:
  169. containers.append('list')
  170. if args.want_set:
  171. containers.append('set')
  172. if args.want_map:
  173. containers.append('map')
  174. if containers == []:
  175. print "Nothing to do."
  176. print "(Why did you prevent generating pre-processed headers for all Boost.MPL container types?)"
  177. sys.exit(0)
  178. # Possibly fix the header-comments of input-files needed for pre-processing.
  179. if args.verbose:
  180. print "Checking if prior to pre-processing some input-files need fixing."
  181. needFixing = fixmpl.check_input_files(headerDir, sourceDir, containers, args.seqType, args.verbose)
  182. if needFixing:
  183. if args.verbose:
  184. print "Fixing of some input-files prior to pre-processing is needed."
  185. print "Will fix them now!"
  186. fixmpl.fix_input_files(headerDir, sourceDir, containers, args.seqType, args.verbose)
  187. # Some verbose debug output.
  188. if args.verbose:
  189. print "Containers for which to pre-process headers: ", containers
  190. # Create (additional) input files for generating pre-processed headers of numbered sequence MPL containers.
  191. if args.seqType == "both" or args.seqType == "numbered":
  192. create_input_for_numbered_sequences(headerDir, sourceDir, containers, args.numElements)
  193. # Modify settings for generating pre-processed headers of variadic sequence MPL containers.
  194. if args.seqType == "both" or args.seqType == "variadic":
  195. adjust_container_limits_for_variadic_sequences(headerDir, containers, args.numElements)
  196. # Generate MPL-preprocessed files.
  197. os.chdir( sourceDir )
  198. if args.seqType == "both" or args.seqType == "numbered":
  199. if args.want_vector:
  200. if args.verbose:
  201. print "Pre-process headers for Boost.MPL numbered vectors."
  202. os.system( "python " + os.path.join( sourceDir, "preprocess_vector.py" ) + " all " + args.sourceDir )
  203. if args.want_list:
  204. if args.verbose:
  205. print "Pre-process headers for Boost.MPL numbered lists."
  206. os.system( "python " + os.path.join( sourceDir, "preprocess_list.py" ) + " all " + args.sourceDir )
  207. if args.want_set:
  208. if args.verbose:
  209. print "Pre-process headers for Boost.MPL numbered sets."
  210. os.system( "python " + os.path.join( sourceDir, "preprocess_set.py" ) + " all " + args.sourceDir )
  211. if args.want_map:
  212. if args.verbose:
  213. print "Pre-process headers for Boost.MPL numbered maps."
  214. os.system( "python " + os.path.join( sourceDir, "preprocess_map.py" ) + " all " + args.sourceDir )
  215. if args.seqType == "both" or args.seqType == "variadic":
  216. if args.verbose:
  217. print "Pre-process headers for Boost.MPL variadic containers."
  218. os.system( "python " + os.path.join( sourceDir, "preprocess.py" ) + " all " + args.sourceDir )
  219. if __name__ == '__main__':
  220. main()