void ShellCommand::DoSetWorkingDirectory(ProjectPtr proj, bool isCustom, bool isFileOnly) { //when using custom build, user can select different working directory if (proj) { if (isCustom) { //first set the path to the project working directory ::wxSetWorkingDirectory(proj->GetFileName().GetPath()); BuildConfigPtr buildConf = WorkspaceST::Get()->GetProjBuildConf(m_info.GetProject(), m_info.GetConfiguration()); if (buildConf) { wxString wd = buildConf->GetCustomBuildWorkingDir(); if (wd.IsEmpty()) { // use the project path wd = proj->GetFileName().GetPath(); } else { // expand macros from path wd = ExpandAllVariables(wd, WorkspaceST::Get(), proj->GetName(), buildConf->GetName(), wxEmptyString); } ::wxSetWorkingDirectory(wd); } } else { if(m_info.GetProjectOnly() || isFileOnly) { //first set the path to the project working directory ::wxSetWorkingDirectory(proj->GetFileName().GetPath()); } } } }
void PSCustomBuildPage::Load(BuildConfigPtr buildConf) { m_checkEnableCustomBuild->SetValue(buildConf->IsCustomBuild()); m_textCtrlCustomBuildWD->SetValue(buildConf->GetCustomBuildWorkingDir()); m_dvListCtrlTargets->DeleteAllItems(); wxVector<wxVariant> cols; cols.clear(); cols.push_back(ProjectCustomBuildTragetDlg::CUSTOM_TARGET_BUILD); cols.push_back(buildConf->GetCustomBuildCmd()); m_dvListCtrlTargets->AppendItem(cols); cols.clear(); cols.push_back(ProjectCustomBuildTragetDlg::CUSTOM_TARGET_CLEAN); cols.push_back(buildConf->GetCustomCleanCmd()); m_dvListCtrlTargets->AppendItem(cols); cols.clear(); cols.push_back(ProjectCustomBuildTragetDlg::CUSTOM_TARGET_REBUILD); cols.push_back(buildConf->GetCustomRebuildCmd()); m_dvListCtrlTargets->AppendItem(cols); cols.clear(); cols.push_back(ProjectCustomBuildTragetDlg::CUSTOM_TARGET_COMPILE_SINGLE_FILE); cols.push_back(buildConf->GetSingleFileBuildCommand()); m_dvListCtrlTargets->AppendItem(cols); cols.clear(); cols.push_back(ProjectCustomBuildTragetDlg::CUSTOM_TARGET_PREPROCESS_FILE); cols.push_back(buildConf->GetPreprocessFileCommand()); m_dvListCtrlTargets->AppendItem(cols); // Initialize the custom build targets std::map<wxString, wxString> targets = buildConf->GetCustomTargets(); std::map<wxString, wxString>::iterator titer = targets.begin(); for(; titer != targets.end(); ++titer) { if(ProjectCustomBuildTragetDlg::IsPredefinedTarget(titer->first)) continue; cols.clear(); cols.push_back(titer->first); cols.push_back(titer->second); m_dvListCtrlTargets->AppendItem(cols); } m_dlg->SetCustomBuildEnabled(m_checkEnableCustomBuild->IsChecked()); }
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; if(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_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; } }
wxString MacroManager::DoExpand( const wxString& expression, IManager* manager, const wxString& project, bool applyEnv, const wxString& confToBuild) { wxString expandedString(expression); clCxxWorkspace* workspace = clCxxWorkspaceST::Get(); if(!manager) { manager = clGetManager(); } size_t retries = 0; wxString dummyname, dummfullname; while((retries < 5) && FindVariable(expandedString, dummyname, dummyname)) { ++retries; DollarEscaper de(expandedString); if(workspace) { expandedString.Replace(wxT("$(WorkspaceName)"), workspace->GetName()); ProjectPtr proj = workspace->GetProject(project); if(proj) { wxString prjBuildWd; wxString prjRunWd; wxString project_name(proj->GetName()); // make sure that the project name does not contain any spaces project_name.Replace(wxT(" "), wxT("_")); BuildConfigPtr bldConf = workspace->GetProjBuildConf(proj->GetName(), confToBuild); if(bldConf) { bool isCustom = bldConf->IsCustomBuild(); expandedString.Replace(wxT("$(ProjectOutputFile)"), bldConf->GetOutputFileName()); // An alias expandedString.Replace(wxT("$(OutputFile)"), bldConf->GetOutputFileName()); // When custom build project, use the working directory set in the // custom build tab, otherwise use the project file's path prjBuildWd = isCustom ? bldConf->GetCustomBuildWorkingDir() : proj->GetFileName().GetPath(); prjRunWd = bldConf->GetWorkingDirectory(); } expandedString.Replace(wxT("$(ProjectWorkingDirectory)"), prjBuildWd); expandedString.Replace(wxT("$(ProjectRunWorkingDirectory)"), prjRunWd); expandedString.Replace(wxT("$(ProjectPath)"), proj->GetFileName().GetPath()); expandedString.Replace(wxT("$(WorkspacePath)"), workspace->GetWorkspaceFileName().GetPath()); expandedString.Replace(wxT("$(ProjectName)"), project_name); if(bldConf) { expandedString.Replace(wxT("$(IntermediateDirectory)"), bldConf->GetIntermediateDirectory()); expandedString.Replace(wxT("$(ConfigurationName)"), bldConf->GetName()); expandedString.Replace(wxT("$(OutDir)"), bldConf->GetIntermediateDirectory()); } if(expandedString.Find(wxT("$(ProjectFiles)")) != wxNOT_FOUND) expandedString.Replace(wxT("$(ProjectFiles)"), proj->GetFiles()); if(expandedString.Find(wxT("$(ProjectFilesAbs)")) != wxNOT_FOUND) expandedString.Replace(wxT("$(ProjectFilesAbs)"), proj->GetFiles(true)); } } if(manager) { IEditor* editor = manager->GetActiveEditor(); if(editor) { wxFileName fn(editor->GetFileName()); expandedString.Replace(wxT("$(CurrentFileName)"), fn.GetName()); wxString fpath(fn.GetPath()); fpath.Replace(wxT("\\"), wxT("/")); expandedString.Replace(wxT("$(CurrentFilePath)"), fpath); expandedString.Replace(wxT("$(CurrentFileExt)"), fn.GetExt()); expandedString.Replace(wxT("$(CurrentFileFullName)"), fn.GetFullName()); wxString ffullpath(fn.GetFullPath()); ffullpath.Replace(wxT("\\"), wxT("/")); expandedString.Replace(wxT("$(CurrentFileFullPath)"), ffullpath); expandedString.Replace(wxT("$(CurrentSelection)"), editor->GetSelection()); if(expandedString.Find(wxT("$(CurrentSelectionRange)")) != wxNOT_FOUND) { int start = editor->GetSelectionStart(), end = editor->GetSelectionEnd(); wxString output = wxString::Format(wxT("%i:%i"), start, end); expandedString.Replace(wxT("$(CurrentSelectionRange)"), output); } } } // exapand common macros wxDateTime now = wxDateTime::Now(); expandedString.Replace(wxT("$(User)"), wxGetUserName()); expandedString.Replace(wxT("$(Date)"), now.FormatDate()); if(manager && applyEnv) { expandedString.Replace(wxT("$(CodeLitePath)"), manager->GetInstallDirectory()); // Apply the environment and expand the variables EnvSetter es(NULL, NULL, project, confToBuild); expandedString = manager->GetEnv()->ExpandVariables(expandedString, false); } } return expandedString; }