]> git.tdb.fi Git - builder.git/commitdiff
Add support for generating Visual C++ projects
authorMikko Rasa <tdb@tdb.fi>
Sun, 22 Aug 2021 11:33:13 +0000 (14:33 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 22 Aug 2021 11:33:13 +0000 (14:33 +0300)
source/builder.cpp
source/builder.h
source/sourcepackage.cpp
source/systemtools.cpp
source/vcxprojectfile.cpp [new file with mode: 0644]
source/vcxprojectfile.h [new file with mode: 0644]
source/vcxprojectgenerator.cpp [new file with mode: 0644]
source/vcxprojectgenerator.h [new file with mode: 0644]

index 6eeaa2fa3ca3b39e05298b3bd780ff5401cccbaa..3ad6146349fbaf331fa2779e97407e4ff2aa6f6a 100644 (file)
@@ -59,6 +59,15 @@ void Builder::set_architecture(const string &name)
        }
 }
 
+vector<string> Builder::get_build_types() const
+{
+       vector<string> keys;
+       keys.reserve(build_types.size());
+       for(BuildTypeMap::const_iterator i=build_types.begin(); i!=build_types.end(); ++i)
+               keys.push_back(i->first);
+       return keys;
+}
+
 void Builder::set_build_type(const string &name)
 {
        build_type = &get_item(build_types, name);
index d12cf003a68d7fc4d994b47b9dcb8f611f1622ff..9ea6677ecfebe8caf06625cbfff0ad4a9a9b43ce 100644 (file)
@@ -74,6 +74,7 @@ public:
        const Architecture &get_current_arch() const { return *current_arch; }
        const Architecture &get_native_arch() const { return native_arch; }
        void set_build_type(const std::string &);
+       std::vector<std::string> get_build_types() const;
        const BuildType &get_build_type() const { return *build_type; }
        BuildGraph &get_build_graph() { return build_graph; }
        void set_prefix(const Msp::FS::Path &);
index 0a69d7aa22f058641980b03768b8ccce030babb0..c4061a4afbeceae8a153cf45a1b05f0d82530cc3 100644 (file)
@@ -17,6 +17,7 @@
 #include "sourcegenerator.h"
 #include "sourcepackage.h"
 #include "tool.h"
+#include "vcxprojectfile.h"
 
 using namespace std;
 using namespace Msp;
@@ -145,6 +146,9 @@ void SourcePackage::do_prepare()
                        builder.get_build_graph().get_target("install")->add_dependency(*builder.get_toolchain().get_tool("CP").create_target(*pc));
                }
        }
+
+       if(arch.get_system()=="windows")
+               new VcxProjectFile(builder, *this);
 }
 
 void SourcePackage::save_caches()
index 77fe2bedb9c2b701ad67f89fc05802de8c2fb1ad..c35b4aebf286de7255680ac2d7e2609afdf8c5c8 100644 (file)
@@ -6,6 +6,7 @@
 #include "gnulinker.h"
 #include "mingwdlltool.h"
 #include "systemtools.h"
+#include "vcxprojectgenerator.h"
 
 using namespace std;
 
@@ -27,6 +28,10 @@ SystemTools::SystemTools(Builder &builder, const Architecture &arch)
 
        add_tool(new GnuLinker(builder, arch));
        add_tool(new GnuArchiver(builder, arch));
+
        if(arch.get_system()=="windows")
+       {
                add_tool(new MingwDllTool(builder, arch));
+               add_tool(new VcxProjectGenerator(builder));
+       }
 }
diff --git a/source/vcxprojectfile.cpp b/source/vcxprojectfile.cpp
new file mode 100644 (file)
index 0000000..7b09db0
--- /dev/null
@@ -0,0 +1,9 @@
+#include "builder.h"
+#include "sourcepackage.h"
+#include "vcxprojectfile.h"
+
+VcxProjectFile::VcxProjectFile(Builder &b, const SourcePackage &p):
+       FileTarget(b, p, p.get_source_directory()/(p.get_name()+".vcxproj"))
+{
+       tool = &builder.get_toolchain().get_tool("VCXG");
+}
diff --git a/source/vcxprojectfile.h b/source/vcxprojectfile.h
new file mode 100644 (file)
index 0000000..b5a90d1
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef VCXPROJECTFILE_H_
+#define VCXPROJECTFILE_H_
+
+#include "filetarget.h"
+
+class VcxProjectFile: public FileTarget
+{
+public:
+       VcxProjectFile(Builder &, const SourcePackage &);
+
+       virtual const char *get_type() const { return "VcxProjectFile"; }
+};
+
+#endif
diff --git a/source/vcxprojectgenerator.cpp b/source/vcxprojectgenerator.cpp
new file mode 100644 (file)
index 0000000..b5c42c5
--- /dev/null
@@ -0,0 +1,126 @@
+#include <msp/core/application.h>
+#include <msp/fs/utils.h>
+#include <msp/io/print.h>
+#include <msp/strings/utils.h>
+#include "builder.h"
+#include "csourcefile.h"
+#include "executable.h"
+#include "sourcepackage.h"
+#include "vcxprojectfile.h"
+#include "vcxprojectgenerator.h"
+
+using namespace std;
+using namespace Msp;
+
+VcxProjectGenerator::VcxProjectGenerator(Builder &b):
+       Tool(b, "VCXG")
+{ }
+
+Target *VcxProjectGenerator::create_target(const list<Target *> &, const string &)
+{
+       throw logic_error("Not implemented");
+}
+
+Task *VcxProjectGenerator::run(const Target &target) const
+{
+       const VcxProjectFile &project = dynamic_cast<const VcxProjectFile &>(target);
+       Worker *worker = new Worker(project);
+       return new InternalTask(worker);
+}
+
+
+VcxProjectGenerator::Worker::Worker(const VcxProjectFile &t):
+       target(t)
+{ }
+
+void VcxProjectGenerator::Worker::main()
+{
+       const SourcePackage &spkg = *target.get_package();
+       Builder &builder = spkg.get_builder();
+
+       IO::BufferedFile out(target.get_path().str(), IO::M_WRITE);
+       IO::print(out, "<Project DefaultTargets=\"Build\" ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
+
+       IO::print(out, "\t<ItemGroup Label=\"ProjectConfigurations\">\n");
+       vector<string> build_types = builder.get_build_types();
+       const char *platforms[] = { "x86-32", "x86-64", 0 };
+       for(const char **i=platforms; *i; ++i)
+               for(vector<string>::const_iterator j=build_types.begin(); j!=build_types.end(); ++j)
+               {
+                       IO::print(out, "\t\t<ProjectConfiguration Include=\"%s|%s\">\n", *j, *i);
+                       IO::print(out, "\t\t\t<Configuration>%s</Configuration>\n", *j);
+                       IO::print(out, "\t\t\t<Platform>%s</Platform>\n", *i);
+                       IO::print(out, "\t\t</ProjectConfiguration>\n");
+               }
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<PropertyGroup Label=\"Globals\">\n");
+       IO::print(out, "\t\t<VCProjectVersion>15.0</VCProjectVersion>\n");
+       IO::print(out, "\t\t<Keyword>MakeFileProj</Keyword>\n");
+       IO::print(out, "\t</PropertyGroup>\n");
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n");
+
+       const Target *world = builder.get_build_graph().get_target("world");
+       const Target::Dependencies &world_deps = world->get_dependencies();
+       const Executable *exe = 0;
+       for(Target::Dependencies::const_iterator i=world_deps.begin(); (!exe && i!=world_deps.end()); ++i)
+               if((*i)->get_package()==&spkg)
+                       exe = dynamic_cast<const Executable *>(*i);
+
+       const char *argv0 = Application::get_argv0();
+       for(const char **i=platforms; *i; ++i)
+               for(vector<string>::const_iterator j=build_types.begin(); j!=build_types.end(); ++j)
+               {
+                       IO::print(out, "\t<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='%s|%s'\" Label=\"Configuration\">\n", *j, *i);
+                       IO::print(out, "\t\t<ConfigurationType>MakeFile</ConfigurationType>\n");
+                       IO::print(out, "\t\t<NMakeBuildCommandLine>%s --arch=%s --build-type=%s</NMakeBuildCommandLine>\n", argv0, *i, *j);
+                       IO::print(out, "\t\t<NMakeCleanCommandLine>%s --arch=%s --build-type=%s -c</NMakeCleanCommandLine>\n", argv0, *i, *j);
+                       IO::print(out, "\t\t<NMakeReBuildCommandLine>%s --arch=%s --build-type=%s -B</NMakeReBuildCommandLine>\n", argv0, *i, *j);
+                       if(exe)
+                               IO::print(out, "\t\t<NMakeOutput>%s</NMakeOutput>\n", exe->get_path());
+                       IO::print(out, "\t</PropertyGroup>\n");
+               }
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n");
+
+       const BuildGraph::TargetMap &targets = builder.get_build_graph().get_targets();
+       vector<const FileTarget *> sources;
+       vector<const FileTarget *> includes;
+       vector<const FileTarget *> others;
+       for(BuildGraph::TargetMap::const_iterator i=targets.begin(); i!=targets.end(); ++i)
+               if(i->second->get_package()==&spkg && !i->second->is_buildable())
+                       if(const FileTarget *file = dynamic_cast<const FileTarget *>(i->second))
+                       {
+                               if(dynamic_cast<const CSourceFile *>(file))
+                               {
+                                       string ext = tolower(FS::extpart(FS::basename(file->get_path())));
+                                       if(ext==".h" || ext==".hpp")
+                                               includes.push_back(file);
+                                       else
+                                               sources.push_back(file);
+                               }
+                               else
+                                       others.push_back(file);
+                       }
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(vector<const FileTarget *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
+               IO::print(out, "\t\t<ClCompile Include=\"%s\" />\n", (*i)->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(vector<const FileTarget *>::const_iterator i=includes.begin(); i!=includes.end(); ++i)
+               IO::print(out, "\t\t<ClInclude Include=\"%s\" />\n", (*i)->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<ItemGroup>\n");
+       for(vector<const FileTarget *>::const_iterator i=others.begin(); i!=others.end(); ++i)
+               IO::print(out, "\t\t<None Include=\"%s\" />\n", (*i)->get_path());
+       IO::print(out, "\t</ItemGroup>\n");
+
+       IO::print(out, "\t<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\" />\n");
+       IO::print(out, "</Project>\n");
+
+       status = Task::SUCCESS;
+}
diff --git a/source/vcxprojectgenerator.h b/source/vcxprojectgenerator.h
new file mode 100644 (file)
index 0000000..919b8ec
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef VCXPROJECTGENERATOR_H_
+#define VCXPROJECTGENERATOR_H_
+
+#include "internaltask.h"
+#include "tool.h"
+
+class VcxProjectFile;
+
+class VcxProjectGenerator: public Tool
+{
+private:
+       class Worker: public InternalTask::Worker
+       {
+       private:
+               const VcxProjectFile &target;
+
+       public:
+               Worker(const VcxProjectFile &);
+
+       private:
+               virtual void main();
+       };
+
+public:
+       VcxProjectGenerator(Builder &);
+
+       virtual Target *create_target(const std::list<Target *> &, const std::string &);
+       virtual Task *run(const Target &) const;
+};
+
+#endif