Improve avoiding C++ linkage, definition creation and cmake tests (#710)

diff --git a/src/Makefile.am b/src/Makefile.am
index 912a98c..833d1f9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -223,21 +223,14 @@
 
 CLEANFILES += harfbuzz.def
 harfbuzz.def: $(HBHEADERS) $(HBNODISTHEADERS)
-	$(AM_V_GEN) (echo EXPORTS; \
-	(cat $^ || echo 'hb_ERROR ()' ) | \
-	$(EGREP) '^hb_.* \(' | \
-	sed -e 's/ (.*//' | \
-	LC_ALL=C sort; \
-	echo LIBRARY libharfbuzz-0.dll; \
-	) >"$@"
-	@ ! grep -q hb_ERROR "$@" \
-	|| ($(RM) "$@"; false)
+	$(AM_V_GEN) headers="$^" $(srcdir)/gen-def.py $@
 
 
 GENERATORS = \
 	gen-arabic-table.py \
 	gen-indic-table.py \
 	gen-use-table.py \
+	gen-def.py \
 	$(NULL)
 EXTRA_DIST += $(GENERATORS)
 
diff --git a/src/check-defs.sh b/src/check-defs.sh
index c7eac35..9b4b757 100755
--- a/src/check-defs.sh
+++ b/src/check-defs.sh
@@ -5,7 +5,6 @@
 
 test -z "$srcdir" && srcdir=.
 test -z "$libs" && libs=.libs
-test -z "$MAKE" && MAKE=make
 stat=0
 
 if which nm 2>/dev/null >/dev/null; then
@@ -16,26 +15,36 @@
 fi
 
 defs="harfbuzz.def"
-$MAKE $defs > /dev/null
+if ! test -f "$defs"; then
+	echo "check-defs.sh: '$defs' not found; skipping test"
+	exit 77
+fi
+
 tested=false
 for def in $defs; do
 	lib=`echo "$def" | sed 's/[.]def$//;s@.*/@@'`
-	so=$libs/lib${lib}.so
+	for suffix in so dylib; do
+		so=$libs/lib${lib}.$suffix
+		if ! test -f "$so"; then continue; fi
 
-	EXPORTED_SYMBOLS="`nm "$so" | grep ' [BCDGINRSTVW] .' | grep -v ' _fini\>\| _init\>\| _fdata\>\| _ftext\>\| _fbss\>\| __bss_start\>\| __bss_start__\>\| __bss_end__\>\| _edata\>\| _end\>\| _bss_end__\>\| __end__\>\| __gcov_flush\>\| llvm_' | cut -d' ' -f3`"
+		EXPORTED_SYMBOLS="`nm "$so" | grep ' [BCDGINRSTVW] .' | grep -v ' _fini\>\| _init\>\| _fdata\>\| _ftext\>\| _fbss\>\| __bss_start\>\| __bss_start__\>\| __bss_end__\>\| _edata\>\| _end\>\| _bss_end__\>\| __end__\>\| __gcov_flush\>\| llvm_' | cut -d' ' -f3`"
 
-	if test -f "$so"; then
+		# On mac, C symbols are prefixed with _
+		if test $suffix = dylib; then prefix="_"; fi
 
-		echo "Checking that $so has the same symbol list as $def"
-		{
-			echo EXPORTS
-			echo "$EXPORTED_SYMBOLS"
-			# cheat: copy the last line from the def file!
-			tail -n1 "$def"
-		} | diff "$def" - >&2 || stat=1
+		if test -f "$so"; then
 
-		tested=true
-	fi
+			echo "Checking that $so has the same symbol list as $def"
+			{
+				echo EXPORTS
+				echo "$EXPORTED_SYMBOLS" | sed -e "s/^${prefix}hb/hb/g"
+				# cheat: copy the last line from the def file!
+				tail -n1 "$def"
+			} | diff "$def" - >&2 || stat=1
+
+			tested=true
+		fi
+	done
 done
 if ! $tested; then
 	echo "check-defs.sh: libharfbuzz shared library not found; skipping test"
diff --git a/src/gen-def.py b/src/gen-def.py
new file mode 100755
index 0000000..ad1606e
--- /dev/null
+++ b/src/gen-def.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+
+from __future__ import print_function
+
+import io, os, re, sys
+
+headers_content = []
+for h in os.environ["headers"].split (' '):
+	if h.endswith (".h"):
+		with io.open(h, encoding='utf8') as f: headers_content.append (f.read ())
+
+result = ("EXPORTS\n" +
+	"\n".join (sorted (re.findall (r"^hb_\w+(?= \()", "\n".join (headers_content), re.M))) +
+	"\nLIBRARY libharfbuzz-0.dll")
+
+with open (sys.argv[1], "w") as f: f.write (result)