summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.ide/src/org/yocto/sdk/ide/YoctoSDKChecker.java
blob: 714470b56640bd74f3f5af387fe6f5962c486def (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * Copyright (c) 2013 BMW Car IT GmbH.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel - initial API and implementation
 * BMW Car IT - include error and advice messages with check results
 *******************************************************************************/
package org.yocto.sdk.ide;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.jface.preference.IPreferenceStore;
import org.yocto.sdk.ide.natures.YoctoSDKProjectNature;
import org.yocto.sdk.ide.utils.YoctoSDKUtils;
import org.yocto.sdk.ide.utils.YoctoSDKUtilsConstants;

public class YoctoSDKChecker {
	private static final String[] saInvalidVer = {"1.0", "0.9", "0.9+"};
	private static final String SYSROOTS_DIR = "sysroots";
	private static final String SDK_VERSION = "OECORE_SDK_VERSION";

	public static enum SDKCheckResults {
		SDK_PASS("", false),
		TOOLCHAIN_LOCATION_EMPTY(
				"Poky.SDK.Location.Empty", true),
		TOOLCHAIN_LOCATION_NONEXIST(
				"Poky.SDK.Location.Nonexist", true),
		SDK_TARGET_EMPTY(
				"Poky.SDK.Target.Empty", true),
		SDK_NO_TARGET_SELECTED(
				"Poky.SDK.No.Target.Selected", false),
		SYSROOT_EMPTY(
				"Poky.Sysroot.Empty", true),
		SYSROOT_NONEXIST(
				"Poky.Sysroot.Nonexist", true),
		QEMU_KERNEL_EMPTY(
				"Poky.Qemu.Kernel.Empty", true),
		QEMU_KERNEL_NONEXIST(
				"Poky.Qemu.Kernel.Nonexist", true),
		WRONG_ADT_VERSION(
				"Poky.ADT.Sysroot.Wrongversion", false),
		ENV_SETUP_SCRIPT_NONEXIST(
				"Poky.Env.Script.Nonexist", false),
		TOOLCHAIN_NO_SYSROOT(
				"Poky.Toolchain.No.Sysroot", false),
		TOOLCHAIN_HOST_MISMATCH(
				"Poky.Toolchain.Host.Mismatch", false);

		private static final String DEFAULT_ADVICE = "Default.Advice";
		private static final String ADVICE_SUFFIX = ".Advice";

		private final String messageID;
		private final boolean addDefaultAdvice;

		private SDKCheckResults(final String messageID, final boolean addDefaultAdvice) {
			this.messageID = messageID;
			this.addDefaultAdvice = addDefaultAdvice;
		}

		public String getMessage() {
			return YoctoSDKMessages.getString(messageID);
		}

		public String getAdvice() {
			String advice = YoctoSDKMessages.getString(messageID + ADVICE_SUFFIX);

			if (addDefaultAdvice) {
				advice += YoctoSDKMessages.getString(DEFAULT_ADVICE);
			}

			return advice;
		}
	};

	public static enum SDKCheckRequestFrom {
		Wizard("Poky.SDK.Error.Origin.Wizard"),
		Menu("Poky.SDK.Error.Origin.Menu"),
		Preferences("Poky.SDK.Error.Origin.Preferences"),
		Other("Poky.SDK.Error.Origin.Other");

		private final String errorMessageID;

		private SDKCheckRequestFrom(final String errorMessageID) {
			this.errorMessageID = errorMessageID;
		}

		public String getErrorMessage() {
			return YoctoSDKMessages.getString(errorMessageID);
		}
	};

	public static void checkIfGloballySelectedYoctoProfileIsValid() throws YoctoGeneralException {
		YoctoProfileElement profileElement = YoctoSDKUtils.getProfilesFromDefaultStore();
		IPreferenceStore selectedProfileStore = YoctoSDKPlugin.getProfilePreferenceStore(profileElement.getSelectedProfile());
		YoctoUIElement elem = YoctoSDKUtils.getElemFromStore(selectedProfileStore);

		SDKCheckResults result = checkYoctoSDK(elem);
		if (result != SDKCheckResults.SDK_PASS){
			String strErrorMsg =  getErrorMessage(result, SDKCheckRequestFrom.Wizard);
			throw new YoctoGeneralException(strErrorMsg);
		}
	}

	public static SDKCheckResults checkYoctoSDK(YoctoUIElement elem) {
		if (elem.getStrToolChainRoot().isEmpty())
			return SDKCheckResults.TOOLCHAIN_LOCATION_EMPTY;
		else {
			File fToolChain = new File(elem.getStrToolChainRoot());
			if (!fToolChain.exists())
				return SDKCheckResults.TOOLCHAIN_LOCATION_NONEXIST;
		}

		if (elem.getStrSysrootLoc().isEmpty()) {
			return SDKCheckResults.SYSROOT_EMPTY;
		} else {
			File fSysroot = new File(elem.getStrSysrootLoc());
			if (!fSysroot.exists())
				return SDKCheckResults.SYSROOT_NONEXIST;
		}

		if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_SDK_MODE) {
			//Check for SDK compatible with the host arch
			String platform = getPlatformArch();
			String sysroot_dir_str = elem.getStrToolChainRoot() + "/" + SYSROOTS_DIR;
			File sysroot_dir = new File(sysroot_dir_str);
			if (!sysroot_dir.exists())
				return SDKCheckResults.TOOLCHAIN_NO_SYSROOT;

			String toolchain_host_arch = null;

			try {
				toolchain_host_arch = findHostArch(sysroot_dir);
			} catch(NullPointerException e) {
				return SDKCheckResults.TOOLCHAIN_NO_SYSROOT;
			}

			if (!toolchain_host_arch.equalsIgnoreCase(platform)) {
				if (!platform.matches("i\\d86") || !toolchain_host_arch.matches("i\\d86"))
					return SDKCheckResults.TOOLCHAIN_HOST_MISMATCH;
			}
		}

		if (elem.getStrTarget().isEmpty() && elem.getStrTargetsArray().length > 0) {
			return SDKCheckResults.SDK_NO_TARGET_SELECTED;
		}

		if (elem.getIntTargetIndex() < 0 || elem.getStrTarget().isEmpty()) {
			//if this is poky tree mode, prompt user whether bitbake meta-ide-support is executed?
			if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_TREE_MODE)
				return SDKCheckResults.ENV_SETUP_SCRIPT_NONEXIST;
			else
				return SDKCheckResults.SDK_TARGET_EMPTY;
		} else {
			String sFileName;

			if (elem.getEnumPokyMode() == YoctoUIElement.PokyMode.POKY_SDK_MODE) {
				sFileName = elem.getStrToolChainRoot()+"/" + YoctoSDKUtilsConstants.DEFAULT_ENV_FILE_PREFIX + elem.getStrTarget();
			} else {
				//POKY TREE Mode
				sFileName = elem.getStrToolChainRoot() + YoctoSDKUtilsConstants.DEFAULT_TMP_PREFIX +
						YoctoSDKUtilsConstants.DEFAULT_ENV_FILE_PREFIX + elem.getStrTarget();
			}

			try {
				File file = new File(sFileName);
				boolean bVersion = false;

				if (file.exists()) {
					BufferedReader input = new BufferedReader(new FileReader(file));

					try {
						String line = null;

						while ((line = input.readLine()) != null) {
							if (line.startsWith("export "+ SDK_VERSION)) {
								int beginIndex = 2;
								String sVersion = "";
								for (;;) {
									char cValue = line.charAt(line.indexOf('=') + beginIndex++);

									if ((cValue != '.') && (!Character.isDigit(cValue)) && (cValue != '+'))
										break;
									else
										sVersion += String.valueOf(cValue);
								}

								for (int i = 0; i < saInvalidVer.length; i++) {
									if (!sVersion.equals(saInvalidVer[i])) {
										bVersion = true;
										break;
									}
								}

								break;
							}
						}
					} finally {
						input.close();
					}

					if (!bVersion)
						return SDKCheckResults.WRONG_ADT_VERSION;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (elem.getEnumDeviceMode() == YoctoUIElement.DeviceMode.QEMU_MODE) {
			if (elem.getStrQemuKernelLoc().isEmpty()) {
				return SDKCheckResults.QEMU_KERNEL_EMPTY;
			} else {
				File fQemuKernel = new File(elem.getStrQemuKernelLoc());
				if (!fQemuKernel.exists())
					return SDKCheckResults.QEMU_KERNEL_NONEXIST;
			}
		}

		return SDKCheckResults.SDK_PASS;
	}

	public static String getErrorMessage(SDKCheckResults result, SDKCheckRequestFrom from) {
		String strErrorMsg;
		strErrorMsg = from.getErrorMessage();
		strErrorMsg += "\n" + result.getMessage();
		strErrorMsg += "\n" + result.getAdvice();

		return strErrorMsg;
	}

	private static String getPlatformArch() {
		String value = null;
		try
		{
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("uname -m");
			InputStream stdin = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(stdin);
			BufferedReader br = new BufferedReader(isr);
			String line = null;

			while ( (line = br.readLine()) != null) {
				value = line;
			}
			proc.waitFor();

		} catch (Throwable t) {
			t.printStackTrace();
		}
		return value;
	}

	private static String findHostArch(File sysroot_dir) {
		FilenameFilter nativeFilter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.endsWith("sdk-linux")) {
					return true;
				} else {
					return false;
				}
			}
		};

		File[] files = sysroot_dir.listFiles(nativeFilter);
		String arch = null;

		for (File file : files) {
			if (file.isDirectory()) {
				String path = file.getName();
				String[] subPath = path.split("-");
				arch = subPath[0];
			} else {
				continue;
			}
		}

		return arch;
	}
}