summaryrefslogtreecommitdiff
path: root/tests/java_api/RSUnitTests/RSUnitTests.py
blob: e47a60f8f731cb64e28cafaf37f27c921b8dbf6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
# Copyright 2017, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Generates necessary files for unit test versions for testing.

After adding/changing RenderScript unit tests, run `python RSUnitTests.py`
to ensure that forward/backward compatibility tests run properly.

This file is so when updating/adding unit tests there is one central location
for managing the list of unit tests and their versions.

This is necessary since forward compatibility unit tests are chosen at compile
time where backward compatibility tests are chosen at runtime.

Generates a Java file for backward compatibility testing.
Generates an Android.mk file for forward compatibility testing.
"""


import shutil
import sys
import os


# List of API versions and the tests that correspond to the API version
# The test name must correspond to a UT_{}.java file
# e.g. alloc -> UT_alloc.java
UNIT_TESTS = {
    19: [
        'alloc',
        'array_alloc',
        'array_init',
        'atomic',
        'bitfield',
        'bug_char',
        'check_dims',
        'clamp_relaxed',
        'clamp',
        'constant',
        'convert_relaxed',
        'convert',
        'copy_test',
        'element',
        'foreach',
        'foreach_bounds',
        'fp_mad',
        'instance',
        'int4',
        'kernel',
        'kernel_struct',
        'math',
        'min',
        'noroot',
        'primitives',
        'refcount',
        'rsdebug',
        'rstime',
        'rstypes',
        'sampler',
        'static_globals',
        'struct_field_simple',
        'struct',
        'unsigned',
        'vector',
    ],

    21: [
        'foreach_multi',
        'math_agree',
        'math_conformance',
    ],

    23: [
        'alloc_copy',
        'alloc_copyPadded',
        'ctxt_default',
        'kernel2d',
        'kernel2d_oldstyle',
        'kernel3d',
        'rsdebug_23',
        'script_group2_gatherscatter',
        'script_group2_nochain',
        'script_group2_pointwise',
    ],

    24: [
        'fp16',
        'fp16_globals',
        'math_24',
        'math_fp16',
        'reduce_backward',
        'reduce',
        'rsdebug_24',
        'script_group2_float',
        'single_source_alloc',
        'single_source_ref_count',
        'single_source_script',
        'small_struct',
        'small_struct_2',
    ],

    26: [
        'blur_validation',
        'struct_field',
    ],
}


# Tests that only belong to RSTest_Compat (support lib tests)
SUPPORT_LIB_ONLY_UNIT_TESTS = {
    'alloc_supportlib',
    'apitest',
}


# Tests that are skipped in RSTest_Compat (support lib tests)
SUPPORT_LIB_IGNORE_TESTS = {
    'fp16',
    'fp16_globals',
    'math_fp16',
}


# Dictionary mapping unit tests to the corresponding needed .rs files
# Only needed if UT_{}.java does not map to {}.rs
UNIT_TEST_RS_FILES_OVERRIDE = {
    'alloc_copy': [],
    'alloc_copyPadded': [],
    'blur_validation': [],
    'script_group2_float': ['float_test.rs'],
    'script_group2_gatherscatter': ['addup.rs'],
    'script_group2_nochain': ['increment.rs', 'increment2.rs', 'double.rs'],
    'script_group2_pointwise': ['increment.rs', 'double.rs'],
}


# List of API versions and the corresponding build tools release version
# For use with forward compatibility testing
BUILD_TOOL_VERSIONS = {
    21: '21.1.2',
    22: '22.0.1',
    23: '23.0.3',
    24: '24.0.3',
    25: '25.0.2',
}


# ---------- Makefile generation ----------


def WriteMakeCopyright(gen_file):
  """Writes the copyright for a Makefile to a file."""
  gen_file.write(
      '#\n'
      '# Copyright (C) 2017 The Android Open Source Project\n'
      '#\n'
      '# Licensed under the Apache License, Version 2.0 (the "License");\n'
      '# you may not use this file except in compliance with the License.\n'
      '# You may obtain a copy of the License at\n'
      '#\n'
      '#      http://www.apache.org/licenses/LICENSE-2.0\n'
      '#\n'
      '# Unless required by applicable law or agreed to in writing, software\n'
      '# distributed under the License is distributed on an "AS IS" BASIS,\n'
      '# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
      '# See the License for the specific language governing permissions and\n'
      '# limitations under the License.\n'
      '#\n\n'
  )


def WriteMakeSrcFiles(gen_file, api_version):
  """Writes applicable LOCAL_SRC_FILES to gen_file.

  Includes everything under ./src, base UnitTest class, and test files.

  api_version: only tests that can run on this version are added."""
  # Get all tests compatible with the build tool version
  # Compatible means build tool version >= test version
  tests = []
  for test_version, tests_for_version in UNIT_TESTS.iteritems():
    if api_version >= test_version:
      tests.extend(tests_for_version)
  tests = sorted(tests)
  gen_file.write(
      'LOCAL_SRC_FILES := $(call all-java-files-under,src)\\\n'
      '    $(my_rs_unit_tests_path)/UnitTest.java\\\n'
  )
  for test in tests:
    # Add the Java and corresponding rs files to LOCAL_SRC_FILES
    gen_file.write(
        '    $(my_rs_unit_tests_path)/{}\\\n'
        .format(JavaFileForUnitTest(test))
    )
    for rs_file in RSFilesForUnitTest(test):
      gen_file.write(
          '    $(my_rs_unit_tests_path)/{}\\\n'.format(
              rs_file
          )
      )


# ---------- Java file generation ----------


def WriteJavaCopyright(gen_file):
  """Writes the copyright for a Java file to gen_file."""
  gen_file.write(
      '/*\n'
      ' * Copyright (C) 2017 The Android Open Source Project\n'
      ' *\n'
      ' * Licensed under the Apache License, Version 2.0 (the "License");\n'
      ' * you may not use this file except in compliance with the License.\n'
      ' * You may obtain a copy of the License at\n'
      ' *\n'
      ' *      http://www.apache.org/licenses/LICENSE-2.0\n'
      ' *\n'
      ' * Unless required by applicable law or agreed to in writing, software\n'
      ' * distributed under the License is distributed on an "AS IS" BASIS,\n'
      ' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n'
      ' * See the License for the specific language governing permissions and\n'
      ' * limitations under the License.\n'
      ' */\n\n'
  )


# ---------- Unit Test file functions ----------


def JavaFileForUnitTest(test):
  """Returns the Java file name for a unit test."""
  return 'UT_{}.java'.format(test)


def RSFilesForUnitTest(test):
  """Returns a list of .rs files associated with a test."""
  if test in UNIT_TEST_RS_FILES_OVERRIDE:
    return UNIT_TEST_RS_FILES_OVERRIDE[test]
  else:
    # Default is one .rs file with the same name as the input
    return ['{}.rs'.format(test)]


# ---------- Dirs ----------


def ThisScriptDir():
  """Returns the directory this script is in."""
  return os.path.dirname(os.path.realpath(__file__))


def UnitTestDir():
  """Returns the path to the directory containing the unit tests."""
  return os.path.join(ThisScriptDir(), 'src', 'com', 'android', 'rs',
                      'unittest')


def SupportLibOnlyTestDir():
  """Returns the path to the directory with unit tests for support lib."""
  return os.path.join(ThisScriptDir(), 'supportlibonlysrc', 'com',
                      'android', 'rs', 'unittest')


def SupportLibGenTestDir():
  """Returns the path to the directory with unit tests for support lib."""
  return os.path.join(ThisScriptDir(), 'supportlibsrc_gen', 'com',
                      'android', 'rs', 'unittest')


# ---------- RSTest_Compat/RSTest_Compat19 test generation ----------


def AllUnitTestsExceptSupportLibOnly():
  """Returns a set of all unit tests except SUPPORT_LIB_ONLY_UNIT_TESTS."""
  ret = set()
  for _, tests in UNIT_TESTS.iteritems():
    ret.update(tests)
  return ret


def CopyUnitTestJavaFileToSupportLibTest(java_file_name, java_file_dir):
  """Copies the Java file to the support lib dir.

  Replaces RenderScript imports with corresponding support lib imports."""
  in_path = os.path.join(java_file_dir, java_file_name)
  out_path = os.path.join(SupportLibGenTestDir(), java_file_name)
  with open(in_path, 'r') as in_file, open(out_path, 'w') as out_file:
    out_file.write(
        '// This file is automatically generated from\n'
        '// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py\n'
    )
    for line in in_file.readlines():
      if line.startswith('import android.renderscript.'):
        line = line.replace('android.renderscript.',
                            'android.support.v8.renderscript.')
      out_file.write(line)


def CopyUnitTestRSToSupportLibTest(rs_file_name, rs_file_dir):
  """Copies the .rs to the support lib dir."""
  in_path = os.path.join(rs_file_dir, rs_file_name)
  out_path = os.path.join(SupportLibGenTestDir(), rs_file_name)
  with open(out_path, 'w') as out_file:
    out_file.write(
        '// This file is automatically generated from\n'
        '// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py\n'
    )
    with open(in_path, 'r') as in_file:
      out_file.write(in_file.read())


def CopySharedRshToSupportLibTest():
  """Copies shared.rsh to the support lib dir.

  Adds a #define RSTEST_COMPAT at the end."""
  shared_rsh = 'shared.rsh'
  CopyUnitTestRSToSupportLibTest(shared_rsh, UnitTestDir())
  with open(os.path.join(SupportLibGenTestDir(), shared_rsh), 'a') as shared:
    shared.write('\n#define RSTEST_COMPAT\n')


def CopyUnitTestToSupportLibTest(test, test_dir):
  """Copies all files corresponding to a unit test to support lib dir."""
  CopyUnitTestJavaFileToSupportLibTest(JavaFileForUnitTest(test), test_dir)
  for rs in RSFilesForUnitTest(test):
    CopyUnitTestRSToSupportLibTest(rs, test_dir)


def GenerateSupportLibUnitTests():
  """Generates all support lib unit tests."""
  if os.path.exists(SupportLibGenTestDir()):
    shutil.rmtree(SupportLibGenTestDir())
  os.makedirs(SupportLibGenTestDir())

  CopySharedRshToSupportLibTest()
  CopyUnitTestJavaFileToSupportLibTest('UnitTest.java', UnitTestDir())

  for test in AllUnitTestsExceptSupportLibOnly() - SUPPORT_LIB_IGNORE_TESTS:
    CopyUnitTestToSupportLibTest(test, UnitTestDir())

  for test in SUPPORT_LIB_ONLY_UNIT_TESTS:
    CopyUnitTestToSupportLibTest(test, SupportLibOnlyTestDir())

  print ('Generated support lib tests at {}'
         .format(SupportLibGenTestDir()))


# ---------- RSTest_Compat19 ----------


def WriteSupportLib19Makefile(gen_file):
  """Writes the Makefile for support lib 19 testing."""
  WriteMakeCopyright(gen_file)
  gen_file.write(
      '# This file is auto-generated by frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py.\n'
      '# To change unit tests version, please run the Python script above.\n\n'
      'LOCAL_PATH := $(call my-dir)\n'
      'include $(CLEAR_VARS)\n\n'
      'LOCAL_PACKAGE_NAME := RSTest_Compat19\n'
      'LOCAL_MODULE_TAGS := tests\n\n'
      'LOCAL_STATIC_JAVA_LIBRARIES := \\\n'
      '    android-support-test \\\n'
      '    android-support-v8-renderscript \\\n\n'
      'LOCAL_RENDERSCRIPT_TARGET_API := 19\n'
      'LOCAL_RENDERSCRIPT_COMPATIBILITY := true\n'
      'LOCAL_RENDERSCRIPT_FLAGS := -rs-package-name=android.support.v8.renderscript\n'
      'LOCAL_MIN_SDK_VERSION := 8\n\n'
      'my_rs_unit_tests_path := ../RSUnitTests/supportlibsrc_gen/com/android/rs/unittest\n'
  )
  WriteMakeSrcFiles(gen_file, 19)
  gen_file.write(
      '\n'
      'include $(BUILD_PACKAGE)\n\n'
      'my_rs_unit_tests_path :=\n\n'
  )


def SupportLib19MakefileLocation():
  """Returns the location of Makefile for backward compatibility 19 testing."""
  return os.path.join(ThisScriptDir(), '..', 'RSTest_CompatLib19', 'Android.mk')


def GenerateSupportLib19():
  """Generates the necessary file for Support Library tests (19)."""
  with open(SupportLib19MakefileLocation(), 'w') as gen_file:
    WriteSupportLib19Makefile(gen_file)
  print ('Generated support lib (19) Makefile at {}'
         .format(SupportLib19MakefileLocation()))


# ---------- RSTestForward ----------


def ForwardTargetName(build_tool_version_name):
  """Returns the target name for a forward compatibility build tool name."""
  make_target_name = 'RSTestForward_{}'.format(build_tool_version_name)
  make_target_name = make_target_name.replace('.', '_')
  return make_target_name


def ForwardMakefileLocation():
  """Returns the location of the Makefile for forward compatibility testing."""
  return os.path.join(ThisScriptDir(), '..', 'RSTestForward', 'Android.mk')


def WriteForwardMakefile(gen_file):
  """Writes the Makefile for forward compatibility testing.

  Makefile contains a build target per build tool version
  for forward compatibility testing based on the unit test list at the
  top of this file."""
  WriteMakeCopyright(gen_file)
  gen_file.write(
      '# This file is auto-generated by frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py.\n'
      '# To change unit tests version, please run the Python script above.\n\n'
      'ifneq ($(ENABLE_RSTESTS),)\n\n'
      'LOCAL_PATH := $(call my-dir)\n'
      'my_rs_unit_tests_path := ../RSUnitTests/src/com/android/rs/unittest\n'
  )
  all_make_target_names = []
  for build_tool_version in sorted(BUILD_TOOL_VERSIONS.keys()):
    build_tool_version_name = BUILD_TOOL_VERSIONS[build_tool_version]
    make_target_name = ForwardTargetName(build_tool_version_name)
    all_make_target_names.append(make_target_name)
    gen_file.write(
        '\n'
        '# RSTestForward for build tool version {}\n\n'
        'include $(CLEAR_VARS)\n\n'
        'LOCAL_MODULE_TAGS := tests\n'
        'LOCAL_STATIC_JAVA_LIBRARIES := android-support-test\n'
        'LOCAL_COMPATIBILITY_SUITE := device-tests\n'
        'LOCAL_RENDERSCRIPT_TARGET_API := 0\n'
        'LOCAL_PACKAGE_NAME := {}\n'
        'my_rs_path := $(TOP)/prebuilts/renderscript/host/linux-x86/{}\n'
        'LOCAL_RENDERSCRIPT_CC := $(my_rs_path)/bin/llvm-rs-cc\n'
        'LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE := $(my_rs_path)/include $(my_rs_path)/clang-include\n'
        'my_rs_path :=\n'.format(
            build_tool_version_name, make_target_name, build_tool_version_name
        )
    )
    WriteMakeSrcFiles(gen_file, build_tool_version)
    gen_file.write(
        '\n'
        'include $(BUILD_PACKAGE)\n'
    )
  # Create RSTestForward phony target which builds all above targets
  gen_file.write(
      '\n'
      'my_rs_unit_tests_path :=\n\n'
      'endif\n'.format(' '.join(all_make_target_names))
  )


def ForwardMakeTargetsLocation():
  """Returns the location of the file with all forward compatibility targets."""
  return os.path.join(ThisScriptDir(), '..', 'RSTestForward', 'Targets.mk')


def WriteForwardMakeTargets(gen_file):
  """Writes forward compatibility make target names to gen_file."""
  gen_file.write('RSTESTFORWARD_TARGETS := \\\n')
  for build_tool_version_name in sorted(BUILD_TOOL_VERSIONS.values()):
    make_target_name = ForwardTargetName(build_tool_version_name)
    gen_file.write('    {} \\\n'.format(make_target_name))


def GenerateForward():
  """Generates the necessary file for forward compatibility testing."""
  with open(ForwardMakefileLocation(), 'w') as gen_file:
    WriteForwardMakefile(gen_file)
  print ('Generated forward compatibility Makefile at {}'
         .format(ForwardMakefileLocation()))
  with open(ForwardMakeTargetsLocation(), 'w') as gen_file:
    WriteForwardMakeTargets(gen_file)
  print ('Generated forward compatibility targets at {}'
         .format(ForwardMakeTargetsLocation()))


# ---------- RSTestBackward ----------


def BackwardJavaFileLocation():
  """Returns the location of Java file for backward compatibility testing."""
  return os.path.join(ThisScriptDir(), '..', 'RSTestBackward', 'src', 'com',
                      'android', 'rs', 'testbackward', 'RSTests.java')


def Backward19JavaFileLocation():
  """Returns the location of Java file for backward compatibility 19 testing."""
  return os.path.join(ThisScriptDir(), '..', 'RSTestBackward19', 'src', 'com',
                      'android', 'rs', 'testbackward19', 'RSTests.java')


def Backward19MakefileLocation():
  """Returns the location of Makefile for backward compatibility 19 testing."""
  return os.path.join(ThisScriptDir(), '..', 'RSTestBackward19', 'Android.mk')


def WriteBackwardJavaFile(gen_file, package, max_api_version=None):
  """Writes the Java file for backward compatibility testing to gen_file.

  Java file determines unit tests for backward compatibility
  testing based on the unit test list at the top of this file."""
  WriteJavaCopyright(gen_file)
  gen_file.write(
      'package {};\n'
      '\n'
      'import com.android.rs.unittest.*;\n'
      '\n'
      'import java.util.ArrayList;\n'
      '\n'
      '/**\n'
      ' * This class is auto-generated by frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py.\n'
      ' * To change unit tests version, please run the Python script above.\n'
      ' */\n'
      'public class RSTests {{\n'
      '    public static Iterable<Class<? extends UnitTest>> getTestClassesForCurrentAPIVersion() {{\n'
      '        int thisApiVersion = android.os.Build.VERSION.SDK_INT;\n'
      '\n'
      '        ArrayList<Class<? extends UnitTest>> validClasses = new ArrayList<>();'.format(
          package
      )
  )
  for version in sorted(UNIT_TESTS.keys()):
    if max_api_version is None or version <= max_api_version:
      tests = sorted(UNIT_TESTS[version])
      gen_file.write(
          '\n\n        if (thisApiVersion >= {}) {{\n'.format(version)
      )
      for test in tests:
        gen_file.write(
            '            validClasses.add(UT_{}.class);\n'.format(test)
        )
      gen_file.write('        }')
  gen_file.write('\n\n        return validClasses;\n    }\n}\n')


def WriteBackward19Makefile(gen_file):
  """Writes the Makefile for backward compatibility 19 testing."""
  WriteMakeCopyright(gen_file)
  gen_file.write(
      '# This file is auto-generated by frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py.\n'
      '# To change unit tests version, please run the Python script above.\n\n'
      'LOCAL_PATH := $(call my-dir)\n'
      'include $(CLEAR_VARS)\n\n'
      'LOCAL_MODULE_TAGS := tests\n'
      'LOCAL_STATIC_JAVA_LIBRARIES := android-support-test\n'
      'LOCAL_COMPATIBILITY_SUITE := device-tests\n'
      'LOCAL_RENDERSCRIPT_TARGET_API := 19\n'
      'LOCAL_MIN_SDK_VERSION := 17\n'
      'LOCAL_PACKAGE_NAME := RSTestBackward19\n'
      'my_rs_unit_tests_path := ../RSUnitTests/src/com/android/rs/unittest\n'
  )
  WriteMakeSrcFiles(gen_file, 19)
  gen_file.write(
      '\n'
      'include $(BUILD_PACKAGE)\n\n'
      'my_rs_unit_tests_path :=\n\n'
  )


def GenerateBackward():
  """Generates Java file for backward compatibility testing."""
  with open(BackwardJavaFileLocation(), 'w') as gen_file:
    WriteBackwardJavaFile(gen_file, 'com.android.rs.testbackward')
  print ('Generated backward compatibility Java file at {}'
         .format(BackwardJavaFileLocation()))


def GenerateBackward19():
  """Generates files for backward compatibility testing for API 19."""
  with open(Backward19JavaFileLocation(), 'w') as gen_file:
    WriteBackwardJavaFile(gen_file, 'com.android.rs.testbackward19', 19)
  print ('Generated backward compatibility (19) Java file at {}'
         .format(Backward19JavaFileLocation()))

  with open(Backward19MakefileLocation(), 'w') as gen_file:
    WriteBackward19Makefile(gen_file)
  print ('Generated backward compatibility (19) Makefile at {}'
         .format(Backward19MakefileLocation()))


# ---------- Main ----------


def DisplayHelp():
  """Prints help message."""
  print >> sys.stderr, ('Usage: {} [forward] [backward] [backward19] [help|-h|--help]\n'
                        .format(sys.argv[0]))
  print >> sys.stderr, ('[forward]: write forward compatibility Makefile to\n    {}\n'
                        .format(ForwardMakefileLocation()))
  print >> sys.stderr, ('[backward]: write backward compatibility Java file to\n    {}\n'
                        .format(BackwardJavaFileLocation()))
  print >> sys.stderr, ('[backward19]: write backward compatibility Java file (19) to\n    {}\n'
                        .format(Backward19JavaFileLocation()))
  print >> sys.stderr, ('[backward19]: write backward compatibility Makefile (19) to\n    {}\n'
                        .format(Backward19MakefileLocation()))
  print >> sys.stderr, ('[supportlib]: generate support lib unit tests to\n    {}\n'
                        .format(SupportLibGenTestDir()))
  print >> sys.stderr, ('[supportlib19]: generate support lib Makefile (19) to\n    {}\n'
                        .format(SupportLib19MakefileLocation()))
  print >> sys.stderr, 'if no options are chosen, then all files are generated'


def main():
  """Parses sys.argv and does stuff."""
  display_help = False
  error = False
  actions = []

  for arg in sys.argv[1:]:
    if arg in ('help', '-h', '--help'):
      display_help = True
    elif arg == 'backward':
      actions.append(GenerateBackward)
    elif arg == 'backward19':
      actions.append(GenerateBackward19)
    elif arg == 'forward':
      actions.append(GenerateForward)
    elif arg == 'supportlib':
      actions.append(GenerateSupportLibUnitTests)
    elif arg == 'supportlib19':
      actions.append(GenerateSupportLib19)
    else:
      print >> sys.stderr, 'unrecognized arg: {}'.format(arg)
      error = True

  if display_help or error:
    DisplayHelp()
  elif actions:
    for action in actions:
      action()
  else:
    # No args - do default action.
    GenerateBackward()
    GenerateBackward19()
    GenerateForward()
    GenerateSupportLibUnitTests()
    GenerateSupportLib19()


if __name__ == '__main__':
  main()