Note that for language with GTIRB APIs (at least C++, Python, Common Lisp) using the GTIRB API is preferrable to manipulating protobuf directly.
GTIRB uses a serialized format that consists of an 8-byte signature followed by serialized protobuf data. The protobuf data allows for exploration and manipulation in the language of your choice. The Google protocol buffers homepage lists the languages in which protocol buffers can be used directly; users of other languages can convert the protobuf-formatted data to JSON format and then use the JSON data in their applications. In the future we intend to define a standard JSON schema for GTIRB.
The 8-byte signature that prefixes the protobuf data includes both GTIRB's magic number and the version fot GTIRB's protobuf specification that is in use. The layout is as follows:
- Bytes 0-4 contain the ASCII characters:
GTIRB
. - Bytes 5-6 are considered reserved for future use and should be 0.
- Byte 7 contains the GTIRB protobuf spec version in use.
Directory gtirb/src/proto
contains the protocol buffer message type
definitions for GTIRB. You can inspect these .proto
files to
determine the structure of the various GTIRB message types. The
top-level message type is IR
.
If you have not used protocol buffers before, there are several useful resources available at https://developers.google.com/protocol-buffers/, including an installation guide and a tutorial.
In general, writing an application to use GTIRB data in protocol buffer format will involve the following steps.
-
Install the protocol buffer compiler (
protoc
) from https://github.com/protocolbuffers/protobuf/releases, if you haven't already done so. -
Install any required protocol buffer library or libraries for the programming language you are using.
-
Invoke the protocol buffer compiler on the
.proto
files ingtirb/src/proto/
to generate code in the language you wish to use. -
Write your application, importing/including the file or files you generated in step 3.
The Protocol Buffers API Reference provides language-specific instructions for the various supported programming languages, along with links to information for cases where support is provided by third-party plug-ins.
To create a Python application that uses serialized GTIRB data, do the following.
-
Install the protocol buffer compiler (
protoc
). -
Install the Python protobuf library, if you haven't already done so.
$ pip install protobuf
-
Generate Python message definitions in a dedicated directory (for example,
python/
).$ mkdir -p python $ for f in src/proto/*.proto; do protoc -Isrc/proto --python_out=python $f done
This will create a number of files with names of the form
<bn>_pb2.py
in thepython/
subdirectory of your working directory: one for each<bn>.proto
in src/proto/, includingIR_pb2.py
. -
Write your application. Make sure that it imports
IR_pb2
, or the parts of it that you require. -
Run your application, making sure that the directory containing your message definitions is in the
PYTHONPATH
.
Directory gtirb/doc/examples
contains several example Python scripts
that use protocol buffers to explore serialized GTIRB data.
To create a Java application that uses serialized GTIRB data, do the following.
-
Install the protocol buffer compiler (
protoc
). -
Download the
protobuf
Java runtime from https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java and save it somewhere suitable. -
Generate Java message definitions in a dedicated directory (for example,
java/
).$ mkdir -p java $ for f in src/proto/*.proto; do protoc -Isrc/proto --java_out=java $f done
This will create a subdirectory
java/proto/', containing a number of files with names of the form
OuterClass.java: one for each
.protoin
src/proto/`. -
Compile the Java message definitions, making sure the
protobuf
Java runtime.jar
file is in yourCLASSPATH
.$ mkdir -p java/classfiles $ CLASSPATH=<path/to/protobuf_jar>
javac -d java/classfiles java/proto/*.java(If you want to build a
.jar
file to combine all these.class
files, do so at this stage.) -
Write your application. Make sure that it imports all the classes you need from the
proto
package. -
Compile and run your application, making sure that your CLASSPATH contains both the
protobuf
Java runtime.jar
file and the location of the your compiled message definition classes.
Directory gtirb/doc/examples
contains several example Java programs
that use protocol buffers to explore serialized GTIRB data.