Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Help- Adding Library Programaticaly

Dear All,
I have updated my code. I want to add library when user clicks on button. Please help me. I am stucked here since very long.
Below is the code which I used it from PTP & ealrier post.

Thank you very much.
Regards,
Sagar

public class TestHandler extends AbstractHandler implements IHandler {

 
        public Object execute(ExecutionEvent event) throws ExecutionException {
       
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject proj = root.getProject("project1");   //Can I get the currrent Project Name instead of hardcoding it?
        IWorkbenchWindow window =
            HandlerUtil.getActiveWorkbenchWindowChecked(event);
                MessageDialog.openInformation(
                    window.getShell(), "MenuEclipseArticle Plug-in",
                    "Hello, Eclipse world");
                /*IProject proj = projects[0];*/
                //System.out.println( proj );
         
                //IProject projects = getProject();
         String newIncludePath = "/home/sagar/workspace";
         String newLib = "yes";
         String newLibSearchPath = "/home/sagar/workspace";
           
               
                System.out.println( proj );
                IManagedBuildInfo info = null;
                try {
                     info = ManagedBuildManager.getBuildInfo(proj);  // how do I get the current executing project here ?
                   } catch (Exception e) {}
                   System.out.println( info );    //code doesnot reach here
               IManagedProject mProj = info.getManagedProject();
            
               IConfiguration[] configs = mProj.getConfigurations();
             
               for (int i = 0; i < configs.length; i++) {
                      IConfiguration cf = configs[i];
                      addIncludePath(cf, newIncludePath);
                                        addLinkerOpt(cf,newLib,newLibSearchPath);
                                         setBuildCommand(cf,"gcc");
                                   }
                                   ManagedBuildManager.saveBuildInfo(proj, true);
                     
                            
 
 
                               

  return null;


               
    }

    public IProject getProject(){
                CProjectWizard wiz=null;
                final java.lang.String     PAGE_ID =    "New Probekit File (Probe)";
                MBSCustomPageData pData = MBSCustomPageManager.getPageData(PAGE_ID);
               
       
                 try {
                    IWizardPage wp = pData.getWizardPage();// in the constructor, this isn't available
                    IWizard w = wp.getWizard();
                    if(w instanceof CProjectWizard){
                        wiz = (CProjectWizard)w;
                    }
                    if(wiz==null){
                        System.out.println("Can't get CProjectWizard from MBS page data. Quitting. No MPI info added to project.");
                        return null;
                    }
                }
                catch (Exception e) {
                    Throwable reason = e.getCause();
                    System.out.println(e.getMessage()+" reason: "+reason);
                   
                }
                IProject proj=wiz.getProject(true);
                return proj;

    }
   
           private void addIncludePath(IConfiguration cf, String newIncludePath)
     {
   
                 String ext = "c";
                 ITool cfTool = cf.getToolFromInputExtension(ext);
   
                 String optID = "gnu.c.compiler.option.include.paths ";
                 IOption option = cfTool.getOptionById(optID);
   
                 String[] includePaths = null;
                 try {
                       includePaths = option.getIncludePaths();
                 } catch (BuildException e) {}
                 //add include path
                 int len = includePaths.length;
                 String newIncludePaths[] = new String[len + 1];
                 System.arraycopy(includePaths, 0, newIncludePaths, 0, len);
                 newIncludePaths[len] = newIncludePath;
   
                 ManagedBuildManager.setOption(cf, cfTool, option,
     newIncludePaths);
   
           }

           private void addLinkerOpt(IConfiguration cf, String libName, String
                    libPath) {
                                String ext = "o";
                                ITool cfTool = cf.getToolFromInputExtension(ext);
                  
                                // add to -l (libraries)
                                String optLibsID = " gnu.c.link.option.libs";
                                IOption option = cfTool.getOptionById(optLibsID);
                                addOptionValue(cf, cfTool, option, libName);
                  
                                //    add to       -L (library search path)
                                String optPathsID="gnu.c.link.option.paths";
                                option=cfTool.getOptionById(optPathsID);
                                addOptionValue(cf, cfTool, option, libPath);
                                } 
          
           private void setBuildCommand(IConfiguration cf, String buildCmd) {
                            ITool compiler = cf.getToolFromInputExtension("c");
                            ITool linker= cf.getToolFromInputExtension("o");
                            compiler.setToolCommand(buildCmd);
                            linker.setToolCommand(buildCmd);
                      }


               private void addOptionValue(IConfiguration cf, ITool tool, IOption option, String value) {
                       try {
                           int type = option.getValueType();
                           String[] valueList = null;
                           switch (type) {
                           case IOption.INCLUDE_PATH:
                               valueList = option.getIncludePaths();
                               valueList = add(valueList,value);
                               break;
                           case IOption.LIBRARIES:
                               valueList = option.getLibraries();
                               valueList=addNotPath(valueList, value);
                               break;
                           case IOption.LIBRARY_PATHS:// this is type for library search path cdt 4.0
                               valueList=option.getBasicStringListValue();
                               valueList=addNotPath(valueList,value);
                               break;
                          
                           default:
                               System.out.println("MPIProjectWizard runnable postprocessing (MPIProjectRunnable), can't get type of option for " + option.getName());
                               return;
                           }
                           // update the option in the managed builder options
                           ManagedBuildManager.setOption(cf, tool, option, valueList);
              
                       } catch (BuildException e) {
                           System.out.println("MPIProjectRunnable.addOptionValue(), "+e.getMessage());
                           System.out.println(e.getMessage());
                           e.printStackTrace();
                       }
              
                   }

               private String[] addNotPath(String[] strList, String newStr) {
                           int len = strList.length;
                           String newList[] = new String[len + 1];
                           System.arraycopy(strList, 0, newList, 0, len);
                           newList[len] = newStr;
                           return newList;
 
               }   
              
               private String[] add(String[] existingPaths, String newPath) {
                           String pathSep=java.io.File.pathSeparator;  // semicolon for windows, colon for Mac/Linux
                           List<String> newPathList = new ArrayList<String>();
                           String path;
                           for (int i = 0; i < existingPaths.length; i++) {
                               path = existingPaths[i];
                               newPathList.add(path);
                           }
                           String[] newPathArray=newPath.split(pathSep);
                           for (int i = 0; i < newPathArray.length; i++) {
                               path = newPathArray[i];
                               newPathList.add(path);
                           }
                          
                           String[] newArray=(String[])newPathList.toArray(new String[0]);
                           return newArray;
                          
                           /*
                  
                           */
                       }
     
              
}

Back to the top