You might have noticed, if you are maintaining an older system, that it is not exactly obvious in Debian how one should go about building optimized versions of some frequently used executable packages (like gzip, libraries, bash, perl etc). There are various solutions like apt-build, pentium-builder, and apt-get source –compile, but none of these did what I wanted; allowed me to maintain a locally optimized package that is only replaced with a non optimized version by the package manager when necessary (not on every upgrade).
1. Change debian/control
Change the Package: line from Package: foo to Package: foo-586
Add Provides: foo Replaces: foo and Conflicts: foo lines (see http://www.gatago.com/linux/debian/www/14642325.html why this should be done)
If any other packages have versioned dependencies on the foo package, instead of Conflicts: foo and Provides: foo, you will need to add another Package stanza for foo to build a versioned dummy package. Then make that dummy package Depends: foo-586
2. Change debian/rules
Make sure that the CFLAGS passed to configure include -mpentium or whatever options you need to compile optimized for your specific machine
Also, if the package builder uses debian/foo to refer to the package build directory in the script, you need to replace all occurrences of this with debian/foo-586. Better yet, replace them with debian/$(PACKAGE), define PACKAGE=foo-586 at the top, and submit a bug to improve the rules file.
3. Rename debian control files
Anything like foo.dirs, foo.install, foo.lintian, foo.pre/post/inst/rm will have to be renamed to foo-586.dirs, etc. And any references in debian/rules will have to be updated to reflect that. (Again, $(PACKAGE) is handy.)
Then dpkg-buildpackage -rfakeroot and install the package. dpkg should automatically figure out what is to be done, but you may have to use –force-depends if you know exactly why dpkg is confused and not installing your package (hint: make sure your Provides: is correct).
Apt-get upgrade will not replace this package, but apt-get dist-upgrade may. You might want to add a line to the postrm script to notify you via email when the package has been replaced so that you know it is time to build a new one.