Skip to content

Commit c441957

Browse files
committed
Fixed some bugs, added an option to generate inst files next to the headers.
1 parent b216a7a commit c441957

File tree

6 files changed

+90
-30
lines changed

6 files changed

+90
-30
lines changed

build_rules/Xt_Build_Instances.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class Xt_Build_Instances : Task
1919
[Required] public String LinkToolCommand { get; set; }
2020
[Required] public ITaskItem[] CurrentProject {get; set; }
2121
[Required] public String SolutionFilePath { get; set; }
22+
[Required] public String Configuration { get; set; }
23+
[Required] public String Platform { get; set; }
2224

2325
class Node {
2426
public Project project = null;
@@ -81,11 +83,13 @@ private bool WalkProjectDependencies()
8183
{
8284
solution_file = SolutionFile.Parse(SolutionFilePath); // throws
8385

84-
// by default the projects are loaded without reference to a particular solution
86+
// By default the projects are loaded without reference to a particular solution
8587
// but if e.g an <Import .. /> references something relative to $(SolutionDir)
86-
// then the project will fail to load without explicitly setting that property here
88+
// then the project will fail to load without explicitly setting that property here.
89+
// The properties read from the projects may also depend on Configuration and Platform.
8790
project_collection = new ProjectCollection( new Dictionary<String, String> {
88-
{ "SolutionDir", Path.GetDirectoryName(SolutionFilePath) + "\\" }
91+
{ "SolutionDir", Path.GetDirectoryName(SolutionFilePath) + "\\" },
92+
{ "Configuration", Configuration }, { "Platform", Platform }
8993
});
9094

9195
string my_path = CurrentProject[0].GetMetadata("FullPath");
@@ -122,14 +126,15 @@ private bool WalkProjectDependencies()
122126
return true;
123127
}
124128

129+
// note: this is currently unused
125130
List<Node> GetChangedNodes()
126131
{
127132
// the link tool writes to a file which contains
128133
// the guids of projects that contain .xti-s that it added instantiations to
129134
// look up nodes corresponding to changed projects in the dependency graph
130135
var nodes = new List<Node>();
131136
string guid;
132-
System.IO.StreamReader file = new System.IO.StreamReader(ProjectListFile);
137+
StreamReader file = new StreamReader(ProjectListFile);
133138
while((guid = file.ReadLine()) != null) {
134139
Node node = null;
135140
if(!guid_to_node.TryGetValue(guid, out node)) {
@@ -141,6 +146,7 @@ List<Node> GetChangedNodes()
141146
//Project project = node.project;
142147
//Log.LogMessage(MessageImportance.High, "Found project {0} for {1}", project.FullPath, guid);
143148
}
149+
file.Dispose();
144150
return nodes;
145151
}
146152

@@ -151,24 +157,29 @@ List<string> FindInstFiles() {
151157
// by construction guid_to_node should only contain projects that
152158
// the root project references or depends on transitively
153159
foreach(Node node in guid_to_node.Values) {
154-
string[] props = {"Xt_InstFilePath", "Xt_HeaderCachePath", "Xt_InstSuffix"};
160+
string[] props = {"Xt_InstFilePath_FromHeader", "Xt_InstFilePath", "Xt_HeaderCachePath", "Xt_InstSuffix"};
155161
string[] values = props.Select(prop => node.project.GetPropertyValue(prop)).ToArray();
156162
if(values.Any(value => value == "")) continue;
157163
//for(int i = 0; i < props.Length; i++)
158164
//Log.LogMessage(MessageImportance.High, "{0} = {1}", props[i], values[i]);
159-
string inst_base_path = values[0], header_cache_path = values[1], inst_suffix = values[2];
165+
bool xti_from_header = Convert.ToBoolean(values[0]);
166+
string inst_base_path = values[1], header_cache_path = values[2], inst_suffix = values[3];
160167
string header_path;
161-
System.IO.StreamReader file = new System.IO.StreamReader(header_cache_path);
168+
StreamReader file = new StreamReader(header_cache_path);
162169
while((header_path = file.ReadLine()) != null) {
163-
string header_name = System.IO.Path.GetFileNameWithoutExtension(header_path);
164-
string xti_path = inst_base_path + header_name + inst_suffix;
170+
string xti_path = !xti_from_header ?
171+
(inst_base_path + Path.GetFileNameWithoutExtension(header_path) + inst_suffix)
172+
: Path.ChangeExtension(header_path, inst_suffix);
165173
//Log.LogMessage(MessageImportance.High, "xti_path = {0}", xti_path);
166174
if(!File.Exists(xti_path)) {
167175
Log.LogMessage(MessageImportance.High, "error: xti file does not exist: {0}", xti_path);
168176
files_missing = true;
169177
}
170178
inst_files.Add(xti_path);
171179
}
180+
// Xt_Headers_Read may touch this file right after we're done
181+
// so make sure the file gets closed first
182+
file.Dispose();
172183
}
173184
return files_missing ? null : inst_files;
174185
} catch (Exception e) {
@@ -179,12 +190,16 @@ List<string> FindInstFiles() {
179190

180191
bool Init()
181192
{
182-
if(!WalkProjectDependencies())
193+
if(!WalkProjectDependencies()) {
194+
Log.LogMessage(MessageImportance.High, "failed to walk dependencies");
183195
return false;
196+
}
184197

185198
List<string> inst_files = FindInstFiles();
186-
if(inst_files == null)
199+
if(inst_files == null) {
200+
Log.LogMessage(MessageImportance.High, "no inst files found");
187201
return false;
202+
}
188203

189204
string command = LinkToolCommand + " \"" + String.Join(";", inst_files) + "\"";
190205
Log.LogMessage(MessageImportance.High, "xt_inst_gen link: {0}", command);

build_rules/xt_inst_gen.targets

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Xt_ClangPath Condition="'$(Xt_ClangPath)' ==''" >c:\Program Files\LLVM\bin</Xt_ClangPath>
5-
<Xt_TargetsPath Condition="'$(Xt_TargetsPath)' ==''" >$(SolutionDir)build_rules\</Xt_TargetsPath>
5+
<Xt_TargetsPath Condition="'$(Xt_TargetsPath)' ==''" >c:\Program Files\export_template\etc\</Xt_TargetsPath>
66
<Xt_InstSuffix Condition="'$(Xt_InstSuffix)' ==''" >.xti</Xt_InstSuffix>
77
<Xt_InstFilePath Condition="'$(Xt_InstFilePath)' ==''" >$(SolutionDir)intermediate\src\$(ProjectName)\</Xt_InstFilePath>
8-
<Xt_InstGen_Path Condition="'$(Xt_InstGen_Path)' ==''" >xt_inst_gen.exe</Xt_InstGen_Path>
8+
<Xt_InstFilePath_FromHeader Condition="'$(Xt_InstFilePath_FromHeader)' ==''" >false</Xt_InstFilePath_FromHeader>
9+
<Xt_InstGen_Path Condition="'$(Xt_InstGen_Path)' ==''" >c:\Program Files\export_template\bin\xt_inst_gen.exe</Xt_InstGen_Path>
910
<Xt_IntDir_FullPath>$(MSBuildProjectDirectory)\$(IntDir)</Xt_IntDir_FullPath>
1011
<Xt_IntDir_FullPath Condition="$([System.IO.Path]::IsPathRooted('$(IntDir)'))">$(IntDir)</Xt_IntDir_FullPath>
1112
<Xt_HeaderCachePath Condition="'$(Xt_HeaderCachePath)'==''" >$(Xt_IntDir_FullPath)xt_header_cache.txt</Xt_HeaderCachePath>
1213
<Xt_ProjectListFile Condition="'$(Xt_ProjectListFile)'==''" >$(Xt_IntDir_FullPath)xt_project_list.txt</Xt_ProjectListFile>
1314
<Xt_BuildLogPath Condition="'$(Xt_BuildLogPath)' ==''" >$(Xt_IntDir_FullPath)$(MSBuildProjectName).log</Xt_BuildLogPath>
1415
</PropertyGroup>
1516

16-
<ItemDefinitionGroup>
17+
<ItemDefinitionGroup Condition="$(Xt_InstFilePath_FromHeader) == 'false'" >
1718
<ClCompile>
1819
<AdditionalIncludeDirectories>$(Xt_InstFilePath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
1920
</ClCompile>
@@ -35,6 +36,13 @@
3536
<Xt_CompileName>{$([System.Guid]::NewGuid())}</Xt_CompileName>
3637
</PropertyGroup>
3738

39+
<ItemDefinitionGroup>
40+
<Xt_Headers>
41+
<InstFilePath Condition="$(Xt_InstFilePath_FromHeader) == 'false'">$(Xt_InstFilePath)%(Filename)$(Xt_InstSuffix)</InstFilePath>
42+
<InstFilePath Condition="$(Xt_InstFilePath_FromHeader) == 'true'">%(RootDir)%(Directory)%(Filename)$(Xt_InstSuffix)</InstFilePath>
43+
</Xt_Headers>
44+
</ItemDefinitionGroup>
45+
3846
<Target Name="XT_Header_PreFilter" BeforeTargets="XT_Header_Filter">
3947
<ItemGroup>
4048
<!-- We can't just get all header files in the project directory because
@@ -59,20 +67,20 @@
5967

6068
<Target Name="Xt_Headers_Read" BeforeTargets="XT_Inst_Gen">
6169
<!-- the XT_Header_Filter doesn't create a cache file if there are no headers in the project,
62-
but the Xt_Build_Instances assumes that a missing cache file is an error,
63-
so create it if it doesn't already exist !-->
70+
but the Xt_Build_Instances assumes that a missing cache file is an error,
71+
so create it if it doesn't already exist !-->
6472
<WriteLinesToFile File="$(Xt_HeaderCachePath)" Overwrite="false" />
6573
<ReadLinesFromFile File="$(Xt_HeaderCachePath)"> <Output TaskParameter="Lines" ItemName="Xt_Headers"/> </ReadLinesFromFile>
6674
<Message Text="read headers: @(Xt_Headers)" Importance="High"/>
6775
</Target>
6876

6977
<!-- In order for a change to the .xti file to trigger building the .cu file,
7078
the xti file must be added to the .cu file's .deps file, generated by the AddCudaCompileDeps target. !-->
71-
<Target Name="XT_Inst_Gen" BeforeTargets="ClCompile;AddCudaCompileDeps" Inputs="@(Xt_Headers)" Outputs="@(Xt_Headers->'$(Xt_InstFilePath)%(Filename)$(Xt_InstSuffix)')">
79+
<Target Name="XT_Inst_Gen" BeforeTargets="ClCompile;AddCudaCompileDeps" Inputs="@(Xt_Headers)" Outputs="@(Xt_Headers->'%(InstFilePath)')">
7280
<!--
7381
Add temporary ClCompile/Link items in order to read default metadata that's defined in
7482
ItemDefinitionGroup, then remove the items.
75-
-->
83+
!-->
7684
<ItemGroup>
7785
<ClCompile Include="$(Xt_CompileName)" />
7886
<Xt_CompDef Include="@(ClCompile->WithMetadataValue('Identity', '$(Xt_CompileName)'))" />
@@ -86,7 +94,7 @@
8694

8795
<ItemGroup>
8896
<Xt_Comp_Set Include="@(Xt_Headers)">
89-
<Command>$(Xt_Command) comp "%(FullPath)" "$(Xt_InstFilePath)%(Filename)$(Xt_InstSuffix)" "$(Xt_PreprocessorDefinitions)" "$(Xt_AdditionalIncludeDirectories)" "$(ProjectGuid)"</Command>
97+
<Command>$(Xt_Command) comp "%(FullPath)" "%(Xt_Headers.InstFilePath)" "$(Xt_PreprocessorDefinitions)" "$(Xt_AdditionalIncludeDirectories)" "$(ProjectGuid)"</Command>
9098
</Xt_Comp_Set>
9199
</ItemGroup>
92100

@@ -116,6 +124,8 @@
116124
<LinkToolCommand ParameterType="System.String" Required="true" />
117125
<ProjectListFile ParameterType="System.String" Required="true" />
118126
<SolutionFilePath ParameterType="System.String" Required="true" />
127+
<Configuration ParameterType="System.String" Required="true" />
128+
<Platform ParameterType="System.String" Required="true" />
119129
</ParameterGroup>
120130
<Task>
121131
<!-- for the CL and Link tasks !-->
@@ -139,7 +149,8 @@
139149
</PropertyGroup>
140150

141151
<Message Text="there were link errors - generating template instantiations if necessary" Importance="High"/>
142-
<Xt_Build_Instances ProjectListFile="$(Xt_ProjectListFile)" CurrentProject="@(Xt_CurrentProject)" SolutionFilePath="$(SolutionPath)"
152+
<Xt_Build_Instances ProjectListFile="$(Xt_ProjectListFile)" CurrentProject="@(Xt_CurrentProject)"
153+
SolutionFilePath="$(SolutionPath)" Configuration="$(Configuration)" Platform="$(Platform)"
143154
LinkToolCommand="$(Xt_Command_Link)" />
144155
<Message Text="build finished" Importance="High" />
145156
</Target>

export_template-config.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ set(XT_INST_SUFFIX ".xti" CACHE STRING
3434

3535
set(XT_INST_FILE_PATH "$(SolutionDir)intermediate/src/$(ProjectName)" CACHE STRING
3636
"path where the generated files will be placed (uses VS macros)")
37+
38+
option(XT_INST_FILE_FROM_HEADER "place the generated files next to the headers" OFF)
39+
if(XT_INST_FILE_FROM_HEADER)
40+
set(XT_INST_FILE_FROM_HEADER_VAL "true")
41+
else()
42+
set(XT_INST_FILE_FROM_HEADER_VAL "false")
43+
endif()
3744

3845
function(target_export_template targets)
3946
foreach(target ${ARGV})
@@ -43,6 +50,7 @@ function(target_export_template targets)
4350
VS_GLOBAL_Xt_TargetsPath ${XT_TARGETS_PATH}/
4451
VS_GLOBAL_Xt_InstSuffix ${XT_INST_SUFFIX}
4552
VS_GLOBAL_Xt_InstFilePath ${XT_INST_FILE_PATH}/
53+
VS_GLOBAL_Xt_InstFilePath_FromHeader ${XT_INST_FILE_FROM_HEADER_VAL}
4654
)
4755

4856
target_link_libraries(${target}

props/xt_user_template.props

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
<!-- settings for building the xt_inst_gen tool !-->
55
<PropertyGroup Label="UserMacros">
66
<!-- clang library path !-->
7-
<Xt_ClangLibPath >c:\Program Files\LLVM\lib</Xt_ClangLibPath>
8-
<!-- path to the cppast repository with a trailing slash (looks for \include, \external) !-->
9-
<Xt_cppast_Path>c:\...\cppast\</Xt_cppast_Path>
10-
<!-- cppast build path (with a trailing slash) !-->
11-
<Xt_cppast_BuildPath>c:\...\cppast\bin\</Xt_cppast_BuildPath>
7+
<Xt_ClangLibPath >c:\Program Files\LLVM\lib</Xt_ClangLibPath>
8+
<!-- path to the cppast repository with a trailing slash (looks for \include, \external) !-->
9+
<Xt_cppast_Path>c:\...\cppast\</Xt_cppast_Path>
10+
<!-- cppast build path (with a trailing slash) !-->
11+
<Xt_cppast_BuildPath>c:\...\cppast\bin\</Xt_cppast_BuildPath>
1212
</PropertyGroup>
1313
<!-- settings for projects that use the tool and the custom build rules !-->
1414
<PropertyGroup Label="UserMacros">
15-
<!-- clang binary path !-->
15+
<!-- clang binary path !-->
1616
<Xt_ClangPath >c:\Program Files\LLVM\bin</Xt_ClangPath>
17-
<!-- full path to the xt_inst_gen.exe tool !-->
17+
<!-- full path to the xt_inst_gen.exe tool !-->
1818
<Xt_InstGen_Path >c:\Program Files\export_template\bin\xt_inst_gen.exe</Xt_InstGen_Path>
19-
<!-- location of xt_inst_gen.target and the .cs files it depends on (with a trailing slash) !-->
19+
<!-- location of xt_inst_gen.target and the .cs files it depends on (with a trailing slash) !-->
2020
<Xt_TargetsPath >c:\Program Files\export_template\etc\</Xt_TargetsPath>
21-
<!-- extension used for the generated template instantiation files !-->
21+
<!-- extension used for the generated template instantiation files !-->
2222
<Xt_InstSuffix >.xti</Xt_InstSuffix>
23-
<!-- path where the generated files will be placed (with a trailing slash) !-->
23+
<!-- path where the generated files will be placed (with a trailing slash) !-->
2424
<Xt_InstFilePath >$(SolutionDir)intermediate\src\$(ProjectName)\</Xt_InstFilePath>
25+
<Xt_InstFilePath_FromHeader>false</Xt_InstFilePath_FromHeader>
2526
</PropertyGroup>
2627
<PropertyGroup />
2728
<ItemDefinitionGroup />

src/xt_inst_gen/xt_filt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ int do_filt(int argc, const char *argv[])
5757
auto cache_last_modified = fs::last_write_time(header_cache_file_path, err);
5858
auto cached_headers = get_cached_headers(header_cache_file_path);
5959

60+
// todo: if the path to the header cache does not exist, create directories ?
6061
std::ofstream f { header_cache_file_path };
6162
if (!f) {
6263
fmt::print("failed to open header cache file '{}'\n", header_cache_file_path);

test/run_one.bat

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@ECHO OFF
2+
set test=%1
3+
set verbosity=%2
4+
5+
set current_path=%cd%
6+
set mypath=%~dp0
7+
8+
if "%test%" == "" goto end
9+
if not exist "%mypath%\%test%" goto end
10+
11+
if "%verbosity%" == "" set verbosity=m
12+
set verbosity_long=minimal
13+
if "%verbosity%" == "d" set verbosity_long=diagnostic
14+
15+
cd %mypath%\%test%
16+
if not exist build mkdir build
17+
cd build
18+
19+
cmake -G "Visual Studio 15 2017 Win64" ../
20+
set solution_dir=%mypath%\%test%\build\
21+
cmake --build . --config Debug -- -v:%verbosity% "/p:Xt_BuildLogPath=build.log;SolutionDir=%solution_dir%;SolutionPath=%solution_dir%test_%test%.sln" -flp:LogFile=build.log;Verbosity=%verbosity_long%
22+
cd %current_path%
23+
24+
:end

0 commit comments

Comments
 (0)