@@ -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 ;
0 commit comments