On 04/07/2011 10:52 AM, Adam Mercer wrote: > Hi > > In one of my macros I need to do one thing on linux platforms and > another on others, in the macro I have the followings: > > if test "x$build_os" = "xlinux"; then > # case for linux > else > # other platforms > fi > > I recently received a bug report that this wasn't working as the users > system had $build_os defined as 'linux-gnu', whereas on others it was > just 'linux'. I then tried to change the if statement to: > > if test "x$build_os" = "xlinux" -o test "x$build_os" = "xlinux-gnu"; then 'test -o' is completely non-portable. But even if you use it, you had a syntax error (notice your extra 'test' operand); you meant: if test "x$build_os" = "xlinux" -o "x$build_os" = "xlinux-gnu"; then But, seeing how -o is non-portable, this is the correct portable alternative: if test "x$build_os" = xlinux || test "x$build_os" = xlinux-gnu; then However, that gets tedious. When doing host comparisons, it's often faster to use case statements (the glob lets you match multiple platforms in one pattern, and you can match multiple patterns). For example: case $build_os in linux* | cygwin*) # case for linux, linux-gnu, and cygwin ;; *) # other platforms ;; esac Be careful with globs that do ranges; you need to double quote to preserve those ranges into the output. Bad: case $build_os in solaris[89]) ...;; esac Good, option 1 (useful if ... has no macros, allowing copy and paste to and from the shell): [ case $build_os in solaris[89]) var=value;; esac ] Good, option 2 (useful if ... has macros that must not be double-quoted, and where shell copy and paste is already a non-issue): case $build_os in solaris[[89]]) AC_MSG_ERROR([unsupported]);; esac Finally, remember that while there are cases, like making sane guesses while cross-compiling, where probing $host_os is the only way, it goes against autoconf philosophy of testing features rather than platforms. And make sure that if you are making platform-specific variables that: 1) your code will still work even if your platform-specific guess was wrong (ie. guess pessimistically), and 2) you provide a cache variable to allow the user to override things if you guess wrong. -- Eric Blake eblake@xxxxxxxxxx +1-801-349-2682 Libvirt virtualization library http://libvirt.org
Attachment:
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Autoconf mailing list Autoconf@xxxxxxx http://lists.gnu.org/mailman/listinfo/autoconf