Home » Language IDEs » CDT » Programatically create a project?
Programatically create a project? [message #227953] Sat, 13 December 2008 09:04 Go to next message
Bryan Hunt  is currently offline Bryan Hunt
Messages: 270
Registered: July 2009
Senior Member
Is there a section of code I could lift from a unit test that
programatically creates a C / C++ project?

I'm using the CDT launcher, and I need to write a unit test that uses
the launcher. In order for the launcher to be happy, I must have a
valid C / C++ project. I don't need to create an executable, just
enough for the launcher to consider it a valid project.

Bryan
Re: Programatically create a project? [message #227977 is a reply to message #227953 ] Sat, 13 December 2008 12:54 Go to previous messageGo to next message
Andrew Gvozdev  is currently offline Andrew Gvozdev
Messages: 80
Registered: July 2009
Member
Bryan Hunt wrote:

> Is there a section of code I could lift from a unit test that
> programatically creates a C / C++ project?

> I'm using the CDT launcher, and I need to write a unit test that uses
> the launcher. In order for the launcher to be happy, I must have a
> valid C / C++ project. I don't need to create an executable, just
> enough for the launcher to consider it a valid project.

I suppose the most accurate code would be C/C++ New Project Wizard code. I
also referred to org.eclipse.cdt.projectmodel.tests in
org.eclipse.cdt.managedbuilder.core.tests. Here is an excerpt of the code
I am using for my tests, this would create Standard Makefile project:

// Create and persist Standard Makefile project
{
// Create model project and accompanied project description
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();

IProject newProjectHandle = root.getProject(projectName);
Assert.assertNotNull(newProjectHandle);
Assert.assertFalse(newProjectHandle.exists());

IProjectDescription description =
workspace.newProjectDescription(newProjectHandle.getName());
IProject project =
CCorePlugin.getDefault().createCDTProject(description, newProjectHandle,
new NullProgressMonitor());
Assert.assertTrue(newProjectHandle.isOpen());

ICProjectDescriptionManager mngr =
CoreModel.getDefault().getProjectDescriptionManager();
ICProjectDescription des = mngr.createProjectDescription(project,
false);
ManagedProject mProj = new ManagedProject(des);

Configuration cfg = new Configuration(mProj, null,
"your.configuration.id", "YourConfigurationName");

IBuilder bld = cfg.getEditableBuilder();
Assert.assertNotNull(bld);
Assert.assertFalse(bld.isInternalBuilder());

bld.setManagedBuildOn(false);

CConfigurationData data = cfg.getConfigurationData();
Assert.assertNotNull(data);
des.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDE R_ID, data);

// Persist the project description
mngr.setProjectDescription(project, des);

project.close(null);
}


Thanks,
Andrew

> Bryan
Re: Programatically create a project? [message #228143 is a reply to message #227977 ] Wed, 17 December 2008 21:36 Go to previous messageGo to next message
Bryan Hunt  is currently offline Bryan Hunt
Messages: 270
Registered: July 2009
Senior Member
Andrew,

Thanks for the info. I was able to use this code to get my unit test
working. :)

Bryan

On 2008-12-13 11:54:11 -0600, angvoz.dev.gmail.com (Andrew Gvozdev) said:

> Bryan Hunt wrote:
>
>> Is there a section of code I could lift from a unit test that
>> programatically creates a C / C++ project?
>
>> I'm using the CDT launcher, and I need to write a unit test that uses
>> the launcher. In order for the launcher to be happy, I must have a
>> valid C / C++ project. I don't need to create an executable, just
>> enough for the launcher to consider it a valid project.
>
> I suppose the most accurate code would be C/C++ New Project Wizard
> code. I also referred to org.eclipse.cdt.projectmodel.tests in
> org.eclipse.cdt.managedbuilder.core.tests. Here is an excerpt of the
> code I am using for my tests, this would create Standard Makefile
> project:
>
> // Create and persist Standard Makefile project
> {
> // Create model project and accompanied project description
> IWorkspace workspace = ResourcesPlugin.getWorkspace();
> IWorkspaceRoot root = workspace.getRoot();
>
> IProject newProjectHandle = root.getProject(projectName);
> Assert.assertNotNull(newProjectHandle);
> Assert.assertFalse(newProjectHandle.exists());
>
> IProjectDescription description =
> workspace.newProjectDescription(newProjectHandle.getName());
> IProject project =
> CCorePlugin.getDefault().createCDTProject(description,
> newProjectHandle, new NullProgressMonitor());
> Assert.assertTrue(newProjectHandle.isOpen());
>
> ICProjectDescriptionManager mngr =
> CoreModel.getDefault().getProjectDescriptionManager();
> ICProjectDescription des = mngr.createProjectDescription(project, false);
> ManagedProject mProj = new ManagedProject(des);
>
> Configuration cfg = new Configuration(mProj, null,
> "your.configuration.id", "YourConfigurationName");
>
> IBuilder bld = cfg.getEditableBuilder();
> Assert.assertNotNull(bld);
> Assert.assertFalse(bld.isInternalBuilder());
>
> bld.setManagedBuildOn(false);
>
> CConfigurationData data = cfg.getConfigurationData();
> Assert.assertNotNull(data);
> des.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDE R_ID, data);
>
> // Persist the project description
> mngr.setProjectDescription(project, des);
>
> project.close(null);
> }
>
>
> Thanks,
> Andrew
>
>> Bryan
Re: Programatically create a project? [message #229922 is a reply to message #227977 ] Tue, 03 February 2009 09:02 Go to previous messageGo to next message
Andrew Gvozdev  is currently offline Andrew Gvozdev
Messages: 80
Registered: July 2009
Member
For the cross-reference, I posted also how to create managed project
programatically in this thread:
http://www.nabble.com/Programatically-create-CDT-project-td2 1096039.html

Andrew Gvozdev wrote:
> Bryan Hunt wrote:
>> Is there a section of code I could lift from a unit test that
>> programatically creates a C / C++ project?
>> I'm using the CDT launcher, and I need to write a unit test that uses
>> the launcher. In order for the launcher to be happy, I must have a
>> valid C / C++ project. I don't need to create an executable, just
>> enough for the launcher to consider it a valid project.

> I suppose the most accurate code would be C/C++ New Project Wizard code. I
> also referred to org.eclipse.cdt.projectmodel.tests in
> org.eclipse.cdt.managedbuilder.core.tests. Here is an excerpt of the code
> I am using for my tests, this would create Standard Makefile project:

> // Create and persist Standard Makefile project
> {
> // Create model project and accompanied project description
> IWorkspace workspace = ResourcesPlugin.getWorkspace();
> IWorkspaceRoot root = workspace.getRoot();

> IProject newProjectHandle = root.getProject(projectName);
> Assert.assertNotNull(newProjectHandle);
> Assert.assertFalse(newProjectHandle.exists());

> IProjectDescription description =
> workspace.newProjectDescription(newProjectHandle.getName());
> IProject project =
> CCorePlugin.getDefault().createCDTProject(description, newProjectHandle,
> new NullProgressMonitor());
> Assert.assertTrue(newProjectHandle.isOpen());

> ICProjectDescriptionManager mngr =
> CoreModel.getDefault().getProjectDescriptionManager();
> ICProjectDescription des = mngr.createProjectDescription(project,
> false);
> ManagedProject mProj = new ManagedProject(des);

> Configuration cfg = new Configuration(mProj, null,
> "your.configuration.id", "YourConfigurationName");

> IBuilder bld = cfg.getEditableBuilder();
> Assert.assertNotNull(bld);
> Assert.assertFalse(bld.isInternalBuilder());

> bld.setManagedBuildOn(false);

> CConfigurationData data = cfg.getConfigurationData();
> Assert.assertNotNull(data);
> des.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDE R_ID, data);

> // Persist the project description
> mngr.setProjectDescription(project, des);

> project.close(null);
> }


> Thanks,
> Andrew

>> Bryan
Re: Programatically create a project? [message #231197 is a reply to message #227977 ] Thu, 05 March 2009 09:19 Go to previous messageGo to next message
John Moule  is currently offline John Moule
Messages: 8
Registered: July 2009
Junior Member
Hi,

I've used the code you posted successfully (thanks a lot), but I also want
to use a specific toolchain when the project is created. The toolchain (a
slightly adapted GNU one) exists in my toolset plugin and I can
successfully create a project with this toolchain using the new project
wizard. But now I'd like the ability to do this programmatically. I'd like
to specify myToolChain like this:

...
Configuration cfg = new Configuration(mProj, myToolChain,
"com.xyz.microtoolset.toolchain.gnu.base", "XYZ Micro Toolset");
...

But I can't figure how to create the myToolChain object easily. I've
looked at the constructors in
org.eclipse.cdt.managedbuilder.internal.core.ToolChain but I don't
understand how to specify the various params they require using what I've
got.

Any tips would be gratefully received.

cheers john





Andrew Gvozdev wrote:

> Bryan Hunt wrote:

>> Is there a section of code I could lift from a unit test that
>> programatically creates a C / C++ project?

>> I'm using the CDT launcher, and I need to write a unit test that uses
>> the launcher. In order for the launcher to be happy, I must have a
>> valid C / C++ project. I don't need to create an executable, just
>> enough for the launcher to consider it a valid project.

> I suppose the most accurate code would be C/C++ New Project Wizard code. I
> also referred to org.eclipse.cdt.projectmodel.tests in
> org.eclipse.cdt.managedbuilder.core.tests. Here is an excerpt of the code
> I am using for my tests, this would create Standard Makefile project:

> // Create and persist Standard Makefile project
> {
> // Create model project and accompanied project description
> IWorkspace workspace = ResourcesPlugin.getWorkspace();
> IWorkspaceRoot root = workspace.getRoot();

> IProject newProjectHandle = root.getProject(projectName);
> Assert.assertNotNull(newProjectHandle);
> Assert.assertFalse(newProjectHandle.exists());

> IProjectDescription description =
> workspace.newProjectDescription(newProjectHandle.getName());
> IProject project =
> CCorePlugin.getDefault().createCDTProject(description, newProjectHandle,
> new NullProgressMonitor());
> Assert.assertTrue(newProjectHandle.isOpen());

> ICProjectDescriptionManager mngr =
> CoreModel.getDefault().getProjectDescriptionManager();
> ICProjectDescription des = mngr.createProjectDescription(project,
> false);
> ManagedProject mProj = new ManagedProject(des);

> Configuration cfg = new Configuration(mProj, null,
> "your.configuration.id", "YourConfigurationName");

> IBuilder bld = cfg.getEditableBuilder();
> Assert.assertNotNull(bld);
> Assert.assertFalse(bld.isInternalBuilder());

> bld.setManagedBuildOn(false);

> CConfigurationData data = cfg.getConfigurationData();
> Assert.assertNotNull(data);
> des.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDE R_ID, data);

> // Persist the project description
> mngr.setProjectDescription(project, des);

> project.close(null);
> }


> Thanks,
> Andrew

>> Bryan
Re: Programatically create a project? [message #231220 is a reply to message #231197 ] Fri, 06 March 2009 06:55 Go to previous messageGo to next message
John Moule  is currently offline John Moule
Messages: 8
Registered: July 2009
Junior Member
Hi,

... I've sussed it. I struck gold in
org.eclipse.cdt.managedbuilder.core.ManagedBuildManager; its full of
useful statics. So to get my toolchain that I'm interested in I use the
following (note error checks not included!):

IToolChain[] tcs = ManagedBuildManager.getRealToolChains();
ToolChain tc=null;
for (int i=0; i<tcs.length; i++) {
if(tcs[i].getName().contains("XYZ Micro Toolset")) {
tc = (ToolChain) tcs[i];
break;
}
}
Configuration cfg = new Configuration(mProj, tc,
"com.xyz.microtoolset.toolchain.gnu.base", "XYZ Micro Toolset");

cheers john


John Moule wrote:

> Hi,

> I've used the code you posted successfully (thanks a lot), but I also want
> to use a specific toolchain when the project is created. The toolchain (a
> slightly adapted GNU one) exists in my toolset plugin and I can
> successfully create a project with this toolchain using the new project
> wizard. But now I'd like the ability to do this programmatically. I'd like
> to specify myToolChain like this:

> ...
> Configuration cfg = new Configuration(mProj, myToolChain,
> "com.xyz.microtoolset.toolchain.gnu.base", "XYZ Micro Toolset");
> ...

> But I can't figure how to create the myToolChain object easily. I've
> looked at the constructors in
> org.eclipse.cdt.managedbuilder.internal.core.ToolChain but I don't
> understand how to specify the various params they require using what I've
> got.

> Any tips would be gratefully received.

> cheers john





> Andrew Gvozdev wrote:

>> Bryan Hunt wrote:

>>> Is there a section of code I could lift from a unit test that
>>> programatically creates a C / C++ project?

>>> I'm using the CDT launcher, and I need to write a unit test that uses
>>> the launcher. In order for the launcher to be happy, I must have a
>>> valid C / C++ project. I don't need to create an executable, just
>>> enough for the launcher to consider it a valid project.

>> I suppose the most accurate code would be C/C++ New Project Wizard code. I
>> also referred to org.eclipse.cdt.projectmodel.tests in
>> org.eclipse.cdt.managedbuilder.core.tests. Here is an excerpt of the code
>> I am using for my tests, this would create Standard Makefile project:

>> // Create and persist Standard Makefile project
>> {
>> // Create model project and accompanied project description
>> IWorkspace workspace = ResourcesPlugin.getWorkspace();
>> IWorkspaceRoot root = workspace.getRoot();

>> IProject newProjectHandle = root.getProject(projectName);
>> Assert.assertNotNull(newProjectHandle);
>> Assert.assertFalse(newProjectHandle.exists());

>> IProjectDescription description =
>> workspace.newProjectDescription(newProjectHandle.getName());
>> IProject project =
>> CCorePlugin.getDefault().createCDTProject(description, newProjectHandle,
>> new NullProgressMonitor());
>> Assert.assertTrue(newProjectHandle.isOpen());

>> ICProjectDescriptionManager mngr =
>> CoreModel.getDefault().getProjectDescriptionManager();
>> ICProjectDescription des = mngr.createProjectDescription(project,
>> false);
>> ManagedProject mProj = new ManagedProject(des);

>> Configuration cfg = new Configuration(mProj, null,
>> "your.configuration.id", "YourConfigurationName");

>> IBuilder bld = cfg.getEditableBuilder();
>> Assert.assertNotNull(bld);
>> Assert.assertFalse(bld.isInternalBuilder());

>> bld.setManagedBuildOn(false);

>> CConfigurationData data = cfg.getConfigurationData();
>> Assert.assertNotNull(data);
>> des.createConfiguration(ManagedBuildManager.CFG_DATA_PROVIDE R_ID, data);

>> // Persist the project description
>> mngr.setProjectDescription(project, des);

>> project.close(null);
>> }


>> Thanks,
>> Andrew

>>> Bryan
Re: Programatically create a project? [message #496036 is a reply to message #231220 ] Sat, 07 November 2009 08:22 Go to previous messageGo to next message
Yevgeny Shifrin  is currently offline Yevgeny Shifrin
Messages: 42
Registered: July 2009
Member
Hi,

I am writing eclipse plug-in, in which I would like to create C++ project. I took the code from this post.
Unfortunately I am not able to find correct imports for "ManagedProject" and "Configuration". Eclipse suggests the following imports:
import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
import org.eclipse.cdt.managedbuilder.internal.core.Configuration;

And then I have warnings for both imports:
Discouraged access: The type Configuration / ManagedProject is not accessible due to restriction on required library E:\Professional\eclipse_dev\eclipse3.5_rcp\eclipse\plugins
\org.eclipse.cdt.managedbuilder.core_6.0.0.200909110608.jar

Any idea how to solve it, which import to use? In plugin.xml I added org.eclipse.cdt.managedbuilder.core(6.0.0) dependency.

Thanks,
Yevgeny
Re: Programatically create a project? [message #496048 is a reply to message #496036 ] Sat, 07 November 2009 09:46 Go to previous messageGo to next message
Derek  is currently offline Derek
Messages: 49
Registered: July 2009
Member
On 07/11/2009 13:22, Yevgeny Shifrin wrote:
> Hi,
>
> I am writing eclipse plug-in, in which I would like to create C++
> project. I took the code from this post. Unfortunately I am not able to
> find correct imports for "ManagedProject" and "Configuration". Eclipse
> suggests the following imports:
> import org.eclipse.cdt.managedbuilder.internal.core.ManagedProject;
> import org.eclipse.cdt.managedbuilder.internal.core.Configuration;
>
> And then I have warnings for both imports:
> Discouraged access: The type Configuration / ManagedProject is not
> accessible due to restriction on required library
> E:\Professional\eclipse_dev\eclipse3.5_rcp\eclipse\plugins
> \org.eclipse.cdt.managedbuilder.core_6.0.0.200909110608.jar
>
> Any idea how to solve it, which import to use? In plugin.xml I added
> org.eclipse.cdt.managedbuilder.core(6.0.0) dependency.
>
> Thanks,
> Yevgeny
Try using IManagedProject and IConfiguration

--
Derek
Re: Programatically create a project? [message #496689 is a reply to message #496048 ] Tue, 10 November 2009 18:07 Go to previous message
Yevgeny Shifrin  is currently offline Yevgeny Shifrin
Messages: 42
Registered: July 2009
Member
Hi Derek,

Thank you for trying to help. I am not sure I understand your suggestion.

I changed the code from:
ManagedProject mProj = new ManagedProject(des);
Configuration cfg = new Configuration( mProj,null,"your.configuration.id", "YourConfigurationName");
to:
IManagedProject mProj = new ManagedProject(des);
IConfiguration cfg = new Configuration( mProj,null,"your.configuration.id", "YourConfigurationName");

I still need to allocate ManagedProject and Configuration objects so they still must be added to the import. Am I missing something?

Thanks,
Yevgeny
Previous Topic:Remote Debug C++ Multithreads
Next Topic:Error Highlighting in Eclipse C++
Goto Forum: