Blog

  • 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.

  • Writing Free Software – Part 2: Makefiles

    Okay, so now we know how to write hello world in C#, compile it and run it. Next, let’s try to automate the build portion a bit. It’s not going to be very much of an improvement to start out with; we’re just replacing the gmcs command with a make command. But it’s laying a framework, so let’s ignore that for the time being.

    Get back to the workspace

    $ cd ~/src/greeting

    Create a simple Makefile

    $ 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
    # the same as gmcs -out:Greeting.exe Greeting.cs
    	gmcs -out:$@ $^
    

    Test the Makefile

    $ make
    gmcs -out:Greeting.exe Greeting.cs
    $ make clean
    rm Greeting.exe
    $ make Greeting.exe
    gmcs -out:Greeting.exe Greeting.cs
    $ ./Greeting.exe 
    Greetings, World!
    

    Conclusion

    So, there’s a quick overview of writing a Makefile. If you want to learn all of the nitty-gritty details, I recommend reading the manual over at gnu.org.

    We’ll use these short lessons as building blocks for larger tasks, like creating a distribution using automake and autoconf. Baby steps first :)

  • Writing Free Software – Part 1

    There are a lot of folks who want to learn how to write software, I hear. So I’ll see if I can write something up about it.

    I’m going to assume that the reader has at their disposal an i386-class computer running Ubuntu or Debian. This will all probably work for other operating systems, but it’s what I’ve got, and I’m not going to bend over backwards. :) I’m also going to write it in C#. Get over it. If something doesn’t work for you, if I miss a prerequisite or whatever, you can send me an email and I’ll fix the post.

    Install the C# compiler

    $ sudo apt-get install mono-gmcs

    pretty easy, eh?

    create a workspace

    $ mkdir -p ~/src/greeting

    Also pretty easy.

    Write a simple app

    $ cd ~/src/greeting
    $ cat > Greeting.cs
    // This brings in Console.Writeline, used below
    using System;
    
    class Greeting {
      // This is the program entry point
      public static int Main( string [] argc ){
    
        // A simple operation to confirm that our app is running:
        Console.WriteLine("Greetings, World!"); 
    
        // Well-behaved software returns an int from Main
        return 0;
      }
    }
    ^D
    

    Compile the app

    $ gmcs Greeting.cs

    Run the app


    $ ./Greeting.exe
    Greetings, World!

    Summary

    Okay, so that’s not all that impressive, I know. I’m starting small. I intend to teach folks how to write an autotools package, including support for producing and consuming pkg-config bits (app-name.pc files), internationalization, etc. It’ll be fun, really. Just give me a little while to get it finished. :)

  • autotools help on IRC

    It has come to my attention that there is a lack of autotools (autoconf, automake, autoheader, libtool, etc) help on IRC. If anybody is looking, they might check out #mono on irc.gimp.net; there are quite a few folks there who are familiar with the suite, and if I can ever figure out how to get back on during the day, I’d be happy to help folks out as time permits.

  • To Prebuild or not to Prebuild?

    So… On IRC today, John, Michael and I discussed rolling Prebuild and xbuild into one product. It seems that we’re duplicating effort and competing for each others’ users. It sounds to me like we should converge the codebases, and make Prebuild.exe a wrapper around the innards of xbuild.

    Thoughts?

  • Taking a job at MS

    So… next week, I start a contract with Microsoft in Redmond. I’ll be working on what seems like a fun project. I’ve enjoyed working at CarDomain for the last four months. Fun projects, a cohesive group, neat technology. I’m mostly moving on because a) I’ve been writing web pages for a long time and want to try something else and b) the commute will be much easier and I’ll get to drive in with Hannah!

    I’m looking forward to being able to play with some of the fancy internal toys that I’ve heard so much about. Someone told me yesterday that even the folks who were hardcore MS haters love their job there. I’m certainly not a hater. Perhaps skeptical, but not a hater. I figure I’ll learn a bunch of fun new things even in the worst case scenario.

    Anyway. Now that the various colliertech.org bits are functioning again, I thought I’d write something.

  • The Cretins

    My brother’s motorcycle club, The Cretins, recently had an article published about them in the Seattle PI. They even quoted him. I didn’t realize he was the president of the club. The rumor goes that he missed a meeting, and wasn’t there to defend himself.