1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : vcimporter.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "vcimporter.h"
#include "macros.h"
#include "workspace.h"
#include "wx/filename.h"
#include "wx/tokenzr.h"
#include "xmlutils.h"
#define NEXT_LINE(line) \
line.Empty(); \
if(!ReadLine(line)) { \
return false; \
}
VcImporter::VcImporter(const wxString& fileName, const wxString& defaultCompiler)
: m_fileName(fileName)
, m_is(NULL)
, m_tis(NULL)
, m_compiler(defaultCompiler)
, m_compilerLowercase(defaultCompiler)
{
m_compilerLowercase.MakeLower();
wxFileName fn(m_fileName);
m_isOk = fn.FileExists();
if(m_isOk) {
m_is = new wxFileInputStream(fn.GetFullPath());
m_tis = new wxTextInputStream(*m_is);
}
}
VcImporter::~VcImporter()
{
if(m_is) {
delete m_is;
}
if(m_tis) {
delete m_tis;
}
}
bool VcImporter::ReadLine(wxString& line)
{
line = wxEmptyString;
if(m_isOk) {
while(!m_is->Eof()) {
line = m_tis->ReadLine();
TrimString(line);
if(line.Length() == 1 || line.Length() == 2 || line.IsEmpty() || line.StartsWith(wxT("#"))) {
// get next line
continue;
} else {
return true;
}
}
}
return false;
}
bool VcImporter::Import(wxString& errMsg)
{
wxString line;
while(true) {
if(!ReadLine(line))
break;
if(line.StartsWith(wxT("Project"))) {
if(!OnProject(line, errMsg)) {
return false;
}
}
}
// create LE files
CreateWorkspace();
CreateProjects();
return true;
}
bool VcImporter::OnProject(const wxString& firstLine, wxString& errMsg)
{
// first line contains the project name, project file path, and project id
wxStringTokenizer tkz(firstLine, wxT("="));
if(tkz.CountTokens() != 2) {
errMsg = wxT("Invalid 'Project' section found. expected <expr> = <expr>");
return false;
}
tkz.NextToken();
wxString token = tkz.NextToken();
TrimString(token);
wxStringTokenizer tk2(token, wxT(","));
if(tk2.CountTokens() != 3) {
errMsg = wxT("Invalid 'Project' section found. expected <project name>, <project file path>, <project id>");
return false;
}
VcProjectData pd;
pd.name = tk2.NextToken();
RemoveGershaim(pd.name);
pd.filepath = tk2.NextToken();
RemoveGershaim(pd.filepath);
// Make sure that the project path has a forward slash style
pd.filepath.Replace(wxT("\\"), wxT("/"));
pd.id = tk2.NextToken();
RemoveGershaim(pd.id);
std::pair<wxString, VcProjectData> p;
p.first = pd.id;
p.second = pd;
m_projects.insert(p);
// Skip all lines until EndProject section is found
wxString line;
while(true) {
NEXT_LINE(line);
if(line == wxT("EndProject")) {
return true;
}
}
return false;
}
void VcImporter::RemoveGershaim(wxString& str)
{
TrimString(str);
str = str.AfterFirst(wxT('"'));
str = str.BeforeLast(wxT('"'));
}
void VcImporter::CreateWorkspace()
{
// create a workspace file from the data we collected
wxFileName fn(m_fileName);
wxString errMsg;
clCxxWorkspaceST::Get()->CreateWorkspace(fn.GetName(), fn.GetPath(), errMsg);
}
//
// ConfigurationType = 1 // exe
// ConfigurationType = 2 // dll
// ConfigurationType = 4 // static lib
//
void VcImporter::CreateProjects()
{
std::map<wxString, VcProjectData>::iterator iter = m_projects.begin();
for(; iter != m_projects.end(); iter++) {
// load xml file
VcProjectData pd = iter->second;
ConvertProject(pd);
}
}
bool VcImporter::ConvertProject(VcProjectData& data)
{
wxXmlDocument doc(data.filepath);
if(!doc.IsOk()) {
return false;
}
// to create a project skeleton, we need the project type
// since VS allows each configuration to be of different
// type, while LE allows single type for all configurations
// we use the first configuration type that we find
wxXmlNode* configs = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("Configurations"));
if(!configs) {
return false;
}
// find the first configuration node
wxXmlNode* config = XmlUtils::FindFirstByTagName(configs, wxT("Configuration"));
if(!config)
return false;
// read the configuration type, default is set to Executeable
long type = XmlUtils::ReadLong(config, wxT("ConfigurationType"), 1);
wxString projectType;
wxString errMsg;
switch(type) {
case 2: // dll
projectType = PROJECT_TYPE_DYNAMIC_LIBRARY;
break;
case 4: // static library
projectType = PROJECT_TYPE_STATIC_LIBRARY;
break;
case 1: // exe
default:
projectType = PROJECT_TYPE_EXECUTABLE;
break;
}
// now we can create the project
wxFileName fn(data.filepath);
fn.MakeAbsolute();
if(!clCxxWorkspaceST::Get()->CreateProject(data.name, fn.GetPath(), projectType, "", true, errMsg)) {
return false;
}
// get the new project instance
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(data.name, errMsg);
ProjectSettingsPtr le_settings(new ProjectSettings(NULL));
// remove the default 'Debug' configuration
le_settings->RemoveConfiguration(wxT("Debug"));
le_settings->SetProjectType(projectType);
while(config) {
if(config->GetName() == wxT("Configuration")) {
AddConfiguration(le_settings, config);
}
config = config->GetNext();
}
proj->SetSettings(le_settings);
// add all virtual folders
wxXmlNode* files = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("Files"));
if(files) {
proj->BeginTranscation();
CreateFiles(files, wxEmptyString, proj);
proj->CommitTranscation();
}
return true;
}
void VcImporter::AddConfiguration(ProjectSettingsPtr settings, wxXmlNode* config)
{
// configuration name
wxString name = XmlUtils::ReadString(config, wxT("Name"));
name = name.BeforeFirst(wxT('|'));
name.Replace(wxT(" "), wxT("_"));
BuildConfigPtr le_conf(new BuildConfig(NULL));
le_conf->SetName(name);
le_conf->SetIntermediateDirectory(XmlUtils::ReadString(config, wxT("IntermediateDirectory")));
// get the compiler settings
wxXmlNode* cmpNode = XmlUtils::FindNodeByName(config, wxT("Tool"), wxT("VCCLCompilerTool"));
// get the include directories
le_conf->SetIncludePath(SplitString(XmlUtils::ReadString(cmpNode, wxT("AdditionalIncludeDirectories"))));
le_conf->SetPreprocessor(XmlUtils::ReadString(cmpNode, wxT("PreprocessorDefinitions")));
// Select the best compiler for the import process (we select g++ by default)
le_conf->SetCompilerType(m_compiler);
// Get the configuration type
long type = XmlUtils::ReadLong(config, wxT("ConfigurationType"), 1);
wxString projectType;
wxString errMsg;
switch(type) {
case 2: // dll
projectType = PROJECT_TYPE_DYNAMIC_LIBRARY;
break;
case 4: // static library
projectType = PROJECT_TYPE_STATIC_LIBRARY;
break;
case 1: // exe
default:
projectType = PROJECT_TYPE_EXECUTABLE;
break;
}
le_conf->SetProjectType(projectType);
// if project type is DLL or Executable, copy linker settings as well
if(settings->GetProjectType(le_conf->GetName()) == PROJECT_TYPE_EXECUTABLE ||
settings->GetProjectType(le_conf->GetName()) == PROJECT_TYPE_DYNAMIC_LIBRARY) {
wxXmlNode* linkNode = XmlUtils::FindNodeByName(config, wxT("Tool"), wxT("VCLinkerTool"));
if(linkNode) {
wxString outputFileName(XmlUtils::ReadString(linkNode, wxT("OutputFile")));
#ifndef __WXMSW__
outputFileName.Replace(wxT(".dll"), wxT(".so"));
outputFileName.Replace(wxT(".exe"), wxT(""));
#endif
le_conf->SetOutputFileName(outputFileName);
// read in the additional libraries & libpath
wxString libs = XmlUtils::ReadString(linkNode, wxT("AdditionalDependencies"));
// libs is a space delimited string
wxStringTokenizer tk(libs, wxT(" "));
libs.Empty();
while(tk.HasMoreTokens()) {
libs << tk.NextToken() << wxT(";");
}
le_conf->SetLibraries(libs);
le_conf->SetLibPath(XmlUtils::ReadString(linkNode, wxT("AdditionalLibraryDirectories")));
}
} else {
// static library
wxXmlNode* libNode = XmlUtils::FindNodeByName(config, wxT("Tool"), wxT("VCLibrarianTool"));
if(libNode) {
wxString outputFileName(XmlUtils::ReadString(libNode, wxT("OutputFile")));
outputFileName.Replace(wxT("\\"), wxT("/"));
wxString outputFileNameOnly = outputFileName.AfterLast(wxT('/'));
wxString outputFilePath = outputFileName.BeforeLast(wxT('/'));
if(m_compilerLowercase.Contains(wxT("gnu"))) {
if(outputFileNameOnly.StartsWith(wxT("lib")) == false) {
outputFileNameOnly.Prepend(wxT("lib"));
}
outputFileName.Clear();
outputFileName << outputFilePath << wxT("/") << outputFileNameOnly;
outputFileName.Replace(wxT(".lib"), wxT(".a"));
}
le_conf->SetOutputFileName(outputFileName);
}
}
// add the configuration
settings->SetBuildConfiguration(le_conf);
}
void VcImporter::CreateFiles(wxXmlNode* parent, wxString vdPath, ProjectPtr proj)
{
if(!parent) {
return;
}
wxXmlNode* child = parent->GetChildren();
while(child) {
if(child->GetName() == wxT("Filter")) {
// add new virtual directory
wxString name = XmlUtils::ReadString(child, wxT("Name"));
wxString tmpPath = vdPath;
if(tmpPath.IsEmpty() == false) {
tmpPath << wxT(":");
}
tmpPath << name;
proj->CreateVirtualDir(tmpPath);
CreateFiles(child, tmpPath, proj);
} else if(child->GetName() == wxT("File")) {
// found a file
wxString fileName = XmlUtils::ReadString(child, wxT("RelativePath"));
wxString path = vdPath;
if(path.IsEmpty()) {
path = wxT("src");
}
fileName.Replace(wxT("\\"), wxT("/"));
proj->AddFile(fileName, path);
}
child = child->GetNext();
}
}
wxArrayString VcImporter::SplitString(const wxString& s)
{
wxArrayString arr;
wxString s1(s);
s1.Replace(wxT(","), wxT(";"));
wxStringTokenizer tk1(s1, wxT(";"));
while(tk1.HasMoreTokens()) {
arr.Add(tk1.NextToken());
}
return arr;
}
|