THRIFT-4717: fix up make clean with autoconf
diff --git a/lib/as3/Makefile.am b/lib/as3/Makefile.am
index 5a078c6..0b3c3be 100644
--- a/lib/as3/Makefile.am
+++ b/lib/as3/Makefile.am
@@ -31,6 +31,7 @@
 	./gradlew $(GRADLE_OPTS) clean \
 		-Prelease=true \
                 --console=plain
+	$(RM) -r .gradle
 
 check-local: $(THRIFT)
 	./gradlew $(GRADLE_OPTS) test \
@@ -43,6 +44,9 @@
                 -Psign=true \
 		--console=plain
 
+dist-hook:
+	$(RM) -r $(distdir)/.gradle/
+
 EXTRA_DIST = \
 	CMakeLists.txt \
 	README.md \
diff --git a/lib/csharp/Makefile.am b/lib/csharp/Makefile.am
index 56a8b0f..cc2bbc9 100644
--- a/lib/csharp/Makefile.am
+++ b/lib/csharp/Makefile.am
@@ -92,9 +92,12 @@
 Thrift.45.dll: $(THRIFTCODE)
 	$(CSC) $(CSC_DEFINES) -out:$@ -target:library -reference:System.Web $(THRIFTCODE)
 
-clean-local:
-	$(RM) Thrift.dll  \
-	$(RM) Thrift.45.dll
+CLEANFILES = \
+	Thrift.dll \
+	Thrift.45.dll
+
+DISTCLEANFILES = \
+	Makefile.in
 
 EXTRA_DIST = \
 	$(THRIFTCODE) \
@@ -109,7 +112,3 @@
 	test \
 	coding_standards.md \
 	README.md
-
-MAINTAINERCLEANFILES = \
-	Makefile \
-	Makefile.in
diff --git a/lib/csharp/test/Multiplex/Makefile.am b/lib/csharp/test/Multiplex/Makefile.am
index 57253d6..9c1f1b8 100644
--- a/lib/csharp/test/Multiplex/Makefile.am
+++ b/lib/csharp/test/Multiplex/Makefile.am
@@ -41,8 +41,19 @@
 MultiplexServer.exe: Server/Multiplex.Test.Server.cs ThriftImpl.dll
 	$(CSC) $(CSC_DEFINES) -out:$@ -reference:../../Thrift.dll -reference:ThriftImpl.dll $<
 
+CLEANFILES = \
+	MultiplexClient.exe \
+	MultiplexServer.exe \
+	ThriftImpl.dll
+
+DISTCLEANFILES = \
+	Makefile.in
+
 clean-local:
-	$(RM) -rf gen-csharp *.exe *.dll
+	$(RM) -rf gen-csharp
+
+dist-hook:
+	$(RM) -r $(distdir)/gen-csharp/
 
 TESTPORT = 9501
 check-local: MultiplexServer.exe MultiplexClient.exe
diff --git a/lib/d/Makefile.am b/lib/d/Makefile.am
index 2a81218..4787e0a 100644
--- a/lib/d/Makefile.am
+++ b/lib/d/Makefile.am
@@ -141,9 +141,13 @@
 install-exec-local:
 	$(INSTALL_PROGRAM) $(all_targets) $(DESTDIR)$(libdir)
 
-
 clean-local:
-	$(RM) -rf docs $(D_LIB_NAME) $(D_EVENT_LIB_NAME) $(D_SSL_LIB_NAME) unittest
+	$(RM) -r docs
+	$(RM) $(D_LIB_NAME)
+	$(RM) $(D_EVENT_LIB_NAME)
+	$(RM) $(D_SSL_LIB_NAME)
+	$(RM) -r test/gen-d
+	$(RM) -r unittest
 
 
 #
diff --git a/lib/dart/Makefile.am b/lib/dart/Makefile.am
index ab6ddc0..373a883 100644
--- a/lib/dart/Makefile.am
+++ b/lib/dart/Makefile.am
@@ -22,12 +22,18 @@
 
 clean-local:
 	$(RM) -r .pub
-	find . -type d -name "packages" | xargs $(RM) -r
+	find . -type d -name ".dart_tool" | xargs $(RM) -r
 	find . -type f -name ".packages" | xargs $(RM)
-	find . -type f -name "pubspec.lock" | xargs $(RM)
+	find . -type d -name "packages" | xargs $(RM) -r
 
 check-local: all
 
+dist-hook:
+	$(RM) -r $(distdir)/.pub
+	find $(distdir) -type d -name ".dart_tool" | xargs $(RM) -r
+	find $(distdir) -type f -name ".packages" | xargs $(RM)
+	find $(distdir) -type d -name "packages" | xargs $(RM) -r
+
 EXTRA_DIST = \
 	.analysis_options
 
diff --git a/lib/erl/Makefile.am b/lib/erl/Makefile.am
index 06323b4..fdfc9eb 100644
--- a/lib/erl/Makefile.am
+++ b/lib/erl/Makefile.am
@@ -67,16 +67,26 @@
 	done
 
 uninstall:
-	rm -rf $(DESTDIR)$(ERLANG_INSTALL_LIB_DIR_thrift)
+	$(RM) -rf $(DESTDIR)$(ERLANG_INSTALL_LIB_DIR_thrift)
 
-clean:
-	rm -f .generated
-	rm -rf test/gen-erl/
-	rm -f $(THRIFT_OMIT_FILE)
+clean-local:
 	$(REBAR) clean
+	$(RM)    .generated
+	$(RM) -r .rebar/
+	$(RM) -r deps/
+	$(RM) -r test/gen-erl/
+	$(RM)    $(THRIFT_OMIT_FILE)
 
 maintainer-clean-local:
-	rm -rf ebin
+	$(RM) -r ebin/
+
+dist-hook:
+	$(RM)    $(distdir)/.generated
+	$(RM) -r $(distdir)/.rebar/
+	$(RM) -r $(distdir)/deps/
+	$(RM) -r $(distdir)/ebin/
+	$(RM) -r $(distdir)/test/gen-erl/
+	$(RM)    $(distdir)/$(THRIFT_OMIT_FILE)
 
 EXTRA_DIST = \
 	include \
diff --git a/lib/js/Makefile.am b/lib/js/Makefile.am
index 9ea20a4..b534042 100644
--- a/lib/js/Makefile.am
+++ b/lib/js/Makefile.am
@@ -18,13 +18,45 @@
 #
 
 # Make sure this doesn't fail if ant is not configured.
-# We call install twice to work around npm issues
+# We call npm install twice to work around older npm issues
+# (note these issues may no longer be present, but it is ok)
 #
+
 if HAVE_NPM
+
 SUBDIRS = test
 
-check-local: all
+prereq:
 	$(NPM) install || $(NPM) install
 	$(NPM) list
+
+check-local: prereq all
 	./node_modules/.bin/grunt
+
+doc: prereq
+	./node_modules/.bin/grunt jsdoc
+
 endif
+
+clean-local:
+	$(RM) -r dist
+	$(RM) -r doc
+	$(RM) -r node_modules
+	$(RM) -r test/build/
+	$(RM) -r test/gen-*/
+
+dist-hook:
+	$(RM) -r $(distdir)/dist/
+	$(RM) -r $(distdir)/doc/
+	$(RM) -r $(distdir)/node_modules/
+	$(RM) -r $(distdir)/test/build/
+	$(RM) -r $(distdir)/test/gen-*/
+
+EXTRA_DIST = \
+	coding_standards.md \
+	Gruntfile.js \
+	package.json \
+	package-lock.json \
+	README.md \
+	src \
+	test
diff --git a/lib/js/test/build.xml b/lib/js/test/build.xml
index 4795fd2..d891b43 100755
--- a/lib/js/test/build.xml
+++ b/lib/js/test/build.xml
@@ -73,7 +73,7 @@
       </condition>
       You need libthrift*.jar and libthrift*test.jar located at
       ${thrift.java.dir}/build/libs
-      Did you compile Thrift Java library and its test suite by "ant compile-test"?
+      Did you compile Thrift Java library and its test suite by "make check"?
     </fail>
     <fail>
       <condition>
diff --git a/lib/json/Makefile.am b/lib/json/Makefile.am
index 1051b9b..6c8c0ce 100644
--- a/lib/json/Makefile.am
+++ b/lib/json/Makefile.am
@@ -17,13 +17,17 @@
 # under the License.
 #
 
-SUBDIRS =
-
 if WITH_JAVA
 # Schema validation test depends on java
-SUBDIRS += test
+SUBDIRS = test
 endif
 
+clean-local:
+	$(RM) -r test/build/
+
+dist-hook:
+	$(RM) -r $(distdir)/test/build/
+
 EXTRA_DIST = \
     schema.json \
     test
diff --git a/lib/netcore/Makefile.am b/lib/netcore/Makefile.am
index caf3f34..4e2d6a5 100644
--- a/lib/netcore/Makefile.am
+++ b/lib/netcore/Makefile.am
@@ -17,18 +17,35 @@
 # under the License.
 #
 
-SUBDIRS = . 
-
 all-local:
 	$(DOTNETCORE) build
 
 check-local:
 	$(DOTNETCORE) test Tests/Thrift.Tests/Thrift.Tests.csproj
-	${DOTNETCORE} test Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj
+	$(DOTNETCORE) test Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj
 
 clean-local:
 	$(RM) -r Thrift/bin
 	$(RM) -r Thrift/obj
+	$(RM) -r Tests/Thrift.Tests/bin
+	$(RM) -r Tests/Thrift.Tests/obj
+	$(RM) -r Tests/Thrift.IntegrationTests/bin
+	$(RM) -r Tests/Thrift.IntegrationTests/obj
+	$(RM) -r Tests/Thrift.PublicInterfaces.Compile.Tests/bin
+	$(RM) -r Tests/Thrift.PublicInterfaces.Compile.Tests/obj
+
+dist-hook:
+	$(RM) -r $(distdir)/Thrift/bin
+	$(RM) -r $(distdir)/Thrift/obj
+	$(RM) -r $(distdir)/Tests/Thrift.Tests/bin
+	$(RM) -r $(distdir)/Tests/Thrift.Tests/obj
+	$(RM) -r $(distdir)/Tests/Thrift.IntegrationTests/bin
+	$(RM) -r $(distdir)/Tests/Thrift.IntegrationTests/obj
+	$(RM) -r $(distdir)/Tests/Thrift.PublicInterfaces.Compile.Tests/bin
+	$(RM) -r $(distdir)/Tests/Thrift.PublicInterfaces.Compile.Tests/obj
+
+DISTCLEANFILES = \
+	Makefile.in
 
 EXTRA_DIST = \
 	README.md \
diff --git a/lib/nodejs/Makefile.am b/lib/nodejs/Makefile.am
index 68ea3ea..091f768 100755
--- a/lib/nodejs/Makefile.am
+++ b/lib/nodejs/Makefile.am
@@ -32,7 +32,7 @@
 	cd $(top_srcdir) && $(NPM) test && $(NPM) run lint-tests && cd lib/nodejs
 
 clean-local:
-	$(RM) -r test/gen-nodejs
+	$(RM) -r test/gen-*
 	$(RM) -r $(top_srcdir)/node_modules
 
 EXTRA_DIST = \
diff --git a/lib/php/Makefile.am b/lib/php/Makefile.am
index 8d9050a..7fdebe1 100755
--- a/lib/php/Makefile.am
+++ b/lib/php/Makefile.am
@@ -33,6 +33,7 @@
 phpmodule_SCRIPTS = src/ext/thrift_protocol/modules/thrift_protocol.so
 
 distclean-local:
+	if [ -f src/ext/thrift_protocol/Makefile ]; then cd src/ext/thrift_protocol/ && $(MAKE) distclean; fi
 	cd $(phpmodule_SCRIPTS) && $(PHPIZE) --clean
 
 endif
@@ -128,6 +129,10 @@
   lib/Type/TType.php \
   lib/Type/TConstant.php
 
+clean-local:
+	if [ -f src/ext/thrift_protocol/Makefile ]; then cd src/ext/thrift_protocol/ && $(MAKE) clean; fi
+
+
 EXTRA_DIST = \
   lib \
   src/autoload.php \
@@ -144,6 +149,5 @@
   README.md
 
 MAINTAINERCLEANFILES = \
-  Makefile \
   Makefile.in
 
diff --git a/lib/php/test/Makefile.am b/lib/php/test/Makefile.am
index 4824688..6f4e50a 100755
--- a/lib/php/test/Makefile.am
+++ b/lib/php/test/Makefile.am
@@ -48,6 +48,8 @@
   check-validator \
   check-json-serializer
 
+distclean-local:
+
 clean-local:
 	$(RM) -r ./packages
 	$(RM) TEST-*.xml
diff --git a/lib/py/Makefile.am b/lib/py/Makefile.am
index 5861858..46e4405 100644
--- a/lib/py/Makefile.am
+++ b/lib/py/Makefile.am
@@ -40,13 +40,19 @@
 install-exec-hook:
 	$(PYTHON) setup.py install --root=$(DESTDIR) --prefix=$(PY_PREFIX) $(PYTHON_SETUPUTIL_ARGS)
 
-clean-local:
-	$(RM) -r build
-
 check-local: all py3-test
 	$(PYTHON) test/thrift_json.py
 	$(PYTHON) test/test_sslsocket.py
 
+clean-local:
+	$(RM) -r build
+	find . -type f \( -iname "*.pyc" \) | xargs rm -f
+	find . -type d \( -iname "__pycache__" -or -iname "_trial_temp" \) | xargs rm -rf
+
+dist-hook:
+	find $(distdir) -type f \( -iname "*.pyc" \) | xargs rm -f
+	find $(distdir) -type d \( -iname "__pycache__" -or -iname "_trial_temp" \) | xargs rm -rf
+
 EXTRA_DIST = \
 	CMakeLists.txt \
 	MANIFEST.in \
diff --git a/lib/rb/Makefile.am b/lib/rb/Makefile.am
index 137edb4..1841065 100755
--- a/lib/rb/Makefile.am
+++ b/lib/rb/Makefile.am
@@ -31,6 +31,7 @@
 clean-local:
 	$(BUNDLER) install
 	$(BUNDLER) exec rake clean
+	$(RM) -r spec/gen-rb/
 
 check-local: all
 	$(BUNDLER) install
@@ -38,6 +39,9 @@
 
 endif
 
+dist-hook:
+	$(RM) -r $(distdir)/spec/gen-rb/
+
 EXTRA_DIST = \
 	coding_standards.md \
 	Rakefile \