Skip to content

Commit

Permalink
Custom build rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-shepherd committed Feb 8, 2012
1 parent 8e02c28 commit 5ca2f7c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,10 @@ namespace MakeItSoLib
/// Holds information about a custom build rule on one file
/// of a C++ project.
/// </summary>
public class CustomBuildRuleInfo
public class CustomBuildRuleInfo_CPP
{
#region Public methods and properties

/// <summary>
/// Gets or sets the absolute path to the file the rule is run on.
/// </summary>
public string AbsolutePathToFile
{
get { return m_absolutePathToFile; }
set { m_absolutePathToFile = value; }
}

/// <summary>
/// Gets or sets the relative path to the file the rule is run on.
/// </summary>
Expand All @@ -31,15 +22,6 @@ public string RelativePathToFile
set { m_relativePathToFile = value; }
}

/// <summary>
/// Gets or sets the absolute path to the rule executable.
/// </summary>
public string AbsolutePathToExecutable
{
get { return m_absolutePathToExecutable; }
set { m_absolutePathToExecutable = value; }
}

/// <summary>
/// Gets or sets the relative path to the rule executable.
/// </summary>
Expand All @@ -66,14 +48,6 @@ public List<string> getParameters()
return m_parameters;
}

/// <summary>
/// Adds an absolute path to an output file generated by the rule.
/// </summary>
public void addAbsoluteOutputFile(string path)
{
m_absoluteOuputFiles.Add(path);
}

/// <summary>
/// Adds a relative path to an output file generated by the rule.
/// </summary>
Expand All @@ -82,15 +56,6 @@ public void addRelativeOutputFile(string path)
m_relativeOuputFiles.Add(path);
}

/// <summary>
/// Returns the collection of absolute paths to output files
/// generated from the rule.
/// </summary>
public List<string> getAbsoluteOutputFiles()
{
return m_absoluteOuputFiles;
}

/// <summary>
/// Returns the collection of relative paths to output files
/// generated from the rule.
Expand All @@ -105,21 +70,17 @@ public List<string> getRelativeOutputFiles()
#region Private data

// The path to the file which the rule is to be run on,
// absolute and relative to the project root...
private string m_absolutePathToFile = "";
// relative to the project root...
private string m_relativePathToFile = "";

// The path to the executable to run for the custom rule,
// absolute and relative to the project root...
private string m_absolutePathToExecutable = "";
// relative to the project root...
private string m_relativePathToExecutable = "";

// The collection of parameters to pass to the custom build rule...
private List<string> m_parameters = new List<string>();

// The collection of output files, absolute and relative to the
// project roor...
private List<string> m_absoluteOuputFiles = new List<string>();
// The collection of output files, relative to the project root...
private List<string> m_relativeOuputFiles = new List<string>();

#endregion
Expand Down
2 changes: 1 addition & 1 deletion MakeItSoLib/MakeItSoLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CustomBuildRuleInfo.cs" />
<Compile Include="CustomBuildRuleInfo_CPP.cs" />
<Compile Include="FileInfo.cs" />
<Compile Include="ProjectConfigurationInfo_CSharp.cs" />
<Compile Include="ProjectInfo.cs" />
Expand Down
1 change: 1 addition & 0 deletions MakeItSoLib/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public static string convertLinuxLibraryNameToRawName(string libraryName)
/// Splits the string passed in by the delimiters passed in.
/// Quoted sections are not split, and all tokens have whitespace
/// trimmed from the start and end.
/// </summary>
public static List<string> split(string stringToSplit, params char[] delimiters)
{
List<string> results = new List<string>();
Expand Down
81 changes: 81 additions & 0 deletions SolutionParser_VS2008/ProjectParser_CPP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ private void parseProject_SourceFiles()
/// </summary>
private void parseCustomBuildRule(VCFile file)
{
// We find the relative path to the file from the project root...
string path = Utils.call(() => (file.FullPath));
string relativePath = Utils.makeRelativePath(m_projectInfo.RootFolderAbsolute, path);

// The custom build rules (if there are any) are per configuration. So
// we loop through the configurations for this file...
IVCCollection configurations = Utils.call(() => (file.FileConfigurations as IVCCollection));
Expand All @@ -468,10 +472,56 @@ private void parseCustomBuildRule(VCFile file)
continue;
}

// We will store info about this rule in a CustomBuildRuleInfo_CPP object...
CustomBuildRuleInfo_CPP ruleInfo = new CustomBuildRuleInfo_CPP();
ruleInfo.RelativePathToFile = relativePath;

// There is a custom build rule, so we parse it...
string commandLine = Utils.call(() => (rule.CommandLine));
string expandedCommandLine = Utils.call(() => (configuration.Evaluate(commandLine)));

// We find the collection of output files generated by the rule...
string outputs = Utils.call(() => (rule.Outputs));
string expandedOutputs = Utils.call(() => (configuration.Evaluate(outputs)));

// The command-line may contain references to custom properties for
// the rule. These will be in square brackets,like [property]. We find
// these and replace the property markers in the command-line...
Dictionary<string, string> properties = getCustomBuildRuleProperties(rule);
foreach (KeyValuePair<string, string> property in properties)
{
// We replace values in the command line, and in the output files...
string tokenToReplace = "[" + property.Key + "]";
expandedCommandLine = expandedCommandLine.Replace(tokenToReplace, property.Value);
expandedOutputs = expandedOutputs.Replace(tokenToReplace, property.Value);
}

// We find the collection of output files, changing the paths to
// be in Linux format...
foreach (string outputFile in expandedOutputs.Split(';'))
{
string convertedPath = Utils.makeRelativePath(m_projectInfo.RootFolderAbsolute, outputFile);
ruleInfo.addRelativeOutputFile(convertedPath);
}

// We split the command-line to find the executable and the parameters...
List<string> splitCommandLine = Utils.split(expandedCommandLine, ' ');

// The executable...
if (splitCommandLine.Count >= 1)
{
string ruleExecutable = splitCommandLine[0];
string relativePathToExecutable = Utils.makeRelativePath(m_projectInfo.RootFolderAbsolute, ruleExecutable);
ruleInfo.RelativePathToExecutable = relativePathToExecutable;
}

// The parameters...
for (int j = 1; j < splitCommandLine.Count; ++j)
{
string parameter = splitCommandLine[j];
}


}


Expand All @@ -491,6 +541,37 @@ private void parseCustomBuildRule(VCFile file)
// expandedOutputs = expandedOutputs.Replace(propertyName, propertyValue);
}

/// <summary>
/// Finds the collection of properties associated with the custom build
/// rule passed in. These are passed back in a map of:
/// PropertyName => PropertyValueString
/// </summary>
private Dictionary<string, string> getCustomBuildRuleProperties(VCCustomBuildRule rule)
{
Dictionary<string, string> results = new Dictionary<string, string>();

// We loop through the collection of properties for the rule...
IVCCollection properties = Utils.call(() => (rule.Properties));
int numProperties = Utils.call(() => (properties.Count));
for (int i = 1; i <= numProperties; ++i)
{
VCRuntimeProperty property = Utils.call(() => (properties.Item(i) as VCRuntimeProperty));

// We find the property's name...
string name = property.Name;

// We have to use this rather bizarre method to get the property's value.
// (It seems that the property is a dynamically created COM object.)
Type ruleType = rule.GetType();
object valueAsObject = Utils.call(() => (ruleType.InvokeMember(name, BindingFlags.GetProperty, null, rule, null)));
string valueAsString = valueAsObject.ToString();

results.Add(name, valueAsString);
}

return results;
}

#endregion

#region Private data
Expand Down

0 comments on commit 5ca2f7c

Please sign in to comment.