|
Re: Newbi Question regarding complete C enviorment [message #1707243 is a reply to message #1707207] |
Thu, 03 September 2015 08:52 |
Fadi J Messages: 11 Registered: June 2013 |
Junior Member |
|
|
to the best of my knowledge there is two solutions.
1- you can create an eclipse headless plug-in and then run eclipse from the command line. Notice that when running eclipse from the command line you still needs to open an workspace and create a C project and link its source folder with the SRC code location. If you wish I can provide you with an example.
2- you can run CDT outside eclipse. To do that you needs those jar files (any version is ok):
org.eclipse.cdt.core_5.9.1.201502131403.jar
org.eclipse.cdt.ui_5.9.0.201502131403.jar
org.eclipse.core.contenttype_3.4.200.v20140207-1251.jar
org.eclipse.core.jobs_3.6.1.v20141014-1248.jar
org.eclipse.core.resources_3.9.1.v20140825-1431.jar
org.eclipse.core.runtime_3.10.0.v20140318-2214.jar
org.eclipse.equinox.common_3.6.200.v20130402-1505.jar
org.eclipse.equinox.preferences_3.5.200.v20140224-1527.jar
org.eclipse.osgi_3.10.2.v20150203-1939.jar
but in this case you have to manage the included files manually. An example:
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import org.eclipse.cdt.codan.core.cxx.internal.model.cfg.ControlFlowGraphBuilder;
import org.eclipse.cdt.codan.core.cxx.internal.model.cfg.CxxControlFlowGraph;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.cxx.model.CxxModelsCache;
import org.eclipse.cdt.codan.core.model.ICheckerInvocationContext;
import org.eclipse.cdt.codan.core.model.cfg.IBasicBlock;
import org.eclipse.cdt.codan.core.model.cfg.ICfgData;
import org.eclipse.cdt.codan.core.model.cfg.IControlFlowGraph;
import org.eclipse.cdt.codan.internal.core.CheckerInvocationContext;
import org.eclipse.cdt.codan.internal.core.cfg.ControlFlowGraph;
import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.util.ASTPrinter;
import org.eclipse.cdt.internal.core.dom.IIncludeFileResolutionHeuristics;
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.index.CIndex;
import org.eclipse.cdt.internal.core.parser.IMacroDictionary;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContent;
import org.eclipse.cdt.internal.core.parser.scanner.InternalFileContentProvider;
import org.eclipse.cdt.ui.tests.DOMAST.CPopulateASTViewAction;
import org.eclipse.cdt.ui.tests.DOMAST.DOMASTNodeLeaf;
import org.eclipse.cdt.ui.tests.DOMAST.DOMASTNodeParent;
import org.eclipse.cdt.ui.tests.DOMAST.IPopulateDOMASTAction;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import de.tuc.ifi.sse.ipsse.codeanalyzer.util.Util;
public class Test {
public static IASTTranslationUnit ast;
public static void main(String[] args) {
ILanguage lang = GCCLanguage.getDefault();
FileContent fc = FileContent.createForExternalFileLocation("Test c.c");
try {
InternalFileContentProvider crf = new InternalFileContentProvider() {
@Override
public InternalFileContent getContentForInclusion(IIndexFileLocation ifl, String astPath) {
return (InternalFileContent) FileContent.create(ifl);
}
@Override
public InternalFileContent getContentForInclusion(String path, IMacroDictionary macroDictionary) {
return (InternalFileContent) FileContent.createForExternalFileLocation(path);
}
};
IIncludeFileResolutionHeuristics ifrh = new IIncludeFileResolutionHeuristics() {
String[] classPath;
{
classPath = new String[1];
classPath[0] = "/usr/include";
}
@Override
public String findInclusion(String include, String currentFile) {
if (include.contains("/")) {
include = include.substring(include.lastIndexOf("/") + 1, include.length());
}
FileSearch fileSearch = new FileSearch();
for (int i = 0; i < classPath.length; i++) {
fileSearch.searchDirectory(new File(classPath[i]), include);
}
if (fileSearch.getResult().size() == 0)
return null;
String[] temp = new String[fileSearch.getResult().size()];
for (int i = 0; i < temp.length; i++) {
temp[i] = fileSearch.getResult().get(i);
}
String bestLocation = selectBest(temp, currentFile.toCharArray());
if (bestLocation == null)
return null;
return bestLocation;
}
private String selectBest(String[] files, char[] currentFullPath) {
String best = files[0];
int bestScore = computeScore(best.toCharArray(), currentFullPath);
for (int i = 1; i < files.length; i++) {
String file = files[i];
int score = computeScore(file.toCharArray(), currentFullPath);
if (score > bestScore) {
bestScore = score;
best = file;
}
}
return best;
}
private int computeScore(char[] path1, char[] path2) {
final int limit = Math.min(path1.length, path2.length);
int match = 0;
for (int i = 0; i < limit; i++) {
if (path1[i] != path2[i])
break;
if (path1[i] == '/')
match = i;
}
// Prefer shortest path with longest matches with.
return (match << 16) - path1.length;
}
};
crf.setIncludeResolutionHeuristics(ifrh);
ast = lang.getASTTranslationUnit(fc, new ScannerInfo(), crf, null, 0, ParserUtil.getParserLogService());
//I'm not sure how to compile the files
} catch (CoreException e) {
e.printStackTrace();
}
}
public static class FileSearch {
private String fileNameToSearch;
private List<String> result = new ArrayList<String>();
public String getFileNameToSearch() {
return fileNameToSearch;
}
public void setFileNameToSearch(String fileNameToSearch) {
this.fileNameToSearch = fileNameToSearch;
}
public List<String> getResult() {
return result;
}
public void searchDirectory(File directory, String fileNameToSearch) {
setFileNameToSearch(fileNameToSearch);
if (directory.isDirectory()) {
search(directory);
}
}
private void search(File file) {
if (file.isDirectory()) {
// System.out.println("Searching directory ... " +
// file.getAbsoluteFile());
// do you have permission to read this directory?
if (file.canRead()) {
for (File temp : file.listFiles()) {
if (temp.isDirectory()) {
search(temp);
} else {
if (getFileNameToSearch().equals(temp.getName())) {
result.add(temp.getAbsoluteFile().toString());
}
}
}
}
}
}
}
}
|
|
|
|
Re: Newbi Question regarding complete C enviorment [message #1708130 is a reply to message #1707207] |
Mon, 14 September 2015 06:53 |
Fadi J Messages: 11 Registered: June 2013 |
Junior Member |
|
|
You have to do something like this. but c project instead of java project. Also info about headless eclipse plugins you can find with a google search.
public class ConsoleApp implements IApplication {
@Override
public Object start(IApplicationContext context) {
try {
String[] args = (String[]) context.getArguments().get(
"application.args");
Platform.getBundle("AnyBundleYouNeedsToStart").start();
} catch (Exception e) {
e.printStackTrace();
}
importSourceCode("path to source");
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("Temp Project");
try {
project.delete(true, null);
} catch (CoreException e1) {
}
return IApplication.EXIT_OK;
}
@Override
public void stop() {
}
private static String importSourceCode(String absolutePathToDocument) {
IProject project = null;
String srcFolderLocationInTheProject = "src";
String projectName = "Temp Project";
try {
// Create a project
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
project = root.getProject(projectName);
if (project.exists()){
project.delete(true, null);
}
project.create(null);
project.open(null);
// Because we need a java project, we have to set the Java nature to
// the created project
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });
project.setDescription(description, null);
// create Java project
IJavaProject javaProject = JavaCore.create(project);
// Set the Java build path
IFolder binFolder = project.getFolder("bin");
binFolder.create(false, true, null);
javaProject.setOutputLocation(binFolder.getFullPath(), null);
// Link source folder
ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(
Arrays.asList(javaProject.getRawClasspath()));
// remove all other source entries, because the main project folder
// is a source entry and we can't save other source entry nested in
// it
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getEntryKind() == IClasspathEntry.CPE_SOURCE) {
entries.remove(i);
}
}
IPath srcPath = javaProject.getPath().append(
srcFolderLocationInTheProject);
IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);
entries.add(srcEntry);
javaProject.setRawClasspath((IClasspathEntry[]) entries
.toArray(new IClasspathEntry[entries.size()]), null);
//link the folder with the resources
IFolder srcFolder = project.getFolder(srcFolderLocationInTheProject);
srcFolder.createLink(new Path(absolutePathToDocument.replaceFirst("file:/", "")), IFolder.FOLDER, null);
return "file:/"
+ ResourcesPlugin.getWorkspace().getRoot().getLocation()
+ "/" + projectName + "/" + srcFolderLocationInTheProject;
} catch (Exception e) {
e.printStackTrace();
try {
project.delete(true, null);
} catch (CoreException e1) {
e1.printStackTrace();
}
return null;
}
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02679 seconds