using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace Hast.Layer; /// /// Configuration for IHastlayer.GenerateHardwareAsync. /// public interface IHardwareGenerationConfiguration { /// /// Gets a dictionary that can contain settings for non-default configuration options (like ones required by /// specific transformer implementations). /// IDictionary CustomConfiguration { get; } /// /// Gets or sets a name associated with the hardware generation operation that's meaningful to the consumer. It may /// be logged or saved during hardware generation but otherwise it may not be used in any activities. /// string Label { get; set; } /// /// Gets the collection of the full name of those public members that will be accessible as hardware implementation /// from the host computer. By default all members implemented from interfaces and all public virtual members will /// be included. You can use this to restrict what gets transformed into hardware; if nothing is specified all /// suitable members will be transformed. /// /// /// Specify members with their full name, including the full namespace of the parent type(s) as well as their return /// type and the types of their (type) arguments, e.g.: "System.Boolean /// Contoso.ImageProcessing.FaceRecognition.FaceDetectors::IsFacePresent(System.Byte[]). /// IList HardwareEntryPointMemberFullNames { get; } /// /// Gets the collection of the name prefixes of those public members that will be accessible as hardware /// implementation from the host computer. By default all members implemented from interfaces and all public virtual /// members will be included. You can use this to restrict what gets transformed into hardware; if nothing is /// specified all suitable members will be transformed. /// /// /// Specify members with the leading part of their name as you would access them in C#, e.g.: /// "Contoso.ImageProcessing" will include all members under this namespace. /// "Contoso.ImageProcessing.FaceRecognition.FaceDetectors" will include all members in this class. /// IList HardwareEntryPointMemberNamePrefixes { get; } /// /// Gets a value indicating whether the caching of the generated hardware is allowed. If set to no caching will happen. /// bool EnableCaching { get; } /// /// Gets the name of the FPGA device (board) to transform for. Device-specific configurations are determined by /// device drivers. /// string DeviceName { get; } /// /// Gets the file system path that's temporarily set as the working directory when /// IHastlayer.GenerateHardwareAsync() is executed. If it's left then the current /// executing assembly's location is used. /// string HardwareGenerationPath { get; } /// /// Gets the file system path here where the hardware framework is located. The file describing the hardware to be /// generated will be saved there as well as anything else necessary, and that framework will be used to implement /// the hardware and configure the device. If it's a relative path, it will be resolved inside the . /// string HardwareFrameworkPath { get; } /// /// Gets a value indicating whether hardware transformation takes place. If it doesn't then will be implied to be too. /// bool EnableHardwareTransformation { get; } /// /// Gets a value indicating whether a hardware implementation composer should be used to synthesize hardware from /// the transformed hardware description. /// bool EnableHardwareImplementationComposition { get; } /// /// Gets or sets the path to a the only binary this application should support. If not and /// the binary exists, then all hardware generation is skipped. /// string SingleBinaryPath { get; set; } } public static class HardwareGenerationConfigurationExtensions { /// /// Gets the custom configuration if it exists or creates and adds it if it doesn't. /// /// Type of the configuration object. /// The customizable hardware generation configuration object. /// /// Key where the custom configuration object is stored in . /// /// The existing or newly created configuration object. public static T GetOrAddCustomConfiguration( this IHardwareGenerationConfiguration hardwareConfiguration, string key) where T : new() => hardwareConfiguration.CustomConfiguration.GetOrAddCustomConfiguration(key); /// /// Adds a public method that will be accessible from the host computer as hardware implementation. /// /// The type of the object that will be later fed to the proxy generator. /// An expression with a call to the method. public static void AddHardwareEntryPointMethod( this IHardwareGenerationConfiguration configuration, Expression> expression) => configuration.HardwareEntryPointMemberFullNames.Add(expression.GetMethodFullName()); /// /// Adds a public type the suitable methods of which will be accessible from the host computer as hardware /// implementation. /// /// The type of the object that will be later fed to the proxy generator. public static IHardwareGenerationConfiguration AddHardwareEntryPointType(this IHardwareGenerationConfiguration configuration) { // Object base methods are not needed. var excludedMethodNames = new[] { "Boolean Equals(System.Object)", "Int32 GetHashCode()", "System.Type GetType()", "System.String ToString()", }; // If we'd just add the type's name to HardwareEntryPointMemberNamePrefixes then types with just a different // suffix in their names would still be included. foreach (var method in typeof(T).GetMethods().Where(method => !excludedMethodNames.Contains(method.ToString()))) { configuration.HardwareEntryPointMemberFullNames.Add(method.GetFullName()); } return configuration; } // Properties could be added similarly once properties are supported for direct hardware invocation. This is // unlikely (since it wouldn't be of much use), though properties inside the generated hardware is already // supported. }