Next: , Previous: Defining text aliases, Up: Customizing through XML and Python files


16.5.15 Defining project attributes

The project files are required by GPS, and are used to store various pieces of information related to the current set of source files. This includes how to find the source files, how the files should be compiled, or manipulated through various tools, ....

However, the default set of attributes that are usable in a project file is limited to the attributes needed by the tool packaged with GPS or GNAT.

If you are delivering your own tools, you might want to store similar information in the project files themselves, since these are a very convenient place to associate some specific settings with a given set of source files.

GPS lets manipulate the contents of projects through XML customization files and script commands. You can therefore add you own typed attributes into the projects, so that they are saved automatically when the user saves the project, and reloaded automatically the next time GPS is started.

16.5.15.1 Declaring the new attributes

New project attributes can be declared in two ways: either using the advanced XML tags below, or using the <tool> tag (see Defining tool switches).

The customization files support the <project_attribute> tag, which is used to declare all the new attributes that GPS should expect in a project. Attributes that have not been declared explictly will not be accessible through the GPS scripting languagues, and will generate warnings in the Messages window.

Project attributes are typed: they can either have a single value, or have a set of such values (a list). The values can in turn be a free-form string, a file name, a directory name, or a value extracted from a list of preset values.

Attributes that have been declared in these customization files will also be graphically editable through the project properties dialog, or the project wizard. Therefore, you should specify when an attribute is defined how it should be presented to the GPS user.

The <project_attribute> tag accepts the following attributes:

16.5.15.2 Declaring the type of the new attributes

The type of the project attribute is specified through one or several child tags of <project_attribute>. The following tags are recognized.

In some cases, the type of the project attribute, or at least its default value, depends on what the attribute applies to. The project file support this in the form of indexed project attribute. This is for instance used to specify what should be the name of the executable generated when compiling each of the main files in the project (ie the executable name for gps.adb should be gps.exe, the one for main.c should be myapp.exe, and so on).

Such attributes can also be declared through XML files. In such cases, the <project_attribute> tag should have one <index> child, and zero or more <specialized_index> children. Each of these two tags in turn take one of the already mentioned <string>, <choice> or <shell> tag.

The <index> tag indicates what other project attribute is used to index the current one. In the example given above for the executable names, the index is the attribute that contains the list of main files for the project.

It accepts the following XML attributes:

The <specialized_index> is used to override the default type of the attribute for specific values of the index. For instance, the project files contains an attribute that specify what the name of the compiler is for each language. It is indexed on the project attribute that list the languages used for the source files of the project. Its default value depends on the language ("gnatmake" for Ada, "gcc" for C, and so on). This attribute accepts requires one XML attribute:

Note that almost all the standard project attributes are defined through an XML file, projects.xml, which is part of the GPS installation. Check this file to get advanced examples on how to declare project attributes.

16.5.15.3 Examples

The following example declares three attributes, with a single string as their value. This string represents a file or a directory in the last two cases. You can simply copy this into a .xml file in your $HOME/.gps/plug-ins directory, as usual.

     <?xml version="1.0"?>
     <custom>
       <project_attribute
           name="Single1"
           package="Test"
           editor_page="Tests single"
           editor_section="Single"
           description="Any string">
     
          <string default="Default value" />
       </project_attribute>
     
       <project_attribute
           name="File1"
           package="Test"
           editor_page="Tests single"
           editor_section="Single"
           description="Any file" >
     
           <string type="file" default="/my/file" />
       </project_attribute>
     
       <project_attribute
           name="Directory1"
           package="Test"
           editor_page="Tests single"
           editor_section="Single"
           description="Any directory" >
     
           <string type="directory" default="/my/directory/" />
       </project_attribute>
     </custom>

The following example declares an attribute whose value is a string. However, a list of predefined possible values is also provided, as an help for interactive edition for the user. If the <string> tag wasn't given, the attribute's value would have two be one of the three possible choices.

     <?xml version="1.0" ?>
     <custom>
       <project_attribute
           name="Static2"
           package="Test"
           editor_page="Tests single"
           editor_section="Single"
           description="Choice from static list (or any string)" >
     
           <choice>Choice1</choice>
           <choice default="true" >Choice2</choice>
           <choice>Choice3</choice>
           <string />
       </project_attribute>
     </custom>

The following example declares an attribute whose value is one of the languages currently supported by GPS. Since this list of languages is only know when GPS is executed, a script command is used to query this list.

     <?xml version="1.0" ?>
     <custom>
      <project_attribute
           name="Dynamic1"
           package="Test"
           editor_page="Tests single"
           editor_section="Single"
           description="Choice from dynamic list" >
     
           <shell default="C" >supported_languages</shell>
       </project_attribute>
     </custom>

The following example declares an attribute whose value is a set of file names. The order of files in this list matters to the tools that are using this project attribute.

     <?xml version="1.0" ?>
     <custom>
      <project_attribute
           name="File_List1"
           package="Test"
           editor_page="Tests list"
           editor_section="Lists"
           list="true"
           ordered="true"
           description="List of any file" >
     
           <string type="file" default="Default file" />
       </project_attribute>
     </custom>

The following example declares an attribute whose value is a set of predefined possible values. By default, two such values are selected, unless the user overrides this default setting.

     <?xml version="1.0" ?>
     <custom>
       <project_attribute
           name="Static_List1"
           package="Test"
           editor_page="Tests list"
           editor_section="Lists"
           list="true"
           description="Any set of values from a static list" >
     
           <choice>Choice1</choice>
           <choice default="true">Choice2</choice>
           <choice default="true">Choice3</choice>
       </project_attribute>
     </custom>

The following example declares an attribute whose value is a string. However, the value is specific to each language (this could for instance be used for the name of the compiler to use for a given language). This is an indexed project attribute. It has two default values, one for Ada, one for C. All other languages have no default value.

     <?xml version="1.0" ?>
     <custom>
       <project_attribute
           name="Compiler_Name"
           package="Test"
           editor_page="Tests indexed"
           editor_section="Single"
           <index attribute="languages" package="">
              <string default="" />
           </index>
           <specialized_index value="Ada" >
              <string default="gnatmake" />
           </specialized_index>
           <specialized_index value="C" >
              <string default="gcc" />
           </specialized_index>
       </project_attribute>
     </custom>
16.5.15.4 Accessing the project attributes

The new attributes that were defined are accessible from the GPS scripting languages, like all the standard attributes, see Querying project switches.

You can for instance access the Compiler_Name attribute we created above with a python command similar to:

     GPS.Project.root().get_attribute_as_string ("Compiler_Name", "Test", "Ada")

You can also access the list of main files for the project, for instance, by calling

     GPS.Project.root().get_attribute_as_list ("main")