Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "CDT/Developer/Code Snippets"

< CDT
(Created page with " == General == During CDT development, you may be interested in using code snippets to get certain things done. This page categorizes and provides these code snippets and ex...")
 
Line 10: Line 10:
  
 
== Build Related Snippets ==
 
== Build Related Snippets ==
 +
 +
===  Programmatically set an option in the project settings ===
 +
Before we get into the code, you have to understand a couple of concepts: 
 +
 +
C and C++ build settings are stored in the .cproject file in the root folder of your project. It's an xml file with your settings.
 +
 +
* '''ITool''' -- an  ITool is a sort of utility. Here are a few examples of tools:
 +
GCC Archiver, GCC C++ Compiler, GCC C Compiler, GCC C Linker, GCC C++ Linker, GCC Assembler
 +
 +
* ITool ''extends'' IHoldsOptions -- i.e, methods that ask for an IHoldsOptions will happily accept an ITool.
 +
 +
* '''IOption''' -- an IOption holds some configuration. E.g a boolean whether a checkbox is checked/unchecked.
 +
Now one must note, there are instances of an IOption that are generic 'abstract' instances of an option, and there are more concrete IOptions that can be used to save to disk.
 +
 +
One must also be careful as to how one aquires the IHoldsOptions. You should avoid getting IHoldsOption from the option directly, because if setting it for the first time, it might not be defined and your save-to-disk won't work. Instead get the IHoldsOption directly from your configuration and then get a mutable copy of IOptions from this IHolds options.
 +
 +
The code below assumes you have access to your IProject instance. If not please see: [http://cdt-devel-faq.wikidot.com/#toc4 getting IProject]
 +
 +
<source lang="java">
 +
        //Code to mimic gui 'ok' button actions from:
 +
 +
        //------ Get store that holds the tools settings.
 +
        ToolSettingsPrefStore toolsPrefStore = ToolSettingsPrefStore.getDefault();
 +
 +
        //------  Get gprof Option.
 +
        //Similar to: org.eclipse.cdt.managedbuilder.ui.properties.BuildOptionSettingsUI.propertyChange(PropertyChangeEvent)
 +
        Object[] ImmutableOption = toolsPrefStore.getOption("gnu.cpp.compiler.option.debugging.gprof"); //$NON-NLS-1$
 +
        IOption changedOption = (IOption)ImmutableOption[1];
 +
 +
        //------  Get configuration
 +
        IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(project);
 +
        IConfiguration ActiveConf = buildInfo.getDefaultConfiguration();  //TMP comment: Improved way of getting conf. works
 +
 +
        //Get the ITool of the super class of gprof (which is GCC C++ compiler).
 +
        ITool[] tools = ActiveConf.getTools();
 +
        ITool gccCompileriTool = null;
 +
        for (ITool iTool : tools) {
 +
            if (iTool.getName().equals("GCC C++ Compiler")) {
 +
                gccCompileriTool = iTool;
 +
                break;
 +
            }
 +
        }
 +
 +
        //Now we acquire an option that can be 'set' to something.
 +
        //In contrast to the immutable option, if the user never checked/unchecked the option by hand,
 +
        //then the first time 'set' of this option will work correctly. Whereas
 +
        //the immutable option would only work if the user checked/unchecked the option by hand before.
 +
        IOption MutableOptionToSet = null;
 +
        try {
 +
            MutableOptionToSet = gccCompileriTool.getOptionToSet(changedOption, false);
 +
            MutableOptionToSet.setValue(true);
 +
        } catch (BuildException e) {
 +
            // TODO Auto-generated catch block
 +
            e.printStackTrace();
 +
        }
 +
 +
        //-- get resource info.
 +
        IResourceInfo resourceInfos[] = ActiveConf.getResourceInfos();
 +
        IResourceInfo resourceInfo = resourceInfos[0];
 +
 +
        //------  Mark the option as enabled in the build manager.
 +
        IOption setOption = ManagedBuildManager.setOption(resourceInfo , gccCompileriTool, MutableOptionToSet, true);
 +
 +
        //------  Save to disk.
 +
        ManagedBuildManager.saveBuildInfo(project, true);
 +
</source>

Revision as of 11:49, 16 June 2014


General

During CDT development, you may be interested in using code snippets to get certain things done. This page categorizes and provides these code snippets and explains their usage context.


UI Snippets

Build Related Snippets

Programmatically set an option in the project settings

Before we get into the code, you have to understand a couple of concepts:

C and C++ build settings are stored in the .cproject file in the root folder of your project. It's an xml file with your settings.

  • ITool -- an ITool is a sort of utility. Here are a few examples of tools:

GCC Archiver, GCC C++ Compiler, GCC C Compiler, GCC C Linker, GCC C++ Linker, GCC Assembler

  • ITool extends IHoldsOptions -- i.e, methods that ask for an IHoldsOptions will happily accept an ITool.
  • IOption -- an IOption holds some configuration. E.g a boolean whether a checkbox is checked/unchecked.

Now one must note, there are instances of an IOption that are generic 'abstract' instances of an option, and there are more concrete IOptions that can be used to save to disk.

One must also be careful as to how one aquires the IHoldsOptions. You should avoid getting IHoldsOption from the option directly, because if setting it for the first time, it might not be defined and your save-to-disk won't work. Instead get the IHoldsOption directly from your configuration and then get a mutable copy of IOptions from this IHolds options.

The code below assumes you have access to your IProject instance. If not please see: getting IProject

        //Code to mimic gui 'ok' button actions from:
 
        //------ Get store that holds the tools settings.
        ToolSettingsPrefStore toolsPrefStore = ToolSettingsPrefStore.getDefault();
 
        //------  Get gprof Option.
        //Similar to: org.eclipse.cdt.managedbuilder.ui.properties.BuildOptionSettingsUI.propertyChange(PropertyChangeEvent)
        Object[] ImmutableOption = toolsPrefStore.getOption("gnu.cpp.compiler.option.debugging.gprof"); //$NON-NLS-1$
        IOption changedOption = (IOption)ImmutableOption[1];
 
        //------  Get configuration
        IManagedBuildInfo buildInfo = ManagedBuildManager.getBuildInfo(project);
        IConfiguration ActiveConf = buildInfo.getDefaultConfiguration();  //TMP comment: Improved way of getting conf. works
 
        //Get the ITool of the super class of gprof (which is GCC C++ compiler).
        ITool[] tools = ActiveConf.getTools();
        ITool gccCompileriTool = null;
        for (ITool iTool : tools) {
            if (iTool.getName().equals("GCC C++ Compiler")) {
                gccCompileriTool = iTool;
                break;
            }
        }
 
        //Now we acquire an option that can be 'set' to something.
        //In contrast to the immutable option, if the user never checked/unchecked the option by hand,
        //then the first time 'set' of this option will work correctly. Whereas
        //the immutable option would only work if the user checked/unchecked the option by hand before.
        IOption MutableOptionToSet = null;
        try {
            MutableOptionToSet = gccCompileriTool.getOptionToSet(changedOption, false);
            MutableOptionToSet.setValue(true);
        } catch (BuildException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        //-- get resource info.
        IResourceInfo resourceInfos[] = ActiveConf.getResourceInfos();
        IResourceInfo resourceInfo = resourceInfos[0];
 
        //------  Mark the option as enabled in the build manager.
        IOption setOption = ManagedBuildManager.setOption(resourceInfo , gccCompileriTool, MutableOptionToSet, true);
 
        //------  Save to disk.
        ManagedBuildManager.saveBuildInfo(project, true);

Back to the top