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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : CMakeGenerator.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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/* ************************************************************************ */
/* */
/* CMakePlugin for Codelite */
/* Copyright (C) 2013 Jiří Fatka <ntsfka@gmail.com> */
/* */
/* 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 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* ************************************************************************ */
/* ************************************************************************ */
/* INCLUDES */
/* ************************************************************************ */
// Declarations
#include "CMakeGenerator.h"
#include "fileextmanager.h"
// wxWidgets
#include <wx/tokenzr.h>
#include <wx/ffile.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
// Plugin
#include "CMakePlugin.h"
#include "globals.h"
/* ************************************************************************ */
/* FUNCTIONS */
/* ************************************************************************ */
/**
* @brief Check if filename exists and asks if user want it overwrite if
* exists.
*
* @param filename Tested filename.
*
* @return If file can be written.
*/
static bool CheckExists(const wxFileName& filename)
{
// Output file exists, overwrite?
if (filename.Exists()) {
wxString msg;
msg << CMakePlugin::CMAKELISTS_FILE
<< " exists. Overwrite?\n"
<< "("
<< filename.GetFullPath()
<< ")";
wxStandardID answer = ::PromptForYesNoDialogWithCheckbox( msg,
"CMakePluginOverwriteDlg",
_("Overwrite"),
_("Don't Overwrite"),
_("Remember my answer and don't annoy me again"),
wxYES_NO | wxCANCEL| wxCENTER | wxICON_QUESTION | wxYES_DEFAULT);
return (answer == wxID_YES);
}
// File doesn't exists, write it
return true;
}
/* ************************************************************************ */
/**
* @brief Writes content into file.
*
* @param filename Output file name.
* @param content Written text.
*/
static void WriteContent(const wxFileName& filename, const wxString& content)
{
// Write result CMakeLists.txt
wxFFile output(filename.GetFullPath(), "w+b");
if (output.IsOpened()) {
output.Write(content);
output.Close();
}
}
/* ************************************************************************ */
/* CLASSES */
/* ************************************************************************ */
bool
CMakeGenerator::Generate(Workspace* workspace)
{
if ( !workspace )
return false;
// Export all projects
wxArrayString projectsArr;
workspace->GetProjectList( projectsArr );
for(size_t i=0; i<projectsArr.GetCount(); ++i) {
ProjectPtr pProj = workspace->GetProject(projectsArr.Item(i));
Generate( pProj, false );
}
// ====----------------------------
// Now generate the workspace
// ====----------------------------
// Get workspace directory.
const wxFileName workspaceDir = workspace->GetWorkspaceFileName().
GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
// Output file name
const wxFileName filename(workspaceDir.GetPath(), CMakePlugin::CMAKELISTS_FILE);
if (!CheckExists(filename))
return false;
// File content
wxString content;
// Print project name
content << "# Workspace name\n";
content << "project(" << workspace->GetName() << ")\n\n";
// Environment variables
{
wxString variables = workspace->GetEnvironmentVariabels(); // Nice typo
variables.Trim().Trim(false);
if (!variables.IsEmpty()) {
// Split into a list of pairs
const wxArrayString list = wxStringTokenize(variables, "\n;");
for (wxArrayString::const_iterator it = list.begin(),
ite = list.end(); it != ite; ++it) {
// Split into name, value pair
const wxArrayString pair = wxSplit(*it, '=');
const wxString& name = pair[0];
const wxString value = (pair.GetCount() >= 2) ? pair[1] : "";
// Set environment variable
content << "set(" << name << " \"" << value << "\")\n";
}
content << "\n";
}
}
content << "# Projects\n";
// Get full paths to all projects
const wxArrayString projects = workspace->GetAllProjectPaths();
// Foreach projects path
for (wxArrayString::const_iterator it = projects.begin(),
ite = projects.end(); it != ite; ++it) {
wxFileName fullpath = *it;
fullpath.MakeRelativeTo(workspaceDir.GetPath());
// Create path to CMakeLists.txt in the project
const wxFileName cmakelist(fullpath.GetPath(), CMakePlugin::CMAKELISTS_FILE);
// Skip directories without CMakeLists.txt
if (!cmakelist.Exists())
continue;
// Write
wxString projectDir = fullpath.GetPath();
if ( projectDir.IsEmpty() ) projectDir = ".";
content << "add_subdirectory(" << projectDir << ")\n";
}
// Write result
WriteContent(filename, content);
return true;
}
/* ************************************************************************ */
bool
CMakeGenerator::Generate(ProjectPtr project, bool topProject)
{
wxASSERT(project);
// Get project directory
wxString projectDir = project->GetFileName().GetPath();
// Get the project build configuration and compiler
BuildConfigPtr buildConf = project->GetBuildConfiguration();
// Sanity
if ( !buildConf || buildConf->IsCustomBuild() ) {
// no build config or custom build?
return false;
}
CompilerPtr compiler = buildConf->GetCompiler();
if ( !compiler ) {
return false;
}
wxFileName projectPath = project->GetFileName();
wxArrayString depsPaths;
wxArrayString depsProjectNames;
if ( topProject ) {
wxArrayString depsProjects = project->GetDependencies( buildConf->GetName() );
for(size_t i=0; i<depsProjects.GetCount(); ++i) {
ProjectPtr pProj = WorkspaceST::Get()->GetProject( depsProjects.Item(i) );
if ( pProj ) {
if ( Generate( pProj, false ) ) {
wxString depProjFilePath = pProj->GetFileName().GetFullPath();
wxFileName fnDepProj( depProjFilePath );
fnDepProj.MakeRelativeTo( projectPath.GetPath() );
wxString relPath = fnDepProj.GetPath(false, wxPATH_UNIX);
// Keep the project path relative to current project
depsPaths.Add( relPath );
// Keep the project name
depsProjectNames.Add( pProj->GetName() );
}
}
}
}
// Output file name
wxFileName filename(projectDir, CMakePlugin::CMAKELISTS_FILE);
if (!CheckExists(filename))
return false;
// File content
wxString content;
content << "cmake_minimum_required(VERSION 2.8.11)\n\n";
// Print project name
content << "project(" << project->GetName() << ")\n\n";
// Add the dependencies first
if ( !depsPaths.IsEmpty() ) {
content << "# Add the build order directories\n";
}
for(size_t i=0; i<depsPaths.GetCount(); ++i) {
content << "add_subdirectory( " << depsPaths.Item(i) << " )\n";
}
content << "\n\n";
// Add include directories
{
// Get the incldue paths for the project. This also includes any
// includes from $(shell ...) commands
wxArrayString includes = project->GetIncludePaths(true);
content << "include_directories(\n";
for(size_t i=0; i<includes.GetCount(); ++i) {
wxString &includePath = includes.Item(i);
includePath.Replace("\\", "/");
includePath.Trim(false).Trim();
if ( includePath.IsEmpty() ) {
continue;
}
content << " " << includes.Item(i) << "\n";
}
content << "\n)\n\n";
}
// Add preprocessor definitions
{
wxArrayString defines = project->GetPreProcessors(false);
if ( !defines.IsEmpty() ) {
content << "add_definitions(\n";
for(size_t i=0; i<defines.GetCount(); ++i) {
wxString &pp = defines.Item(i);
pp.Trim().Trim(false);
if ( pp.IsEmpty() ) {
continue;
}
content << " -D" << pp << "\n";
}
content << ")\n\n";
}
}
// Add linker options
{
wxString links = buildConf->GetLinkOptions();
links.Trim().Trim(false);
links.Replace(";", " ");
if (!links.IsEmpty()) {
content << "# Linker options\n";
content << "set(CMAKE_LDFLAGS \"${CMAKE_LDFLAGS} " << links << "\")\n\n";
}
}
// Add libraries paths
{
wxString lib_paths = buildConf->GetLibPath();
wxString lib_switch = "-L";
// Get switch from compiler
if (compiler) {
lib_switch = compiler->GetSwitch("LibraryPath");
// Append global library paths
lib_paths << ";" << compiler->GetGlobalLibPath();
}
// Get list of library paths
wxArrayString lib_paths_list = wxStringTokenize(lib_paths, ";", wxTOKEN_STRTOK);
// Clear string
lib_paths.Clear();
// Append modified values
for (size_t i = 0; i < lib_paths_list.GetCount(); ++i) {
lib_paths << lib_switch << "\\\"" << lib_paths_list.Item(i) << "\\\" ";
}
content << "# Library path\n";
content << "set(CMAKE_LDFLAGS \"${CMAKE_LDFLAGS} " << lib_paths << "\")\n\n";
}
wxArrayString cppSources, cSources;
// Write sources
// Get files in the project
std::vector<wxFileName> files;
project->GetFiles(files);
{
for (size_t i = 0; i < files.size(); ++i) {
wxString sourceFile = files.at(i).GetFullPath();
// CMake does not handle backslash properly
sourceFile.Replace("\\", "/");
if ( FileExtManager::GetType(sourceFile) == FileExtManager::TypeSourceCpp) {
cppSources.Add( sourceFile );
} else if ( FileExtManager::GetType(sourceFile) == FileExtManager::TypeSourceC ) {
cSources.Add( sourceFile );
}
}
if ( !cSources.IsEmpty() ) {
content << "# Define the C sources\n";
content << "set ( C_SRCS\n";
for(size_t i=0; i<cSources.GetCount(); ++i) {
content << " " << cSources.Item(i) << "\n";
}
content << ")\n\n";
}
if ( !cppSources.IsEmpty() ) {
content << "# Define the CXX sources\n";
content << "set ( CXX_SRCS\n";
for(size_t i=0; i<cppSources.GetCount(); ++i) {
content << " " << cppSources.Item(i) << "\n";
}
content << ")\n\n";
}
}
// Add CXX compiler options
{
wxArrayString buildOptsArr = project->GetCXXCompilerOptions();
if ( !buildOptsArr.IsEmpty() && !cppSources.IsEmpty() ) {
content << "set_source_files_properties(\n ${CXX_SRCS} PROPERTIES COMPILE_FLAGS \n \"";
for(size_t i=0; i<buildOptsArr.GetCount(); ++i) {
content << " " << buildOptsArr.Item(i) ;
}
content << "\")\n\n";
}
}
// Add C compiler options
{
wxArrayString buildOptsArr = project->GetCCompilerOptions();
if ( !buildOptsArr.IsEmpty() && !cSources.IsEmpty() ) {
content << "set_source_files_properties(\n ${C_SRCS} PROPERTIES COMPILE_FLAGS \n \"";
for(size_t i=0; i<buildOptsArr.GetCount(); ++i) {
content << " " << buildOptsArr.Item(i) ;
}
content << "\")\n\n";
}
}
// Get project type
{
wxString type = buildConf->GetProjectType();
if (type == Project::EXECUTABLE) {
content << "add_executable(" << project->GetName() << " ${CXX_SRCS} ${C_SRCS})\n\n";
} else if (type == Project::DYNAMIC_LIBRARY) {
content << "add_library(" << project->GetName() << " SHARED ${CXX_SRCS} ${C_SRCS})\n\n";
} else {
content << "add_library(" << project->GetName() << " ${CXX_SRCS} ${C_SRCS})\n\n";
}
}
// Add link libraries
{
wxString libs = buildConf->GetLibraries();
libs.Trim().Trim(false);
if (!libs.IsEmpty()) {
libs.Replace(";", "\n ");
content << "target_link_libraries(" <<
project->GetName() << "\n " <<
libs << "\n)\n\n";
}
}
// And setup the deps
// Set up a dependecy between the this project and the subproject
if ( !depsProjectNames.IsEmpty() ) {
content << "# Setup dependencies\n";
}
for(size_t i=0; i<depsProjectNames.GetCount(); ++i) {
content << "add_dependencies( " << project->GetName() << " " << depsProjectNames.Item(i) << " )\n";
}
// Write result
WriteContent(filename, content);
return true;
}
/* ************************************************************************ */
|