[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] How to set scanner profile discovery command programmatically

I'm trying to set scanner profile discovery command programmatically to something other than gcc, but couldn't get my setting to stick. Here is the code I'm using:

public class MyWizardHandler extends MBSWizardHandler {
  private static final String BUILD_INFO_PROVIDER_ID = "specsFile";  //$NON-NLS-1$
  private static final String BUILD_COMMAND = "make";  //$NON-NLS-1$
  private static final String BUILD_ARGUMENTS = "";  //$NON-NLS-1$
  private static final String BUILD_TARGET = "all";  //$NON-NLS-1$
  private static final String BUILD_TARGET_CLEAN = "clean";  //$NON-NLS-1$
  private static final String GCC_PATH = "my_gcc";  //$NON-NLS-1$

  private static final String[] BINARY_PARSERS = new String[] {
    "org.eclipse.cdt.core.GNU_ELF"  //$NON-NLS-1$
  };
  private static final String[] ERROR_PARSERS = new String[] {
    "org.eclipse.cdt.core.MakeErrorParser",  //$NON-NLS-1$
    "org.eclipse.cdt.core.GCCErrorParser",  //$NON-NLS-1$
    "org.eclipse.cdt.core.GASErrorParser",  //$NON-NLS-1$
    "org.eclipse.cdt.core.GLDErrorParser"  //$NON-NLS-1$
  };
  private static final String ROOT_VARIABLE = RootVariableManager.ROOT_VARIABLE;

  private MyCppWizardPage myCppPage;

  public MyWizardHandler(Composite parent, IWizard wizard) {
    super(Messages.MyCppWizard_name, parent, wizard);
  }

  @Override
  public IWizardPage getSpecificPage() {
    if (myCppPage == null) {
      myCppPage = new MyCppWizardPage();
      myCppPage.setWizard(getWizard());
    }
    return myCppPage;
  }

  @Override
  public boolean canFinish() {
    return getSpecificPage().isPageComplete();
  }

  @Override
  public void addTc(IToolChain toolchain) {
    // Don't accept any toolchains except the default one.
    if (toolchain == null) {
      full_tcs.put(Messages.MyCppWizard_crosstool_toolchain, null);
    }
  }

  @Override
  public void createProject(IProject project, boolean defaults, boolean onFinish)
      throws CoreException {
    IPath rootPath = myCppPage.getRootPath();
    ICProjectDescriptionManager manager =
        CoreModel.getDefault().getProjectDescriptionManager();
    ICProjectDescription description =
        manager.createProjectDescription(project, false, !onFinish);
    ManagedBuildInfo info = ManagedBuildManager.createBuildInfo(project);
    ManagedProject managedProj = new ManagedProject(description);
    info.setManagedProject(managedProj);

    cfgs = getConfigPage().getCfgItems(defaults);
    ICLanguageSettingEntry[] includeEntries = getIncludeEntries();

    for (CfgHolder cfgHolder : cfgs) {
      ToolChain toolchain = (ToolChain) cfgHolder.getToolChain();
      String s = toolchain == null ? "0" : toolchain.getId();  //$NON-NLS-1$
      Configuration cfg = new Configuration(managedProj, toolchain,
          ManagedBuildManager.calculateChildId(s, null), cfgHolder.getName());
      cfg.setArtifactName(removeSpaces(project.getName()));

      // Scanner configuration discovery.
      ICfgScannerConfigBuilderInfo2Set cbi =
          CfgScannerConfigProfileManager.getCfgScannerConfigBuildInfo(cfg);
      Map<CfgInfoContext, IScannerConfigBuilderInfo2> infoMap = cbi.getInfoMap();
      for (CfgInfoContext infoContext : infoMap.keySet()) {
        IScannerConfigBuilderInfo2 buildInfo = cbi.getInfo(infoContext);
        buildInfo.setBuildOutputFileActionEnabled(true);
        buildInfo.setProviderOutputParserEnabled(BUILD_INFO_PROVIDER_ID, true);
        buildInfo.setProviderRunCommand(BUILD_INFO_PROVIDER_ID, GCC_PATH);
        buildInfo.save();
      }

      // Build settings.
      IBuilder builder = cfg.getEditableBuilder();
      if (builder != null) {
        if (builder.isInternalBuilder()) {
          IConfiguration prefCfg =
              ManagedBuildManager.getPreferenceConfiguration(false);
          IBuilder prefBuilder = prefCfg.getBuilder();
          cfg.changeBuilder(prefBuilder,
              ManagedBuildManager.calculateChildId(cfg.getId(), null),
              prefBuilder.getName());
          builder = cfg.getEditableBuilder();
        }
        builder.setBuildAttribute(IMakeBuilderInfo.BUILD_LOCATION,
            String.format("${%s}", ROOT_VARIABLE));  //$NON-NLS-1$
        builder.setBuildAttribute(IMakeBuilderInfo.BUILD_COMMAND,
            BUILD_COMMAND);
        builder.setBuildAttribute(IMakeBuilderInfo.BUILD_ARGUMENTS,
            BUILD_ARGUMENTS);
        builder.setBuildAttribute(IMakeBuilderInfo.BUILD_TARGET_INCREMENTAL,
            BUILD_TARGET);
        builder.setBuildAttribute(IMakeBuilderInfo.BUILD_TARGET_CLEAN,
            BUILD_TARGET_CLEAN);
        builder.setManagedBuildOn(false);
        builder.setAutoBuildEnable(false);
        builder.setCleanBuildEnable(true);
        builder.setIncrementalBuildEnable(true);
      }
     
      // Project variable.
      CConfigurationData data = "">
      ICConfigurationDescription cfgDescription =
          description.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID, data);
      RootVariableManager.createRootPathVariable(rootPath, cfgDescription);

      // Include path.
      CFolderData rootFolderData = data.getRootFolderData();
      CLanguageData[] languageDatas = rootFolderData.getLanguageDatas();
      for (CLanguageData languageData : languageDatas) {
        languageData.setEntries(ICLanguageSettingEntry.INCLUDE_PATH, includeEntries);
      }

      // Binary parsers.
      ICTargetPlatformSetting platformSetting = cfgDescription.getTargetPlatformSetting();
      platformSetting.setBinaryParserIds(BINARY_PARSERS);

      // Error parsers.
      ICBuildSetting buildSetting = cfgDescription.getBuildSetting();
      buildSetting.setErrorParserIDs(ERROR_PARSERS);
    }

    manager.setProjectDescription(project, description);

    postProcess(project, true);

    // Linked folders.
    Set<IPath> linkTargets = myCppPage.getLinkTargets();
    if (!linkTargets.isEmpty()) {
      Job job = new LinksChangeJob(project, new IFolder[0],
          linkTargets.toArray(new IPath[linkTargets.size()]));
      job.schedule();
    }
  }

  private ICLanguageSettingEntry[] getIncludeEntries() {
    List<ICLanguageSettingEntry> includes =
        new ArrayList<ICLanguageSettingEntry>();
    String dir = String.format("${%s}", ROOT_VARIABLE);  //$NON-NLS-1$
    includes.add(new CIncludePathEntry(dir, 0));
    dir = String.format("${%s}/stl/gcc3", ROOT_VARIABLE);  //$NON-NLS-1$
    includes.add(new CIncludePathEntry(dir, 0));

    return includes.toArray(new ICLanguageSettingEntry[includes.size()]);
  }

  public boolean canCreateWithoutToolchain() {
    return true;
  }

  @Override
  public IToolChain[] getSelectedToolChains() {
    // If no toolchain is selected by the user, use the default toolchain.
    if (full_tcs.isEmpty() || table.getSelection().length == 0) {
      return new IToolChain[] { null };
    } else {
      return super.getSelectedToolChains();
    }
  }
}