summaryrefslogtreecommitdiff
path: root/cpp/source/org/openoffice/ide/eclipse/cpp/CDTHelper.java
blob: a7ff37eeb306b387a611ffc6569d77db4e8669af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*************************************************************************
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2009 by Novell, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Cédric Bosdonnat.
 *
 * Copyright: 2009 by Novell, Inc.
 *
 * All Rights Reserved.
 * 
 ************************************************************************/
package org.openoffice.ide.eclipse.cpp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICFolderDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IManagedBuildInfo;
import org.eclipse.cdt.managedbuilder.core.IOption;
import org.eclipse.cdt.managedbuilder.core.ITool;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.core.ManagedCProjectNature;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.openoffice.ide.eclipse.core.PluginLogger;

/**
 * Utility class providing shortcuts for CDT operations on projects.
 * 
 * @author cbosdonnat
 *
 */
public class CDTHelper {

    /**
     * Add the C++ project nature and all the other needed natures.
     * 
     * @param pProject the project on which to set the natures
     * @param pMonitor a progress monitor
     * 
     * @throws CoreException if anything wrong happens
     */
    public static void addCDTNature( IProject pProject, IProgressMonitor pMonitor ) throws CoreException {
        if (!pProject.exists()) {
            pProject.create( pMonitor );
            PluginLogger.debug(
                    "Project created during language specific operation"); //$NON-NLS-1$
        }

        if (!pProject.isOpen()) {
            pProject.open( pMonitor );
            PluginLogger.debug("Project opened"); //$NON-NLS-1$
        }

        CProjectNature.addCNature(pProject, pMonitor );
        CCProjectNature.addCCNature( pProject, pMonitor );
        ManagedCProjectNature.addManagedNature( pProject, pMonitor );
    }
    
    /**
     * Add some configuration entries like includes, macros, libraries paths.
     * 
     * @param pProject the project on which to add the entries
     * @param pNewEntries the new entries to add
     * @param pEntriesType the entries type in {@link ICSettingEntry}
     * 
     * @see #changeEntries(IProject, ICLanguageSettingEntry[], int, boolean)
     */
    public static void addEntries( IProject pProject, ICLanguageSettingEntry[] pNewEntries, int pEntriesType ) {
        changeEntries( pProject, pNewEntries, pEntriesType, false );
    }
    
    /**
     * Remove some configuration entries like includes, macros, libraries paths.
     * 
     * @param pProject the project on which to remove the entries
     * @param pOldEntries the new entries to remove
     * @param pEntriesType the entries type in {@link ICSettingEntry}
     * 
     * @see #changeEntries(IProject, ICLanguageSettingEntry[], int, boolean)
     */
    public static void removeEntries( IProject pProject, ICLanguageSettingEntry[] pOldEntries, int pEntriesType ) {
        changeEntries( pProject, pOldEntries, pEntriesType, true );
    }
    
    /**
     * Add/Remove some configuration entries like includes, macros, libraries paths.
     * 
     * @param pProject the project to handle
     * @param pEntries the entries to add/remove
     * @param pEntriesType the entries type in {@link ICSettingEntry}
     * @param pRemove <code>true</code> if the entries have to be removed, <code>false</code> 
     *          if they have to be added
     */
    private static void changeEntries( IProject pProject, ICLanguageSettingEntry[] pEntries, 
            int pEntriesType, boolean pRemove ) {
        ICProjectDescription prjDesc = CoreModel.getDefault().getProjectDescription( pProject );
        ICConfigurationDescription[] configs = prjDesc.getConfigurations();
        
        // Set them on all the languages of all the configurations
        for (ICConfigurationDescription config : configs) {
            ICFolderDescription folder = config.getRootFolderDescription();
            ICLanguageSetting[] languages = folder.getLanguageSettings();
            for (ICLanguageSetting lang : languages) {
                List<ICLanguageSettingEntry> entries = lang.getSettingEntriesList( pEntriesType );
                for ( ICLanguageSettingEntry entry : pEntries ) {
                    boolean contained = entries.contains( entry );
                    if ( contained && pRemove ) {
                        entries.remove( entry );
                    } else if ( !contained && !pRemove ) {
                        entries.add( entry );
                    }
                }
                lang.setSettingEntries( pEntriesType, entries );
            }
        }
        
        try {
            CoreModel.getDefault().setProjectDescription( pProject, prjDesc );
        } catch ( CoreException e ) {
            PluginLogger.error( Messages.getString("CDTHelper.PathEntryError"), e ); //$NON-NLS-1$
        }
    }
    
    /**
     * Add some libraries (-l gcc option) to the project.
     * 
     * @param pProject the project to handle
     * @param pLibNames the libraries to add
     * 
     * @see #changeLibs(IProject, String[], boolean)
     */
    public static void addLibs( IProject pProject, String[] pLibNames ) {
        changeLibs( pProject, pLibNames, false );
    }
    
    /**
     * Remove some libraries (-l gcc option) to the project.
     * 
     * @param pProject the project to handle
     * @param pLibNames the libraries to remove
     * 
     * @see #changeLibs(IProject, String[], boolean)
     */
    public static void removeLibs( IProject pProject, String[] pLibNames ) {
        changeLibs( pProject, pLibNames, true );
    }
    
    /**
     * Add/remove some libraries (-l gcc option) to the project.
     * 
     * @param pProject the project to handle
     * @param pLibNames the libraries to add/remove
     * @param pRemove <code>true</code> if the libs have to be removed, 
     *              <code>false</code> if they have to be added
     */
    private static void changeLibs( IProject pProject, String[] pLibNames, boolean pRemove ) {
        IManagedBuildInfo infos = ManagedBuildManager.getBuildInfo( pProject );
        IConfiguration[] configs = infos.getManagedProject().getConfigurations();
        
        for (IConfiguration config : configs ) {
            ITool tool = config.calculateTargetTool();
            IOption[] options = tool.getOptions();
            for (IOption option : options) {
                try {
                    if ( option.getValueType() == IOption.LIBRARIES ) {
                        // Append the libraries if not already set
                        String[] libs = option.getLibraries();
                        ArrayList<String> newLibs = new ArrayList<String>( Arrays.asList( libs ) );
                        for ( String lib : pLibNames ) {
                            boolean contained = newLibs.contains( lib );
                            if ( !contained && !pRemove ) {
                                newLibs.add( lib );
                            } else if ( contained && pRemove ) {
                                newLibs.remove( lib );
                            }
                        }
                        ManagedBuildManager.setOption(config, tool, option, 
                                newLibs.toArray( new String[ newLibs.size() ] ) );
                    }
                } catch ( Exception e ) {
                    PluginLogger.error( Messages.getString("CDTHelper.LinkerOptionsError"), e ); //$NON-NLS-1$
                }
            }
        }
        ManagedBuildManager.saveBuildInfo( pProject, false );
    }
}