Perl Example: Hello World Script Package

Here is a simple script that we will package first:

#! /usr/bin/env perl

print "Hello World\n";

There is nothing special about this program other than instead of calling perl directly it calls the program env. The problem with scripts in general is that they need to have the first line hard-coded with the full path of the interpreter which executes the script. This is not a problem with interpreters like the Bourne shell which is always found at /bin/sh. However for a program such as perl there are no such guarantees. The env program is another executable that is always located in the same directory. It can be used to find perl and then execute it with the rest of the file. This is thus a cheap way to achieve relocatability. What is not addressed by this env is finding the correct version of perl. For this you would need a hard-coded path that is set by a setup package.

To turn this into a package we need to do a couple of things. The first is to decide where the script should be located in a user installation. Like most executables we will want that to be $GPT_INSTALL_LOCATION/bin. Next we need to create a couple of files.

The first file is called filelist and it is always located in the top directory of a package. This contains the a listing of the installed files. NOTE that this is not a listing of files in the source directory but rather a listing of files in the installation. Since we only have one file our filelist will only have one entry:

bin/hello-world.pl

Filelists can also be generated and/or modified during the build process. A filelist file only need to be present when gpt-build is installing the packaging data after building the software.

The second file that needs to be created is the source packaging data file called pkg_data_src.gpt. This file also needs to be in the top source directory (it can also be in a subdirectory called pkgdata). This file contains the build instructions for the package as well as package definitions. These definitions are automatically transposed into the binary packages that result from the build.

The first section of the file contains the XML header as well as the name, version, and other identity data of the package. The Aging_Version element has three attributes that are discussed here. One thing further to notice is that we switched the hyphen in the hello-world script to an underline in the pacakge name. Hyphens are special characters with GPT and other package managers. They are often used to delimit fields in a filename. It is legal to use a hyphen in a package name but not wise[1].


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gpt_package_metadata SYSTEM "package.dtd">

<gpt_package_metadata Name="hello_world" Format_Version="0.02">

    <Aging_Version Major="0" Minor="0" Age="0"/>
    <Description >Simple Packaging Example</Description>
    <Version_Stability Release="Experimental"/>

The other major section is the Build instructions shown below which instruct GPT how to install the script. As can be seen the build steps are standard bourne shell commands which need to be platform portable.[2]. The only thing special is INSTALLDIR_GPTMACRO which is a GPT build macro. A list of these macros are found here. This particular macro gets expanded into the user's installation location before the build step is executed.



    <Build_Instructions>

      <Build_Step>mkdir -p INSTALLDIR_GPTMACRO/bin</Build_Step>
      <Build_Step>cp hello-world.pl INSTALLDIR_GPTMACRO/bin</Build_Step>
      <Build_Step>chmod 755 INSTALLDIR_GPTMACRO/bin/hello-world.pl</Build_Step>

    </Build_Instructions>

The complete set of files for this package can be found here.

The directory containing these files can now be recognized as source by GPT. A gpt-build command will install the script and create a binary package entry in the installation:

bash$ cd examples/hello_world
bash$ gpt-build
gpt-build ====> CHECKING BUILD DEPENDENCIES FOR hello_world
gpt-build ====> Changing to /home/mbletzin/nmi/gpt-docs/examples/hello_world
gpt-build ====> BUILDING FLAVOR 
gpt-build ====> Changing to /home/mbletzin/install/globus/etc
gpt-build ====> REMOVING empty package hello_world-noflavor-dev
gpt-build ====> REMOVING empty package hello_world-noflavor-rtl

Running hello-world.pl will now work. In addition, running gpt-query shows that hello_world-noflavor-pgm pkg version: 0.0.0 is now a part of the installation. GPT has figured out for you that no build flavors have been used and that the script belongs in a pgm package.



[1] The Globus Toolkit™ had a coding convention where executable name delimiter was the hyphen and the delimiter for every other file was the underline character. This convention has been used in this book because the author is too old to change :).

[2] In this case the commands are semi-portable because the mkdir -p does not work on some older UNIX systems.