Skip to content

Commit c7297e6

Browse files
committed
Allow to use one pch for many targets.
1 parent a90d50b commit c7297e6

16 files changed

Lines changed: 176 additions & 60 deletions

File tree

cppan.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ projects:
418418
file(GLOB_RECURSE x "${SDIR}/*")
419419
source_group(TREE ${SDIR} PREFIX "Source Files" FILES ${x})
420420
421+
x:
422+
421423
test.unit.path:
422424
copy_to_output_dir: false
423425
api_name: SW_MANAGER_API

src/builder/command.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,23 @@ void Command::execute1(std::error_code *ec)
697697
return s;
698698
};
699699

700-
auto make_error_string = [this, &save_command](const String &e)
700+
auto print_outputs = [this]()
701+
{
702+
/*boost::trim(out.text);
703+
boost::trim(err.text);
704+
String s;
705+
if (!out.text.empty())
706+
s += out.text + "\n";
707+
if (!err.text.empty())
708+
s += err.text + "\n";
709+
if (!s.empty())
710+
LOG_INFO(logger, s);*/
711+
};
712+
713+
auto make_error_string = [this, &save_command, &print_outputs](const String &e)
701714
{
702715
postProcess(false);
716+
print_outputs();
703717

704718
String s = "When building: " + getName();
705719
if (!out.text.empty())
@@ -746,10 +760,12 @@ void Command::execute1(std::error_code *ec)
746760
save_command();
747761

748762
postProcess(); // process deps
763+
print_outputs();
749764
}
750765
catch (std::exception &e)
751766
{
752-
throw SW_RUNTIME_ERROR(make_error_string(e.what()));
767+
auto err = make_error_string(e.what());
768+
throw SW_RUNTIME_ERROR(err);
753769
}
754770
}
755771

src/builder/file.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,18 @@ void FileRecord::setGenerator(const std::shared_ptr<builder::Command> &g, bool i
404404
String err;
405405
err += "Setting generator twice on file: " + file.u8string() + "\n";
406406
if (gold)
407-
err += "first generator:\n " + gold->print();
407+
{
408+
err += "first generator:\n " + gold->print() + "\n";
409+
err += "first generator hash:\n " + std::to_string(gold->getHash());
410+
}
408411
else
409412
err += "first generator is empty";
410413
err += "\n";
411414
if (g)
412-
err += "second generator:\n " + g->print();
415+
{
416+
err += "second generator:\n " + g->print() + "\n";
417+
err += "second generator hash:\n " + std::to_string(g->getHash());
418+
}
413419
else
414420
err += "second generator is empty";
415421
throw SW_RUNTIME_ERROR(err);

src/driver/cpp/compiler.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,9 @@ void detectNonWindowsCompilers(struct Solution &s)
812812
NativeLinkerOptions LOpts;
813813
//LOpts.System.LinkDirectories.insert("/lib");
814814
//LOpts.System.LinkDirectories.insert("/lib/x86_64-linux-gnu");
815-
//LOpts.System.LinkLibraries.push_back("stdc++");
815+
#ifndef _WIN32
816+
LOpts.System.LinkLibraries.push_back("stdc++");
817+
#endif
816818
//LOpts.System.LinkLibraries.push_back("stdc++fs");
817819
LOpts.System.LinkLibraries.push_back("pthread"); // remove and add to progs explicitly?
818820
LOpts.System.LinkLibraries.push_back("dl"); // remove and add to progs explicitly?
@@ -852,8 +854,8 @@ void detectNonWindowsCompilers(struct Solution &s)
852854
}
853855

854856
//p = resolve("ld.gold");
855-
//for (auto &v : gcc_vers)
856-
for (auto &v : gccpp_vers) // this links correct c++ library
857+
for (auto &v : gcc_vers)
858+
//for (auto &v : gccpp_vers) // this links correct c++ library
857859
{
858860
p = resolve(v);
859861
if (!p.empty())
@@ -940,8 +942,8 @@ void detectNonWindowsCompilers(struct Solution &s)
940942
// clang
941943
{
942944
//p = resolve("ld.gold");
943-
//for (auto &v : clang_vers)
944-
for (auto &v : clangpp_vers) // this links correct c++ library
945+
for (auto &v : clang_vers)
946+
//for (auto &v : clangpp_vers) // this links correct c++ library
945947
{
946948
p = resolve(v);
947949
if (!p.empty())

src/driver/cpp/solution.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,7 @@ FilesMap Build::build_configs_separate(const Files &files)
15361536
L->Force = vs::ForceType::Multiple;
15371537
L->IgnoreWarnings().insert(4006); // warning LNK4006: X already defined in Y; second definition ignored
15381538
L->IgnoreWarnings().insert(4070); // warning LNK4070: /OUT:X.dll directive in .EXP differs from output filename 'Y.dll'; ignoring directive
1539+
L->IgnoreWarnings().insert(4088); // warning LNK4088: image being generated due to /FORCE option; image may not run
15391540
}
15401541

15411542
for (auto &d : udeps)

src/driver/cpp/source_file.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,10 @@ std::shared_ptr<builder::Command> NativeSourceFile::getCommand(const TargetBase
574574
{
575575
auto cmd = compiler->getCommand(t);
576576
for (auto &d : dependencies)
577-
cmd->dependencies.insert(d->getCommand(t));
577+
{
578+
if (d)
579+
cmd->dependencies.insert(d->getCommand(t));
580+
}
578581
return cmd;
579582
}
580583

src/driver/cpp/source_file.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ struct PrecompiledHeader
181181
path source;
182182
// path pch; // file itself
183183
bool force_include_pch = false;
184+
185+
// internal
186+
bool created = false;
184187
};
185188

186189
struct SW_DRIVER_CPP_API RcToolSourceFile : SourceFile

src/driver/cpp/target.cpp

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,15 +1048,15 @@ void NativeExecutedTarget::addPrecompiledHeader(const path &h, const path &cpp)
10481048
addPrecompiledHeader(pch);
10491049
}
10501050

1051-
void NativeExecutedTarget::addPrecompiledHeader(PrecompiledHeader p)
1051+
void NativeExecutedTarget::addPrecompiledHeader(PrecompiledHeader &p)
10521052
{
10531053
check_absolute(p.header);
10541054
if (!p.source.empty())
10551055
check_absolute(p.source);
10561056

10571057
bool force_include_pch_header_to_pch_source = true;
10581058
bool force_include_pch_header_to_target_source_files = p.force_include_pch;
1059-
auto pch = p.source;
1059+
auto &pch = p.source;
10601060
path pch_dir = BinaryDir.parent_path() / "pch";
10611061
if (!pch.empty())
10621062
{
@@ -1097,20 +1097,24 @@ void NativeExecutedTarget::addPrecompiledHeader(PrecompiledHeader p)
10971097
// before adding pch source file to target
10981098
// on this step we setup compilers to USE our created pch
10991099
// MSVC does it explicitly, gnu does implicitly; check what about clang
1100+
CompilerType cc = CompilerType::UnspecifiedCompiler;
11001101
for (auto &f : gatherSourceFiles())
11011102
{
11021103
if (auto sf = f->as<NativeSourceFile>())
11031104
{
11041105
if (auto c = sf->compiler->as<VisualStudioCompiler>())
11051106
{
1107+
cc = c->Type;
11061108
setup_use_vc(c);
11071109
}
11081110
else if (auto c = sf->compiler->as<ClangClCompiler>())
11091111
{
1112+
cc = c->Type;
11101113
setup_use_vc(c);
11111114
}
11121115
else if (auto c = sf->compiler->as<ClangCompiler>())
11131116
{
1117+
cc = c->Type;
11141118
break_gch_deps[pch] = gch_fn_clang;
11151119

11161120
// !
@@ -1129,6 +1133,7 @@ void NativeExecutedTarget::addPrecompiledHeader(PrecompiledHeader p)
11291133
}
11301134
else if (auto c = sf->compiler->as<GNUCompiler>())
11311135
{
1136+
cc = c->Type;
11321137
break_gch_deps[pch] = gch_fn;
11331138

11341139
// !
@@ -1146,54 +1151,73 @@ void NativeExecutedTarget::addPrecompiledHeader(PrecompiledHeader p)
11461151
}
11471152
}
11481153

1149-
*this += pch;
1150-
11511154
// on this step we setup compilers to CREATE our pch
1152-
if (auto sf = ((*this)[pch]).as<NativeSourceFile>())
1155+
if (!p.created)
11531156
{
1154-
auto setup_create_vc = [&sf, &force_include_pch_header_to_pch_source, &p, &pch_fn, &pdb_fn, &obj_fn](auto &c)
1157+
*this += pch;
1158+
if (auto sf = ((*this)[pch]).as<NativeSourceFile>(); sf)
11551159
{
1156-
sf->setOutputFile(obj_fn);
1157-
1158-
if (force_include_pch_header_to_pch_source)
1159-
c->ForcedIncludeFiles().push_back(p.header);
1160-
c->PrecompiledHeaderFilename() = pch_fn;
1161-
c->PrecompiledHeaderFilename.output_dependency = true;
1162-
c->PrecompiledHeader().create = p.header;
1163-
c->PDBFilename = pdb_fn;
1164-
c->PDBFilename.intermediate_file = false;
1165-
//c->PDBFilename.output_dependency = true;
1166-
};
1160+
auto setup_create_vc = [&sf, &force_include_pch_header_to_pch_source, &p, &pch_fn, &pdb_fn, &obj_fn](auto &c)
1161+
{
1162+
sf->setOutputFile(obj_fn);
11671163

1168-
if (auto c = sf->compiler->as<VisualStudioCompiler>())
1169-
{
1170-
setup_create_vc(c);
1171-
}
1172-
else if (auto c = sf->compiler->as<ClangClCompiler>())
1173-
{
1174-
setup_create_vc(c);
1175-
}
1176-
else if (auto c = sf->compiler->as<ClangCompiler>())
1177-
{
1178-
sf->setOutputFile(gch_fn_clang);
1179-
c->Language = "c++-header";
1180-
if (force_include_pch_header_to_pch_source)
1181-
c->ForcedIncludeFiles().push_back(p.header);
1182-
c->EmitPCH = true;
1164+
if (force_include_pch_header_to_pch_source)
1165+
c->ForcedIncludeFiles().push_back(p.header);
1166+
c->PrecompiledHeaderFilename() = pch_fn;
1167+
c->PrecompiledHeaderFilename.output_dependency = true;
1168+
c->PrecompiledHeader().create = p.header;
1169+
c->PDBFilename = pdb_fn;
1170+
c->PDBFilename.intermediate_file = false;
1171+
//c->PDBFilename.output_dependency = true;
1172+
};
1173+
1174+
if (auto c = sf->compiler->as<VisualStudioCompiler>())
1175+
{
1176+
setup_create_vc(c);
1177+
}
1178+
else if (auto c = sf->compiler->as<ClangClCompiler>())
1179+
{
1180+
setup_create_vc(c);
1181+
}
1182+
else if (auto c = sf->compiler->as<ClangCompiler>())
1183+
{
1184+
sf->setOutputFile(gch_fn_clang);
1185+
c->Language = "c++-header";
1186+
if (force_include_pch_header_to_pch_source)
1187+
c->ForcedIncludeFiles().push_back(p.header);
1188+
c->EmitPCH = true;
1189+
}
1190+
else if (auto c = sf->compiler->as<GNUCompiler>())
1191+
{
1192+
sf->setOutputFile(gch_fn);
1193+
c->Language = "c++-header";
1194+
if (force_include_pch_header_to_pch_source)
1195+
c->ForcedIncludeFiles().push_back(p.header);
1196+
1197+
IncludeDirectories.insert(pch_dir);
1198+
}
1199+
p.created = true;
11831200
}
1184-
else if (auto c = sf->compiler->as<GNUCompiler>())
1201+
}
1202+
else
1203+
{
1204+
switch (cc)
11851205
{
1186-
sf->setOutputFile(gch_fn);
1187-
c->Language = "c++-header";
1188-
if (force_include_pch_header_to_pch_source)
1189-
c->ForcedIncludeFiles().push_back(p.header);
1190-
1191-
IncludeDirectories.insert(pch_dir);
1206+
case CompilerType::MSVC:
1207+
case CompilerType::ClangCl:
1208+
*this += obj_fn;
1209+
break;
1210+
case CompilerType::Clang:
1211+
break;
1212+
case CompilerType::GNU:
1213+
break;
1214+
default:
1215+
throw SW_RUNTIME_ERROR("unknown compiler for pch");
11921216
}
11931217
}
11941218
}
11951219

1196-
NativeExecutedTarget &NativeExecutedTarget::operator=(PrecompiledHeader pch)
1220+
NativeExecutedTarget &NativeExecutedTarget::operator=(PrecompiledHeader &pch)
11971221
{
11981222
addPrecompiledHeader(pch);
11991223
return *this;

src/driver/cpp/target.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ struct SW_DRIVER_CPP_API NativeExecutedTarget : NativeTarget,
712712
bool ExportIfStatic = false;
713713
path InstallDirectory;
714714
bool PackageDefinitions = false;
715-
//bool StartupProject = false; // move to description? move to Generator.VS... struct?
715+
bool StartupProject = false; // move to description? move to Generator.VS... struct? IDE struct?
716716

717717
bool ImportFromBazel = false;
718718
StringSet BazelNames;
@@ -777,8 +777,8 @@ struct SW_DRIVER_CPP_API NativeExecutedTarget : NativeTarget,
777777
void configureFile(path from, path to, ConfigureFlags flags = ConfigureFlags::Default);
778778

779779
void addPrecompiledHeader(const path &h, const path &cpp = path());
780-
void addPrecompiledHeader(PrecompiledHeader pch);
781-
NativeExecutedTarget &operator=(PrecompiledHeader pch);
780+
void addPrecompiledHeader(PrecompiledHeader &pch);
781+
NativeExecutedTarget &operator=(PrecompiledHeader &pch);
782782

783783
virtual bool isStaticOnly() const { return false; }
784784
virtual bool isSharedOnly() const { return false; }

src/manager/pch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
#pragma once
2+
3+
#ifdef _WIN32
4+
#define _WIN32_WINNT 0x0602
5+
#endif
26

37
#include "cppan_version.h"
48
#include "lock.h"

0 commit comments

Comments
 (0)