sysperfana/analyzetoolext/com.nokia.s60tools.analyzetool/src/com/nokia/s60tools/analyzetool/builder/AnalyzeToolBuilder.java
changeset 1 1050670c6980
child 6 f65f740e69f9
equal deleted inserted replaced
0:5ad7ad99af01 1:1050670c6980
       
     1 /*
       
     2  * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  Definitions for the class AnalyzeToolBuilder
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.s60tools.analyzetool.builder;
       
    19 
       
    20 import java.util.AbstractList;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Iterator;
       
    23 import java.util.List;
       
    24 import java.util.Locale;
       
    25 
       
    26 import org.eclipse.core.resources.IFile;
       
    27 import org.eclipse.core.resources.IProject;
       
    28 import org.eclipse.core.runtime.CoreException;
       
    29 import org.eclipse.core.runtime.IPath;
       
    30 import org.eclipse.core.runtime.IProgressMonitor;
       
    31 import org.eclipse.core.runtime.IStatus;
       
    32 import org.eclipse.core.runtime.Path;
       
    33 import org.eclipse.core.runtime.Status;
       
    34 import org.eclipse.core.runtime.jobs.Job;
       
    35 import org.eclipse.jface.preference.IPreferenceStore;
       
    36 import org.eclipse.ui.PlatformUI;
       
    37 
       
    38 import com.nokia.carbide.cdt.builder.CarbideBuilderPlugin;
       
    39 import com.nokia.carbide.cdt.builder.builder.CarbideCPPBuilder;
       
    40 import com.nokia.carbide.cdt.builder.builder.CarbideCommandLauncher;
       
    41 import com.nokia.carbide.cdt.builder.project.ICarbideBuildConfiguration;
       
    42 import com.nokia.carbide.cdt.builder.project.ICarbideProjectInfo;
       
    43 import com.nokia.carbide.cdt.builder.BuildArgumentsInfo;
       
    44 import com.nokia.s60tools.analyzetool.Activator;
       
    45 import com.nokia.s60tools.analyzetool.global.Constants;
       
    46 import com.nokia.s60tools.analyzetool.global.Util;
       
    47 import com.nokia.s60tools.analyzetool.ui.CustomInputDialog;
       
    48 
       
    49 /**
       
    50  * Base class for AnalyzeTool builders.
       
    51  *
       
    52  * This class modifies Carbide builder settings during the AnalyzeTool build.
       
    53  * This class can be used to built whole project or selected components with
       
    54  * AnalyzeTool
       
    55  *
       
    56  * @author kihe
       
    57  *
       
    58  */
       
    59 public class AnalyzeToolBuilder extends CarbideCPPBuilder {
       
    60 
       
    61 	/** Pre-builder id. */
       
    62 	public static final String AT_BUILDER_ID = "com.nokia.s60tools.analyzetool.analyzeToolPreBuilder";
       
    63 
       
    64 	/** CarbideCommandLauncher reference. */
       
    65 	protected static CarbideCommandLauncher cmdLauncher = null;
       
    66 
       
    67 	/** Used mmp files. */
       
    68 	protected List<String> mmpFiles;
       
    69 
       
    70 	/** Verbose atool.exe output? */
       
    71 	private boolean verbose;
       
    72 
       
    73 	/**
       
    74 	 * Gets shared instance of CarbideCommandLauncher.
       
    75 	 *
       
    76 	 * @return CarbideCommandLauncher reference
       
    77 	 */
       
    78 	protected static CarbideCommandLauncher getCarbideCommandLauncher() {
       
    79 		return cmdLauncher;
       
    80 	}
       
    81 
       
    82 	/**
       
    83 	 * Checks is SBS v2 build activated for the project.
       
    84 	 *
       
    85 	 * @param cpi
       
    86 	 *            ICarbideProjectInfo
       
    87 	 * @return True if SBS v2 build is activated otherwise False
       
    88 	 */
       
    89 	public static boolean isSBSBuildActivated(final ICarbideProjectInfo cpi) {
       
    90 		try {
       
    91 			final Class<?> buildManagerClass = Class
       
    92 					.forName("com.nokia.carbide.cdt.builder.ICarbideBuildManager");
       
    93 
       
    94 			java.lang.reflect.Method[] methods = buildManagerClass.getMethods();
       
    95 			for (int i = 0; i < methods.length; i++) {
       
    96 				java.lang.reflect.Method oneMethod = methods[i];
       
    97 				if (oneMethod.toString().contains("isCarbideSBSv2Project")) {
       
    98 					IProject project = cpi.getProject();
       
    99 					if (project == null) {
       
   100 						return false;
       
   101 					}
       
   102 					Object[] objs = new Object[1];
       
   103 					objs[0] = project;
       
   104 					Boolean obj = (Boolean) oneMethod.invoke(
       
   105 							CarbideBuilderPlugin.getBuildManager(), objs);
       
   106 					return obj.booleanValue();
       
   107 
       
   108 				}
       
   109 			}
       
   110 		} catch (ClassNotFoundException cnfe) {
       
   111 			cnfe.printStackTrace();
       
   112 			return false;
       
   113 		} catch (SecurityException se) {
       
   114 			se.printStackTrace();
       
   115 			return false;
       
   116 		} catch (IllegalAccessException iae) {
       
   117 			iae.printStackTrace();
       
   118 			return false;
       
   119 		} catch (IllegalArgumentException iare) {
       
   120 			iare.printStackTrace();
       
   121 			return false;
       
   122 		} catch (java.lang.reflect.InvocationTargetException ite) {
       
   123 			ite.printStackTrace();
       
   124 			return false;
       
   125 		}
       
   126 		return false;
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Ask data file name from the user and after that executes atool.exe.
       
   131 	 *
       
   132 	 * @param monitor
       
   133 	 *            {@link IProgressMonitor} reference
       
   134 	 * @param cpi
       
   135 	 *            {@link ICarbideProjectInfo} reference
       
   136 	 * @param files
       
   137 	 *            Used mmp files
       
   138 	 *
       
   139 	 * @return COMMAND_LINE_ERROR_CODE.OK if no errors, otherwise error code
       
   140 	 */
       
   141 	private int askFileNameAndBuild(final IProgressMonitor monitor,
       
   142 			final ICarbideProjectInfo cpi, final List<IFile> files) {
       
   143 		// ask for the user data file name
       
   144 		CustomInputDialog dialog = new CustomInputDialog(
       
   145 				Constants.ANALYZE_TOOL_TITLE,
       
   146 				Constants.DIALOG_INPUT_DATA_FILE_NAME, "");
       
   147 		dialog.open();
       
   148 
       
   149 		//get data file name
       
   150 		String dataFileName = dialog.getUserInput();
       
   151 		int errorCode = Constants.COMMAND_LINE_ERROR_CODE.OK.getCode();
       
   152 		// user press "Cancel"
       
   153 		if (dataFileName == null || ("").equals(dataFileName)) {
       
   154 			buildCancelled(monitor, true);
       
   155 		} else { // if user specify data file name
       
   156 			errorCode = executeAtool(dataFileName, Constants.ATOOL_INST,
       
   157 					Constants.LOGGING_S60, files, cpi, monitor);
       
   158 		}
       
   159 		return errorCode;
       
   160 	}
       
   161 
       
   162 	/**
       
   163 	 * Ask logging mode from the user.
       
   164 	 *
       
   165 	 * @return User selected logging mode
       
   166 	 */
       
   167 	private String askLoggingMode() {
       
   168 
       
   169 		IPreferenceStore store = Activator.getPreferences();
       
   170 
       
   171 		// logging modes
       
   172 		AbstractList<String> modes = new ArrayList<String>();
       
   173 		modes.add(Constants.PREFS_EXT);
       
   174 
       
   175 		// if fast data gathering mode is enabled => add to selection list
       
   176 		if( store.getBoolean(Constants.LOGGING_FAST_ENABLED ) ) {
       
   177 			modes.add(Constants.PREFS_EXT_FAST);
       
   178 		}
       
   179 		modes.add(Constants.PREFS_S60);
       
   180 
       
   181 		// open selection dialog
       
   182 		String userSelection = Util.openSelectionDialog(
       
   183 				Constants.DIALOG_SELECT_LOGGING_MODE, null, modes);
       
   184 
       
   185 		// get used logging mode for the atool.exe
       
   186 		// user selected logging mode can not be used straight because it is
       
   187 		// different in the UI and which atool.exe uses
       
   188 		String modeSelection = "";
       
   189 		if (userSelection == Constants.PREFS_EXT) {
       
   190 			modeSelection = Constants.LOGGING_EXT;
       
   191 		} else if (userSelection == Constants.PREFS_S60) {
       
   192 			modeSelection = Constants.LOGGING_S60;
       
   193 		} else if( userSelection == Constants.PREFS_EXT_FAST) {
       
   194 			modeSelection = Constants.LOGGING_EXT_FAST;
       
   195 		}
       
   196 		return modeSelection;
       
   197 	}
       
   198 
       
   199 	/**
       
   200 	 * Cancels AnalyzeTool build.
       
   201 	 *
       
   202 	 * @param monitor
       
   203 	 *            Currently running progress monitor
       
   204 	 * @param continueBuild
       
   205 	 *            Stop the whole build chain (including Carbide other builders)
       
   206 	 *            or not
       
   207 	 */
       
   208 	public final void buildCancelled(final IProgressMonitor monitor,
       
   209 			final boolean continueBuild) {
       
   210 
       
   211 		IPreferenceStore store = Activator.getPreferences();
       
   212 
       
   213 		store.setValue(Constants.PREFS_BUILD_CANCELLED, true);
       
   214 
       
   215 		// if user wants to continue build
       
   216 		if (!continueBuild) {
       
   217 			// write info to the Carbide console view
       
   218 			getCarbideCommandLauncher().writeToConsole(
       
   219 					Constants.BUILD_CANCELLED);
       
   220 
       
   221 			// update monitor state
       
   222 			monitor.setCanceled(true);
       
   223 			monitor.done();
       
   224 
       
   225 		}
       
   226 
       
   227 		boolean promptMPPChange = store.getBoolean(Constants.PREFS_PROMPT_MMP);
       
   228 		boolean manageDeps = store.getBoolean(Constants.PREFS_MANAGE_DEPS);
       
   229 		boolean useConcBuild = store.getBoolean(Constants.PREFS_CONC_BUILD);
       
   230 
       
   231 		// build canceled set project preference back to normal
       
   232 		IPreferenceStore cStore = CarbideBuilderPlugin.getDefault()
       
   233 				.getPreferenceStore();
       
   234 		cStore
       
   235 				.setValue(
       
   236 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MMP_CHANGED_ACTION_PROMPT,
       
   237 						promptMPPChange);
       
   238 		cStore
       
   239 				.setValue(
       
   240 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MANAGE_DEPENDENCIES,
       
   241 						manageDeps);
       
   242 		cStore
       
   243 				.setValue(
       
   244 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_USE_CONCURRENT_BUILDING,
       
   245 						useConcBuild);
       
   246 
       
   247 		// CarbideCPPBuilder to forgot build state
       
   248 		super.forgetLastBuiltState();
       
   249 	}
       
   250 
       
   251 	/**
       
   252 	 * Builds user selected components.
       
   253 	 *
       
   254 	 * Normally this method is called from the CompileSymbianComponent
       
   255 	 *
       
   256 	 * @param selectedFiles
       
   257 	 *            User selected files
       
   258 	 */
       
   259 	public final void buildComponents(final List<IFile> selectedFiles) {
       
   260 		// create new job
       
   261 		Job buildJob = new Job(Constants.BUILD_AND_INSTRUMENT) {
       
   262 			@Override
       
   263 			protected IStatus run(final IProgressMonitor monitor) {
       
   264 				try {
       
   265 
       
   266 					// get project
       
   267 					IProject lastProject = selectedFiles.get(0).getProject();
       
   268 
       
   269 					// get CarbideProjectInfo
       
   270 					ICarbideProjectInfo cpi = CarbideBuilderPlugin
       
   271 							.getBuildManager().getProjectInfo(lastProject);
       
   272 
       
   273 					// get command launcher
       
   274 					cmdLauncher = new CarbideCommandLauncher(lastProject,
       
   275 							monitor, Constants.atoolParserIds, cpi
       
   276 									.getINFWorkingDirectory());
       
   277 					cmdLauncher.showCommand(true);
       
   278 
       
   279 					// run pre steps for the build
       
   280 					if (!runPreSteps(cmdLauncher, monitor, cpi)) {
       
   281 						return new Status(IStatus.ERROR, Constants.ANALYZE_TOOL_TITLE,
       
   282 								IStatus.ERROR, Constants.CANCELLED, null);
       
   283 					}
       
   284 
       
   285 					// build selected components
       
   286 					runBuild(Constants.ATOOL_INST, monitor, cpi, selectedFiles);
       
   287 
       
   288 					// run post steps
       
   289 					runPostSteps(cpi);
       
   290 
       
   291 					//after the build is finished => open the console view
       
   292 					Util.openConsoleView();
       
   293 				} finally {
       
   294 					monitor.done();
       
   295 				}
       
   296 				return new Status(IStatus.OK, Constants.ANALYZE_TOOL_TITLE,
       
   297 						IStatus.OK, Constants.COMPLETE, null);
       
   298 			}
       
   299 		};
       
   300 
       
   301 		buildJob.setPriority(Job.BUILD);
       
   302 		buildJob.schedule();
       
   303 	}
       
   304 
       
   305 	/**
       
   306 	 * Checks that platform is ARMV5, GCEE or WINSCW
       
   307 	 *
       
   308 	 * @param cpi
       
   309 	 *            ICarbideProjectInfo
       
   310 	 * @return True if platform is set to ARMV5, GCCE or WINSCW otherwise False
       
   311 	 */
       
   312 	public final boolean checkPlatform(final ICarbideProjectInfo cpi) {
       
   313 		String platform = cpi.getDefaultConfiguration().getPlatformString();
       
   314 		if ( platform.equals(Constants.BUILD_TARGET_ARMV5) || platform.equals(Constants.BUILD_TARGET_WINSCW)
       
   315 				|| platform.equals(Constants.BUILD_TARGET_GCEE) ) {
       
   316 			return true;
       
   317 		}
       
   318 		return false;
       
   319 	}
       
   320 
       
   321 	/**
       
   322 	 * Executes atool.exe with options.
       
   323 	 *
       
   324 	 * @param dataFileName
       
   325 	 *            Used data file name
       
   326 	 * @param type
       
   327 	 *            Type of execution. Possible types ATOOL_INST or ATOOL_UNINST
       
   328 	 * @param loggingModeCommand
       
   329 	 *            Used logging mode
       
   330 	 * @param userSelectedMmpFiles
       
   331 	 *            List of user selected mmp files
       
   332 	 * @param cpi
       
   333 	 *            ICarbideProjectInfo reference
       
   334 	 * @param monitor
       
   335 	 *            Progress monitor reference
       
   336 	 *
       
   337 	 * @return COMMAND_LINE_ERROR_CODE.OK if no errors, otherwise error code
       
   338 	 */
       
   339 	protected final int executeAtool(final String dataFileName,
       
   340 			final String type, final String loggingModeCommand,
       
   341 			final List<IFile> userSelectedMmpFiles,
       
   342 			final ICarbideProjectInfo cpi, final IProgressMonitor monitor) {
       
   343 
       
   344 		// get used platform
       
   345 		String platform = cpi.getDefaultConfiguration().getPlatformString().toLowerCase(Locale.US);
       
   346 
       
   347 		// get build target
       
   348 		String buildTarget = cpi.getDefaultConfiguration().getTargetString().toLowerCase(Locale.US);
       
   349 
       
   350 		// used arguments to atool.exe
       
   351 		AbstractList<String> usedArguments = new ArrayList<String>();
       
   352 
       
   353 		// which logging mode is used
       
   354 		if( loggingModeCommand.equalsIgnoreCase(Constants.LOGGING_EXT) )
       
   355 		{
       
   356 			usedArguments.add(Constants.ATOOL_INST_E);
       
   357 		}
       
   358 		else if( loggingModeCommand.equalsIgnoreCase(Constants.LOGGING_EXT_FAST)) {
       
   359 			usedArguments.add(Constants.ATOOL_INST_EF);
       
   360 		}
       
   361 		else
       
   362 		{
       
   363 			usedArguments.add(Constants.ATOOL_INST_I);
       
   364 		}
       
   365 
       
   366 		// if data file is set
       
   367 		if (dataFileName != null && !("").equals(dataFileName)) {
       
   368 			usedArguments.add("-f");
       
   369 			usedArguments.add(dataFileName);
       
   370 		}
       
   371 
       
   372 		// if "verbose atool.exe output" is enabled
       
   373 		if (verbose) {
       
   374 			usedArguments.add(Constants.ATOOL_SHOW_DEBUG);
       
   375 		}
       
   376 
       
   377 		IPreferenceStore store = Activator.getPreferences();
       
   378 		if( store.getBoolean(Constants.USE_CALLSTACK_SIZE) ) {
       
   379 			int callstackSize = store.getInt(Constants.CALLSTACK_SIZE);
       
   380 			usedArguments.add(Constants.CALLSTACK_SIZE_OPTION);
       
   381 			usedArguments.add(Integer.toString(callstackSize));
       
   382 		}
       
   383 
       
   384 		//add build command
       
   385 		// if project using SBSv2 build system
       
   386 		boolean sbsBuild = isSBSBuildActivated(cpi);
       
   387 		if (sbsBuild) {
       
   388 			usedArguments.add("sbs");
       
   389 			usedArguments.add("-c");
       
   390 			StringBuffer buildCommand = new StringBuffer();
       
   391 			buildCommand.append(platform);
       
   392 			buildCommand.append('_');
       
   393 			buildCommand.append(buildTarget);
       
   394 			usedArguments.add(buildCommand.toString());
       
   395 		}
       
   396 		else
       
   397 		{
       
   398 			usedArguments.add("abld");
       
   399 			usedArguments.add("build");
       
   400 			usedArguments.add(platform);
       
   401 			usedArguments.add(buildTarget);
       
   402 		}
       
   403 
       
   404 		int errorCode = Constants.COMMAND_LINE_ERROR_CODE.OK.getCode();
       
   405 		// if user has selected custom setup of components =>build them
       
   406 		// this means that call is come from CompileSymbianComponent class
       
   407 		if (userSelectedMmpFiles != null && !userSelectedMmpFiles.isEmpty()) {
       
   408 
       
   409 			//get atool.exe command
       
   410 			Iterator<IFile> files = userSelectedMmpFiles.iterator();
       
   411 			while(files.hasNext()) {
       
   412 				IFile file = files.next();
       
   413 				IPath location = file.getLocation();
       
   414 
       
   415 				String mmpFileName = getMMPFileName(location, true);
       
   416 				if( mmpFileName == null || ("").equals(mmpFileName)) {
       
   417 					continue;
       
   418 				}
       
   419 
       
   420 				// if this is first mmp file add it parameter list
       
   421 				usedArguments.add("-p");
       
   422 				usedArguments.add(mmpFileName);
       
   423 			}
       
   424 
       
   425 			//now the command is ready
       
   426 			//execute command
       
   427 			// execute atool.exe via CommandLauncher class
       
   428 			cmdLauncher.showCommand(true);
       
   429 			String[] arguments = new String[usedArguments.size()];
       
   430 		    usedArguments.toArray(arguments);
       
   431 		    errorCode = cmdLauncher.executeCommand(new Path(Util
       
   432 					.getAtoolInstallFolder()), arguments,
       
   433 					CarbideCPPBuilder.getResolvedEnvVars(cpi
       
   434 							.getDefaultConfiguration()), cpi
       
   435 							.getINFWorkingDirectory());
       
   436 
       
   437 
       
   438 			// if user press "Cancel"
       
   439 			if (monitor.isCanceled()) {
       
   440 				buildCancelled(monitor, false);
       
   441 				monitor.done();
       
   442 			}
       
   443 
       
   444 			// thru selected files and build with the Carbide builder
       
   445 			for (int i = 0; i < userSelectedMmpFiles.size(); i++) {
       
   446 
       
   447 				// get one file
       
   448 				IFile file = userSelectedMmpFiles.get(i);
       
   449 
       
   450 				// get file location
       
   451 				IPath fileLocation = file.getLocation();
       
   452 
       
   453 				// invoke normal Carbide build
       
   454 				try {
       
   455 					CarbideCPPBuilder.invokeSymbianComponenetAction(cpi
       
   456 							.getDefaultConfiguration(),
       
   457 							CarbideCPPBuilder.BUILD_COMPONENT_ACTION,
       
   458 							fileLocation, cmdLauncher, monitor, true);
       
   459 				} catch (CoreException e) {
       
   460 					e.printStackTrace();
       
   461 				}
       
   462 
       
   463 				// if user press "Cancel"
       
   464 				if (monitor.isCanceled()) {
       
   465 					buildCancelled(monitor, false);
       
   466 					monitor.done();
       
   467 				}
       
   468 			}
       
   469 
       
   470 			// Carbide build is finished
       
   471 			// now remove AnalyzeTool made modifications
       
   472 			// if project contains build erros => only uninstrument mmp
       
   473 			// file
       
   474 			if (CarbideCPPBuilder.projectHasBuildErrors(cpi
       
   475 					.getProject())) {
       
   476 				usedArguments.set(0, Constants.ATOOL_UNINST_FAILED);
       
   477 			}
       
   478 
       
   479 			// project succesfully build => uninstrumet project
       
   480 			runUninstrument(Constants.ATOOL_UNINST, cpi, monitor);
       
   481 
       
   482 		}
       
   483 		// if build from bld.inf file
       
   484 		else if (cpi.isBuildingFromInf()) {
       
   485 			cmdLauncher.showCommand(true);
       
   486 			String[] arguments = new String[usedArguments.size()];
       
   487 		    usedArguments.toArray(arguments);
       
   488 		    errorCode = cmdLauncher.executeCommand(new Path(Util.getAtoolInstallFolder()),
       
   489 					arguments,
       
   490 					CarbideCPPBuilder.getResolvedEnvVars(cpi
       
   491 							.getDefaultConfiguration()), cpi
       
   492 							.getINFWorkingDirectory());
       
   493 
       
   494 			if (mmpFiles != null) {
       
   495 				mmpFiles.clear();
       
   496 			}
       
   497 
       
   498 			// if user press "Cancel"
       
   499 			if (monitor.isCanceled()) {
       
   500 				buildCancelled(monitor, false);
       
   501 				monitor.done();
       
   502 			}
       
   503 
       
   504 		} else { // instrument only defined components
       
   505 			// get build components
       
   506 			mmpFiles = cpi.getInfBuildComponents();
       
   507 
       
   508 			for (int i = 0; i < mmpFiles.size(); i++) {
       
   509 				usedArguments.add("-p");
       
   510 				usedArguments.add(mmpFiles.get(i));
       
   511 			}
       
   512 			cmdLauncher.showCommand(true);
       
   513 			String[] arguments = new String[usedArguments.size()];
       
   514 		    usedArguments.toArray(arguments);
       
   515 		    errorCode = cmdLauncher.executeCommand(new Path(Util
       
   516 					.getAtoolInstallFolder()), arguments,
       
   517 					CarbideCPPBuilder.getResolvedEnvVars(cpi
       
   518 							.getDefaultConfiguration()), cpi
       
   519 							.getINFWorkingDirectory());
       
   520 
       
   521 			// if user press "Cancel"
       
   522 			if (monitor.isCanceled()) {
       
   523 				buildCancelled(monitor, false);
       
   524 				monitor.done();
       
   525 			}
       
   526 		}
       
   527 		return errorCode;
       
   528 	}
       
   529 
       
   530 	/**
       
   531 	 * Parses mmp file from the entered path
       
   532 	 * @param fileLocation MMP file location
       
   533 	 * @param sbsBuild IS sbsv2 build system activated
       
   534 	 * @return MMP file name without path
       
   535 	 */
       
   536 	public String getMMPFileName(IPath fileLocation, boolean sbsBuild)
       
   537 	{
       
   538 		// because mmp file contains project related path
       
   539 		// we need to parse mmp file name
       
   540 		int index = Util.getLastSlashIndex(fileLocation.toString());
       
   541 		String mmpFileName = null;
       
   542 		if (index == -1) {
       
   543 			// mmp file is incorrect => skip this file
       
   544 			return mmpFileName;
       
   545 		}
       
   546 
       
   547 		// parse mmp file name
       
   548 		mmpFileName = fileLocation.toString().substring(
       
   549 				index + 1, fileLocation.toString().length());
       
   550 
       
   551 		//if not using the SBS2 builds => nees to remove mmp file name extension
       
   552 		if( !sbsBuild && mmpFileName.endsWith(".mmp")) {
       
   553 			mmpFileName = mmpFileName.substring(0, mmpFileName.length()-4);
       
   554 		}
       
   555 		return mmpFileName;
       
   556 	}
       
   557 
       
   558 
       
   559 	/**
       
   560 	 * Runs AnalyzeTool build.
       
   561 	 *
       
   562 	 * @param type
       
   563 	 *            Type of execution. Possible types ATOOL_INST or ATOOL_UNINST
       
   564 	 * @param monitor
       
   565 	 *            Progress monitor reference
       
   566 	 * @param cpi
       
   567 	 *            ICarbideProjectInfo reference
       
   568 	 * @param files
       
   569 	 *            List of user selected mmp files
       
   570 	 * @return True is no errors otherwise False
       
   571 	 */
       
   572 	protected final boolean runBuild(final String type,
       
   573 			final IProgressMonitor monitor, final ICarbideProjectInfo cpi,
       
   574 			final List<IFile> files) {
       
   575 
       
   576 		// check AnalyzeTool libraries if the AnalyzeTool libraries missing =>
       
   577 		// creates error marker for the project and cancels build
       
   578 		if( !Util.checkAtoolLibs(cpi) )
       
   579 		{
       
   580 			buildCancelled(monitor, false);
       
   581 			return false;
       
   582 		}
       
   583 
       
   584 		// get preference store
       
   585 		IPreferenceStore store = Activator.getPreferences();
       
   586 
       
   587 		// get active logging mode
       
   588 		String loggingMode = store.getString(Constants.LOGGING_MODE);
       
   589 		String s60FileNameMode = store.getString(Constants.S60_LOG_FILE_MODE);
       
   590 		verbose = store.getBoolean(Constants.ATOOL_VERBOSE);
       
   591 
       
   592 		// possible error code from command line engine
       
   593 		int errorCode = Constants.COMMAND_LINE_ERROR_CODE.OK.getCode();
       
   594 
       
   595 		// if logging mode is set to "ask always"
       
   596 		// ask for user used logging mode
       
   597 		if (Constants.LOGGING_ASK_ALLWAYS.equals(loggingMode)) {
       
   598 			String modeSelection = askLoggingMode();
       
   599 
       
   600 			// user press "Cancel"
       
   601 			if (modeSelection == null || ("").equals(modeSelection)) {
       
   602 				buildCancelled(monitor, true);
       
   603 				return false;
       
   604 			}
       
   605 			// if user selects S60 log file and mode
       
   606 			// and data file must ask for user
       
   607 			else if (Constants.LOGGING_S60.equals(modeSelection)
       
   608 					&& Constants.LOGGING_S60_USER_SPECIFIED
       
   609 							.equals(s60FileNameMode)) {
       
   610 				errorCode = askFileNameAndBuild(monitor, cpi, files);
       
   611 			} else { // no need to ask data file for the user => just build
       
   612 						// with
       
   613 				// user selected logging mode
       
   614 				errorCode = executeAtool(null, type, modeSelection, files, cpi, monitor);
       
   615 			}
       
   616 
       
   617 		}
       
   618 		// if used logging mode is s60
       
   619 		// and data file name must ask for the user
       
   620 		else if (Constants.LOGGING_S60.equals(loggingMode)
       
   621 				&& Constants.LOGGING_S60_USER_SPECIFIED.equals(s60FileNameMode)) {
       
   622 			errorCode = askFileNameAndBuild(monitor, cpi, files);
       
   623 		}
       
   624 		// build with selected mode
       
   625 		else {
       
   626 			errorCode = executeAtool(null, type, loggingMode, files, cpi, monitor);
       
   627 		}
       
   628 
       
   629 
       
   630 		// no errors from command line engine
       
   631 		if( errorCode == Constants.COMMAND_LINE_ERROR_CODE.OK.getCode() ) {
       
   632 			return true;
       
   633 		}
       
   634 
       
   635 		// if some error code is returned from command line engine display it to user
       
   636 		// and cancel build
       
   637 		Constants.COMMAND_LINE_ERROR_CODE error = Util.getErrorCode(errorCode);
       
   638 		Util.displayCommandLineError(error);
       
   639 		buildCancelled(monitor, false);
       
   640 		return false;
       
   641 	}
       
   642 
       
   643 	/**
       
   644 	 * After the built is finished set preferences back to normal.
       
   645 	 * @param cpi ICarbideProjectInfo refernece
       
   646 	 */
       
   647 	public final void runPostSteps(ICarbideProjectInfo cpi) {
       
   648 
       
   649 		IPreferenceStore store = Activator.getPreferences();
       
   650 		boolean keepFilesSync = store.getBoolean(Constants.PREFS_KEEP_IN_SYNC);
       
   651 		boolean promptMPPChange = store.getBoolean(Constants.PREFS_PROMPT_MMP);
       
   652 		boolean manageDeps = store.getBoolean(Constants.PREFS_MANAGE_DEPS);
       
   653 		boolean useConcBuild = store.getBoolean(Constants.PREFS_CONC_BUILD);
       
   654 
       
   655 		// try to load ProjectUIPlugin class
       
   656 		try {
       
   657 			Class.forName("com.nokia.carbide.cpp.project.ui.utils.ProjectUIUtils");
       
   658 
       
   659 			// no need to keep project files in sync
       
   660 			// this should fix the mmp selection dialog prompt
       
   661 			com.nokia.carbide.cpp.project.ui.utils.ProjectUIUtils
       
   662 					.setKeepProjectsInSync(keepFilesSync);
       
   663 		} catch (ClassNotFoundException cnte) {
       
   664 			//Do nothing by design
       
   665 		}
       
   666 
       
   667 		// set builder preference to not prompt mmp file change dialog
       
   668 		IPreferenceStore cStore = CarbideBuilderPlugin.getDefault()
       
   669 				.getPreferenceStore();
       
   670 
       
   671 
       
   672 		// add existing/default values of Carbide builder settings
       
   673 		cStore
       
   674 				.setValue(
       
   675 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MMP_CHANGED_ACTION_PROMPT,
       
   676 						promptMPPChange);
       
   677 		cStore
       
   678 				.setValue(
       
   679 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MANAGE_DEPENDENCIES,
       
   680 						manageDeps);
       
   681 		cStore
       
   682 				.setValue(
       
   683 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_USE_CONCURRENT_BUILDING,
       
   684 						useConcBuild);
       
   685 
       
   686 
       
   687 		// try to remove -debug parameter from the build arguments list
       
   688 		try {
       
   689 			//get build configuration
       
   690 			ICarbideBuildConfiguration config = cpi.getDefaultConfiguration();
       
   691 
       
   692 			//get build arguments info
       
   693 			BuildArgumentsInfo info = config.getBuildArgumentsInfoCopy();
       
   694 
       
   695 			//get used platform
       
   696 			String platform = config.getPlatformString();
       
   697 
       
   698 			String debug = "-debug";
       
   699 			String abldArgs = info.abldBuildArgs;
       
   700 
       
   701 			//if platform is set to ARMV5 or GCCE
       
   702 			if ( platform.contains(Constants.BUILD_TARGET_ARMV5) || platform.contains(Constants.BUILD_TARGET_GCEE)) {
       
   703 
       
   704 				//get "-debug" string index
       
   705 				int index = abldArgs.indexOf(debug);
       
   706 
       
   707 				//if abld build arguments contains only "-debug" parameter
       
   708 				if( abldArgs.equals(debug) ) {
       
   709 					info.abldBuildArgs = "";
       
   710 					config.setBuildArgumentsInfo(info);
       
   711 				}
       
   712 				//remove just "-debug" word
       
   713 				else if( abldArgs.contains(debug) && index != -1 ) {
       
   714 					info.abldBuildArgs = abldArgs.substring(0,index) + abldArgs.substring(index+debug.length(), abldArgs.length());
       
   715 					config.setBuildArgumentsInfo(info);
       
   716 				}
       
   717 			}
       
   718 
       
   719 		}catch( java.lang.NoSuchMethodError nsme ) {
       
   720 			//Do nothing by design
       
   721 			//user might run AT with too old Carbide version
       
   722 		}catch( Exception e ) {
       
   723 			e.printStackTrace();
       
   724 		}
       
   725 
       
   726 		//after the build is finished => open the console view
       
   727 		Util.openConsoleView();
       
   728 	}
       
   729 
       
   730 	/**
       
   731 	 * Checks is atool.exe available and modifies Carbide preferences.
       
   732 	 *
       
   733 	 * @param launcher
       
   734 	 *            Command launcher
       
   735 	 * @param monitor
       
   736 	 *            Progress monitor
       
   737 	 * @param cpi
       
   738 	 *            Carbide project info
       
   739 	 * @return True is all checks are OK, otherwise False
       
   740 	 */
       
   741 	protected final boolean runPreSteps(final CarbideCommandLauncher launcher,
       
   742 			final IProgressMonitor monitor, final ICarbideProjectInfo cpi) {
       
   743 
       
   744 
       
   745 		// set command launchers
       
   746 		cmdLauncher = launcher;
       
   747 
       
   748 		// check is atool.exe available
       
   749 		if (!Util.isAtoolAvailable()) {
       
   750 			launcher.writeToConsole(Constants.INFO_ATOOL_NOT_AVAILABLE);
       
   751 			buildCancelled(monitor, false);
       
   752 			Util.showErrorMessage(Constants.ERROR_ATOOL_NOT_AVAILABLE);
       
   753 			return false;
       
   754 		}
       
   755 		
       
   756 		// check AnalyzeTool version, 1.6.0 and forward versions is supported
       
   757 		int compared = Util.compareVersionNumber(Util.getAtoolVersionNumber(Util.getAtoolInstallFolder()), Constants.MIN_VERSION);
       
   758 		if( compared == Constants.VERSION_NUMBERS_SECOND || compared == Constants.VERSION_NUMBERS_INVALID ) {
       
   759 			buildCancelled(monitor, false);
       
   760 			Util.showMessage(Constants.TOO_OLD_ENGINE);
       
   761 			return false;
       
   762 		}
       
   763 
       
   764 		// remove existing error markers
       
   765 		try {
       
   766 			CarbideCPPBuilder.removeAllMarkers(cpi.getProject());
       
   767 		}catch (CoreException ce) {
       
   768 			ce.printStackTrace();
       
   769 			return false;
       
   770 		}
       
   771 		
       
   772 		// check used platform
       
   773 		if (!checkPlatform(cpi)) {
       
   774 			buildCancelled(monitor, Util
       
   775 					.openConfirmationDialog(Constants.PLATFORM_NOT_SUPPORTED));
       
   776 			return false;
       
   777 		}
       
   778 
       
   779 		// check AnalyzeTool libraries if the AnalyzeTool libraries missing =>
       
   780 		// creates error marker for the project and cancels build
       
   781 		if( !Util.checkAtoolLibs(cpi) )
       
   782 		{
       
   783 			PlatformUI.getWorkbench().getDisplay().syncExec( new Runnable() {
       
   784 				public void run() {
       
   785 					// show error message for the user
       
   786 					Util.showMessage(Constants.CAN_NOT_FIND_LIBRARIES);
       
   787 				}
       
   788 			});
       
   789 			buildCancelled(monitor, false);
       
   790 			return false;
       
   791 		}
       
   792 
       
   793 		// use CarbideCPPBuilder to bldmake bldfiles for selected project
       
   794 		String[] bldmakeArgs = { "bldfiles" };
       
   795 		CarbideCPPBuilder.invokeBldmakeCommand(cpi.getDefaultConfiguration(),
       
   796 				launcher, bldmakeArgs, true);
       
   797 
       
   798 		IPreferenceStore store = Activator.getPreferences();
       
   799 
       
   800 		// try to load ProjectUIPlugin class
       
   801 		try {
       
   802 			Class.forName("com.nokia.carbide.cpp.project.ui.ProjectUIPlugin");
       
   803 
       
   804 			// get keep project files in sync flag
       
   805 			boolean keepFilesSync = com.nokia.carbide.cpp.project.ui.utils.ProjectUIUtils
       
   806 					.keepProjectsInSync();
       
   807 
       
   808 			store.setValue(Constants.PREFS_KEEP_IN_SYNC, keepFilesSync);
       
   809 
       
   810 			// no need to keep project files in sync
       
   811 			// this should fix the mmp selection dialog prompt
       
   812 			com.nokia.carbide.cpp.project.ui.utils.ProjectUIUtils
       
   813 					.setKeepProjectsInSync(false);
       
   814 		} catch (ClassNotFoundException cnfe) {
       
   815 			//Do nothing by design
       
   816 		}
       
   817 
       
   818 		// set builder preference to not prompt mmp file change dialog
       
   819 		IPreferenceStore cStore = CarbideBuilderPlugin.getDefault()
       
   820 				.getPreferenceStore();
       
   821 
       
   822 		// get existing values
       
   823 		boolean promptMPPChange = cStore
       
   824 				.getBoolean(com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MMP_CHANGED_ACTION_PROMPT);
       
   825 		boolean manageDeps = cStore
       
   826 				.getBoolean(com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MANAGE_DEPENDENCIES);
       
   827 		boolean useConcBuild = cStore
       
   828 				.getBoolean(com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_USE_CONCURRENT_BUILDING);
       
   829 
       
   830 		//store existing values
       
   831 		store.setValue(Constants.PREFS_PROMPT_MMP, promptMPPChange);
       
   832 		store.setValue(Constants.PREFS_MANAGE_DEPS, manageDeps);
       
   833 		store.setValue(Constants.PREFS_CONC_BUILD, useConcBuild);
       
   834 		// add custom values for AnalyzeTool build
       
   835 		cStore
       
   836 				.setValue(
       
   837 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MMP_CHANGED_ACTION_PROMPT,
       
   838 						false);
       
   839 		cStore
       
   840 				.setValue(
       
   841 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_MANAGE_DEPENDENCIES,
       
   842 						false);
       
   843 		cStore
       
   844 				.setValue(
       
   845 						com.nokia.carbide.cdt.builder.BuilderPreferenceConstants.PREF_USE_CONCURRENT_BUILDING,
       
   846 						false);
       
   847 		
       
   848 		// try to add -debug parameter to the build arguments list
       
   849 		try {
       
   850 			//get selected build configuration
       
   851 			ICarbideBuildConfiguration config = cpi.getDefaultConfiguration();
       
   852 
       
   853 			//get build arguments info
       
   854 			BuildArgumentsInfo info = config.getBuildArgumentsInfoCopy();
       
   855 
       
   856 			//get selected platform
       
   857 			String platform = cpi.getDefaultConfiguration().getPlatformString();
       
   858 
       
   859 			//if platform is ARMV5 or GCCE and the "-debug" parameter is not set => we need set the parameter
       
   860 			if ( (platform.contains(Constants.BUILD_TARGET_ARMV5) || platform.contains(Constants.BUILD_TARGET_GCEE)) && !info.abldBuildArgs.contains("-debug") ) {
       
   861 				if( info.abldBuildArgs.length() > 0 && !info.abldBuildArgs.endsWith(" ") ) {
       
   862 					info.abldBuildArgs += " ";
       
   863 				}
       
   864 				info.abldBuildArgs += "-debug";
       
   865 				config.setBuildArgumentsInfo(info);
       
   866 			}
       
   867 
       
   868 		}
       
   869 		//catch NoSuchMethodError because is it possible to use older versions where this method is not available
       
   870 		catch( java.lang.NoSuchMethodError nsme)
       
   871 		{
       
   872 			//Do nothing by design
       
   873 		}
       
   874 
       
   875 		return true;
       
   876 	}
       
   877 
       
   878 	/**
       
   879 	 * Executes uninstrument for the project.
       
   880 	 * @param command Uninstrument command
       
   881 	 * @param cpi ICarbideProjectInfo reference
       
   882 	 * @param monitor IProgressMonitor reference
       
   883 	 */
       
   884 	public void runUninstrument(String command, ICarbideProjectInfo cpi, IProgressMonitor monitor)
       
   885 	{
       
   886 		cmdLauncher.showCommand(true);
       
   887 		String[] arguments = new String[1];
       
   888 	    arguments[0] = command;
       
   889 		int error = cmdLauncher.executeCommand(new Path(Util
       
   890 				.getAtoolInstallFolder()),arguments,
       
   891 				CarbideCPPBuilder.getResolvedEnvVars(cpi
       
   892 						.getDefaultConfiguration()), cpi
       
   893 					.getINFWorkingDirectory());
       
   894 		
       
   895 		// if some occurs => display it to user
       
   896 		if( error != Constants.COMMAND_LINE_ERROR_CODE.OK.getCode() ) {
       
   897 			Constants.COMMAND_LINE_ERROR_CODE errorCode = Util.getErrorCode(error);
       
   898 			Util.displayCommandLineError(errorCode);
       
   899 		}
       
   900 	}
       
   901 }