GenerateAndroidProject.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  3. # All rights reserved.
  4. # Code licensed under the BSD License.
  5. # http://www.anki3d.org/LICENSE
  6. import os
  7. import optparse
  8. import shutil
  9. import fileinput
  10. import sys
  11. class Context:
  12. target = ""
  13. asserts_dir = None
  14. out_dir = ""
  15. shader_compiler = ""
  16. def parse_commandline():
  17. """ Parse the command line arguments """
  18. parser = optparse.OptionParser(usage="usage: %prog [options]", description="Generate an Android gradle project")
  19. parser.add_option("-o", "--out-dir", dest="out_dir", type="string", help="Where to create the project")
  20. parser.add_option("-t", "--target", dest="target", type="string", help="The name of .so to package")
  21. parser.add_option("-a", "--assets", dest="assets", type="string", help="Assets directory")
  22. parser.add_option("-c",
  23. "--shader-compiler",
  24. dest="shader_compiler",
  25. type="string",
  26. help="The path of the shader compiler")
  27. (options, args) = parser.parse_args()
  28. required = "target out_dir shader_compiler".split()
  29. for r in required:
  30. if options.__dict__[r] is None:
  31. parser.print_help()
  32. parser.error("parameter \"%s\" required" % r)
  33. ctx = Context()
  34. ctx.target = options.target
  35. if options.assets is not None:
  36. ctx.asserts_dir = os.path.abspath(options.assets)
  37. ctx.out_dir = os.path.abspath(options.out_dir)
  38. ctx.shader_compiler = os.path.abspath(options.shader_compiler)
  39. return ctx
  40. def replace_in_file(filename, to_replace, to_replace_with):
  41. to_replace_with = to_replace_with.replace("\\", "\\\\")
  42. with fileinput.FileInput(filename, inplace=True) as file:
  43. for line in file:
  44. print(line.replace(to_replace, to_replace_with), end="")
  45. def main():
  46. """ The main """
  47. ctx = parse_commandline()
  48. this_script_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
  49. # Copy template
  50. project_dir = os.path.join(ctx.out_dir, "AndroidProject_%s" % ctx.target)
  51. if not os.path.isdir(project_dir):
  52. shutil.copytree(this_script_dir, project_dir)
  53. else:
  54. print("Project directory (%s) already exists. Won't copy template" % project_dir)
  55. # RM the script
  56. try:
  57. os.remove(os.path.join(project_dir, "GenerateAndroidProject.py"))
  58. except OSError:
  59. pass
  60. # Create the assets dir structure
  61. assets_dir = os.path.join(project_dir, "assets")
  62. if not os.path.isdir(assets_dir):
  63. os.mkdir(assets_dir)
  64. os.symlink(os.path.join(this_script_dir, "../../EngineAssets"),
  65. os.path.join(project_dir, "assets/EngineAssets"))
  66. if ctx.asserts_dir is not None:
  67. os.symlink(ctx.asserts_dir, os.path.join(project_dir, "assets/Assets"))
  68. else:
  69. print("Asset directory (%s) already exists. Skipping" % assets_dir)
  70. # Write the asset directory structure to a file
  71. dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w", newline="\n")
  72. for root, dirs, files in os.walk(assets_dir, followlinks=True):
  73. for f in files:
  74. if f.find("DirStructure.txt") >= 0 or f.find(".ankiprogbin") >= 0:
  75. continue
  76. filename = os.path.join(root, f)
  77. filename = filename.replace(assets_dir, "")
  78. filename = filename.replace("\\", "/")
  79. if filename[0] == '/':
  80. filename = filename[1:]
  81. dir_structure_file.write("%s\n" % filename)
  82. # Write the shaders
  83. shaders_dir = os.path.join(this_script_dir, "../../AnKi/Shaders")
  84. for root, dirs, files in os.walk(shaders_dir, followlinks=False):
  85. for f in files:
  86. if f.find(".ankiprog") == -1:
  87. continue
  88. filename = os.path.join(root, f)
  89. filename = filename.replace(shaders_dir, "")
  90. dir_structure_file.write("ShaderBinaries%sbin\n" % filename)
  91. dir_structure_file.close()
  92. # strings.xml
  93. replace_in_file(os.path.join(project_dir, "app/src/main/res/values/strings.xml"), "%APP_NAME%", ctx.target)
  94. # build.gradle
  95. build_gradle = os.path.join(project_dir, "app/build.gradle")
  96. replace_in_file(build_gradle, "%TARGET%", ctx.target)
  97. replace_in_file(build_gradle, "%PYTHON%", sys.executable)
  98. replace_in_file(build_gradle, "%COMPILER%", ctx.shader_compiler)
  99. replace_in_file(build_gradle, "%CMAKE%", os.path.join(this_script_dir, "../../CMakeLists.txt"))
  100. # Manifest
  101. replace_in_file(os.path.join(project_dir, "app/src/main/AndroidManifest.xml"), "%TARGET%", ctx.target)
  102. # Done
  103. print("Generated project: %s" % project_dir)
  104. if __name__ == "__main__":
  105. main()