$OpenBSD: patch-aclocal_m4,v 1.1 2017/06/19 21:02:11 sebastia Exp $

Index: aclocal.m4
--- aclocal.m4.orig
+++ aclocal.m4
@@ -1,4 +1,3 @@
-builtin(include,objc.m4)
 builtin(include,pthread.m4)
 builtin(include,platform.m4)
 builtin(include,check.m4)
@@ -23,7 +22,7 @@ builtin(include,check.m4)
 #	Result is cached.
 #
 #	Defines one of the following preprocessor macros:
-#		APPLE_RUNTIME GNU_RUNTIME
+#		APPLE_RUNTIME GNU_RUNTIME MODERN_RUNTIME
 #
 #	Substitutes the following variables:
 #		OBJC_RUNTIME OBJC_RUNTIME_FLAGS OBJC_LIBS
@@ -31,7 +30,7 @@ builtin(include,check.m4)
 #------------------------------------------------------------------------
 AC_DEFUN([OD_OBJC_RUNTIME],[
 	AC_REQUIRE([AC_PROG_OBJC])
-	AC_ARG_WITH(objc-runtime, AC_HELP_STRING([--with-objc-runtime], [Specify either "GNU" or "apple"]), [with_objc_runtime=${withval}])
+	AC_ARG_WITH(objc-runtime, AC_HELP_STRING([--with-objc-runtime], [Specify either "GNU", "apple", or "modern"]), [with_objc_runtime=${withval}])
 
 	if test x"${with_objc_runtime}" != x; then
 		case "${with_objc_runtime}" in
@@ -39,8 +38,10 @@ AC_DEFUN([OD_OBJC_RUNTIME],[
 				;;
 			apple)
 				;;
+			modern)
+				;;
 			*)
-				AC_MSG_ERROR([${with_objc_runtime} is not a valid argument to --with-objc-runtime. Please specify either "GNU" or "apple"])
+				AC_MSG_ERROR([${with_objc_runtime} is not a valid argument to --with-objc-runtime. Please specify either "GNU", "apple", or "modern"])
 				;;
 		esac
 	fi
@@ -57,7 +58,7 @@ AC_DEFUN([OD_OBJC_RUNTIME],[
 	OLD_OBJCFLAGS="${OBJCFLAGS}"
 
 	# Add -lobjc. The following tests will ensure that the library exists and functions with the detected Objective C compiler
-	LIBS="${LIBS} -lobjc"
+	LIBS="${LIBS} -lobjc2"
 
 	# Test if pthreads are required to link against
 	# libobjc - this is the case on FreeBSD.
@@ -112,11 +113,11 @@ AC_DEFUN([OD_OBJC_RUNTIME],[
 	AC_MSG_RESULT(${od_cv_objc_req_pthread})
 
 	if test x"${od_cv_objc_req_pthread}" = x"no"; then
-		OBJC_LIBS="-lobjc"
+		OBJC_LIBS="-lobjc2"
 		OBJC_PTHREAD_LIBS="${PTHREAD_LIBS}"
 		OBJC_PTHREAD_CFLAGS="${PTHREAD_CFLAGS}"
 	elif test x"${od_cv_objc_req_pthread}" = x"yes"; then
-		OBJC_LIBS="-lobjc ${PTHREAD_LIBS}"
+		OBJC_LIBS="-lobjc2 ${PTHREAD_LIBS}"
 		OBJCFLAGS="${OBJCFLAGS} ${PTHREAD_CFLAGS}"
 	fi
 
@@ -174,17 +175,61 @@ AC_DEFUN([OD_OBJC_RUNTIME],[
 		od_cv_objc_runtime_gnu="no"
 	fi
 
+	if test x"${with_objc_runtime}" = x || test x"${with_objc_runtime}" = x"modern"; then
+		AC_MSG_CHECKING([for Modern Objective C runtime])
+		AC_CACHE_VAL(od_cv_objc_runtime_modern, [
+			# The following uses quadrigraphs
+			# '@<:@' = '['
+			# '@:>@' = ']'
+			AC_LINK_IFELSE([
+					AC_LANG_PROGRAM([
+							#include <objc/objc.h>
+							#include <objc/runtime.h>
+						], [
+							id class = objc_lookUpClass("NSObject");
+							id obj = @<:@class alloc@:>@;
+							puts(@<:@obj name@:>@);
+						])
+					], [
+						od_cv_objc_runtime_modern="yes"
+					], [
+						od_cv_objc_runtime_modern="no"
+					]
+			)
+		])
+		AC_MSG_RESULT(${od_cv_objc_runtime_modern})
+	else
+		od_cv_objc_runtime_modern="no"
+	fi
+
 	# Apple runtime is prefered
-	if test x"${od_cv_objc_runtime_apple}" = x"yes"; then
-			OBJC_RUNTIME="APPLE_RUNTIME"
-			OBJC_RUNTIME_FLAGS="-fnext-runtime"
-			AC_MSG_NOTICE([Using Apple Objective-C runtime])
-			AC_DEFINE([APPLE_RUNTIME], 1, [Define if using the Apple Objective-C runtime and compiler.]) 
-	elif test x"${od_cv_objc_runtime_gnu}" = x"yes"; then
-			OBJC_RUNTIME="GNU_RUNTIME"
-			OBJC_RUNTIME_FLAGS="-fgnu-runtime"
-			AC_MSG_NOTICE([Using GNU Objective-C runtime])
-			AC_DEFINE([GNU_RUNTIME], 1, [Define if using the GNU Objective-C runtime and compiler.]) 
+	#if test x"${od_cv_objc_runtime_apple}" = x"yes"; then
+	#		OBJC_RUNTIME="APPLE_RUNTIME"
+	#		OBJC_RUNTIME_FLAGS="-fnext-runtime"
+	#		AC_MSG_NOTICE([Using Apple Objective-C runtime])
+	#		AC_DEFINE([APPLE_RUNTIME], 1, [Define if using the Apple Objective-C runtime and compiler.]) 
+	#elif test x"${od_cv_objc_runtime_gnu}" = x"yes"; then
+	#		OBJC_RUNTIME="GNU_RUNTIME"
+	#		OBJC_RUNTIME_FLAGS="-fgnu-runtime"
+	#		AC_MSG_NOTICE([Using GNU Objective-C runtime])
+	#		AC_DEFINE([GNU_RUNTIME], 1, [Define if using the GNU Objective-C runtime and compiler.]) 
+	if test x"${od_cv_objc_runtime_modern}" = x"yes"; then
+			OBJC_RUNTIME="MODERN_RUNTIME"
+			case "${target_os}" in
+				darwin*) OBJC_RUNTIME_FLAGS="-fnext-runtime"
+					 LDFLAGS="-framework Foundation ${LDFLAGS}";;
+				*) AC_CHECK_PROG([have_gs_config],
+				                 [gnustep-config], [yes])
+				   if test x"$have_gs_config" != x"yes"; then
+				     AC_MSG_ERROR([Cannot find gnustep-config.])
+				   else
+				   OBJC_RUNTIME_FLAGS="`gnustep-config --objc-flags`"
+				   OBJC_LIBS="`gnustep-config --base-libs`"
+				   fi
+				   ;;
+			esac
+			AC_MSG_NOTICE([Using Modern Objective-C runtime])
+			AC_DEFINE([MODERN_RUNTIME], 1, [Define if using the Modern Objective-C runtime and compiler.])
 	else
 			AC_MSG_FAILURE([Could not locate a working Objective-C runtime.])
 	fi
