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
This entry was posted in autotools, c#, CLI, debian, Free Software, mono, Software. Bookmark the permalink.

4 Responses to Writing Free Software – Part 10: Subdirectories

  1. This is already the best tutorial out there on how to get started with configure etc. and believe me I’ve looked. Please keep writing!

  2. Thank you Daniel, that’s high praise. I intend to keep it up. There’s so much more that can be done with these tools. :)

  3. Stephen Gentle says:

    I prefer to just make one makefile for my source dir, and just put dir/file in the sources, because recursive make is evil (see http://miller.emu.id.au/pmiller/books/rmch/ )

    -Stephen

  4. Stephen,

    This seems reasonable to me. Jon Pryor also mentioned this and pointed me toward your link and the pdf version at

    http://aegis.sourceforge.net/auug97.pdf

    I think this sort of collapsing of recursive Makefile files into a single authoritative file should be handled by automake, however, and not manually tackled by the Makefile.am writer. For the sake of simplicity, I will leave the SUBDIRS section as it is and recommend that readers take a look at the links mentioned by you and Jon.

    Cheers,

    C.J.

Leave a Reply