Saturday, 28 May 2016

Procenv 0.46 - now with more platform godness

I have just released procenv version 0.46. Although this is a very minor release for the existing platforms (essentially 1 bug fix), this release now introduces support for a new platform...


Yup - OS X now joins the ranks of supported platforms.

Although adding support for Darwin was made significantly easier as a result of the recent internal restructure of the procenv code, it did present a challenge: I don't own any Apple hardware. I could have borrowed a Macbook, but instead I decided to see this as a challenge:

  • Could I port procenv to Darwin without actually having a local Apple system?
 Well, you've just read the answer, but how did I do this?

Stage 1: Docker

Whilst surfing around I came across this interesting docker image:

It provides a Darwin toolchain that I could run under Linux. It didn't take very long to follow my own instructions on porting procenv to a new platform. But although I ended up with a binary, I couldn't actually run it, partly because Darwin uses a different binary file format to Linux: rather than ELF, it uses the Mach-O format.

Stage 2: Travis

The final piece of the puzzle for me was solved by Travis. I'd read the very good documentation on their site, but had initially assumed that you could only build Objective-C based projects on OSX with Travis. But a quick test proved my assumption to be incorrect: it didn't take much more than adding "osx" to the os list and "clang" to the compiler list in procenv's .travis.yml to have procenv building and running (it runs itself as part of its build) on OSX under Travis!

Essentially, the following YAML snippet from procenv's .travis.yml did most of the work:

language: c
  - gcc
  - clang
  - linux
  - osx

All that remained was to install the build-time dependencies to the same file with this additional snippet:

  - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
  - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install expat check perl; fi

(Note that it seems Travis is rather picky about before_install - all code must be on a single line, hence the rather awkward-to-read "if; then ....; fi" tests).


Although I've never personally run procenv under OSX, I have got a good degree of confidence that it does actually work.

That said, it would be useful if someone could independently verify this claim on a real system!) Feel free to raise bugs, send code (or even Apple hardware :-) my way!

Tuesday, 22 March 2016

Procenv 0.45 released

Yesterday, I released procenv 0.45.

Although not a huge amount has changed on the outside in this release, rather
dramatic changes have occurred on the inside, as evinced by the following:

$ git diff 0.44..0.45 --stat|grep "src/.*\.[ch]"
 src/messages.h                          |    41 +
 src/output.c                            |  2001 ++++++
 src/output.h                            |   179 +
 src/platform-headers.h                  |   243 +
 src/platform.h                          |   167 +
 src/platform/android/platform.c         |    46 +
 src/platform/freebsd/platform-freebsd.h |    43 +
 src/platform/freebsd/platform.c         |   479 ++
 src/platform/hurd/platform-hurd.h       |    38 +
 src/platform/hurd/platform.c            |    91 +
 src/platform/linux/platform-linux.h     |   174 +
 src/platform/linux/platform.c           |  2176 ++++++
 src/platform/minix/platform-minix.h     |    25 +
 src/platform/minix/platform.c           |   126 +
 src/platform/netbsd/platform-netbsd.h   |    25 +
 src/platform/netbsd/platform.c          |   237 +
 src/platform/openbsd/platform-openbsd.h |    25 +
 src/platform/openbsd/platform.c         |   199 +
 src/platform/platform-generic.c         |   922 +++
 src/platform/platform-generic.h         |    66 +
 src/platform/unknown/platform-unknown.h |    25 +
 src/platform/unknown/platform.c         |    89 +
 src/pr_list.c                           |     2 +-
 src/pr_list.h                           |     2 +-
 src/procenv.c                           | 11575 +++++++-----------------------
 src/procenv.h                           |   918 +--
 src/pstring.c                           |   309 +
 src/pstring.h                           |    55 +
 src/string-util.c                       |   543 ++
 src/string-util.h                       |    60 +
 src/types.h                             |    34 +
 src/util.c                              |   288 +
 src/util.h                              |    57 +

Partly driven by "#ifdef hell", and part inspired by Hisham's Fosdem presentation on htop, "Going cross-platform: how htop was made portable", I decided to modularise the code and introduce platform drivers encapsulating the O/S specifics and allowing procenv.c itself to be considerably simplified. This allows for easier maintenance and easier porting to new operating systems.

To prove it, procenv now runs on OpenBSD, NetBSD and Minix 3!

There is still further refactoring to do, but the code is now in much better shape for others to work with.

So, if you'd like to get involved in porting procenv to [insert-your-favourite-as-yet-unsupported-OS-here], get involved! I even wrote a simple porting guide to help get your started: