private static void GenerateHeaderGuard(string SearchPath, params string[] Extension) { string[] files = FileSystemUtilites.GetAllFiles(SearchPath, Extension); foreach (string filePath in files) { if (IsForbiddenPath(filePath)) { continue; } List <string> lines = new List <string>(File.ReadAllLines(filePath)); string macroName = Path.GetFileName(filePath).ToUpper().Replace('.', '_'); string controlStatement = "#ifndef " + macroName; string defineStatement = "#define " + macroName; string endStatement = "#endif // " + macroName; int i = Find(controlStatement, lines); if (i != 1) { if (i != -1) { lines.RemoveAt(i); } lines.Insert(1, controlStatement); } i = Find(defineStatement, lines); if (i != 2) { if (i != -1) { lines.RemoveAt(i); } lines.Insert(2, defineStatement); } i = Find(endStatement, lines); if (i != lines.Count - 1) { if (i != -1) { lines.RemoveAt(i); } lines.Add(endStatement); } File.WriteAllLines(filePath, lines.ToArray()); } }
private static void GenerateLicenseAgreement(string SearchPath, params string[] Extension) { string[] files = FileSystemUtilites.GetAllFiles(SearchPath, Extension); foreach (string filePath in files) { if (IsForbiddenPath(filePath)) { continue; } List <string> lines = new List <string>(File.ReadAllLines(filePath)); int i = Find("// Copyright", lines); if (i == 0) { lines[0] = Copyright_Text; } else { if (i != -1) { lines.RemoveAt(i); } lines.Insert(0, Copyright_Text); } i = Find(PragmaOnce_Text, lines); if (i > -1) { lines.RemoveAt(i); } File.WriteAllLines(filePath, lines.ToArray()); } }
private static void AddAllInclusionsFromDependencies(CPPProject.Profile Profile, SourceBuilder Builder) { if (Builder == null) { return; } if (Builder.SelectedRule.LibraryUseType != BuildRules.LibraryUseTypes.UseOnly) { Profile.AddPreprocessorDefinition(BuildSystemHelper.GetAPIPreprocessor(Builder.BuildRule.ModuleName, (Builder.SelectedRule.LibraryUseType == BuildRules.LibraryUseTypes.DynamicLibrary ? BuildSystemHelper.APIPreprocessorTypes.Import : BuildSystemHelper.APIPreprocessorTypes.Empty))); Profile.AddPreprocessorDefinition(BuildSystemHelper.GetExternPreprocessor(Builder.BuildRule.ModuleName, BuildSystemHelper.ExternPreprocessorTypes.Empty)); string[] libFiles = FileSystemUtilites.GetAllFiles(Builder.BinariesPath, "*" + EnvironmentHelper.StaticLibraryExtentions); if (libFiles != null) { foreach (string libFile in libFiles) { Profile.AddIncludeLibraries(libFile); } } } if (Builder.SelectedRule.DependencyModulesName == null) { return; } foreach (string dep in Builder.SelectedRule.DependencyModulesName) { SourceBuilder builder = BuildSystem.GetSourceBuilder(dep); AddAllInclusionsFromDependencies(Profile, builder); } }
private static void IncludeFilesCorrection(string SearchPath, params string[] Extension) { string[] files = FileSystemUtilites.GetAllFiles(SearchPath, Extension); foreach (string filePath in files) { if (IsForbiddenPath(filePath)) { continue; } List <string> lines = new List <string>(File.ReadAllLines(filePath)); for (int i = 0; i < lines.Count; i++) { string line = lines[i]; if (!line.StartsWith("#include")) { continue; } int index = line.IndexOf('"'); if (index != -1) { line = line.Remove(index, 1).Insert(index, "<"); index = line.IndexOf('"'); line = line.Remove(index, 1).Insert(index, ">"); } line = line.Replace('/', '\\'); if (!line.ToLower().Contains("public") && !line.ToLower().Contains("private")) { index = line.IndexOf('<'); if (index == -1) { continue; } index++; string file = line.Substring(index, line.IndexOf('>') - index); string[] foundFiles = FileSystemUtilites.GetAllFiles(SearchPath, Path.GetFileName(file)); if (foundFiles.Length == 0) { continue; } string includeFilePath = foundFiles[0]; if (IsThirdpartyPath(includeFilePath) || IsIntermediatePath(includeFilePath)) { continue; } bool isPublic = includeFilePath.Contains("Public"); line = line.Insert(index, (isPublic ? "Public" : "Private") + EnvironmentHelper.PathSeparator); } lines[i] = line; } File.WriteAllLines(filePath, lines.ToArray()); } }
private bool MustCompile() { if (!File.Exists(EnvironmentHelper.FinalOutputDirectory + SelectedRule.TargetName + GetExtension(this))) { return(true); } string hashesFilePath = intermediateModulePath + HashesFileName; ISerializeObject hashesData = null; if (File.Exists(hashesFilePath)) { hashesData = Creator.Create <ISerializeObject>(File.ReadAllText(hashesFilePath)); } if (hashesData == null) { hashesData = Creator.Create <ISerializeObject>(); } bool result = false; string configurationTypeName = typeof(ProjectBase.ProfileBase.BuildConfigurations).Name; if (!hashesData.Contains(configurationTypeName) || hashesData.Get <int>(configurationTypeName) != (int)BuildSystem.BuildConfiguration) { result = true; } hashesData.Set(configurationTypeName, (int)BuildSystem.BuildConfiguration); string platformTypeName = typeof(ProjectBase.ProfileBase.PlatformTypes).Name; if (!hashesData.Contains(platformTypeName) || hashesData.Get <int>(platformTypeName) != (int)BuildSystem.PlatformType) { result = true; } hashesData.Set(platformTypeName, (int)BuildSystem.PlatformType); if (!result) { List <string> extensions = new List <string>(); extensions.AddRange(EnvironmentHelper.HeaderFileExtensions); extensions.AddRange(EnvironmentHelper.CompileFileExtensions); extensions.AddRange(EnvironmentHelper.CSharpFileExtensions); string[] files = FileSystemUtilites.GetAllFiles(sourcePathRoot, extensions.ToArray()); foreach (string file in files) { string filePathHash = GetHash(file).ToString(); int contentHash = GetHash(File.ReadAllText(file)); if (hashesData.Contains(filePathHash) && hashesData.Get <int>(filePathHash) == contentHash) { continue; } hashesData.Set(filePathHash, contentHash); result = true; } } File.WriteAllText(hashesFilePath, hashesData.Content); return(result); }
private void GenerateAndBuildProjectFile(bool ForceToRebuild) { LogCurrentInfo(); if (!ForceToRebuild && !MustCompile()) { state = States.AlreadyUpdated; return; } CPPProject cppProj = new CPPProject(); CPPProject.Profile profile = (CPPProject.Profile)cppProj.CreateProfile(); profile.AssemblyName = SelectedRule.TargetName; profile.OutputType = BuildSystemHelper.LibraryUseTypesToOutputType(SelectedRule.LibraryUseType); profile.OutputPath = BinariesPath; profile.Optimization = CPPProject.Profile.Optimizations.Disabled; profile.MinimalRebuild = true; profile.PlatformType = BuildSystem.PlatformType; profile.BuildConfiguration = BuildSystem.BuildConfiguration; if (profile.BuildConfiguration == ProjectBase.ProfileBase.BuildConfigurations.Debug) { profile.GenerateDebugInformation = true; //if (SelectedRule.LibraryUseType == BuildRules.LibraryUseTypes.Executable) // profile.RuntimeLibrary = CPPProject.Profile.RuntimeLibraries.MultiThreadedDebug; //else profile.RuntimeLibrary = CPPProject.Profile.RuntimeLibraries.MultiThreadedDebugDLL; } else if (profile.BuildConfiguration == ProjectBase.ProfileBase.BuildConfigurations.Release) { profile.GenerateDebugInformation = false; //if (SelectedRule.LibraryUseType == BuildRules.LibraryUseTypes.Executable) // profile.RuntimeLibrary = CPPProject.Profile.RuntimeLibraries.MultiThreaded; //else profile.RuntimeLibrary = CPPProject.Profile.RuntimeLibraries.MultiThreadedDLL; } profile.AddIncludeDirectories(EnvironmentHelper.ProcessDirectory); profile.AddIncludeDirectories(sourcePathRoot); profile.AddIncludeDirectories(generatedFilesPath); if (SelectedRule.DependencyModulesName != null) { foreach (string dep in SelectedRule.DependencyModulesName) { AddDependency(profile, dep); } } if (SelectedRule.GenerateReflection) { AddDependency(profile, BuildSystemHelper.ReflectionModuleName); } profile.AddPreprocessorDefinition(BuildSystemHelper.GetModuleNamePreprocessor(BuildRule.ModuleName)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetAPIPreprocessor(BuildRule.ModuleName, BuildSystemHelper.APIPreprocessorTypes.Export)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetExternPreprocessor(BuildRule.ModuleName, BuildSystemHelper.ExternPreprocessorTypes.Fill)); if (SelectedRule.PreprocessorDefinitions != null) { foreach (string def in SelectedRule.PreprocessorDefinitions) { profile.AddPreprocessorDefinition(def); } } profile.AddPreprocessorDefinition(BuildSystemHelper.GetConfigurationModePreprocessor(BuildSystem.BuildConfiguration)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetPlatformPreprocessor(EnvironmentHelper.Platform)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetPlatformTypesPreprocessor(BuildSystem.PlatformType)); if (SelectedRule.DependencyStaticLibraries != null) { foreach (string lib in SelectedRule.DependencyStaticLibraries) { profile.AddIncludeLibraries(lib); } } string[] files = FileSystemUtilites.GetAllFiles(sourcePathRoot, EnvironmentHelper.HeaderFileExtensions); foreach (string file in files) { cppProj.AddIncludeFile(file); string outputBasePath = generatedFilesPath + Path.GetFileNameWithoutExtension(file); if (SelectedRule.GenerateReflection) { string outputBaseFileName = outputBasePath + ".Reflection"; if (ParseForReflection(file, outputBaseFileName)) { cppProj.AddIncludeFile(outputBaseFileName + ".h"); cppProj.AddCompileFile(outputBaseFileName + ".cpp"); } } if (SelectedRule.GenerateWrapper) { string outputBaseFileName = outputBasePath + ".Wrapper"; if (ParseForWrapper(file, outputBaseFileName)) { cppProj.AddIncludeFile(outputBaseFileName + ".h"); cppProj.AddCompileFile(outputBaseFileName + ".cpp"); wrapperCSFiles.Add(outputBaseFileName + ".cs"); } } } if (SelectedRule.IncludeModulesName != null) { foreach (string moduleName in SelectedRule.IncludeModulesName) { SourceBuilder builder = BuildSystem.GetSourceBuilder(moduleName); if (builder == null) { continue; } profile.AddIncludeDirectories(builder.sourcePathRoot); profile.AddPreprocessorDefinition(BuildSystemHelper.GetAPIPreprocessor(builder.BuildRule.ModuleName, BuildSystemHelper.APIPreprocessorTypes.Empty)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetExternPreprocessor(builder.BuildRule.ModuleName, BuildSystemHelper.ExternPreprocessorTypes.Empty)); } } if (SelectedRule.AdditionalIncludeDirectory != null) { foreach (string dir in SelectedRule.AdditionalIncludeDirectory) { string dirInfo = FileSystemUtilites.PathSeperatorCorrection(dir); if (dirInfo.StartsWith("" + EnvironmentHelper.PathSeparator)) { dirInfo = dirInfo.Substring(1); } profile.AddIncludeLibraryDirectories(sourcePathRoot + dirInfo); } } files = FileSystemUtilites.GetAllFiles(sourcePathRoot, EnvironmentHelper.CompileFileExtensions); foreach (string file in files) { cppProj.AddCompileFile(file); } if (SelectedRule.AdditionalCompileFile != null) { foreach (string file in SelectedRule.AdditionalCompileFile) { cppProj.AddCompileFile(EnvironmentHelper.ProcessDirectory + file); } } profile.IntermediatePath = intermediateModulePath; BuildProjectFile(profile); }
public static bool Create() { RuleLibraryBuilder rulesBuilder = new RuleLibraryBuilder(EnvironmentHelper.ProcessDirectory); if (!rulesBuilder.Build()) { return(false); } CPPProject projectFile = new CPPProject(); foreach (ProjectBase.ProfileBase.BuildConfigurations configuration in BuildConfigurations) { foreach (ProjectBase.ProfileBase.PlatformTypes platform in PlatformTypes) { foreach (BuildRules buildRule in rulesBuilder.Rules) { foreach (BuildRules.RuleBase rule in buildRule.Rules) { if (rule.LibraryUseType != BuildRules.LibraryUseTypes.Executable) { continue; } CPPProject.Profile profile = (CPPProject.Profile)projectFile.CreateProfile(); profile.Name = buildRule.ModuleName; profile.BuildConfiguration = configuration; profile.PlatformType = platform; profile.OutputType = ProjectBase.ProfileBase.OutputTypes.Makefile; profile.OutputPath = EnvironmentHelper.FinalOutputDirectory + rule.TargetName + EnvironmentHelper.ExecutableExtentions; profile.IntermediatePath = EnvironmentHelper.IntermediateDirectory; profile.NMakeBuildCommandLine = string.Format("\"$(SolutionDir)Binaries/Frontend.exe\" -Action BuildEngine -Architecture {0} -Configuration {1}", platform, configuration); profile.NMakeReBuildCommandLine = string.Format("\"$(SolutionDir)Binaries/Frontend.exe\" -Action RebuildEngine -Architecture {0} -Configuration {1}", platform, configuration); profile.NMakeCleanCommandLine = string.Format("\"$(SolutionDir)Binaries/Frontend.exe\" -Action CleanEngine -Architecture {0} -Configuration {1}", platform, configuration); //profile.AddIncludeDirectories("$(ProjectDir)"); foreach (BuildRules buildRule1 in rulesBuilder.Rules) { foreach (BuildRules.RuleBase rule1 in buildRule1.Rules) { profile.AddIncludeDirectories(FileSystemUtilites.GetParentDirectory(buildRule1.Path)); profile.AddIncludeDirectories(FileSystemUtilites.PathSeperatorCorrection(buildRule1.Path)); profile.AddIncludeDirectories(FileSystemUtilites.PathSeperatorCorrection(profile.IntermediatePath + rule1.TargetName + EnvironmentHelper.PathSeparator + BuildSystemHelper.GeneratedPathName)); if (rule1.IncludesPath != null) { foreach (string includePath in rule1.IncludesPath) { profile.AddIncludeDirectories(FileSystemUtilites.PathSeperatorCorrection(buildRule1.Path + includePath)); } } profile.AddPreprocessorDefinition(BuildSystemHelper.GetAPIPreprocessor(rule1.TargetName, BuildSystemHelper.APIPreprocessorTypes.Empty)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetExternPreprocessor(rule1.TargetName, BuildSystemHelper.ExternPreprocessorTypes.Empty)); if (rule1.PreprocessorDefinitions != null) { foreach (string pd in rule1.PreprocessorDefinitions) { profile.AddPreprocessorDefinition(pd); } } } } profile.AddPreprocessorDefinition(BuildSystemHelper.GetConfigurationModePreprocessor(configuration)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetPlatformPreprocessor(EnvironmentHelper.Platform)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetPlatformTypesPreprocessor(platform)); profile.AddPreprocessorDefinition(BuildSystemHelper.GetModuleNamePreprocessor("")); } } } } string[] files = FileSystemUtilites.GetAllFiles(WorkingDirectory, EnvironmentHelper.CSharpFileExtensions); foreach (string file in files) { projectFile.AddExtraFile(file); } files = FileSystemUtilites.GetAllFiles(WorkingDirectory, EnvironmentHelper.HeaderFileExtensions); foreach (string file in files) { projectFile.AddIncludeFile(file); } files = FileSystemUtilites.GetAllFiles(WorkingDirectory, EnvironmentHelper.CompileFileExtensions); foreach (string file in files) { projectFile.AddCompileFile(file); } MicrosoftVCProjectGenerator generator = new MicrosoftVCProjectGenerator(); generator.ToolsVersion = MSBuildProcess.Info.ToolsVersion; File.WriteAllText(ProjectFilePath, generator.Generate(projectFile, true)); File.WriteAllText(ProjectFilePath + ".filters", generator.GenerateFilter(projectFile, WorkingDirectory)); return(true); }
public bool Build() { string projectDir = EnvironmentHelper.IntermediateDirectory + ProjectName + EnvironmentHelper.PathSeparator; if (!Directory.Exists(projectDir)) { Directory.CreateDirectory(projectDir); } CSProject csproj = new CSProject(); CSProject.Profile profile = (CSProject.Profile)csproj.CreateProfile(); profile.FrameworkVersion = CSProject.Profile.FrameworkVersions.v4_5; profile.AssemblyName = ProjectName; profile.OutputPath = projectDir + "Build" + EnvironmentHelper.PathSeparator; profile.IntermediatePath = projectDir; profile.OutputType = ProjectBase.ProfileBase.OutputTypes.DynamicLinkLibrary; csproj.AddReferenceBinaryFile(Assembly.GetExecutingAssembly().Location); string[] files = FileSystemUtilites.GetAllFiles(processDirectory, "*" + BuildRules.BuildRuleFilePostfix); DateTime startTime = DateTime.Now; ConsoleHelper.WriteInfo("Building rules starts at " + startTime.ToString()); ConsoleHelper.WriteInfo("Found rules :"); if (files.Length == 0) { ConsoleHelper.WriteInfo("No building rules found, aborting process"); return(false); } foreach (string rules in files) { csproj.AddCompileFile(rules); ConsoleHelper.WriteInfo("\t" + Path.GetFileName(rules)); } Compiler compiler = new Compiler(); compiler.ErrorRaised += OnError; if (compiler.Build(profile)) { Assembly rulesLibrary = Assembly.LoadFile(profile.OutputPath + ProjectName + EnvironmentHelper.DynamicLibraryExtentions); List <string> buildRulesFiles = new List <string>(); List <BuildRules> buildRules = new List <BuildRules>(); foreach (string buildRuleName in files) { string fileName = Path.GetFileNameWithoutExtension(buildRuleName); string typeName = fileName.Replace(".", ""); Type type = rulesLibrary.GetType(BuildRules.NamespacePrefix + typeName); if (type == null) { ConsoleHelper.WriteWarning("In " + fileName + ", type " + typeName + " doesn't exists, building related module will be ignore"); continue; } buildRulesFiles.Add(buildRuleName); BuildRules buildRule = (BuildRules)Activator.CreateInstance(type); buildRule.Path = FileSystemUtilites.PathSeperatorCorrection(Path.GetDirectoryName(buildRuleName)) + EnvironmentHelper.PathSeparator; Type[] types = buildRule.GetType().GetNestedTypes(); buildRule.Rules = new BuildRules.RuleBase[types.Length]; for (int i = 0; i < types.Length; ++i) { buildRule.Rules[i] = (BuildRules.RuleBase)Activator.CreateInstance(types[i]); } buildRules.Add(buildRule); } RulesFiles = buildRulesFiles.ToArray(); Rules = buildRules.ToArray(); return(true); } ConsoleHelper.WriteInfo("Building rules takes " + (DateTime.Now - startTime).ToHHMMSS()); return(false); }