Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] How to programmatically setup a preprocessor symbol ?

Here is the code I'm using for this (based on posts in this maillist,
CDT newsgroup and unofficial wiki):

	private void configureProject() throws CoreException {
		ICProjectDescriptionManager mngr =
CoreModel.getDefault().getProjectDescriptionManager();
		ICProjectDescription des = mngr.getProjectDescription(project, true);
		ICConfigurationDescription confDes = des.getDefaultSettingConfiguration();

		// Configure include paths
		ICFolderDescription folderDescription =
confDes.getRootFolderDescription(); // or use
getResourceDescription(IResource), or pick one from
getFolderDescriptions()
		ICLanguageSetting[] languageSettings =
folderDescription.getLanguageSettings();

		ICLanguageSettingEntry[] projectDefines = getDefines();

		for(int idx = 0; idx < languageSettings.length; idx++)
		{
			ICLanguageSetting lang = languageSettings[idx];
			lang.setSettingEntries(ICSettingEntry.MACRO, projectDefines);
		}

		des.setActiveConfiguration(confDes);
		des.setCdtProjectCreated();
		mngr.setProjectDescription(project, des, true, null);

    }

	private ICLanguageSettingEntry[] getModelDefines() {
		ArrayList<ICLanguageSettingEntry> defines = new
ArrayList<ICLanguageSettingEntry>();
		Map<String, String> includeList = getComponentDefines();  // This
method returns a map of key/values
		Iterator<Entry<String, String>> iter = includeList.entrySet().iterator();
		while(iter.hasNext())
		{
			Entry<String, String> def = iter.next();
			ICMacroEntry entry =
				(ICMacroEntry)CDataUtil.createEntry(ICLanguageSettingEntry.MACRO,
					def.getKey(), def.getValue(), null, 0);
			defines.add(entry);
		}
		return defines.toArray(new ICLanguageSettingEntry[0]);
	}


Back to the top