LyAutoGen.cmake 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #! ly_add_autogen: adds a code generation step to the specified target.
  9. #
  10. # \arg:NAME name of the target
  11. # \arg:OUTPUT_NAME (optional) overrides the name of the output target. If not specified, the name will be used.
  12. # \arg:INCLUDE_DIRECTORIES list of directories to use as include paths
  13. # \arg:AUTOGEN_RULES a set of AutoGeneration rules to be passed to the AzAutoGen expansion system
  14. # \arg:ALLFILES list of all source files contained by the target
  15. function(ly_add_autogen)
  16. set(options)
  17. set(oneValueArgs NAME)
  18. set(multiValueArgs INCLUDE_DIRECTORIES AUTOGEN_RULES ALLFILES)
  19. cmake_parse_arguments(ly_add_autogen "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  20. if(ly_add_autogen_AUTOGEN_RULES)
  21. set(AZCG_INPUTFILES ${ly_add_autogen_ALLFILES})
  22. list(FILTER AZCG_INPUTFILES INCLUDE REGEX ".*\.(xml|json|jinja)$")
  23. # Writes AzAutoGen input files into tmp ${ly_add_autogen_NAME}_input_files.txt file to avoid long command error
  24. set(input_files_path "${CMAKE_CURRENT_BINARY_DIR}/Azcg/Temp/${ly_add_autogen_NAME}_input_files.txt")
  25. file(CONFIGURE OUTPUT "${input_files_path}" CONTENT [[@AZCG_INPUTFILES@]] @ONLY)
  26. get_target_property(target_type ${ly_add_autogen_NAME} TYPE)
  27. if (target_type STREQUAL INTERFACE_LIBRARY)
  28. target_include_directories(${ly_add_autogen_NAME} INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/Azcg/Generated/${ly_add_autogen_NAME}")
  29. else()
  30. target_include_directories(${ly_add_autogen_NAME} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/Azcg/Generated/${ly_add_autogen_NAME}")
  31. endif()
  32. execute_process(
  33. COMMAND ${LY_PYTHON_CMD} "${LY_ROOT_FOLDER}/cmake/AzAutoGen.py" "${ly_add_autogen_NAME}" "${CMAKE_BINARY_DIR}/Azcg/TemplateCache/" "${CMAKE_CURRENT_BINARY_DIR}/Azcg/Generated/${ly_add_autogen_NAME}/" "${CMAKE_CURRENT_SOURCE_DIR}" "${input_files_path}" "${ly_add_autogen_AUTOGEN_RULES}" "-n"
  34. OUTPUT_VARIABLE AUTOGEN_OUTPUTS
  35. )
  36. string(STRIP "${AUTOGEN_OUTPUTS}" AUTOGEN_OUTPUTS)
  37. set(AZCG_DEPENDENCIES ${AZCG_INPUTFILES})
  38. list(APPEND AZCG_DEPENDENCIES "${LY_ROOT_FOLDER}/cmake/AzAutoGen.py")
  39. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${AZCG_DEPENDENCIES})
  40. add_custom_command(
  41. OUTPUT ${AUTOGEN_OUTPUTS}
  42. DEPENDS ${AZCG_DEPENDENCIES}
  43. COMMAND ${LY_PYTHON_CMD} "${LY_ROOT_FOLDER}/cmake/AzAutoGen.py" "--prune" "${ly_add_autogen_NAME}" "${CMAKE_BINARY_DIR}/Azcg/TemplateCache/" "${CMAKE_CURRENT_BINARY_DIR}/Azcg/Generated/${ly_add_autogen_NAME}/" "${CMAKE_CURRENT_SOURCE_DIR}" "${input_files_path}" "${ly_add_autogen_AUTOGEN_RULES}"
  44. COMMENT "Running AutoGen for ${ly_add_autogen_NAME}"
  45. VERBATIM
  46. )
  47. set_target_properties(${ly_add_autogen_NAME} PROPERTIES AUTOGEN_INPUT_FILES "${AZCG_INPUTFILES}")
  48. set_target_properties(${ly_add_autogen_NAME} PROPERTIES AUTOGEN_OUTPUT_FILES "${AUTOGEN_OUTPUTS}")
  49. target_sources(${ly_add_autogen_NAME} PRIVATE ${AUTOGEN_OUTPUTS})
  50. endif()
  51. endfunction()