YAMAMOTO Takashi | 6df0abb | 2013-07-01 17:24:04 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 2 | # Copyright 2013, Big Switch Networks, Inc. |
| 3 | # |
| 4 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 5 | # the following special exception: |
| 6 | # |
| 7 | # LOXI Exception |
| 8 | # |
| 9 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 10 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 11 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 12 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 13 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 14 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 15 | # |
| 16 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 17 | # |
| 18 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 19 | # a copy of the EPL at: |
| 20 | # |
| 21 | # http://www.eclipse.org/legal/epl-v10.html |
| 22 | # |
| 23 | # Unless required by applicable law or agreed to in writing, software |
| 24 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 26 | # EPL for the specific language governing permissions and limitations |
| 27 | # under the EPL. |
| 28 | |
| 29 | """ |
| 30 | @brief |
| 31 | Process openflow header files to create language specific LOXI interfaces |
| 32 | |
| 33 | First cut at simple python script for processing input files |
| 34 | |
| 35 | Internal notes |
| 36 | |
| 37 | An input file for each supported OpenFlow version is passed in |
| 38 | on the command line. |
| 39 | |
| 40 | Expected input file format: |
| 41 | |
| 42 | These will probably be collapsed into a python dict or something |
| 43 | |
| 44 | The first line has the ofC version identifier and nothing else |
| 45 | The second line has the openflow wire protocol value and nothing else |
| 46 | |
| 47 | The main content is struct elements for each OF recognized class. |
| 48 | These are taken from current versions of openflow.h but are modified |
| 49 | a bit. See Overview for more information. |
| 50 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 51 | Class canonical form: A list of entries, each of which is a |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 52 | pair "type, name;". The exception is when type is the keyword |
| 53 | 'list' in which the syntax is "list(type) name;". |
| 54 | |
| 55 | From this, internal representations are generated: For each |
| 56 | version, a dict indexed by class name. One element (members) is |
| 57 | an array giving the member name and type. From this, wire offsets |
| 58 | can be calculated. |
| 59 | |
| 60 | |
| 61 | @fixme Clean up the lang module architecture. It should provide a |
| 62 | list of files that it wants to generate and maps to the filenames, |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 63 | subdirectory names and generation functions. It should also be |
| 64 | defined as a class, probably with the constructor taking the |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 65 | language target. |
| 66 | |
| 67 | @fixme Clean up global data structures such as versions and of_g |
| 68 | structures. They should probably be a class or classes as well. |
| 69 | |
| 70 | """ |
| 71 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 72 | from collections import OrderedDict, defaultdict |
| 73 | import copy |
| 74 | import glob |
| 75 | from optparse import OptionParser |
| 76 | import os |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 77 | import re |
| 78 | import string |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 79 | import sys |
| 80 | |
| 81 | import cmdline |
| 82 | from loxi_globals import OFVersions |
| 83 | import loxi_globals |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 84 | import loxi_utils.loxi_utils as loxi_utils |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 85 | import pyparsing |
| 86 | import loxi_front_end.parser as parser |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 87 | import loxi_front_end.frontend as frontend |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 88 | import loxi_ir |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 89 | from generic_utils import * |
| 90 | |
| 91 | root_dir = os.path.dirname(os.path.realpath(__file__)) |
| 92 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 93 | def process_input_file(filename): |
| 94 | """ |
| 95 | Process an input file |
| 96 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 97 | Does not modify global state. |
| 98 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 99 | @param filename The input filename |
| 100 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 101 | @returns An OFInput object |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 102 | """ |
| 103 | |
| 104 | # Parse the input file |
| 105 | try: |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 106 | with open(filename, 'r') as f: |
| 107 | ast = parser.parse(f.read()) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 108 | except pyparsing.ParseBaseException as e: |
| 109 | print "Parse error in %s: %s" % (os.path.basename(filename), str(e)) |
| 110 | sys.exit(1) |
| 111 | |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 112 | # Create the OFInput from the AST |
| 113 | try: |
Rich Lane | 72875d6 | 2013-11-13 12:39:53 -0800 | [diff] [blame] | 114 | ofinput = frontend.create_ofinput(os.path.basename(filename), ast) |
Rich Lane | d47e5a2 | 2013-05-09 14:21:16 -0700 | [diff] [blame] | 115 | except frontend.InputError as e: |
| 116 | print "Error in %s: %s" % (os.path.basename(filename), str(e)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 117 | sys.exit(1) |
| 118 | |
| 119 | return ofinput |
| 120 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 121 | def read_input(): |
| 122 | """ |
| 123 | Read in from files given on command line and update global state |
| 124 | |
| 125 | @fixme Should select versions to support from command line |
| 126 | """ |
| 127 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 128 | ofinputs_by_version = defaultdict(lambda: []) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 129 | filenames = sorted(glob.glob("%s/openflow_input/*" % root_dir)) |
| 130 | |
Rich Lane | d23c013 | 2013-05-16 16:18:54 -0700 | [diff] [blame] | 131 | # Ignore emacs backup files |
| 132 | filenames = [x for x in filenames if not x.endswith('~')] |
| 133 | |
Rich Lane | d798755 | 2013-11-13 10:38:54 -0800 | [diff] [blame] | 134 | # Read input files |
| 135 | all_ofinputs = [] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 136 | for filename in filenames: |
| 137 | log("Processing struct file: " + filename) |
| 138 | ofinput = process_input_file(filename) |
| 139 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 140 | for wire_version in ofinput.wire_versions: |
Rich Lane | c9e111d | 2013-05-09 16:10:30 -0700 | [diff] [blame] | 141 | ofinputs_by_version[wire_version].append(ofinput) |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 142 | return ofinputs_by_version |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 143 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 144 | def build_ir(ofinputs_by_version): |
| 145 | classes = [] |
| 146 | enums = [] |
| 147 | for wire_version, ofinputs in ofinputs_by_version.items(): |
| 148 | version = OFVersions.from_wire(wire_version) |
| 149 | ofprotocol = loxi_ir.build_protocol(version, ofinputs) |
| 150 | loxi_globals.ir[version] = ofprotocol |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 151 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 152 | loxi_globals.unified = loxi_ir.build_unified_ir(loxi_globals.ir) |
Rich Lane | 38388e6 | 2013-04-08 14:09:46 -0700 | [diff] [blame] | 153 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 154 | ################################################################ |
| 155 | # |
| 156 | # Debug |
| 157 | # |
| 158 | ################################################################ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 159 | |
| 160 | if __name__ == '__main__': |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 161 | (options, args, target_versions) = cmdline.process_commandline() |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 162 | # @fixme Use command line params to select log |
| 163 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 164 | logging.basicConfig(level = logging.INFO if not options.verbose else logging.DEBUG) |
| 165 | |
| 166 | # Import the language file |
| 167 | lang_file = "lang_%s" % options.lang |
| 168 | lang_module = __import__(lang_file) |
| 169 | |
| 170 | if hasattr(lang_module, "config_sanity_check") and not lang_module.config_sanity_check(): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 171 | debug("Config sanity check failed\n") |
| 172 | sys.exit(1) |
| 173 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 174 | # If list files, just list auto-gen files to stdout and exit |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 175 | if options.list_files: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 176 | for name in lang_module.targets: |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 177 | print options.install_dir + '/' + name |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 178 | sys.exit(0) |
| 179 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 180 | log("\nGenerating files for target language %s\n" % options.lang) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 181 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 182 | loxi_globals.OFVersions.target_versions = target_versions |
| 183 | inputs = read_input() |
| 184 | build_ir(inputs) |
| 185 | #log_all_class_info() |
| 186 | lang_module.generate(options.install_dir) |