Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cmake/CMakeListsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace proj2cmake;
void cmake::ListsWriter::operator()(std::ostream& os)
{
os << "SET(" << cmake::tokenize(mProject.first.name) << "_SRC" << std::endl;
for(auto&& f : mProject.second.compileFiles)
for(auto&& f : *mProject.second.spCompileFiles)
{
os << " " << f << std::endl;
}
Expand All @@ -16,9 +16,23 @@ void cmake::ListsWriter::operator()(std::ostream& os)
os << std::endl;

os << "SET(" << cmake::tokenize(mProject.first.name) << "_DEPS" << std::endl;
for(auto&& proc : mProject.second.referencedProjects)
for(auto&& proc : *mProject.second.spReferencedProjects)
{
os << " " << cmake::tokenize(proc.name) << std::endl;
}
os << " )" << std::endl;

os << "SET(" << cmake::tokenize(mProject.first.name) << "_ADDITIONAL_INCLUDE_DIRS" << std::endl;
for (auto&& dir : *mProject.second.spAdditionalIncludeDirs)
{
os << " " << dir << std::endl;
}
os << " )" << std::endl;

os << "SET(" << cmake::tokenize(mProject.first.name) << "_ADDITIONAL_LINK_DIRS" << std::endl;
for (auto&& dir : *mProject.second.spAdditionalLinkDirs)
{
os << " " << dir << std::endl;
}
os << " )" << std::endl;
}
143 changes: 143 additions & 0 deletions src/cmake/CMakeMsc.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "CMakeMsc.hpp"
#include "CMakeListsWriter.hpp"
#include "CMakeConfigTemplateWriter.hpp"
#include "CMakeSubDirRegistering.hpp"

using namespace proj2cmake;

Expand Down Expand Up @@ -61,3 +64,143 @@ std::string cmake::tokenize(const std::string& name)

return res;
}


void cmake::writeGeneratedNote(std::ostream& os)
{
os << "#" << std::endl;
os << "# This file was genared by proj2cmake and will be overwritten on it's next run!" << std::endl;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

genared == generated

os << "# Please put all configurations in the cmake_conf/*.cmake files." << std::endl;
os << "#" << std::endl;
os << std::endl;
}

void cmake::writeProject(std::ofstream& os, const vcx::Solution& solution, const vcx::ProjectsPaths& dirToProj)
{
os << "PROJECT(" << solution.name << ")" << std::endl;
os << std::endl;
os << "set(CMAKE_INCLUDE_CURRENT_DIR ON)" << std::endl;
os << std::endl;
fs::path confFile = "cmake_conf";
confFile /= (solution.name + ".cmake");

//os << "IF(EXISTS \"${" << solution.name << "_SOURCE_DIR}/cmake_conf/" << solution.name << ".cmake\")" << std::endl;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To leave commented out code is a bad practice.

os << "INCLUDE(\"${" << solution.name << "_SOURCE_DIR}/cmake_conf/" << solution.name << ".cmake\")" << std::endl;
//os << "ENDIF()" << std::endl;
os << std::endl;

auto fullConfPath = solution.basePath / confFile;
if (fs::exists(fullConfPath) == false)
{
fs::create_directories(fullConfPath.parent_path());
cmake::ConfigTemplateWriter writer(solution);
std::ofstream os(fullConfPath.native());
writer(os);
}

cmake::CMakeSubDirRegistering subDirRegister(os);
for (auto&& subDir : dirToProj)
{
if (solution.basePath == subDir.first)
continue;

subDirRegister(subDir.second[0]->first.projectFile.parent_path());
}
os << std::endl;
}

void cmake::writeSolution(const vcx::Solution& solution)
{

vcx::ProjectsPaths dirToProj;

for (auto p : solution.projects)
{
auto&& pInfo = p.first;
auto&& project = p.second;

if (project.spCompileFiles->empty())
continue;

auto cmakeSrcFile = solution.basePath / pInfo.projectFile;
cmakeSrcFile.replace_extension(".cmake");

cmake::ListsWriter writer(p);

std::ofstream os(cmakeSrcFile.native());
cmake::writeGeneratedNote(os);
writer(os);
dirToProj[cmakeSrcFile.parent_path()].push_back(std::make_shared<vcx::ProjectPair>(p));
}

bool hasProject = false;

for (auto p : dirToProj)
{
auto f = p.first / "CMakeLists.txt";
std::ofstream os(f.native());
cmake::writeGeneratedNote(os);

os << "cmake_minimum_required(VERSION 2.8)" << std::endl;
os << std::endl;
if (p.first == solution.basePath)
{
cmake::writeProject(os, solution, dirToProj);
hasProject = true;
}

for (auto&& pr : p.second)
{
auto&& pInfo = pr->first;
auto&& project = pr->second;

os << std::endl;

auto cmakeSrcFile = solution.basePath / pInfo.projectFile;
cmakeSrcFile.replace_extension(".cmake");
std::cout << "Generating cmake " << cmakeSrcFile.string() << std::endl;

os << "INCLUDE(\"" + cmakeSrcFile.filename().string() + "\")" << std::endl;
os << std::endl;
if (project.bMultiThreaded)
os << "set(CMAKE_CXX_FLAGS_RELEASE \"/MT\")" << std::endl;
if (project.bMultiThreadedDebug)
os << "set(CMAKE_CXX_FLAGS_DEBUG \"/MTd\")" << std::endl;
os << std::endl;

os << cmake::cmakeStartType(pInfo.name, project.type) << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_SRC})" << std::endl;
os << std::endl;

if (project.spAdditionalIncludeDirs->size() > 0)
{
os << "TARGET_INCLUDE_DIRECTORIES(" << cmake::tokenize(pInfo.name) << " PUBLIC " << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_ADDITIONAL_INCLUDE_DIRS})" << std::endl;
os << std::endl;
}

if (project.spAdditionalLinkDirs->size() > 0)
{
os << "LINK_DIRECTORIES(" << cmake::tokenize(pInfo.name) << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_ADDITIONAL_LINK_DIRS})" << std::endl;
os << std::endl;
}

os << "TARGET_LINK_LIBRARIES(" << cmake::tokenize(pInfo.name) << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_DEPS}" << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_ADDITIONAL_DEPS}" << std::endl;
os << " ${SOLUTION_" << cmake::cmakeTypeCaption(project.type) << "_DEPS}" << std::endl;
os << " ${SOLUTION_GENERAL_DEPS})" << std::endl;
os << std::endl;
}
}

if (!hasProject)
{
auto f = solution.basePath / "CMakeLists.txt";
std::ofstream os(f.native());
cmake::writeProject(os, solution, dirToProj);
}

std::cout << dirToProj.size() << " projects have been converted" << std::endl;
}
8 changes: 8 additions & 0 deletions src/cmake/CMakeMsc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "../vcx/VCXStructs.hpp"

namespace fs = boost::filesystem;

namespace proj2cmake
{
namespace cmake
Expand All @@ -15,5 +17,11 @@ std::string cmakeStartType(const std::string& name, vcx::ConfigurationType type)

std::string tokenize(const std::string& name);

void writeGeneratedNote(std::ostream& os);

void writeProject(std::ofstream& os, const vcx::Solution& solution, const vcx::ProjectsPaths& dirToProj);

void writeSolution(const vcx::Solution& solution);

}
}
121 changes: 7 additions & 114 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,57 +1,11 @@
#include "vcx/VCXParser.hpp"

#include "cmake/CMakeListsWriter.hpp"
#include "cmake/CMakeConfigTemplateWriter.hpp"
#include "vcx/VCXStructs.hpp"
#include "cmake/CMakeMsc.hpp"
#include "cmake/CMakeSubDirRegistering.hpp"

#include <map>
#include <iostream>

using namespace proj2cmake;
namespace fs = boost::filesystem;
using ProjectsPaths = std::map<fs::path, std::vector<vcx::ProjectPair*>>;

void writeGeneratedNote(std::ostream& os, const char* procName)
{
os << "#" << std::endl;
os << "# This file was genared by " << procName << " and will be overwritten on it's next run!" << std::endl;
os << "# Please put all configurations in the cmake_conf/*.cmake files." << std::endl;
os << "#" << std::endl;
os << std::endl;
}

void writeProject(std::ofstream& os, const vcx::Solution& solution, const ProjectsPaths& dirToProj)
{
os << "PROJECT(" << solution.name << ")" << std::endl;
os << std::endl;

fs::path confFile = "cmake_conf";
confFile /= (solution.name + ".cmake");

//os << "IF(EXISTS \"${" << solution.name << "_SOURCE_DIR}/cmake_conf/" << solution.name << ".cmake\")" << std::endl;
os << "INCLUDE(\"${" << solution.name << "_SOURCE_DIR}/" + confFile.string() + "\")" << std::endl;
//os << "ENDIF()" << std::endl;
os << std::endl;

auto fullConfPath = solution.basePath / confFile;
if(fs::exists(fullConfPath) == false)
{
fs::create_directories(fullConfPath.parent_path());
cmake::ConfigTemplateWriter writer(solution);
std::ofstream os(fullConfPath.native());
writer(os);
}

cmake::CMakeSubDirRegistering subDirRegister(os);
for(auto&& subDir : dirToProj)
{
if (solution.basePath == subDir.first)
continue;

subDirRegister(subDir.second[0]->first.projectFile.parent_path());
}
os << std::endl;
}

int main(int argc, char** argv)
{
Expand All @@ -74,74 +28,13 @@ int main(int argc, char** argv)
}

vcx::SolutionParser parser(solutionFile);
auto solution = parser();

ProjectsPaths dirToProj;

for(auto&& p : solution.projects)
{
auto&& pInfo = p.first;
auto&& project = p.second;

if(project.compileFiles.empty())
continue;

auto cmakeSrcFile = solution.basePath / pInfo.projectFile;
cmakeSrcFile.replace_extension(".cmake");
cmake::ListsWriter writer(p);

std::ofstream os(cmakeSrcFile.native());
writeGeneratedNote(os, procName);
writer(os);

dirToProj[cmakeSrcFile.parent_path()].push_back(&p);
}

bool hasProject = false;

for(auto&& p : dirToProj)
{
auto f = p.first / "CMakeLists.txt";
std::ofstream os(f.native());
writeGeneratedNote(os, procName);

os << "cmake_minimum_required(VERSION 2.8)" << std::endl;
os << std::endl;
if(p.first == solution.basePath)
{
writeProject(os, solution, dirToProj);
hasProject = true;
}

for(auto&& pr : p.second)
{
auto&& pInfo = pr->first;
auto&& project = pr->second;

os << std::endl;

auto cmakeSrcFile = solution.basePath / pInfo.projectFile;
cmakeSrcFile.replace_extension(".cmake");

os << "INCLUDE(\"" + cmakeSrcFile.filename().string() + "\")" << std::endl;
os << std::endl;
os << cmake::cmakeStartType(pInfo.name, project.type) << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_SRC})" << std::endl;
os << std::endl;
os << "TARGET_LINK_LIBRARIES(" << cmake::tokenize(pInfo.name) << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_DEPS}" << std::endl;
os << " ${" << cmake::tokenize(pInfo.name) << "_ADDITIONAL_DEPS}" << std::endl;
os << " ${SOLUTION_" << cmake::cmakeTypeCaption(project.type) << "_DEPS}" << std::endl;
os << " ${SOLUTION_GENERAL_DEPS})" << std::endl;
os << std::endl;
}
try {
cmake::writeSolution(parser());
}

if (!hasProject)
{
auto f = solution.basePath / "CMakeLists.txt";
std::ofstream os(f.native());
writeProject(os, solution, dirToProj);
catch (std::runtime_error e) {
std::cerr << e.what() << std::endl;
return 1;
}

return 0;
Expand Down
Loading