Make paths to grammar output files
authorDavid Neto <dneto@google.com>
Tue, 12 Apr 2016 20:09:13 +0000 (16:09 -0400)
committerDavid Neto <dneto@google.com>
Tue, 12 Apr 2016 20:09:13 +0000 (16:09 -0400)
The directory for a grammar output file will be created if
it doesn't already exist.

This is required to make the Android.mk build work.

utils/generate_grammar_tables.py

index 0046083..0cbe969 100755 (executable)
@@ -5,6 +5,7 @@ from __future__ import print_function
 
 import functools
 import json
+import os.path
 import re
 
 # Prefix for all C variables generated by this script.
@@ -13,6 +14,18 @@ PYGEN_VARIABLE_PREFIX = 'pygen_variable'
 CAPABILITY_BIT_MAPPING = {}
 
 
+def make_path_to_file(f):
+    """Makes all ancestor directories to the given file, if they
+    don't yet exist.
+
+    Arguments:
+        f: The file whose ancestor directories are to be created.
+    """
+    dir = os.path.dirname(os.path.abspath(f))
+    if not os.path.isdir(dir):
+        os.makedirs(dir)
+
+
 def populate_capability_bit_mapping_dict(cap_dict):
     """Populates CAPABILITY_BIT_MAPPING.
 
@@ -361,6 +374,8 @@ def main():
         populate_capability_bit_mapping_dict(cap_dict[0])
 
         if args.core_insts_output is not None:
+            make_path_to_file(args.core_insts_output)
+            make_path_to_file(args.operand_kinds_output)
             print(generate_instruction_table(grammar['instructions'], False),
                   file=open(args.core_insts_output, 'w'))
             print(generate_operand_kind_table(grammar['operand_kinds']),
@@ -369,12 +384,14 @@ def main():
     if args.extinst_glsl_grammar is not None:
         with open(args.extinst_glsl_grammar) as json_file:
             grammar = json.loads(json_file.read())
+            make_path_to_file(args.glsl_insts_output)
             print(generate_instruction_table(grammar['instructions'], True),
                   file=open(args.glsl_insts_output, 'w'))
 
     if args.extinst_opencl_grammar is not None:
         with open(args.extinst_opencl_grammar) as json_file:
             grammar = json.loads(json_file.read())
+            make_path_to_file(args.opencl_insts_output)
             print(generate_instruction_table(grammar['instructions'], True),
                   file=open(args.opencl_insts_output, 'w'))