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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 Eran Ifrah
// 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 "CMakeBuilder.h"
#include "fileextmanager.h"
#include "fileutils.h"
#include <algorithm>
// wxWidgets
#include <wx/ffile.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
#include <wx/tokenzr.h>
// Plugin
#include "CMakePlugin.h"
#include "globals.h"
#define CODELITE_CMAKE_PREFIX "# -*- CMakeLists.txt generated by CodeLite IDE. Do not edit by hand -*-"
#define CMAKELISTS_USER_CODE_01_PREFIX "#{{{{ User Code 01"
#define CMAKELISTS_USER_CODE_02_PREFIX "#{{{{ User Code 02"
#define CMAKELISTS_USER_CODE_1_PREFIX "#{{{{ User Code 1"
#define CMAKELISTS_USER_CODE_2_PREFIX "#{{{{ User Code 2"
#define CMAKELISTS_USER_CODE_3_PREFIX "#{{{{ User Code 3"
#define CMAKELISTS_USER_CODE_END "#}}}}"
namespace
{
wxArrayString wxArrayUniqueMerge(const wxArrayString& arr1, const wxArrayString& arr2)
{
wxArrayString outArr;
for(size_t i = 0; i < arr1.size(); ++i) {
if(outArr.Index(arr1.Item(i)) == wxNOT_FOUND) {
outArr.Add(arr1.Item(i));
}
}
for(size_t i = 0; i < arr2.size(); ++i) {
if(outArr.Index(arr2.Item(i)) == wxNOT_FOUND) {
outArr.Add(arr2.Item(i));
}
}
return outArr;
}
} // namespace
CMakeGenerator::CMakeGenerator()
: m_counter(0)
{
}
CMakeGenerator::~CMakeGenerator() {}
bool CMakeGenerator::CheckExists(const wxFileName& filename)
{
// Output file exists, overwrite?
if(filename.Exists()) {
// Check to see if this file was generated by CodeLite
wxString content;
FileUtils::ReadFileContent(filename, content);
if(!content.StartsWith(CODELITE_CMAKE_PREFIX)) {
wxString msg;
msg << _("A custom ") << CMakePlugin::CMAKELISTS_FILE << _(" exists.\nWould you like to overwrite it?\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);
} else {
// A CodeLite generated CMakeLists.txt file
// Read any user custom code before we overwrite the content
ReadUserCode(content);
return true;
}
}
// File doesn't exists, write it
return true;
}
/**
* @brief The Generator main entry point
*/
bool CMakeGenerator::Generate(ProjectPtr p)
{
m_counter = 0;
CHECK_COND_RET_FALSE(p);
BuildConfigPtr buildConf = p->GetBuildConfiguration();
CHECK_COND_RET_FALSE(buildConf);
clCxxWorkspace* workspace = clCxxWorkspaceST::Get();
// Generate CMakeLists.txt file for each dependency project
wxArrayString projectsArr = p->GetDependencies(buildConf->GetName());
for(size_t i = 0; i < projectsArr.GetCount(); ++i) {
ProjectPtr pProj = workspace->GetProject(projectsArr.Item(i));
CMakeGenerator generator;
wxString cmakeFileContent = generator.GenerateProject(pProj, false);
if(!cmakeFileContent.IsEmpty()) {
// Write the content to the file system
wxFileName cmakelists(pProj->GetFileName());
cmakelists.SetFullName(CMakePlugin::CMAKELISTS_FILE);
FileUtils::WriteFileContent(cmakelists, cmakeFileContent);
}
}
// Generate the top level CMakeLists.txt
wxString topProjectContent = GenerateProject(p, true);
// ====--------------------------------
// Now generate the top level project
// ====--------------------------------
// Output file name
const wxFileName filename(p->GetFileName().GetPath(), CMakePlugin::CMAKELISTS_FILE);
// Check for file existance and read any user code from the current CMakeLists.txt file
if(!CheckExists(filename))
return false;
// File content
wxString mainProjectContent;
// Print project name
mainProjectContent << CODELITE_CMAKE_PREFIX << "\n\n";
mainProjectContent << "cmake_minimum_required(VERSION 3.5)\n";
AddUserCodeSection(mainProjectContent, CMAKELISTS_USER_CODE_01_PREFIX, m_userBlock01);
mainProjectContent << "enable_language(CXX C ASM)\n";
mainProjectContent << "# Project name\n";
mainProjectContent << "project(" << p->GetName() << ")\n\n";
AddUserCodeSection(mainProjectContent, CMAKELISTS_USER_CODE_02_PREFIX, m_userBlock02);
mainProjectContent << "# This setting is useful for providing JSON file used by CodeLite for code completion\n";
mainProjectContent << "set(CMAKE_EXPORT_COMPILE_COMMANDS 1)\n\n";
// Export common variables into CMake variables
{
mainProjectContent << "set(CONFIGURATION_NAME \"" << workspace->GetBuildMatrix()->GetSelectedConfigurationName()
<< "\")\n";
mainProjectContent << "\n";
}
wxFileName fn = workspace->GetFileName();
fn.MakeRelativeTo(p->GetFileName().GetPath());
wxString workspacePath = fn.GetPath(false, wxPATH_UNIX);
if(workspacePath.IsEmpty()) {
workspacePath = ".";
}
mainProjectContent << "set(CL_WORKSPACE_DIRECTORY " << workspacePath << ")\n";
// Set the output directory. We want a common location for all projects
// so we set it under cmake-build-Debug/bin
wxString outputDir;
outputDir << "${CMAKE_CURRENT_LIST_DIR}/${CL_WORKSPACE_DIRECTORY}/"
<< "cmake-build-${CONFIGURATION_NAME}/output";
mainProjectContent << "# Set default locations\n";
mainProjectContent << "set(CL_OUTPUT_DIRECTORY " << outputDir << ")\n";
mainProjectContent << "set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CL_OUTPUT_DIRECTORY})\n";
mainProjectContent << "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CL_OUTPUT_DIRECTORY})\n";
mainProjectContent << "set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CL_OUTPUT_DIRECTORY})\n";
mainProjectContent << "\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, '=');
if(pair.IsEmpty()) {
continue;
}
const wxString& name = pair[0];
const wxString value = (pair.GetCount() >= 2) ? pair[1] : "";
// Set environment variable
mainProjectContent << "set(" << name << " \"" << value << "\")\n";
}
mainProjectContent << "\n";
}
}
mainProjectContent << "# Projects\n";
// the workspace and the active project "sits" on the same folder
// merge the two cmakelists.txt files
mainProjectContent << "\n\n";
mainProjectContent << "# Top project\n";
mainProjectContent << topProjectContent;
// Write the workspace
wxFileName fnMainCMakeLists(p->GetFileName());
fnMainCMakeLists.SetFullName(CMakePlugin::CMAKELISTS_FILE);
FileUtils::WriteFileContent(fnMainCMakeLists, mainProjectContent);
return true;
}
/* ************************************************************************ */
wxString CMakeGenerator::GenerateProject(ProjectPtr project, bool topProject, const wxString& configName)
{
wxASSERT(project);
wxString projectPathVariable; // PROJECT_<name>_PATH
wxString projectPathVariableValue; // ${PROJECT_<name>_PATH}
wxString workspacePath;
projectPathVariable << "PROJECT_" << project->GetName() << "_PATH";
projectPathVariableValue << "${PROJECT_" << project->GetName() << "_PATH}";
wxFileName fnWorkspace = clCxxWorkspaceST::Get()->GetFileName();
fnWorkspace.MakeRelativeTo(project->GetFileName().GetPath());
workspacePath << "${CMAKE_CURRENT_LIST_DIR}/" << fnWorkspace.GetPath(wxPATH_NO_SEPARATOR, wxPATH_UNIX);
// Sources are kept relative in the project
// we need to fix this
const Project::FilesMap_t& allFiles = project->GetFiles();
// Get the project build configuration and compiler
BuildConfigPtr buildConf = project->GetBuildConfiguration(configName);
if(!buildConf)
return "";
wxArrayString cppSources, cSources, resourceFiles, asmSources;
cppSources.reserve(allFiles.size());
cSources.reserve(allFiles.size());
resourceFiles.reserve(allFiles.size());
asmSources.reserve(allFiles.size());
std::for_each(allFiles.begin(), allFiles.end(), [&](const Project::FilesMap_t::value_type& vt) {
clProjectFile::Ptr_t file = vt.second;
if(!file->IsExcludeFromConfiguration(buildConf->GetName())) {
wxFileName fn(file->GetFilename());
// Append each file with the "${CMAKE_CURRENT_LIST_DIR}/" prefix
wxString file_name;
file_name << "${CMAKE_CURRENT_LIST_DIR}/" << file->GetFilenameRelpath();
file_name.Replace("\\", "/");
wxString fullname = fn.GetFullName();
if(FileExtManager::GetType(fullname) == FileExtManager::TypeSourceC) {
cSources.Add(file_name);
} else if(FileExtManager::GetType(fullname) == FileExtManager::TypeSourceCpp) {
cppSources.Add(file_name);
} else if(FileExtManager::GetType(fullname) == FileExtManager::TypeResource) {
resourceFiles.Add(file_name);
} else if(FileExtManager::GetType(fullname) == FileExtManager::TypeAsm) {
asmSources.Add(file_name);
}
}
});
// Get project directory
wxString projectDir = project->GetFileName().GetPath();
// Sanity
if(!buildConf || buildConf->IsCustomBuild()) {
// no build config or custom build?
return "";
}
CompilerPtr compiler = buildConf->GetCompiler();
if(!compiler) {
return "";
}
wxFileName projectPath = project->GetFileName();
// Output file name
wxFileName filename(projectDir, CMakePlugin::CMAKELISTS_FILE);
if(!CheckExists(filename))
return "";
// File content
wxString content;
if(!topProject) {
content << Prefix(project);
}
if(topProject) {
// Add the dependencies
wxArrayString projectsArr = project->GetDependencies(buildConf->GetName());
if(!projectsArr.IsEmpty()) {
content << "## Add the dependencies\n";
}
for(size_t i = 0; i < projectsArr.GetCount(); ++i) {
ProjectPtr pProj = clCxxWorkspaceST::Get()->GetProject(projectsArr.Item(i));
wxFileName fnProject = pProj->GetFileName();
fnProject.MakeRelativeTo(project->GetFileName().GetPath());
wxString path = fnProject.GetPath();
path.Prepend("${CMAKE_SOURCE_DIR}/");
path.Replace("\\", "/");
content << "add_subdirectory(" << path << " ${CMAKE_CURRENT_BINARY_DIR}/" << pProj->GetName()
<< "-build)\n";
}
}
content << "# Define some variables\n";
content << "set(" << projectPathVariable << " \"${CMAKE_CURRENT_LIST_DIR}\")\n";
content << "set(WORKSPACE_PATH \"" << workspacePath << "\")\n";
content << "\n";
// Add the first hook here
AddUserCodeSection(content, CMAKELISTS_USER_CODE_1_PREFIX, m_userBlock1);
// Add include directories
{
wxArrayString includes = ::wxStringTokenize(buildConf->GetIncludePath(), ";", wxTOKEN_STRTOK);
content << "include_directories(\n";
for(size_t i = 0; i < includes.GetCount(); ++i) {
wxString& includePath = includes.Item(i);
includePath.Replace("\\", "/");
includePath.Trim(false).Trim();
// Replace standard macros with CMake variables
includePath.Replace("$(WorkspacePath)", "${WORKSPACE_PATH}");
includePath.Replace("$(WorkspaceConfiguration)", "${CONFIGURATION_NAME}");
includePath.Replace("$(ProjectPath)", projectPathVariableValue);
if(includePath.IsEmpty()) {
continue;
}
content << " " << includes.Item(i) << "\n";
}
content << "\n)\n\n";
}
// Add custom command from the compiler options
{
wxArrayString c_optsNames, c_opts, cxx_optNames, cxx_opts;
content << "\n# Compiler options\n";
wxString cxxCmpOptions = buildConf->GetCompileOptions();
ExpandOptions(cxxCmpOptions, content, cxx_optNames, cxx_opts);
wxString cCmpOptions = buildConf->GetCCompileOptions();
ExpandOptions(cCmpOptions, content, c_optsNames, c_opts);
// Merge the two
wxArrayString opts = wxArrayUniqueMerge(cxx_opts, c_opts);
wxArrayString optsNames = wxArrayUniqueMerge(cxx_optNames, c_optsNames);
for(size_t i = 0; i < optsNames.size(); ++i) {
content << "add_definitions(${" << optsNames.Item(i) << "})\n";
}
for(size_t i = 0; i < opts.size(); ++i) {
content << "add_definitions(" << opts.Item(i) << ")\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
{
wxArrayString optsNames, opts;
wxString linkOptions = buildConf->GetLinkOptions();
content << "\n# Linker options\n";
ExpandOptions(linkOptions, content, optsNames, opts);
bool first = true;
for(size_t i = 0; i < optsNames.size(); ++i) {
content << "set(LINK_OPTIONS " << (first ? "" : "${LINK_OPTIONS} ") << "${" << optsNames.Item(i) << "})\n";
first = false;
}
for(size_t i = 0; i < opts.size(); ++i) {
content << "set(LINK_OPTIONS " << (first ? "" : "${LINK_OPTIONS} ") << opts.Item(i) << ")\n";
first = false;
}
content << "\n";
}
{
wxArrayString optsNames, opts;
wxString rcOptions = buildConf->GetResCompileOptions();
content << "\nif(WIN32)\n";
content << " # Resource options\n";
ExpandOptions(rcOptions, content, optsNames, opts, " ");
bool first = true;
for(size_t i = 0; i < optsNames.size(); ++i) {
content << " set(RC_OPTIONS " << (first ? "" : "${RC_OPTIONS} ") << "${" << optsNames.Item(i) << "})\n";
first = false;
}
for(size_t i = 0; i < opts.size(); ++i) {
content << " set(RC_OPTIONS " << (first ? "" : "${RC_OPTIONS} ") << opts.Item(i) << ")\n";
first = false;
}
content << "endif(WIN32)\n";
content << "\n";
// Resource options
}
// Add libraries paths
{
wxString lib_paths = buildConf->GetLibPath();
// Get switch from compiler
if(compiler) {
// Append global library paths
lib_paths << ";" << compiler->GetGlobalLibPath();
}
// to replace all instances of backslashes with forward slashes issue #1073
lib_paths.Replace("\\", "/");
lib_paths.Trim(false).Trim();
// 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) {
wxString libPath = lib_paths_list.Item(i);
// Replace standard macros with CMake variables
libPath.Replace("$(WorkspacePath)", "${WORKSPACE_PATH}");
libPath.Replace("$(WorkspaceConfiguration)", "${CONFIGURATION_NAME}");
libPath.Replace("$(ProjectPath)", projectPathVariableValue);
::WrapWithQuotes(libPath);
lib_paths << " " << libPath << "\n";
}
content << "# Library path\n";
content << "link_directories(\n" << lib_paths << ")\n\n";
}
// Write sources
{
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";
}
if(!resourceFiles.IsEmpty()) {
content << "if(WIN32)\n";
content << " # Define the resource files\n";
content << " set ( RC_SRCS\n";
for(size_t i = 0; i < resourceFiles.GetCount(); ++i) {
content << " " << resourceFiles.Item(i) << "\n";
}
content << " )\n";
content << "endif(WIN32)\n\n";
}
if(!asmSources.IsEmpty()) {
content << "# Define the ASM sources\n";
content << "set ( ASM_SRCS\n";
for(size_t i = 0; i < asmSources.GetCount(); ++i) {
content << " " << asmSources.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";
}
}
{
content << "if(WIN32)\n"
<< " enable_language(RC)\n"
<< " set(CMAKE_RC_COMPILE_OBJECT\n"
<< " \"<CMAKE_RC_COMPILER> ${RC_OPTIONS} -O coff -i <SOURCE> -o <OBJECT>\")\n"
<< "endif(WIN32)\n\n";
}
// Add the second hook here
AddUserCodeSection(content, CMAKELISTS_USER_CODE_2_PREFIX, m_userBlock2);
// Get project type
{
wxString type = buildConf->GetProjectType();
if(type == PROJECT_TYPE_EXECUTABLE) {
content << "add_executable(" << project->GetName() << " ${RC_SRCS} ${CXX_SRCS} ${C_SRCS} ${ASM_SRCS})\n";
content << "target_link_libraries(" << project->GetName() << " ${LINK_OPTIONS})\n\n";
} else if(type == PROJECT_TYPE_DYNAMIC_LIBRARY) {
content << "add_library(" << project->GetName()
<< " SHARED ${RC_SRCS} ${CXX_SRCS} ${C_SRCS} ${ASM_SRCS})\n";
content << "target_link_libraries(" << project->GetName() << " ${LINK_OPTIONS})\n\n";
} else {
content << "add_library(" << project->GetName() << " ${RC_SRCS} ${CXX_SRCS} ${C_SRCS} ${ASM_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";
}
}
// set the dependecies for the top level project
if(topProject) {
wxArrayString projects = project->GetDependencies(buildConf->GetName());
if(!projects.IsEmpty()) {
content << "\n";
content << "# Adding dependencies\n";
for(size_t i = 0; i < projects.GetCount(); ++i) {
content << "add_dependencies(" << project->GetName() << " " << projects.Item(i) << ")\n";
}
}
}
// Add pre|post build commands
{
BuildCommandList commands;
buildConf->GetPreBuildCommands(commands);
AddBuildCommands("PRE_BUILD", commands, project, content);
}
{
BuildCommandList commands;
buildConf->GetPostBuildCommands(commands);
AddBuildCommands("POST_BUILD", commands, project, content);
}
// Add the last hook here
AddUserCodeSection(content, CMAKELISTS_USER_CODE_3_PREFIX, m_userBlock3);
// Write result
return content;
}
void CMakeGenerator::AddBuildCommands(const wxString& buildType, const BuildCommandList& commands, ProjectPtr project,
wxString& content)
{
if(!commands.empty()) {
wxString projectPathVariableValue;
projectPathVariableValue << "${PROJECT_" << project->GetName() << "_PATH}";
content << "\n# Adding " << buildType << " commands\n";
std::for_each(commands.begin(), commands.end(), [&](const BuildCommand& buildCommand) {
if(buildCommand.GetEnabled()) {
wxString cmd = buildCommand.GetCommand();
cmd.Replace("$(WorkspacePath)", "${WORKSPACE_PATH}");
cmd.Replace("$(ProjectPath)", projectPathVariableValue);
content << "add_custom_command(\n"
<< " TARGET " << project->GetName() << "\n"
<< " " << buildType << "\n"
<< " COMMAND " << cmd << ")\n";
}
});
content << "\n";
}
}
bool CMakeGenerator::CanGenerate(ProjectPtr project)
{
clCxxWorkspace* workspace = clCxxWorkspaceST::Get();
wxFileName workspaceFile = workspace->GetFileName();
std::vector<wxFileName> cmakelistsArr;
cmakelistsArr.push_back(wxFileName(workspaceFile.GetPath(), CMakePlugin::CMAKELISTS_FILE));
cmakelistsArr.push_back(wxFileName(project->GetFileName().GetPath(), CMakePlugin::CMAKELISTS_FILE));
BuildConfigPtr buildConf = project->GetBuildConfiguration();
CHECK_COND_RET_FALSE(buildConf);
// Add the projects
wxArrayString projects = project->GetDependencies(buildConf->GetName());
for(size_t i = 0; i < projects.size(); ++i) {
ProjectPtr p = workspace->GetProject(projects.Item(i));
if(p) {
cmakelistsArr.push_back(wxFileName(p->GetFileName().GetPath(), CMakePlugin::CMAKELISTS_FILE));
}
}
// Read each of the CMakeLists.txt file in the array, and check if we can find our top level comment
for(size_t i = 0; i < cmakelistsArr.size(); ++i) {
const wxFileName& fn = cmakelistsArr.at(i);
wxString content;
if(fn.Exists() && FileUtils::ReadFileContent(fn, content)) {
if(!content.StartsWith(CODELITE_CMAKE_PREFIX)) {
return false;
}
}
}
return true;
}
void CMakeGenerator::ExpandOptions(const wxString& options, wxString& content, wxArrayString& arrVars,
wxArrayString& arrOut, const wxString& indent)
{
arrVars.clear();
arrOut.clear();
wxArrayString arrOptions = ::wxStringTokenize(options, ";", wxTOKEN_STRTOK);
for(size_t i = 0; i < arrOptions.size(); ++i) {
wxString cmpOption(arrOptions.Item(i));
cmpOption.Trim().Trim(false);
wxString tmp;
// Expand backticks / $(shell ...) syntax supported by CodeLite
if(cmpOption.StartsWith(wxT("$(shell "), &tmp) || cmpOption.StartsWith(wxT("`"), &tmp)) {
cmpOption.swap(tmp);
tmp.Clear();
if(cmpOption.EndsWith(wxT(")"), &tmp) || cmpOption.EndsWith(wxT("`"), &tmp)) {
cmpOption.swap(tmp);
}
wxString varname;
varname << "CL_VAR_" << (++m_counter);
// Create a CMake command that executes this command
content << indent << "execute_process(COMMAND \n"
<< indent << " " << cmpOption << "\n"
<< indent << " "
<< "OUTPUT_VARIABLE\n"
<< indent << " "
<< "CL_TMP_VAR\n"
<< indent << " "
<< "OUTPUT_STRIP_TRAILING_WHITESPACE)\n";
content << indent << "string(STRIP ${CL_TMP_VAR} " << varname << ")\n";
arrVars.Add(varname);
} else {
arrOut.Add(cmpOption); // Keep as is
}
}
}
wxString CMakeGenerator::Prefix(ProjectPtr project)
{
// File content
wxString content;
content << CODELITE_CMAKE_PREFIX << "\n\n";
content << "cmake_minimum_required(VERSION 3.5)\n";
AddUserCodeSection(content, CMAKELISTS_USER_CODE_01_PREFIX, m_userBlock01);
content << "enable_language(CXX C ASM)\n\n";
// Print project name
content << "project(" << project->GetName() << ")\n\n";
AddUserCodeSection(content, CMAKELISTS_USER_CODE_02_PREFIX, m_userBlock02);
return content;
}
bool CMakeGenerator::IsCustomCMakeLists(const wxFileName& fn)
{
if(fn.FileExists()) {
wxFileName fn;
wxString content;
if(FileUtils::ReadFileContent(fn, content)) {
return !content.StartsWith(CODELITE_CMAKE_PREFIX);
}
}
return false;
}
void CMakeGenerator::ReadUserCode(const wxString& content)
{
m_userBlock01.clear();
m_userBlock02.clear();
m_userBlock1.clear();
m_userBlock2.clear();
m_userBlock3.clear();
wxArrayString lines = ::wxStringTokenize(content, "\n", wxTOKEN_RET_DELIMS);
while(!lines.IsEmpty()) {
const wxString& curline = lines.Item(0);
lines.RemoveAt(0);
if(curline.StartsWith(CMAKELISTS_USER_CODE_1_PREFIX)) {
ReadUntilEndOfUserBlock(lines, m_userBlock1);
} else if(curline.StartsWith(CMAKELISTS_USER_CODE_2_PREFIX)) {
ReadUntilEndOfUserBlock(lines, m_userBlock2);
} else if(curline.StartsWith(CMAKELISTS_USER_CODE_3_PREFIX)) {
ReadUntilEndOfUserBlock(lines, m_userBlock3);
} else if(curline.StartsWith(CMAKELISTS_USER_CODE_01_PREFIX)) {
ReadUntilEndOfUserBlock(lines, m_userBlock01);
} else if(curline.StartsWith(CMAKELISTS_USER_CODE_02_PREFIX)) {
ReadUntilEndOfUserBlock(lines, m_userBlock02);
}
}
}
void CMakeGenerator::ReadUntilEndOfUserBlock(wxArrayString& lines, wxString& content)
{
bool removeLf = false;
while(!lines.IsEmpty()) {
const wxString& curline = lines.Item(0);
lines.RemoveAt(0);
if(curline.StartsWith(CMAKELISTS_USER_CODE_END)) {
return;
} else {
content << curline;
// removeLf = true;
}
}
if(removeLf) {
content.RemoveLast();
}
}
void CMakeGenerator::AddUserCodeSection(wxString& content, const wxString& sectionPrefix, const wxString& sectionCode)
{
content << "\n";
content << "\n";
content << sectionPrefix << "\n";
if(sectionCode.IsEmpty()) {
content << "# Place your code here"
<< "\n";
} else {
content << sectionCode;
}
content << CMAKELISTS_USER_CODE_END;
content << "\n\n";
}
|