From 7a9a51e337b2c07970429cf6226a0dc60ac4adf4 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 1/2] configure: improve detection of BUILDCC and set
 BUILDEXEEXT

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

diff --git a/configure.ac b/configure.ac
index a67ad0e..11b9c59 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..f6f6532
--- /dev/null
+++ b/m4/buildcc.m4
@@ -0,0 +1,76 @@
+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], [_cv_vlc_prog_buildcc], [
+    _cv_vlc_prog_buildcc=no
+    if test -z "$BUILDCC"; then
+      AC_LANG_PUSH([C])
+
+      saved_CC="$CC"
+      saved_CPP="$CPP"
+      saved_CFLAGS="$CFLAGS"
+      saved_CPPFLAGS="$CPPFLAGS"
+
+      CPP=
+      CPPFLAGS=
+
+      for CC in c11-gcc c11 c99-gcc c99 gcc
+      do
+        for CFLAGS in "" -std=gnu99 -std=c99
+        do
+          AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#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 */;
+}
+          ]])],
+          [compile_success=yes],
+          [compile_success=no])
+
+          if test "$compile_success" = yes; then
+            _cv_vlc_prog_buildcc="$CC $CFLAGS"
+            break 2
+          fi
+        done
+      done
+
+      CC="$saved_CC"
+      CPP="$saved_CPP"
+      CFLAGS="$saved_CFLAGS"
+      CPPFLAGS="$saved_CPPFLAGS"
+
+      AC_LANG_POP([C])
+    else
+      _cv_vlc_prog_buildcc="$BUILDCC"
+    fi
+  ])
+
+  AC_SUBST([BUILDCC])
+  BUILDCC="$_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

