Spec File Changes For Subpackages
The creation of subpackages is based strictly on the contents of the spec file. This doesn't mean that you'll have to learn an entirely new set of tags, conditionals, and directives in order to create subpackages. In fact, you'll only need to learn one.
The primary change to a spec file is structural and starts with the definition of a preamble for each subpackage.
When we introduced RPM package building in Chapter 10, we said that every spec file contains a preamble. The preamble contains a variety of tags that define all sorts of information about the package. In a single package situation, the preamble must be at the start of the spec file. The spec file we're creating will have one there, too.
When creating a spec file that will build subpackages, each subpackage also needs a preamble of its own. These "sub-preambles" need only define information for the subpackage when that information differs from what is defined in the main preamble. For example, if we wanted to define an installation prefix for a subpackage, we would add the appropriate prefix tag to that subpackage's preamble. That subpackage would then be relocatable.
In a single-package spec file, there is nothing that explicitly identifies the preamble, other than its position at the top of the file. For subpackages, however, we need to be a bit more explicit. So we use the %package directive to identify the preamble for each subpackage.
The %package directive actually performs two functions. As we mentioned above, it is used to denote the start of a subpackage's preamble. It also plays a role in forming the subpackage's name. As an example, let's say the main preamble contains the following name tag:
name: foo |
Later in the spec file, there is a %package directive:
%package bar |
This would result in the name of the subpackage being foo-bar.
In this way, it's easy to see the relationship of the subpackage to the main package (or other subpackages, for that matter). Of course, this naming convention might not be appropriate in every case. So there is an option to the %package directive for just this circumstance.
The -n option is used to change the final name of a subpackage from <mainpackage>-<subpackage> to <subpackage>. Let's modify the %package directive in our example above to be:
%package -n bar |
The result is that the subpackage name would then be bar instead of foo-bar.
Updating Our Spec File
Let's apply some of our newly found knowledge to the spec file we're writing. Here's the list of subpackages that we need to create:
- The server subpackage, to be called foo-server.
- The client subpackage, to be called foo-client.
- The baz development library subpackage, to be called bazlib.
Since our package name is foo, and since the %package directive creates subpackage names by prepending the package name, the %package directives for the foo-server and foo-client subpackages would be written as:
%package server %package client |
Since the baz library's package name is not to start with foo, we need to use the -n option on its %package directive:
%package -n bazlib |
Our requirements further state that foo-server and foo-client are to have the same version as the main package.
One of the time-saving aspects of using subpackages is that there is no need to duplicate information for each subpackage if it is already defined in the main package. Therefore, since the main package's preamble has a version tag defining the version as 2.7, the two subpackages that lack a version tag in their preambles will simply inherit the main package's version definition.
Since the bazlib subpackage's preamble contains a version tag, it must have its own unique version.
In addition, each subpackage must have its own summary tag.
So based on these requirements, our spec file now looks like this:
Name: foo Version: 2.7 Release: 1 Source: foo-2.7.tgz License: probably not Summary: The foo app, and the baz library needed to build it Group: bogus/junque %description This is the long description of the foo app, and the baz library needed to build it... %package server Summary: The foo server %package client Summary: The foo client %package -n bazlib Version: 5.6 Summary: The baz library |
We can see the subpackage structure starting to appear now.
Required Tags In Subpackages
There are a few more tags we should add to the subpackages in our example spec file. In fact, if these tags are not present, RPM will issue a most impressive warning:
# rpmbuild -ba foo-2.7.spec* Package: foo * Package: foo-server Field must be present : Description Field must be present : Group * Package: foo-client Field must be present : Description Field must be present : Group * Package: bazlib Field must be present : Description Field must be present : Group Spec file check failed!! Tell rpm-list@redhat.com if this is incorrect. # |
Our spec file is incomplete. The bottom line is that each subpackage must have these three tags:
- The %description tag.
- The group tag.
- The summary tag.
It's easy to see that the first two tags are required, but what about summary? Well, we lucked out on that one: we already included a summary for each subpackage in our example spec file.
Let's take a look at the %description tag first.
As you've probably noticed, the %description tag differs from other tags. First of all, it starts with a percent sign. Secondly, its data can span multiple lines. The third difference is that the %description tag must include the name of the subpackage it describes. This is done by appending the subpackage name to the %description tag itself. So given these %package directives:
%package server %package client %package -n bazlib |
our %description tags would start with:
%description server %description client %description -n bazlib |
Notice that we've included the -n option in the %description tag for bazlib. This was intentional, as it makes the name completely unambiguous.
OK, let's take a look at the spec file after we've added the appropriate %descriptions, along with group tags for each subpackage:
Name: foo Version: 2.7 Release: 1 Source: foo-2.7.tgz License: probably not Summary: The foo app, and the baz library needed to build it Group: bogus/junque %description This is the long description of the foo app, and the baz library needed to build it... %package server Summary: The foo server Group: bogus/junque %description server This is the long description for the foo server... %package client Summary: The foo client Group: bogus/junque %description client This is the long description for the foo client... %package -n bazlib Version: 5.6 Summary: The baz library Group: bogus/junque %description -n bazlib This is the long description for the bazlib... |
Let's take a look at what we've done. We've created a main preamble as we normally would. We then created three additional preambles, each starting with a %package directive. Finally, we added a few tags to the subpackage preambles.
But what about version tags? Aren't the server and client subpackages missing them?
Not really. Remember that if a subpackage is missing a given tag, it will inherit the value of that tag from the main preamble. We're well on our way to having a complete spec file, but we aren't quite there yet.
Let's continue by looking at the next part of the spec file that changes when building subpackages.
In an ordinary single-package spec file, the %files list is used to determine which files are actually going to be packaged. It is no different when building subpackages. What is different, is that there must be a %files list for each subpackage.
Since each %files list must be associated with a particular %package directive, we simply label each %files list with the name of the subpackage, as specified by each %package directive. Going back to our example, our %package lines were:
%package server %package client %package -n bazlib |
Therefore, our %files lists should start with:
%files server %files client %files -n bazlib |
In addition, we need the main package's %files list, which remains unnamed:
%files |
The contents of each %files list is dictated entirely by the software's requirements. If, for example, a certain file needs to be packaged in more than one package, it's perfectly all right to include the filename in more than one list.
The %files list wields considerable power over subpackages. It's even possible to prevent a package from being created by using the %files list. But is there a reason why you'd want to go to the trouble of setting up subpackages, only to keep one from being created?
Actually, there is. Take, for example, the case where client/server-based software is to be packaged. Certainly, it makes sense to create two subpackages: one for the client and one for the server. But what about the main package? Is there any need for it?
Quite often there's no need for a main package. In those cases, removing the main %files list entirely will result in no main package being built.
Please keep in mind that an empty %files list (ie, a %files list that contains no files) is not the same as not having a %files list at all. As we noted above, entirely removing a %files list results in RPM not creating that package. However, if RPM comes across a %files list with no files, it will happily create an empty package file.
This feature (which also works with subpackage %files lists) comes in handy when used in concert with conditionals. If a %files list is enclosed by a conditional, the package will be created (or not) based on the evaluation of the conditional.
Our Spec File So Far…
Ok, let's update our example spec file. Here's what it looks like after adding each of the subpackages' %files lists:
Name: foo Version: 2.7 Release: 1 Source: foo-2.7.tgz License: probably not Summary: The foo app, and the baz library needed to build it Group: bogus/junque %description This is the long description of the foo app, and the baz library needed to build it... %package server Summary: The foo server Group: bogus/junque %package client Summary: The foo client Group: bogus/junque %package -n bazlib Version: 5.6 Summary: The baz library Group: bogus/junque %files /usr/local/foo-file %files server /usr/local/server-file %files client /usr/local/client-file %files -n bazlib /usr/local/bazlib-file |
As you can see we've added %files lists for:
- The main foo package.
- The foo-server subpackage.
- The foo-client subpackage.
- The bazlib subpackage.
Each package contains a single file. [1] If there was no need for a main package, we could simply remove the unnamed %files list. Keep in mind that even if you do not create a main package, the tags defined in the main package's preamble will appear somewhere — specifically, in the source package file.
Let's look at the last subpackage-specific part of the spec file: the install- and erase-time scripts.
Install- and Erase-time Scripts
The install- and erase-time scripts, %pre, %preun, %post, and %postun, can all be named using exactly the same method as was used for the other subpackage-specific sections of the spec file. The script used during package verification, %verifyscript, can be made package-specific as well. Using the subpackage structure from our example spec file, we would end up with script definitions like:
- %pre server
- %postun client
- %preun -n bazlib
- %verifyscript client
Other than the change in naming, there's only one thing to be aware of when creating scripts for subpackages. It's important that you consider the possibility of scripts from various subpackages interacting with each other. Of course, this is simply good script-writing practice, even if the packages involved are not related.
Here we've added some scripts to our spec file. So that our example doesn't get too complex, we've just added preinstall scripts for each package:
Name: foo Version: 2.7 Release: 1 Source: foo-2.7.tgz License: probably not Summary: The foo app, and the baz library needed to build it Group: bogus/junque %description This is the long description of the foo app, and the baz library needed to build it... %package server Summary: The foo server Group: bogus/junque %description server This is the long description for the foo server... %package client Summary: The foo client Group: bogus/junque %description client This is the long description for the foo client... %package -n bazlib Version: 5.6 Summary: The baz library Group: bogus/junque %description -n bazlib This is the long description for the bazlib... %pre echo "This is the foo package preinstall script" %pre server echo "This is the foo-server subpackage preinstall script" %pre client echo "This is the foo-client subpackage preinstall script" %pre -n bazlib echo "This is the bazlib subpackage preinstall script" %files /usr/local/foo-file %files server /usr/local/server-file %files client /usr/local/client-file %files -n bazlib /usr/local/bazlib-file |
As pre-install scripts go, these don't do very much. But they will allow us to see how subpackage-specific scripts can be defined.
Those of you that have built packages before probably realize that our spec file is missing something. Let's add that part now.
Building Subpackages
Now it's time to give our example spec file a try. The build process is not that much different from a single-package spec file:
# rpmbuild -ba foo-2.7.spec* Package: foo * Package: foo-server * Package: foo-client * Package: bazlib … Executing: %prep … Executing: %build … Executing: %install … Executing: special doc + cd /usr/src/redhat/BUILD + cd foo-2.7 + DOCDIR=//usr/doc/foo-2.7-1 + DOCDIR=//usr/doc/foo-server-2.7-1 + DOCDIR=//usr/doc/foo-client-2.7-1 + DOCDIR=//usr/doc/bazlib-5.6-1 + exit 0 Binary Packaging: foo-2.7-1 Finding dependencies... usr/local/foo-file 1 block Generating signature: 0 Wrote: /usr/src/redhat/RPMS/i386/foo-2.7-1.i386.rpm Binary Packaging: foo-server-2.7-1 Finding dependencies... usr/local/server-file 1 block Generating signature: 0 Wrote: /usr/src/redhat/RPMS/i386/foo-server-2.7-1.i386.rpm Binary Packaging: foo-client-2.7-1 Finding dependencies... usr/local/client-file 1 block Generating signature: 0 Wrote: /usr/src/redhat/RPMS/i386/foo-client-2.7-1.i386.rpm Binary Packaging: bazlib-5.6-1 Finding dependencies... usr/local/bazlib-file 1 block Generating signature: 0 Wrote: /usr/src/redhat/RPMS/i386/bazlib-5.6-1.i386.rpm … Source Packaging: foo-2.7-1 foo-2.7.spec foo-2.7.tgz 4 blocks Generating signature: 0 Wrote: /usr/src/redhat/SRPMS/foo-2.7-1.src.rpm # |
Starting at the top, we start the build with the usual command. Immediately following the command, RPM indicates that four packages are to be built from this spec file. The %prep, %build, and %install scripts then execute as usual.
Next, RPM executes its "special doc" internal script, even though we haven't declared any files to be documentation. It's worth noting, however, that the DOCDIR
environment variables show that if the spec file had declared some of the files as documentation, RPM would have created the appropriate documentation directories for each of the packages.
At this point, RPM creates the binary packages. As we can see, each package contains the file defined in its %files list.
Finally, the source package file is created. It contains the spec file and the original sources, just like any other source package.
One spec file. One set of sources. One build command. Four packages. [1] All in all, a pretty good deal, isn't it?
Let's take a look at our newly created packages. As with any other package, each subpackage should be tested by installing it on a system that has not had that software installed before. In this section, we'll just snoop around the subpackages and point out how they differ from packages built one to a spec file.
First, let's just look at each package's information:
# rpm -qip foo-2.7-1.i386.rpmName : foo Distribution: (none) Version : 2.7 Vendor: (none) Release : 1 Build Date: Wed Nov 06 13:33:37 1996 Install date: (none) Build Host: foonly.rpm.org Group : bogus/junque Source RPM: foo-2.7-1.src.rpm Size : 35 Summary : The foo app, and the baz library needed to build it Description : This is the long description of the foo app, and the baz library needed to build it... # # rpm -qip foo-server-2.7-1.i386.rpmName : foo-server Distribution: (none) Version : 2.7 Vendor: (none) Release : 1 Build Date: Wed Nov 06 13:33:37 1996 Install date: (none) Build Host: foonly.rpm.org Group : bogus/junque Source RPM: foo-2.7-1.src.rpm Size : 42 Summary : The foo server Description : This is the long description for the foo server... # # rpm -qip foo-client-2.7-1.i386.rpmName : foo-client Distribution: (none) Version : 2.7 Vendor: (none) Release : 1 Build Date: Wed Nov 06 13:33:37 1996 Install date: (none) Build Host: foonly.rpm.org Group : bogus/junque Source RPM: foo-2.7-1.src.rpm Size : 42 Summary : The foo client Description : This is the long description for the foo client... # # rpm -qip bazlib-5.6-1.i386.rpmName : bazlib Distribution: (none) Version : 5.6 Vendor: (none) Release : 1 Build Date: Wed Nov 06 13:33:37 1996 Install date: (none) Build Host: foonly.rpm.org Group : bogus/junque Source RPM: foo-2.7-1.src.rpm Size : 38 Summary : The baz library Description : This is the long description for the bazlib... # |
Here we've used RPM's query capability to display a list of summary information for each package. A few points are worth noting.
First, each package lists foo-2.7-1.src.rpm as its source package file. This is the only way to tell if two package files were created from the same set of sources. Trying to use a package's name as an indicator is futile, as the bazlib package shows us.
The next thing to notice is that the summaries and descriptions for each package are specific to that package. Since these tags were placed and named according to each package, that should be no surprise.
Finally, we can see that each package's version has been either "inherited" from the main package's preamble, or, as in the case of the bazlib package, the main package's version has been overridden by a version tag added to bazlib's preamble.
If we look at the source package's information, we see that its information has been taken entirely from the main package's set of tags:
# rpm -qip foo-2.7-1.src.rpmName : foo Distribution: (none) Version : 2.7 Vendor: (none) Release : 1 Build Date: Wed Nov 06 13:33:37 1996 Install date: (none) Build Host: foonly.rpm.org Group : bogus/junque Source RPM: (none) Size : 1415 Summary : The foo app, and the baz library needed to build it Description : This is the long description of the foo app, and the baz library needed to build it... # |
It's easy to see that if there was no %files list for the main package, and therefore, no main package, the tags in the main preamble would still be used in the source package. This is why RPM enforces the requirement that the main preamble contain copyright, %description, and group tags. So, here's a word to the wise: Don't put something stupid in the main preamble's %description just to satisfy RPM. Your witty saying will be immortalized for all time in every source package you distribute. [2]
The easiest way to verify that the %pre scripts we defined for each package were actually used is to simply install each package:
# rpm -Uvh foo-2.7-1.i386.rpmfoo This is the foo package preinstall script ################################################## # # rpm -Uvh foo-server-2.7-1.i386.rpmfoo-server This is the foo-server subpackage preinstall script ################################################## # # rpm -Uvh foo-client-2.7-1.i386.rpmfoo-client This is the foo-client subpackage preinstall script ################################################## # # rpm -Uvh bazlib-5.6-1.i386.rpmbazlib This is the bazlib subpackage preinstall script ################################################## # |
As expected, the unique %pre script for each package has been included. Of course, if we hadn't wanted to actually install the packages, we could have used RPM's --scripts option to display the scripts:
# rpm -qp --scripts foo-2.7-1.i386.rpmpreinstall script: echo "This is the foo package preinstall script" postinstall script: (none) preuninstall script: (none) postuninstall script: (none) verify script: (none) # |
This approach might be a bit safer, particularly if installing the newly built package would disrupt operations on your build system.