Do you want to change compiler flags that the NetBeans IDE uses by default for compiling C/C++ files?
Do you want to add a compiler that is not supported by the NetBeans IDE? This article can help you to do this!
This tutorial demonstrates how to edit existing tool collections or create a custom tool collection
in NetBeans IDE 6.9.
Tool Collections
A tool collection is a set of compilers, make utility, and debugger. Every
time you compile, make, or debug your code, the
NetBeans IDE runs an executable file described in the tool collection descriptor.
Code Assistance features use include paths and macros taken from the tool collection.
The tool collection descriptor is an xml file located in the module
${NB}/cnd/modules/org-netbeans-modules-cnd-toolchain.jar
in the folder org/netbeans/modules/cnd/toolchain/resources/toolchaindefinition/. You should
not edit these original NetBeans tool descriptor xml files. This article tells
you how to create duplicates that you can edit.
Editing Tool Collection Descriptors
Editing a tool collection descriptor is a bit tricky since the NetBeans IDE
does not provide a GUI for editing descriptors. But here we describe how to
make dupluicate "shadow" copies of the tool collection descriptors to enable you to edit them
to change the way the IDE uses the tools.
Exit the IDE if it is running.
Add the following flag to the netbeans_default_options entry in the file ${NB}/etc/netbeans.conf:
-J-Dcnd.toolchain.personality.create_shadow=true
Start the IDE, choose Tools > Options, and select C/C++ in the top pane of
the Options window. The IDE creates "shadow" versions of the tool collection descriptors in your user
directory:
${userdir}/config/CND/ToolChain
The list of descriptors created should be similar to the following:
You can edit the descriptor for the toolchain you are using, and the IDE will use the descriptor after you
restart. See the Details for the Tool Collection XML File for information about
the tags used in these descriptor XML fies.
Remove the -J-Dcnd.toolchain.personality.create_shadow=true flag before you start IDE next time.
The IDE needs only once to create the shadow descriptors.
Creating a Custom Tool Collection
You can create your own tool collection. The following example creates a new
NetBeans module that contains a new tool collection. For general information about
creating NetBeans modules,
see NetBeans Plugin Quick Start.
Make sure your NetBeans installation includes the NetBeans Plugin Development module
by choosing Tools > Plugins and clicking the Installed tab. If the
NetBeans Plugin Development module is not installed, click the
Available Plugins tab and install the module. You can find it quickly by typing "plugin" in the
Search box.
Create a new NetBeans module by choosing File > New Project.
On the Choose Project page of the New Project wizard, select the category
NetBeans Modules and the project Module in the first step of the wizard.
Click Next.
On the Name and Location page, type the project name, for example mytoolchain
and leave all other fields as is. Cick Next.
On the Basic Module Configuration page,
type the code name base, for example
org.myhome.mytoolchain, and select the check box Generate XML Layer. Click
Finish. The IDE creates a new project.
Find the file layer.xml in the Projects window, under the Source Packages node.
Double-click
the layer.xml file to open it in the editor.
Copy and paste the following text inside the filesystem tag:
Open the Bundle.properties file and add the following string: CND/ToolChains/GNU_tuned=My GNU Tuned Tool Collection
Create the subpackage toolchain by right-clicking the mytoolchain package under
Source Packages and selecting New > Java Package.
Create a new file in the toolchain package by right-clicking the toolchain subpackage
and selecting New > Empty File. Name the file GNU_tuned_flavor.xml and click Next.
Copy and paste the following text in GNU_tuned_flavor.xml:
Create another new file in the toolchain package by right-clicking the toolchain subpackage
and selecting New > Empty File. Name the file GNU_tuned_cpp.xml and click Next.
Copy and paste the following text in GNU_tuned_cpp.xml:
Notice the debug flags are set to -g3 and -gdwarf-2, which are different from the
flags set in the default GNU tool collection description.
The project tree looks like the following:
In the Projects window, right-click the mytoolchain project node and choose Run.
The module is built and installed in a new instance of the IDE, which is the
default target platform of modules. The target platform opens so that you can try out the new module.
In the running module,
choose Tools > Options, select C/C++ from the top pane
of the Options window, and select the Build Tools tab.
If the new tool collection (GNU_tuned) is not shown, click Restore Default. Click Yes
to continue when prompted to rescan your environment.
The IDE shows the new tool collection:
Create a new C/C++ sample project Welcome by selecting File >
New Project > Samples > C/C++ > Welcome.
Right-click the project node and choose Properties. In the Project
Properties dialog box, select the Build node, set the Tool Collection to
your GNU_tuned tool collection and click OK.
Build the project. Note that the compiler has flags -g3 -gdwarf-2
So in the GNU_tuned tool collection, we have managed to change compiler flags for debug, Development Mode
for the GNU compiler.
If you want to use the new module for the GNU_tuned tool collection
in your regular IDE, you can create a binary (a .nbm file) and add it as a plugin:
Right-click the mytoolchain module project and select Create NBM. The .nbm file is created in the
build subdirectory of the project, which you can see in the Files tab.
Select Tools > Plugins, then click the Downloaded tab in the Plugins dialog box.
Click Add Plugins, navigate to the build directory, select the module .nbm file, and click Open.
The plugin module is added to the list in the Downloaded tab.
Click the checkbox to select the module in the Downloaded tab, then click the Install button.
The NetBeans Plugin Installer opens.
Click Next to advance through the installer, and continue until the installer is finished.
Restart the IDE, and then choose Tools > Options, select C/C++ from the top pane
of the Options window, and select the Build Tools tab.
If the new tool collection (GNU_tuned) is not shown, click Restore Default. Click Yes
to continue when prompted to rescan your environment.
Details for the Tool Collection XML File
This is a description of most important tags in the tool collection xml file.
For the scheme of all supported tags and attributes of toolchain xml files, you can
refer to the file
toolchaindefinition.xsd
in the NetBeans IDE 6.9 source tree.
Tool collection definition tags
Tags
Attributes
Description
toolchain
Name of tool collection
name
Name of tool collection
display
Display name of tool collection
family
Group name of tool collection
platforms
Supported platforms
stringvalue
List of supported platforms separated by comma. Possible values are:
linux
unix
sun_intel
sun_sparc
windows
mac
none
makefile_writer
Custom makefile writer.
class
Class name of custom makefile writer. It should implement
org.netbeans.modules.cnd.makeproject.spi.configurations.MakefileWriter.
drive_letter_prefix
Special prefix for file names
stringvalue
"/" for unix "/cygdrive/" for cygwin on Windows
base_folders
Container for base_folder tags.
One or more base_folder tags are contained in one base_folders tag.
base_folder
Description of base directory for compilers.
This tag can contain the following tags:
regestry
Windows registry key of the tool. Note that the XML tag must
be spelled "regestry" although this is a mispelling.
pattern
Regular expression that allows NetBeans IDE to find compiler in registry
suffix
Folder with executable files
path_patern
Regular expression that allows NetBeans IDE to find compiler by scanning paths.
Note that the XML tag must
be spelled "path_patern" although this is a mispelling.
command_folders
Container for command_folder tags.
One or more commander_folder tags are contained in one command_folders tag.
command_folder
Describes the directory where UNIX-like commands are located.
Only needed for MinGW compiler on Windows. The command_folder tag
can contain the following tags:
regestry
Windows registry key of commands. Note that the XML tag must
be spelled "regestry" although this is a mispelling.
pattern
Regular expression that allows NetBeans IDE to find the commands folder in the registry
suffix
Folder with executable files
path_patern
Regular expression that allows NetBeans IDE to find commands. Note that the XML tag must
be spelled "path_patern" although this is a mispelling.
This table lists the tags used to describe the compilers and specify compiler flags for the toolchain.
Tags
Description
Example for GNU compiler
c,cpp
Set of compiler flags are located in following sub nodes
recognizer
Regular expression that allow to find compiler
For GNU under cygwin on Windows .*[\\/].*cygwin.*[\\/]bin[\\/]?$
compiler
Compiler name (name of executable file)
gcc or g++
version
Version flag
--version
system_include_paths
Flags to get system include paths
-x c -E -v
system_macros
Flags to get system macros
-x c -E -dM
user_include
Flag to add user include path
-I
user_macro
Flag to add user macro
-D
development_mode
Groups of flags for different development modes
warning_level
Groups of flags for different warning level
architecture
Groups of flags for different architecture
strip
Flag for stripping debug information
-s
dependency_generation
Flags for dependency generation
-MMD -MP -MF $@.d
precompiled_header
Flags for precompiled header
-o $@
multithreading
Groups of flags for multithreading support
standard
Groups of flags for different language standards
language_extension
Groups of flags for different language extensions
Creating a Custom Compiler Provider
NetBeans IDE has a default compiler provider:
org.netbeans.modules.cnd.toolchain.compilers.MakeProjectCompilerProvider
This provider works well enough for most toolchains, but you can
also create own provider.
To create your own compiler provider:
extend abstract class org.netbeans.modules.cnd.api.compilers.CompilerProvider
define class as service and put it before default provider:
@org.openide.util.lookup.ServiceProvider(service = org.netbeans.modules.cnd.spi.toolchain.CompilerProvider.class,
position=500)
public class CustomCompilerProvider extends CompilerProvider {
...
}
The position=500 attribute guarantees that the custom provider will be invoked before the default provider.
The custom provider should return not NULL "Tool" in method "createCompiler()" for tool that should be overridden.
Creating a Custom Compiler Error Handler
NetBeans IDE has a two default compiler error handlers.
for GNU compiler
for Sun Studio compiler
The GNU compiler handler works well enough for any GNU compiler, but if you want you can
define your own compiler error handlers.
To create your own compiler error handler:
extend abstract class org.netbeans.modules.cnd.spi.toolchain.CompilerProvider
define class as service:
@org.openide.util.lookup.ServiceProvider(service = org.netbeans.modules.cnd.spi.toolchain.CompilerProvider.class)
public class CustomCompilerProvider extends ErrorParserProvider {
...
@Override
public String getID() {
return "MyParser"; // NOI18N
}
}
link error scanner and tool collection description by ID: