Author: C.J. Collier

  • Writing Free Software: Part 11 – A simple man page

    Introduction

    Okay… it’s high time we wrote some docs. I come from a perl background, so we’re going to write the docs using perldoc. I know there are better ways to do this for C# projects and I’m happy to take contributions to this series. Just ask for contributor status and you can post the next article :)

    Return to the workspace

    $ cd ~/src/greeting

    Create the POD

    $ mkdir doc
    $ cat > doc/greeting.pod.in

    =head1 NAME
    
    Greeting - Example to illustrate writing and distributing Free Software
    
    =head1 VERSION
    
    Version @PACKAGE_VERSION@
    
    =head1 SYNOPSIS
    
     Greeting will greet the planet specified numerically
     (1 = Mercury .. 9 = Pluto) or the world, given
     no argument or arg value 0.
    
     $ greeting
     Greetings World!
     $ greeting -n 1
     Greetings Mercury!
     $ greeting --number 3
     Greetings Earth!
    
    =head1 COPYRIGHT
    
    Copyright 2008 J. Random Hacker .
    
      Permission is granted to copy, distribute and/or modify
      this document under the terms of the GNU Free
      Documentation License, Version 1.2 or any later version
      published by the Free Software Foundation; with no
      Invariant Sections, with no Front-Cover Texts, and with
      no Back-Cover Texts.
    
    =cut
    

    Update configure.ac

    $ patch configure.ac
    @@ -11,11 +11,18 @@
    AC_MSG_ERROR([Can't find "gmcs" in your PATH])
    fi
     
    +AC_PATH_PROG(PERLDOC, perldoc, no)
    +if test "x$PERLDOC" = "x" ; then
    + AC_MSG_ERROR([Can't find "perldoc" in your PATH])
    +fi
    +
    AC_CONFIG_FILES([
    Makefile
    ndesk/Makefile
    src/Makefile
    greeting
    +doc/Makefile
    +doc/greeting.pod
    ])
    AC_OUTPUT
     
    @@ -24,6 +31,7 @@
    echo "Thank you for configuring '$PACKAGE_NAME' version '$PACKAGE_VERSION'"
    echo "Runtime: $RUNTIME"
    echo "C# compiler: $CSC"
    +echo "Perldoc interpreter: $PERLDOC"
    echo "installation prefix: $prefix"
    echo ""
    echo "================="

    Create doc/Makefile.am file for man page

    $ cat > doc/Makefile.am

    all: man1/greeting.1
    
    man1/greeting.1: man1 greeting.pod
            $(PERLDOC) -d man1/greeting.1 greeting.pod
    
    man1:
            mkdir man1
    
    clean:
            rm -rf man1
    
    noinst_docdir = $(prefix)/share/man/man1
    noinst_doc_DATA = man1/greeting.1
    
    DISTCLEAN_FILES = greeting.pod
    

    Update Makefile.am

    $ patch Makefile.am
    @@ -1,3 +1,3 @@
    -SUBDIRS = src ndesk
    +SUBDIRS = src ndesk doc
     
    bin_SCRIPTS = greeting

    Re-build Makefile files and configure script

    $ autoreconf

    Test

    $ make
    ...
    $ man -M doc greeting
    ...
    $ sudo make install
    ...
    $ man greeting

    Conclusion

    There’s a lot more to documentation than this, but like the other pieces of this series, this is an introduction that should get you to a point where you can distributed “just enough.” Some other good tools to look at:

    I’m serious about having others beef up this series with a 11.1, 11.2, etc. Leave a comment or catch me on IRC and I’ll hook you up with contributor status.

  • Next article in the “Writing Free Software” series

    Hey all,

    I’m not certain what the next topic should be. I’ve got a few ideas. Could the interested parties let me know what they’re curious most about?

    • documentation
    • distributing .exe.config and .dll.config files
    • inter-package dependencies using pkg-config
    • creating sub packages
    • test harnesses and the “check” target
    • the debian/ directory and .deb packages
    • … something completely different

    I’m looking for comments on this one!

  • Writing Free Software – Part 10: Subdirectories

    In this installment, I’ll cover creating different subdirectories for different parts of the code. We’ll move NDesk.Options.cs into an ndesk directory and Greeting.cs into a src directory. Some modifications need to be made to various pieces of the distribution in order for this to work out correctly. We’ll do the minimum required now and cover the pieces that will make it more extensible but complex in future entries.

    Return to the workplace

    $ cd ~/src/greeting

    Move the source to their new directories

    $ mkdir src ndesk
    $ mv NDesk.Options.cs ndesk
    $ mv Greeting.cs src
    $ mv Makefile.am src

    Create a new root Makefile.am file

    $ cat > Makefile.am
    SUBDIRS = src ndesk

    bin_SCRIPTS = greeting
    ^D

    Create ndesk/Makefile.am

    $ cat > ndesk/Makefile.am
    EXTRA_DIST = NDesk.Options.cs

    Alter src/Makefile.am to cope with new layout

    $ patch src/Makefile.am

    10,11c10,11
    < Greeting.exe: Greeting.cs NDesk.Options.cs
    < # the same as $(CSC) -out:Greeting.exe Greeting.cs
    ---
    > Greeting.exe: Greeting.cs $(top_srcdir)/ndesk/NDesk.Options.cs
    > # the same as $(CSC) -out:Greeting.exe Greeting.cs ...
    14,15c14
    < EXTRA_DIST = Greeting.cs NDesk.Options.cs
    <
    ---
    > EXTRA_DIST = Greeting.cs
    18,19d16
    <
    < bin_SCRIPTS = greeting
    ^D^D
    patching file src/Makefile.am

    Alter configure.ac to cope with new layout

    $ patch configure.ac

    @@ -13,6 +13,8 @@
     
     AC_CONFIG_FILES([
     Makefile
    +ndesk/Makefile
    +src/Makefile
     greeting
     ])
     AC_OUTPUT
    missing header for unified diff at line 1 of patch
    patching file configure.ac
    

    Prove that we didn’t break anything

    $$ automake && autoconf && ./configure
    ...
    $ make
    ...
    $ sudo make install
    ...
    $ greeting
    Greetings, World!

    Conclusion

    In this lesson, I gave an example of re-factoring the build system to use subdirectories. This method can be used to separate portions of one’s codebase. In addition, we saw the use of the $srcdir variable, which stores the location of the root of the source distribution.

    In future posts, we will use the work done in this lesson as a basis for some more re-factoring. We will also learn how to do things such as

    • creating sub projects
    • inter-package dependencies using pkg-config
  • Writing Free Software – Part 9: Creating the wrapper script

    A previous post covered altering the install target so that it places the .exe assembly into the filesystem of the installer’s computer. In this part, we will cover creating the so-called “wrapper” script, which is the way recommended by the mono project’s application deployment guidelines to make the assembly executable on the system outside of the source distribution.

    Return to the workplace

    $ cd ~/src/greeting/

    Create the wrapper script template

    $ cat > greeting.in
    #!/bin/bash
     
    exec @RUNTIME@ @prefix@/lib/mono/@PACKAGE_NAME@/Greeting.exe "$@"
    ^D

    Alter the configure.ac file to process the wrapper template

    $ patch configure.ac

    @@ -13,6 +13,7 @@
     
     AC_CONFIG_FILES([
     Makefile
    +greeting
     ])
     AC_OUTPUT

    ^D^D

    Alter the Makefile.am file to distribute the wrapper template

    $ cat >> Makefile.am
     
    bin_SCRIPTS = greeting
     
    ^D

    Re-build the Makefile and wrapper script

    $ autoconf && automake && ./configure

    Test the install target

    $ make && sudo make install
    $ which greeting
    /usr/local/bin/greeting
    $ greeting ––number 8
    Greetings, Neptune!

    Conclusion

    In this post, I gave an example of creating a simple wrapper shell script and using the autotools infrastructure to locate and use the system C# compiler and CLI runtime. This may not seem like much of a feat, but many systems have their binaries installed in weird locations and other quirks. The autotools system is able to detect and take care of these details without having the maintainer of the software create system-specific tweaks that would undoubtedly reduce the number of platforms on which the software would work.

  • Writing Free Software – Part 8: Exploring configure.ac variables

    Introduction

    One of autotools’ primary purposes is to allow software to be built and installed on a wide range of platforms. The configure script is responsible for a great deal of this flexibility. When it is run, it interrogates the system on which it is being run to determine the features available. If required features are missing, the script can do any number of things, including but not limited to:

    • reporting a problem and bailing out
    • trying an alternate approach
    • attempting to fetch the missing software and continuing/aborting depending on the outcome
    • recommending that the person running the configure script install the required bits
    • mocking the user

    In this post, I will note a few of the variables that can be used to determine the configuration of the system.

    Return to the workplace

    $ cd ~/src/greeting

    Modify configure.ac to report some findings

    $ patch configure.ac

    --- configure.ac.orig	2009-03-20 14:49:16.000000000 -0700
    +++ configure.ac	2009-03-20 14:46:16.000000000 -0700
    @@ -1,5 +1,11 @@
     AC_INIT([greeting],[0.0.1])
     AM_INIT_AUTOMAKE([1.9 tar-ustar])
    +
    +AC_PATH_PROG(RUNTIME, mono, no)
    +if test "x$RUNTIME" = "x" ; then
    +  AC_MSG_ERROR([Can't find "mono" in your PATH])
    +fi
    +
     AC_PATH_PROG(CSC, gmcs, no)
     if test "x$CSC" = "x" ; then
       AC_MSG_ERROR([Can't find "gmcs" in your PATH])
    @@ -8,3 +14,12 @@
     Makefile
     ])
     AC_OUTPUT
    +
    +echo "================="
    +echo ""
    +echo "Thank you for configuring '$PACKAGE_NAME' version '$PACKAGE_VERSION'"
    +echo "Runtime:             $RUNTIME"
    +echo "C# compiler:         $CSC"
    +echo "installation prefix: $prefix"
    +echo ""
    +echo "================="
    ^D^D
    

    Re-run configure to see results

    $ autoconf && ./configure
    checking for a BSD-compatible install... /usr/bin/install -c
    checking whether build environment is sane... yes
    checking for a thread-safe mkdir -p... /bin/mkdir -p
    checking for gawk... no
    checking for mawk... mawk
    checking whether make sets $(MAKE)... yes
    checking how to create a ustar tar archive... gnutar
    checking for mono... /usr/bin/mono
    checking for gmcs... /usr/bin/gmcs
    configure: creating ./config.status
    config.status: creating Makefile
    =================
     
    Thank you for configuring 'greeting' version '0.0.1'
    Runtime: /usr/bin/mono
    C# compiler: /usr/bin/gmcs
    installation prefix: /usr/local
     
    =================

    Conclusion

    One of the primary reasons that autotools is so powerful is that it can figure out what’s on your system and use it to build the software in the package. In following posts, we will make use of some of these system configuration variables.

  • Writing Free Software – Part 7: Creating a working install target

    Introduction

    This entry will show how to add the bits required for an install target. There are two pieces that need to be added to the Makefile.am:

    • target_DATA, which is a list of files associated with the target
    • targetdir, which is the location where the above files will be installed

    Return to the workplace

    $ cd ~/src/greeting

    Add the required variables to Makefile.am

    $ cat >> Makefile.am
    Greeting.exedir = $(prefix)/lib/mono/greeting
    Greeting.exe_DATA = Greeting.exe
    ^D
    $ sudo make install
    ...
    $ ls /usr/local/lib/mono/greeting/
    Greeting.exe

    Conclusion

    We now have the bits in place to put Greeting.exe in the correct directory, but no way to execute it. We’ll address that in a future post. Until then, you might be interested in reading through the application deployment guidelines

  • Writing Free Software – Part 6: Making a simple distribution

    You might remember from a previous post that we’ve already got a “dist” target which creates a tarball of the source. However, the source doesn’t include any of the code we wrote, only the files generated by autotools. In this article, we’ll make the modifications to the Makefile.am file required to include our source in the dist tarball.

    Return to our workspace

    $ cd ~/src/greeting

    Add an EXTRA_DIST automake variable

    $ echo "EXTRA_DIST = Greeting.cs NDesk.Options.cs" >> Makefile.am

    Re-build our Makefile

    $ ./configure

    Test the distdir target

    $ make distdir
    ...
    $ ls greeting-0.0.1
    AUTHORS Greeting.cs Makefile.in README configure.ac
    COPYING INSTALL NDesk.Options.cs aclocal.m4 install-sh
    ChangeLog Makefile.am NEWS configure missing
    $ make dist
    ...

    Conclusion

    Another short one. Sorry for the brevity. But as you can see, a little bit goes a long way.

  • Writing Free Software – Part 5: Consuming configure.ac variables

    This one has to be quick folks. The family needs my support this evening ;)

    In the last episode, we created a simple configure.ac and defined a variable, CSC. We didn’t change the Makefile other than re-naming it to Makefile.am. In this article, we’ll simply show how to consume the variable which was defined in configure.ac.

    Return to our workplace

    $ cd ~/src/greeting

    Alter Makefile.am to use the C# compiler defined in configure.ac

    $ sed -e 's/gmcs/\$\(CSC\)/' Makefile.am
    $ cat Makefile.am
    # the first target is the one executed when no target is named
    all: Greeting.exe
    #don't do anything
     
    # running 'make clean' will execute this target
    clean:
    rm Greeting.exe
     
    # running 'make Greeting.exe' will execute this target
    Greeting.exe: Greeting.cs NDesk.Options.cs
    # the same as $(CSC) -out:Greeting.exe Greeting.cs
    $(CSC) -out:$@ $^

    Prove that it didn’t break things


    $ automake && ./configure
    $ make clean
    cd . && /bin/sh /home/cjac/src/greeting/missing --run automake-1.10 --gnu Makefile
    cd . && /bin/sh ./config.status Makefile
    config.status: creating Makefile
    rm Greeting.exe
    $ make
    /usr/bin/gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs

    $ ./Greeting.exe ––number 9
    Greetings, Pluto!

    Conclusion

    That’s all I’ve got time for right now. Zelda’s crying for daddy!

  • Writing Free Software – Part 4: configure.ac

    In this section, we’ll create a minimalist configure.ac, re-name our Makefile to Makefile.am, generate a configure script, and use this configure script to produce a Makefile. The end result won’t do much more than our manually-created Makefile. It will, however, allow us to make use of the autoconf and automake infrastructure in future lessons. So let’s get started.

    Return to our workplace

    $ cd ~/src/greeting

    Create a configure.ac file

    $ cat > configure.ac
    AC_INIT([greeting],[0.0.1])
    AM_INIT_AUTOMAKE([1.9 tar-ustar])
    AC_PATH_PROG(CSC, gmcs, no)
    if test "x$CSC" = "x" ; then
      AC_MSG_ERROR([Can't find "gmcs" in your PATH])
    fi
    AC_CONFIG_FILES([
    Makefile
    ])
    AC_OUTPUT
    ^D

    Re-name your Makefile

    $ mv Makefile Makefile.am

    Install autoconf & automake

    $ sudo apt-get install autoconf automake

    Create files required by automake

    $ for i in NEWS README AUTHORS ChangeLog ; do touch $i ; done

    Import required m4 macros

    $ aclocal

    Ask automake to install missing pieces

    $ automake -a

    Process configure.ac and Makefile.am

    $ autoreconf

    Create a Makefile using configure script

    $ ./configure

    Test the targets we created before

    $ make
    gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs
    $ make clean
    rm Greeting.exe
    $ make Greeting.exe
    gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs

    Verify that our code still runs

    $ ./Greeting.exe -n 1
    Greetings, Mercury!
    $ ./Greeting.exe -1
    Greetings, World!
    $ ./Greeting.exe ––number 3
    Greetings, Earth!

    Try some of the targets that automake generates

    $ touch Makefile.in && make Makefile
     cd . && /bin/sh ./config.status Makefile 
    config.status: creating Makefile
    make: `Makefile' is up to date.
    $ make distdir
    ...
    $ ls greeting-0.0.1
    AUTHORS  ChangeLog  Makefile.am  NEWS    aclocal.m4  configure.ac  missing
    COPYING  INSTALL    Makefile.in  README  configure   install-sh
    $ make dist
    ...
    $ ls greeting-0.0.1.tar.gz 
    greeting-0.0.1.tar.gz
    

    Conclusion

    We have now created a simple autotools framework to do what we were doing manually. I’ll be honest: this is still pretty manual. In future lessons we’ll make some tweaks to it which make use of the extensive features of the system.

  • Writing Free Software – Part 3: Command line options

    Introduction

    In this installment, we will look at parsing command-line options. We’re going to use Jon Pryor‘s NDesk.Options library. Since he has not made a .deb package yet, we’ll use curl to integrate NDesk.Options.cs into our codebase.

    Get back to our workspace

    $ cd ~/src/greeting

    Get curl

    $ sudo apt-get install curl

    Pull down a copy of NDesk.Options

    $ curl "http://git.ndesk.org/?p=ndesk-options;a=blob_plain;f=src/NDesk.Options/NDesk.Options/Options.cs;hb=c52aa20569b49b620deb0fd28b19909b7c577d47" > NDesk.Options.cs

    Update our Makefile

    Updates are bolded below.

    $ cat > Makefile
    # the first target is the one executed when no target is named
    all: Greeting.exe
    #don't do anything
    
    # running 'make clean' will execute this target
    clean:
            rm Greeting.exe
    
    # running 'make Greeting.exe' will execute this target
    Greeting.exe: Greeting.cs NDesk.Options.cs
    # the same as gmcs -out:Greeting.exe Greeting.cs NDesk.Options.cs
            gmcs -out:$@ $^
    

    Update our C# code

    Updates are bolded below.

    $ cat > Greeting.cs
    using System;        // This brings in Console.Writeline, used below
    using NDesk.Options; // This brings in the options parser used below
    
    class Greeting {
      private static string [] planet =
        { "World", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn",
          "Uranus", "Neptune", "Pluto" };
    
      // This is the program entry point
      public static int Main( string [] argv ){
    
        // default to "World"
        int planetNum = 0;
    
        // set up an option parser
        var p = new OptionSet () {
            { "n|number=",  "The planet number to greet",
              (int v) => planetNum = (v < planet.Length ? v : 0) }
        };
        p.Parse (argv);
    
        // A simple operation to confirm that our app is running:
        Console.WriteLine("Greetings, {0}!", planet[planetNum]); 
    
        // Well-behaved software returns an int from Main
        return 0;
      }
    }
    

    Compile

    $ make

    Run it a few times

    $ ./Greeting.exe
    Greetings, World!
    $ ./Greeting.exe –n 1
    Greetings, Mercury!
    $ ./Greeting.exe ––number 2
    Greetings, Venus!
    $ ./Greeting.exe ––number 10
    Greetings, World!
    $ ./Greeting.exe –n 99
    Greetings, World!

    Summary

    That about wraps up sending command line arguments to the application. Thanks to Jon for providing support for NDesk on #mono. You’re right. I should read your docs instead of bothering you!

    We now have a bit more complication in our build system and have something we can re-factor a bit. In the next article, we’ll start to make use of some of the autotools bits.