Skip to content

Commit eaf4f2f

Browse files
committed
Improve VS generator.
1 parent d658b13 commit eaf4f2f

7 files changed

Lines changed: 124 additions & 60 deletions

File tree

src/client/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ SUBCOMMAND_DECL(ide)
769769

770770
extern ::cl::opt<String> cl_generator;
771771
extern bool gPrintDependencies;
772-
static ::cl::opt<bool, true> print_dependencies("print-dependencies", ::cl::location(gRunAppInContainer), ::cl::sub(subcommand_generate));
772+
static ::cl::opt<bool, true> print_dependencies("print-dependencies", ::cl::location(gPrintDependencies), ::cl::sub(subcommand_generate));
773773

774774
SUBCOMMAND_DECL(generate)
775775
{

src/driver/cpp/generator/generator.cpp

Lines changed: 84 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ static String get_project_configuration(const SolutionSettings &s)
267267

268268
static String get_vs_file_type_by_ext(const path &p)
269269
{
270-
String type = "ClCoNonempile";
270+
String type = "None";
271271
if (p.extension() == ".rc")
272272
type = "ResourceCompile";
273273
else if (p.extension() == ".rule")
274274
type = "CustomBuild";
275275
else if (isCppHeaderFileExtension(p.extension().string()))
276276
type = "ClInclude";
277-
else if (isCppSourceFileExtensions(p.extension().string()))
277+
else if (isCppSourceFileExtensions(p.extension().string()) || p.extension() == ".c")
278278
type = "ClCompile";
279279
return type;
280280
}
@@ -504,6 +504,19 @@ void ProjectContext::printProject(
504504
addBlock("Import", "", { { "Project", "$(VCTargetsPath)\\Microsoft.Cpp.props" } });
505505
addPropertySheets(b);
506506

507+
auto get_int_dir = [&dir, &projects_dir](auto &nt, auto &s)
508+
{
509+
auto tdir = dir / projects_dir;
510+
if (s.TargetOS.is(ArchType::x86_64))
511+
tdir /= "x64";
512+
return tdir / sha256_short(nt.pkg.toString()) / sha256_short(get_project_configuration(s));
513+
};
514+
515+
StringSet filters; // dirs
516+
FiltersContext fctx;
517+
fctx.beginProject();
518+
fctx.beginBlock("ItemGroup");
519+
507520
for (auto &s : b.solutions)
508521
{
509522
beginBlock("PropertyGroup", { { "Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
@@ -582,12 +595,8 @@ void ProjectContext::printProject(
582595

583596
beginBlock("PropertyGroup", { { "Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
584597
{
585-
auto tdir = dir / projects_dir;
586-
if (s.Settings.TargetOS.is(ArchType::x86_64))
587-
tdir /= "x64";
588-
589598
//addBlock("OutDir", normalize_path_windows(current_thread_path() / "bin\\"));
590-
addBlock("IntDir", normalize_path_windows(tdir / sha256_short(nt.pkg.toString())) + "\\");
599+
addBlock("IntDir", normalize_path_windows(get_int_dir(nt, s.Settings)) + "\\");
591600
addBlock("TargetName", nt.pkg.toString());
592601
//addBlock("TargetExt", ext);
593602
}
@@ -623,20 +632,39 @@ void ProjectContext::printProject(
623632
{
624633
beginBlock("Link");
625634
beginBlock("AdditionalDependencies", { { "Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
626-
for (auto &d : nt.Dependencies)
635+
std::set<void*> visited;
636+
std::function<void(NativeExecutedTarget&)> f;
637+
f = [&f, this, &dir, &s, &visited](auto &nt)
627638
{
628-
if (d->isDummy())
629-
continue;
639+
if (visited.find(&nt) != visited.end())
640+
return;
641+
visited.insert(&nt);
630642

631-
deps.insert(d->target->pkg.toString());
643+
for (auto &d : nt.Dependencies)
644+
{
645+
if (d->isDummy())
646+
continue;
632647

633-
auto tdir = dir;
634-
if (s.Settings.TargetOS.is(ArchType::x86_64))
635-
tdir /= "x64";
636-
tdir /= get_configuration(s.Settings);
637-
tdir /= d->target->pkg.toString() + ".lib";
638-
addText(normalize_path_windows(tdir) + ";");
639-
}
648+
deps.insert(d->target->pkg.toString());
649+
650+
auto tdir = dir;
651+
if (s.Settings.TargetOS.is(ArchType::x86_64))
652+
tdir /= "x64";
653+
tdir /= get_configuration(s.Settings);
654+
tdir /= d->target->pkg.toString() + ".lib";
655+
addText(normalize_path_windows(tdir) + ";");
656+
657+
if (s.Settings.Native.LibrariesType == LibraryType::Static)
658+
if (d->target->getType() == TargetType::NativeLibrary || d->target->getType() == TargetType::NativeStaticLibrary)
659+
{
660+
if (auto nt3 = d->target->as<NativeExecutedTarget>())
661+
{
662+
f(*nt3);
663+
}
664+
}
665+
}
666+
};
667+
f(nt);
640668
addText("%(AdditionalDependencies)");
641669
endBlock(true);
642670
endBlock();
@@ -645,11 +673,6 @@ void ProjectContext::printProject(
645673
endBlock();
646674
}
647675

648-
StringSet filters; // dirs
649-
FiltersContext fctx;
650-
fctx.beginProject();
651-
fctx.beginBlock("ItemGroup");
652-
653676
bool add_sources =
654677
ptype == VSProjectType::Utility ||
655678
g.type == GeneratorType::VisualStudio ||
@@ -666,22 +689,22 @@ void ProjectContext::printProject(
666689
File ff(p, *base_nt.getSolution()->fs);
667690
if (g.type == GeneratorType::VisualStudio && ff.isGenerated())
668691
{
669-
auto gen = ff.getFileRecord().getGenerator();
670-
if (rules.find(gen.get()) == rules.end())
692+
for (auto &s : b.solutions)
671693
{
672-
rules.insert(gen.get());
694+
File ff(p, *s.fs);
695+
auto gen = ff.getFileRecord().getGenerator();
696+
if (rules.find(gen.get()) == rules.end())
697+
{
698+
rules.insert(gen.get());
673699

674-
auto rule = dir / projects_dir / name / (p.filename().string() + ".rule");
675-
if (!fs::exists(rule))
676-
write_file(rule, "");
700+
auto rule = get_int_dir(base_nt, s.Settings) / "rules" / (p.filename().string() + ".rule");
701+
if (!fs::exists(rule))
702+
write_file(rule, "");
677703

678-
beginBlock(get_vs_file_type_by_ext(rule), { { "Include", rule.string() } });
679-
for (auto &s : b.solutions)
680-
{
704+
beginBlock(get_vs_file_type_by_ext(rule), {{"Include", rule.string()}});
681705
String cmd;
682706

683-
beginBlock("AdditionalInputs", { {
684-
"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
707+
beginBlock("AdditionalInputs", {{"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'"}});
685708
//addText(normalize_path_windows(gen->program) + ";");
686709
if (auto dc = gen->as<driver::cpp::Command>())
687710
{
@@ -707,8 +730,7 @@ void ProjectContext::printProject(
707730
endBlock(true);
708731
for (auto &o : gen->outputs)
709732
{
710-
beginBlock("Outputs", { {
711-
"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
733+
beginBlock("Outputs", {{"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'"}});
712734
addText(normalize_path_windows(o) + ";");
713735
endBlock(true);
714736
}
@@ -723,28 +745,34 @@ void ProjectContext::printProject(
723745
if (!gen->err.file.empty())
724746
cmd += " 2> \"" + normalize_path_windows(gen->err.file) + "\"";
725747

726-
beginBlock("Command", { {
727-
"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'" } });
748+
beginBlock("Command", {{"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'"}});
728749
addText(cmd);
729750
endBlock(true);
730-
}
731751

732-
beginBlock("Message");
733-
addText(gen->getName());
734-
endBlock(true);
752+
beginBlock("Message");
753+
addText(gen->getName());
754+
endBlock(true);
735755

736-
endBlock();
756+
endBlock();
757+
758+
auto filter = ". SW Rules";
759+
filters.insert(filter);
737760

738-
auto filter = ". SW Rules";
739-
filters.insert(filter);
761+
fctx.beginBlock(get_vs_file_type_by_ext(rule), {{"Include", rule.string()}});
762+
fctx.addBlock("Filter", make_backslashes(filter));
763+
fctx.endBlock();
764+
}
740765

741-
fctx.beginBlock(get_vs_file_type_by_ext(rule), { {"Include", rule.string()} });
742-
fctx.addBlock("Filter", make_backslashes(filter));
743-
fctx.endBlock();
766+
beginBlock(get_vs_file_type_by_ext(p), { { "Include", p.string() },
767+
{"Condition", "'$(Configuration)|$(Platform)'=='" + get_project_configuration(s.Settings) + "'"} });
768+
endBlock();
744769
}
745770
}
746-
beginBlock(get_vs_file_type_by_ext(p), { { "Include", p.string() } });
747-
endBlock();
771+
else
772+
{
773+
beginBlock(get_vs_file_type_by_ext(p), { { "Include", p.string() } });
774+
endBlock();
775+
}
748776
}
749777
endBlock();
750778
}
@@ -869,6 +897,10 @@ SolutionContext::Project &SolutionContext::addProject(VSProjectType type, const
869897
projects[n].name = n;
870898
projects[n].pctx.ptype = type;
871899
projects[n].solution_dir = solution_dir;
900+
901+
if (!solution_dir.empty())
902+
nested_projects[n] = solution_dir;
903+
872904
return projects[n];
873905
}
874906

@@ -1218,8 +1250,8 @@ void VSGenerator::generate(const Build &b)
12181250
auto pps = pp.toString();
12191251
/*if (t->pkg.getOverriddenDir()) // uncomment for overridden
12201252
pps = overridden_deps_subdir / pps;
1221-
else if (!t->Local)
1222-
pps = deps_subdir / pps;*/
1253+
else */if (!t->Local)
1254+
pps = deps_subdir / pps;
12231255

12241256
auto t2 = VSProjectType::Makefile;
12251257
if (type == GeneratorType::VisualStudio)

src/driver/cpp/solution.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ Solution::Solution(const Solution &rhs)
418418
, file_storage_local(rhs.file_storage_local)
419419
, command_storage(rhs.command_storage)
420420
, prefix_source_dir(rhs.prefix_source_dir)
421+
, build(rhs.build)
421422
{
422423
checker.solution = this;
423424
}
@@ -1564,7 +1565,9 @@ bool Build::prepareStep()
15641565

15651566
Solution &Build::addSolutionRaw()
15661567
{
1567-
return solutions.emplace_back(*this);
1568+
auto &s = solutions.emplace_back(*this);
1569+
s.build = this;
1570+
return s;
15681571
}
15691572

15701573
Solution &Build::addSolution()

src/driver/cpp/solution.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ struct SW_DRIVER_CPP_API SolutionSettings
110110
String getConfig(const TargetBase *t, bool use_short_config = false) const;
111111
};
112112

113+
struct Build;
114+
113115
/**
114116
* \brief Single configuration solution.
115117
*/
@@ -130,6 +132,7 @@ struct SW_DRIVER_CPP_API Solution : TargetBase
130132
path config_file_or_dir; // original file or dir
131133
bool disable_compiler_lookup = false;
132134
path prefix_source_dir; // used for fetches (additional root dir to config/sources)
135+
const Build *build = nullptr;
133136

134137
VariablesType Variables;
135138

src/driver/cpp/target/native.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,23 @@ Commands NativeExecutedTarget::getCommands1() const
991991

992992
// set fancy name
993993
if (/*!Local && */!IsConfig && !do_not_mangle_object_names)
994-
c->name = "[" + pkg.toString() + "]" + getSelectedTool()->Extension;
994+
{
995+
c->name.clear();
996+
997+
if (getSolution()->build->solutions.size() > 1)
998+
{
999+
auto i = std::find_if(getSolution()->build->solutions.begin(), getSolution()->build->solutions.end(), [this](auto &s)
1000+
{
1001+
return &s == getSolution();
1002+
});
1003+
if (i == getSolution()->build->solutions.end())
1004+
throw SW_RUNTIME_ERROR("Wrong sln");
1005+
1006+
c->name += "sln [" + std::to_string(i - getSolution()->build->solutions.begin() + 1) +
1007+
"/" + std::to_string(getSolution()->build->solutions.size()) + "] ";
1008+
}
1009+
c->name += "[" + pkg.toString() + "]" + getSelectedTool()->Extension;
1010+
}
9951011

9961012
// copy deps
9971013
/*auto cdb = std::make_shared<ExecuteCommand>(true, [p = pkg(), c = getConfig()]
@@ -1521,7 +1537,7 @@ bool NativeExecutedTarget::prepare()
15211537
Definitions["SW_EXPORT"] = "__attribute__ ((visibility (\"default\")))";
15221538
Definitions["SW_IMPORT"] = "__attribute__ ((visibility (\"default\")))";
15231539
}
1524-
Definitions["SW_STATIC="];
1540+
//Definitions["SW_STATIC="];
15251541
}
15261542
RETURN_PREPARE_MULTIPASS_NEXT_PASS;
15271543
case 2:

test/build/cpp/multiconf/src/main3.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
#include "lib6.h"
2+
13
#include <iostream>
24

35
int main()
46
{
7+
f();
8+
59
printf(R"(
610
#include <iostream>
711

test/build/cpp/multiconf/sw.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ void build(Solution &s)
1515
auto &t2 = s.addExecutable("test2");
1616
t2 += "src/main2.cpp";
1717

18-
auto &t3 = s.addExecutable("test3");
18+
auto &l6 = s.addLibrary("lib6");
19+
l6.ApiName = "L6_API";
20+
l6 += "src/lib6.*"_rr;
21+
22+
auto &t3 = s.addExecutable("test3");
1923
t3.CPPVersion = CPPLanguageStandard::CPP11;
2024
t3 += "src/main3.cpp";
25+
t3 += l6;
26+
27+
auto &l5 = s.addLibrary("lib5");
28+
l5.ApiName = "L5_API";
29+
l5 += "src/lib5.cpp";
2130

2231
auto &t4 = s.addExecutable("test4");
2332
{
2433
auto c = t4.addCommand();
2534
c << cmd::prog(t3)
2635
<< cmd::std_out("main4.cpp");
2736
}
28-
29-
//auto &t3 = s.addExecutable("test3");
30-
//t3 += "src/a/main3.cpp";
3137
}

0 commit comments

Comments
 (0)