GenerateAndroidProject.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/python3
  2. # Copyright (C) 2009-2022, 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. def parse_commandline():
  16. """ Parse the command line arguments """
  17. parser = optparse.OptionParser(usage="usage: %prog [options]", description="Generate an Android gradle project")
  18. parser.add_option("-o", "--out-dir", dest="out_dir", type="string", help="Where to create the project")
  19. parser.add_option("-t", "--target", dest="target", type="string", help="The name of .so to package")
  20. parser.add_option("-a", "--assets", dest="assets", type="string", help="Assets directory")
  21. (options, args) = parser.parse_args()
  22. required = "target out_dir".split()
  23. for r in required:
  24. if options.__dict__[r] is None:
  25. parser.print_help()
  26. parser.error("parameter \"%s\" required" % r)
  27. ctx = Context()
  28. ctx.target = options.target
  29. if options.assets is not None:
  30. ctx.asserts_dir = os.path.abspath(options.assets)
  31. ctx.out_dir = os.path.abspath(options.out_dir)
  32. return ctx
  33. def replace_in_file(filename, to_replace, to_replace_with):
  34. to_replace_with = to_replace_with.replace("\\", "\\\\")
  35. with fileinput.FileInput(filename, inplace=True) as file:
  36. for line in file:
  37. print(line.replace(to_replace, to_replace_with), end="")
  38. def main():
  39. """ The main """
  40. ctx = parse_commandline()
  41. this_script_dir = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
  42. # Copy template
  43. project_dir = os.path.join(ctx.out_dir, "AndroidProject_%s" % ctx.target)
  44. if not os.path.isdir(project_dir):
  45. shutil.copytree(this_script_dir, project_dir)
  46. else:
  47. print("Project directory (%s) already exists. Won't copy template" % project_dir)
  48. # RM the script
  49. try:
  50. os.remove(os.path.join(project_dir, "GenerateAndroidProject.py"))
  51. except OSError:
  52. pass
  53. # Create the assets dir structure
  54. assets_dir = os.path.join(project_dir, "assets")
  55. if not os.path.isdir(assets_dir):
  56. os.mkdir(assets_dir)
  57. os.mkdir(os.path.join(project_dir, "assets/AnKi/"))
  58. os.symlink(os.path.join(this_script_dir, "../../AnKi/Shaders"),
  59. os.path.join(project_dir, "assets/AnKi/Shaders"))
  60. os.symlink(os.path.join(this_script_dir, "../../EngineAssets"),
  61. os.path.join(project_dir, "assets/EngineAssets"))
  62. os.mkdir(os.path.join(project_dir, "assets/ThirdParty/"))
  63. os.symlink(os.path.join(this_script_dir, "../../ThirdParty/FidelityFX"),
  64. os.path.join(project_dir, "assets/ThirdParty/FidelityFX"))
  65. if ctx.asserts_dir is not None:
  66. os.symlink(ctx.asserts_dir, os.path.join(project_dir, "assets/Assets"))
  67. else:
  68. print("Asset directory (%s) already exists. Skipping" % assets_dir)
  69. # Write the asset directory structure to a file
  70. dir_structure_file = open(os.path.join(assets_dir, "DirStructure.txt"), "w", newline="\n")
  71. for root, dirs, files in os.walk(assets_dir, followlinks=True):
  72. for f in files:
  73. if f.find("DirStructure.txt") >= 0:
  74. continue
  75. filename = os.path.join(root, f)
  76. filename = filename.replace(assets_dir, "")
  77. filename = filename.replace("\\", "/")
  78. if filename[0] == '/':
  79. filename = filename[1:]
  80. dir_structure_file.write("%s\n" % filename)
  81. dir_structure_file.close()
  82. # strings.xml
  83. replace_in_file(os.path.join(project_dir, "app/src/main/res/values/strings.xml"), "%APP_NAME%", ctx.target)
  84. # build.gradle
  85. build_gradle = os.path.join(project_dir, "app/build.gradle")
  86. replace_in_file(build_gradle, "%TARGET%", ctx.target)
  87. replace_in_file(build_gradle, "%PYTHON%", sys.executable)
  88. replace_in_file(build_gradle, "%CMAKE%", os.path.join(this_script_dir, "../../CMakeLists.txt"))
  89. # Manifest
  90. replace_in_file(os.path.join(project_dir, "app/src/main/AndroidManifest.xml"), "%TARGET%", ctx.target)
  91. # Done
  92. print("Generated project: %s" % project_dir)
  93. if __name__ == "__main__":
  94. main()