📄 contikimotetypedialog.java
字号:
compilationThread.start(); while (compilationThread.isAlive()) { try { Thread.sleep(100); } catch (InterruptedException e) { // NOP } } if (!compilationSucceded) { if (libFile.exists()) { libFile.delete(); } if (depFile.exists()) { depFile.delete(); } if (mapFile.exists()) { mapFile.delete(); } libraryCreatedOK = false; } else { libraryCreatedOK = true; if (!libFile.exists() || !depFile.exists() || !mapFile.exists()) libraryCreatedOK = false; } if (libraryCreatedOK) { progressBar.setBackground(Color.GREEN); progressBar.setString("compilation succeded"); button.grabFocus(); myDialog.getRootPane().setDefaultButton(createButton); } else { progressBar.setBackground(Color.ORANGE); progressBar.setString("compilation failed"); myDialog.getRootPane().setDefaultButton(testButton); } progressBar.setIndeterminate(false); progressBar.setValue(0); createButton.setEnabled(libraryCreatedOK); } /** * Generates new source file by reading default source template and replacing * fields with sensors, core interfaces and processes. Also includes default * processes from GUI external configuration. * * @param id * Mote type ID (decides name of new source file) * @param sensors * Names of sensors * @param coreInterfaces * Names of core interfaces * @param userProcesses * Names of user processes * @return New filename * @throws Exception * If any error occurs */ public static String generateSourceFile(String id, Vector<String> sensors, Vector<String> coreInterfaces, Vector<String> userProcesses) throws Exception { // SENSORS String sensorString = ""; String externSensorDefs = ""; for (String sensor : sensors) { if (!sensorString.equals("")) sensorString += ", "; sensorString += "&" + sensor; externSensorDefs += "extern const struct sensors_sensor " + sensor + ";\n"; } if (!sensorString.equals("")) sensorString = "SENSORS(" + sensorString + ");"; else sensorString = "SENSORS(NULL);"; // CORE INTERFACES String interfaceString = ""; String externInterfaceDefs = ""; for (String coreInterface : coreInterfaces) { if (!interfaceString.equals("")) interfaceString += ", "; interfaceString += "&" + coreInterface; externInterfaceDefs += "SIM_INTERFACE_NAME(" + coreInterface + ");\n"; } if (!interfaceString.equals("")) interfaceString = "SIM_INTERFACES(" + interfaceString + ");"; else interfaceString = "SIM_INTERFACES(NULL);"; // PROCESSES (including any default processes) String userProcessString = ""; String externProcessDefs = ""; for (String process : userProcesses) { if (!userProcessString.equals("")) userProcessString += ", "; userProcessString += "&" + process; externProcessDefs += "PROCESS_NAME(" + process + ");\n"; } String defaultProcessString = ""; String defaultProcesses[] = GUI.getExternalToolsSetting( "CONTIKI_STANDARD_PROCESSES").split(";"); for (String process : defaultProcesses) { if (!defaultProcessString.equals("")) defaultProcessString += ", "; defaultProcessString += "&" + process; } if (userProcessString.equals("")) { logger .warn("No application processes specified! Sure you don't want any?"); } String processString; if (!defaultProcessString.equals("")) processString = "PROCINIT(" + defaultProcessString + ");"; else processString = "PROCINIT(NULL);"; if (!userProcessString.equals("")) processString += "\nAUTOSTART_PROCESSES(" + userProcessString + ");"; else processString += "\nAUTOSTART_PROCESSES(NULL);"; // CHECK JNI CLASS AVAILABILITY String libString = CoreComm.getAvailableClassName(); if (libString == null) { logger.fatal("No more libraries can be loaded!"); throw new Exception("Maximum number of mote types already exist"); } // GENERATE NEW FILE BufferedWriter destFile = null; BufferedReader sourceFile = null; String destFilename = null; try { Reader reader; String mainTemplate = GUI .getExternalToolsSetting("CONTIKI_MAIN_TEMPLATE_FILENAME"); if ((new File(mainTemplate)).exists()) { reader = new FileReader(mainTemplate); } else { InputStream input = ContikiMoteTypeDialog.class .getResourceAsStream('/' + mainTemplate); if (input == null) { throw new FileNotFoundException(mainTemplate + " not found"); } reader = new InputStreamReader(input); } sourceFile = new BufferedReader(reader); destFilename = ContikiMoteType.tempOutputDirectory.getPath() + File.separatorChar + id + ".c"; destFile = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(destFilename))); // Replace special fields in template String line; while ((line = sourceFile.readLine()) != null) { line = line .replaceFirst("\\[PROCESS_DEFINITIONS\\]", externProcessDefs); line = line.replaceFirst("\\[PROCESS_ARRAY\\]", processString); line = line.replaceFirst("\\[SENSOR_DEFINITIONS\\]", externSensorDefs); line = line.replaceFirst("\\[SENSOR_ARRAY\\]", sensorString); line = line.replaceFirst("\\[INTERFACE_DEFINITIONS\\]", externInterfaceDefs); line = line.replaceFirst("\\[INTERFACE_ARRAY\\]", interfaceString); line = line.replaceFirst("\\[CLASS_NAME\\]", libString); destFile.write(line + "\n"); } destFile.close(); sourceFile.close(); } catch (Exception e) { try { if (destFile != null) destFile.close(); if (sourceFile != null) sourceFile.close(); } catch (Exception e2) { } // Forward exception throw e; } return destFilename; } /** * Compiles a mote type shared library using the standard Contiki makefile. * * @param identifier * Mote type identifier * @param contikiDir * Contiki base directory * @param sourceFiles * Source files and directories to include in compilation * @param includeSymbols * Generate and include symbols in library file * @param outputStream * Output stream from compilation (optional) * @param errorStream * Error stream from compilation (optional) * @return True if compilation succeded, false otherwise */ public static boolean compileLibrary(String identifier, File contikiDir, Vector<File> sourceFiles, boolean includeSymbols, final PrintStream outputStream, final PrintStream errorStream) { File libFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.librarySuffix); File mapFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.mapSuffix); File depFile = new File(ContikiMoteType.tempOutputDirectory, identifier + ContikiMoteType.dependSuffix); // Recheck that contiki path exists if (!contikiDir.exists()) { if (errorStream != null) errorStream.println("Bad Contiki OS path"); logger.fatal("Contiki path does not exist"); return false; } if (!contikiDir.isDirectory()) { if (errorStream != null) errorStream.println("Bad Contiki OS path"); logger.fatal("Contiki path is not a directory"); return false; } if (libFile.exists()) { if (errorStream != null) errorStream.println("Bad output filenames"); logger.fatal("Could not overwrite already existing library"); return false; } if (CoreComm.hasLibraryFileBeenLoaded(libFile)) { if (errorStream != null) errorStream.println("Bad output filenames"); logger .fatal("A library has already been loaded with the same name before"); return false; } if (depFile.exists()) { if (errorStream != null) errorStream.println("Bad output filenames"); logger.fatal("Could not overwrite already existing dependency file"); return false; } if (mapFile.exists()) { if (errorStream != null) errorStream.println("Bad output filenames"); logger.fatal("Could not overwrite already existing map file"); return false; } try { // Let Contiki 2.x regular make file compile String[] cmd = new String[]{ GUI.getExternalToolsSetting("PATH_MAKE"), libFile.getPath().replace(File.separatorChar, '/'), "-f", contikiDir.getPath().replace(File.separatorChar, '/') + "/Makefile.include"}; String sourceDirs = System.getProperty("PROJECTDIRS", ""); String sourceFileNames = ""; String ccFlags = GUI.getExternalToolsSetting("COMPILER_ARGS", ""); for (File sourceFile : sourceFiles) { if (sourceFile.isDirectory()) { // Add directory to search path sourceDirs += " " + sourceFile.getPath().replace(File.separatorChar, '/'); ccFlags += " -I" + sourceFile.getPath().replace(File.separatorChar, '/'); } else if (sourceFile.isFile()) { // Add both file name and directory if (sourceFile.getParent() != null) { sourceDirs += " " + sourceFile.getParent().replace(File.separatorChar, '/'); } sourceFileNames += " " + sourceFile.getName(); } else { // Add filename and hope Contiki knows where to find it... sourceFileNames += " " + sourceFile.getName(); } } logger.info("-- Compiling --"); logger.info("Project dirs: " + sourceDirs); logger.info("Project sources: " + sourceFileNames); logger.info("Compiler flags: " + ccFlags); String[] env = new String[]{ "CONTIKI=" + contikiDir.getPath().replace(File.separatorChar, '/'), "TARGET=cooja", "TYPEID=" + identifier, "LD_ARGS_1=" + GUI.getExternalToolsSetting("LINKER_ARGS_1", ""), "LD_ARGS_2=" + GUI.getExternalToolsSetting("LINKER_ARGS_2", ""), "EXTRA_CC_ARGS=" + ccFlags, "SYMBOLS=" + (includeSymbols?"1":""), "CC=" + GUI.getExternalToolsSetting("PATH_C_COMPILER"), "LD=" + GUI.getExternalToolsSetting("PATH_LINKER"), "PROJECTDIRS=" + sourceDirs, "PROJECT_SOURCEFILES=" + sourceFileNames, "PATH=" + System.getenv("PATH")}; Process p = Runtime.getRuntime().exec(cmd, env, null); final BufferedReader input = new BufferedReader(new InputStreamReader(p .getInputStream())); final BufferedReader err = new BufferedReader(new InputStreamReader(p .getErrorStream())); Thread readInput = new Thread(new Runnable() { public void run() { String readLine; try { while ((readLine = input.readLine()) != null) { if (outputStream != null && readLine != null) outputStream.println(readLine); } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); Thread readError = new Thread(new Runnable() { public void run() { String readLine; try { while ((readLine = err.readLine()) != null) { if (errorStream != null && readLine != null) errorStream.println(readLine); } } catch (IOException e) { logger.warn("Error while reading from process"); } } }, "read input stream thread"); readInput.start(); readError.start(); while (readInput.isAlive() || readError.isAlive()) { Thread.sleep(100); } input.close(); err.close(); p.waitFor(); if (p.exitValue() != 0) { logger.fatal("Make file returned error: " + p.exitValue()); return false; } } catch (Exception e) { logger.fatal("Error while compiling library: " + e); return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -