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: |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 114 | ofinput = frontend.create_ofinput(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 | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 134 | for filename in filenames: |
| 135 | log("Processing struct file: " + filename) |
| 136 | ofinput = process_input_file(filename) |
| 137 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 138 | for wire_version in ofinput.wire_versions: |
Rich Lane | c9e111d | 2013-05-09 16:10:30 -0700 | [diff] [blame] | 139 | ofinputs_by_version[wire_version].append(ofinput) |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 140 | return ofinputs_by_version |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 141 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 142 | def build_ir(ofinputs_by_version): |
| 143 | classes = [] |
| 144 | enums = [] |
| 145 | for wire_version, ofinputs in ofinputs_by_version.items(): |
| 146 | version = OFVersions.from_wire(wire_version) |
| 147 | ofprotocol = loxi_ir.build_protocol(version, ofinputs) |
| 148 | loxi_globals.ir[version] = ofprotocol |
Rich Lane | 4d9f0f6 | 2013-05-09 15:50:57 -0700 | [diff] [blame] | 149 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 150 | loxi_globals.unified = loxi_ir.build_unified_ir(loxi_globals.ir) |
Rich Lane | 38388e6 | 2013-04-08 14:09:46 -0700 | [diff] [blame] | 151 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 152 | ################################################################ |
| 153 | # |
| 154 | # Debug |
| 155 | # |
| 156 | ################################################################ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 157 | |
| 158 | if __name__ == '__main__': |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 159 | (options, args, target_versions) = cmdline.process_commandline() |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 160 | # @fixme Use command line params to select log |
| 161 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 162 | logging.basicConfig(level = logging.INFO if not options.verbose else logging.DEBUG) |
| 163 | |
| 164 | # Import the language file |
| 165 | lang_file = "lang_%s" % options.lang |
| 166 | lang_module = __import__(lang_file) |
| 167 | |
| 168 | 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] | 169 | debug("Config sanity check failed\n") |
| 170 | sys.exit(1) |
| 171 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 172 | # If list files, just list auto-gen files to stdout and exit |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 173 | if options.list_files: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 174 | for name in lang_module.targets: |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 175 | print options.install_dir + '/' + name |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 176 | sys.exit(0) |
| 177 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 178 | log("\nGenerating files for target language %s\n" % options.lang) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 179 | |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 180 | loxi_globals.OFVersions.target_versions = target_versions |
| 181 | inputs = read_input() |
| 182 | build_ir(inputs) |
| 183 | #log_all_class_info() |
| 184 | lang_module.generate(options.install_dir) |