gemcmake.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. from __future__ import (absolute_import, division,
  9. print_function, unicode_literals)
  10. import json
  11. import sys
  12. import json
  13. import os
  14. import subprocess
  15. import argparse
  16. import waffiles2cmake
  17. fileContents = ""
  18. def getCopyright():
  19. return """#
  20. # Copyright (c) Contributors to the Open 3D Engine Project.
  21. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  22. #
  23. # SPDX-License-Identifier: Apache-2.0 OR MIT
  24. #
  25. #
  26. """
  27. def getGemCMakeListsTemplate():
  28. return """ly_add_target(
  29. NAME {GEM_NAME}.Static STATIC
  30. NAMESPACE Gem
  31. FILES_CMAKE
  32. {GEM_NAME_LOWERCASE}_files.cmake
  33. INCLUDE_DIRECTORIES
  34. PRIVATE
  35. Source
  36. PUBLIC
  37. Include
  38. BUILD_DEPENDENCIES
  39. PRIVATE
  40. #AZ::AzCore
  41. )
  42. ly_add_target(
  43. NAME {GEM_NAME} ${PAL_TRAIT_MONOLITHIC_DRIVEN_MODULE_TYPE}
  44. NAMESPACE Gem
  45. FILES_CMAKE
  46. {GEM_NAME_LOWERCASE}_shared_files.cmake
  47. INCLUDE_DIRECTORIES
  48. PRIVATE
  49. Source
  50. PUBLIC
  51. Include
  52. BUILD_DEPENDENCIES
  53. PRIVATE
  54. Gem::{GEM_NAME}.Static
  55. )
  56. if(PAL_TRAIT_BUILD_HOST_TOOLS)
  57. ly_add_target(
  58. NAME {GEM_NAME}.Editor GEM_MODULE
  59. NAMESPACE Gem
  60. FILES_CMAKE
  61. {GEM_NAME_LOWERCASE}_editor_files.cmake
  62. INCLUDE_DIRECTORIES
  63. PRIVATE
  64. Source
  65. PUBLIC
  66. Include
  67. BUILD_DEPENDENCIES
  68. PRIVATE
  69. #AZ::AzCore
  70. )
  71. endif()
  72. ################################################################################
  73. # Tests
  74. ################################################################################
  75. if(PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  76. ly_add_target(
  77. NAME {GEM_NAME}.Tests ${PAL_TRAIT_TEST_TARGET_TYPE}
  78. NAMESPACE Gem
  79. FILES_CMAKE
  80. {GEM_NAME_LOWERCASE}_tests_files.cmake
  81. INCLUDE_DIRECTORIES
  82. PRIVATE
  83. Tests
  84. BUILD_DEPENDENCIES
  85. PRIVATE
  86. AZ::AzTest
  87. Gem::{GEM_NAME}.Static
  88. )
  89. ly_add_googletest(
  90. NAME {GEM_NAME}.Tests
  91. )
  92. endif()
  93. """
  94. def getEmptyCMakeFiles():
  95. return """set(FILES
  96. )
  97. """
  98. def getDefaultTargetsForGem(gem_name, gem_uuid, gem_version, cmakeListTemplate):
  99. gem_name_lowercase = gem_name.lower()
  100. gem_uuid_lowercase = gem_uuid.lower()
  101. return cmakeListTemplate()\
  102. .replace('{GEM_NAME}', gem_name)\
  103. .replace('{GEM_NAME_LOWERCASE}', gem_name_lowercase)\
  104. .replace('{GEM_UUID}', gem_uuid_lowercase)\
  105. .replace('{GEM_VERSION}', gem_version)
  106. def createEmptyCMakeLists(cmakelists_path):
  107. with open(cmakelists_path, 'w') as destination_file:
  108. destination_file.write(getCopyright())
  109. def createGemCMakeLists(cmakelists_path, gem_name, gem_uuid, gem_version, cmakeListTemplate):
  110. with open(cmakelists_path, 'w') as destination_file:
  111. destination_file.write(getCopyright())
  112. destination_file.write(getDefaultTargetsForGem(gem_name, gem_uuid, gem_version, cmakeListTemplate))
  113. def addSubdirectoryToCMakeLists(cmakelists_path, folder_name):
  114. if os.path.exists(cmakelists_path):
  115. print('Editing file {}'.format(cmakelists_path))
  116. subprocess.run(['p4', 'edit', cmakelists_path])
  117. else:
  118. print('Adding file {}'.format(cmakelists_path))
  119. createEmptyCMakeLists(cmakelists_path)
  120. subprocess.run(['p4', 'add', cmakelists_path])
  121. # Edit the file
  122. with open(cmakelists_path, 'r') as source_file:
  123. fileContents = source_file.read()
  124. lineToAdd = 'add_subdirectory({})\n'.format(folder_name)
  125. if fileContents.find(lineToAdd) == -1:
  126. fileContents += lineToAdd
  127. with open(cmakelists_path, 'w') as destination_file:
  128. destination_file.write(fileContents)
  129. def generateCMakeFilesForGem(gem_path, gem_name, gem_uuid, gem_version, cmakeListTemplate):
  130. gem_code_path = os.path.join(gem_path, 'Code', 'CMakeLists.txt')
  131. print('Adding file {}'.format(gem_code_path))
  132. createGemCMakeLists(gem_code_path, gem_name, gem_uuid, gem_version, cmakeListTemplate)
  133. subprocess.run(['p4', 'add', gem_code_path])
  134. gem_shared_filename = gem_name.lower() + '_shared_files.cmake'
  135. gem_shared_files = os.path.join(gem_path, 'Code', gem_shared_filename)
  136. with open(gem_shared_files, 'w') as destination_file:
  137. destination_file.write(getCopyright())
  138. destination_file.write(getEmptyCMakeFiles())
  139. subprocess.run(['p4', 'add', gem_shared_files])
  140. waffiles2cmake.main()
  141. def main():
  142. """script main function"""
  143. parser = argparse.ArgumentParser(description='This script creates a basic CMakeLists.txt file for a gem using the gem.json',
  144. formatter_class=argparse.RawTextHelpFormatter)
  145. parser.add_argument('path_to_gems', type=str, nargs='+',
  146. help='list of gem directories to look create CMakeLists.txt files within and add to p4')
  147. args = parser.parse_args()
  148. for input_path in args.path_to_gems:
  149. if not os.path.isdir(input_path):
  150. print('Expected a valid path, got {}'.format(input_path))
  151. sys.exit(1)
  152. gem_path = os.path.abspath(input_path)
  153. gem_name = os.path.basename(gem_path)
  154. gems_path = os.path.dirname(gem_path)
  155. # Get the UUID
  156. gem_json_file = os.path.join(gem_path, 'gem.json')
  157. with open(gem_json_file) as f:
  158. gem_json_dict = json.load(f)
  159. gem_uuid = gem_json_dict['Uuid']
  160. gem_version = gem_json_dict['Version']
  161. addSubdirectoryToCMakeLists(os.path.join(gems_path, 'CMakeLists.txt'), gem_name)
  162. addSubdirectoryToCMakeLists(os.path.join(gem_path, 'CMakeLists.txt'), 'Code')
  163. generateCMakeFilesForGem(gem_path, gem_name, gem_uuid, gem_version, getGemCMakeListsTemplate)
  164. #entrypoint
  165. if __name__ == '__main__':
  166. main()