diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 577299576a649d1498881a9376e4de13988291fd..39a7879bb69caed1fcbc4c2b0d587a953a760a2c 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -40,9 +40,24 @@ VERSION = @PACKAGE_VERSION@
 MAJORVERSION = @PG_MAJORVERSION@
 VERSION_NUM = @PG_VERSION_NUM@
 
-# Support for VPATH builds
-# (PGXS VPATH support is handled separately in pgxs.mk)
-ifndef PGXS
+# Set top_srcdir, srcdir, and VPATH.
+ifdef PGXS
+top_srcdir = $(top_builddir)
+
+# If VPATH is set or Makefile is not in current directory we are building
+# the extension with VPATH so we set the variable here.
+ifdef VPATH
+srcdir = $(VPATH)
+else
+ifeq ($(CURDIR),$(dir $(firstword $(MAKEFILE_LIST))))
+srcdir = .
+VPATH =
+else
+srcdir = $(dir $(firstword $(MAKEFILE_LIST)))
+VPATH = $(srcdir)
+endif
+endif
+else # not PGXS
 vpath_build = @vpath_build@
 abs_top_builddir = @abs_top_builddir@
 abs_top_srcdir = @abs_top_srcdir@
diff --git a/src/makefiles/pgxs.mk b/src/makefiles/pgxs.mk
index b5113726841aea42664ba364e0b91cf35dd8d836..b6874ced91c750adb71584991b36894aa5348059 100644
--- a/src/makefiles/pgxs.mk
+++ b/src/makefiles/pgxs.mk
@@ -62,21 +62,6 @@ ifdef PGXS
 top_builddir := $(dir $(PGXS))../..
 include $(top_builddir)/src/Makefile.global
 
-top_srcdir = $(top_builddir)
-# If VPATH is set or Makefile is not in current directory we are building
-# the extension with VPATH so we set the variable here.
-ifdef VPATH
-srcdir = $(VPATH)
-else
-ifeq ($(CURDIR),$(dir $(firstword $(MAKEFILE_LIST))))
-srcdir = .
-VPATH =
-else
-srcdir = $(dir $(firstword $(MAKEFILE_LIST)))
-VPATH = $(srcdir)
-endif
-endif
-
 # These might be set in Makefile.global, but if they were not found
 # during the build of PostgreSQL, supply default values so that users
 # of pgxs can use the variables.