From ca7bb43754533df3d771d6d4ad24ec9b0218f1fc Mon Sep 17 00:00:00 2001
From: KO Myung-Hun <komh@chollian.net>
Date: Wed, 8 Jul 2015 15:55:08 +0900
Subject: [PATCH] configure: improve detection of BUILDCC and set BUILDEXEEXT

---
 configure.ac  |  6 +----
 m4/buildcc.m4 | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 5 deletions(-)
 create mode 100644 m4/buildcc.m4

diff --git a/configure.ac b/configure.ac
index a3dc3e1..1e4ee45 100644
--- a/configure.ac
+++ b/configure.ac
@@ -70,11 +70,7 @@ AM_PROG_AS
 AC_ARG_VAR([DESKTOP_FILE_VALIDATE], [Validator for desktop entry files])
 AC_CHECK_PROGS(DESKTOP_FILE_VALIDATE, [${DESKTOP_FILE_VALIDATE} desktop-file-validate], :)
 AC_PATH_PROG(YASM, yasm)
-AC_ARG_VAR([BUILDCC], [Build system C11 or C99 compiler command])
-AC_PATH_PROGS(BUILDCC, [c11-gcc c11 c99-gcc c99], [false])
-AS_IF([test "$BUILDCC" = "false"], [
-  AC_MSG_ERROR([Cannot find native C99 compiler: please define BUILDCC.])
-])
+VLC_PROG_BUILDCC
 
 dnl Check for compiler properties
 AC_C_CONST
diff --git a/m4/buildcc.m4 b/m4/buildcc.m4
new file mode 100644
index 0000000..72c3a97
--- /dev/null
+++ b/m4/buildcc.m4
@@ -0,0 +1,71 @@
+dnl Copyright (C) 2015 KO Myung-Hun <komh@chollian.net>
+dnl This file is free software; You get unlimited permission to copy
+dnl and/or distribute it, with or without modifications, as long as
+dnl this notice is preserved.
+
+AC_DEFUN([VLC_PROG_BUILDCC], [
+  AC_ARG_VAR([BUILDCC], [Build system C11 or C99 compiler command])
+  AC_CACHE_CHECK([for build system C11 or C99 compiler],
+                 [vlc_cv_vlc_prog_buildcc], [
+    vlc_cv_vlc_prog_buildcc=no
+    if test -z "$BUILDCC"; then
+      rm -f conftest.c
+
+      cat > conftest.c << EOF
+#ifndef __STDC_VERSION__
+#error Need C Compiler
+#endif
+
+#if __STDC_VERSION__ < 199901L
+#error Need more recent C Compiler
+#endif
+
+void foo(void)
+{
+    for (int i = 0; i; i++)
+        /* Nothing */;
+}
+
+int main(void)
+{
+    foo();
+
+    return 0;
+}
+EOF
+
+      for buildcc in "$CC" "$CC $CFLAGS $CPPFLAGS" gcc c11-gcc c11 c99-gcc c99; do
+        for opt in "" -std=gnu99 -std=c99; do
+          rm -f a.out b.out a.exe a_out.exe conftest conftest.exe
+
+          if AC_TRY_COMMAND([$buildcc $opt conftest.c]); then
+            if AC_TRY_COMMAND([./a.out || ./b.out || ./a.exe || ./a_out.exe || ./conftest]); then
+              vlc_cv_vlc_prog_buildcc="$buildcc $opt"
+              break 2
+            fi
+          fi
+        done
+      done
+    else
+      vlc_cv_vlc_prog_buildcc="$BUILDCC"
+    fi
+  ])
+
+  rm -f conftest.c a.out b.out a.exe a_out.exe conftest conftest.exe
+
+  AC_SUBST([BUILDCC])
+  BUILDCC="$vlc_cv_vlc_prog_buildcc"
+
+  if test "$BUILDCC" = no; then
+    AC_MSG_ERROR([Cannot find native C99 compiler: please define BUILDCC.])
+  fi
+
+  case "${build_os}" in
+    os2*)
+      BUILDEXEEXT=.exe
+      AC_SUBST(BUILDEXEEXT)
+      ;;
+    *)
+      ;;
+  esac
+])
-- 
1.9.5

