core/com.nokia.carbide.cpp.sdk.core/src/com/nokia/carbide/cpp/internal/api/sdk/SymbianBuildContext.java
changeset 1882 e8d8fb31697d
parent 1878 6507baa94ab8
parent 1881 fccb2e8f8b9b
child 1883 16954591245c
child 1886 d61921bcf8f3
equal deleted inserted replaced
1878:6507baa94ab8 1882:e8d8fb31697d
     1 /*
       
     2 * Copyright (c) 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 the License "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 */
       
    13 package com.nokia.carbide.cpp.internal.api.sdk;
       
    14 
       
    15 import java.io.File;
       
    16 import java.util.*;
       
    17 
       
    18 import org.eclipse.core.runtime.IPath;
       
    19 import org.osgi.framework.Version;
       
    20 
       
    21 import com.nokia.carbide.cpp.epoc.engine.preprocessor.*;
       
    22 import com.nokia.carbide.cpp.internal.sdk.core.model.SymbianMissingSDKFactory;
       
    23 import com.nokia.carbide.cpp.sdk.core.*;
       
    24 
       
    25 public class SymbianBuildContext implements ISymbianBuildContext {
       
    26 
       
    27 	private String sdkId;
       
    28 	private String platform;
       
    29 	private String target;
       
    30 	private String displayString = null;
       
    31 	private String sbsv2Alias = null;
       
    32 	
       
    33 	private static String EMULATOR_DISPLAY_TEXT = "Emulator"; //$NON-NLS-1$
       
    34 	private static String PHONE_DISPLAY_TEXT = "Phone"; //$NON-NLS-1$
       
    35 	private static String DEBUG_DISPLAY_TEXT = "Debug"; //$NON-NLS-1$
       
    36 	private static String RELEASE_DISPLAY_TEXT = "Release"; //$NON-NLS-1$
       
    37 	private static String SPACE_DISPLAY_TEXT = " "; //$NON-NLS-1$
       
    38 	private static String SDK_NOT_INSTALLED = "SDK not installed"; //$NON-NLS-1$
       
    39 	
       
    40 	// a copy of bad SDK default to fall back
       
    41 	private static ISymbianSDK fallbackForBadSdk = SymbianMissingSDKFactory.createInstance("dummy_ID"); //$NON-NLS-1$
       
    42 	
       
    43 	public SymbianBuildContext(ISymbianSDK theSDK, String thePlatform, String theTarget) {
       
    44 		sdkId = theSDK.getUniqueId();
       
    45 		platform = thePlatform.toUpperCase();
       
    46 		target = theTarget.toUpperCase();
       
    47 		
       
    48 		getDisplayString();
       
    49 	}
       
    50 	
       
    51 	public SymbianBuildContext(ISymbianSDK theSDK, String thePlatform, String theTarget, String theSBSv2Alias) {
       
    52 		sdkId = theSDK.getUniqueId();
       
    53 		platform = thePlatform.toUpperCase();
       
    54 		target = theTarget.toUpperCase();
       
    55 		sbsv2Alias = theSBSv2Alias;
       
    56 		
       
    57 		getDisplayString();
       
    58 	}
       
    59 
       
    60 	
       
    61 	@Override
       
    62 	public int hashCode() {
       
    63 		final int prime = 31;
       
    64 		int result = 1;
       
    65 		result = prime * result
       
    66 				+ ((platform == null) ? 0 : platform.hashCode());
       
    67 		result = prime * result + ((getSDK() == null) ? 0 : getSDK().getEPOCROOT().hashCode());
       
    68 		result = prime * result + ((target == null) ? 0 : target.hashCode());
       
    69 		return result;
       
    70 	}
       
    71 
       
    72 
       
    73 	@Override
       
    74 	public boolean equals(Object obj) {
       
    75 		if (this == obj)
       
    76 			return true;
       
    77 		if (obj == null)
       
    78 			return false;
       
    79 		if (!(obj instanceof SymbianBuildContext))
       
    80 			return false;
       
    81 		final SymbianBuildContext other = (SymbianBuildContext) obj;
       
    82 		if (platform == null) {
       
    83 			if (other.platform != null)
       
    84 				return false;
       
    85 		} else if (!platform.equals(other.platform))
       
    86 			return false;
       
    87 		if (getSDK() == null) {
       
    88 			if (other.getSDK() != null)
       
    89 				return false;
       
    90 		} else if (!getSDK().getEPOCROOT().equals(other.getSDK().getEPOCROOT()))
       
    91 			return false;
       
    92 		if (target == null) {
       
    93 			if (other.target != null)
       
    94 				return false;
       
    95 		} else if (!target.equals(other.target)) {
       
    96 			return false;
       
    97 		} else if (sbsv2Alias!= null && !sbsv2Alias.equals(other.sbsv2Alias)) {
       
    98 			return false;
       
    99 		}
       
   100 		return true;
       
   101 	}
       
   102 
       
   103 
       
   104 	public ISymbianSDK getSDK() {
       
   105 		
       
   106 		ISymbianSDK sdk = SDKCorePlugin.getSDKManager().getSDK(sdkId, true);
       
   107 		if (sdk == null){
       
   108 			sdk = fallbackForBadSdk;
       
   109 		}
       
   110 		
       
   111 		return sdk;
       
   112 	}
       
   113 
       
   114 	public String getPlatformString() {
       
   115 		return platform.toUpperCase();
       
   116 	}
       
   117 
       
   118 	public String getTargetString() {
       
   119 		return target.toUpperCase();
       
   120 	}
       
   121 
       
   122 	public String getDisplayString() {
       
   123 		if (displayString == null) {
       
   124 			// in the form Emulation Debug (WINSCW) [S60_3rd_MR] or
       
   125 			// Phone Release (GCCE) [S60_3rd_MR]
       
   126 			if (platform.compareTo(ISymbianBuildContext.EMULATOR_PLATFORM) == 0) {
       
   127 				displayString = EMULATOR_DISPLAY_TEXT;
       
   128 			} else {
       
   129 				displayString = PHONE_DISPLAY_TEXT;
       
   130 			}
       
   131 			
       
   132 			if (target.compareTo(ISymbianBuildContext.DEBUG_TARGET) == 0) {
       
   133 				displayString = displayString + SPACE_DISPLAY_TEXT + DEBUG_DISPLAY_TEXT;
       
   134 			} else {
       
   135 				displayString = displayString + SPACE_DISPLAY_TEXT + RELEASE_DISPLAY_TEXT;
       
   136 			}
       
   137 			
       
   138 			String basePlatform;
       
   139 			if (sbsv2Alias != null)
       
   140 				basePlatform = sbsv2Alias;
       
   141 			else
       
   142 				basePlatform = platform;
       
   143 			
       
   144 			displayString = displayString + " (" + basePlatform + ") [" + getSDK().getUniqueId() + "]"; //$NON-NLS-1$
       
   145 		}
       
   146 		return displayString;
       
   147 	}
       
   148 	
       
   149 	public static ISymbianBuildContext getBuildContextFromDisplayName(String displayName) {
       
   150 		if (isValidConfigName(displayName)) {
       
   151 			String sdkId = getSDKIDFromConfigName(displayName);
       
   152 			ISymbianSDK sdk = SDKCorePlugin.getSDKManager().getSDK(sdkId, true);
       
   153 			if (sdk == null) {
       
   154 				// add a dummy should a build context ask for a removed SDK
       
   155 				sdk = SDKManagerInternalAPI.addMissingSdk(sdkId);
       
   156 			}
       
   157 						
       
   158 			return new SymbianBuildContext(sdk, 
       
   159 						getPlatformFromBuildConfigName(displayName), 
       
   160 						getTargetFromBuildConfigName(displayName),
       
   161 						getSBSv2AliasFromConfigName(displayName));
       
   162 		}
       
   163 		return new SymbianBuildContext(fallbackForBadSdk, SDK_NOT_INSTALLED, SDK_NOT_INSTALLED);
       
   164 	}
       
   165 
       
   166 	/**
       
   167 	 * See if the build configuration is an SBSv2 alias, and if so get the build-able alias name 
       
   168 	 * @param displayName
       
   169 	 * @return The full SBSv2 alias that can be used with -c, otherwise null if not SBSv2
       
   170 	 */
       
   171 	private static String getSBSv2AliasFromConfigName(String displayName) {
       
   172 		int indexBegin = displayName.indexOf("(");  //$NON-NLS-1$
       
   173 		int indexEnd = displayName.indexOf(")");  //$NON-NLS-1$
       
   174 		if (indexBegin > 0 && indexEnd > 0){
       
   175 			String configPart =  displayName.substring(indexBegin+1, indexEnd);
       
   176 			if (configPart.split("_").length > 1){
       
   177 				return configPart;
       
   178 			}
       
   179 		} 
       
   180 		
       
   181 		return null;
       
   182 	}
       
   183 
       
   184 	private static String getPlatformFromBuildConfigName(String configName) {
       
   185 		String[] tokens = configName.split(SPACE_DISPLAY_TEXT);
       
   186 		String sdkIdToken = tokens[2];
       
   187 		if (sdkIdToken.contains("_")){
       
   188 			sdkIdToken = sdkIdToken.substring(1, sdkIdToken.length()-1);
       
   189 			String[] aliasTokens = sdkIdToken.split("_");
       
   190 			return aliasTokens[0];
       
   191 		} else {
       
   192 			return sdkIdToken.substring(1, sdkIdToken.length()-1);
       
   193 		}
       
   194 		
       
   195 	}
       
   196 
       
   197 	public static String getSDKIDFromConfigName(String configName) {
       
   198 		int indexBegin = configName.indexOf("[");  //$NON-NLS-1$
       
   199 		int indexEnd = configName.indexOf("]");  //$NON-NLS-1$
       
   200 		if (indexBegin > 0 && indexEnd > 0){
       
   201 			return configName.substring(indexBegin+1, indexEnd);
       
   202 		} else {
       
   203 			return ""; //$NON-NLS-1$
       
   204 		}
       
   205 	}
       
   206 
       
   207 	private static String getTargetFromBuildConfigName(String configName) {
       
   208 		String[] tokens = configName.split(SPACE_DISPLAY_TEXT);
       
   209 		if (tokens[1].compareTo(DEBUG_DISPLAY_TEXT) == 0) {
       
   210 			return ISymbianBuildContext.DEBUG_TARGET;
       
   211 		} else {
       
   212 			return ISymbianBuildContext.RELEASE_TARGET;
       
   213 		}
       
   214 	}
       
   215 
       
   216 	private static boolean isValidConfigName(String configName) {
       
   217 		// <Phone | Emulator> <Target> (<Platform>) [<SDK ID>]
       
   218 		if (configName != null && !configName.equals("")) { //$NON-NLS-1$
       
   219 			String[] tokens = configName.split(SPACE_DISPLAY_TEXT);
       
   220 			if (tokens.length >= 4) {
       
   221 				if (tokens[0].compareTo(EMULATOR_DISPLAY_TEXT) == 0 || tokens[0].compareTo(PHONE_DISPLAY_TEXT) == 0) {
       
   222 					if (tokens[1].compareTo(DEBUG_DISPLAY_TEXT) == 0 || tokens[1].compareTo(RELEASE_DISPLAY_TEXT) == 0) {
       
   223 						if (tokens[2].matches("(.*)")) { //$NON-NLS-1$
       
   224 							if (tokens[3].matches("\\[.*")) { //$NON-NLS-1$
       
   225 								return true;
       
   226 							}
       
   227 						}
       
   228 					}
       
   229 				}
       
   230 			}
       
   231 		}
       
   232 		return false;
       
   233 	}
       
   234 	
       
   235 	public String toString() {
       
   236 		return getDisplayString();
       
   237 	}
       
   238 	
       
   239 	public String getDefaultDefFileDirectoryName(boolean isASSP) {
       
   240 		// TODO: How the ASSP option affects the path?
       
   241 
       
   242 		String dirName = getDefFileDirectoryNameForPlatform(platform);
       
   243 		if (dirName == null) {
       
   244 			// check BSF's
       
   245 			IBSFCatalog catalog = getSDK().getBSFCatalog();
       
   246 	    	if (catalog != null) {
       
   247 	    		for (IBSFPlatform plat : catalog.getPlatforms()) {
       
   248 	    			if (plat.getName().compareToIgnoreCase(platform) == 0) {
       
   249 	    				String mainPlatform = catalog.getReleasePlatform(platform);
       
   250 	    				if (mainPlatform != null) {
       
   251 	    					dirName = getDefFileDirectoryNameForPlatform(mainPlatform);
       
   252 	    					if (dirName == null || dirName.length() < 1) {
       
   253 	    						// fallback - all BSF's should be EABI anyway
       
   254 			    				return "EABI"; //$NON-NLS-1$
       
   255 	    					}
       
   256 	    				}
       
   257 	    			}
       
   258 	    		}
       
   259 	    	}
       
   260 		}
       
   261 		
       
   262 		if (dirName == null) {
       
   263 			// fallback for unknown cases
       
   264 			dirName = platform;
       
   265 		}
       
   266 		
       
   267 		return dirName;
       
   268 	}
       
   269 	
       
   270 	private String getDefFileDirectoryNameForPlatform(String platform) {
       
   271 		if (platform.equals(EMULATOR_PLATFORM)) {
       
   272 			return "BWINS"; //$NON-NLS-1$
       
   273 		} else if (platform.equals(ARMV5_PLATFORM)
       
   274 					|| platform.equals(ARMV5_ABIV2_PLATFORM)
       
   275 					|| platform.equals(ARMV6_PLATFORM)
       
   276 					|| platform.equals(ARMV6_ABIV2_PLATFORM)
       
   277 					|| platform.equals(GCCE_PLATFORM)) {
       
   278 			return "EABI"; //$NON-NLS-1$
       
   279 		}
       
   280 		return null;
       
   281 	}
       
   282 
       
   283 	public IPath getCompilerPrefixFile() {
       
   284 		if (platform.equals(GCCE_PLATFORM) || 
       
   285 				(sbsv2Alias != null && sbsv2Alias.toUpperCase().contains(GCCE_PLATFORM))) {
       
   286 			return getGCCEPrefixFilePath();
       
   287 		} else if (platform.equals(ARMV5_PLATFORM)
       
   288 					|| platform.equals(ARMV5_ABIV2_PLATFORM)
       
   289 					|| platform.equals(ARMV6_PLATFORM)
       
   290 					|| platform.equals(ARMV6_ABIV2_PLATFORM)) {
       
   291 			return getRVCTPrefixFilePath();
       
   292 		} else {
       
   293 			// check BSF's
       
   294 			IBSFCatalog catalog = getSDK().getBSFCatalog();
       
   295 	    	if (catalog != null) {
       
   296 	    		for (IBSFPlatform plat : catalog.getPlatforms()) {
       
   297 	    			if (plat.getName().compareToIgnoreCase(platform) == 0) {
       
   298 	    				String mainPlatform = catalog.getReleasePlatform(platform);
       
   299 	    				if (mainPlatform != null) {
       
   300 	    					if (mainPlatform.equals(GCCE_PLATFORM)) {
       
   301 	    						return getGCCEPrefixFilePath();
       
   302 	    					} else if (mainPlatform.equals(ARMV5_PLATFORM) 
       
   303 	    								|| mainPlatform.equals(ARMV5_ABIV2_PLATFORM)
       
   304 	    								|| mainPlatform.equals(ARMV6_PLATFORM)
       
   305 	    								|| mainPlatform.equals(ARMV6_ABIV2_PLATFORM)) {
       
   306 	    						return getRVCTPrefixFilePath();
       
   307 	    					} else {
       
   308 	    						// fallback - all BSF's should be EABI anyway
       
   309 	    						return getRVCTPrefixFilePath();
       
   310 	    					}
       
   311 	    				}
       
   312 	    			}
       
   313 	    		}
       
   314 	    	}
       
   315 		}
       
   316 
       
   317 		// fallback for WINSCW, MSVC, etc.
       
   318 		return null;
       
   319 	}
       
   320 
       
   321 	private IPath getGCCEPrefixFilePath() {
       
   322 		return getSDK().getIncludePath().append("gcce/gcce.h"); //$NON-NLS-1$
       
   323 	}
       
   324 
       
   325 	private IPath getRVCTPrefixFilePath() {
       
   326 		IRVCTToolChainInfo[] installedRVCTTools = SDKCorePlugin.getSDKManager().getInstalledRVCTTools();
       
   327 		// use default in case tools aren't installed
       
   328 		String rvctFragment = "rvct2_2"; //$NON-NLS-1$
       
   329 		if (installedRVCTTools.length > 0) {
       
   330 			rvctFragment = getRVCTFragment(installedRVCTTools[0]);
       
   331 		}
       
   332 		IPath prefixFilePath = getSDK().getIncludePath().append(rvctFragment).append(rvctFragment + ".h"); //$NON-NLS-1$
       
   333 		if (prefixFilePath.toFile().exists()){
       
   334 			return prefixFilePath;
       
   335 		} else {
       
   336 			// SF kits around SF^3 started to only use a single rvct.h header instead of specific versioned ones
       
   337 			// based on the default installation
       
   338 			return getSDK().getIncludePath().append("rvct").append("rvct" + ".h");
       
   339 		}
       
   340 	}
       
   341 
       
   342 	private String getRVCTFragment(IRVCTToolChainInfo info) {
       
   343 		int major = 0, minor = 0;
       
   344 		if (info != null) {
       
   345 			Version rvctToolsVersion = info.getRvctToolsVersion();
       
   346 			if (rvctToolsVersion != null) {
       
   347 				major = info.getRvctToolsVersion().getMajor();
       
   348 				minor = info.getRvctToolsVersion().getMinor();
       
   349 			}
       
   350 		}
       
   351 		return "rvct" + major + "_" + minor; //$NON-NLS-1$ //$NON-NLS-2$
       
   352 	}
       
   353 
       
   354 	public List<IDefine> getVariantHRHDefines() {
       
   355 
       
   356 		return getCachedData().getVariantHRHDefines();
       
   357 	}
       
   358 
       
   359 	public List<File> getPrefixFileIncludes() {
       
   360 		return getCachedData().getPrefixFileIncludes();
       
   361 	}
       
   362 
       
   363 
       
   364 	public List<IDefine> getCompilerMacros() {
       
   365 		// we parse the compiler prefix file to gather macros.  this can be time consuming so do it
       
   366 		// once and cache the values.  only reset the cache when the compiler prefix has changed.
       
   367 		
       
   368 		IPath prefixFile = getCompilerPrefixFile();
       
   369 		if (prefixFile == null) {
       
   370 			return Collections.emptyList();
       
   371 		}
       
   372 		
       
   373 		return getCachedData().getCompilerMacros(prefixFile);
       
   374 	}
       
   375 
       
   376 
       
   377 	public String getBuildVariationName() {
       
   378 		String varName = "";
       
   379 		
       
   380 		String[] tokens = getPlatformString().split("\\.");
       
   381 		if (tokens.length == 2){
       
   382 			varName = tokens[1];
       
   383 		}
       
   384 		
       
   385 		return varName;
       
   386 	}
       
   387 
       
   388 
       
   389 	public boolean isSymbianBinaryVariation() {
       
   390 		if (getPlatformString().split("\\.").length == 2){
       
   391 			return true;
       
   392 		} else {
       
   393 			return false;
       
   394 		}
       
   395 	}
       
   396 
       
   397 	/**
       
   398 	 * Get the cache holding the data that applies to this build context globally.
       
   399 	 * A build context is subclassed by CarbideBuildConfiguration, which has multiple
       
   400 	 * instances at runtime, thus, a SymbianBuildContext instance should not hold a cache itself.
       
   401 	 * @return cache, never <code>null</code>
       
   402 	 */
       
   403 	private SymbianBuildContextDataCache getCachedData() {
       
   404 		return SymbianBuildContextDataCache.getCache(this);
       
   405 	}
       
   406 	
       
   407 
       
   408 	public String getBasePlatformForVariation() {
       
   409 		String plat = "";
       
   410 		
       
   411 		String[] tokens = getPlatformString().split("\\.");
       
   412 		if (tokens.length == 2){
       
   413 			plat = tokens[0];
       
   414 		} else {
       
   415 			return platform;
       
   416 		}
       
   417 		
       
   418 		return plat;
       
   419 	}
       
   420 
       
   421 
       
   422 	/**
       
   423 	 * Get the list of #include paths detected for this context.
       
   424 	 * @return List or <code>null</code>
       
   425 	 */
       
   426 	public List<File> getCachedSystemIncludePaths() {
       
   427 		return getCachedData().getSystemIncludePaths();
       
   428 	}
       
   429 
       
   430 	public String getSBSv2Alias() {
       
   431 		return sbsv2Alias;
       
   432 	}
       
   433 }