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