Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f098d1f

Browse files
committed
Added some tests, fixed some bugs.
1 parent 46d3ad9 commit f098d1f

File tree

10 files changed

+71
-26
lines changed

10 files changed

+71
-26
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ jobs:
251251
"/c/Program Files/CMake/bin/cpack" -G NSIS -C Debug
252252
253253
#cmd.exe /C "cpp_modules-0.0.1-win32.exe /S"
254-
cmd.exe /C "cpp_modules-0.0.1-win32.exe /S /D=D:\tmp\cpp_modules"
255-
mv /d/tmp/cpp_modules "/c/Program Files"
254+
cmd.exe /C "cpp_modules-0.0.1-win32.exe /S /D=C:\Program Files\cpp_modules"
256255
cat CPackConfig.cmake
257256
else
258257
#cmake_path=`which cmake` # sudo changes the path and we have cmake 3.12 installed by default

build_rules/cpp_modules-config.cmake

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,45 @@ function(target_cpp_modules targets)
7777
set_property(TARGET ${target} PROPERTY VS_GLOBAL_CppM_ClangScanDepsPath "${CPPM_SCANNER_PATH}")
7878

7979
target_link_libraries(${target}
80-
${CPPM_TARGETS_PATH}/cpp_modules.targets
80+
"${CPPM_TARGETS_PATH}/cpp_modules.targets"
8181
)
8282
endforeach()
8383

84-
add_library(_CPPM_ALL_BUILD EXCLUDE_FROM_ALL ${CPPM_TARGETS_PATH}/dummy.cpp)
84+
add_library(_CPPM_ALL_BUILD EXCLUDE_FROM_ALL "${CPPM_TARGETS_PATH}/dummy.cpp")
8585
set_property(TARGET _CPPM_ALL_BUILD PROPERTY EXCLUDE_FROM_DEFAULT_BUILD TRUE)
8686
add_dependencies(_CPPM_ALL_BUILD ${ARGV})
87-
target_link_libraries(_CPPM_ALL_BUILD ${CPPM_TARGETS_PATH}/cpp_modules.targets)
87+
target_link_libraries(_CPPM_ALL_BUILD "${CPPM_TARGETS_PATH}/cpp_modules.targets")
8888
endif()
8989
endfunction()
9090

91+
function(cppm_list_transform_to_absolute_path files_var)
92+
set(files_in "${${files_var}}")
93+
set(files_out "")
94+
foreach(maybe_rel_path ${files_in})
95+
get_filename_component(abs_path "${maybe_rel_path}"
96+
ABSOLUTE BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
97+
list(APPEND files_out "${abs_path}")
98+
endforeach()
99+
set(${files_var} "${files_out}" PARENT_SCOPE)
100+
endfunction()
101+
91102
function(target_cpp_header_units target)
92-
set(headers ${ARGN})
93-
target_sources(${target} PRIVATE ${headers})
103+
set(headers "${ARGN}")
104+
target_sources(${target} PRIVATE "${headers}")
94105

95106
if(CMAKE_GENERATOR MATCHES "Ninja")
96107
foreach(header ${headers})
97108
# the MSBuild customization already adds these to the sources
98-
set_source_files_properties(${header} PROPERTIES LANGUAGE CXX)
109+
set_source_files_properties("${header}" PROPERTIES LANGUAGE CXX)
99110
if(NOT MSVC)
100-
set_source_files_properties(${header} COMPILE_FLAGS "-xc++")
111+
set_source_files_properties("${header}" COMPILE_FLAGS "-xc++")
101112
endif()
102113
endforeach()
103114
endif()
104115

105-
list(TRANSFORM headers PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
116+
cppm_list_transform_to_absolute_path(headers)
106117
if(CMAKE_GENERATOR MATCHES "Visual Studio")
107-
set_property(TARGET ${target} PROPERTY VS_GLOBAL_CppM_Legacy_Headers ${headers})
118+
set_property(TARGET ${target} PROPERTY VS_GLOBAL_CppM_Header_Units "${headers}")
108119
endif()
109120
if(CMAKE_GENERATOR MATCHES "Ninja")
110121
file(APPEND "${CMAKE_BINARY_DIR}/scanner_config.txt" "header_units ${target} ${headers}\n")

build_rules/cpp_modules.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,15 @@ public override void on_result(uint item_idx, DepInfo dep_info, bool out_of_date
549549
//cppm.Log.LogMessage(MessageImportance.High, "results for {0}:", dep_info.input);
550550
var item = itemset.Get(dep_info.input);
551551
item.is_ood = out_of_date;
552+
bool is_header_unit = (item.GetMetadata("CppM_Header_Unit") == "true");
552553
var mdef = new ModuleDefinition {
553554
base_command = cppm.GetBaseCommand(item, preprocess: false),
554555
obj_file = NormalizeFilePath(item.GetMetadata("ObjectFileName") +
555-
Path.GetFileNameWithoutExtension(item.path) + ".obj"),
556-
importable_header = (item.GetMetadata("CppM_Header_Unit") == "true"),
556+
// for header units the full path is already provided by the targets file
557+
(is_header_unit ? "" : Path.GetFileNameWithoutExtension(item.path) + ".obj")),
558+
importable_header = is_header_unit,
557559
};
558-
if(mdef.importable_header)
560+
if(is_header_unit)
559561
mdef.exported_module = GetImportableHeaderModuleName(item.path);
560562

561563
foreach(var dep in ens(dep_info.depends)) {

build_rules/cpp_modules.targets

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040

4141
<ItemGroup>
4242
<!-- for header units listed via CMake !-->
43-
<ClInclude Remove="$(CppM_Legacy_Headers)" />
44-
<ClCompile Include="$(CppM_Legacy_Headers)">
43+
<ClInclude Remove="$(CppM_Header_Units)" />
44+
<ClCompile Include="$(CppM_Header_Units)">
45+
<!-- B.h must not have the same ObjectFileName as B.cpp !-->
46+
<ObjectFileName>$(IntDir)%(Filename)%(Extension).obj</ObjectFileName>
4547
<CppM_Header_Unit>true</CppM_Header_Unit>
4648
</ClCompile>
4749
<!-- for header units listed via the project properties, but the following is an error !-->
48-
<!--<CppM_Legacy_Headers Include="@(CLCompile)" Condition="'%(ClCompile.CppM_Header_Unit)' == 'true'" />!-->
50+
<!--<CppM_Header_Units Include="@(CLCompile)" Condition="'%(ClCompile.CppM_Header_Unit)' == 'true'" />!-->
4951
</ItemGroup>
5052

5153
<UsingTask TaskName="CppM_CL" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
@@ -100,8 +102,7 @@
100102
<ItemGroup>
101103
<CppM_CL_Input_All Include="@(ClCompile)" Condition="'%(ClCompile.PrecompiledHeader)' != 'Create' and '%(ClCompile.ExcludedFromBuild)'!='true' and '%(ClCompile.CompilerIteration)' == '' and @(ClCompile) != ''">
102104
<CppM_ModuleDefinitionFile>$(CppM_IntDir_FullPath)%(CLCompile.FileName)$(CppM_Mdef_Suffix)</CppM_ModuleDefinitionFile>
103-
<CppM_CMI_File>$(CppM_IntDir_FullPath)%(Filename).ifc</CppM_CMI_File>
104-
<CppM_ObjectFilePath>$(CppM_IntDir_FullPath)%(Filename).obj</CppM_ObjectFilePath>
105+
<CppM_CMI_File>$(CppM_IntDir_FullPath)%(Filename)%(Extension).ifc</CppM_CMI_File>
105106
<BuildingInIDE>$(BuildingInsideVisualStudio)</BuildingInIDE>
106107
<!--<Sources>@(ClCompile)</Sources>!-->
107108
<Sources>%(ClCompile.FullPath)</Sources>
@@ -158,7 +159,6 @@
158159
<CppM_To_Delete_In_IntDir Include="pp.modulemap;pp_commands.json;module_map.json;build.ninja;build_rec.ninja" />
159160
<CppM_To_Delete Include="@(CppM_To_Delete_In_IntDir->'$(CppM_IntDir_FullPath)%(FileName)%(Extension)')" />
160161
<CppM_To_Delete Include="$(TLogLocation)pp\*;$(TLogLocation)src\*" />
161-
<CppM_To_Delete Include="@(CppM_CL_Input_All->'%(ObjectFileName)%(FileName).i')" />
162162
<CppM_To_Delete Include="$(CppM_CMI_Path)*$(CppM_CMI_Ext)" />
163163
<CppM_To_Delete Include="@(CppM_CL_Input_All->'%(CppM_ModuleDefinitionFile)')" />
164164
</ItemGroup>

src/test/msbuild.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace fs = std::filesystem;
1212
namespace msbuild {
1313

1414
ConfigString cfg_run_one { "msbuild-run-one", "" };
15-
ConfigString cfg_run_set { "msbuild-run-set", "dag,concurrent" };
15+
ConfigString cfg_run_set { "msbuild-run-set", "dag,concurrent,generated" };
1616

1717
ConfigString cfg_generator { "msbuild-generator", "Visual Studio 16 2019" };
1818
ConfigString cfg_arch { "msbuild-arch", "X64" };

src/test/scanner_test.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ inline bool ends_with(std::string_view str, std::string_view with) {
174174
return str.substr(str.size() - with.size(), with.size()) == with;
175175
}
176176

177+
constexpr std::string_view default_cmd_suffix = "ASDFG";
178+
177179
struct TempFileScanTest : public TempFileTest {
178180
private:
179181
cppm::ScanItemSet item_set;
@@ -224,7 +226,8 @@ struct TempFileScanTest : public TempFileTest {
224226
.target_idx = target_idx,
225227
.is_header_unit = ends_with(file, ".h")
226228
});
227-
std::string cmd = fmt::format("{} /Fo\"{}.{}.obj\"", default_command, file, item_set.targets[target_idx]);
229+
std::string cmd = fmt::format("{} /Fo\"{}.{}.obj\" /D {}",
230+
default_command, file, item_set.targets[target_idx], default_cmd_suffix);
228231
item_set.commands.emplace_back(std::move(cmd));
229232
}
230233

@@ -240,6 +243,16 @@ struct TempFileScanTest : public TempFileTest {
240243
add_item(file, target_idx);
241244
}
242245

246+
void set_command_suffix(cppm::scan_item_idx_t idx, std::string_view suffix) {
247+
// by construction, cmd idx == item idx
248+
std::string& cmd = item_set.commands[id_cast<cppm::cmd_idx_t>(idx)];
249+
auto d_pos = cmd.rfind("/D ");
250+
assert(d_pos != std::string::npos);
251+
auto prev_suffix_pos = d_pos + 3;
252+
auto prev_suffix_length = cmd.size() - prev_suffix_pos;
253+
cmd.replace(prev_suffix_pos, prev_suffix_length, suffix);
254+
}
255+
243256
void scan() {
244257
auto item_set_owned_view = cppm::ScanItemSetOwnedView::from(item_set);
245258
auto item_set_view = cppm::ScanItemSetView::from(item_set_owned_view);
@@ -495,7 +508,13 @@ TEST_CASE("test1", "[scanner]") {
495508

496509
test.scan_check({}); // no changes again
497510

498-
// todo: test command changed
511+
test.set_command_suffix(a, "TTRTETRRE");
512+
test.scan_check({ a }); // the command changed for a
513+
test.set_command_suffix(b, "JFDJD");
514+
test.set_command_suffix(a, default_cmd_suffix);
515+
test.scan_check({ a, b });
516+
test.set_command_suffix(b, default_cmd_suffix);
517+
test.scan_check({ b });
499518
}
500519
}
501520

@@ -717,7 +736,7 @@ export module c;
717736
test.scan_check({ a }, { a });
718737
}
719738

720-
#if 0
739+
#if 0 // todo:
721740
SECTION("missing modules") {
722741

723742
}

src/test/system_test.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ void run_one(const std::string& test, const run_one_params& p) {
125125
fs::create_directory(build_path);
126126
fs::current_path(build_path);
127127

128-
fmt::print(fmt::fg(fmt::color::yellow), "=== running {} ===\n", test);
128+
fmt::print(fmt::fg(fmt::color::yellow), "=== running '{}' with {} / {} ===\n",
129+
test, (p.generator == "Ninja" ? "ninja" : "msbuild"), compiler_name(p.compiler));
129130

130131
if (p.generator == "Ninja") {
131132
run_one_ninja(test, p, build_path);
@@ -158,4 +159,12 @@ Compiler get_compiler_from_str(std::string_view compiler) {
158159
throw std::invalid_argument(fmt::format("unsupported compiler: {}", compiler));
159160
}
160161

162+
std::string_view compiler_name(Compiler compiler) {
163+
if (compiler == Compiler::msvc) return "msvc";
164+
else if (compiler == Compiler::clang_cl) return"clang_cl";
165+
else if (compiler == Compiler::clang) return"clang";
166+
else if (compiler == Compiler::gcc) return"gcc";
167+
throw std::invalid_argument(fmt::format("unsupported compiler: {}", compiler));
168+
}
169+
161170
} // namespace system_test

src/test/system_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ enum Compiler {
2222
#endif
2323

2424
std::string_view get_compiler_path(Compiler compiler);
25+
// todo: use reflection in C++23 to convert to/from enum names
2526
Compiler get_compiler_from_str(std::string_view compiler);
27+
std::string_view compiler_name(Compiler compiler);
2628

2729
struct run_one_params {
2830
std::string_view test_path;

tests/dag/C3.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import std.core;
44

55
import C4;
66

7+
#ifdef __clang__
8+
import <C5.h>;
9+
#else
710
#include <C5.h>
11+
#endif
812

913
#include "funcsig.h"
1014

tests/generated/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ add_custom_command(
1414
add_executable(A
1515
A.cpp
1616
"${CMAKE_CURRENT_BINARY_DIR}/B.cpp"
17-
"${CMAKE_CURRENT_BINARY_DIR}/B.h"
1817
"${CMAKE_CURRENT_BINARY_DIR}/C.m.cpp"
1918
)
2019

0 commit comments

Comments
 (0)