| 1 | = Software Atrophy - An Example = |
| 2 | Sep 20 2015 |
| 3 | |
| 4 | [[Image(htdocs:images/scons/SCons.png, alt="SCons Logo")]] |
| 5 | |
| 6 | Previously, I'd put up an article describing the [Programming/Python/SconsBasedBuildSystemObjectModel component object model] of my [http://www.scons.org/ SCons] based build system at Axham Games. If you haven't already read that, you might want to take a gander; it would ease understanding of what is discussed here. Somewhere along the way, as tends to happen with a lot of software systems, our build scripts began showing signs of atrophy. |
| 7 | |
| 8 | > '''Note:''' This is written with a view to help myself and others running development teams. This post goes into details about poorly done software development work, but the goal is not to cast blame, but to learn how to spot the signs early and make appropriate course corrections before reaching a point where a system needs to be completely re-written. |
| 9 | |
| 10 | > '''Note 2:''' This was originally written in Jan 2015, but was put on the back burner because I got injured in an accident. I didn't find time till now to wrap it up and publish it. |
| 11 | |
| 12 | == Quick Recap == |
| 13 | |
| 14 | In our system we treat sets of libraries as a single component and a Python class represents a component. E.g. all the boost libraries have common parameters and are group together as a single component. Each project can choose which libraries within the set to use by specifying them in the SConstruct file and overriding them within per project SConscript files. |
| 15 | |
| 16 | Here's an example of the configuration used to link to Lua libraries. |
| 17 | |
| 18 | {{{#!python |
| 19 | env['LUA_DIR'] = util.join_path(externals_dir, build_mode, 'lua') |
| 20 | env['LUA_PLUGIN_LIBRARIES'] = set(['lua52']) |
| 21 | env['LUA_INCLUDE_DIR_EPILOG'] = 'include' |
| 22 | env['LUA_VARIANT'] = externals_variant |
| 23 | env['LUA_VERSION'] = '5-2' |
| 24 | }}} |
| 25 | |
| 26 | We also rely on the developer having correct environment variables set for paths to tools since they |
| 27 | can be installed in different locations on any given machine. As can be seen from the example above, |
| 28 | we construct any paths in an OS agnostic manner using Python's standard facilities. One critical |
| 29 | rule that has to be followed is [[span(style=color: #FF0000, "'''No hard-coded paths'''")]] since we compile using different tool chains (mingw GNU C++, Visual C++) and multiple platforms (Win32, Win64, Linux, etc). |
| 30 | |
| 31 | == What Went Wrong == |
| 32 | |
| 33 | === 1. Core Libraries Component === |
| 34 | |
| 35 | On any given platform, there are usually core libraries like user32.lib on Windows or ws2_32.lib when using mingw. Additionally, there may be libraries such as stdc++-6 which will need to be linked to when compiling with the GNU C++ compiler. One way in which some core libraries are different from others within the component are that the corresponding dynamic libs are installed either as part of the system or via an external package; an easy way to think of them is as though they are similar to the Visual C++ runtime libraries, the .NET runtime or the Java runtime. |
| 36 | |
| 37 | Unfortunately, even though this had been explained to developers on the team, it was either forgotten or ignored. So, the result was that the developer set it up to copy the core dynamic libs from their location to the target directory. This is an '''absolute no-no''' for system libraries. |
| 38 | |
| 39 | === 2. Hard-coded Path === |
| 40 | |
| 41 | The developer further compounded the error of copying system libraries to the target directory, by hard-coding the path of the system directory (in this case, the mingw-msys bin directory) in the core libraries component's Python class. But, it was done in a way that's not easy to detect, by constructing the path using Python's standard facilities. |
| 42 | |
| 43 | `dynamic_lib_path = util.join_path('c:', 'tools', 'mingw', 'bin')` |
| 44 | |
| 45 | Not only would this fail were mingw installed in a different directory, this is also OS specific. The build would fail in the Linux and OSX environments. |
| 46 | |
| 47 | == How it was Detected == |
| 48 | |
| 49 | Simple answer: Completely by accident. I have a couple of machines on which I do development, one of which is a laptop on which I was prototyping before I founded Axham Games. So, the various tools were installed in non-standard directories on that laptop. When I ran a build on the laptop, it promptly broke indicating that the dynamic lib in the above hard-coded path did not exist. When used properly, the build system is designed to be able to deal with tools installed in non-standard directories, since their locations are supposed to be picked up from environment variables. Hard coding the path broke it. |
| 50 | |
| 51 | Why the developer did this is mystery (he is no longer at the studio); especially considering that there are pre-existing examples of how to do this according to established convention, which is to have each developer define an environment variable to specify the directory. At best, this piece of work is reflective of a poor work ethic i.e. that the developer adhered only to the letter of the rule, not its spirit. At worst, this is insidious; the hard coding of the path was "cleverly" obfuscated by using Python's standard path construction facilities and unless one looks closely, everything seems kosher. |
| 52 | |
| 53 | == The Fix == |
| 54 | |
| 55 | The correct fix is to allow any component to specify that certain libraries that are linked to do not (or should not) have dynamic libraries copied over to the target directory. |
| 56 | |
| 57 | To that effect, the base Component class from which each of the individual components derive needs to be modified to store the library list of what is not to be installed. We do this by adding one member list `_dynamic_libraries_dni` (dni = do not install) and two properties `dynamic_libraries_dni` and `dynamic_libraries_installable` as follows: |
| 58 | |
| 59 | {{{#!python |
| 60 | class Component(object): |
| 61 | def __init__(self): |
| 62 | ... |
| 63 | ... |
| 64 | self._dynamic_libraries_dni = [] |
| 65 | ... |
| 66 | ... |
| 67 | |
| 68 | ... |
| 69 | ... |
| 70 | |
| 71 | @property |
| 72 | def dynamic_libraries_dni(self): |
| 73 | return self._dynamic_libraries_dni[:] |
| 74 | |
| 75 | @property |
| 76 | def dynamic_libraries_installable(self): |
| 77 | return list({ dl for dl in self._dynamic_libraries |
| 78 | if dl not in self._dynamic_libraries_dni }), |
| 79 | |
| 80 | ... |
| 81 | ... |
| 82 | }}} |
| 83 | |
| 84 | These properties and member variable propagate through to the derived classes and are used in per-project SConscript files which invoke the `dynamic_libraries_installable` property to determine the libraries to install. |
| 85 | |
| 86 | We also remove the hard-coded path in the Core component implementation as it was incorrect and since it is no longer needed. |
| 87 | |
| 88 | == Quick Thoughts on Atrophy Prevention == |
| 89 | |
| 90 | [[span(style=color: #008000, '''tl;dr version:''' This problem could have been avoided if the build system changes had been code reviewed like all other production code.)]] |
| 91 | |
| 92 | Recalling some discussions with a former colleague, software is not a write-once-and-forget sort of thing. All software is organic in nature; it's more like a tree. It needs to be nurtured, attended to, and from time to time, pruned. |
| 93 | |
| 94 | All code, particularly production code, ought to be written carefully and code-reviewed as part of the check-in process, sometimes by more than one pair of eyes. But the same level of rigor is not applied to internal support systems like build systems and developer tools. In small teams like ours, there aren't sufficient resources to pay attention at that level of detail to non-shipping code unless it is a significant pain point or as in this case, breaks in one or more environments. |
| 95 | |
| 96 | Looking back at this more critically, possible only with time and distance from the series of events that preceeded this find, the crux of the issue was: |
| 97 | |
| 98 | 1. a bad hire - the hack engineer who implemented the broken solution |
| 99 | 1. a manager [me] who didn't oversee the engineer's work more closely, and more importantly, terminate the engineer months prior despite sufficient warning signs. Note that the termination is not for mistakes, it's for a myriad of reasons including poor work ethic. |