Eclipse CDT gets GCC options from code

I am working on an Eclipse plugin which, among other things, needs to check which compiler options are set in the current C project. In principle, I want to access the settings β†’ Properties / C / C ++ Build β†’ Settings β†’ GCC C Compiler β†’ All options.

I was looking for how to access it, but I did not find a way to do this. I tried to use it with the settings, for example, in the following code:

IEclipsePreferences root = Platform.getPreferencesService().getRootNode(); 

I can access my plugin settings this way, but not project C.

Does anyone know a way to do this? I do not need to change the compiler options, just to find out which flags are set.

UPDATE: I found a solution.

  IResourceInfo info = getResourceInfo(translationUnit, description); ITool tools[] = info.getTools(); for (ITool t : tools) { if (t.getName().compareToIgnoreCase("GCC C Compiler") == 0) { try { //Finally the field I was looking for String commandLine = t.getToolCommandFlagsString(getProject().getFullPath(), null); } catch (BuildException e) { e.printStackTrace(); } } } 

Then I can parse the string, not perfect, but it works. I got the getResourceInfo () function from this post: How to programmatically change the Eclipse CDT tool settings for a file?

So thanks justinmreina for the answer!

+1
source share
1 answer

your walk on a dark and lonely road, my friend :). but hilarious nonetheless.

Settings on a Custom Toolchain / Tool

Here is an example when someone tried to programmatically set the GNU tools / tools parameters:

*, and here is the same author's background thread when solving it:

What he did here will lead you to your decision. I would suggest looking at the plugin.xml org.eclipse.cdt.managedbuild.gnu.ui file first. Focus on tools, tools and their options.

Finding GNU C Tools / Tools Options

Also, here is a useful article that I returned a bit, "finding dang parameters in a GNU C project." Not an exact question with an OP, but the answer is relevant to your question.

Conclusion

I very much doubt that you will find the answer <10 lines of code, even to set the -v flag for the compiler ... If you find a simple result, I would suggest placing it here as a continuation.

Good luck

EDIT . I have been nibbling this for some time because I recently stumbled / failed. Here's how to set parameters from code.

 //assumptions //#1 project is [0] in workspace //#2 compiler is [2] in workspace //get project IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject("hello_world"); //get <storageModule moduleId="org.eclipse.cdt.core.settings"> IManagedBuildInfo info = ManagedBuildManager.getBuildInfo(proj); //get <storageModule moduleId="cdtBuildSystem"> IManagedProject sub_info = info.getManagedProject(); //get <configuration name="Debug"> IConfiguration config = sub_info.getConfigurations()[0]; //get <toolChain> IToolChain toolchain = config.getToolChain(); //get <tool name="GCC C Compiler"> ITool tool = toolchain.getTools()[2]; //get <option> IOption option = tool.getOptionBySuperClassId("gnu.c.compiler.option.misc.other"); //----append new flag----// String new_opt_value = option.getValue() + " -mySuperFlag"; //-----store it----// ManagedBuildManager.setOption(config, tool, option, new_opt_value); ManagedBuildManager.saveBuildInfo(proj, true); 

Notes - as soon as you start viewing this operation as an "Eclipse Resource", the methodology becomes (somewhat ...) understandable - each call of an object to access a field is simply access to another section in the XML schema of the .cproject resource

Hope this helps!

+2
source

Source: https://habr.com/ru/post/958353/


All Articles