swr/rast: Cleanup and generalize gen_archrast
authorAlok Hota <alok.hota@intel.com>
Fri, 31 Aug 2018 17:13:56 +0000 (12:13 -0500)
committerAlok Hota <alok.hota@intel.com>
Wed, 1 May 2019 15:11:30 +0000 (15:11 +0000)
- Update meson.build
  - Includes current_build_dir() fix
    meson/swr: replace hard-coded path with current_build_dir()

Fixes: 93cd9905c8fbb98985ae "swr/rast: Cleanup and generalize gen_archrast"
Signed-off-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Alok Hota <alok.hota@intel.com>
Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
  - Clean up meson.build (remove foreach loop, replace with single call)
- Update SConscript
  - use `$SOURCES` to call `CodeGenerate` with multiple source files

Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
src/gallium/drivers/swr/SConscript
src/gallium/drivers/swr/rasterizer/codegen/gen_archrast.py
src/gallium/drivers/swr/rasterizer/codegen/meson.build

index a89d02c5db0828b8acf6013907cc90489ea69f15..b0bead242f7812e3093147de9acee0a6be7f98a8 100644 (file)
@@ -103,44 +103,26 @@ Depends('rasterizer/jitter/gen_state_llvm.h',
         swrroot + 'rasterizer/codegen/templates/gen_llvm.hpp')
 
 env.CodeGenerate(
-    target = 'rasterizer/archrast/gen_ar_event.hpp',
     script = swrroot + 'rasterizer/codegen/gen_archrast.py',
-    source = 'rasterizer/archrast/events.proto',
-    command = python_cmd + ' $SCRIPT --proto $SOURCE --proto_private ' + srcroot + '/rasterizer/archrast/events_private.proto --output $TARGET --gen_event_hpp'
-)
+    target = ['rasterizer/archrast/gen_ar_event.hpp',
+              'rasterizer/archrast/gen_ar_event.cpp',
+              'rasterizer/archrast/gen_ar_eventhandler.hpp',
+              'rasterizer/archrast/gen_ar_eventhandlerfile.hpp'],
+    source = [srcroot + '/rasterizer/archrast/events.proto',
+              srcroot + '/rasterizer/archrast/events_private.proto'],
+    command = python_cmd + ' $SCRIPT --proto $SOURCES --output-dir ' + bldroot + '/rasterizer/archrast')
 Depends('rasterizer/archrast/gen_ar_event.hpp',
         swrroot + 'rasterizer/archrast/events_private.proto')
 Depends('rasterizer/jitter/gen_state_llvm.h',
         swrroot + 'rasterizer/codegen/templates/gen_ar_event.hpp')
-
-env.CodeGenerate(
-    target = 'rasterizer/archrast/gen_ar_event.cpp',
-    script = swrroot + 'rasterizer/codegen/gen_archrast.py',
-    source = 'rasterizer/archrast/events.proto',
-    command = python_cmd + ' $SCRIPT --proto $SOURCE --proto_private ' + srcroot + '/rasterizer/archrast/events_private.proto --output $TARGET --gen_event_cpp'
-)
 Depends('rasterizer/archrast/gen_ar_event.cpp',
         swrroot + 'rasterizer/archrast/events_private.proto')
 Depends('rasterizer/jitter/gen_state_llvm.h',
         swrroot + 'rasterizer/codegen/templates/gen_ar_event.cpp')
-
-env.CodeGenerate(
-    target = 'rasterizer/archrast/gen_ar_eventhandler.hpp',
-    script = swrroot + 'rasterizer/codegen/gen_archrast.py',
-    source = 'rasterizer/archrast/events.proto',
-    command = python_cmd + ' $SCRIPT --proto $SOURCE --proto_private ' + srcroot + '/rasterizer/archrast/events_private.proto --output $TARGET --gen_eventhandler_hpp'
-)
 Depends('rasterizer/archrast/gen_ar_eventhandler.hpp',
         swrroot + 'rasterizer/archrast/events_private.proto')
 Depends('rasterizer/jitter/gen_state_llvm.h',
         swrroot + 'rasterizer/codegen/templates/gen_ar_eventhandler.hpp')
-
-env.CodeGenerate(
-    target = 'rasterizer/archrast/gen_ar_eventhandlerfile.hpp',
-    script = swrroot + 'rasterizer/codegen/gen_archrast.py',
-    source = 'rasterizer/archrast/events.proto',
-    command = python_cmd + ' $SCRIPT --proto $SOURCE --proto_private ' + srcroot + '/rasterizer/archrast/events_private.proto --output $TARGET --gen_eventhandlerfile_hpp'
-)
 Depends('rasterizer/archrast/gen_ar_eventhandlerfile.hpp',
         swrroot + 'rasterizer/archrast/events_private.proto')
 Depends('rasterizer/jitter/gen_state_llvm.h',
index c5842aa48d24f82f2876f7d71a83da9d0ac52dc5..7c953801a916855d467b3b3c88595bb11104915c 100644 (file)
@@ -116,37 +116,17 @@ def main():
 
     # Parse args...
     parser = ArgumentParser()
-    parser.add_argument('--proto', '-p', help='Path to proto file', required=True)
-    parser.add_argument('--proto_private', '-pp', help='Path to private proto file', required=True)
-    parser.add_argument('--output', '-o', help='Output filename (i.e. event.hpp)', required=True)
-    parser.add_argument('--gen_event_hpp', help='Generate event header', action='store_true', default=False)
-    parser.add_argument('--gen_event_cpp', help='Generate event cpp', action='store_true', default=False)
-    parser.add_argument('--gen_eventhandler_hpp', help='Generate eventhandler header', action='store_true', default=False)
-    parser.add_argument('--gen_eventhandlerfile_hpp', help='Generate eventhandler header for writing to files', action='store_true', default=False)
+    parser.add_argument('--proto', '-p', dest="protos", nargs='+', help='Path to all proto file(s) to process. Accepts one or more paths (i.e. events.proto and events_private.proto)', required=True)
+    parser.add_argument('--output-dir', help='Output dir (defaults to ./codegen). Will create folder if it does not exist.', required=False, default='codegen')
     args = parser.parse_args()
 
-    proto_filename = args.proto
-    proto_private_filename = args.proto_private
+    if not os.path.exists(args.output_dir):
+        MakeDir(args.output_dir)
 
-    (output_dir, output_filename) = os.path.split(args.output)
-
-    if not output_dir:
-        output_dir = '.'
-
-    #print('output_dir = %s' % output_dir, file=sys.stderr)
-    #print('output_filename = %s' % output_filename, file=sys.stderr)
-
-    if not os.path.exists(proto_filename):
-        print('Error: Could not find proto file %s' % proto_filename, file=sys.stderr)
-        return 1
-
-    if not os.path.exists(proto_private_filename):
-        print('Error: Could not find private proto file %s' % proto_private_filename, file=sys.stderr)
-        return 1
-
-    final_output_dir = output_dir
-    MakeDir(final_output_dir)
-    output_dir = MakeTmpDir('_codegen')
+    for f in args.protos:
+        if not os.path.exists(f):
+            print('Error: Could not find proto file %s' % f, file=sys.stderr)
+            return 1
 
     protos = {}
     protos['events'] = {}       # event dictionary containing events with their fields
@@ -154,66 +134,48 @@ def main():
     protos['enums'] = {}
     protos['enum_names'] = []
 
-    parse_protos(protos, proto_filename)
-    parse_protos(protos, proto_private_filename)
+    # Parse each proto file and add to protos container
+    for f in args.protos:
+        print("Parsing proto file: %s" % os.path.normpath(f))
+        parse_protos(protos, f)
+
+    files = [
+        ["gen_ar_event.hpp", ""],
+        ["gen_ar_event.cpp", ""],
+        ["gen_ar_eventhandler.hpp", "gen_ar_event.hpp"],
+        ["gen_ar_eventhandlerfile.hpp", "gen_ar_eventhandler.hpp"]
+    ]
 
     rval = 0
 
     try:
-        # Generate event header
-        if args.gen_event_hpp:
+        # Delete existing files
+        for f in files:
+            filename = f[0]
+            output_fullpath = os.path.join(args.output_dir, filename)
+            if os.path.exists(output_fullpath):
+                print("Deleting existing file: %s" % output_fullpath)
+                os.remove(output_fullpath)
+
+        # Generate files from templates
+        for f in files:
+            filename = f[0]
+            event_header = f[1]
             curdir = os.path.dirname(os.path.abspath(__file__))
-            template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.hpp'])
-            output_fullpath = os.sep.join([output_dir, output_filename])
+            template_file = os.path.join(curdir, 'templates', filename)
+            output_fullpath = os.path.join(args.output_dir, filename)
 
+            print("Generating: %s" % output_fullpath)
             MakoTemplateWriter.to_file(template_file, output_fullpath,
                     cmdline=sys.argv,
-                    filename=output_filename,
-                    protos=protos)
+                    filename=filename,
+                    protos=protos,
+                    event_header=event_header)
 
-        # Generate event implementation
-        if args.gen_event_cpp:
-            curdir = os.path.dirname(os.path.abspath(__file__))
-            template_file = os.sep.join([curdir, 'templates', 'gen_ar_event.cpp'])
-            output_fullpath = os.sep.join([output_dir, output_filename])
-
-            MakoTemplateWriter.to_file(template_file, output_fullpath,
-                    cmdline=sys.argv,
-                    filename=output_filename,
-                    protos=protos)
-
-        # Generate event handler header
-        if args.gen_eventhandler_hpp:
-            curdir = os.path.dirname(os.path.abspath(__file__))
-            template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandler.hpp'])
-            output_fullpath = os.sep.join([output_dir, output_filename])
-
-            MakoTemplateWriter.to_file(template_file, output_fullpath,
-                    cmdline=sys.argv,
-                    filename=output_filename,
-                    event_header='gen_ar_event.hpp',
-                    protos=protos)
-
-        # Generate event handler header
-        if args.gen_eventhandlerfile_hpp:
-            curdir = os.path.dirname(os.path.abspath(__file__))
-            template_file = os.sep.join([curdir, 'templates', 'gen_ar_eventhandlerfile.hpp'])
-            output_fullpath = os.sep.join([output_dir, output_filename])
-
-            MakoTemplateWriter.to_file(template_file, output_fullpath,
-                    cmdline=sys.argv,
-                    filename=output_filename,
-                    event_header='gen_ar_eventhandler.hpp',
-                    protos=protos)
-
-        rval = CopyDirFilesIfDifferent(output_dir, final_output_dir)
-
-    except:
+    except Exception as e:
+        print(e)
         rval = 1
 
-    finally:
-        DeleteDirTree(output_dir)
-
     return rval
 
 if __name__ == '__main__':
index 28275176022277a48a6693420b45ebaeb29895f1..98da7d3fc61e722ca425e985f61a07e906054095 100644 (file)
@@ -61,26 +61,16 @@ foreach x : [[swr_context_files, 'gen_swr_context_llvm.h'],
   )
 endforeach
 
-foreach x : [['gen_ar_event.hpp', '--gen_event_hpp'],
-             ['gen_ar_event.cpp', '--gen_event_cpp'],
-             ['gen_ar_eventhandler.hpp', '--gen_eventhandler_h'],
-             ['gen_ar_eventhandlerfile.hpp', '--gen_eventhandlerfile_h']]
-  files_swr_common += custom_target(
-    x[0],
-    input : ['gen_archrast.py', swr_event_proto_files, swr_event_pproto_files],
-    output : x[0],
-    command : [
-      prog_python,
-      '@INPUT0@',
-      '--proto', '@INPUT1@',
-      '--proto_private', '@INPUT2@',
-      '--output', '@OUTPUT@',
-      x[1],
-    ],
-    depend_files : files(
-      join_paths('templates', x[0]),
-      'gen_common.py',
-    ),
-  )
+ar_output_filenames = ['gen_ar_event.hpp', 'gen_ar_event.cpp', 'gen_ar_eventhandler.hpp', 'gen_ar_eventhandlerfile.hpp']
+ar_template_filenames = []
+foreach fname : ar_output_filenames
+    ar_template_filenames += join_paths('templates', fname)
 endforeach
 
+files_swr_common += custom_target(
+    'gen_archrast',
+    input : ['gen_archrast.py', swr_event_proto_files, swr_event_pproto_files],
+    output : ar_output_filenames,
+    command : [prog_python, '@INPUT0@', '--proto', '@INPUT1@', '@INPUT2@', '--output-dir', meson.current_build_dir()],
+    depend_files : files('gen_common.py', ar_template_filenames)
+)