前言:
因为来实现Freescale环境在Arduino IDE的实现,需要对源代码的进行分析。在使用IDE的使用过程中,我们发现如果直接在Editor中include库的话,编译器将无视,所以必须点击工具栏的Sketch->Import Library才能成功include所需要的库。
分析:
IDE的源代码在arduino-1.0.X/app/src/processing/app中。很容易找到关于Sketch的.java文件。
寻找到引入库的函数importLibrary,如下:
public void importLibrary(String jarPath) {
// make sure the user didn't hide the sketch folder
ensureExistence();
String list[] = Compiler.headerListFromIncludePath(jarPath);
// import statements into the main sketch file (code[0])
// if the current code is a .java file, insert into current
//if (current.flavor == PDE) {
if (hasDefaultExtension(current)) {
setCurrentCode(0);
}
// could also scan the text in the file to see if each import
// statement is already in there, but if the user has the import
// commented out, then this will be a problem.
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < list.length; i++) {
buffer.append("#include <");
buffer.append(list[i]);
buffer.append(">\n");
}
buffer.append('\n');
buffer.append(editor.getText());
editor.setText(buffer.toString());
editor.setSelection(0, 0); // scroll to start
setModified(true);
}
可见ide将库的路径添加到Compiler.headerListFromIncludePath中,然后在Editor中写入"#include ".
然后找到Compiler文件(位于arduino-1.0.X/app/src/processing/app/debug)Compiler.java中headerListFromIncludePath()函数,如下:
static public String[] headerListFromIncludePath(String path) {
FilenameFilter onlyHFiles = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".h");
}
};
return (new File(path)).list(onlyHFiles);
}
static public ArrayList findFilesInPath(String path, String extension,
boolean recurse) {
return findFilesInFolder(new File(path), extension, recurse);
}
static public ArrayList findFilesInFolder(File folder, String extension,
boolean recurse) {
ArrayList files = new ArrayList();
if (folder.listFiles() == null) return files;
for (File file : folder.listFiles()) {
if (file.getName().startsWith(".")) continue; // skip hidden files
if (file.getName().endsWith("." + extension))
files.add(file);
if (recurse && file.isDirectory()) {
files.addAll(findFilesInFolder(file, extension, true));
}
}
return files;
}
}
以下函数选择板子:
String avrBasePath = Base.getAvrBasePath();
Map boardPreferences = Base.getBoardPreferences();
String core = boardPreferences.get("build.core");
if (core == null) {
RunnerException re = new RunnerException(_("No board selected; please choose a board from the Tools > Board menu."));
re.hideStackTrace();
throw re;
}
String corePath;
最后编译选配置为“-I”:
for (int i = 0; i < includePaths.size(); i++) {
baseCommandCompiler.add("-I" + (String) includePaths.get(i));
}
关键词:
Arduino
入库
机制
String