Class that embeds an arbitrary file into a static library.
The static library then export two symbols: one for the data and one for the size.
private Dictionary <string, string> CreateBundle(DataLibraryCreator creator, string assembly, bool isText) { Dictionary <String, String> bundle = new Dictionary <String, String>(); // Create the static library for the assembly creator.Compress = this.Compress && !isText; creator.CreateStaticLibrary(assembly, isText); bundle[KEY_FILE] = assembly; bundle[KEY_NAME] = Path.GetFileName(assembly); bundle[KEY_SYMBOL] = creator.SymbolName; bundle[KEY_LIBRARY] = creator.OutputFile; bundle[KEY_INPUT_SIZE] = creator.InputSize.ToString(); bundle[KEY_OUTPUT_SIZE] = creator.OutputSize.ToString(); return(bundle); }
private Dictionary<string, string> CreateBundle(DataLibraryCreator creator, string assembly, bool isText) { Dictionary<String, String> bundle = new Dictionary<String, String>(); // Create the static library for the assembly creator.Compress = this.Compress && !isText; creator.CreateStaticLibrary(assembly, isText); bundle[KEY_FILE] = assembly; bundle[KEY_NAME] = Path.GetFileName(assembly); bundle[KEY_SYMBOL] = creator.SymbolName; bundle[KEY_LIBRARY] = creator.OutputFile; bundle[KEY_INPUT_SIZE] = creator.InputSize.ToString(); bundle[KEY_OUTPUT_SIZE] = creator.OutputSize.ToString(); return bundle; }
/// <summary> /// Generates the native executable. /// </summary> /// <param name = "directory">The directory.</param> /// <returns></returns> public String Generate(String directory) { NativeContext nativeContext = new NativeContext(this.TargetOSVersion, this.TargetArchitecture); if (this.NativeCompiler == null) { this.NativeCompiler = nativeContext.Compiler; } // We embed: // - the assemblies (with their configuration) // - the machine configuration // // For every embedded assembly, we store: // - the name of the assembly // - the pretty name of the assembly (for inclusion in source code) // - the path to the static library that embed the assembly // - the path to the static library that embed the assembly configuration file List <Dictionary <String, String> > assemblies = new List <Dictionary <string, string> >(); List <Dictionary <String, String> > configurations = new List <Dictionary <string, string> >(); // Create an instance of creator DataLibraryCreator creator = new DataLibraryCreator(); creator.Logger = this.Logger; creator.ArchitectureFlags = nativeContext.ArchitectureFlags; creator.OutputDirectory = directory; this.Logger.LogInfo("Creating static libraries..."); // For each assembly and its config, generate a native library); foreach (String assembly in this.Assemblies) { String name = Path.GetFileName(assembly); this.Logger.LogInfo("Processing static library " + name); Dictionary <string, string> bundle = CreateBundle(creator, assembly, false); assemblies.Add(bundle); String config = assembly + ".config"; if (File.Exists(config)) { this.Logger.LogInfo("Processing configuration for " + name); bundle = CreateBundle(creator, config, true); bundle[KEY_ASSEMBLY] = name; configurations.Add(bundle); } } // Store the main image String mainImage = assemblies[0][KEY_NAME]; // Sort all the symbols assemblies.Sort((D1, D2) => D1[KEY_SYMBOL].CompareTo(D2[KEY_SYMBOL])); configurations.Sort((D1, D2) => D1[KEY_SYMBOL].CompareTo(D2[KEY_SYMBOL])); // Generate a native library for the machine configuration Dictionary <String, String> machineConfig = null; if (this.MachineConfiguration != null) { machineConfig = CreateBundle(creator, this.MachineConfiguration, true); } this.Logger.LogInfo("Creating source file..."); // Generate the main source file String mainSource = Path.Combine(directory, "main.c"); this.GenerateMainSource(mainSource, mainImage, assemblies, configurations, machineConfig); // Dump the header file nativeContext.WriteHeader(directory); // Dump the library file nativeContext.WriteLibrary(directory); // Stage 1: Preparation of common properties String mainObject = Path.Combine(directory, "main.o"); String executableFile = Path.Combine(directory, Path.GetFileNameWithoutExtension(mainImage)); String nativeOptions = String.Format(" {0} {1} ", nativeContext.ArchitectureFlags, nativeContext.SDKFlags); // Stage 2: Compilation this.Compile(directory, mainSource, mainObject, nativeOptions); // Stage 3: Linkage this.Link(directory, mainObject, executableFile, nativeOptions, assemblies, configurations, machineConfig); this.Logger.LogInfo("Embedding done"); return(executableFile); }
/// <summary> /// Generates the native executable. /// </summary> /// <param name = "directory">The directory.</param> /// <returns></returns> public String Generate(String directory) { NativeContext nativeContext = new NativeContext(this.TargetOSVersion, this.TargetArchitecture); if (this.NativeCompiler == null) { this.NativeCompiler = nativeContext.Compiler; } // We embed: // - the assemblies (with their configuration) // - the machine configuration // // For every embedded assembly, we store: // - the name of the assembly // - the pretty name of the assembly (for inclusion in source code) // - the path to the static library that embed the assembly // - the path to the static library that embed the assembly configuration file List<Dictionary<String, String>> assemblies = new List<Dictionary<string, string>>(); List<Dictionary<String, String>> configurations = new List<Dictionary<string, string>>(); // Create an instance of creator DataLibraryCreator creator = new DataLibraryCreator(); creator.Logger = this.Logger; creator.ArchitectureFlags = nativeContext.ArchitectureFlags; creator.OutputDirectory = directory; this.Logger.LogInfo("Creating static libraries..."); // For each assembly and its config, generate a native library); foreach (String assembly in this.Assemblies) { String name = Path.GetFileName(assembly); this.Logger.LogInfo("Processing static library " + name); Dictionary<string, string> bundle = CreateBundle(creator, assembly, false); assemblies.Add(bundle); String config = assembly + ".config"; if (File.Exists(config)) { this.Logger.LogInfo("Processing configuration for " + name); bundle = CreateBundle(creator, config, true); bundle[KEY_ASSEMBLY] = name; configurations.Add(bundle); } } // Store the main image String mainImage = assemblies[0][KEY_NAME]; // Sort all the symbols assemblies.Sort((D1, D2) => D1[KEY_SYMBOL].CompareTo(D2[KEY_SYMBOL])); configurations.Sort((D1, D2) => D1[KEY_SYMBOL].CompareTo(D2[KEY_SYMBOL])); // Generate a native library for the machine configuration Dictionary<String, String> machineConfig = null; if (this.MachineConfiguration != null) { machineConfig = CreateBundle(creator, this.MachineConfiguration, true); } this.Logger.LogInfo("Creating source file..."); // Generate the main source file String mainSource = Path.Combine(directory, "main.c"); this.GenerateMainSource(mainSource, mainImage, assemblies, configurations, machineConfig); // Dump the header file nativeContext.WriteHeader(directory); // Dump the library file nativeContext.WriteLibrary(directory); // Stage 1: Preparation of common properties String mainObject = Path.Combine(directory, "main.o"); String executableFile = Path.Combine(directory, Path.GetFileNameWithoutExtension(mainImage)); String nativeOptions = String.Format(" {0} {1} ", nativeContext.ArchitectureFlags, nativeContext.SDKFlags); // Stage 2: Compilation this.Compile(directory, mainSource, mainObject, nativeOptions); // Stage 3: Linkage this.Link(directory, mainObject, executableFile, nativeOptions, assemblies, configurations, machineConfig); this.Logger.LogInfo("Embedding done"); return executableFile; }