Version:

# Build Generated Source Files with AzAutoGen

For complicated systems that use a lot of boilerplate code, it’s useful to perform some kind of light automation to generate source files where possible. To generate files during builds, Open 3D Engine (O3DE) uses the AzAutoGen tool through a CMake command. O3DE stores the output source code in your CMake build directory and picks up the code to build it in your target.

To use AzAutoGen, you must provide Jinja templates and XML or JSON data files that AzAutoGen can generate output files from. The contents of these files depend on your intended purpose for using AzAutoGen. For a complete description of how AzAutoGen works and how to write templates and data inputs for generation, refer to Automate Source Generation from Templates with AzAutoGen.

## Integrating with an O3DE target build

AzAutoGen is invoked when CMake builds an O3DE target. To invoke AzAutoGen and generate the output source files for your project or Gem, complete the following steps:

1. Define the autogen rules by passing in a set of AUTOGEN_RULES to the ly_add_target(...) command in your project’s CMakeLists.txt file. Each rule maps a set of data input files to a single template and specifies the output filenames for the generated content.

For specifications on defining autogen rules, refer to the Autogen rules section on this page.

2. Add your data input files (.xml and .json) and templates (.jinja) to your project’s set of all files, so CMake can locate them when building. To do this:

• In your project’s *_files.cmake file, pass the filenames into the set(FILE...) command.
• In your project’s CMakeLists.txt file, include the *_files.cmake file to the FILE_CMAKE parameter.

### Example

The following example shows how autogen rules are formatted.

AUTOGEN_RULES
# Test wildcarded expansion for xml
*.AutoEnum.xml,AutoEnum_Header.jinja,$path/$fileprefix.AutoEnum.h
*.AutoEnum.xml,AutoEnum_Source.jinja,$path/$fileprefix.AutoEnum.cpp

# Test wildcarded expansion for json
*.AutoStruct.json,AutoStruct_Header.jinja,$path/$fileprefix.AutoStruct.h

# Test explicit data input file
AzCore/AutoGen/WeaponTypes.AutoStruct.json,AutoStruct_Header.jinja,$path/$fileprefix2.AutoStruct.h

# Test globbing data input files into a single output file


## Integrating with any target

Most of the time, you want to run AzAutoGen by passing autogen rules into ly_add_target(), as outlined in the Integrating with an O3DE target build section earlier. For situations where a target is already defined and AzAutoGen needs to be invoked, use the ly_add_autogen() CMake command. This command associates a set of autogen rules, including build outputs, with an existing target. The ly_add_autogen() function is defined in LyAutoGen.cmake . It takes the following parameters:

ly_add_autogen(
NAME Name of the target to add the autogen step to
OUTPUT_NAME (optional) Overrides the name of the output target. If not specified, the name will be used.
INCLUDE_DIRECTORIES List of directories to use as include paths
AUTOGEN_RULES Set of autogen rules that describe output generation and are passed to the AzAutoGen expansion system
ALLFILES List of data input files contained by the target and used to generate source code
)

TitleDescription
Automate Source Generation from Templates with AzAutoGenHow to generate new types of network packets for O3DE.
Networking Auto-packetsHow to create new packet types for AzNetworking using AzAutoGen.
Creating Custom Nodes in Script CanvasHow to create custom nodes in Script Canvas using XML definitions and turn the nodes into code with AzAutoGen.