Fixing a Repetitive Task

Working in computers, there are times I find myself doing a sequence of commands again and again. At some point, I will realize I should just automate that mess. It becomes more convenient to run a single command than to remember the commands for each step of the task at hand.

These scripts have evolved and become more complicated. A little error checking here. Array-filling there. The goal remains the same - do one task. The tasks to be done just get more ambitious over time.

One thing that I strive to eliminate in my code, be it shell script or otherwise, is hard-coding values. The abstraction of using variables is just too useful to ignore. I just loathe the idea of having to change the same value in multiple places in code. And don’t forget one value or expect bad things to happen. Or to remember to change that hard-coded value later? Remember…I’ve heard that word before somewhere.

Using a cross-platform GUI library, it becomes important to stay up to date. On both Microsoft Windows(MSW) and Linux, I have become accustomed to building my applications against the latest wxWidgets source. The task - update and build wxWidgets, became another automated task for me. The details needed to build the wxWidgets libraries consistently with each iteration made scripting invaluable. I didn’t have to recall what flags I used last time - it’s in the script file.

For my wxWidgets cross-compile script, I had, until recently, left the identity of dependent dll’s hard coded. My script would copy these supporting files over to a MSW share at the end. Any cross built application I created would need these supporting libraries to run. However, libraries can get updated meaning I would have to remember (there’s that word again…) to fix my script if and after an update. Not the most ideal situation.

I finally dealt with this gnawing problem.

I’m pleased with the results and I had to share. If anything, inspiration for others to tackle their shell demons.

The secret sauce to finding the dependencies is a single line of script:

text=$(x86_64-w64-mingw32-objdump -x "${file}" \
| sed -r -n 's/\tDLL\sName\:\s(([z]?lib).*\.dll).*/\1/p' )

The command x86_64-w64-mingw32-objdump looks at the file, passed in as a variable, and reports object file details. The -x switch is used to ensure all headers are reported. The file name passed in? The wxWidgets sample file widgets.exe, an example of ALL wxWidgets GUI elements.

The output is piped to the sed command which uses a regular expression to look for lines containing the string:

DLL Name: lib_something_.dll

The text lib*.dll, or zlib*.dll, is then added to a string variable text.

The string variable is then turned into an array. The array is then compared to another array to ensure unique values are present. And lastly, each element of the resulting array - representing a filename of a dependent dll - is then used to identify which link library files need to be copied onto the MSW share.

Again, I was really pleased with the results!

Yeah, repetitive task solutions!