以编程方式配置CDT项目

时间:2018-10-31 08:39:28

标签: eclipse-plugin eclipse-cdt

我用新的项目类型创建了一个Eclipse插件,创建该项目类型还添加了CDT的C / C ++性质。现在,我想为该项目类型提供默认配置,特别是我希望将其配置为使用CMake4Eclipse构建类型。

我需要设置:

  1. 当前工具链
  2. 当前生成器
  3. CMake主机操作系统覆盖
  4. 来源位置
  5. 环境

对于这两个方法,我都无法弄清楚如何以编程方式设置它们。我具有静态默认配置的解决方案是可以接受的。

1 个答案:

答案 0 :(得分:0)

第1步和第2步(设置工具链和构建器),我使用以下代码成功管理:

// convert to C/C++ project
    CCorePlugin ccore = CCorePlugin.getDefault();
    ccore.convertProjectToNewC(project, ManagedBuildManager.CFG_DATA_PROVIDER_ID, monitor);
    CCorePlugin.getDefault().convertProjectFromCtoCC(project, monitor);

// get toolchain and cmake builder
    IBuilder cmakeBuilder = ManagedBuildManager.getExtensionBuilder("de.marw.cdt.cmake.core.genscriptbuilder");
    IToolChain toolChain = ManagedBuildManager.getExtensionToolChain("cdt.managedbuild.toolchain.gnu.base");

// arcane incantations based on org.eclipse.cdt.managedbuilder.ui.wizards.NewMakeProjFromExisting.performFinish()
    ICProjectDescriptionManager pdMgr = CoreModel.getDefault().getProjectDescriptionManager();
    ICProjectDescription projDesc = pdMgr.createProjectDescription(project, false);
    ManagedBuildInfo info = ManagedBuildManager.createBuildInfo(project);
    ManagedProject mProj = new ManagedProject(projDesc);
    info.setManagedProject(mProj);

    CfgHolder cfgHolder = new CfgHolder(toolChain, null);
    IConfiguration config = new Configuration(mProj, (ToolChain) toolChain,
    ManagedBuildManager.calculateChildId(toolChain.getId(), null), cfgHolder.getName());
// set cmake builder
    config.changeBuilder(cmakeBuilder, ManagedBuildManager.calculateChildId(config.getId(), null),
    cmakeBuilder.getName());
// make sure makefile generation is enabled
    config.setManagedBuildOn(true);
    CConfigurationData data = config.getConfigurationData();
    projDesc.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID, data);

    pdMgr.setProjectDescription(project, projDesc);

第3步(cmake配置)无法立即使用,cmake4eclipse插件不会导出更改其设置所需的API。因此,我创建了一个片段,用于导入和导出必要的程序包。然后使用上面的代码进行配置很简单:

ICConfigurationDescription desc = projDesc.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID, data);
CMakePreferences cmakePref = ConfigurationManager.getInstance().getOrCreate(desc);
cmakePref.getWindowsPreferences().setGenerator(CmakeGenerator.NMakeMakefiles);
cmakePref.getDefines().add(new CmakeDefine("CMAKE_BUILD_TYPE",CmakeVariableType.STRING,"Release"));

第4步(设置源文件夹)如下:

  private void setAsSourceFolder(IResource folder, IProject project) throws CoreException {
    ICSourceEntry newEntry = new CSourceEntry(folder.getProjectRelativePath(), null, 0);
    ICProjectDescription des = CCorePlugin.getDefault().getProjectDescription(project, true);

    ICConfigurationDescription[] cfgs = des.getConfigurations();
    for (ICConfigurationDescription cfg : cfgs) {
      cfg.setSourceEntries(new ICSourceEntry[] { newEntry });
    }

    CCorePlugin.getDefault().setProjectDescription(project, des);
  }