// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; using System.Collections.Generic; using System.IO; using Flax.Build.NativeCpp; namespace Flax.Build { /// /// Defines a module that can be compiled and used by other modules and targets. /// public class Module { /// /// The module name. /// public string Name; /// /// The module file path. /// public string FilePath; /// /// The path of the folder that contains this module file. /// public string FolderPath; /// /// The name for the output binary module. Can be used to merge multiple native modules into single library. If set to null or the module won't be using scripting API features. /// public string BinaryModuleName; /// /// True if module has native code to build. Can be used for C#-only modules. /// public bool BuildNativeCode = true; /// /// True if module has C# code to build. Can be used for native modules without C# bindings nor code. /// public bool BuildCSharp = true; /// /// True if module can be deployed. /// public bool Deploy = true; /// /// Custom module tags. Used by plugins and external tools. /// public Dictionary Tags = new Dictionary(); /// /// Initializes a new instance of the class. /// public Module() { var type = GetType(); Name = BinaryModuleName = type.Name; } /// /// Initializes the module properties. /// public virtual void Init() { } /// /// Setups the module build options. Can be used to specify module dependencies, external includes and other settings. /// /// The module build options. public virtual void Setup(BuildOptions options) { options.ScriptingAPI.Defines.Add(GetCSharpBuildDefine(options.Configuration)); options.ScriptingAPI.Defines.Add(GetCSharpPlatformDefine(options.Platform.Target)); if ((options.Platform != null && !options.Platform.HasDynamicCodeExecutionSupport) || Configuration.AOTMode != DotNetAOTModes.None) { options.ScriptingAPI.Defines.Add("USE_AOT"); } } internal static string GetCSharpBuildDefine(TargetConfiguration configuration) { switch (configuration) { case TargetConfiguration.Debug: return "BUILD_DEBUG"; case TargetConfiguration.Development: return "BUILD_DEVELOPMENT"; case TargetConfiguration.Release: return "BUILD_RELEASE"; default: throw new Exception(); } } internal static string GetCSharpPlatformDefine(TargetPlatform platform) { switch (platform) { case TargetPlatform.Windows: return "PLATFORM_WINDOWS"; case TargetPlatform.XboxOne: return "PLATFORM_XBOX_ONE"; case TargetPlatform.UWP: return "PLATFORM_UWP"; case TargetPlatform.Linux: return "PLATFORM_LINUX"; case TargetPlatform.PS4: return "PLATFORM_PS4"; case TargetPlatform.PS5: return "PLATFORM_PS5"; case TargetPlatform.XboxScarlett: return "PLATFORM_XBOX_SCARLETT"; case TargetPlatform.Android: return "PLATFORM_ANDROID"; case TargetPlatform.Switch: return "PLATFORM_SWITCH"; case TargetPlatform.Mac: return "PLATFORM_MAC"; case TargetPlatform.iOS: return "PLATFORM_IOS"; default: throw new InvalidPlatformException(platform); } } /// /// Setups the module building environment. Allows to modify compiler and linker options. /// /// The module build options. public virtual void SetupEnvironment(BuildOptions options) { options.CompileEnv.PreprocessorDefinitions.AddRange(options.PublicDefinitions); options.CompileEnv.PreprocessorDefinitions.AddRange(options.PrivateDefinitions); options.CompileEnv.IncludePaths.AddRange(options.PublicIncludePaths); options.CompileEnv.IncludePaths.AddRange(options.PrivateIncludePaths); } /// /// Gets the files to deploy. /// /// The output files list. public virtual void GetFilesToDeploy(List files) { // By default deploy all C++ header files files.AddRange(Directory.GetFiles(FolderPath, "*.h", SearchOption.AllDirectories)); } /// /// Adds the file to the build sources if exists. /// /// The options. /// The source file path. protected void AddSourceFileIfExists(BuildOptions options, string path) { if (File.Exists(path)) options.SourceFiles.Add(path); } } }