Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] createCProject vs createNewStyleCProject

This code was included in a an old cdt-dev post:

On Mon, Jul 15, 2013 at 2:32 PM, Sergey Prigogin <eclipse.sprigogin@xxxxxxxxx> wrote:
Here is the solution I ended up with.

I completely removed "org.eclipse.cdt.managedbuilder.core.buildDefinitions" extension and left only the following extension in plugin.xml:

  <extension
        point="org.eclipse.cdt.core.LanguageSettingsProvider">
     <provider
           class="org.eclipse.cdt.core.language.settings.providers.LanguageSettingsGenericProvider"
           id="myLanguageSettingsProvider"
           name="My C/C++ Settings"
           prefer-non-shared="true">
        <language-scope id="org.eclipse.cdt.core.gcc"/>
        <language-scope id="org.eclipse.cdt.core.g++"/>
     </provider>
  </extension>

Here are the main pieces of the Java code (the code is made available under EPL):

  private static final String MY_SETTING_PROVIDER_ID = "myLanguageSettingsProvider";

  public configureProject(IProject project, IProgressMonitor monitor) throws CoreException {
    CCorePlugin cPlugin = CCorePlugin.getDefault();
    cPlugin.createCDTProject(project.getDescription(), project, monitor);
    cPlugin.convertProjectFromCtoCC(project, monitor);

    // Configure the project description.
    ICProjectDescriptionManager manager = cPlugin.getProjectDescriptionManager();

    ICProjectDescription projectDescription =
        manager.getProjectDescription(project, ICProjectDescriptionManager.GET_WRITABLE);
    ICConfigurationDescription configDescription = null;
    if (projectDescription == null) {
      projectDescription = manager.createProjectDescription(project, false);
    } else {
      configDescription = projectDescription.getActiveConfiguration();
    }

    if (configDescription == null) {
      ICConfigurationDescription base =
          cPlugin.getPreferenceConfiguration(ManagedBuildManager.CFG_DATA_PROVIDER_ID);
      configDescription =
          projectDescription.createConfiguration("my_configuration_id", "Default", base);
    }

    // Disable CDT's managed build.
    IConfiguration configuration =
        ManagedBuildManager.getConfigurationForDescription(configDescription);
    configuration.setArtifactName(project.getName());
    configuration.setBuildCommand("");
    configuration.setBuildArguments("");
    configuration.setCleanCommand("");
    IBuilder builder = configuration.getEditableBuilder();
    builder.setCommand("");
    builder.setManagedBuildOn(false);
    builder.setAutoBuildEnable(false);
    builder.setCleanBuildEnable(false);
    builder.setFullBuildEnable(false);
    builder.setIncrementalBuildEnable(false);

    // Configure the language settings providers.
    LanguageSettingsSerializableProvider settingsProvider =
        findLanguageSettingsProvider(configDescription);
    if (settingsProvider == null) {
      ILanguageSettingsProvider userSettingsProvider =
          LanguageSettingsManager.getExtensionProviderCopy(USER_LANGUAGE_SETTINGS_PROVIDER_ID,
              true);
      settingsProvider = (LanguageSettingsSerializableProvider)
          LanguageSettingsManager.getExtensionProviderCopy(MY_SETTING_PROVIDER_ID, true);
      ((ILanguageSettingsProvidersKeeper) configDescription).setLanguageSettingProviders(
          ImmutableList.of(userSettingsProvider, settingsProvider));
    }

    projectDescription.setDefaultSettingConfiguration(configDescription);
    projectDescription.setActiveConfiguration(configDescription);
    CConfigurationData data = "">
    // Disable binary parsers.
    data.getTargetPlatformData().setBinaryParserIds(new String[0]);
    // Save the modified project description.
    manager.setProjectDescription(project, projectDescription);
    projectDescription.setReadOnly(true, true);  // Done with project description changes.

    configureLanguageSettings(settingsProvider, configDescription);

    // Remove scanner configuration discovery nature since scanner configuration discovery
    // is not used. ManagedCProjectNature.MNG_NATURE_ID nature is preserved since it
    // allows us to control which files are indexed and which files are not.
    removeNatures(project, ScannerConfigNature.NATURE_ID);
    // Remove CDT builder since the project is using a different build system.
    removeBuilders(project, ManagedCProjectNature.BUILDER_ID);
  }

  /**
   * Finds and returns the language settings provider with id MY_SETTING_PROVIDER_ID.
   */
  private static @Nullable LanguageSettingsSerializableProvider findLanguageSettingsProvider(
      ICConfigurationDescription configDescription) {
    List<ILanguageSettingsProvider> providers =
        ((ILanguageSettingsProvidersKeeper) configDescription).getLanguageSettingProviders();
    for (ILanguageSettingsProvider provider : providers) {
      if (provider.getId().equals(MY_SETTING_PROVIDER_ID)) {
        return (LanguageSettingsSerializableProvider) provider;
      }
    }
    return null;
  }

  /**
   * Populates settingsProvider with include directories and predefined macros.
   */
  private boolean configureLanguageSettings(LanguageSettingsSerializableProvider settingsProvider,
      ICConfigurationDescription configDescription) throws CoreException {
    ...
  }

  /**
   * Removes the natures defined by {@code naturesToRemove} from the {@link IProject}.
   */
  private static void removeNatures(IProject project, String... naturesToRemove)
      throws CoreException {
    IProjectDescription description = project.getDescription();
    String[] natureIds = description.getNatureIds();
    int k = 0;
    for (int i = 0; i < natureIds.length; i++) {
      String natureId = natureIds[i];
      if (indexOf(natureId, naturesToRemove) < 0) {
        natureIds[k++] = natureId;
      }
    }
    if (k < natureIds.length) {
      description.setNatureIds(Arrays.copyOf(natureIds, k));
      project.setDescription(description, null);
    }
  }

  /**
   * Removes the builders defined by {@code buildersToRemove} from the {@link IProject}.
   */
  private static void removeBuilders(IProject project, String... buildersToRemove)
      throws CoreException {
    IProjectDescription description = project.getDescription();
    ICommand[] commands = description.getBuildSpec();
    int k = 0;
    for (int i = 0; i < commands.length; i++) {
      ICommand command = commands[i];
      if (indexOf(command.getBuilderName(), buildersToRemove) < 0) {
        commands[k++] = command;
      }
    }
    if (k < commands.length) {
      description.setBuildSpec(Arrays.copyOf(commands, k));
      project.setDescription(description, IResource.FORCE, null);
    }
  }

  /**
   * Returns the index within {@code array} of the first occurrence of {@code obj},
   * or -1 if {@code obj} is not found in {@code array}.
   */
  private static <T, U extends T> int indexOf(U obj, T[] array) {
    for (int i = 0; i < array.length; i++) {
      if (obj == array[i] || (obj != null && obj.equals(array[i]) {
        return i;
      }
    }
    return -1;
  }


On Fri, Jan 9, 2015 at 1:09 PM, Doug Schaefer <dschaefer@xxxxxxx> wrote:
Yes, we don’t need to wait.

But love to see your project creation code. Mine is here:


But it uses ManagedProject, ManagedBuildInfo, ToolChain, and Configuration which are all internal to the managed build system. Love to know what the proper way to set up a project with a given toolchain.

Thanks,
Doug.

From: Sergey Prigogin <eclipse.sprigogin@xxxxxxxxx>
Reply-To: "CDT General developers list." <cdt-dev@xxxxxxxxxxx>
Date: Friday, January 9, 2015 at 3:16 PM
To: "CDT General developers list." <cdt-dev@xxxxxxxxxxx>
Subject: Re: [cdt-dev] createCProject vs createNewStyleCProject

I agree that an official API for programmatically creating projects is badly needed, but I don't think fixing the tests should wait for it.

BTW, I have code that creates C++ projects with an external builder that doesn't call any non-API or deprecated methods.

-sergey

On Thu, Jan 8, 2015 at 7:36 PM, Doug Schaefer <dschaefer@xxxxxxx> wrote:
Yes. And we really need an official API for programmatically creating projects. I have copies of random code with lots of yellow underlines all over the place to do it.

Sent from my BlackBerry 10 smartphone on the Rogers network.
From: Sergey Prigogin
Sent: Thursday, January 8, 2015 7:42 PM
To: CDT General developers list.
Reply To: CDT General developers list.
Subject: [cdt-dev] createCProject vs createNewStyleCProject

I've just noticed that most tests use CProjectHelper.createCProject as opposed to CProjectHelper.createNewStyleCProject. In fact createNewStyleCProject methods don't even seem to support creation of C++ projects. This situation looks dangerous since the tests may not catch failures that happen only with the new style projects. Should we convert all tests to run on the new-style projects?

-sergey

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/cdt-dev


Back to the top