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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : custombuildrequest.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 "asyncprocess.h"
#include "build_settings_config.h"
#include "buildmanager.h"
#include "cl_command_event.h"
#include "compiler.h"
#include "custombuildrequest.h"
#include "dirsaver.h"
#include "environmentconfig.h"
#include "event_notifier.h"
#include "globals.h"
#include "imanager.h"
#include "macros.h"
#include "plugin.h"
#include "workspace.h"
#include <wx/app.h>
#include <wx/ffile.h>
#include <wx/log.h>
#ifdef __WXMSW__
#define ECHO_CMD wxT("@echo ")
#define ECHO_OFF wxT("@echo off")
#define SILENCE_OP wxT("@")
#define SCRIPT_EXT wxT(".bat")
#else
#define ECHO_CMD wxT("echo ")
#define ECHO_OFF wxT("")
#define SILENCE_OP wxT("")
#define SCRIPT_EXT wxT(".sh")
#endif
CustomBuildRequest::CustomBuildRequest(const QueueCommand& buildInfo, const wxString& fileName)
: ShellCommand(buildInfo)
, m_fileName(fileName)
{
}
CustomBuildRequest::~CustomBuildRequest() {}
void CustomBuildRequest::Process(IManager* manager)
{
wxString cmd;
wxString errMsg;
wxStringMap_t om;
clCxxWorkspace* w(manager->GetWorkspace());
EnvironmentConfig* env(manager->GetEnv());
ProjectPtr proj = w->FindProjectByName(m_info.GetProject(), errMsg);
if(!proj) {
AppendLine(_("Cant find project: ") + m_info.GetProject());
return;
}
// Notify plugins that a compile process is going to start
clBuildEvent event(wxEVT_BUILD_STARTING);
event.SetProjectName(proj->GetName());
event.SetConfigurationName(m_info.GetConfiguration());
if(EventNotifier::Get()->ProcessEvent(event)) {
// the build is being handled by some plugin, no need to build it
// using the standard way
return;
}
SendStartMsg();
BuildConfigPtr bldConf = w->GetProjBuildConf(m_info.GetProject(), m_info.GetConfiguration());
if(!bldConf) {
wxLogMessage(wxString::Format(wxT("Failed to find build configuration for project '%s' and configuration '%s'"),
m_info.GetProject().c_str(), m_info.GetConfiguration().c_str()));
return;
}
// try the special targets first:
bool isClean(false);
if(m_info.GetCustomBuildTarget() == wxT("Build")) {
cmd = bldConf->GetCustomBuildCmd();
} else if(m_info.GetCustomBuildTarget() == wxT("Clean")) {
cmd = bldConf->GetCustomCleanCmd();
isClean = true;
} else if(m_info.GetCustomBuildTarget() == wxT("Rebuild")) {
cmd = bldConf->GetCustomRebuildCmd();
} else if(m_info.GetCustomBuildTarget() == wxT("Compile Single File")) {
cmd = bldConf->GetSingleFileBuildCommand();
} else if(m_info.GetCustomBuildTarget() == wxT("Preprocess File")) {
cmd = bldConf->GetPreprocessFileCommand();
}
// if still no luck, try with the other custom targets
if(cmd.IsEmpty()) {
std::map<wxString, wxString> targets = bldConf->GetCustomTargets();
std::map<wxString, wxString>::iterator iter = targets.find(m_info.GetCustomBuildTarget());
if(iter != targets.end()) {
cmd = iter->second;
}
}
if(cmd.IsEmpty()) {
// if we got an error string, use it
if(errMsg.IsEmpty() == false) {
AppendLine(errMsg);
} else {
AppendLine(_("Command line is empty. Build aborted."));
}
return;
}
// Working directory:
// By default we use the project path
//////////////////////////////////////////////////////
DirSaver ds;
// first set the path to the project working directory
::wxSetWorkingDirectory(proj->GetFileName().GetPath());
// If a working directory was specified, use it instead
wxString wd = bldConf->GetCustomBuildWorkingDir();
wd.Trim().Trim(false);
wxString filename = m_fileName;
if(filename.IsEmpty() && manager->GetActiveEditor()) {
filename = manager->GetActiveEditor()->GetFileName().GetFullPath();
}
if(wd.IsEmpty()) {
// use the project path
wd = proj->GetFileName().GetPath();
} else {
// expand macros from the working directory
wd = ExpandAllVariables(wd, clCxxWorkspaceST::Get(), proj->GetName(), bldConf->GetName(), filename);
}
{
// Ensure that the path to the working directory exist
wxFileName fnwd(wd, "");
if(!fnwd.DirExists()) {
fnwd.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
}
}
::wxSetWorkingDirectory(wd);
// Print message to the build tab
AppendLine(wxString::Format(_("MESSAGE: Entering directory `%s'\n"), wd.c_str()));
// Command handling:
//////////////////////////////////////////////////////
// expand the variables of the command
cmd = ExpandAllVariables(cmd, w, m_info.GetProject(), m_info.GetConfiguration(), filename);
// in case our configuration includes post/pre build commands
// we generate a makefile to include them as well and we update
// the build command
bool bCommandAltered = DoUpdateCommand(manager, cmd, proj, bldConf, isClean);
#ifdef __WXMSW__
// Windows CD command requires the paths to be backslashe
if(cmd.Find(wxT("cd ")) != wxNOT_FOUND) cmd.Replace(wxT("/"), wxT("\\"));
#endif
// Wrap the build command in the shell, so it will be able
// to perform 'chain' commands like
// cd SOMEWHERE && make && ...
// Dont wrap the command if it was altered previously
if(!bCommandAltered) {
WrapInShell(cmd);
}
// print the build command
AppendLine(cmd + wxT("\n"));
wxString configName(m_info.GetConfiguration());
// also, send another message to the main frame, indicating which project is being built
// and what configuration
wxString text;
if(isClean) {
text << wxGetTranslation(CLEAN_PROJECT_PREFIX);
} else {
text << wxGetTranslation(BUILD_PROJECT_PREFIX);
}
text << m_info.GetProject() << wxT(" - ") << configName << wxT(" ]----------\n");
AppendLine(text);
// Avoid Unicode chars coming from the compiler by setting LC_ALL to "C"
om["LC_ALL"] = "C";
EnvSetter environment(env, &om, proj->GetName(), m_info.GetConfiguration());
m_proc = CreateAsyncProcess(this, cmd);
if(!m_proc) {
wxString message;
message << _("Failed to start build process, command: ") << cmd << _(", process terminated with exit code: 0");
AppendLine(message);
return;
}
}
bool CustomBuildRequest::DoUpdateCommand(IManager* manager, wxString& cmd, ProjectPtr proj, BuildConfigPtr bldConf,
bool isClean)
{
BuildCommandList preBuildCmds, postBuildCmds;
wxArrayString pre, post;
bldConf->GetPreBuildCommands(preBuildCmds);
bldConf->GetPostBuildCommands(postBuildCmds);
// collect all enabled commands
BuildCommandList::iterator iter = preBuildCmds.begin();
for(; iter != preBuildCmds.end(); iter++) {
BuildCommand command = *iter;
if(command.GetEnabled()) {
pre.Add(command.GetCommand());
}
}
iter = postBuildCmds.begin();
for(; iter != postBuildCmds.end(); iter++) {
BuildCommand command = *iter;
if(command.GetEnabled()) {
post.Add(command.GetCommand());
}
}
if(pre.empty() && post.empty()) {
return false;
}
// we need to create a makefile which includes all the pre-build, the actual build command and the post-build
// commands
// (in this exact order).
wxString script;
script << ECHO_OFF << wxT("\n");
if(pre.IsEmpty() == false && !isClean) {
script << ECHO_CMD << wxT("Executing Pre Build commands ...\n");
for(size_t i = 0; i < pre.GetCount(); i++) {
script << SILENCE_OP << pre.Item(i) << wxT("\n");
}
script << ECHO_CMD << wxT("Done\n");
}
// add the command
script << cmd << wxT("\n");
if(post.IsEmpty() == false && !isClean) {
script << ECHO_CMD << wxT("Executing Post Build commands ...\n");
for(size_t i = 0; i < post.GetCount(); i++) {
script << SILENCE_OP << post.Item(i) << wxT("\n");
}
script << ECHO_CMD << wxT("Done\n");
}
// write the makefile
wxFFile output;
wxString fn;
fn << proj->GetName() << SCRIPT_EXT;
output.Open(fn, wxT("w+"));
if(output.IsOpened()) {
output.Write(script);
output.Close();
}
cmd.Clear();
#ifdef __WXMSW__
cmd << wxT("\"") << fn << wxT("\"");
#else
// *nix
cmd << wxT("/bin/sh './") << fn << wxT("'");
#endif
return true;
}
|