summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java')
-rw-r--r--plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java291
1 files changed, 291 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java
new file mode 100644
index 0000000..714470b
--- /dev/null
+++ b/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java
@@ -0,0 +1,291 @@
1/*******************************************************************************
2 * Copyright (c) 2010 Intel Corporation.
3 * Copyright (c) 2013 BMW Car IT GmbH.
4 *
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * Intel - initial API and implementation
12 * BMW Car IT - include error and advice messages with check results
13 *******************************************************************************/
14package org.yocto.sdk.ide;
15
16import java.io.BufferedReader;
17import java.io.File;
18import java.io.FileReader;
19import java.io.FilenameFilter;
20import java.io.IOException;
21import java.io.InputStream;
22import java.io.InputStreamReader;
23
24import org.eclipse.jface.preference.IPreferenceStore;
25import org.yocto.sdk.ide.natures.YoctoSDKProjectNature;
26import org.yocto.sdk.ide.utils.YoctoSDKUtils;
27import org.yocto.sdk.ide.utils.YoctoSDKUtilsConstants;
28
29public class YoctoSDKChecker {
30 private static final String[] saInvalidVer = {"1.0", "0.9", "0.9+"};
31 private static final String SYSROOTS_DIR = "sysroots";
32 private static final String SDK_VERSION = "OECORE_SDK_VERSION";
33
34 public static enum SDKCheckResults {
35 SDK_PASS("", false),
36 TOOLCHAIN_LOCATION_EMPTY(
37 "Poky.SDK.Location.Empty", true),
38 TOOLCHAIN_LOCATION_NONEXIST(
39 "Poky.SDK.Location.Nonexist", true),
40 SDK_TARGET_EMPTY(
41 "Poky.SDK.Target.Empty", true),
42 SDK_NO_TARGET_SELECTED(
43 "Poky.SDK.No.Target.Selected", false),
44 SYSROOT_EMPTY(
45 "Poky.Sysroot.Empty", true),
46 SYSROOT_NONEXIST(
47 "Poky.Sysroot.Nonexist", true),
48 QEMU_KERNEL_EMPTY(
49 "Poky.Qemu.Kernel.Empty", true),
50 QEMU_KERNEL_NONEXIST(
51 "Poky.Qemu.Kernel.Nonexist", true),
52 WRONG_ADT_VERSION(
53 "Poky.ADT.Sysroot.Wrongversion", false),
54 ENV_SETUP_SCRIPT_NONEXIST(
55 "Poky.Env.Script.Nonexist", false),
56 TOOLCHAIN_NO_SYSROOT(
57 "Poky.Toolchain.No.Sysroot", false),
58 TOOLCHAIN_HOST_MISMATCH(
59 "Poky.Toolchain.Host.Mismatch", false);
60
61 private static final String DEFAULT_ADVICE = "Default.Advice";
62 private static final String ADVICE_SUFFIX = ".Advice";
63
64 private final String messageID;
65 private final boolean addDefaultAdvice;
66
67 private SDKCheckResults(final String messageID, final boolean addDefaultAdvice) {
68 this.messageID = messageID;
69 this.addDefaultAdvice = addDefaultAdvice;
70 }
71
72 public String getMessage() {
73 return YoctoSDKMessages.getString(messageID);
74 }
75
76 public String getAdvice() {
77 String advice = YoctoSDKMessages.getString(messageID + ADVICE_SUFFIX);
78
79 if (addDefaultAdvice) {
80 advice += YoctoSDKMessages.getString(DEFAULT_ADVICE);
81 }
82
83 return advice;
84 }
85 };
86
87 public static enum SDKCheckRequestFrom {
88 Wizard("Poky.SDK.Error.Origin.Wizard"),
89 Menu("Poky.SDK.Error.Origin.Menu"),
90 Preferences("Poky.SDK.Error.Origin.Preferences"),
91 Other("Poky.SDK.Error.Origin.Other");
92
93 private final String errorMessageID;
94
95 private SDKCheckRequestFrom(final String errorMessageID) {
96 this.errorMessageID = errorMessageID;
97 }
98
99 public String getErrorMessage() {
100 return YoctoSDKMessages.getString(errorMessageID);
101 }
102 };
103
104 public static void checkIfGloballySelectedYoctoProfileIsValid() throws YoctoGeneralException {
105 YoctoProfileElement profileElement = YoctoSDKUtils.getProfilesFromDefaultStore();
106 IPreferenceStore selectedProfileStore = YoctoSDKPlugin.getProfilePreferenceStore(profileElement.getSelectedProfile());
107 YoctoUIElement elem = YoctoSDKUtils.getElemFromStore(selectedProfileStore);
108
109 SDKCheckResults result = checkYoctoSDK(elem);
110 if (result != SDKCheckResults.SDK_PASS){
111 String strErrorMsg = getErrorMessage(result, SDKCheckRequestFrom.Wizard);
112 throw new YoctoGeneralException(strErrorMsg);
113 }
114 }
115
116 public static SDKCheckResults checkYoctoSDK(YoctoUIElement elem) {
117 if (elem.getStrToolChainRoot().isEmpty())
118 return SDKCheckResults.TOOLCHAIN_LOCATION_EMPTY;
119 else {
120 File fToolChain = new File(elem.getStrToolChainRoot());
121 if (!fToolChain.exists())
122 return SDKCheckResults.TOOLCHAIN_LOCATION_NONEXIST;
123 }
124
125 if (elem.getStrSysrootLoc().isEmpty()) {
126 return SDKCheckResults.SYSROOT_EMPTY;
127 } else {
128 File fSysroot = new File(elem.getStrSysrootLoc());
129 if (!fSysroot.exists())
130 return SDKCheckResults.SYSROOT_NONEXIST;
131 }
132
133 if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_SDK_MODE) {
134 //Check for SDK compatible with the host arch
135 String platform = getPlatformArch();
136 String sysroot_dir_str = elem.getStrToolChainRoot() + "/" + SYSROOTS_DIR;
137 File sysroot_dir = new File(sysroot_dir_str);
138 if (!sysroot_dir.exists())
139 return SDKCheckResults.TOOLCHAIN_NO_SYSROOT;
140
141 String toolchain_host_arch = null;
142
143 try {
144 toolchain_host_arch = findHostArch(sysroot_dir);
145 } catch(NullPointerException e) {
146 return SDKCheckResults.TOOLCHAIN_NO_SYSROOT;
147 }
148
149 if (!toolchain_host_arch.equalsIgnoreCase(platform)) {
150 if (!platform.matches("i\\d86") || !toolchain_host_arch.matches("i\\d86"))
151 return SDKCheckResults.TOOLCHAIN_HOST_MISMATCH;
152 }
153 }
154
155 if (elem.getStrTarget().isEmpty() && elem.getStrTargetsArray().length > 0) {
156 return SDKCheckResults.SDK_NO_TARGET_SELECTED;
157 }
158
159 if (elem.getIntTargetIndex() < 0 || elem.getStrTarget().isEmpty()) {
160 //if this is poky tree mode, prompt user whether bitbake meta-ide-support is executed?
161 if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_TREE_MODE)
162 return SDKCheckResults.ENV_SETUP_SCRIPT_NONEXIST;
163 else
164 return SDKCheckResults.SDK_TARGET_EMPTY;
165 } else {
166 String sFileName;
167
168 if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_SDK_MODE) {
169 sFileName = elem.getStrToolChainRoot()+"/" + YoctoSDKUtilsConstants.DEFAULT_ENV_FILE_PREFIX + elem.getStrTarget();
170 } else {
171 //POKY TREE Mode
172 sFileName = elem.getStrToolChainRoot() + YoctoSDKUtilsConstants.DEFAULT_TMP_PREFIX +
173 YoctoSDKUtilsConstants.DEFAULT_ENV_FILE_PREFIX + elem.getStrTarget();
174 }
175
176 try {
177 File file = new File(sFileName);
178 boolean bVersion = false;
179
180 if (file.exists()) {
181 BufferedReader input = new BufferedReader(new FileReader(file));
182
183 try {
184 String line = null;
185
186 while ((line = input.readLine()) != null) {
187 if (line.startsWith("export "+ SDK_VERSION)) {
188 int beginIndex = 2;
189 String sVersion = "";
190 for (;;) {
191 char cValue = line.charAt(line.indexOf('=') + beginIndex++);
192
193 if ((cValue != '.') && (!Character.isDigit(cValue)) && (cValue != '+'))
194 break;
195 else
196 sVersion += String.valueOf(cValue);
197 }
198
199 for (int i = 0; i < saInvalidVer.length; i++) {
200 if (!sVersion.equals(saInvalidVer[i])) {
201 bVersion = true;
202 break;
203 }
204 }
205
206 break;
207 }
208 }
209 } finally {
210 input.close();
211 }
212
213 if (!bVersion)
214 return SDKCheckResults.WRONG_ADT_VERSION;
215 }
216 } catch (IOException e) {
217 e.printStackTrace();
218 }
219 }
220
221 if (elem.getEnumDeviceMode() == YoctoUIElement.DeviceMode.QEMU_MODE) {
222 if (elem.getStrQemuKernelLoc().isEmpty()) {
223 return SDKCheckResults.QEMU_KERNEL_EMPTY;
224 } else {
225 File fQemuKernel = new File(elem.getStrQemuKernelLoc());
226 if (!fQemuKernel.exists())
227 return SDKCheckResults.QEMU_KERNEL_NONEXIST;
228 }
229 }
230
231 return SDKCheckResults.SDK_PASS;
232 }
233
234 public static String getErrorMessage(SDKCheckResults result, SDKCheckRequestFrom from) {
235 String strErrorMsg;
236 strErrorMsg = from.getErrorMessage();
237 strErrorMsg += "\n" + result.getMessage();
238 strErrorMsg += "\n" + result.getAdvice();
239
240 return strErrorMsg;
241 }
242
243 private static String getPlatformArch() {
244 String value = null;
245 try
246 {
247 Runtime rt = Runtime.getRuntime();
248 Process proc = rt.exec("uname -m");
249 InputStream stdin = proc.getInputStream();
250 InputStreamReader isr = new InputStreamReader(stdin);
251 BufferedReader br = new BufferedReader(isr);
252 String line = null;
253
254 while ( (line = br.readLine()) != null) {
255 value = line;
256 }
257 proc.waitFor();
258
259 } catch (Throwable t) {
260 t.printStackTrace();
261 }
262 return value;
263 }
264
265 private static String findHostArch(File sysroot_dir) {
266 FilenameFilter nativeFilter = new FilenameFilter() {
267 public boolean accept(File dir, String name) {
268 if (name.endsWith("sdk-linux")) {
269 return true;
270 } else {
271 return false;
272 }
273 }
274 };
275
276 File[] files = sysroot_dir.listFiles(nativeFilter);
277 String arch = null;
278
279 for (File file : files) {
280 if (file.isDirectory()) {
281 String path = file.getName();
282 String[] subPath = path.split("-");
283 arch = subPath[0];
284 } else {
285 continue;
286 }
287 }
288
289 return arch;
290 }
291}