12

I have a project that uses automake to create the configure and all related files (I'm using autoreconf command to make all this stuff). So, I'm trying to set some conditional files to compile when the project is compiling for macOS (OS X), Windows or Linux. But it fails with the following:

 $ autoreconf -i ..
src/Makefile.am:30: error: LINUX does not appear in AM_CONDITIONAL
autoreconf: automake failed with exit status: 1

And the part containing the error in that Makefile.am is the following:

if OSX
    butt_SOURCES += CurrentTrackOSX.h CurrentTrackOSX.m
endif
if LINUX
    butt_SOURCES += currentTrack.h currentTrackLinux.cpp
endif
if WINDOWS
    butt_SOURCES += currentTrack.h currentTrack.cpp
endif

My question is, how can I check if the OS is Linux? And if it's possible, is there a better way to check the OS in automake?

melchor629
  • 242
  • 3
  • 11
  • To be pedantic (and pedantry is necessary when dealing with the autotools), `automake` does not create `configure` and has nothing to do with it at all. `automake` creates `Makefile.in` based on the template `Makefile.am`. – William Pursell Aug 11 '16 at 14:08
  • There are ways to check if the OS is Linux, but you don't care, and you shouldn't care. You care about the particular features of the system, not what the system is. Determine the features you need, test for those particular features, and don't worry about whether it is Linux or OS X or Windows. Doing so will be more robust, since you ought not assume all Linux are the same, anyway. – William Pursell Aug 11 '16 at 14:10
  • @William Pursell Thanks for the clarification about automake, I use `autoreconf` to make all the stuff, and I don't know the exact name for it. And for this files in special, I only need the standard C library. On OS X needs AppleScript, that is only available there. And on Windows is directly unsupported (is a stub function). So I only need to check if is Linux because C library is common on all Linux. – melchor629 Aug 11 '16 at 14:14
  • So you don't care whether or not it is Linux, you care if AppleScript is available or if you have a particular libc. Check for that. – William Pursell Aug 11 '16 at 14:40

2 Answers2

24

You can detect it directly in the Makefile, or define the conditionals in the configure source file (probably configure.ac), since you are using autoreconf:

# AC_CANONICAL_HOST is needed to access the 'host_os' variable    
AC_CANONICAL_HOST

build_linux=no
build_windows=no
build_mac=no

# Detect the target system
case "${host_os}" in
    linux*)
        build_linux=yes
        ;;
    cygwin*|mingw*)
        build_windows=yes
        ;;
    darwin*)
        build_mac=yes
        ;;
    *)
        AC_MSG_ERROR(["OS $host_os is not supported"])
        ;;
esac

# Pass the conditionals to automake
AM_CONDITIONAL([LINUX], [test "$build_linux" = "yes"])
AM_CONDITIONAL([WINDOWS], [test "$build_windows" = "yes"])
AM_CONDITIONAL([OSX], [test "$build_mac" = "yes"])

Note: host_os refers to the target system, so if you are cross-compiling it sets the OS conditional of the system you are compiling to.

Community
  • 1
  • 1
tversteeg
  • 4,717
  • 10
  • 42
  • 77
  • I tried to detect it directly in `Makefile.am` but gives me an error. No when using the solution with `configure.ac`. Thanks for the answer – melchor629 Aug 11 '16 at 14:46
-3

Have you tried using the lsb_release command to determine the type and version of operating system ?

bocian85
  • 130
  • 2
  • 1
    `lsb` means Linux Standard Base. Do you think this command will be available on many other operating systems..? It's not on MSYS2/MinGW64, for instance, which makes it useless in my book. `uname` is a much more appropriate and likely-to-exist command. But anyway, ideally this kind of check is something that the Autotools should handle, rather than requiring the user to call and parse some other tool, even if it was relevant. (Autotools isn't _quite_ ideal, though!) – underscore_d Dec 29 '16 at 11:23