diff --git a/.gitignore b/.gitignore
index 4976fd9..99c8297 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,3 +40,8 @@ lib*.pc
 /Debug/
 /Release/
 /tmp_install/
+/CMakeCache.txt
+/CMakeFiles
+/cmake_install.cmake
+/install_manifest.txt
+/build
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..f9da88e
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,1343 @@
+cmake_minimum_required(VERSION 2.8.4)
+
+project(Postgres C)
+
+include(CheckTypeSize)
+include(CheckSymbolExists)
+include(CheckFunctionExists)
+include(CheckIncludeFiles)
+include(CheckCSourceCompiles)
+include(CheckCSourceRuns)
+include(TestBigEndian)
+include(CheckStructHasMember)
+
+if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
+	message(STATUS "${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}")
+	message(FATAL_ERROR "You must use another folder for build PostgreSQL")
+endif()
+
+set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
+
+set(POSTGRES_MAJOR_VERSION 10)
+set(POSTGRES_MINOR_VERSION 0)
+set(POSTGRES_PATCH_VERSION 0)
+set(POSTGRES_VERSION
+  ${POSTGRES_MAJOR_VERSION}.${POSTGRES_MINOR_VERSION}.${POSTGRES_PATCH_VERSION})
+
+set(PG_VERSION "10devel")
+set(PG_VERSION_NUM 100000)
+set(PACKAGE_BUGREPORT "pgsql-bugs@postgresql.org")
+
+ # Offer the user the choice of overriding the installation directories
+set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries")
+set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables")
+set(INSTALL_INCLUDE_DIR include CACHE PATH
+  "Installation directory for header files")
+
+if(NOT PGBINDIR)
+	set(PGBINDIR "${CMAKE_INSTALL_PREFIX}/bin")
+endif(NOT PGBINDIR)
+
+if(NOT PGSHAREDIR)
+	set(PGSHAREDIR "${CMAKE_INSTALL_PREFIX}/share/postgresql")
+endif(NOT PGSHAREDIR)
+
+if(NOT SYSCONFDIR)
+	set(SYSCONFDIR "${CMAKE_INSTALL_PREFIX}/etc")
+endif(NOT SYSCONFDIR)
+
+if(NOT INCLUDEDIR)
+	set(INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
+endif(NOT INCLUDEDIR)
+
+if(NOT PKGINCLUDEDIR)
+	set(PKGINCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
+endif(NOT PKGINCLUDEDIR)
+
+if(NOT INCLUDEDIRSERVER)
+	set(INCLUDEDIRSERVER "${CMAKE_INSTALL_PREFIX}/include/server")
+endif(NOT INCLUDEDIRSERVER)
+
+if(MSVC)
+	if(NOT LIBDIR)
+		set(LIBDIR "${CMAKE_INSTALL_PREFIX}/bin")
+	endif(NOT LIBDIR)
+
+	if(NOT PKGLIBDIR)
+		set(PKGLIBDIR "${CMAKE_INSTALL_PREFIX}/bin")
+	endif(NOT PKGLIBDIR)
+else()
+	if(NOT LIBDIR)
+		set(LIBDIR "${CMAKE_INSTALL_PREFIX}/lib")
+	endif(NOT LIBDIR)
+
+	if(NOT PKGLIBDIR)
+		set(PKGLIBDIR "${CMAKE_INSTALL_PREFIX}/lib")
+	endif(NOT PKGLIBDIR)
+endif()
+
+if(NOT LOCALEDIR)
+	set(LOCALEDIR "${CMAKE_INSTALL_PREFIX}/share/locale")
+endif(NOT LOCALEDIR)
+
+if(NOT DOCDIR)
+	set(DOCDIR "${CMAKE_INSTALL_PREFIX}/share/doc/")
+endif(NOT DOCDIR)
+
+if(NOT HTMLDIR)
+	set(HTMLDIR "${CMAKE_INSTALL_PREFIX}/share/doc/")
+endif(NOT HTMLDIR)
+
+if(NOT MANDIR)
+	set(MANDIR "${CMAKE_INSTALL_PREFIX}/share/man")
+endif(NOT MANDIR)
+
+if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -multiply_defined suppress -undefined dynamic_lookup")
+	set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -multiply_defined suppress -undefined dynamic_lookup")
+endif()
+
+if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD|NetBSD|OpenBSD")
+	set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "-Wl,-x,-soname,")
+endif()
+
+if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-ignored-attributes")
+endif()
+
+if(MSVC)
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_NO_WARNINGS")
+endif()
+
+test_big_endian(WORDS_BIGENDIAN)
+set(FIND_LIBRARY_USE_LIB64_PATHS ON)
+
+find_package(Perl REQUIRED)
+option(WITH_PERL "libperl is optional" ON)
+if(WITH_PERL)
+	find_package(PerlLibs)
+endif()
+if(PERLLIBS_FOUND)
+	EXECUTE_PROCESS(COMMAND ${PERL_EXECUTABLE} -MExtUtils::Embed -e ccopts OUTPUT_VARIABLE PERL_CFLAGS)
+	EXECUTE_PROCESS(COMMAND ${PERL_EXECUTABLE} -MExtUtils::Embed -e ldopts OUTPUT_VARIABLE PERL_LDFLAGS)
+	STRING(REGEX REPLACE "[\r\n]" " " PERL_CFLAGS ${PERL_CFLAGS})
+	STRING(REGEX REPLACE " +$" "" PERL_CFLAGS ${PERL_CFLAGS})
+	STRING(STRIP "${PERL_CFLAGS}" PERL_CFLAGS)
+	STRING(REGEX REPLACE "[\r\n]" " " PERL_LDFLAGS ${PERL_LDFLAGS})
+	STRING(REGEX REPLACE " +$" "" PERL_LDFLAGS ${PERL_LDFLAGS})
+	STRING(REGEX REPLACE "-lperl" "" PERL_LDFLAGS ${PERL_LDFLAGS})
+	if(MINGW AND NOT MSYS)
+		STRING(REGEX REPLACE "\\\\" "/" PERL_LDFLAGS ${PERL_LDFLAGS})
+	endif()
+	STRING(STRIP "${PERL_LDFLAGS}" PERL_LDFLAGS)
+endif()
+
+find_package(BISON REQUIRED)
+find_package(FLEX REQUIRED)
+find_package(Threads)
+
+option(WITH_OPENSSL "OPENSSL is optional" ON)
+if(WITH_OPENSSL)
+	find_package(OpenSSL)
+endif()
+find_package(ZLIB)
+
+option(WITH_PYTHON "Python is optional" ON)
+if(WITH_PYTHON)
+	find_package(PythonInterp)
+	find_package(PythonLibs)
+endif()
+find_package(SELinux)
+
+option(WITH_LIBXML "LIBXML is optional" ON)
+if(WITH_LIBXML)
+	find_package(LibXml2)
+	find_package(LibXslt)
+endif()
+
+option(WITH_TCL "TCL is optional" ON)
+if(WITH_TCL)
+	find_package(TCL)
+endif()
+find_package(LibSocket)
+
+option(USE_PAM "build with PAM support" OFF)
+if(USE_PAM)
+	find_library(PAM_LIB pam)
+	if(NOT PAM_LIB)
+		message(ERROR "library 'pam' is required for PAM")
+	endif()
+endif()
+
+option(USE_LDAP "build with LDAP support" OFF)
+if(USE_LDAP)
+	find_package(LDAP REQUIRED)
+endif()
+
+if(WIN32 OR MINGW)
+	set(NEED_REPL_SNPRINTF ON)
+else()
+	set(NEED_REPL_SNPRINTF OFF)
+endif()
+
+option(ENABLE_NLS "Define if you want National Language Support" OFF)
+if(ENABLE_NLS)
+	include(MakeNLS)
+	find_package(Gettext REQUIRED)
+	find_program(GETTEXT_XGETTEXT_EXECUTABLE xgettext)
+	if(NOT GETTEXT_XGETTEXT_EXECUTABLE)
+		message(FATAL_ERROR "xgettext not found")
+	endif(NOT GETTEXT_XGETTEXT_EXECUTABLE)
+	if(NOT NLS_LANGUAGES)
+		set(NLS_LANGUAGES "ko;cs;pt_BR;zh_CN;ru;fr;de;es;it;tr;ja;pl;zh_TW")
+	endif()
+
+	if(NOT NEED_REPL_SNPRINTF)
+		check_c_source_runs("
+			#include <stdio.h>
+			#include <string.h>
+
+			int main()
+			{
+			  char buf[100];
+
+			  /* can it swap arguments? */
+			  snprintf(buf, 100, \"%2\$d %1\$d\", 3, 4);
+			  if (strcmp(buf, \"4 3\") != 0)
+				return 1;
+			  return 0;
+			}
+		" SNPRINTF_ARG_CONTROL)
+		if(NOT SNPRINTF_ARG_CONTROL)
+			set(NEED_REPL_SNPRINTF ON)
+		endif()
+	endif()
+endif()
+
+option(ENABLE_GSS "Define to build with GSSAPI support." OFF)
+if(ENABLE_GSS)
+	check_include_files(gssapi/gssapi.h HAVE_GSSAPI_GSSAPI_H)
+	check_include_files(gssapi.h HAVE_GSSAPI_H)
+	set(PG_KRB_SRVTAB "FILE:${SYSCONFDIR}/krb5.keytab")
+	if(WIN32)
+		SET(GSS_LIBS "-lgssapi32")
+	else()
+		set(CMAKE_REQUIRED_LIBRARIES_OLD ${CMAKE_REQUIRED_LIBRARIES})
+		set(CMAKE_REQUIRED_LIBRARIES "-lgssapi_krb5")
+		check_function_exists(gss_init_sec_context HAVE_GSS_INIT)
+		if(NOT HAVE_GSS_INIT)
+			set(CMAKE_REQUIRED_LIBRARIES "-lgss")
+			check_function_exists(gss_init_sec_context HAVE_GSS_INIT)
+			if(NOT HAVE_GSS_INIT)
+				set(CMAKE_REQUIRED_LIBRARIES "-lgssapi -lkrb5 -lcrypto")
+				check_function_exists(gss_init_sec_context HAVE_GSS_INIT)
+			endif(NOT HAVE_GSS_INIT)
+		endif(NOT HAVE_GSS_INIT)
+		if(HAVE_GSS_INIT)
+			set(GSS_LIBS ${CMAKE_REQUIRED_LIBRARIES})
+		else()
+			message(FATAL_ERROR "Not found gss_init_sec_context function for GSSAPI")
+		endif()
+		set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_OLD})
+	endif()
+else()
+	set(GSS_LIBS "")
+endif()
+
+option(USE_SYSTEMD "Define to build with systemd support" OFF)
+option(USE_BSD_AUTH "Define to build with BSD Authentication support" OFF)
+option(USE_ASSERT_CHECKING "Define to build with assertion checks." OFF)
+option(USE_BONJOUR "Define to build with Bonjour support." OFF)
+if(USE_BONJOUR)
+	check_include_files(dns_sd.h HAVE_DNS_SD_H)
+	if(NOT HAVE_DNS_SD_H)
+		message(FATAL_ERROR "header file <dns_sd.h> is required for Bonjour")
+	endif()
+endif()
+
+if(NOT MSVC)
+	find_library(DL_LIBRARIES NAMES dl)
+	find_library(M_LIB m)
+	if(NOT M_LIB)
+		set(M_LIB "")
+	endif()
+endif()
+
+if(NOT DL_LIBRARIES)
+	set(DL_LIBRARIES "")
+endif()
+
+if(LIBXML2_FOUND)
+	set(HAVE_LIBXML2 1)
+	set(USE_LIBXML 1)
+endif()
+
+if(LIBXSLT_FOUND)
+	set(HAVE_LIBXSLT 1)
+	set(USE_LIBXSLT 1)
+endif()
+
+if(ZLIB_FOUND)
+	set(HAVE_LIBZ 1)
+endif()
+
+if(CMAKE_COMPILER_IS_GNUCC)
+	if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 3.3 OR CMAKE_C_COMPILER_VERSION VERSION_EQUAL 3.3)
+		# Disable strict-aliasing rules; needed for gcc 3.3+
+		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
+	endif()
+
+	if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 3.4 OR CMAKE_C_COMPILER_VERSION VERSION_EQUAL 3.4)
+		# Disable FP optimizations that cause various errors on gcc 4.5+ or maybe 4.6+
+		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fwrapv")
+	endif()
+
+	if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.5 OR CMAKE_C_COMPILER_VERSION VERSION_EQUAL 4.5)
+		# Disable FP optimizations that cause various errors on gcc 4.5+ or maybe 4.6+
+		set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fexcess-precision=standard")
+	endif()
+
+	# Optimization flags for specific files that benefit from vectorization
+	set(CFLAGS_VECTOR "${CFLAGS_VECTOR} -funroll-loops")
+	if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.5 OR CMAKE_C_COMPILER_VERSION VERSION_EQUAL 4.5)
+		set(CFLAGS_VECTOR "${CFLAGS_VECTOR} -ftree-vectorize")
+	endif()
+
+	option(PROFILE_PID_DIR "Enable to allow profiling output to be saved separately for each process." OFF)
+endif()
+
+if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+	# Disable strict-aliasing rules; needed for gcc 3.3+
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
+	# Disable FP optimizations that cause various errors on gcc 4.5+ or maybe 4.6+
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fwrapv")
+
+	# Optimization flags for specific files that benefit from vectorization
+	set(CFLAGS_VECTOR "${CFLAGS_VECTOR} -funroll-loops")
+	set(CFLAGS_VECTOR "${CFLAGS_VECTOR} -ftree-vectorize")
+endif()
+
+if(CMAKE_C_COMPILER_ID STREQUAL "Intel")
+	# Intel's compiler has a bug/misoptimization in checking for
+	# division by NAN (NaN == 0), -mp1 fixes it, so add it to the CFLAGS.
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mp1")
+	# Make sure strict aliasing is off (though this is said to be the default)
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
+endif()
+
+check_symbol_exists(strlcpy "stdio.h;string.h" HAVE_DECL_STRLCPY)
+if(NOT HAVE_DECL_STRLCPY)
+	set(HAVE_DECL_STRLCPY 0)
+endif()
+check_symbol_exists(strlcat "stdio.h;string.h" HAVE_DECL_STRLCAT)
+if(NOT HAVE_DECL_STRLCAT)
+	set(HAVE_DECL_STRLCAT 0)
+endif()
+check_symbol_exists(snprintf "stdio.h;string.h" HAVE_DECL_SNPRINTF)
+if(NOT HAVE_DECL_SNPRINTF)
+	set(HAVE_DECL_SNPRINTF 0)
+endif()
+check_symbol_exists(vsnprintf "stdio.h;string.h" HAVE_DECL_VSNPRINTF)
+if(NOT HAVE_DECL_VSNPRINTF)
+	set(HAVE_DECL_VSNPRINTF 0)
+endif()
+check_symbol_exists(unsetenv "stdlib.h" HAVE_UNSETENV)
+check_symbol_exists(srandom "stdlib.h" HAVE_SRANDOM)
+
+# Test math functions
+check_symbol_exists(rint "math.h" HAVE_RINT)
+
+if(WIN32 AND NOT MINGW)
+	set(LIB_M "")
+else()
+	set(LIB_M m)
+endif()
+set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${LIB_M}")
+check_c_source_compiles("
+	#include <math.h>
+	double glob_double;
+	int main(void){
+		return isinf(glob_double) ? 0 : 1;
+	}
+" HAVE_ISINF)
+
+check_include_files(ieeefp.h HAVE_IEEEFP_H)
+if(NOT HAVE_IEEEFP_H)
+	set(HAVE_IEEEFP_H 0)
+else()
+	set(HAVE_IEEEFP_H 1)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};ieeefp.h")
+endif()
+
+check_include_files(fp_class.h HAVE_FP_CLASS_H)
+if (HAVE_FP_CLASS_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};fp_class.h")
+endif()
+set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};float.h;math.h")
+
+# Need for isinf implementation
+check_function_exists(fpclass HAVE_FPCLASS)
+check_function_exists(fp_class HAVE_FP_CLASS)
+check_function_exists(fp_class_d HAVE_FP_CLASS_D)
+check_function_exists(class HAVE_CLASS)
+
+check_function_exists(getpeereid HAVE_GETPEEREID)
+check_function_exists(getpeerucred HAVE_GETPEERUCRED)
+check_function_exists(memmove HAVE_MEMMOVE)
+if(MSVC)
+	check_function_exists(_mbstowcs_l HAVE_MBSTOWCS_L)
+else()
+	check_function_exists(mbstowcs_l HAVE_MBSTOWCS_L)
+endif()
+
+check_function_exists(towlower HAVE_TOWLOWER)
+check_function_exists(wcstombs HAVE_WCSTOMBS)
+
+check_function_exists(mkdtemp HAVE_MKDTEMP)
+check_function_exists(mkstemp HAVE_MKSTEMP)
+check_function_exists(poll HAVE_POLL)
+
+check_function_exists(getrlimit HAVE_GETRLIMIT)
+check_function_exists(readlink HAVE_READLINK)
+check_function_exists(cbrt HAVE_CBRT)
+check_function_exists(pthread_is_threaded_np HAVE_PTHREAD_IS_THREADED_NP)
+check_function_exists(random HAVE_RANDOM)
+check_function_exists(sync_file_range HAVE_SYNC_FILE_RANGE)
+
+#So strange
+check_function_exists(pstat HAVE_PSTAT)
+
+check_include_files(ifaddrs.h HAVE_IFADDRS_H)
+if(HAVE_IFADDRS_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};ifaddrs.h")
+endif()
+check_function_exists(getifaddrs HAVE_GETIFADDRS)
+
+if(MSVC)
+	set(HAVE_MINIDUMP_TYPE 1)
+endif()
+
+if(NOT CMAKE_SYSTEM_NAME STREQUAL "SunOS")
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};fcntl.h")
+	check_function_exists(posix_fadvise HAVE_DECL_POSIX_FADVISE)
+	if(HAVE_DECL_POSIX_FADVISE)
+		set(HAVE_POSIX_FADVISE 1)
+	endif()
+endif()
+
+find_library(CRYPT_LIB crypt)
+if(CRYPT_LIB)
+	set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} ${CRYPT_LIB}")
+endif()
+check_include_files(crypt.h HAVE_CRYPT_H)
+if(HAVE_CRYPT_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};crypt.h")
+endif()
+check_function_exists(crypt HAVE_CRYPT)
+
+check_include_files(unistd.h HAVE_UNISTD_H)
+if(HAVE_UNISTD_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};unistd.h")
+endif()
+check_function_exists(fdatasync HAVE_DECL_FDATASYNC)
+check_function_exists(sys_siglist HAVE_DECL_FDATASYNC)
+check_function_exists(setproctitle HAVE_SETPROCTITLE)
+check_function_exists(setsid HAVE_SETSID)
+if(HAVE_UNISTD_H)
+	check_symbol_exists(sys_siglist "signal.h;unistd.h" HAVE_DECL_SYS_SIGLIST)
+	check_symbol_exists(opterr "unistd.h" HAVE_INT_OPTERR)
+	check_symbol_exists(optreset "unistd.h" HAVE_INT_OPTRESET)
+else()
+	check_symbol_exists(sys_siglist "signal.h" HAVE_DECL_SYS_SIGLIST)
+endif()
+
+set(CMAKE_MACOSX_RPATH 1)
+#set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+if(MSVC)
+	check_function_exists(_fseeki64 HAVE_FSEEKO)
+else()
+	check_function_exists(fseeko HAVE_FSEEKO)
+endif()
+
+if(NOT MSVC)
+	check_include_files(dlfcn.h HAVE_DLFCN_H)
+	if(HAVE_DLFCN_H)
+		set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};dlfcn.h")
+	endif()
+	set(CMAKE_REQUIRED_LIBRARIES ${DL_LIBRARIES})
+	check_function_exists(dlopen HAVE_DLOPEN)
+endif()
+
+set(CMAKE_REQUIRED_LIBRARIES "")
+check_include_files(strings.h HAVE_STRINGS_H)
+check_include_files(string.h HAVE_STRING_H)
+if(HAVE_STRINGS_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};strings.h")
+endif()
+if(HAVE_STRING_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};string.h")
+endif()
+check_function_exists(fls HAVE_FLS)
+
+check_include_files(getopt.h HAVE_GETOPT_H)
+if(HAVE_GETOPT_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};getopt.h")
+endif()
+
+check_function_exists(strtoll HAVE_STRTOLL)
+check_function_exists(strtoq HAVE_STRTOQ)
+check_function_exists(strtoull HAVE_STRTOULL)
+check_function_exists(strtouq HAVE_STRTOUQ)
+
+check_type_size("struct option" HAVE_STRUCT_OPTION)
+check_function_exists(getopt HAVE_GETOPT)
+check_function_exists(getopt_long HAVE_GETOPT_LONG)
+check_function_exists(gethostbyname_r HAVE_GETHOSTBYNAME_R)
+check_function_exists(getpwuid_r HAVE_GETPWUID_R)
+check_function_exists(strerror_r HAVE_STRERROR_R)
+check_function_exists(strerror HAVE_STRERROR)
+#I am not sure about this code.
+check_c_source_runs("
+#include <string.h>
+int main(void){
+#ifndef _AIX
+int strerror_r(int, char *, size_t);
+#else
+/* Older AIX has 'int' for the third argument so we don't test the args. */
+int strerror_r();
+#endif
+return 0;
+}
+" STRERROR_R_INT)
+
+check_c_source_runs("
+int main(void){
+int a = 0; int *p = &a; int r;
+ __asm__ __volatile__ (\" lwarx %0,0,%1,1\n\" : \"=&r\"(r) : \"r\"(p));
+return 0;
+}
+" HAVE_PPC_LWARX_MUTEX_HINT)
+
+check_c_source_runs("
+#include <machine/vmparam.h>
+#include <sys/exec.h>
+int main(void){
+PS_STRINGS->ps_nargvstr = 1;
+PS_STRINGS->ps_argvstr = \"foo\";
+return 0;
+}
+" HAVE_PS_STRINGS)
+
+check_include_files(sys/mman.h HAVE_SYS_MMAN_H)
+if(HAVE_SYS_MMAN_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};sys/mman.h")
+endif()
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+	set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES} -lrt")
+endif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+check_function_exists(shm_open HAVE_SHM_OPEN)
+
+check_include_files(sys/time.h HAVE_SYS_TIME_H)
+check_include_files(sys/resource.h HAVE_SYS_RESOURCE_H)
+if(HAVE_SYS_TIME_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};sys/time.h")
+endif()
+if(HAVE_SYS_RESOURCE_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};sys/resource.h")
+endif()
+check_function_exists(getrusage HAVE_GETRUSAGE)
+check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
+
+check_include_files(utime.h HAVE_UTIME_H)
+if(HAVE_UTIME_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};utime.h")
+endif()
+check_function_exists(utime HAVE_UTIME)
+check_function_exists(utimes HAVE_UTIMES)
+
+set(OLD_INCLUDES "${CMAKE_EXTRA_INCLUDE_FILES}")
+set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};netinet/in.h;arpa/inet.h")
+check_function_exists(inet_aton HAVE_INET_ATON)
+set(CMAKE_EXTRA_INCLUDE_FILES "${OLD_INCLUDES}")
+
+if(OPENSSL_FOUND)
+	set(USE_OPENSSL 1)
+	set(CMAKE_REQUIRED_INCLUDES "${CMAKE_REQUIRED_INCLUDES};${OPENSSL_INCLUDE_DIR}")
+	set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
+	check_function_exists(SSL_get_current_compression HAVE_SSL_GET_CURRENT_COMPRESSION)
+endif(OPENSSL_FOUND)
+
+
+check_include_files(sys/un.h HAVE_SYS_UN_H)
+check_include_files(ucred.h HAVE_UCRED_H)
+check_include_files(sys/ucred.h HAVE_SYS_UCRED_H)
+check_include_files(sys/types.h HAVE_SYS_TYPES_H)
+check_include_files(sys/socket.h HAVE_SYS_SOCKET_H)
+check_include_files(sys/sockio.h HAVE_SYS_SOCKIO_H)
+check_include_files(stdint.h HAVE_STDINT_H)
+
+check_include_files(sys/select.h HAVE_SYS_SELECT_H)
+check_include_files(sys/poll.h HAVE_SYS_POLL_H)
+check_include_files(sys/epoll.h HAVE_SYS_EPOLL_H)
+check_include_files(sys/pstat.h HAVE_SYS_PSTAT_H)
+check_include_files(sys/tas.h HAVE_SYS_TAS_H)
+check_include_files(dld.h HAVE_DLD_H)
+check_include_files(langinfo.h HAVE_LANGINFO_H)
+check_include_files(poll.h HAVE_POLL_H)
+
+check_include_files(wchar.h HAVE_WCHAR_H)
+check_include_files(wctype.h HAVE_WCTYPE_H)
+check_include_files(winldap.h HAVE_WINLDAP_H)
+check_include_files(pwd.h HAVE_PWD_H)
+check_include_files(crtdefs.h HAVE_CRTDEFS_H)
+
+check_include_files(nbtool_config.h HAVE_NBTOOL_CONFIG_H)
+
+check_include_files(mbarrier.h HAVE_MBARRIER_H)
+check_include_files(atomic.h HAVE_ATOMIC_H)
+
+check_include_files(netinet/tcp.h HAVE_NETINET_TCP_H)
+check_include_files(net/if.h HAVE_NET_IF_H)
+
+check_include_files(pam/pam_appl.h HAVE_PAM_PAM_APPL_H)
+check_include_files(security/pam_appl.h HAVE_SECURITY_PAM_APPL_H)
+if(USE_PAM AND NOT (HAVE_PAM_PAM_APPL_H OR HAVE_SECURITY_PAM_APPL_H))
+	message(FATAL_ERROR "header file <security/pam_appl.h> or <pam/pam_appl.h> is required for PAM.")
+endif()
+
+check_include_files(syslog.h HAVE_SYSLOG)
+check_include_files(termios.h HAVE_TERMIOS_H)
+
+if(USE_SYSTEMD)
+	check_include_files(systemd/sd-daemon.h HAVE_SYSTEMD_SD_DAEMON_H)
+	if(NOT HAVE_SYSTEMD_SD_DAEMON_H)
+		message(FATAL_ERROR "header file <systemd/sd-daemon.h> is required for systemd support")
+	endif()
+endif()
+
+check_include_files(bsd_auth.h HAVE_BSD_AUTH_H)
+if(USE_BSD_AUTH AND NOT HAVE_BSD_AUTH_H)
+	message(FATAL_ERROR "header file <bsd_auth.h> is required for BSD Authentication support")
+endif()
+
+
+#Check ReadLine includes
+option(WITH_READLINE "do not use GNU Readline nor BSD Libedit for editing" ON)
+if(WITH_READLINE AND NOT MSVC)
+	find_package(Readline)
+	if(READLINE_FOUND)
+		#Sometimes for redline need curses
+		message(STATUS "Found Readline: ${READLINE_LIBRARY}")
+		find_package(Curses)
+		if(NOT CURSES_FOUND)
+			set(CURSES_NEED_NCURSES TRUE)
+		endif()
+		find_package(Curses)
+		check_include_files("stdio.h;readline.h" HAVE_READLINE_H)
+		check_include_files("stdio.h;history.h" HAVE_HISTORY_H)
+		check_include_files("stdio.h;readline/history.h" HAVE_READLINE_HISTORY_H)
+		check_include_files("stdio.h;readline/readline.h" HAVE_READLINE_READLINE_H)
+		check_include_files("stdio.h;editline/history.h" HAVE_EDITLINE_HISTORY_H)
+		check_include_files("stdio.h;editline/readline.h" HAVE_EDITLINE_READLINE_H)
+
+
+		set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${READLINE_LIBRARY})
+		if(CURSES_FOUND)
+			set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${CURSES_LIBRARIES})
+			set(READLINE_LIBRARY ${READLINE_LIBRARY} ${CURSES_LIBRARIES})
+		endif()
+		check_function_exists(rl_completion_append_character HAVE_RL_COMPLETION_APPEND_CHARACTER)
+		check_function_exists(rl_completion_matches HAVE_RL_COMPLETION_MATCHES)
+		check_function_exists(rl_filename_completion_function HAVE_RL_FILENAME_COMPLETION_FUNCTION)
+		check_function_exists(rl_reset_screen_size HAVE_RL_RESET_SCREEN_SIZE)
+		check_function_exists(append_history HAVE_APPEND_HISTORY)
+		check_function_exists(history_truncate_file HAVE_HISTORY_TRUNCATE_FILE)
+	endif(READLINE_FOUND)
+endif()
+
+if(READLINE_FOUND)
+	set(HAVE_LIBREADLINE TRUE)
+else()
+	set(READLINE_LIBRARY "")
+	set(HAVE_LIBREADLINE FALSE)
+endif()
+
+check_include_files("sys/ipc.h" HAVE_SYS_IPC_H)
+check_include_files("sys/sem.h" HAVE_SYS_SEM_H)
+check_include_files("sys/shm.h" HAVE_SYS_SHM_H)
+check_include_files("sys/ioctl.h" HAVE_SYS_IOCTL_H)
+
+# Check if _GNU_SOURCE is available.
+check_symbol_exists(__GNU_LIBRARY__ "features.h" _GNU_SOURCE)
+check_symbol_exists(F_FULLFSYNC "fcntl.h" HAVE_DECL_F_FULLFSYNC)
+
+include(ReplacePython)
+include(CheckCpuID)
+include(CheckSSE42)
+
+if(MSVC)
+	if(HAVE__GET_CPUID OR HAVE__CPUID)
+		set(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK 1)
+	endif()
+else()
+	if(HAVE_SSE42 AND HAVE_SSE42_INTRINSICS)
+		set(USE_SSE42_CRC32C 1)
+	elseif(HAVE_SSE42_INTRINSICS AND (HAVE__GET_CPUID OR HAVE__CPUID))
+		set(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK 1)
+	else()
+		set(USE_SLICING_BY_8_CRC32C 1)
+	endif()
+endif()
+
+include(FuncAcceptArgtypes)
+include(CheckTypeAlignment)
+check_type_alignment(double ALIGNOF_DOUBLE)
+check_type_alignment(int ALIGNOF_INT)
+check_type_alignment(long ALIGNOF_LONG)
+check_type_alignment("long long int" ALIGNOF_LONG_LONG_INT)
+check_type_alignment(short ALIGNOF_SHORT)
+
+check_type_size(int64 HAVE_INT64)
+check_type_size(uint64 HAVE_UINT64)
+check_type_size(int8 HAVE_INT8)
+check_type_size(uint8 HAVE_UINT8)
+check_type_size("void *" VOID_POINTER_SIZE)
+math(EXPR VOID_POINTER_SIZE_BIT "${VOID_POINTER_SIZE}*8")
+check_type_size("long int" LONG_INT_SIZE)
+check_type_size("long long int" HAVE_LONG_LONG_INT)
+check_type_size("long" SIZEOF_LONG)
+check_type_size("size_t" SIZEOF_SIZE_T)
+check_type_size(__int128 PG_INT128_TYPE)
+if(PG_INT128_TYPE AND NOT WIN32)
+	set(HAVE_INT128 1)
+	set(PG_INT128_TYPE __int128)
+endif()
+
+check_include_files("locale.h" HAVE_LOCALE_H)
+check_include_files("xlocale.h" HAVE_XLOCALE_H)
+if(HAVE_LOCALE_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};locale.h")
+endif()
+check_type_size("locale_t" HAVE_LOCALE_T)
+if(NOT HAVE_LOCALE_T AND HAVE_XLOCALE_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};xlocale.h")
+	check_type_size("locale_t" HAVE_LOCALE_T)
+	if(HAVE_LOCALE_T)
+		set(LOCALE_T_IN_XLOCALE 1)
+	else()
+		set(LOCALE_T_IN_XLOCALE 0)
+	endif()
+else()
+	set(LOCALE_T_IN_XLOCALE 0)
+endif()
+
+check_type_size("wcstombs_l" HAVE_WCSTOMBS_L)
+#check_function_exists(wcstombs_l HAVE_WCSTOMBS_L)
+if(NOT HAVE_WCSTOMBS_L AND HAVE_XLOCALE_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_EXTRA_INCLUDE_FILES};xlocale.h")
+	check_type_size("wcstombs_l" HAVE_WCSTOMBS_L)
+	if(HAVE_WCSTOMBS_L)
+		set(WCSTOMBS_L_IN_XLOCALE 1)
+	else()
+		set(WCSTOMBS_L_IN_XLOCALE 0)
+	endif()
+else()
+	set(WCSTOMBS_L_IN_XLOCALE 0)
+endif()
+
+if(LONG_INT_SIZE EQUAL 8)
+	set(PG_INT64_TYPE "long int")
+	set(HAVE_LONG_INT_64 ${LONG_INT_SIZE})
+else(LONG_INT_SIZE EQUAL 8)
+	if(HAVE_LONG_LONG_INT EQUAL 8)
+		set(PG_INT64_TYPE "long long int")
+		set(HAVE_LONG_LONG_INT_64 1)
+	else()
+		message(FATAL_ERROR "Cannot find a working 64-bit integer type.")
+	endif()
+endif(LONG_INT_SIZE EQUAL 8)
+
+message(STATUS "PG_INT64_TYPE: ${PG_INT64_TYPE} HAVE_LONG_INT_64: ${HAVE_LONG_INT_64}")
+# Compute maximum alignment of any basic type.
+# We assume long's alignment is at least as strong as char, short, or int;
+# but we must check long long (if it exists) and double.
+
+if(NOT MAXIMUM_ALIGNOF)
+	set(MAX_ALIGNOF ${ALIGNOF_LONG})
+	if(MAX_ALIGNOF LESS ALIGNOF_DOUBLE)
+		set(MAX_ALIGNOF ${ALIGNOF_DOUBLE})
+	endif(MAX_ALIGNOF LESS ALIGNOF_DOUBLE)
+	if(HAVE_LONG_LONG_INT_64 AND MAX_ALIGNOF LESS HAVE_LONG_LONG_INT_64)
+		set(MAX_ALIGNOF ${HAVE_LONG_LONG_INT_64})
+	endif(HAVE_LONG_LONG_INT_64 AND MAX_ALIGNOF LESS HAVE_LONG_LONG_INT_64)
+	if(MAX_ALIGNOF)
+		set(MAXIMUM_ALIGNOF ${MAX_ALIGNOF})
+	endif(MAX_ALIGNOF)
+endif(NOT MAXIMUM_ALIGNOF)
+message(STATUS "MAXIMUM_ALIGNOF ${MAXIMUM_ALIGNOF}")
+
+if(HAVE_LONG_LONG_INT_64)
+	if(NOT NEED_REPL_SNPRINTF)
+		include(CheckSnprintfLongLongIntModifier)
+		if(NOT LONG_LONG_INT_MODIFIER)
+			set(LONG_LONG_INT_MODIFIER "ll")
+			set(NEED_REPL_SNPRINTF ON)
+		endif(NOT LONG_LONG_INT_MODIFIER)
+	else(NOT NEED_REPL_SNPRINTF)
+		set(LONG_LONG_INT_MODIFIER "ll")
+	endif(NOT NEED_REPL_SNPRINTF)
+else(HAVE_LONG_LONG_INT_64)
+	set(LONG_LONG_INT_MODIFIER "l")
+endif(HAVE_LONG_LONG_INT_64)
+
+if(HAVE_LONG_LONG_INT_64)
+	message(STATUS "HAVE_LONG_LONG_INT_64 ${HAVE_LONG_LONG_INT_64}")
+endif()
+if(HAVE_LONG_LONG_INT_64)
+	include(CheckLLConstants)
+endif()
+
+#TODO: some strange here
+option(USE_FLOAT4_BYVAL "float4 values are passed by value" ON)
+if(USE_FLOAT4_BYVAL)
+	set(FLOAT4PASSBYVAL 1)
+else(USE_FLOAT4_BYVAL)
+	set(FLOAT4PASSBYVAL 0)
+endif(USE_FLOAT4_BYVAL)
+
+if(VOID_POINTER_SIZE EQUAL 8)
+	option(USE_FLOAT8_BYVAL "float8 values are passed by value" ON)
+else()
+	option(USE_FLOAT8_BYVAL "float8 values are passed by value" OFF)
+endif()
+if(USE_FLOAT8_BYVAL AND NOT (VOID_POINTER_SIZE EQUAL 8))
+	message(FATAL_ERROR "USE_FLOAT8_BYVAL is not supported on 32-bit platforms.")
+elseif(USE_FLOAT8_BYVAL AND VOID_POINTER_SIZE EQUAL 8)
+	set(FLOAT8PASSBYVAL 1)
+	set(USE_FLOAT8_BYVAL 1)
+else()
+	set(FLOAT8PASSBYVAL 0)
+	set(USE_FLOAT8_BYVAL 0)
+endif(USE_FLOAT8_BYVAL AND NOT (VOID_POINTER_SIZE EQUAL 8))
+
+if (_GNU_SOURCE)
+	add_definitions(-D_GNU_SOURCE)
+endif()
+if(Threads_FOUND)
+	set(ENABLE_THREAD_SAFETY 1)
+	set(THREADS_PREFER_PTHREAD_FLAG ON)
+	set(PTHREAD_CFLAGS "-D_REENTRANT -D_THREAD_SAFE -D_POSIX_PTHREAD_SEMANTICS")
+	if(THREADS_HAVE_PTHREAD_ARG)
+		set(PTHREAD_CFLAGS "${PTHREAD_CFLAGS} -pthread")
+	endif()
+	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PTHREAD_CFLAGS}")
+endif()
+
+set(PORT_DIR "${PROJECT_SOURCE_DIR}/src/port")
+set(PQ_BACKEND_DIR "${PROJECT_SOURCE_DIR}/src/backend/libpq")
+set(MB_UTILS_BACKEND_DIR "${PROJECT_SOURCE_DIR}/src/backend/utils/mb")
+
+
+set(fallback_SRCS "")
+
+if(NOT HAVE_DECL_STRLCPY)
+  set(fallback_SRCS ${fallback_SRCS} "${PORT_DIR}/strlcpy.c")
+endif(NOT HAVE_DECL_STRLCPY)
+
+if(NOT HAVE_DECL_STRLCAT)
+	set(fallback_SRCS ${fallback_SRCS} "${PORT_DIR}/strlcat.c")
+endif(NOT HAVE_DECL_STRLCAT)
+
+if(NOT HAVE_GETPEEREID)
+	set(fallback_SRCS ${fallback_SRCS} "${PORT_DIR}/getpeereid.c")
+endif(NOT HAVE_GETPEEREID)
+
+set(TABLE_BLOCKSIZE 8 CACHE STRING "set table block size in kB")
+
+if(TABLE_BLOCKSIZE EQUAL 1)
+	set(BLCKSZ 1024)
+elseif(TABLE_BLOCKSIZE EQUAL 2)
+	set(BLCKSZ 2048)
+elseif(TABLE_BLOCKSIZE EQUAL 4)
+	set(BLCKSZ 4096)
+elseif(TABLE_BLOCKSIZE EQUAL 8)
+	set(BLCKSZ 8192)
+elseif(TABLE_BLOCKSIZE EQUAL 16)
+	set(BLCKSZ 16384)
+elseif(TABLE_BLOCKSIZE EQUAL 32)
+	set(BLCKSZ 32768)
+else(TABLE_BLOCKSIZE EQUAL 1)
+	message(FATAL_ERROR "Invalid block size. Allowed values are 1,2,4,8,16,32.")
+endif(TABLE_BLOCKSIZE EQUAL 1)
+
+message(STATUS "BLCKSZ - ${BLCKSZ}")
+
+set(SEGSIZE 1 CACHE STRING "set table segment size in GB")
+math(EXPR RELSEG_SIZE "(1024 / ${TABLE_BLOCKSIZE}) * ${SEGSIZE} * 1024")
+
+set(WAL_BLOCKSIZE 8 CACHE STRING "set WAL block size in kB")
+
+if(WAL_BLOCKSIZE EQUAL 1)
+	set(XLOG_BLCKSZ 1024)
+elseif(WAL_BLOCKSIZE EQUAL 2)
+	set(XLOG_BLCKSZ 2048)
+elseif(WAL_BLOCKSIZE EQUAL 4)
+	set(XLOG_BLCKSZ 4096)
+elseif(WAL_BLOCKSIZE EQUAL 8)
+	set(XLOG_BLCKSZ 8192)
+elseif(WAL_BLOCKSIZE EQUAL 16)
+	set(XLOG_BLCKSZ 16384)
+elseif(WAL_BLOCKSIZE EQUAL 32)
+	set(XLOG_BLCKSZ 32768)
+elseif(WAL_BLOCKSIZE EQUAL 64)
+	set(XLOG_BLCKSZ 65536)
+else(WAL_BLOCKSIZE EQUAL 1)
+	message(FATAL_ERROR "Invalid WAL block size. Allowed values are 1,2,4,8,16,32,64.")
+endif(WAL_BLOCKSIZE EQUAL 1)
+
+message(STATUS "XLOG_BLCKSZ - ${XLOG_BLCKSZ}")
+
+set(WAL_SEGSIZE 16 CACHE STRING "set WAL segment size in MB")
+
+if (";1;2;4;8;16;32;64;" MATCHES ";${WAL_SEGSIZE};")
+	math(EXPR XLOG_SEG_SIZE "${WAL_SEGSIZE} * 1024 * 1024")
+else()
+	message(FATAL_ERROR "${WAL_SEGSIZE} Invalid WAL segment size. Allowed values are 1,2,4,8,16,32,64.")
+endif()
+
+message(STATUS "XLOG_SEG_SIZE - ${XLOG_SEG_SIZE}")
+
+option(HAVE_ATOMICS "Define to ON if you want to use atomics if available." ON)
+
+if(HAVE_ATOMICS)
+	check_c_source_compiles("
+		int main(void){
+			char lock = 0;
+			__sync_lock_test_and_set(&lock, 1);
+			__sync_lock_release(&lock);
+			return 0;
+		}
+	" HAVE_GCC__SYNC_CHAR_TAS)
+
+	check_c_source_compiles("
+		int main(void){
+			int lock = 0;
+			__sync_lock_test_and_set(&lock, 1);
+			__sync_lock_release(&lock);
+			return 0;
+		}
+	" HAVE_GCC__SYNC_INT32_TAS)
+
+	check_c_source_compiles("
+		int main(void){
+			int val = 0;
+			__sync_val_compare_and_swap(&val, 0, 37);
+			return 0;
+		}
+	" HAVE_GCC__SYNC_INT32_CAS)
+
+	check_c_source_compiles("
+		int main(void){
+			${PG_INT64_TYPE} lock = 0;
+			__sync_val_compare_and_swap(&lock, 0, (${PG_INT64_TYPE}) 37);
+			return 0;
+		}
+	" HAVE_GCC__SYNC_INT64_CAS)
+
+	check_c_source_compiles("
+		int main(void){
+			int val = 0;
+			int expect = 0;
+			__atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
+			return 0;
+		}
+	" HAVE_GCC__ATOMIC_INT32_CAS)
+
+	check_c_source_compiles("
+		int main(void){
+			${PG_INT64_TYPE} val = 0;
+			${PG_INT64_TYPE} expect = 0;
+			__atomic_compare_exchange_n(&val, &expect, 37, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED);
+			return 0;
+		}
+	" HAVE_GCC__ATOMIC_INT64_CAS)
+endif(HAVE_ATOMICS)
+
+check_c_source_runs("
+	int main(void){
+		static unsigned long int x = __builtin_bswap32(0xaabbccdd);
+		return 0;
+	}
+" HAVE__BUILTIN_BSWAP32)
+
+check_c_source_runs("
+	int main(void){
+		static unsigned long int x = __builtin_bswap64(0xaabbccddeeff0011);
+		return 0;
+	}
+" HAVE__BUILTIN_BSWAP64)
+
+check_c_source_runs("
+	int main(void){
+		static int x; static int y[__builtin_constant_p(x) ? x : 1];
+		return 0;
+	}
+" HAVE__BUILTIN_CONSTANT_P)
+
+check_c_source_runs("
+	int main(void){
+		int x; static int y[__builtin_types_compatible_p(__typeof__(x), int)];
+		return 0;
+	}
+" HAVE__BUILTIN_TYPES_COMPATIBLE_P)
+
+check_c_source_runs("
+	int main(void){
+		__builtin_unreachable();
+		return 0;
+	}
+" HAVE__BUILTIN_UNREACHABLE)
+
+check_c_source_runs("
+	int main(void){
+		{ _Static_assert(1, \"foo\"); }
+		return 0;
+	}
+" HAVE__STATIC_ASSERT)
+
+if(NOT PGPORT)
+	set(PGPORT 5432)
+endif(NOT PGPORT)
+
+include(CheckFlexibleArray)
+
+check_c_source_compiles("
+	#include <sys/time.h>
+	int main(void){
+		struct timeval *tp;
+		struct timezone *tzp;
+		gettimeofday(tp,tzp);
+		return 0;
+	}
+" GETTIMEOFDAY_2ARG)
+
+if(NOT GETTIMEOFDAY_2ARG)
+	set(GETTIMEOFDAY_1ARG 1)
+endif(NOT GETTIMEOFDAY_2ARG)
+
+check_c_source_compiles("
+	#include <time.h>
+	int main(void){
+		int res;
+	#ifndef __CYGWIN__
+		res = timezone / 60;
+	#else
+		res = _timezone / 60;
+	#endif
+		return 0;
+	}
+" HAVE_INT_TIMEZONE)
+
+check_c_source_compiles("
+	#include <stdio.h>
+	int main(void){
+		printf(\"%s\", __func__);
+		return 0;
+	}
+" HAVE_FUNCNAME__FUNC)
+
+check_c_source_compiles("
+	#include <stdio.h>
+	int main(void){
+		printf(\"%s\", __FUNCTION__);
+		return 0;
+	}
+" HAVE_FUNCNAME__FUNCTION)
+
+check_c_source_compiles("
+	#include <stdio.h>
+	int main(void){
+		#define debug(...) fprintf(stderr, __VA_ARGS__)
+		debug(\"%s\", \"blarg\");
+		return 0;
+	}
+" HAVE__VA_ARGS)
+
+check_struct_has_member("struct tm" tm_zone "sys/types.h;time.h" HAVE_TM_ZONE LANGUAGE C)
+check_struct_has_member("struct tm" tm_gmtoff "sys/types.h;time.h" HAVE_STRUCT_TM_TM_ZONE LANGUAGE C)
+set(CMAKE_EXTRA_INCLUDE_FILES "time.h")
+check_type_size("*tzname" HAVE_TZNAME)
+
+if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
+	set(CMAKE_REQUIRED_FLAGS "-Werror")
+endif()
+check_c_source_compiles("
+	extern int pgac_write(int ignore, const char *fmt,...) __attribute__((format(gnu_printf, 2, 3)));
+	int main(void){return 0;}
+" PG_PRINTF_ATTRIBUTE)
+
+if(PG_PRINTF_ATTRIBUTE)
+	set(PG_PRINTF_ATTRIBUTE gnu_printf)
+else(PG_PRINTF_ATTRIBUTE)
+	set(PG_PRINTF_ATTRIBUTE printf)
+endif(PG_PRINTF_ATTRIBUTE)
+
+if(NOT MEMSET_LOOP_LIMIT)
+	set(MEMSET_LOOP_LIMIT 1024)
+endif(NOT MEMSET_LOOP_LIMIT)
+
+
+if(WIN32 OR MINGW)
+	set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;winsock2.h;ws2tcpip.h")
+else()
+	set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;sys/socket.h;netdb.h")
+endif()
+
+check_type_size("struct addrinfo" HAVE_STRUCT_ADDRINFO)
+check_type_size("struct cmsgcred" HAVE_STRUCT_CMSGCRED)
+if(HAVE_STRUCT_ADDRINFO)
+	CHECK_STRUCT_HAS_MEMBER("struct addrinfo" sa_len "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_STRUCT_SOCKADDR_SA_LEN LANGUAGE C)
+	if(NOT (WIN32 OR MINGW))
+		set(HAVE_GETADDRINFO 1)
+	endif()
+endif(HAVE_STRUCT_ADDRINFO)
+
+check_type_size("struct sockaddr_storage" HAVE_STRUCT_SOCKADDR_STORAGE)
+if(HAVE_STRUCT_SOCKADDR_STORAGE)
+	CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" ss_family "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY LANGUAGE C)
+	CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" __ss_family "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY LANGUAGE C)
+	CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" ss_len "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN LANGUAGE C)
+	CHECK_STRUCT_HAS_MEMBER("struct sockaddr_storage" __ss_len "${CMAKE_EXTRA_INCLUDE_FILES}" HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN LANGUAGE C)
+endif(HAVE_STRUCT_SOCKADDR_STORAGE)
+
+# If `struct sockaddr_un' exists, define HAVE_UNIX_SOCKETS.
+if(HAVE_SYS_UN_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;sys/un.h")
+else(HAVE_SYS_UN_H)
+	set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h")
+endif(HAVE_SYS_UN_H)
+check_type_size("struct sockaddr_un" HAVE_UNIX_SOCKETS)
+
+if(WIN32)
+	set(HAVE_IPV6 1)
+else()
+	set(CMAKE_EXTRA_INCLUDE_FILES "netinet/in.h")
+	check_type_size("struct sockaddr_in6" HAVE_IPV6)
+endif()
+
+set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h;sys/ipc.h;sys/sem.h")
+check_type_size("union semun" HAVE_UNION_SEMUN)
+
+check_include_file("sys/stat.h" HAVE_SYS_STAT_H)
+
+check_symbol_exists(fdatasync "unistd.h" HAVE_FDATASYNC)
+
+if(WIN32 OR MINGW)
+	set(USE_WIN32_SEMAPHORES 1)
+	set(SEMA_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/win32_sema.c")
+else(WIN32 OR MINGW)
+	if(USE_NAMED_POSIX_SEMAPHORES)
+		set(USE_NAMED_POSIX_SEMAPHORES 1)
+		set(SEMA_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/posix_sema.c")
+	elseif(USE_UNNAMED_POSIX_SEMAPHORES)
+		set(USE_UNNAMED_POSIX_SEMAPHORES 1)
+		set(SEMA_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/posix_sema.c")
+	else(USE_NAMED_POSIX_SEMAPHORES)
+		set(USE_SYSV_SEMAPHORES 1)
+		set(SEMA_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/sysv_sema.c")
+	endif(USE_NAMED_POSIX_SEMAPHORES)
+endif(WIN32 OR MINGW)
+
+#Realy bad name for win32
+set(USE_SYSV_SHARED_MEMORY 1)
+if(WIN32 OR MINGW)
+	set(SHMEM_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/win32_shmem.c")
+else(WIN32 OR MINGW)
+	set(SHMEM_IMPLEMENTATION "${PROJECT_SOURCE_DIR}/src/backend/port/sysv_shmem.c")
+endif(WIN32 OR MINGW)
+
+option(HAVE_SPINLOCKS "Define to ON if you have spinlocks." ON)
+
+
+option(USE_INTEGER_DATETIMES "Define to ON if you want 64-bit integer timestamp and interval support." ON)
+
+if(WIN32)
+	option(HAVE_SYMLINK "Define to ON if you have the `symlink' function." ON)
+else(WIN32)
+	set(HAVE_SYMLINK 1)
+endif(WIN32)
+
+option(PG_KRB_SRVNAM "Define to the name of the default PostgreSQL service principal in Kerberos (GSSAPI)." "postgres")
+
+#TODO: Need test this
+if(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
+	if(CMAKE_SYSTEM_PROCESSOR MATCHES "sparc")
+		set(TAS sunstudio_sparc.s)
+	else(CMAKE_SYSTEM_PROCESSOR MATCHES "sparc")
+		set(TAS sunstudio_x86.s)
+	endif(CMAKE_SYSTEM_PROCESSOR MATCHES "sparc")
+elseif(CMAKE_C_COMPILER_ID STREQUAL "HP-UX")
+	set(TAS hpux_hppa.s)
+else(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
+	set(TAS dummy.s)
+endif(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
+
+if(WIN32 OR MINGW)
+	set(CMAKE_EXTRA_INCLUDE_FILES
+		${CMAKE_EXTRA_INCLUDE_FILES}
+		windows.h
+		string.h
+		dbghelp.h
+	)
+	set(CMAKE_REQUIRED_DEFINITIONS "WIN32_LEAN_AND_MEAN")
+	check_type_size(MINIDUMP_TYPE NAVE_MINIDUMP_TYPE)
+endif(WIN32 OR MINGW)
+
+set(WIN32_STACK_RLIMIT 4194304)
+if(WIN32)
+	add_definitions(-DWIN32_STACK_RLIMIT=${WIN32_STACK_RLIMIT})
+endif()
+
+if(NEED_REPL_SNPRINTF)
+	option(USE_REPL_SNPRINTF "Use replacement snprintf() functions." ON)
+endif()
+
+include(GenDef)
+#Not work correctly for postgres see bug: 
+#https://gitlab.kitware.com/cmake/cmake/issues/16161
+#set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
+
+include(RegressCheck)
+
+# Need add sco and unixware?
+if(WIN32 OR MINGW)
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/win32.h)
+elseif(APPLE)
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/darwin.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/linux.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/hpux.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/freebsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/openbsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/newtbsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/solaris.h)
+elseif(CYGWIN)
+	set(pgos_include_SRCS ${PROJECT_SOURCE_DIR}/src/include/port/cygwin.h)
+else(WIN32 OR MINGW)
+	message(WARNING "${CMAKE_SYSTEM_NAME}")
+endif(WIN32 OR MINGW)
+
+# Try make fake host tuple for regress and isolation tests
+if(MINGW AND VOID_POINTER_SIZE EQUAL 8)
+	set(HOST_TUPLE "x86_64-w64-mingw32")
+elseif(MINGW AND VOID_POINTER_SIZE EQUAL 4)
+	set(HOST_TUPLE "i686-pc-mingw32")
+elseif(CYGWIN)
+	set(HOST_TUPLE "i686-pc-cygwin")
+elseif(MSVC AND VOID_POINTER_SIZE EQUAL 4)
+	set(HOST_TUPLE "i686-pc-win32vc")
+else()
+	set(HOST_TUPLE ${CMAKE_HOST_SYSTEM})
+endif()
+
+set(WITH_UUID "OFF" CACHE STRING "type of uuid lib [bsd, e2fs, ossp]")
+if(WITH_UUID)
+	find_package(LibUUID)
+
+	if(WITH_UUID STREQUAL "bsd")
+		set(HAVE_UUID_BSD 1)
+		set(UUID_EXTRA_OBJS
+			${PROJECT_SOURCE_DIR}/contrib/pgcrypto/md5.c
+			${PROJECT_SOURCE_DIR}/contrib/pgcrypto/sha1.c
+		)
+	elseif(WITH_UUID STREQUAL "e2fs")
+		set(HAVE_UUID_E2FS 1)
+		set(UUID_EXTRA_OBJS
+			${PROJECT_SOURCE_DIR}/contrib/pgcrypto/md5.c
+			${PROJECT_SOURCE_DIR}/contrib/pgcrypto/sha1.c
+		)
+	elseif(WITH_UUID STREQUAL "ossp")
+		set(HAVE_UUID_OSSP 1)
+		set(UUID_EXTRA_OBJS "")
+	else()
+		message(WARNING "Not correct type of uuid lib:${WITH_UUID}")
+	endif()
+endif()
+
+file(GENERATE OUTPUT ${PROJECT_SOURCE_DIR}/src/include/pg_config_os.h
+	INPUT ${pgos_include_SRCS})
+
+if(MINGW OR MSVC)
+	include_directories("${PROJECT_SOURCE_DIR}/src/include/port/win32")
+endif()
+
+if(MSVC)
+	include_directories("${PROJECT_SOURCE_DIR}/src/include/port/win32_msvc")
+endif()
+
+configure_file(
+	"${PROJECT_SOURCE_DIR}/src/include/pg_config_cmake.in"
+	"${PROJECT_SOURCE_DIR}/src/include/pg_config.h"
+)
+
+configure_file(
+	"${PROJECT_SOURCE_DIR}/src/include/pg_config_ext_cmake.in"
+	"${PROJECT_SOURCE_DIR}/src/include/pg_config_ext.h"
+)
+
+configure_file(
+	"${PROJECT_SOURCE_DIR}/src/include/pg_config_paths_cmake.in"
+	"${PROJECT_SOURCE_DIR}/src/port/pg_config_paths.h"
+)
+
+configure_file(
+	"${PROJECT_SOURCE_DIR}/src/interfaces/ecpg/include/ecpg_config_cmake.in"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/ecpg/include/ecpg_config.h"
+)
+
+configure_file(
+	"${PROJECT_SOURCE_DIR}/PGXS.cmake.in"
+	"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PGXS.cmake"
+	@ONLY
+)
+
+install(FILES ${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/PGXS.cmake
+		DESTINATION ${LIBDIR}/cmake)
+
+
+option(CMAKE_USE_FOLDERS "Enable folder grouping of projects in IDEs." ON)
+mark_as_advanced(CMAKE_USE_FOLDERS)
+
+macro(CMAKE_SET_TARGET_FOLDER tgt folder)
+	if(CMAKE_USE_FOLDERS)
+		set_property(GLOBAL PROPERTY USE_FOLDERS ON)
+		if(MSVC AND TARGET ${tgt})
+			set_property(TARGET "${tgt}" PROPERTY FOLDER "${folder}")
+		endif()
+	else()
+		set_property(GLOBAL PROPERTY USE_FOLDERS OFF)
+	endif()
+endmacro()
+
+# Add sub-directories
+add_subdirectory(src)
+add_subdirectory(contrib)
+
+message(STATUS "------------------")
+message(STATUS "    Conclusion")
+message(STATUS "Host tuple: ${HOST_TUPLE}")
+message(STATUS "OpenSSL: ${OPENSSL_FOUND} version: ${OPENSSL_VERSION} libs: ${OPENSSL_LIBRARIES}")
+message(STATUS "Python: ${PYTHONINTERP_FOUND}")
+message(STATUS "PythonLibs: ${PYTHONLIBS_FOUND}")
+message(STATUS "PerlLibs: ${PERLLIBS_FOUND}")
+message(STATUS "LibXML2: ${LIBXML2_FOUND}")
+message(STATUS "LibXslt: ${LIBXSLT_FOUND}")
+message(STATUS "TCL: ${TCL_FOUND}")
+message(STATUS "PAM: ${PAM_LIB}")
+message(STATUS "------------------")
diff --git a/PGXS.cmake.in b/PGXS.cmake.in
new file mode 100644
index 0000000..01a1100
--- /dev/null
+++ b/PGXS.cmake.in
@@ -0,0 +1,48 @@
+cmake_minimum_required(VERSION 2.8)
+
+set(POSTGRES_MAJOR_VERSION "@POSTGRES_MAJOR_VERSION@")
+set(POSTGRES_MINOR_VERSION "@POSTGRES_MINOR_VERSION@")
+set(POSTGRES_PATCH_VERSION "@POSTGRES_MINOR_VERSION@")
+set(POSTGRES_VERSION "@POSTGRES_VERSION@")
+set(PG_VERSION "9.6devel")
+set(PG_VERSION_NUM 90600)
+
+set(PGBINDIR "@PGBINDIR@")
+set(PGSHAREDIR "@PGSHAREDIR@")
+set(SYSCONFDIR "@SYSCONFDIR@")
+set(INCLUDEDIR "@INCLUDEDIR@")
+set(PKGINCLUDEDIR "@PKGINCLUDEDIR@")
+set(INCLUDEDIRSERVER "@INCLUDEDIRSERVER@")
+set(LIBDIR "@LIBDIR@")
+set(PKGLIBDIR "@PKGLIBDIR@")
+set(LOCALEDIR "@LOCALEDIR@")
+set(DOCDIR "@DOCDIR@")
+set(HTMLDIR "@HTMLDIR@")
+set(MANDIR "@MANDIR@")
+
+
+set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
+set(CMAKE_MACOSX_RPATH 1)
+
+set(CMAKE_CXX_FLAGS "@CMAKE_CXX_FLAGS@")
+set(CMAKE_C_FLAGS "@CMAKE_C_FLAGS@")
+set(CMAKE_SHARED_LINKER_FLAGS "@CMAKE_SHARED_LINKER_FLAGS@")
+set(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG "@CMAKE_SHARED_LIBRARY_SONAME_C_FLAG@")
+set(CMAKE_SHARED_LINKER_FLAGS "@CMAKE_SHARED_LINKER_FLAGS@")
+
+include_directories(${INCLUDEDIRSERVER})
+link_directories(${LIBDIR})
+
+if(WIN32)
+	set(contrib_libs
+		port
+		pgcommon
+	)
+endif()
+
+if(MSVC)
+	set(contrib_libs
+		${contrib_libs}
+		postgres
+	)
+endif()
diff --git a/cmake/CheckCpuID.cmake b/cmake/CheckCpuID.cmake
new file mode 100644
index 0000000..3346302
--- /dev/null
+++ b/cmake/CheckCpuID.cmake
@@ -0,0 +1,17 @@
+CHECK_C_SOURCE_COMPILES("
+	#include <cpuid.h>
+	int main() {
+		unsigned int exx[4] = {0, 0, 0, 0};
+		__get_cpuid(1, &exx[0], &exx[1], &exx[2], &exx[3]);
+		return 0;
+	}
+" HAVE__GET_CPUID)
+
+CHECK_C_SOURCE_COMPILES("
+	#include <intrin.h>
+	int main() {
+		unsigned int exx[4] = {0, 0, 0, 0};
+		__cpuid(exx[0], 1);
+		return 0;
+	}
+" HAVE__CPUID)
diff --git a/cmake/CheckFlexibleArray.cmake b/cmake/CheckFlexibleArray.cmake
new file mode 100644
index 0000000..ce57aa4
--- /dev/null
+++ b/cmake/CheckFlexibleArray.cmake
@@ -0,0 +1,37 @@
+set(C99_C_FLAG_CANDIDATES
+   " "
+   "-std=c99"
+   "-std=gnu99"
+   "-c99"
+   "-AC99"
+   "-xc99=all"
+   "-qlanglvl=extc99"
+)
+
+message(STATUS "Check flexible array support")
+if(NOT MSVC)
+	foreach(FLAG ${C99_C_FLAG_CANDIDATES})
+		set(CMAKE_REQUIRED_FLAGS "${FLAG}")
+		unset(FLEXIBLE_ARRAY_MEMBER CACHE)
+		CHECK_C_SOURCE_COMPILES("
+			int main(void){
+				int x = 10;
+				int y[x];
+				return 0;
+			}
+		" FLEXIBLE_ARRAY_MEMBER)
+		if(FLEXIBLE_ARRAY_MEMBER)
+			set(C99_C_FLAGS_INTERNAL "${FLAG}")
+			set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${FLAG}")
+			break()
+		endif(FLEXIBLE_ARRAY_MEMBER)
+	endforeach(FLAG ${C99_C_FLAG_CANDIDATES})
+endif()
+
+if(FLEXIBLE_ARRAY_MEMBER)
+	message(STATUS "Check flexible array support - yes with ${C99_C_FLAGS_INTERNAL}")
+	set(FLEXIBLE_ARRAY_MEMBER "")
+else(FLEXIBLE_ARRAY_MEMBER)
+	message(STATUS "Check flexible array support - no")
+	set(FLEXIBLE_ARRAY_MEMBER 1)
+endif(FLEXIBLE_ARRAY_MEMBER)
\ No newline at end of file
diff --git a/cmake/CheckLLConstants.cmake b/cmake/CheckLLConstants.cmake
new file mode 100644
index 0000000..68bf65f
--- /dev/null
+++ b/cmake/CheckLLConstants.cmake
@@ -0,0 +1,9 @@
+CHECK_C_SOURCE_COMPILES("
+	main() {
+		#define INT64CONST(x)  x##LL
+		long long int foo = INT64CONST(0x1234567890123456);
+		return 0;
+	}
+" HAVE_LL_CONSTANTS)
+
+message(STATUS "HAVE_LL_CONSTANTS ${HAVE_LL_CONSTANTS}")
\ No newline at end of file
diff --git a/cmake/CheckSSE42.cmake b/cmake/CheckSSE42.cmake
new file mode 100644
index 0000000..3fee3b4
--- /dev/null
+++ b/cmake/CheckSSE42.cmake
@@ -0,0 +1,30 @@
+foreach(FLAG ";-msse4.2")
+	unset(HAVE_SSE42_INTRINSICS CACHE)
+	set(CMAKE_REQUIRED_FLAGS ${FLAG})
+	check_c_source_runs("
+		#include <nmmintrin.h>
+		int main(void) {
+			unsigned int crc = 0;
+			crc = _mm_crc32_u8(crc, 0);
+			crc = _mm_crc32_u32(crc, 0);
+			/* return computed value, to prevent the above being optimized away */
+			return !(crc == 0);
+		}
+	" HAVE_SSE42_INTRINSICS)
+	if (HAVE_SSE42_INTRINSICS)
+		set(CFLAGS_SSE42 ${FLAG})
+		break()
+	endif()
+endforeach()
+
+message(STATUS "SSE4.2 flags: ${CFLAGS_SSE42}")
+set(CMAKE_REQUIRED_FLAGS ${CFLAGS_SSE42})
+check_c_source_compiles("
+	#ifndef __SSE4_2__
+	#error __SSE4_2__ not defined
+	#endif
+	int main(void){
+		return 0;
+	}
+" HAVE_SSE42)
+set(CMAKE_REQUIRED_FLAGS "")
diff --git a/cmake/CheckSnprintfLongLongIntModifier.cmake b/cmake/CheckSnprintfLongLongIntModifier.cmake
new file mode 100644
index 0000000..f076238
--- /dev/null
+++ b/cmake/CheckSnprintfLongLongIntModifier.cmake
@@ -0,0 +1,31 @@
+set(CMAKE_REQUIRED_INCLUDES "stdio.h")
+foreach(pgac_modifier "ll" "q" "I64")
+	unset(LONG_LONG_INT_MODIFIER CACHE)
+	CHECK_C_SOURCE_COMPILES("
+		typedef long long int ac_int64;
+
+		ac_int64 a = 20000001;
+		ac_int64 b = 40000005;
+
+		int does_int64_snprintf_work() {
+			ac_int64 c;
+			char buf[100];
+			if (sizeof(ac_int64) != 8)
+				return 0;	/* doesn't look like the right size */
+			
+			c = a * b;
+			snprintf(buf, 100, \"%${pgac_modifier}d\", c);
+			if (strcmp(buf, \"800000140000005\") != 0)
+				return 0;	/* either multiply or snprintf is busted */
+			return 1;
+		}
+		main() {
+			exit(! does_int64_snprintf_work());
+		}
+	" LONG_LONG_INT_MODIFIER)
+	if(LONG_LONG_INT_MODIFIER)
+		set(LONG_LONG_INT_MODIFIER ${pgac_modifier})
+		break()
+	endif(LONG_LONG_INT_MODIFIER)
+endforeach(pgac_modifier)
+message(STATUS "LONG_LONG_INT_MODIFIER ${LONG_LONG_INT_MODIFIER}")
\ No newline at end of file
diff --git a/cmake/CheckTypeAlignment.cmake b/cmake/CheckTypeAlignment.cmake
new file mode 100644
index 0000000..37cbb58
--- /dev/null
+++ b/cmake/CheckTypeAlignment.cmake
@@ -0,0 +1,29 @@
+macro(CHECK_TYPE_ALIGNMENT TYPE NAME)
+	if(NOT ${NAME})
+		message(STATUS "Check alignment of ${TYPE}")
+		
+		set(INCLUDE_HEADERS "#include <stddef.h>
+			#include <stdio.h>
+			#include <stdlib.h>")
+
+		if(HAVE_STDINT_H)
+			set(INCLUDE_HEADERS "${INCLUDE_HEADERS}\n#include <stdint.h>\n")
+		endif(HAVE_STDINT_H)
+
+		file(WRITE "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/check_${NAME}_alignment.c"
+			"${INCLUDE_HEADERS}
+			int main(){
+				char diff;
+				struct foo {char a; ${TYPE} b;};
+				struct foo *p = (struct foo *) malloc(sizeof(struct foo));
+				diff = ((char *)&p->b) - ((char *)&p->a);
+				return diff;}
+		")
+
+		try_run(${NAME} COMPILE_RESULT "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/"
+			"${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/check_${NAME}_alignment.c")
+
+		message(STATUS "Check alignment of ${TYPE} - ${${NAME}}")
+
+	endif(NOT ${NAME})
+endmacro(CHECK_TYPE_ALIGNMENT TYPE NAME ALIGNOF_TYPE)
\ No newline at end of file
diff --git a/cmake/FindLDAP.cmake b/cmake/FindLDAP.cmake
new file mode 100644
index 0000000..8a5bc63
--- /dev/null
+++ b/cmake/FindLDAP.cmake
@@ -0,0 +1,41 @@
+# - Try to find the LDAP client libraries
+# Once done this will define
+#
+#  LDAP_FOUND - system has libldap
+#  LDAP_INCLUDE_DIR - the ldap include directory
+#  LDAP_LIBRARIES - libldap + liblber (if found) library
+#  LBER_LIBRARIES - liblber library
+
+if(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
+	# Already in cache, be silent
+	set(Ldap_FIND_QUIETLY TRUE)
+endif(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
+
+if(UNIX)
+	FIND_PATH(LDAP_INCLUDE_DIR ldap.h)
+	FIND_LIBRARY(LDAP_LIBRARIES NAMES ldap)
+	FIND_LIBRARY(LBER_LIBRARIES NAMES lber)
+
+else(UNIX)
+	FIND_PATH(LDAP_INCLUDE_DIR winldap.h)
+	FIND_LIBRARY(LDAP_LIBRARIES NAMES wldap32)
+endif(UNIX)
+
+if(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
+	set(LDAP_FOUND TRUE)
+	if(LBER_LIBRARIES)
+		set(LDAP_LIBRARIES ${LDAP_LIBRARIES} ${LBER_LIBRARIES})
+	endif(LBER_LIBRARIES)
+endif(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
+
+if(LDAP_FOUND)
+	if(NOT Ldap_FIND_QUIETLY)
+		message(STATUS "Found ldap: ${LDAP_LIBRARIES}")
+	endif(NOT Ldap_FIND_QUIETLY)
+else(LDAP_FOUND)
+	if (Ldap_FIND_REQUIRED)
+		message(FATAL_ERROR "Could NOT find ldap")
+	endif (Ldap_FIND_REQUIRED)
+endif(LDAP_FOUND)
+
+MARK_AS_ADVANCED(LDAP_INCLUDE_DIR LDAP_LIBRARIES LBER_LIBRARIES)
diff --git a/cmake/FindLibSocket.cmake b/cmake/FindLibSocket.cmake
new file mode 100644
index 0000000..148f4ec
--- /dev/null
+++ b/cmake/FindLibSocket.cmake
@@ -0,0 +1,11 @@
+find_library( LIBSOCKET_LIBRARY NAMES libsocket socket )
+mark_as_advanced( LIBSOCKET_LIBRARY )
+
+# handle the QUIETLY and REQUIRED arguments and set LIBSOCKET_FOUND to TRUE if
+# all listed variables are TRUE
+include( FindPackageHandleStandardArgs )
+FIND_PACKAGE_HANDLE_STANDARD_ARGS( LIBSOCKET DEFAULT_MSG LIBSOCKET_LIBRARY )
+
+if( LIBSOCKET_FOUND )
+	set( LIBSOCKET_LIBRARIES ${LIBSOCKET_LIBRARY} )
+endif( LIBSOCKET_FOUND )
diff --git a/cmake/FindLibUUID.cmake b/cmake/FindLibUUID.cmake
new file mode 100644
index 0000000..10626c5
--- /dev/null
+++ b/cmake/FindLibUUID.cmake
@@ -0,0 +1,50 @@
+# - Try to find LIBUUID
+# Find LIBUUID headers, libraries and the answer to all questions.
+#
+#  LIBUUID_FOUND               True if libuuid got found
+#  LIBUUID_INCLUDE_DIRS        Location of libuuid headers
+#  LIBUUID_LIBRARIES           List of libraries to use libuuid
+#
+# Copyright (c) 2008 Bjoern Ricks <bjoern.ricks@googlemail.com>
+#
+#  Redistribution and use is allowed according to the terms of the New
+#  BSD license.
+#  For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+#
+
+INCLUDE(FindPkgConfig)
+
+IF(LibUUID_FIND_REQUIRED)
+	SET(_pkgconfig_REQUIRED "REQUIRED")
+ELSE(LibUUID_FIND_REQUIRED)
+	SET(_pkgconfig_REQUIRED "")
+ENDIF (LibUUID_FIND_REQUIRED)
+
+IF(LIBUUID_MIN_VERSION)
+	PKG_SEARCH_MODULE(LIBUUID ${_pkgconfig_REQUIRED} uuid>=${LIBUUID_MIN_VERSION})
+ELSE(LIBUUID_MIN_VERSION)
+	PKG_SEARCH_MODULE(LIBUUID ${_pkgconfig_REQUIRED} uuid)
+ENDIF(LIBUUID_MIN_VERSION)
+
+IF(NOT LIBUUID_FOUND)
+	FIND_PATH(LIBUUID_INCLUDE_DIRS uuid/uuid.h)
+	FIND_LIBRARY(LIBUUID_LIBRARIES uuid)
+
+	# Report results
+	IF(LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
+		SET(LIBUUID_FOUND 1)
+		IF(NOT LIBUUID_FIND_QUIETLY)
+			MESSAGE(STATUS "Found libuuid: ${LIBUUID_LIBRARIES}")
+		ENDIF(NOT LIBUUID_FIND_QUIETLY)
+	ELSE(LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
+		IF(LIBUUID_FIND_REQUIRED)
+			MESSAGE(SEND_ERROR "Could NOT find libuuid")
+		ELSE(LIBUUID_FIND_REQUIRED)
+			IF(NOT LIBUUID_FIND_QUIETLY)
+				MESSAGE(STATUS "Could NOT find libuuid")
+			ENDIF(NOT LIBUUID_FIND_QUIETLY)
+		ENDIF (LIBUUID_FIND_REQUIRED)
+	ENDIF (LIBUUID_LIBRARIES AND LIBUUID_INCLUDE_DIRS)
+ENDIF(NOT LIBUUID_FOUND)
+
+MARK_AS_ADVANCED(LIBUUID_LIBRARIES LIBUUID_INCLUDE_DIRS)
diff --git a/cmake/FindReadline.cmake b/cmake/FindReadline.cmake
new file mode 100644
index 0000000..6603dbe
--- /dev/null
+++ b/cmake/FindReadline.cmake
@@ -0,0 +1,22 @@
+# from http://websvn.kde.org/trunk/KDE/kdeedu/cmake/modules/FindReadline.cmake
+# http://websvn.kde.org/trunk/KDE/kdeedu/cmake/modules/COPYING-CMAKE-SCRIPTS
+# --> BSD licensed
+#
+# GNU Readline library finder
+
+
+if(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
+	set(READLINE_FOUND TRUE)
+else(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
+	FIND_PATH(READLINE_INCLUDE_DIR readline/readline.h
+		/usr/include/readline
+	)
+
+	FIND_LIBRARY(READLINE_LIBRARY NAMES readline PATHS
+		/opt/csw/lib/64/
+	)
+	include(FindPackageHandleStandardArgs)
+	FIND_PACKAGE_HANDLE_STANDARD_ARGS(Readline DEFAULT_MSG READLINE_INCLUDE_DIR READLINE_LIBRARY )
+
+	MARK_AS_ADVANCED(READLINE_INCLUDE_DIR READLINE_LIBRARY)
+endif(READLINE_INCLUDE_DIR AND READLINE_LIBRARY)
diff --git a/cmake/FindSELinux.cmake b/cmake/FindSELinux.cmake
new file mode 100644
index 0000000..dd6e3ca
--- /dev/null
+++ b/cmake/FindSELinux.cmake
@@ -0,0 +1,55 @@
+# - Try to find the SELinux library
+# Once done this will define
+#
+#  SELINUX_FOUND - System has selinux
+#  SELINUX_INCLUDE_DIR - The selinux include directory
+#  SELINUX_LIBRARIES - The libraries needed to use selinux
+#  SELINUX_DEFINITIONS - Compiler switches required for using selinux
+
+#=============================================================================
+# Copyright 2010  Michael Leupold <lemma@confuego.org>
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+
+IF (SELINUX_INCLUDE_DIR AND SELINUX_LIBRARIES)
+	# in cache already
+	SET (SELinux_FIND_QUIETLY TRUE)
+ENDIF (SELINUX_INCLUDE_DIR AND SELINUX_LIBRARIES)
+
+# TODO: This should be restricted to Linux only
+IF (NOT WIN32)
+	# use pkg-config to get the directories
+	FIND_PACKAGE (PkgConfig)
+	PKG_CHECK_MODULES (PC_SELINUX libselinux)
+	SET (SELINUX_DEFINITIONS ${PC_SELINUX_CFLAGS_OTHER})
+
+	FIND_PATH (SELINUX_INCLUDE_DIR selinux/selinux.h
+		HINTS
+		${PC_SELINUX_INCLUDEDIR}
+		${PC_SELINUX_INCLUDE_DIRS}
+	)
+
+	FIND_LIBRARY (SELINUX_LIBRARIES selinux libselinux
+		HINTS
+		${PC_SELINUX_LIBDIR}
+		${PC_SELINUX_LIBRARY_DIRS}
+	)
+
+	INCLUDE (FindPackageHandleStandardArgs)
+
+	# handle the QUIETLY and REQUIRED arguments and set SELINUX_FOUND
+	# to TRUE if all listed variables are TRUE
+	FIND_PACKAGE_HANDLE_STANDARD_ARGS (SELinux DEFAULT_MSG
+		SELINUX_INCLUDE_DIR
+		SELINUX_LIBRARIES
+	)
+
+	MARK_AS_ADVANCED (SELINUX_INCLUDE_DIR SELINUX_LIBRARIES)
+
+ENDIF (NOT WIN32)
diff --git a/cmake/FuncAcceptArgtypes.cmake b/cmake/FuncAcceptArgtypes.cmake
new file mode 100644
index 0000000..e333d69
--- /dev/null
+++ b/cmake/FuncAcceptArgtypes.cmake
@@ -0,0 +1,52 @@
+if(HAVE_SYS_TYPES_H)
+	set(INCLUDE_SYS_TYPES_H "#include <sys/types.h>")
+endif(HAVE_SYS_TYPES_H)
+
+if(HAVE_SYS_SOCKET_H)
+	set(INCLUDE_SYS_SOCKET_H "#include <sys/socket.h>")
+endif(HAVE_SYS_SOCKET_H)
+
+
+message(STATUS "Looking for accept function args")
+set(CMAKE_REQUIRED_QUIET 1)
+foreach(ac_cv_func_accept_return "int" "unsigned int PASCAL" "SOCKET WSAAPI")
+	foreach(ac_cv_func_accept_arg1 "unsigned int" "int" "SOCKET")
+		foreach(ac_cv_func_accept_arg2 "struct sockaddr *" "const struct sockaddr *" "void *")
+			foreach(ac_cv_func_accept_arg3 "int" "size_t" "socklen_t" "unsigned int" "void")
+				unset(AC_FUNC_ACCEPT CACHE)
+				CHECK_C_SOURCE_COMPILES("
+					${INCLUDE_SYS_TYPES_H}
+					${INCLUDE_SYS_SOCKET_H}
+					extern ${ac_cv_func_accept_return} accept (${ac_cv_func_accept_arg1}, ${ac_cv_func_accept_arg2}, ${ac_cv_func_accept_arg3} *);
+					int main(void){return 0;}
+				" AC_FUNC_ACCEPT)
+				if(AC_FUNC_ACCEPT)
+					set(ACCEPT_TYPE_RETURN ${ac_cv_func_accept_return})
+					set(ACCEPT_TYPE_ARG1 ${ac_cv_func_accept_arg1})
+					set(ACCEPT_TYPE_ARG2 ${ac_cv_func_accept_arg2})
+					set(ACCEPT_TYPE_ARG3 ${ac_cv_func_accept_arg3})
+					break()
+				endif(AC_FUNC_ACCEPT)
+			endforeach(ac_cv_func_accept_arg3)
+			if(AC_FUNC_ACCEPT)
+				break()
+			endif(AC_FUNC_ACCEPT)
+		endforeach(ac_cv_func_accept_arg2)
+		if(AC_FUNC_ACCEPT)
+			break()
+		endif(AC_FUNC_ACCEPT)
+	endforeach(ac_cv_func_accept_arg1)
+	if(AC_FUNC_ACCEPT)
+		break()
+	endif(AC_FUNC_ACCEPT)
+endforeach(ac_cv_func_accept_return)
+unset(CMAKE_REQUIRED_QUIET)
+
+if(NOT AC_FUNC_ACCEPT)
+	message(ERROR "could not determine argument types")
+endif(NOT AC_FUNC_ACCEPT)
+if(ACCEPT_TYPE_ARG3 STREQUAL "void")
+	set(ACCEPT_TYPE_ARG3 "int")
+endif()
+
+message(STATUS "Looking for accept function args - found ${ACCEPT_TYPE_RETURN}, ${ACCEPT_TYPE_ARG1}, ${ACCEPT_TYPE_ARG2}, ${ACCEPT_TYPE_ARG3} *")
diff --git a/cmake/GenDef.cmake b/cmake/GenDef.cmake
new file mode 100644
index 0000000..499b0bd
--- /dev/null
+++ b/cmake/GenDef.cmake
@@ -0,0 +1,12 @@
+macro(GEN_DEF NAME)
+	SET_TARGET_PROPERTIES(${NAME} PROPERTIES LINK_FLAGS "/DEF:\"${CMAKE_CURRENT_BINARY_DIR}/${NAME}.def\" ")
+	add_custom_command(
+		TARGET ${NAME}
+		PRE_LINK
+		#COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.def
+		#OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.def
+		COMMAND ${PERL_EXECUTABLE} ${PROJECT_SOURCE_DIR}/src/tools/msvc/gendef.pl ${CMAKE_CURRENT_BINARY_DIR}/$<$<BOOL:${CMAKE_BUILD_TYPE}>:${CMAKE_FILES_DIRECTORY}>${NAME}.dir/${CMAKE_CFG_INTDIR} ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.def ${CMAKE_VS_PLATFORM_NAME}
+		COMMENT " Gen defs "
+	)
+	set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${CMAKE_CURRENT_BINARY_DIR}/${NAME}.def)
+endmacro(GEN_DEF NAME)
diff --git a/cmake/MakeNLS.cmake b/cmake/MakeNLS.cmake
new file mode 100644
index 0000000..c6db90d
--- /dev/null
+++ b/cmake/MakeNLS.cmake
@@ -0,0 +1,21 @@
+macro(MAKE_MO BIN_NAME LANGUAGES)
+	set(gmo_files "")
+	foreach(lang ${LANGUAGES})
+		if (NOT ";${NLS_LANGUAGES};" MATCHES ";${lang};")
+			continue()
+		endif()
+		set(_gmoFile ${CMAKE_CURRENT_BINARY_DIR}/${lang}.gmo)
+		add_custom_command(OUTPUT ${_gmoFile}
+			COMMAND ${GETTEXT_MSGFMT_EXECUTABLE} -o ${_gmoFile} po/${lang}.po
+			WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
+			DEPENDS po/${lang}.po
+		)
+		install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${lang}.gmo DESTINATION share/locale/${lang}/LC_MESSAGES/ RENAME ${BIN_NAME}-${POSTGRES_MAJOR_VERSION}.${POSTGRES_MINOR_VERSION}.mo)
+		if(NOT TARGET pofiles)
+			add_custom_target(pofiles)
+		endif()
+		add_custom_target(${BIN_NAME}-mofile-${lang} ALL DEPENDS ${_gmoFile})
+		add_dependencies(pofiles ${BIN_NAME}-mofile-${lang})
+		set(gmo_files "${gmo_files};${_gmoFile}")
+	endforeach()
+endmacro()
diff --git a/cmake/RegressCheck.cmake b/cmake/RegressCheck.cmake
new file mode 100644
index 0000000..1770597
--- /dev/null
+++ b/cmake/RegressCheck.cmake
@@ -0,0 +1,168 @@
+set(tmp_check_folder ${CMAKE_BINARY_DIR}/src/test/regress/tmp_install)
+
+if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
+	set(env_cmd
+		${CMAKE_COMMAND} -E env
+		"DYLD_LIBRARY_PATH=$ENV{DESTDIR}${LIBDIR}:$$DYLD_LIBRARY_PATH"
+	)
+	set(tmp_env_cmd
+		${CMAKE_COMMAND} -E env
+		"DYLD_LIBRARY_PATH=${tmp_check_folder}${LIBDIR}:$$DYLD_LIBRARY_PATH"
+	)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "AIX")
+	set(env_cmd
+		${CMAKE_COMMAND} -E env
+		"LIBPATH=$ENV{DESTDIR}${LIBDIR}:$LIBPATH"
+	)
+	set(tmp_env_cmd
+		${CMAKE_COMMAND} -E env
+		"LIBPATH=${tmp_check_folder}${LIBDIR}:$LIBPATH"
+	)
+else()
+	if (CMAKE_VERSION VERSION_GREATER "3.2.0")
+		set(env_cmd
+			${CMAKE_COMMAND} -E env
+			"LD_LIBRARY_PATH=$ENV{DESTDIR}${LIBDIR}:$$LD_LIBRARY_PATH"
+		)
+		set(tmp_env_cmd
+			${CMAKE_COMMAND} -E env
+			"LD_LIBRARY_PATH=${tmp_check_folder}${LIBDIR}:$$LD_LIBRARY_PATH"
+		)
+	else()
+		set(env_cmd
+			export "LD_LIBRARY_PATH=$ENV{DESTDIR}${LIBDIR}:$$LD_LIBRARY_PATH" &&
+		)
+		set(tmp_env_cmd
+			export "LD_LIBRARY_PATH=${tmp_check_folder}${LIBDIR}:$$LD_LIBRARY_PATH" &&
+		)
+	endif()
+endif()
+
+if(MSVC OR MSYS OR MINGW OR CMAKE_GENERATOR STREQUAL Xcode)
+	#Need rewrite
+	set(pre_pg_regress_check
+		${PGBINDIR}/pg_regress${CMAKE_EXECUTABLE_SUFFIX}
+		--inputdir="${CMAKE_SOURCE_DIR}/src/test/regress"
+		--temp-instance="tmp_check"
+		--encoding=UTF8
+		--no-locale
+	)
+	set(pre_pg_isolation_regress_check
+		${PGBINDIR}/pg_isolation_regress${CMAKE_EXECUTABLE_SUFFIX}
+		--inputdir="${CMAKE_SOURCE_DIR}/src/test/isolation"
+		--temp-instance="tmp_check"
+		--encoding=UTF8
+		--no-locale
+	)
+else(MSVC)
+	set(pre_pg_regress_check
+		${CMAKE_BINARY_DIR}/src/test/regress/${CMAKE_INSTALL_CONFIG_NAME}/pg_regress${CMAKE_EXECUTABLE_SUFFIX}
+		--inputdir="${CMAKE_SOURCE_DIR}/src/test/regress"
+		--temp-instance="tmp_check"
+		--encoding=UTF8
+		--no-locale
+	)
+	set(pre_pg_isolation_regress_check
+		${CMAKE_BINARY_DIR}/src/test/isolation/${CMAKE_INSTALL_CONFIG_NAME}/pg_isolation_regress${CMAKE_EXECUTABLE_SUFFIX}
+		--inputdir="${CMAKE_SOURCE_DIR}/src/test/isolation"
+		--temp-instance="tmp_check"
+		--encoding=UTF8
+		--no-locale
+	)
+endif()
+
+set(pg_regress_check
+	${env_cmd}
+	${pre_pg_regress_check}
+	--bindir=$ENV{DESTDIR}${PGBINDIR}
+)
+
+set(pg_regress_check_tmp
+	${tmp_env_cmd}
+	${pre_pg_regress_check}
+	--bindir=${tmp_check_folder}${PGBINDIR}
+)
+
+set(pg_isolation_regress_check
+	${env_cmd}
+	${pre_pg_isolation_regress_check}
+	--bindir=$ENV{DESTDIR}${PGBINDIR}
+)
+
+set(pg_isolation_regress_check_tmp
+	${tmp_env_cmd}
+	${pre_pg_isolation_regress_check}
+	--bindir=${tmp_check_folder}${PGBINDIR}
+)
+
+if(MAX_CONNECTIONS)
+	set(MAXCONNOPT "${MAXCONNOPT} --max-connections=${MAX_CONNECTIONS}")
+endif()
+
+if(TEMP_CONFIG)
+	set(TEMP_CONF "${TEMP_CONF} --temp-config=${TEMP_CONFIG}")
+endif()
+
+if(CMAKE_GENERATOR STREQUAL "Ninja")
+	set(check_make_command "ninja")
+else()
+	set(check_make_command "make")
+endif()
+
+macro(REGRESS_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
+	add_custom_target(${TARGET_NAME}_installcheck_tmp
+		COMMAND ${pg_regress_check_tmp} --inputdir="${CMAKE_CURRENT_SOURCE_DIR}" --dbname=${TARGET_NAME}_regress ${REGRESS_OPTS} --dlpath=${tmp_check_folder}${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${REGRESS_FILES}
+		DEPENDS tablespace-setup
+		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+	)
+	
+	add_custom_target(${TARGET_NAME}_installcheck
+		COMMAND ${pg_regress_check} --inputdir="${CMAKE_CURRENT_SOURCE_DIR}" --dbname=${TARGET_NAME}_regress ${REGRESS_OPTS} --dlpath=$ENV{DESTDIR}${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${REGRESS_FILES}
+		DEPENDS tablespace-setup
+		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+	)
+
+	if(CMAKE_GENERATOR STREQUAL "Ninja")
+		add_custom_target(${TARGET_NAME}_check
+			COMMAND ${CMAKE_COMMAND} -E remove_directory ${tmp_check_folder}
+			COMMAND DESTDIR=${tmp_check_folder} ${check_make_command} install
+			COMMAND DESTDIR=${tmp_check_folder} ${check_make_command} ${TARGET_NAME}_installcheck_tmp
+			DEPENDS tablespace-setup
+			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+		)
+	else()
+		add_custom_target(${TARGET_NAME}_check
+			COMMAND ${CMAKE_COMMAND} -E remove_directory ${tmp_check_folder}
+			COMMAND ${check_make_command} install DESTDIR=${tmp_check_folder}
+			COMMAND ${check_make_command} ${TARGET_NAME}_installcheck_tmp DESTDIR=${tmp_check_folder}
+			DEPENDS tablespace-setup
+			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+		)
+	endif()
+	
+	CMAKE_SET_TARGET_FOLDER(${TARGET_NAME}_installcheck_tmp tests/tmp)
+	CMAKE_SET_TARGET_FOLDER(${TARGET_NAME}_installcheck "tests/install")
+	CMAKE_SET_TARGET_FOLDER(${TARGET_NAME}_check tests)
+endmacro(REGRESS_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
+
+macro(ISOLATION_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
+	add_custom_target(${TARGET_NAME}_isolation_installcheck_tmp
+		COMMAND ${pg_isolation_regress_check} --inputdir="${CMAKE_CURRENT_SOURCE_DIR}" --dbname=${TARGET_NAME}_regress ${REGRESS_OPTS} --dlpath=${tmp_check_folder}${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${REGRESS_FILES}
+		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+	)
+
+	add_custom_target(${TARGET_NAME}_check
+		COMMAND ${CMAKE_COMMAND} -E remove_directory ${tmp_check_folder}
+		COMMAND ${check_make_command} install DESTDIR=${tmp_check_folder}
+		COMMAND ${check_make_command} ${TARGET_NAME}_isolation_installcheck_tmp DESTDIR=${tmp_check_folder}
+		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+	)
+	CMAKE_SET_TARGET_FOLDER(${TARGET_NAME}_isolation_installcheck_tmp tests/tmp)
+	CMAKE_SET_TARGET_FOLDER(${TARGET_NAME}_check tests)
+endmacro(ISOLATION_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
+
+macro(CONTRIB_REGRESS_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
+	set(contrib_check_targets ${contrib_check_targets} ${TARGET_NAME}_installcheck_tmp PARENT_SCOPE)
+	set(contrib_installcheck_targets ${contrib_installcheck_targets} ${TARGET_NAME}_installcheck PARENT_SCOPE)
+	REGRESS_CHECK("${TARGET_NAME}" "${REGRESS_OPTS}" "${REGRESS_FILES}")
+endmacro(CONTRIB_REGRESS_CHECK TARGET_NAME REGRESS_OPTS REGRESS_FILES)
diff --git a/cmake/ReplacePython.cmake b/cmake/ReplacePython.cmake
new file mode 100644
index 0000000..f768307
--- /dev/null
+++ b/cmake/ReplacePython.cmake
@@ -0,0 +1,79 @@
+macro(replace_python2 input_file output_file)
+	file(READ ${input_file} tmp_string)
+	string(REGEX REPLACE
+		"except \([a-zA-Z\\.]*\), *\([a-zA-Z]*\):"
+		"except \\1 as \\2:"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"<type 'exceptions\\.\([a-zA-Z]*\)'>" "<class '\\1'>"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"<type 'long'>" "<class 'int'>"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"\([0-9][0-9]*\)L" "\\1"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"\([ [{]\)u\"" "\\1\""
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"\([ [{]\)u'" "\\1'"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"def next" "def __next__"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"LANGUAGE plpythonu" "LANGUAGE plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"LANGUAGE plpython2u" "LANGUAGE plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"EXTENSION \([^ ]*_\)+plpythonu" "EXTENSION \\1plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"EXTENSION plpythonu" "EXTENSION plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"EXTENSION \([^ ]*_\)+plpython2u" "EXTENSION \\1plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"EXTENSION plpython2u" "EXTENSION plpython3u"
+		tmp_string
+		"${tmp_string}")
+	string(REGEX REPLACE
+		"installing required extension \"plpython2u\""
+		"installing required extension \"plpython3u\""
+		tmp_string
+		"${tmp_string}")
+	file(WRITE ${output_file} "${tmp_string}")
+endmacro()
+
+macro(replace_python_files r_regress_files)
+	file(MAKE_DIRECTORY sql/python3 expected/python3 results/python3)
+	set(adition_clean "")
+	set(regress_files3 "")
+	foreach(rfile ${r_regress_files})
+		replace_python2("sql/${rfile}.sql" "sql/python3/${rfile}.sql")
+		if(rfile STREQUAL "plpython_types")
+			replace_python2("expected/plpython_types_3.out" "expected/python3/${rfile}.out")
+		else()
+			replace_python2("expected/${rfile}.out" "expected/python3/${rfile}.out")
+		endif()
+		set(adition_clean "${adition_clean};sql/python3/${rfile}.sql;expected/python3/${rfile}.out;")
+		set(regress_files3 "${regress_files3};python3/${rfile}")
+	endforeach(rfile)
+	SET_DIRECTORY_PROPERTIES(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${adition_clean}")
+endmacro()
diff --git a/contrib/CMakeLists.txt b/contrib/CMakeLists.txt
new file mode 100644
index 0000000..7d25bf0
--- /dev/null
+++ b/contrib/CMakeLists.txt
@@ -0,0 +1,111 @@
+set(contrib_libs postgres)
+
+#TODO: need test under mingw or cygwin
+if(WIN32)
+	set(contrib_libs
+		port
+		pgcommon
+	)
+endif()
+
+if(MSVC OR MINGW)
+	set(contrib_libs
+		port
+		postgres
+		pgcommon_srv
+	)
+endif()
+
+set(contrib_check_targets "")
+set(contrib_installcheck_targets "")
+
+add_subdirectory(adminpack)
+add_subdirectory(auth_delay)
+add_subdirectory(auto_explain)
+add_subdirectory(btree_gin)
+add_subdirectory(btree_gist)
+add_subdirectory(bloom)
+add_subdirectory(chkpass)
+add_subdirectory(citext)
+add_subdirectory(cube)
+add_subdirectory(dblink)
+add_subdirectory(dict_int)
+add_subdirectory(dict_xsyn)
+add_subdirectory(earthdistance)
+add_subdirectory(file_fdw)
+add_subdirectory(fuzzystrmatch)
+add_subdirectory(hstore)
+if(PERLLIBS_FOUND)
+	add_subdirectory(hstore_plperl)
+endif()
+if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
+	add_subdirectory(hstore_plpython)
+endif()
+add_subdirectory(intarray)
+add_subdirectory(isn)
+add_subdirectory(lo)
+add_subdirectory(ltree)
+if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
+	add_subdirectory(ltree_plpython)
+endif()
+add_subdirectory(oid2name)
+add_subdirectory(pageinspect)
+add_subdirectory(passwordcheck)
+add_subdirectory(pg_buffercache)
+add_subdirectory(pg_freespacemap)
+add_subdirectory(pg_prewarm)
+add_subdirectory(pg_standby)
+add_subdirectory(pg_stat_statements)
+add_subdirectory(pg_trgm)
+add_subdirectory(pg_visibility)
+add_subdirectory(pgcrypto)
+add_subdirectory(pgrowlocks)
+add_subdirectory(pgstattuple)
+add_subdirectory(postgres_fdw)
+add_subdirectory(seg)
+if(SELINUX_LIBRARIES)
+	add_subdirectory(sepgsql)
+endif()
+add_subdirectory(spi)
+if(OPENSSL_FOUND)
+	add_subdirectory(sslinfo)
+endif()
+add_subdirectory(tablefunc)
+add_subdirectory(tcn)
+add_subdirectory(tsearch2)
+add_subdirectory(tsm_system_rows)
+add_subdirectory(tsm_system_time)
+add_subdirectory(unaccent)
+if(WITH_UUID)
+	add_subdirectory(uuid-ossp)
+endif()
+add_subdirectory(vacuumlo)
+if(LIBXML2_FOUND)
+	add_subdirectory(xml2)
+endif()
+
+add_custom_target(contrib_check
+	COMMAND ${CMAKE_COMMAND} -E remove_directory ${tmp_check_folder}
+	COMMAND make install DESTDIR=${tmp_check_folder}
+	COMMAND make ${contrib_check_targets} DESTDIR=${tmp_check_folder}
+	DEPENDS tablespace-setup
+	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+)
+
+#Make chain for run tests in strict order
+#TODO: we can't run tests separately now
+set(first_element "")
+list(GET contrib_installcheck_targets 0 first_element)
+add_custom_target(contrib_installcheck
+	COMMAND ${CMAKE_COMMAND} -E remove_directory ${tmp_check_folder}
+	DEPENDS tablespace-setup ${first_element}
+	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+)
+
+set(last_element OFF)
+foreach(loop_var IN ITEMS ${contrib_installcheck_targets})
+	if(last_element)
+		add_dependencies(${last_element} ${loop_var})
+	endif()
+	set(last_element ${loop_var})
+endforeach()
diff --git a/contrib/adminpack/CMakeLists.txt b/contrib/adminpack/CMakeLists.txt
new file mode 100644
index 0000000..c1f5251
--- /dev/null
+++ b/contrib/adminpack/CMakeLists.txt
@@ -0,0 +1,27 @@
+option(PGXS "Separate build" OFF)
+if(PGXS)
+	cmake_minimum_required(VERSION 2.8)
+	find_package(PostgreSQL)
+	include(${PostgreSQL_LIBRARY_DIRS}/cmake/PGXS.cmake)
+else()
+	include_directories("${PROJECT_SOURCE_DIR}/src/include")
+endif()
+
+set(extension_name adminpack)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	add_dependencies(${extension_name} postgres)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES ${extension_name}.control ${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
diff --git a/contrib/auth_delay/CMakeLists.txt b/contrib/auth_delay/CMakeLists.txt
new file mode 100644
index 0000000..b890114
--- /dev/null
+++ b/contrib/auth_delay/CMakeLists.txt
@@ -0,0 +1,27 @@
+option(PGXS "Separate build" OFF)
+if(PGXS)
+	cmake_minimum_required(VERSION 2.8)
+	find_package(PostgreSQL)
+	include(${PostgreSQL_LIBRARY_DIRS}/cmake/PGXS.cmake)
+else()
+	include_directories("${PROJECT_SOURCE_DIR}/src/include")
+	if(OPENSSL_FOUND)
+		include_directories(BEFORE ${OPENSSL_INCLUDE_DIR})
+	endif()
+endif()
+
+set(extension_name auth_delay)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	add_dependencies(${extension_name} postgres)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/contrib/auto_explain/CMakeLists.txt b/contrib/auto_explain/CMakeLists.txt
new file mode 100644
index 0000000..11ca366
--- /dev/null
+++ b/contrib/auto_explain/CMakeLists.txt
@@ -0,0 +1,18 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name auto_explain)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
diff --git a/contrib/bloom/CMakeLists.txt b/contrib/bloom/CMakeLists.txt
new file mode 100644
index 0000000..634b3f9
--- /dev/null
+++ b/contrib/bloom/CMakeLists.txt
@@ -0,0 +1,35 @@
+option(PGXS "Separate build" OFF)
+if(PGXS)
+	cmake_minimum_required(VERSION 2.8)
+	find_package(PostgreSQL)
+	include(${PostgreSQL_LIBRARY_DIRS}/cmake/PGXS.cmake)
+else()
+	include_directories("${PROJECT_SOURCE_DIR}/src/include")
+endif()
+
+set(extension_name bloom)
+
+add_library(${extension_name} MODULE
+	blcost.c
+	blinsert.c
+	blscan.c
+	blutils.c
+	blvacuum.c
+	blvalidate.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	add_dependencies(${extension_name} postgres)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES ${extension_name}.control ${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+CONTRIB_REGRESS_CHECK(${extension_name} "" "bloom")
diff --git a/contrib/btree_gin/CMakeLists.txt b/contrib/btree_gin/CMakeLists.txt
new file mode 100644
index 0000000..de598d9
--- /dev/null
+++ b/contrib/btree_gin/CMakeLists.txt
@@ -0,0 +1,47 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name btree_gin)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES ${extension_name}.control ${extension_name}--1.0.sql ${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regres_files
+	install_btree_gin
+	int2
+	int4
+	int8
+	float4
+	float8
+	money
+	oid
+	timestamp
+	timestamptz
+	time
+	timetz
+	date
+	interval
+	macaddr
+	inet
+	cidr
+	text
+	varchar
+	char
+	bytea
+	bit
+	varbit
+	numeric
+)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regres_files}")
diff --git a/contrib/btree_gist/CMakeLists.txt b/contrib/btree_gist/CMakeLists.txt
new file mode 100644
index 0000000..803cb0e
--- /dev/null
+++ b/contrib/btree_gist/CMakeLists.txt
@@ -0,0 +1,75 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name btree_gist)
+
+add_library(${extension_name} MODULE
+	${extension_name}.c
+	btree_utils_num.c
+	btree_utils_var.c
+	btree_int2.c
+	btree_int4.c
+	btree_int8.c
+	btree_float4.c
+	btree_float8.c
+	btree_cash.c
+	btree_oid.c
+	btree_ts.c
+	btree_time.c
+	btree_date.c
+	btree_interval.c
+	btree_macaddr.c
+	btree_inet.c
+	btree_text.c
+	btree_bytea.c
+	btree_bit.c
+	btree_numeric.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+
+set(regres_files
+	init
+	int2
+	int4
+	int8
+	float4
+	float8
+	cash
+	oid
+	timestamp
+	timestamptz
+	time
+	timetz
+	date
+	interval
+	macaddr
+	inet
+	cidr
+	text
+	varchar
+	char
+	bytea
+	bit
+	varbit
+	numeric
+	not_equal
+)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regres_files}")
diff --git a/contrib/chkpass/CMakeLists.txt b/contrib/chkpass/CMakeLists.txt
new file mode 100644
index 0000000..6151b0e
--- /dev/null
+++ b/contrib/chkpass/CMakeLists.txt
@@ -0,0 +1,22 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name chkpass)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+if(CRYPT_LIB)
+	target_link_libraries(${extension_name} ${CRYPT_LIB})
+endif()
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES ${extension_name}.control ${extension_name}--1.0.sql ${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/citext/CMakeLists.txt b/contrib/citext/CMakeLists.txt
new file mode 100644
index 0000000..3a0eb0e
--- /dev/null
+++ b/contrib/citext/CMakeLists.txt
@@ -0,0 +1,28 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name citext)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.4.sql
+	${extension_name}--1.3--1.4.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+CONTRIB_REGRESS_CHECK(${extension_name} "" "citext")
diff --git a/contrib/cube/CMakeLists.txt b/contrib/cube/CMakeLists.txt
new file mode 100644
index 0000000..9fafdea
--- /dev/null
+++ b/contrib/cube/CMakeLists.txt
@@ -0,0 +1,34 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name cube)
+
+bison_target(CUBE_PARSER cubeparse.y ${CMAKE_CURRENT_SOURCE_DIR}/cubeparse.c)
+flex_target(CUBE_SCANNER cubescan.l  ${CMAKE_CURRENT_SOURCE_DIR}/cubescan.c)
+add_flex_bison_dependency(CUBE_SCANNER CUBE_PARSER)
+set_source_files_properties(${BISON_CUBE_PARSER_OUTPUTS} PROPERTIES OBJECT_DEPENDS ${FLEX_CUBE_SCANNER_OUTPUTS})
+
+add_library(${extension_name} MODULE
+	${extension_name}.c
+	${BISON_CUBE_PARSER_OUTPUTS}
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+CONTRIB_REGRESS_CHECK(${extension_name} "" "cube")
diff --git a/contrib/dblink/CMakeLists.txt b/contrib/dblink/CMakeLists.txt
new file mode 100644
index 0000000..05507c4
--- /dev/null
+++ b/contrib/dblink/CMakeLists.txt
@@ -0,0 +1,30 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+
+set(extension_name dblink)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs} pq)
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files paths dblink)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/dict_int/CMakeLists.txt b/contrib/dict_int/CMakeLists.txt
new file mode 100644
index 0000000..5034d47
--- /dev/null
+++ b/contrib/dict_int/CMakeLists.txt
@@ -0,0 +1,25 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name dict_int)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files dict_int)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/dict_xsyn/CMakeLists.txt b/contrib/dict_xsyn/CMakeLists.txt
new file mode 100644
index 0000000..f8335b6
--- /dev/null
+++ b/contrib/dict_xsyn/CMakeLists.txt
@@ -0,0 +1,28 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name dict_xsyn)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+install(FILES
+	xsyn_sample.rules
+	DESTINATION ${PGSHAREDIR}/tsearch_data)
+
+set(regress_files dict_xsyn)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/earthdistance/CMakeLists.txt b/contrib/earthdistance/CMakeLists.txt
new file mode 100644
index 0000000..b4b4381
--- /dev/null
+++ b/contrib/earthdistance/CMakeLists.txt
@@ -0,0 +1,26 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name earthdistance)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files earthdistance)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/file_fdw/CMakeLists.txt b/contrib/file_fdw/CMakeLists.txt
new file mode 100644
index 0000000..93c354a
--- /dev/null
+++ b/contrib/file_fdw/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name file_fdw)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files file_fdw)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/fuzzystrmatch/CMakeLists.txt b/contrib/fuzzystrmatch/CMakeLists.txt
new file mode 100644
index 0000000..6e21763
--- /dev/null
+++ b/contrib/fuzzystrmatch/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name fuzzystrmatch)
+
+add_library(${extension_name} MODULE ${extension_name}.c dmetaphone.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/hstore/CMakeLists.txt b/contrib/hstore/CMakeLists.txt
new file mode 100644
index 0000000..096e193
--- /dev/null
+++ b/contrib/hstore/CMakeLists.txt
@@ -0,0 +1,38 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name hstore)
+
+add_library(${extension_name} MODULE
+	hstore_io.c
+	hstore_op.c
+	hstore_gist.c
+	hstore_gin.c
+	hstore_compat.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(MINGW)
+	set_target_properties(${extension_name} PROPERTIES LINK_FLAGS -Wl,--out-implib=libmin${extension_name}.dll.a)
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.4.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.3--1.4.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files hstore)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/hstore_plperl/CMakeLists.txt b/contrib/hstore_plperl/CMakeLists.txt
new file mode 100644
index 0000000..ea738ed
--- /dev/null
+++ b/contrib/hstore_plperl/CMakeLists.txt
@@ -0,0 +1,38 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/pl/plperl"
+	"${PROJECT_SOURCE_DIR}/contrib/hstore"
+	${PERL_INCLUDE_PATH}
+)
+
+if(MINGW)
+	link_directories("${PROJECT_BINARY_DIR}/contrib/hstore")
+endif()
+set(extension_name hstore_plperl)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs} ${PERL_LIBRARY} ${PERL_LDFLAGS})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(MINGW)
+	target_link_libraries(${extension_name} minhstore)
+	target_compile_definitions(${extension_name} PRIVATE -DPLPERL_HAVE_UID_GID)
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}u.control
+	${extension_name}u--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files hstore_plperl)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/hstore_plpython/CMakeLists.txt b/contrib/hstore_plpython/CMakeLists.txt
new file mode 100644
index 0000000..fa67001
--- /dev/null
+++ b/contrib/hstore_plpython/CMakeLists.txt
@@ -0,0 +1,57 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/pl/plpython"
+	"${PROJECT_SOURCE_DIR}/contrib/hstore"
+	${PYTHON_INCLUDE_DIRS}
+)
+
+if(MINGW)
+	link_directories(
+		"${PROJECT_BINARY_DIR}/contrib/hstore"
+		"${PROJECT_BINARY_DIR}/src/pl/plpython"
+	)
+endif()
+
+set(extension_name hstore_plpython)
+
+add_library(${extension_name}${PYTHON_VERSION_MAJOR} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name}${PYTHON_VERSION_MAJOR} ${contrib_libs} ${PYTHON_LIBRARIES})
+set_target_properties(${extension_name}${PYTHON_VERSION_MAJOR} PROPERTIES PREFIX "")
+add_dependencies(${extension_name}${PYTHON_VERSION_MAJOR} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(MINGW)
+	target_link_libraries(${extension_name}${PYTHON_VERSION_MAJOR} minhstore minplpython${PYTHON_VERSION_MAJOR})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name}${PYTHON_VERSION_MAJOR} contrib)
+endif()
+install(TARGETS ${extension_name}${PYTHON_VERSION_MAJOR}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}u.control
+	${extension_name}u--1.0.sql
+	${extension_name}2u.control
+	${extension_name}2u--1.0.sql
+	${extension_name}3u.control
+	${extension_name}3u--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files hstore_plpython)
+
+if(PYTHON_VERSION_MAJOR EQUAL 3)
+	replace_python_files("${regress_files}")
+	set(regress_options
+		"--load-extension=hstore"
+	)
+	CONTRIB_REGRESS_CHECK(${extension_name} "${regress_options}" "${regress_files3}")
+else()
+	set(regress_options
+		"--load-extension=hstore"
+		"--load-extension=plpythonu"
+		"--load-extension=hstore_plpythonu"
+	)
+	CONTRIB_REGRESS_CHECK(${extension_name} "${regress_options}" "${regress_files}")
+endif()
diff --git a/contrib/intarray/CMakeLists.txt b/contrib/intarray/CMakeLists.txt
new file mode 100644
index 0000000..e5bee80
--- /dev/null
+++ b/contrib/intarray/CMakeLists.txt
@@ -0,0 +1,36 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name intarray)
+set(extension_lib_name _int)
+
+add_library(${extension_lib_name} MODULE
+	_int_bool.c
+	_int_gist.c
+	_int_op.c
+	_int_tool.c
+	_intbig_gist.c
+	_int_gin.c
+	_int_selfuncs.c
+)
+target_link_libraries(${extension_lib_name} ${contrib_libs})
+set_target_properties(${extension_lib_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_lib_name} postgres)
+if (MSVC)
+	gen_def(${extension_lib_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_lib_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files ${extension_lib_name})
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/isn/CMakeLists.txt b/contrib/isn/CMakeLists.txt
new file mode 100644
index 0000000..9e19366
--- /dev/null
+++ b/contrib/isn/CMakeLists.txt
@@ -0,0 +1,26 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name isn)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files isn)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/lo/CMakeLists.txt b/contrib/lo/CMakeLists.txt
new file mode 100644
index 0000000..0c7aab0
--- /dev/null
+++ b/contrib/lo/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name lo)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/ltree/CMakeLists.txt b/contrib/ltree/CMakeLists.txt
new file mode 100644
index 0000000..e097839
--- /dev/null
+++ b/contrib/ltree/CMakeLists.txt
@@ -0,0 +1,37 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name ltree)
+add_definitions(-DLOWER_NODE)
+
+add_library(${extension_name} MODULE
+	ltree_io.c
+	ltree_op.c
+	lquery_op.c
+	_ltree_op.c
+	crc32.c
+	ltxtquery_io.c
+	ltxtquery_op.c
+	ltree_gist.c
+	_ltree_gist.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if (MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files ltree)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/ltree_plpython/CMakeLists.txt b/contrib/ltree_plpython/CMakeLists.txt
new file mode 100644
index 0000000..dd960aa
--- /dev/null
+++ b/contrib/ltree_plpython/CMakeLists.txt
@@ -0,0 +1,49 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/pl/plpython"
+	"${PROJECT_SOURCE_DIR}/contrib/ltree"
+	${PYTHON_INCLUDE_DIRS}
+)
+
+set(extension_name ltree_plpython)
+
+add_library(${extension_name}${PYTHON_VERSION_MAJOR} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name}${PYTHON_VERSION_MAJOR} ${contrib_libs} ${PYTHON_LIBRARIES})
+set_target_properties(${extension_name}${PYTHON_VERSION_MAJOR} PROPERTIES PREFIX "")
+add_dependencies(${extension_name}${PYTHON_VERSION_MAJOR} postgres ltree)
+if (MSVC)
+	gen_def(${extension_name}${PYTHON_VERSION_MAJOR})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name}${PYTHON_VERSION_MAJOR} contrib)
+endif()
+install(TARGETS ${extension_name}${PYTHON_VERSION_MAJOR}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}u.control
+	${extension_name}u--1.0.sql
+	${extension_name}2u.control
+	${extension_name}2u--1.0.sql
+	${extension_name}3u.control
+	${extension_name}3u--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files ltree_plpython)
+
+
+if(PYTHON_VERSION_MAJOR EQUAL 3)
+	replace_python_files("${regress_files}")
+	set(regress_options
+		"--load-extension=ltree"
+	)
+	CONTRIB_REGRESS_CHECK(${extension_name} "${regress_options}" "${regress_files3}")
+else()
+	set(regress_options
+		"--load-extension=hstore"
+		"--load-extension=plpythonu"
+		"--load-extension=ltree"
+		"--load-extension=ltree_plpythonu"
+	)
+	CONTRIB_REGRESS_CHECK(${extension_name} "${regress_options}" "${regress_files}")
+endif()
diff --git a/contrib/oid2name/CMakeLists.txt b/contrib/oid2name/CMakeLists.txt
new file mode 100644
index 0000000..3f1cefd
--- /dev/null
+++ b/contrib/oid2name/CMakeLists.txt
@@ -0,0 +1,25 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+
+set(extension_name oid2name)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+
+if(MSVC OR MINGW)
+	target_link_libraries(${extension_name} pgcommon pq)
+endif()
+
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/contrib/pageinspect/CMakeLists.txt b/contrib/pageinspect/CMakeLists.txt
new file mode 100644
index 0000000..3997184
--- /dev/null
+++ b/contrib/pageinspect/CMakeLists.txt
@@ -0,0 +1,34 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pageinspect)
+
+add_library(${extension_name} MODULE
+	rawpage.c
+	heapfuncs.c
+	btreefuncs.c
+	fsmfuncs.c
+	brinfuncs.c
+	ginfuncs.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.5.sql
+	${extension_name}--1.4--1.5.sql
+	${extension_name}--1.3--1.4.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/passwordcheck/CMakeLists.txt b/contrib/passwordcheck/CMakeLists.txt
new file mode 100644
index 0000000..468e4e8
--- /dev/null
+++ b/contrib/passwordcheck/CMakeLists.txt
@@ -0,0 +1,17 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name passwordcheck)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/contrib/pg_buffercache/CMakeLists.txt b/contrib/pg_buffercache/CMakeLists.txt
new file mode 100644
index 0000000..4a99728
--- /dev/null
+++ b/contrib/pg_buffercache/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_buffercache)
+
+add_library(${extension_name} MODULE ${extension_name}_pages.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pg_freespacemap/CMakeLists.txt b/contrib/pg_freespacemap/CMakeLists.txt
new file mode 100644
index 0000000..0045ddc
--- /dev/null
+++ b/contrib/pg_freespacemap/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_freespacemap)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pg_prewarm/CMakeLists.txt b/contrib/pg_prewarm/CMakeLists.txt
new file mode 100644
index 0000000..8018abd
--- /dev/null
+++ b/contrib/pg_prewarm/CMakeLists.txt
@@ -0,0 +1,22 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_prewarm)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pg_standby/CMakeLists.txt b/contrib/pg_standby/CMakeLists.txt
new file mode 100644
index 0000000..67305e9
--- /dev/null
+++ b/contrib/pg_standby/CMakeLists.txt
@@ -0,0 +1,14 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_standby)
+
+add_executable(${extension_name} ${extension_name}.c)
+target_link_libraries(${extension_name} port pgcommon pq)
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/contrib/pg_stat_statements/CMakeLists.txt b/contrib/pg_stat_statements/CMakeLists.txt
new file mode 100644
index 0000000..ec6891f
--- /dev/null
+++ b/contrib/pg_stat_statements/CMakeLists.txt
@@ -0,0 +1,26 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_stat_statements)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.4.sql
+	${extension_name}--1.3--1.4.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pg_trgm/CMakeLists.txt b/contrib/pg_trgm/CMakeLists.txt
new file mode 100644
index 0000000..d3bf9b0
--- /dev/null
+++ b/contrib/pg_trgm/CMakeLists.txt
@@ -0,0 +1,33 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_trgm)
+
+add_library(${extension_name} MODULE
+	trgm_op.c
+	trgm_gist.c
+	trgm_gin.c
+	trgm_regexp.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.3.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files pg_trgm pg_word_trgm)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/pg_visibility/CMakeLists.txt b/contrib/pg_visibility/CMakeLists.txt
new file mode 100644
index 0000000..bc8db81
--- /dev/null
+++ b/contrib/pg_visibility/CMakeLists.txt
@@ -0,0 +1,22 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pg_visibility)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pgcrypto/CMakeLists.txt b/contrib/pgcrypto/CMakeLists.txt
new file mode 100644
index 0000000..b4bfabd
--- /dev/null
+++ b/contrib/pgcrypto/CMakeLists.txt
@@ -0,0 +1,133 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+)
+if(ZLIB_FOUND)
+	include_directories(BEFORE ${ZLIB_INCLUDE_DIRS})
+endif()
+
+set(extension_name pgcrypto)
+
+set(pgcrypto_src
+	pgcrypto.c
+	px.c
+	px-hmac.c
+	px-crypt.c
+	crypt-gensalt.c
+	crypt-blowfish.c
+	crypt-des.c
+	crypt-md5.c
+	mbuf.c
+	pgp.c
+	pgp-armor.c
+	pgp-cfb.c
+	pgp-compress.c
+	pgp-decrypt.c
+	pgp-encrypt.c
+	pgp-info.c
+	pgp-mpi.c
+	pgp-pubdec.c
+	pgp-pubenc.c
+	pgp-pubkey.c
+	pgp-s2k.c
+	pgp-pgsql.c
+)
+
+set(regress_files
+	init
+	md5
+	sha1
+	hmac-md5
+	hmac-sha1
+	blowfish
+	rijndael
+	crypt-des
+	crypt-md5
+	crypt-blowfish
+	crypt-xdes
+	pgp-armor
+	pgp-decrypt
+	pgp-encrypt
+	pgp-pubkey-decrypt
+	pgp-pubkey-encrypt
+	pgp-info
+)
+
+if(OPENSSL_FOUND)
+	include_directories(BEFORE ${OPENSSL_INCLUDE_DIR})
+	set(pgcrypto_src ${pgcrypto_src} openssl.c pgp-mpi-openssl.c)
+	set(regress_files ${regress_files}
+		sha2
+		des
+		3des
+		cast5
+	)
+else()
+	add_executable(gen-rtab rijndael.c)
+	target_compile_definitions(gen-rtab PRIVATE -DPRINT_TABS)
+
+	add_custom_command(
+		DEPENDS gen-rtab
+		OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/rijndael.tbl
+		COMMAND gen-rtab > rijndael.tbl
+		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+	)
+
+	set(pgcrypto_src ${pgcrypto_src}
+		md5.c
+		sha1.c
+		sha2.c
+		internal.c
+		internal-sha2.c
+		blf.c
+		rijndael.tbl
+		rijndael.c
+		fortuna.c
+		random.c
+		pgp-mpi-internal.c
+		imath.c
+	)
+	set(regress_files ${regress_files} sha2)
+endif()
+
+if(ZLIB_FOUND)
+	set(regress_files ${regress_files} pgp-compression)
+endif()
+
+add_library(${extension_name} MODULE ${pgcrypto_src})
+target_link_libraries(${extension_name}
+	${contrib_libs}
+)
+if(OPENSSL_FOUND)
+	target_link_libraries(${extension_name} ${OPENSSL_LIBRARIES})
+endif()
+if(CRYPT_LIB)
+	target_link_libraries(${extension_name} ${CRYPT_LIB})
+endif()
+if(ZLIB_FOUND)
+	target_link_libraries(${extension_name} ${ZLIB_LIBRARIES})
+endif()
+if(WIN32)
+	target_link_libraries(${extension_name} ws2_32)
+endif()
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.3.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/pgcrypto/rijndael.c b/contrib/pgcrypto/rijndael.c
index 4adbcc1..af7b87c 100644
--- a/contrib/pgcrypto/rijndael.c
+++ b/contrib/pgcrypto/rijndael.c
@@ -78,8 +78,9 @@ static void gen_tabs(void);
 #endif
 
 #ifdef PRE_CALC_TABLES
-
-#include "rijndael.tbl"
+/* WRAP_INCLUDE for remove CMake circular dependency */
+#define WRAP_INCLUDE(x) x
+#include WRAP_INCLUDE("rijndael.tbl")
 #define tab_gen		1
 #else							/* !PRE_CALC_TABLES */
 
diff --git a/contrib/pgcrypto/rijndael.tbl b/contrib/pgcrypto/rijndael.tbl
index c7610c0..bc0a382 100644
--- a/contrib/pgcrypto/rijndael.tbl
+++ b/contrib/pgcrypto/rijndael.tbl
@@ -1133,6 +1133,7 @@ static const u4byte  il_tab[4][256] = {
 };
 
 static const u4byte rco_tab[10] = {
-  0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010,
+  0x00000001, 0x00000002, 0x00000004, 0x00000008, 0x00000010, 
   0x00000020, 0x00000040, 0x00000080, 0x0000001b, 0x00000036
 };
+
diff --git a/contrib/pgrowlocks/CMakeLists.txt b/contrib/pgrowlocks/CMakeLists.txt
new file mode 100644
index 0000000..ab294ce
--- /dev/null
+++ b/contrib/pgrowlocks/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pgrowlocks)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/pgstattuple/CMakeLists.txt b/contrib/pgstattuple/CMakeLists.txt
new file mode 100644
index 0000000..5f0cac4
--- /dev/null
+++ b/contrib/pgstattuple/CMakeLists.txt
@@ -0,0 +1,33 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name pgstattuple)
+
+add_library(${extension_name} MODULE
+	${extension_name}.c
+	pgstatindex.c
+	pgstatapprox.c
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.4.sql
+	${extension_name}--1.3--1.4.sql
+	${extension_name}--1.2--1.3.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files pgstattuple)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/postgres_fdw/CMakeLists.txt b/contrib/postgres_fdw/CMakeLists.txt
new file mode 100644
index 0000000..eb243ff
--- /dev/null
+++ b/contrib/postgres_fdw/CMakeLists.txt
@@ -0,0 +1,33 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+
+set(extension_name postgres_fdw)
+
+add_library(${extension_name} MODULE
+	${extension_name}.c
+	option.c
+	deparse.c
+	connection.c
+	shippable.c
+)
+target_link_libraries(${extension_name} ${contrib_libs} pq)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files postgres_fdw)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/seg/CMakeLists.txt b/contrib/seg/CMakeLists.txt
new file mode 100644
index 0000000..d464851
--- /dev/null
+++ b/contrib/seg/CMakeLists.txt
@@ -0,0 +1,34 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name seg)
+
+bison_target(CUBE_PARSER segparse.y ${CMAKE_CURRENT_SOURCE_DIR}/segparse.c)
+flex_target(CUBE_SCANNER segscan.l  ${CMAKE_CURRENT_SOURCE_DIR}/segscan.c)
+add_flex_bison_dependency(CUBE_SCANNER CUBE_PARSER)
+set_source_files_properties(${BISON_CUBE_PARSER_OUTPUTS} PROPERTIES OBJECT_DEPENDS ${FLEX_CUBE_SCANNER_OUTPUTS})
+
+add_library(${extension_name} MODULE
+	${extension_name}.c
+	${BISON_CUBE_PARSER_OUTPUTS}
+)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files seg)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/sepgsql/CMakeLists.txt b/contrib/sepgsql/CMakeLists.txt
new file mode 100644
index 0000000..c44c78c
--- /dev/null
+++ b/contrib/sepgsql/CMakeLists.txt
@@ -0,0 +1,45 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name sepgsql)
+
+set(sepgsql_sql_output
+	${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql
+)
+
+add_custom_command(
+	MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql.in
+	OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql
+	COMMAND sed 's,MODULE_PATHNAME,$$libdir/${extension_name},g' sepgsql.sql.in > ${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_library(${extension_name} MODULE
+	hooks.c
+	selinux.c
+	uavc.c
+	label.c
+	dml.c
+	database.c
+	schema.c
+	relation.c
+	proc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql
+)
+target_link_libraries(${extension_name} ${contrib_libs} ${SELINUX_LIBRARIES})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${CMAKE_CURRENT_SOURCE_DIR}/sepgsql.sql
+	DESTINATION ${PGSHAREDIR}/contrib)
+
+#set(regress_files seg)
+#CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/spi/CMakeLists.txt b/contrib/spi/CMakeLists.txt
new file mode 100644
index 0000000..49bc5d7
--- /dev/null
+++ b/contrib/spi/CMakeLists.txt
@@ -0,0 +1,29 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+
+set(modules_list 
+	autoinc
+	insert_username
+	moddatetime
+	refint
+	timetravel
+)
+
+foreach(loop_var IN ITEMS ${modules_list})
+	add_library(${loop_var} MODULE ${loop_var}.c)
+	target_compile_definitions(${loop_var} PRIVATE -DREFINT_VERBOSE)
+	target_link_libraries(${loop_var} ${contrib_libs})
+	set_target_properties(${loop_var} PROPERTIES PREFIX "")
+	add_dependencies(${loop_var} postgres)
+	if(MSVC)
+		gen_def(${loop_var})
+	endif()
+	if(NOT PGXS)
+		CMAKE_SET_TARGET_FOLDER(${loop_var} contrib/spi)
+	endif()
+	install(TARGETS ${loop_var}
+			RUNTIME DESTINATION ${PGBINDIR}
+			LIBRARY DESTINATION ${LIBDIR})
+	install(FILES ${loop_var}.control ${loop_var}--1.0.sql ${loop_var}--unpackaged--1.0.sql
+		DESTINATION ${PGSHAREDIR}/extension)
+endforeach(loop_var)
diff --git a/contrib/sslinfo/CMakeLists.txt b/contrib/sslinfo/CMakeLists.txt
new file mode 100644
index 0000000..60d0a77
--- /dev/null
+++ b/contrib/sslinfo/CMakeLists.txt
@@ -0,0 +1,30 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	${OPENSSL_INCLUDE_DIR}
+)
+
+set(extension_name sslinfo)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs} ${OPENSSL_LIBRARIES})
+if(CRYPT_LIB)
+	target_link_libraries(${extension_name} ${CRYPT_LIB})
+endif()
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.2.sql
+	${extension_name}--1.1--1.2.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/tablefunc/CMakeLists.txt b/contrib/tablefunc/CMakeLists.txt
new file mode 100644
index 0000000..1c1550d
--- /dev/null
+++ b/contrib/tablefunc/CMakeLists.txt
@@ -0,0 +1,25 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name tablefunc)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files tablefunc)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/tcn/CMakeLists.txt b/contrib/tcn/CMakeLists.txt
new file mode 100644
index 0000000..87273a8
--- /dev/null
+++ b/contrib/tcn/CMakeLists.txt
@@ -0,0 +1,21 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name tcn)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
diff --git a/contrib/tsearch2/CMakeLists.txt b/contrib/tsearch2/CMakeLists.txt
new file mode 100644
index 0000000..09e792c
--- /dev/null
+++ b/contrib/tsearch2/CMakeLists.txt
@@ -0,0 +1,25 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name tsearch2)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files tsearch2)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/tsm_system_rows/CMakeLists.txt b/contrib/tsm_system_rows/CMakeLists.txt
new file mode 100644
index 0000000..874b018
--- /dev/null
+++ b/contrib/tsm_system_rows/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name tsm_system_rows)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files tsm_system_rows)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/tsm_system_time/CMakeLists.txt b/contrib/tsm_system_time/CMakeLists.txt
new file mode 100644
index 0000000..c723e1a
--- /dev/null
+++ b/contrib/tsm_system_time/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name tsm_system_time)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files tsm_system_time)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/unaccent/CMakeLists.txt b/contrib/unaccent/CMakeLists.txt
new file mode 100644
index 0000000..be6fa70
--- /dev/null
+++ b/contrib/unaccent/CMakeLists.txt
@@ -0,0 +1,29 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(extension_name unaccent)
+
+add_library(${extension_name} MODULE ${extension_name}.c)
+target_link_libraries(${extension_name} ${contrib_libs})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+install(FILES
+	${extension_name}.rules
+	DESTINATION ${PGSHAREDIR}/tsearch_data)
+
+set(regress_files unaccent)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/uuid-ossp/CMakeLists.txt b/contrib/uuid-ossp/CMakeLists.txt
new file mode 100644
index 0000000..4886f0f
--- /dev/null
+++ b/contrib/uuid-ossp/CMakeLists.txt
@@ -0,0 +1,27 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+include_directories(${LIBUUID_INCLUDE_DIRS})
+include_directories("${PROJECT_SOURCE_DIR}/contrib/pgcrypto")
+
+set(extension_name uuid-ossp)
+
+add_library(${extension_name} MODULE ${extension_name}.c ${UUID_EXTRA_OBJS})
+target_link_libraries(${extension_name} ${contrib_libs} ${LIBUUID_LIBRARIES})
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.0.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files uuid_ossp)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/contrib/uuid-ossp/uuid-ossp.c b/contrib/uuid-ossp/uuid-ossp.c
index 3e12bc4..39bf685 100644
--- a/contrib/uuid-ossp/uuid-ossp.c
+++ b/contrib/uuid-ossp/uuid-ossp.c
@@ -26,15 +26,7 @@
  */
 #define uuid_hash bsd_uuid_hash
 
-#ifdef HAVE_UUID_H
 #include <uuid.h>
-#endif
-#ifdef HAVE_OSSP_UUID_H
-#include <ossp/uuid.h>
-#endif
-#ifdef HAVE_UUID_UUID_H
-#include <uuid/uuid.h>
-#endif
 
 #undef uuid_hash
 
diff --git a/contrib/vacuumlo/CMakeLists.txt b/contrib/vacuumlo/CMakeLists.txt
new file mode 100644
index 0000000..e7b0481
--- /dev/null
+++ b/contrib/vacuumlo/CMakeLists.txt
@@ -0,0 +1,17 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+
+set(extension_name vacuumlo)
+
+add_executable(${extension_name} ${extension_name}.c)
+target_link_libraries(${extension_name} port pgcommon pq)
+set_target_properties(${extension_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_name} postgres)
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/contrib/xml2/CMakeLists.txt b/contrib/xml2/CMakeLists.txt
new file mode 100644
index 0000000..4880422
--- /dev/null
+++ b/contrib/xml2/CMakeLists.txt
@@ -0,0 +1,40 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include"
+	${LIBXML2_INCLUDE_DIR}
+)
+
+if(LIBXSLT_FOUND)
+	include_directories(BEFORE ${LIBXSLT_INCLUDE_DIR})
+endif()
+
+set(extension_name xml2)
+set(extension_lib_name pgxml)
+
+add_library(${extension_lib_name} MODULE xpath.c xslt_proc.c)
+target_link_libraries(${extension_lib_name}
+	${contrib_libs}
+	${LIBXML2_LIBRARIES}
+)
+if(LIBXSLT_FOUND)
+	target_link_libraries(${extension_lib_name} ${LIBXSLT_LIBRARIES})
+endif()
+set_target_properties(${extension_lib_name} PROPERTIES PREFIX "")
+add_dependencies(${extension_lib_name} postgres)
+if(MSVC)
+	gen_def(${extension_name})
+endif()
+if(NOT PGXS)
+	CMAKE_SET_TARGET_FOLDER(${extension_name} contrib)
+endif()
+install(TARGETS ${extension_lib_name}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	${extension_name}.control
+	${extension_name}--1.1.sql
+	${extension_name}--1.0--1.1.sql
+	${extension_name}--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(regress_files xml2)
+CONTRIB_REGRESS_CHECK(${extension_name} "" "${regress_files}")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..fc4f7bd
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,85 @@
+include_directories("${PROJECT_SOURCE_DIR}/src/include")
+
+set(gen_errorcodes_depend
+	${PROJECT_SOURCE_DIR}/src/backend/utils/errcodes.txt
+	${PROJECT_SOURCE_DIR}/src/backend/utils/generate-errcodes.pl
+)
+set(gen_errorcodes_output
+	${PROJECT_SOURCE_DIR}/src/include/utils/errcodes.h
+)
+
+add_custom_command(
+	MAIN_DEPENDENCY ${gen_errorcodes_depend}
+	OUTPUT ${gen_errorcodes_output}
+	COMMAND ${PERL_EXECUTABLE} generate-errcodes.pl errcodes.txt > ${gen_errorcodes_output}
+	WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/backend/utils
+)
+
+add_custom_target(gen_errorcodes DEPENDS ${gen_errorcodes_output})
+
+set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${gen_errorcodes_output})
+
+set(PARSER_SCAN_FLEX_FLAGS, "-Cfe -p -p")
+bison_target(PARSER_GRAM
+	${PROJECT_SOURCE_DIR}/src/backend/parser/gram.y
+	${PROJECT_SOURCE_DIR}/src/backend/parser/gram.c
+)
+flex_target(PARSER_SCAN
+	${PROJECT_SOURCE_DIR}/src/backend/parser/scan.l
+	${PROJECT_SOURCE_DIR}/src/backend/parser/scan.c
+	${PARSER_SCAN_FLEX_FLAGS}
+)
+
+set_source_files_properties(${BISON_PARSER_GRAM_OUTPUTS}
+	PROPERTIES OBJECT_DEPENDS ${FLEX_PARSER_SCAN_OUTPUTS})
+
+add_custom_command(
+	MAIN_DEPENDENCY ${BISON_PARSER_GRAM_OUTPUTS}
+	OUTPUT ${PROJECT_SOURCE_DIR}/src/include/parser/gram.h
+	COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/src/backend/parser/gram.h ${PROJECT_SOURCE_DIR}/src/include/parser/gram.h
+)
+
+add_custom_target(generate_parser_gram_h DEPENDS ${PROJECT_SOURCE_DIR}/src/include/parser/gram.h)
+
+add_subdirectory(include)
+add_subdirectory(port)
+add_subdirectory(common)
+add_subdirectory(fe_utils)
+add_subdirectory(interfaces/libpq)
+add_subdirectory(interfaces/ecpg/pgtypeslib)
+add_subdirectory(interfaces/ecpg/ecpglib)
+add_subdirectory(interfaces/ecpg/compatlib)
+add_subdirectory(interfaces/ecpg/include)
+
+add_subdirectory(backend)
+add_subdirectory(timezone)
+add_subdirectory(test/regress)
+add_subdirectory(test/isolation)
+
+add_subdirectory(pl/plpgsql/src)
+if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
+	add_subdirectory(pl/plpython)
+endif()
+if(PERLLIBS_FOUND)
+	add_subdirectory(pl/plperl)
+endif()
+if(TCL_FOUND)
+	add_subdirectory(pl/tcl)
+endif()
+
+add_subdirectory(bin/initdb)
+add_subdirectory(bin/pg_archivecleanup)
+add_subdirectory(bin/pg_basebackup)
+add_subdirectory(bin/pg_config)
+add_subdirectory(bin/pg_controldata)
+add_subdirectory(bin/pg_ctl)
+add_subdirectory(bin/pg_dump)
+add_subdirectory(bin/pg_resetxlog)
+add_subdirectory(bin/pg_rewind)
+add_subdirectory(bin/pg_test_fsync)
+add_subdirectory(bin/pg_test_timing)
+add_subdirectory(bin/pg_upgrade)
+add_subdirectory(bin/pg_xlogdump)
+add_subdirectory(bin/pgbench)
+add_subdirectory(bin/psql)
+add_subdirectory(bin/scripts)
diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt
new file mode 100644
index 0000000..9633c6c
--- /dev/null
+++ b/src/backend/CMakeLists.txt
@@ -0,0 +1,1103 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+set(postgres_include_catalog "${PROJECT_SOURCE_DIR}/src/include/catalog")
+
+set(gen_fmgrtab_depend
+	utils/Gen_fmgrtab.pl
+	${CMAKE_CURRENT_SOURCE_DIR}/catalog/Catalog.pm
+	${postgres_include_catalog}/pg_proc.h
+)
+set(gen_fmgrtab_output
+	${PROJECT_SOURCE_DIR}/src/include/utils/fmgroids.h
+	${CMAKE_CURRENT_SOURCE_DIR}/utils/fmgrtab.c
+)
+
+add_custom_command(
+	MAIN_DEPENDENCY ${gen_fmgrtab_depend}
+	OUTPUT ${gen_fmgrtab_output}
+	COMMAND ${PERL_EXECUTABLE} -I${CMAKE_CURRENT_SOURCE_DIR}/catalog Gen_fmgrtab.pl ${postgres_include_catalog}/pg_proc.h
+	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/utils/fmgroids.h ${PROJECT_SOURCE_DIR}/src/include/utils/fmgroids.h
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/utils
+)
+
+set(gen_probe_depend
+	${CMAKE_CURRENT_SOURCE_DIR}/utils/Gen_dummy_probes.sed
+	${CMAKE_CURRENT_SOURCE_DIR}/utils/probes.d
+)
+
+set(gen_probe_output
+	${PROJECT_SOURCE_DIR}/src/include/utils/probes.h
+)
+
+add_custom_command(
+	MAIN_DEPENDENCY ${gen_probe_depend}
+	OUTPUT ${gen_probe_output}
+	COMMAND sed -f Gen_dummy_probes.sed probes.d >  ${gen_probe_output}
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/utils
+)
+
+flex_target(GUC_SCANNER utils/misc/guc-file.l  ${CMAKE_CURRENT_SOURCE_DIR}/utils/misc/guc-file.c)
+set_source_files_properties(utils/misc/guc.c PROPERTIES OBJECT_DEPENDS ${FLEX_GUC_SCANNER_OUTPUTS})
+
+if(WIN32 OR MINGW)
+	set(dynloader_SRCS port/dynloader/win32.c)
+	set(dynloader_H port/dynloader/win32.h)
+elseif(CYGWIN)
+	set(dynloader_SRCS port/dynloader/cygwin.c)
+	set(dynloader_H port/dynloader/cygwin.h)
+elseif(APPLE)
+	set(dynloader_SRCS port/dynloader/darwin.c)
+	set(dynloader_H port/dynloader/darwin.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+	set(dynloader_SRCS port/dynloader/linux.c)
+	set(dynloader_H port/dynloader/linux.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "HP-UX")
+	set(dynloader_SRCS port/dynloader/hpux.c)
+	set(dynloader_H port/dynloader/hpux.h)
+#TODO: FreeBSD, OpenBSD, NetBSD is equal loader
+elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
+	set(dynloader_SRCS port/dynloader/freebsd.c)
+	set(dynloader_H port/dynloader/freebsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
+	set(dynloader_SRCS port/dynloader/openbsd.c)
+	set(dynloader_H port/dynloader/openbsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
+	set(dynloader_SRCS port/dynloader/netbsd.c)
+	set(dynloader_H port/dynloader/netbsd.h)
+elseif(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
+	set(dynloader_SRCS port/dynloader/solaris.c)
+	set(dynloader_H port/dynloader/solaris.h)
+else(WIN32 OR MINGW)
+	message(WARNING "${CMAKE_SYSTEM_NAME}")
+endif(WIN32 OR MINGW)
+
+add_custom_command(
+	MAIN_DEPENDENCY "${CMAKE_CURRENT_SOURCE_DIR}/${dynloader_H}"
+	OUTPUT "${PROJECT_SOURCE_DIR}/src/include/dynloader.h"
+	COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/${dynloader_H} ${PROJECT_SOURCE_DIR}/src/include/dynloader.h
+)
+
+#Need for bin/pg_xlogdump
+set(access_rmgrdesc_SRC
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/brindesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/clogdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/committsdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/dbasedesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/gindesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/gistdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/hashdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/heapdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/mxactdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/nbtdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/relmapdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/replorigindesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/seqdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/smgrdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/spgdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/standbydesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/tblspcdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/xactdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/xlogdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/genericdesc.c
+	${CMAKE_CURRENT_SOURCE_DIR}/access/rmgrdesc/logicalmsgdesc.c
+)
+
+set(rmgrdesc_SRC
+	${access_rmgrdesc_SRC}
+	PARENT_SCOPE
+)
+
+set(access_SRCS
+	${gen_fmgrtab_output}
+	${gen_probe_output}
+	${PROJECT_SOURCE_DIR}/src/include/dynloader.h
+	${access_rmgrdesc_SRC}
+
+	access/brin/brin.c
+	access/brin/brin_pageops.c
+	access/brin/brin_revmap.c
+	access/brin/brin_tuple.c
+	access/brin/brin_xlog.c
+	access/brin/brin_minmax.c
+	access/brin/brin_inclusion.c
+	access/brin/brin_validate.c
+
+	access/common/heaptuple.c
+	access/common/indextuple.c
+	access/common/printtup.c
+	access/common/reloptions.c
+	access/common/scankey.c
+	access/common/tupconvert.c
+	access/common/tupdesc.c
+
+	access/gin/ginutil.c
+	access/gin/gininsert.c
+	access/gin/ginxlog.c
+	access/gin/ginentrypage.c
+	access/gin/gindatapage.c
+	access/gin/ginbtree.c
+	access/gin/ginscan.c
+	access/gin/ginget.c
+	access/gin/ginvacuum.c
+	access/gin/ginarrayproc.c
+	access/gin/ginbulk.c
+	access/gin/ginfast.c
+	access/gin/ginpostinglist.c
+	access/gin/ginlogic.c
+	access/gin/ginvalidate.c
+
+	access/gist/gist.c
+	access/gist/gistutil.c
+	access/gist/gistxlog.c
+	access/gist/gistvacuum.c
+	access/gist/gistget.c
+	access/gist/gistscan.c
+	access/gist/gistproc.c
+	access/gist/gistsplit.c
+	access/gist/gistbuild.c
+	access/gist/gistbuildbuffers.c
+	access/gist/gistvalidate.c
+
+	access/hash/hash.c
+	access/hash/hashfunc.c
+	access/hash/hashinsert.c
+	access/hash/hashovfl.c
+	access/hash/hashpage.c
+	access/hash/hashscan.c
+	access/hash/hashsearch.c
+	access/hash/hashsort.c
+	access/hash/hashutil.c
+	access/hash/hashvalidate.c
+
+	access/heap/heapam.c
+	access/heap/hio.c
+	access/heap/pruneheap.c
+	access/heap/rewriteheap.c
+	access/heap/syncscan.c
+	access/heap/tuptoaster.c
+	access/heap/visibilitymap.c
+
+	access/index/genam.c
+	access/index/indexam.c
+	access/index/amapi.c
+	access/index/amvalidate.c
+
+	access/nbtree/nbtcompare.c
+	access/nbtree/nbtinsert.c
+	access/nbtree/nbtpage.c
+	access/nbtree/nbtree.c
+	access/nbtree/nbtsearch.c
+	access/nbtree/nbtutils.c
+	access/nbtree/nbtsort.c
+	access/nbtree/nbtxlog.c
+	access/nbtree/nbtvalidate.c
+
+	access/spgist/spgutils.c
+	access/spgist/spginsert.c
+	access/spgist/spgscan.c
+	access/spgist/spgvacuum.c
+	access/spgist/spgdoinsert.c
+	access/spgist/spgxlog.c
+	access/spgist/spgtextproc.c
+	access/spgist/spgquadtreeproc.c
+	access/spgist/spgkdtreeproc.c
+	access/spgist/spgvalidate.c
+
+	access/tablesample/bernoulli.c
+	access/tablesample/system.c
+	access/tablesample/tablesample.c
+
+	access/transam/clog.c
+	access/transam/commit_ts.c
+	access/transam/multixact.c
+	access/transam/generic_xlog.c
+	access/transam/parallel.c
+	access/transam/rmgr.c
+	access/transam/slru.c
+	access/transam/subtrans.c
+	access/transam/timeline.c
+	access/transam/transam.c
+	access/transam/twophase.c
+	access/transam/twophase_rmgr.c
+	access/transam/varsup.c
+	access/transam/xact.c
+	access/transam/xlog.c
+	access/transam/xlogarchive.c
+	access/transam/xlogfuncs.c
+	access/transam/xloginsert.c
+	access/transam/xlogreader.c
+	access/transam/xlogutils.c
+)
+
+bison_target(BOOTSTRAP_PARSER bootstrap/bootparse.y ${CMAKE_CURRENT_SOURCE_DIR}/bootstrap/bootparse.c)
+flex_target(BOOTSTRAP_SCANNER bootstrap/bootscanner.l  ${CMAKE_CURRENT_SOURCE_DIR}/bootstrap/bootscanner.c)
+add_flex_bison_dependency(BOOTSTRAP_SCANNER BOOTSTRAP_PARSER)
+
+set_source_files_properties(bootstrap/bootparse.c PROPERTIES OBJECT_DEPENDS ${FLEX_BOOTSTRAP_SCANNER_OUTPUTS})
+
+set(bootstrap_SRCS
+	bootstrap/bootstrap.c
+	${BISON_BOOTSTRAP_PARSER_OUTPUTS}
+)
+
+set_source_files_properties(
+	${BISON_BOOTSTRAP_PARSER_OUTPUTS}
+	PROPERTIES GENERATED TRUE
+)
+
+set(POSTGRES_BKI_SRCS
+	${postgres_include_catalog}/pg_proc.h
+	${postgres_include_catalog}/pg_type.h
+	${postgres_include_catalog}/pg_attribute.h
+	${postgres_include_catalog}/pg_class.h
+	${postgres_include_catalog}/pg_attrdef.h
+	${postgres_include_catalog}/pg_constraint.h
+	${postgres_include_catalog}/pg_inherits.h
+	${postgres_include_catalog}/pg_index.h
+	${postgres_include_catalog}/pg_operator.h
+	${postgres_include_catalog}/pg_opfamily.h
+	${postgres_include_catalog}/pg_opclass.h
+	${postgres_include_catalog}/pg_am.h
+	${postgres_include_catalog}/pg_amop.h
+	${postgres_include_catalog}/pg_amproc.h
+	${postgres_include_catalog}/pg_language.h
+	${postgres_include_catalog}/pg_largeobject_metadata.h
+	${postgres_include_catalog}/pg_largeobject.h
+	${postgres_include_catalog}/pg_aggregate.h
+	${postgres_include_catalog}/pg_statistic.h
+	${postgres_include_catalog}/pg_rewrite.h
+	${postgres_include_catalog}/pg_trigger.h
+	${postgres_include_catalog}/pg_event_trigger.h
+	${postgres_include_catalog}/pg_description.h
+	${postgres_include_catalog}/pg_cast.h
+	${postgres_include_catalog}/pg_enum.h
+	${postgres_include_catalog}/pg_namespace.h
+	${postgres_include_catalog}/pg_conversion.h
+	${postgres_include_catalog}/pg_depend.h
+	${postgres_include_catalog}/pg_database.h
+	${postgres_include_catalog}/pg_db_role_setting.h
+	${postgres_include_catalog}/pg_tablespace.h
+	${postgres_include_catalog}/pg_pltemplate.h
+	${postgres_include_catalog}/pg_authid.h
+	${postgres_include_catalog}/pg_auth_members.h
+	${postgres_include_catalog}/pg_shdepend.h
+	${postgres_include_catalog}/pg_shdescription.h
+	${postgres_include_catalog}/pg_ts_config.h
+	${postgres_include_catalog}/pg_ts_config_map.h
+	${postgres_include_catalog}/pg_ts_dict.h
+	${postgres_include_catalog}/pg_ts_parser.h
+	${postgres_include_catalog}/pg_ts_template.h
+	${postgres_include_catalog}/pg_extension.h
+	${postgres_include_catalog}/pg_foreign_data_wrapper.h
+	${postgres_include_catalog}/pg_foreign_server.h
+	${postgres_include_catalog}/pg_user_mapping.h
+	${postgres_include_catalog}/pg_foreign_table.h
+	${postgres_include_catalog}/pg_policy.h
+	${postgres_include_catalog}/pg_replication_origin.h
+	${postgres_include_catalog}/pg_default_acl.h
+	${postgres_include_catalog}/pg_init_privs.h
+	${postgres_include_catalog}/pg_seclabel.h
+	${postgres_include_catalog}/pg_shseclabel.h
+	${postgres_include_catalog}/pg_collation.h
+	${postgres_include_catalog}/pg_range.h
+	${postgres_include_catalog}/pg_transform.h
+	${postgres_include_catalog}/toasting.h
+	${postgres_include_catalog}/indexing.h
+)
+
+set(catalog_SRCS
+	catalog/catalog.c
+	catalog/dependency.c
+	catalog/heap.c
+	catalog/index.c
+	catalog/indexing.c
+	catalog/namespace.c
+	catalog/aclchk.c
+	catalog/objectaccess.c
+	catalog/objectaddress.c
+	catalog/pg_aggregate.c
+	catalog/pg_collation.c
+	catalog/pg_constraint.c
+	catalog/pg_conversion.c
+	catalog/pg_depend.c
+	catalog/pg_enum.c
+	catalog/pg_inherits.c
+	catalog/pg_largeobject.c
+	catalog/pg_namespace.c
+	catalog/pg_operator.c
+	catalog/pg_proc.c
+	catalog/pg_range.c
+	catalog/pg_db_role_setting.c
+	catalog/pg_shdepend.c
+	catalog/pg_type.c
+	catalog/storage.c
+	catalog/toasting.c
+)
+
+set(catalog_include
+	"-I${PROJECT_SOURCE_DIR}/src/backend/catalog"
+	"-I${PROJECT_SOURCE_DIR}/src/include/catalog"
+)
+
+add_custom_command(
+	DEPENDS ${POSTGRES_BKI_SRCS}
+	OUTPUT ${PROJECT_SOURCE_DIR}/src/include/catalog/schemapg.h
+	COMMAND ${PERL_EXECUTABLE} genbki.pl ${catalog_include} --set-version=${POSTGRES_MAJOR_VERSION} ${POSTGRES_BKI_SRCS}
+	COMMAND ${CMAKE_COMMAND} -E copy schemapg.h ${PROJECT_SOURCE_DIR}/src/include/catalog/schemapg.h
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/catalog
+)
+
+set_source_files_properties(utils/cache/relcache.c
+	PROPERTIES OBJECT_DEPENDS ${PROJECT_SOURCE_DIR}/src/include/catalog/schemapg.h)
+
+set(commands_SRCS
+	commands/aggregatecmds.c
+	commands/alter.c
+	commands/analyze.c
+	commands/async.c
+	commands/amcmds.c
+	commands/cluster.c
+	commands/comment.c
+	commands/collationcmds.c
+	commands/constraint.c
+	commands/conversioncmds.c
+	commands/copy.c
+	commands/createas.c
+	commands/dbcommands.c
+	commands/define.c
+	commands/discard.c
+	commands/dropcmds.c
+	commands/event_trigger.c
+	commands/explain.c
+	commands/extension.c
+	commands/foreigncmds.c
+	commands/functioncmds.c
+	commands/indexcmds.c
+	commands/lockcmds.c
+	commands/matview.c
+	commands/operatorcmds.c
+	commands/opclasscmds.c
+	commands/policy.c
+	commands/portalcmds.c
+	commands/prepare.c
+	commands/proclang.c
+	commands/schemacmds.c
+	commands/seclabel.c
+	commands/sequence.c
+	commands/tablecmds.c
+	commands/tablespace.c
+	commands/trigger.c
+	commands/tsearchcmds.c
+	commands/typecmds.c
+	commands/user.c
+	commands/vacuum.c
+	commands/vacuumlazy.c
+	commands/variable.c
+	commands/view.c
+)
+
+set(executor_SRCS
+	executor/execAmi.c
+	executor/execCurrent.c
+	executor/execGrouping.c
+	executor/execIndexing.c
+	executor/execJunk.c
+	executor/execMain.c
+	executor/execProcnode.c
+	executor/execParallel.c
+	executor/execQual.c
+	executor/execScan.c
+	executor/execTuples.c
+	executor/execUtils.c
+	executor/functions.c
+	executor/instrument.c
+	executor/tqueue.c
+	executor/nodeAppend.c
+	executor/nodeAgg.c
+	executor/nodeBitmapAnd.c
+	executor/nodeBitmapOr.c
+	executor/nodeBitmapHeapscan.c
+	executor/nodeBitmapIndexscan.c
+	executor/nodeCustom.c
+	executor/nodeHash.c
+	executor/nodeHashjoin.c
+	executor/nodeIndexscan.c
+	executor/nodeIndexonlyscan.c
+	executor/nodeLimit.c
+	executor/nodeLockRows.c
+	executor/nodeMaterial.c
+	executor/nodeMergeAppend.c
+	executor/nodeMergejoin.c
+	executor/nodeModifyTable.c
+	executor/nodeNestloop.c
+	executor/nodeFunctionscan.c
+	executor/nodeRecursiveunion.c
+	executor/nodeResult.c
+	executor/nodeSamplescan.c
+	executor/nodeSeqscan.c
+	executor/nodeSetOp.c
+	executor/nodeSort.c
+	executor/nodeUnique.c
+	executor/nodeValuesscan.c
+	executor/nodeCtescan.c
+	executor/nodeWorktablescan.c
+	executor/nodeGroup.c
+	executor/nodeGather.c
+	executor/nodeSubplan.c
+	executor/nodeSubqueryscan.c
+	executor/nodeTidscan.c
+	executor/nodeForeignscan.c
+	executor/nodeWindowAgg.c
+	executor/tstoreReceiver.c
+	executor/spi.c
+)
+
+set(lib_SRCS
+	lib/binaryheap.c
+	lib/bipartite_match.c
+	lib/hyperloglog.c
+	lib/ilist.c
+	lib/pairingheap.c
+	lib/rbtree.c
+	lib/stringinfo.c
+)
+
+if(OPENSSL_FOUND)
+	set(libpq_ssl_SRCS libpq/be-secure-openssl.c)
+endif(OPENSSL_FOUND)
+
+set(libpq_SRCS
+	libpq/be-fsstubs.c
+	libpq/be-secure.c
+	libpq/auth.c
+	libpq/crypt.c
+	libpq/hba.c
+	libpq/pqcomm.c
+	libpq/pqformat.c
+	libpq/pqmq.c
+	libpq/pqsignal.c
+	libpq/ifaddr.c
+	${libpq_ssl_SRCS}
+)
+
+set(utils_SRCS
+	utils/error/assert.c
+	utils/error/elog.c
+	utils/cache/attoptcache.c
+	utils/cache/catcache.c
+	utils/cache/evtcache.c
+	utils/cache/inval.c
+	utils/cache/plancache.c
+	utils/cache/relcache.c
+	utils/cache/relmapper.c
+	utils/cache/relfilenodemap.c
+	utils/cache/spccache.c
+	utils/cache/syscache.c
+	utils/cache/lsyscache.c
+	utils/cache/typcache.c
+	utils/cache/ts_cache.c
+	utils/mmgr/aset.c
+	utils/mmgr/mcxt.c
+	utils/mmgr/portalmem.c
+	utils/adt/acl.c
+	utils/adt/arrayfuncs.c
+	utils/adt/array_expanded.c
+	utils/adt/array_selfuncs.c
+	utils/adt/array_typanalyze.c
+	utils/adt/array_userfuncs.c
+	utils/adt/arrayutils.c
+	utils/adt/ascii.c
+	utils/adt/bool.c
+	utils/adt/cash.c
+	utils/adt/char.c
+	utils/adt/date.c
+	utils/adt/datetime.c
+	utils/adt/datum.c
+	utils/adt/dbsize.c
+	utils/adt/domains.c
+	utils/adt/encode.c
+	utils/adt/enum.c
+	utils/adt/expandeddatum.c
+	utils/adt/float.c
+	utils/adt/format_type.c
+	utils/adt/formatting.c
+	utils/adt/genfile.c
+	utils/adt/geo_ops.c
+	utils/adt/geo_selfuncs.c
+	utils/adt/geo_spgist.c
+	utils/adt/inet_cidr_ntop.c
+	utils/adt/inet_net_pton.c
+	utils/adt/int.c
+	utils/adt/int8.c
+	utils/adt/json.c
+	utils/adt/jsonb.c
+	utils/adt/jsonb_gin.c
+	utils/adt/jsonb_op.c
+	utils/adt/jsonb_util.c
+	utils/adt/jsonfuncs.c
+	utils/adt/like.c
+	#utils/adt/like_match.c
+	utils/adt/lockfuncs.c
+	utils/adt/mac.c
+	utils/adt/misc.c
+	utils/adt/nabstime.c
+	utils/adt/name.c
+	utils/adt/network.c
+	utils/adt/network_gist.c
+	utils/adt/network_selfuncs.c
+	utils/adt/numeric.c
+	utils/adt/numutils.c
+	utils/adt/oid.c
+	utils/adt/oracle_compat.c
+	utils/adt/orderedsetaggs.c
+	utils/adt/pg_locale.c
+	utils/adt/pg_lsn.c
+	utils/adt/pg_upgrade_support.c
+	utils/adt/pgstatfuncs.c
+	utils/adt/pseudotypes.c
+	utils/adt/quote.c
+	utils/adt/rangetypes.c
+	utils/adt/rangetypes_gist.c
+	utils/adt/rangetypes_selfuncs.c
+	utils/adt/rangetypes_spgist.c
+	utils/adt/rangetypes_typanalyze.c
+	utils/adt/regexp.c
+	utils/adt/regproc.c
+	utils/adt/ri_triggers.c
+	utils/adt/rowtypes.c
+	utils/adt/ruleutils.c
+	utils/adt/selfuncs.c
+	utils/adt/tid.c
+	utils/adt/timestamp.c
+	utils/adt/trigfuncs.c
+	utils/adt/tsginidx.c
+	utils/adt/tsgistidx.c
+	utils/adt/tsquery.c
+	utils/adt/tsquery_cleanup.c
+	utils/adt/tsquery_gist.c
+	utils/adt/tsquery_op.c
+	utils/adt/tsquery_rewrite.c
+	utils/adt/tsquery_util.c
+	utils/adt/tsrank.c
+	utils/adt/tsvector.c
+	utils/adt/tsvector_op.c
+	utils/adt/tsvector_parser.c
+	utils/adt/txid.c
+	utils/adt/uuid.c
+	utils/adt/varbit.c
+	utils/adt/varchar.c
+	utils/adt/varlena.c
+	utils/adt/amutils.c
+	utils/adt/version.c
+	utils/adt/windowfuncs.c
+	utils/adt/xid.c
+	utils/adt/xml.c
+	utils/adt/network_spgist.c
+	utils/fmgr/dfmgr.c
+	utils/fmgr/fmgr.c
+	utils/fmgr/funcapi.c
+	utils/hash/dynahash.c
+	utils/hash/hashfn.c
+	utils/hash/pg_crc.c
+	utils/init/globals.c
+	utils/init/miscinit.c
+	utils/init/postinit.c
+	utils/resowner/resowner.c
+	utils/sort/logtape.c
+	utils/sort/sortsupport.c
+	utils/sort/tuplesort.c
+	utils/sort/tuplestore.c
+	utils/time/combocid.c
+	utils/time/tqual.c
+	utils/time/snapmgr.c
+	utils/misc/guc.c
+	utils/misc/help_config.c
+	utils/misc/pg_rusage.c
+	utils/misc/pg_controldata.c
+	utils/misc/pg_config.c
+	utils/misc/ps_status.c
+	utils/misc/rls.c
+	utils/misc/sampling.c
+	utils/misc/superuser.c
+	utils/misc/timeout.c
+	utils/misc/tzparser.c
+	utils/mb/encnames.c
+	utils/mb/conv.c
+	utils/mb/mbutils.c
+	utils/mb/wchar.c
+	utils/mb/wstrcmp.c
+	utils/mb/wstrncmp.c
+)
+
+set(nodes_SRCS
+	nodes/nodeFuncs.c
+	nodes/nodes.c
+	nodes/list.c
+	nodes/bitmapset.c
+	nodes/tidbitmap.c
+	nodes/copyfuncs.c
+	nodes/equalfuncs.c
+	nodes/makefuncs.c
+	nodes/outfuncs.c
+	nodes/readfuncs.c
+	nodes/print.c
+	nodes/read.c
+	nodes/params.c
+	nodes/value.c
+	nodes/extensible.c
+)
+
+set(optimizer_SRCS
+	optimizer/geqo/geqo_copy.c
+	optimizer/geqo/geqo_eval.c
+	optimizer/geqo/geqo_main.c
+	optimizer/geqo/geqo_misc.c
+	optimizer/geqo/geqo_mutation.c
+	optimizer/geqo/geqo_pool.c
+	optimizer/geqo/geqo_random.c
+	optimizer/geqo/geqo_recombination.c
+	optimizer/geqo/geqo_selection.c
+	optimizer/geqo/geqo_erx.c
+	optimizer/geqo/geqo_pmx.c
+	optimizer/geqo/geqo_cx.c
+	optimizer/geqo/geqo_px.c
+	optimizer/geqo/geqo_ox1.c
+	optimizer/geqo/geqo_ox2.c
+	optimizer/path/allpaths.c
+	optimizer/path/clausesel.c
+	optimizer/path/costsize.c
+	optimizer/path/equivclass.c
+	optimizer/path/indxpath.c
+	optimizer/path/joinpath.c
+	optimizer/path/joinrels.c
+	optimizer/path/pathkeys.c
+	optimizer/path/tidpath.c
+	optimizer/plan/analyzejoins.c
+	optimizer/plan/createplan.c
+	optimizer/plan/initsplan.c
+	optimizer/plan/planagg.c
+	optimizer/plan/planmain.c
+	optimizer/plan/planner.c
+	optimizer/plan/setrefs.c
+	optimizer/plan/subselect.c
+	optimizer/prep/prepjointree.c
+	optimizer/prep/prepqual.c
+	optimizer/prep/prepsecurity.c
+	optimizer/prep/preptlist.c
+	optimizer/prep/prepunion.c
+	optimizer/util/clauses.c
+	optimizer/util/joininfo.c
+	optimizer/util/orclauses.c
+	optimizer/util/pathnode.c
+	optimizer/util/placeholder.c
+	optimizer/util/plancat.c
+	optimizer/util/predtest.c
+	optimizer/util/relnode.c
+	optimizer/util/restrictinfo.c
+	optimizer/util/tlist.c
+	optimizer/util/var.c
+)
+
+set(rewrite_SRCS
+	rewrite/rewriteRemove.c
+	rewrite/rewriteDefine.c
+	rewrite/rewriteHandler.c
+	rewrite/rewriteManip.c
+	rewrite/rewriteSupport.c
+	rewrite/rowsecurity.c
+)
+
+set(parser_SRCS
+	parser/analyze.c
+	#parser/gram.c
+	parser/parser.c
+	parser/parse_agg.c
+	parser/parse_clause.c
+	parser/parse_coerce.c
+	parser/parse_collate.c
+	parser/parse_cte.c
+	parser/parse_expr.c
+	parser/parse_func.c
+	parser/parse_node.c
+	parser/parse_oper.c
+	parser/parse_param.c
+	parser/parse_relation.c
+	parser/parse_target.c
+	parser/parse_type.c
+	parser/parse_utilcmd.c
+	parser/scansup.c
+	${PROJECT_SOURCE_DIR}/src/backend/parser/gram.c
+	${PROJECT_SOURCE_DIR}/src/backend/parser/scan.c
+)
+
+set_source_files_properties(
+	${PROJECT_SOURCE_DIR}/src/backend/parser/gram.c
+	${PROJECT_SOURCE_DIR}/src/backend/parser/scan.c
+	PROPERTIES GENERATED TRUE
+)
+
+set(tcop_SRCS
+	tcop/dest.c
+	tcop/fastpath.c
+	tcop/postgres.c
+	tcop/pquery.c
+	tcop/utility.c
+)
+
+set(tsearch_SRCS
+	tsearch/ts_locale.c
+	tsearch/ts_parse.c
+	tsearch/wparser.c
+	tsearch/wparser_def.c
+	tsearch/dict.c
+	tsearch/dict_simple.c
+	tsearch/dict_synonym.c
+	tsearch/dict_thesaurus.c
+	tsearch/dict_ispell.c
+	tsearch/regis.c
+	tsearch/spell.c
+	tsearch/to_tsany.c
+	tsearch/ts_selfuncs.c
+	tsearch/ts_typanalyze.c
+	tsearch/ts_utils.c
+)
+
+install(FILES
+			tsearch/dicts/ispell_sample.dict
+			tsearch/dicts/ispell_sample.affix
+			tsearch/dicts/hunspell_sample.affix
+			tsearch/dicts/hunspell_sample_long.affix
+			tsearch/dicts/hunspell_sample_long.dict
+			tsearch/dicts/hunspell_sample_num.affix
+			tsearch/dicts/hunspell_sample_num.dict
+			tsearch/dicts/synonym_sample.syn
+			tsearch/dicts/thesaurus_sample.ths
+		DESTINATION ${PGSHAREDIR}/tsearch_data)
+
+set(regex_SRCS
+	regex/regcomp.c
+	regex/regerror.c
+	regex/regexec.c
+	regex/regfree.c
+	regex/regprefix.c
+	regex/regexport.c
+)
+
+set(postmaster_SRCS
+	postmaster/autovacuum.c
+	postmaster/bgworker.c
+	postmaster/bgwriter.c
+	postmaster/checkpointer.c
+	postmaster/fork_process.c
+	postmaster/pgarch.c
+	postmaster/pgstat.c
+	postmaster/postmaster.c
+	postmaster/startup.c
+	postmaster/syslogger.c
+	postmaster/walwriter.c
+)
+
+if(WIN32 OR CYGWIN OR MINGW)
+	set(backend_port_win32_SRC
+		port/win32/timer.c
+		port/win32/socket.c
+		port/win32/signal.c
+		# I hope it is not necessary for modern MinGW
+		#port/win32/mingwcompat.c
+		port/win32/crashdump.c
+	)
+	if(NAVE_MINIDUMP_TYPE)
+		set(backend_port_win32_SRC
+			${backend_port_win32_SRC}
+			port/win32/crashdump.c
+		)
+	endif(NAVE_MINIDUMP_TYPE)
+endif()
+
+set(gen_lwlocknames_depend
+	${PROJECT_SOURCE_DIR}/src/backend/storage/lmgr/generate-lwlocknames.pl
+	${PROJECT_SOURCE_DIR}/src/backend/storage/lmgr/lwlocknames.txt
+)
+
+set(gen_lwlocknames_output
+	${PROJECT_SOURCE_DIR}/src/backend/storage/lmgr/lwlocknames.c
+	${PROJECT_SOURCE_DIR}/src/backend/storage/lmgr/lwlocknames.h
+	${PROJECT_SOURCE_DIR}/src/include/storage/lwlocknames.h
+)
+
+
+add_custom_command(
+	OUTPUT ${gen_lwlocknames_output}
+	MAIN_DEPENDENCY ${gen_lwlocknames_depend}
+	COMMAND ${PERL_EXECUTABLE} generate-lwlocknames.pl lwlocknames.txt
+	COMMAND ${CMAKE_COMMAND} -E copy lwlocknames.h ${PROJECT_SOURCE_DIR}/src/include/storage/lwlocknames.h
+	WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/backend/storage/lmgr
+)
+
+set(backend_port_SRCS
+	port/atomics.c
+	port/tas/${TAS}
+	${backend_port_win32_SRC}
+	${backend_port_darwin_SRC}
+	${dynloader_SRCS}
+	${SEMA_IMPLEMENTATION}
+	${SHMEM_IMPLEMENTATION}
+)
+
+
+set(storage_SRCS
+	${gen_lwlocknames_output}
+	storage/buffer/buf_table.c
+	storage/buffer/buf_init.c
+	storage/buffer/bufmgr.c
+	storage/buffer/freelist.c
+	storage/buffer/localbuf.c
+	storage/file/fd.c
+	storage/file/buffile.c
+	storage/file/copydir.c
+	storage/file/reinit.c
+	storage/freespace/freespace.c
+	storage/freespace/fsmpage.c
+	storage/freespace/indexfsm.c
+	storage/ipc/dsm_impl.c
+	storage/ipc/dsm.c
+	storage/ipc/ipc.c
+	storage/ipc/ipci.c
+	storage/ipc/pmsignal.c
+	storage/ipc/procarray.c
+	storage/ipc/procsignal.c
+	storage/ipc/shmem.c
+	storage/ipc/shmqueue.c
+	storage/ipc/shm_mq.c
+	storage/ipc/shm_toc.c
+	storage/ipc/sinval.c
+	storage/ipc/sinvaladt.c
+	storage/ipc/standby.c
+	storage/ipc/latch.c
+	storage/large_object/inv_api.c
+	storage/lmgr/lmgr.c # Need some for check s_lock
+	storage/lmgr/lock.c
+	storage/lmgr/proc.c
+	storage/lmgr/deadlock.c
+	storage/lmgr/lwlock.c
+	storage/lmgr/spin.c
+	storage/lmgr/s_lock.c
+	storage/lmgr/predicate.c
+	storage/page/bufpage.c
+	storage/page/checksum.c
+	storage/page/itemptr.c
+	storage/smgr/md.c
+	storage/smgr/smgr.c
+	storage/smgr/smgrtype.c
+)
+
+if(NOT MSVC)
+	set_source_files_properties(storage/page/checksum.c
+		PROPERTIES COMPILE_FLAGS ${CFLAGS_VECTOR})
+endif()
+bison_target(REPL_GRAM
+	replication/repl_gram.y
+	${CMAKE_CURRENT_SOURCE_DIR}/replication/repl_gram.c
+)
+flex_target(REPL_SCAN
+	replication/repl_scanner.l
+	${CMAKE_CURRENT_SOURCE_DIR}/replication/repl_scanner.c
+)
+set_source_files_properties(${BISON_REPL_GRAM_OUTPUTS}
+	PROPERTIES OBJECT_DEPENDS ${FLEX_REPL_SCAN_OUTPUTS})
+
+bison_target(SYNC_GRAM
+	replication/syncrep_gram.y
+	${CMAKE_CURRENT_SOURCE_DIR}/replication/syncrep_gram.c
+)
+flex_target(SYNC_SCAN
+	replication/syncrep_scanner.l
+	${CMAKE_CURRENT_SOURCE_DIR}/replication/syncrep_scanner.c
+)
+set_source_files_properties(${BISON_SYNC_GRAM_OUTPUTS}
+	PROPERTIES OBJECT_DEPENDS ${FLEX_SYNC_SCAN_OUTPUTS})
+
+
+set(replication_SRCS
+	replication/walsender.c
+	replication/walreceiverfuncs.c
+	replication/walreceiver.c
+	replication/basebackup.c
+	replication/slot.c
+	replication/slotfuncs.c
+	replication/syncrep.c
+	replication/logical/decode.c
+	replication/logical/logical.c
+	replication/logical/logicalfuncs.c
+	replication/logical/reorderbuffer.c
+	replication/logical/origin.c
+	replication/logical/snapbuild.c
+	replication/logical/message.c
+	${BISON_REPL_GRAM_OUTPUTS}
+	${BISON_SYNC_GRAM_OUTPUTS}
+)
+
+set(timezone_SRCS
+	${PROJECT_SOURCE_DIR}/src/timezone/localtime.c
+	${PROJECT_SOURCE_DIR}/src/timezone/strftime.c
+	${PROJECT_SOURCE_DIR}/src/timezone/pgtz.c
+)
+
+add_custom_command(
+	OUTPUT "${PROJECT_SOURCE_DIR}/src/backend/utils/sort/qsort_tuple.c"
+	#MAIN_DEPENDENCY gen_qsort_tuple.pl
+	COMMAND ${PERL_EXECUTABLE} "gen_qsort_tuple.pl" > "${PROJECT_SOURCE_DIR}/src/backend/utils/sort/qsort_tuple.c"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/utils/sort
+)
+
+set_source_files_properties(utils/sort/tuplesort.c
+	PROPERTIES OBJECT_DEPENDS "${PROJECT_SOURCE_DIR}/src/backend/utils/sort/qsort_tuple.c")
+
+set(postgres_SRCS
+	${storage_SRCS}
+	${access_SRCS}
+	${bootstrap_SRCS}
+	${commands_SRCS}
+	${catalog_SRCS}
+	${executor_SRCS}
+	foreign/foreign.c
+	${utils_SRCS}
+	${lib_SRCS}
+	${libpq_SRCS}
+	${nodes_SRCS}
+	${optimizer_SRCS}
+	${rewrite_SRCS}
+	${parser_SRCS}
+	${tcop_SRCS}
+	${tsearch_SRCS}
+	${regex_SRCS}
+	${postmaster_SRCS}
+	${backend_port_SRCS}
+	${replication_SRCS}
+	${timezone_SRCS}
+	main/main.c
+)
+
+if(OPENSSL_FOUND)
+	include_directories(BEFORE	${OPENSSL_INCLUDE_DIR})
+endif()
+if(LIBXML2_FOUND)
+	include_directories(BEFORE	${LIBXML2_INCLUDE_DIR})
+endif()
+#include_directories(BEFORE
+#	${OPENSSL_INCLUDE_DIR}
+#	${LIBXML2_INCLUDE_DIR}
+#)
+
+if(MSVC)
+	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:${WIN32_STACK_RLIMIT}")
+endif()
+
+if(MINGW OR CYGWIN)
+	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,${WIN32_STACK_RLIMIT} -Wl,--export-all-symbols")
+endif()
+
+add_executable(postgres
+	${postgres_SRCS}
+)
+
+target_link_libraries(postgres
+	pgcommon_srv
+	port_srv
+	pq
+	${READLINE_LIBRARY}
+	${CMAKE_THREAD_LIBS_INIT}
+	${DL_LIBRARIES}
+	${LIB_M}
+	${GSS_LIBS}
+)
+
+if(OPENSSL_FOUND)
+	target_link_libraries(postgres ${OPENSSL_LIBRARIES})
+endif()
+
+if(LIBXML2_FOUND)
+	target_link_libraries(postgres ${LIBXML2_LIBRARIES})
+endif()
+
+if(USE_PAM)
+	target_link_libraries(postgres ${PAM_LIB})
+endif()
+
+if(LDAP_FOUND)
+	target_link_libraries(postgres ${LBER_LIBRARIES})
+endif()
+
+add_dependencies(postgres gen_errorcodes generate_parser_gram_h)
+
+if(UNIX AND NOT APPLE)
+	target_link_libraries(postgres rt)
+endif()
+
+
+target_compile_definitions(postgres PRIVATE -DPG_KRB_SRVTAB="${PG_KRB_SRVTAB}")
+
+if(WIN32)
+	target_compile_definitions(postgres PRIVATE -DBUILDING_DLL -DEXEC_BACKEND)
+endif()
+
+if(MINGW)
+	set_target_properties(postgres PROPERTIES LINK_FLAGS -Wl,--out-implib=libpostgres.dll.a)
+endif()
+
+set_target_properties(postgres PROPERTIES ENABLE_EXPORTS 1)
+set_target_properties(postgres PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)
+#Realy need?
+add_custom_target(duplicate_oids
+	COMMAND ${PERL_EXECUTABLE} ./duplicate_oids
+	WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/include/catalog
+)
+
+add_library(pqwalreceiver MODULE
+	replication/libpqwalreceiver/libpqwalreceiver.c
+	${backend_port_win32_SRC}
+	${backend_port_darwin_SRC}
+)
+
+if(MSVC)
+	gen_def(pqwalreceiver)
+	gen_def(postgres)
+endif()
+
+if(WIN32)
+	target_link_libraries(pqwalreceiver postgres)
+endif()
+
+#Strange logic
+if(MINGW)
+	target_compile_definitions(pqwalreceiver PRIVATE -DBUILDING_DLL)
+endif()
+
+target_link_libraries(pqwalreceiver pq)
+
+install(TARGETS postgres pqwalreceiver
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+install(FILES
+	catalog/postgres.bki
+	catalog/postgres.description
+	catalog/postgres.shdescription
+	catalog/information_schema.sql
+	catalog/sql_features.txt
+	catalog/system_views.sql
+	libpq/pg_hba.conf.sample
+	libpq/pg_ident.conf.sample
+	utils/misc/postgresql.conf.sample
+DESTINATION ${PGSHAREDIR})
+
+add_subdirectory(utils/mb/conversion_procs)
+add_subdirectory(snowball)
+
+if(ENABLE_NLS)
+	MAKE_MO(postgres "de;es;fr;id;it;ja;pl;pt_BR;ru;zh_CN")
+endif()
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index d907e6b..1aad8f0 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -822,7 +822,7 @@ pg_GSS_recvauth(Port *port)
 			if (!kt_path ||
 				snprintf(kt_path, kt_len, "KRB5_KTNAME=%s",
 						 pg_krb_server_keyfile) != kt_len - 2 ||
-				putenv(kt_path) != 0)
+				pg_putenv_proxy(kt_path) != 0)
 			{
 				ereport(LOG,
 						(errcode(ERRCODE_OUT_OF_MEMORY),
diff --git a/src/backend/snowball/CMakeLists.txt b/src/backend/snowball/CMakeLists.txt
new file mode 100644
index 0000000..1299271
--- /dev/null
+++ b/src/backend/snowball/CMakeLists.txt
@@ -0,0 +1,126 @@
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include/snowball"
+	"${PROJECT_SOURCE_DIR}/src/include/snowball/libstemmer"
+)
+
+set(snow_languages
+	"danish\;danish"
+	"dutch\;dutch"
+	"english\;english"
+	"finnish\;finnish"
+	"french\;french"
+	"german\;german"
+	"hungarian\;hungarian"
+	"italian\;italian"
+	"norwegian\;norwegian"
+	"portuguese\;portuguese"
+	"romanian\;romanian"
+	"russian\;english"
+	"spanish\;spanish"
+	"swedish\;swedish"
+	"turkish\;turkish"
+)
+
+#Create snowball_create.sql
+file(STRINGS snowball.sql.in snowball_sql_tmpl NEWLINE_CONSUME)
+file(STRINGS snowball_func.sql.in snowball_func_sql_tmpl NEWLINE_CONSUME)
+
+file(WRITE "snowball_create.sql" ${snowball_func_sql_tmpl})
+
+foreach(pair_language ${snow_languages})
+	LIST(GET pair_language 0 lang)
+	LIST(GET pair_language 1 ascii_lang)
+	if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/stopwords/${lang}.stop")
+		set(stopwords ", StopWords=${lang}")
+	else()
+		set(stopwords "")
+	endif()
+	string(REPLACE "_LANGNAME_"
+		"${lang}" out_string
+		"${snowball_sql_tmpl}")
+	string(REPLACE "_DICTNAME_"
+		"${lang}_stem" out_string
+		"${out_string}")
+	string(REPLACE "_CFGNAME_"
+		"${lang}" out_string
+		"${out_string}")
+	string(REPLACE "_ASCDICTNAME_"
+		"${ascii_lang}_stem" out_string
+		"${out_string}")
+	string(REPLACE "_NONASCDICTNAME_"
+		"${lang}_stem" out_string
+		"${out_string}")
+	string(REPLACE "_STOPWORDS_"
+		"${stopwords}" out_string
+		"${out_string}")
+	file(APPEND "snowball_create.sql" ${out_string})
+endforeach()
+
+
+set(snowball_SRCS
+	dict_snowball.c
+	libstemmer/api.c
+	libstemmer/utilities.c
+	libstemmer/stem_ISO_8859_1_danish.c
+	libstemmer/stem_ISO_8859_1_dutch.c
+	libstemmer/stem_ISO_8859_1_english.c
+	libstemmer/stem_ISO_8859_1_finnish.c
+	libstemmer/stem_ISO_8859_1_french.c
+	libstemmer/stem_ISO_8859_1_german.c
+	libstemmer/stem_ISO_8859_1_hungarian.c
+	libstemmer/stem_ISO_8859_1_italian.c
+	libstemmer/stem_ISO_8859_1_norwegian.c
+	libstemmer/stem_ISO_8859_1_porter.c
+	libstemmer/stem_ISO_8859_1_portuguese.c
+	libstemmer/stem_ISO_8859_1_spanish.c
+	libstemmer/stem_ISO_8859_1_swedish.c
+	libstemmer/stem_ISO_8859_2_romanian.c
+	libstemmer/stem_KOI8_R_russian.c
+	libstemmer/stem_UTF_8_danish.c
+	libstemmer/stem_UTF_8_dutch.c
+	libstemmer/stem_UTF_8_english.c
+	libstemmer/stem_UTF_8_finnish.c
+	libstemmer/stem_UTF_8_french.c
+	libstemmer/stem_UTF_8_german.c
+	libstemmer/stem_UTF_8_hungarian.c
+	libstemmer/stem_UTF_8_italian.c
+	libstemmer/stem_UTF_8_norwegian.c
+	libstemmer/stem_UTF_8_porter.c
+	libstemmer/stem_UTF_8_portuguese.c
+	libstemmer/stem_UTF_8_romanian.c
+	libstemmer/stem_UTF_8_russian.c
+	libstemmer/stem_UTF_8_spanish.c
+	libstemmer/stem_UTF_8_swedish.c
+	libstemmer/stem_UTF_8_turkish.c
+)
+
+add_library(dict_snowball MODULE ${snowball_SRCS})
+target_link_libraries(dict_snowball postgres)
+if (MSVC)
+	gen_def(dict_snowball)
+	target_link_libraries(dict_snowball pgcommon port)
+endif()
+set_target_properties(dict_snowball PROPERTIES PREFIX "")
+install(TARGETS dict_snowball
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+install(FILES snowball_create.sql
+		DESTINATION ${PGSHAREDIR})
+
+install(FILES
+		stopwords/danish.stop
+		stopwords/dutch.stop
+		stopwords/english.stop
+		stopwords/finnish.stop
+		stopwords/french.stop
+		stopwords/german.stop
+		stopwords/hungarian.stop
+		stopwords/italian.stop
+		stopwords/norwegian.stop
+		stopwords/portuguese.stop
+		stopwords/russian.stop
+		stopwords/spanish.stop
+		stopwords/swedish.stop
+		stopwords/turkish.stop
+		DESTINATION ${PGSHAREDIR}/tsearch_data)
diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c
index a818023..5c26bf2 100644
--- a/src/backend/utils/adt/pg_locale.c
+++ b/src/backend/utils/adt/pg_locale.c
@@ -238,7 +238,7 @@ pg_perm_setlocale(int category, const char *locale)
 
 	snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
 
-	if (putenv(envbuf))
+	if (pg_putenv_proxy(envbuf))
 		return NULL;
 
 	return result;
diff --git a/src/backend/utils/mb/conversion_procs/CMakeLists.txt b/src/backend/utils/mb/conversion_procs/CMakeLists.txt
new file mode 100644
index 0000000..008f58e
--- /dev/null
+++ b/src/backend/utils/mb/conversion_procs/CMakeLists.txt
@@ -0,0 +1,201 @@
+set(procs_list
+	ascii_and_mic
+	cyrillic_and_mic
+	euc_cn_and_mic
+	euc_jp_and_sjis
+	euc_kr_and_mic
+	euc_tw_and_big5
+	latin2_and_win1250
+	latin_and_mic
+	utf8_and_ascii
+	utf8_and_big5
+	utf8_and_cyrillic
+	utf8_and_euc_cn
+	utf8_and_euc_jp
+	utf8_and_euc_kr
+	utf8_and_euc_tw
+	utf8_and_gb18030
+	utf8_and_gbk
+	utf8_and_iso8859
+	utf8_and_iso8859_1
+	utf8_and_johab
+	utf8_and_sjis
+	utf8_and_win
+	utf8_and_uhc
+	utf8_and_euc2004
+	utf8_and_sjis2004
+	euc2004_sjis2004
+)
+
+# conversion_name source_encoding destination_encoding function object
+set(conversions_list
+	"ascii_to_mic\;SQL_ASCII\;MULE_INTERNAL\;ascii_to_mic\;ascii_and_mic"
+	"mic_to_ascii\;MULE_INTERNAL\;SQL_ASCII\;mic_to_ascii\;ascii_and_mic"
+	"koi8_r_to_mic\;KOI8R\;MULE_INTERNAL\;koi8r_to_mic\;cyrillic_and_mic"
+	"mic_to_koi8_r\;MULE_INTERNAL\;KOI8R\;mic_to_koi8r\;cyrillic_and_mic"
+	"iso_8859_5_to_mic\;ISO-8859-5\;MULE_INTERNAL\;iso_to_mic\;cyrillic_and_mic"
+	"mic_to_iso_8859_5\;MULE_INTERNAL\;ISO-8859-5\;mic_to_iso\;cyrillic_and_mic"
+	"windows_1251_to_mic\;WIN1251\;MULE_INTERNAL\;win1251_to_mic\;cyrillic_and_mic"
+	"mic_to_windows_1251\;MULE_INTERNAL\;WIN1251\;mic_to_win1251\;cyrillic_and_mic"
+	"windows_866_to_mic\;WIN866\;MULE_INTERNAL\;win866_to_mic\;cyrillic_and_mic"
+	"mic_to_windows_866\;MULE_INTERNAL\;WIN866\;mic_to_win866\;cyrillic_and_mic"
+	"koi8_r_to_windows_1251\;KOI8R\;WIN1251\;koi8r_to_win1251\;cyrillic_and_mic"
+	"windows_1251_to_koi8_r\;WIN1251\;KOI8R\;win1251_to_koi8r\;cyrillic_and_mic"
+	"koi8_r_to_windows_866\;KOI8R\;WIN866\;koi8r_to_win866\;cyrillic_and_mic"
+	"windows_866_to_koi8_r\;WIN866\;KOI8R\;win866_to_koi8r\;cyrillic_and_mic"
+	"windows_866_to_windows_1251\;WIN866\;WIN1251\;win866_to_win1251\;cyrillic_and_mic"
+	"windows_1251_to_windows_866\;WIN1251\;WIN866\;win1251_to_win866\;cyrillic_and_mic"
+	"iso_8859_5_to_koi8_r\;ISO-8859-5\;KOI8R\;iso_to_koi8r\;cyrillic_and_mic"
+	"koi8_r_to_iso_8859_5\;KOI8R\;ISO-8859-5\;koi8r_to_iso\;cyrillic_and_mic"
+	"iso_8859_5_to_windows_1251\;ISO-8859-5\;WIN1251\;iso_to_win1251\;cyrillic_and_mic"
+	"windows_1251_to_iso_8859_5\;WIN1251\;ISO-8859-5\;win1251_to_iso\;cyrillic_and_mic"
+	"iso_8859_5_to_windows_866\;ISO-8859-5\;WIN866\;iso_to_win866\;cyrillic_and_mic"
+	"windows_866_to_iso_8859_5\;WIN866\;ISO-8859-5\;win866_to_iso\;cyrillic_and_mic"
+	"euc_cn_to_mic\;EUC_CN\;MULE_INTERNAL\;euc_cn_to_mic\;euc_cn_and_mic"
+	"mic_to_euc_cn\;MULE_INTERNAL\;EUC_CN\;mic_to_euc_cn\;euc_cn_and_mic"
+	"euc_jp_to_sjis\;EUC_JP\;SJIS\;euc_jp_to_sjis\;euc_jp_and_sjis"
+	"sjis_to_euc_jp\;SJIS\;EUC_JP\;sjis_to_euc_jp\;euc_jp_and_sjis"
+	"euc_jp_to_mic\;EUC_JP\;MULE_INTERNAL\;euc_jp_to_mic\;euc_jp_and_sjis"
+	"sjis_to_mic\;SJIS\;MULE_INTERNAL\;sjis_to_mic\;euc_jp_and_sjis"
+	"mic_to_euc_jp\;MULE_INTERNAL\;EUC_JP\;mic_to_euc_jp\;euc_jp_and_sjis"
+	"mic_to_sjis\;MULE_INTERNAL\;SJIS\;mic_to_sjis\;euc_jp_and_sjis"
+	"euc_kr_to_mic\;EUC_KR\;MULE_INTERNAL\;euc_kr_to_mic\;euc_kr_and_mic"
+	"mic_to_euc_kr\;MULE_INTERNAL\;EUC_KR\;mic_to_euc_kr\;euc_kr_and_mic"
+	"euc_tw_to_big5\;EUC_TW\;BIG5\;euc_tw_to_big5\;euc_tw_and_big5"
+	"big5_to_euc_tw\;BIG5\;EUC_TW\;big5_to_euc_tw\;euc_tw_and_big5"
+	"euc_tw_to_mic\;EUC_TW\;MULE_INTERNAL\;euc_tw_to_mic\;euc_tw_and_big5"
+	"big5_to_mic\;BIG5\;MULE_INTERNAL\;big5_to_mic\;euc_tw_and_big5"
+	"mic_to_euc_tw\;MULE_INTERNAL\;EUC_TW\;mic_to_euc_tw\;euc_tw_and_big5"
+	"mic_to_big5\;MULE_INTERNAL\;BIG5\;mic_to_big5\;euc_tw_and_big5"
+	"iso_8859_2_to_mic\;LATIN2\;MULE_INTERNAL\;latin2_to_mic\;latin2_and_win1250"
+	"mic_to_iso_8859_2\;MULE_INTERNAL\;LATIN2\;mic_to_latin2\;latin2_and_win1250"
+	"windows_1250_to_mic\;WIN1250\;MULE_INTERNAL\;win1250_to_mic\;latin2_and_win1250"
+	"mic_to_windows_1250\;MULE_INTERNAL\;WIN1250\;mic_to_win1250\;latin2_and_win1250"
+	"iso_8859_2_to_windows_1250\;LATIN2\;WIN1250\;latin2_to_win1250\;latin2_and_win1250"
+	"windows_1250_to_iso_8859_2\;WIN1250\;LATIN2\;win1250_to_latin2\;latin2_and_win1250"
+	"iso_8859_1_to_mic\;LATIN1\;MULE_INTERNAL\;latin1_to_mic\;latin_and_mic"
+	"mic_to_iso_8859_1\;MULE_INTERNAL\;LATIN1\;mic_to_latin1\;latin_and_mic"
+	"iso_8859_3_to_mic\;LATIN3\;MULE_INTERNAL\;latin3_to_mic\;latin_and_mic"
+	"mic_to_iso_8859_3\;MULE_INTERNAL\;LATIN3\;mic_to_latin3\;latin_and_mic"
+	"iso_8859_4_to_mic\;LATIN4\;MULE_INTERNAL\;latin4_to_mic\;latin_and_mic"
+	"mic_to_iso_8859_4\;MULE_INTERNAL\;LATIN4\;mic_to_latin4\;latin_and_mic"
+	"ascii_to_utf8\;SQL_ASCII\;UTF8\;ascii_to_utf8\;utf8_and_ascii"
+	"utf8_to_ascii\;UTF8\;SQL_ASCII\;utf8_to_ascii\;utf8_and_ascii"
+	"big5_to_utf8\;BIG5\;UTF8\;big5_to_utf8\;utf8_and_big5"
+	"utf8_to_big5\;UTF8\;BIG5\;utf8_to_big5\;utf8_and_big5"
+	"utf8_to_koi8_r\;UTF8\;KOI8R\;utf8_to_koi8r\;utf8_and_cyrillic"
+	"koi8_r_to_utf8\;KOI8R\;UTF8\;koi8r_to_utf8\;utf8_and_cyrillic"
+	"utf8_to_koi8_u\;UTF8\;KOI8U\;utf8_to_koi8u\;utf8_and_cyrillic"
+	"koi8_u_to_utf8\;KOI8U\;UTF8\;koi8u_to_utf8\;utf8_and_cyrillic"
+	"utf8_to_windows_866\;UTF8\;WIN866\;utf8_to_win\;utf8_and_win"
+	"windows_866_to_utf8\;WIN866\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_874\;UTF8\;WIN874\;utf8_to_win\;utf8_and_win"
+	"windows_874_to_utf8\;WIN874\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1250\;UTF8\;WIN1250\;utf8_to_win\;utf8_and_win"
+	"windows_1250_to_utf8\;WIN1250\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1251\;UTF8\;WIN1251\;utf8_to_win\;utf8_and_win"
+	"windows_1251_to_utf8\;WIN1251\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1252\;UTF8\;WIN1252\;utf8_to_win\;utf8_and_win"
+	"windows_1252_to_utf8\;WIN1252\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1253\;UTF8\;WIN1253\;utf8_to_win\;utf8_and_win"
+	"windows_1253_to_utf8\;WIN1253\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1254\;UTF8\;WIN1254\;utf8_to_win\;utf8_and_win"
+	"windows_1254_to_utf8\;WIN1254\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1255\;UTF8\;WIN1255\;utf8_to_win\;utf8_and_win"
+	"windows_1255_to_utf8\;WIN1255\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1256\;UTF8\;WIN1256\;utf8_to_win\;utf8_and_win"
+	"windows_1256_to_utf8\;WIN1256\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1257\;UTF8\;WIN1257\;utf8_to_win\;utf8_and_win"
+	"windows_1257_to_utf8\;WIN1257\;UTF8\;win_to_utf8\;utf8_and_win"
+	"utf8_to_windows_1258\;UTF8\;WIN1258\;utf8_to_win\;utf8_and_win"
+	"windows_1258_to_utf8\;WIN1258\;UTF8\;win_to_utf8\;utf8_and_win"
+	"euc_cn_to_utf8\;EUC_CN\;UTF8\;euc_cn_to_utf8\;utf8_and_euc_cn"
+	"utf8_to_euc_cn\;UTF8\;EUC_CN\;utf8_to_euc_cn\;utf8_and_euc_cn"
+	"euc_jp_to_utf8\;EUC_JP\;UTF8\;euc_jp_to_utf8\;utf8_and_euc_jp"
+	"utf8_to_euc_jp\;UTF8\;EUC_JP\;utf8_to_euc_jp\;utf8_and_euc_jp"
+	"euc_kr_to_utf8\;EUC_KR\;UTF8\;euc_kr_to_utf8\;utf8_and_euc_kr"
+	"utf8_to_euc_kr\;UTF8\;EUC_KR\;utf8_to_euc_kr\;utf8_and_euc_kr"
+	"euc_tw_to_utf8\;EUC_TW\;UTF8\;euc_tw_to_utf8\;utf8_and_euc_tw"
+	"utf8_to_euc_tw\;UTF8\;EUC_TW\;utf8_to_euc_tw\;utf8_and_euc_tw"
+	"gb18030_to_utf8\;GB18030\;UTF8\;gb18030_to_utf8\;utf8_and_gb18030"
+	"utf8_to_gb18030\;UTF8\;GB18030\;utf8_to_gb18030\;utf8_and_gb18030"
+	"gbk_to_utf8\;GBK\;UTF8\;gbk_to_utf8\;utf8_and_gbk"
+	"utf8_to_gbk\;UTF8\;GBK\;utf8_to_gbk\;utf8_and_gbk"
+	"utf8_to_iso_8859_2\;UTF8\;LATIN2\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_2_to_utf8\;LATIN2\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_3\;UTF8\;LATIN3\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_3_to_utf8\;LATIN3\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_4\;UTF8\;LATIN4\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_4_to_utf8\;LATIN4\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_9\;UTF8\;LATIN5\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_9_to_utf8\;LATIN5\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_10\;UTF8\;LATIN6\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_10_to_utf8\;LATIN6\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_13\;UTF8\;LATIN7\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_13_to_utf8\;LATIN7\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_14\;UTF8\;LATIN8\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_14_to_utf8\;LATIN8\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_15\;UTF8\;LATIN9\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_15_to_utf8\;LATIN9\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_16\;UTF8\;LATIN10\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_16_to_utf8\;LATIN10\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_5\;UTF8\;ISO-8859-5\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_5_to_utf8\;ISO-8859-5\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_6\;UTF8\;ISO-8859-6\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_6_to_utf8\;ISO-8859-6\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_7\;UTF8\;ISO-8859-7\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_7_to_utf8\;ISO-8859-7\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"utf8_to_iso_8859_8\;UTF8\;ISO-8859-8\;utf8_to_iso8859\;utf8_and_iso8859"
+	"iso_8859_8_to_utf8\;ISO-8859-8\;UTF8\;iso8859_to_utf8\;utf8_and_iso8859"
+	"iso_8859_1_to_utf8\;LATIN1\;UTF8\;iso8859_1_to_utf8\;utf8_and_iso8859_1"
+	"utf8_to_iso_8859_1\;UTF8\;LATIN1\;utf8_to_iso8859_1\;utf8_and_iso8859_1"
+	"johab_to_utf8\;JOHAB\;UTF8\;johab_to_utf8\;utf8_and_johab"
+	"utf8_to_johab\;UTF8\;JOHAB\;utf8_to_johab\;utf8_and_johab"
+	"sjis_to_utf8\;SJIS\;UTF8\;sjis_to_utf8\;utf8_and_sjis"
+	"utf8_to_sjis\;UTF8\;SJIS\;utf8_to_sjis\;utf8_and_sjis"
+	"uhc_to_utf8\;UHC\;UTF8\;uhc_to_utf8\;utf8_and_uhc"
+	"utf8_to_uhc\;UTF8\;UHC\;utf8_to_uhc\;utf8_and_uhc"
+	"euc_jis_2004_to_utf8\;EUC_JIS_2004\;UTF8\;euc_jis_2004_to_utf8\;utf8_and_euc2004"
+	"utf8_to_euc_jis_2004\;UTF8\;EUC_JIS_2004\;utf8_to_euc_jis_2004\;utf8_and_euc2004"
+	"shift_jis_2004_to_utf8\;SHIFT_JIS_2004\;UTF8\;shift_jis_2004_to_utf8\;utf8_and_sjis2004"
+	"utf8_to_shift_jis_2004\;UTF8\;SHIFT_JIS_2004\;utf8_to_shift_jis_2004\;utf8_and_sjis2004"
+	"euc_jis_2004_to_shift_jis_2004\;EUC_JIS_2004\;SHIFT_JIS_2004\;euc_jis_2004_to_shift_jis_2004\;euc2004_sjis2004"
+	"shift_jis_2004_to_euc_jis_2004\;SHIFT_JIS_2004\;EUC_JIS_2004\;shift_jis_2004_to_euc_jis_2004\;euc2004_sjis2004"
+)
+
+file(WRITE "conversion_create.sql" "")
+foreach(pair_conversion ${conversions_list})
+	LIST(GET pair_conversion 0 con_name)
+	LIST(GET pair_conversion 1 se)
+	LIST(GET pair_conversion 2 de)
+	LIST(GET pair_conversion 3 func)
+	LIST(GET pair_conversion 4 obj)
+	
+	file(APPEND "conversion_create.sql" "-- ${se} --> ${de}\n")
+	file(APPEND "conversion_create.sql" "CREATE OR REPLACE FUNCTION ${func} (INTEGER, INTEGER, CSTRING, INTERNAL, INTEGER) RETURNS VOID AS '$libdir/${obj}', '${func}' LANGUAGE C STRICT;\n")
+	file(APPEND "conversion_create.sql" "COMMENT ON FUNCTION ${func}(INTEGER, INTEGER, CSTRING, INTERNAL, INTEGER) IS 'internal conversion function for ${se} to ${de}';\n")
+	file(APPEND "conversion_create.sql" "DROP CONVERSION pg_catalog.${con_name};\n")
+	file(APPEND "conversion_create.sql" "CREATE DEFAULT CONVERSION pg_catalog.${con_name} FOR '${se}' TO '${de}' FROM ${func};\n")
+	file(APPEND "conversion_create.sql" "COMMENT ON CONVERSION pg_catalog.${con_name} IS 'conversion for ${se} to ${de}';\n")
+endforeach()
+
+foreach(loop_var IN ITEMS ${procs_list})
+	if(loop_var STREQUAL "euc_tw_and_big5")
+		add_library(${loop_var} MODULE ${loop_var}/${loop_var}.c ${loop_var}/big5.c)
+	else()
+		add_library(${loop_var} MODULE ${loop_var}/${loop_var}.c)
+	endif()
+	set_target_properties(${loop_var} PROPERTIES PREFIX "")
+	add_dependencies(${loop_var} gen_errorcodes)
+	target_link_libraries(${loop_var} postgres)
+	if(MSVC)
+		gen_def(${loop_var})
+	endif()
+	CMAKE_SET_TARGET_FOLDER(${loop_var} conversion_procs)
+	install(TARGETS ${loop_var}
+			RUNTIME DESTINATION ${PGBINDIR}
+			LIBRARY DESTINATION ${LIBDIR})
+endforeach(loop_var)
+
+install(FILES conversion_create.sql
+		DESTINATION ${PGSHAREDIR})
diff --git a/src/bin/initdb/CMakeLists.txt b/src/bin/initdb/CMakeLists.txt
new file mode 100644
index 0000000..52c3272
--- /dev/null
+++ b/src/bin/initdb/CMakeLists.txt
@@ -0,0 +1,35 @@
+include_directories(BEFORE
+ 	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/timezone"
+)
+
+add_executable(initdb
+	initdb.c
+	findtimezone.c
+	"${PROJECT_SOURCE_DIR}/src/timezone/localtime.c"
+	"${PROJECT_SOURCE_DIR}/src/backend/utils/mb/encnames.c"
+)
+
+target_link_libraries(initdb
+	pgfeutils
+	pgcommon
+	port
+	pq
+	${GSS_LIBS}
+)
+
+if(UNIX AND NOT APPLE)
+	target_link_libraries(initdb rt)
+endif()
+
+target_compile_definitions(initdb PRIVATE -DFRONTEND)
+CMAKE_SET_TARGET_FOLDER(initdb bin)
+
+install(TARGETS initdb
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(initdb "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;sv;zh_CN")
+endif()
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 3350e13..3ed89dc 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1496,10 +1496,10 @@ bootstrap_template1(void)
 	 * there doesn't seem to be any compelling reason to do that.
 	 */
 	snprintf(cmd, sizeof(cmd), "LC_COLLATE=%s", lc_collate);
-	putenv(pg_strdup(cmd));
+	pg_putenv_proxy(pg_strdup(cmd));
 
 	snprintf(cmd, sizeof(cmd), "LC_CTYPE=%s", lc_ctype);
-	putenv(pg_strdup(cmd));
+	pg_putenv_proxy(pg_strdup(cmd));
 
 	unsetenv("LC_ALL");
 
@@ -2757,7 +2757,7 @@ setup_pgdata(void)
 	 * have embedded spaces.
 	 */
 	pgdata_set_env = psprintf("PGDATA=%s", pg_data);
-	putenv(pgdata_set_env);
+	pg_putenv_proxy(pgdata_set_env);
 }
 
 
diff --git a/src/bin/pg_archivecleanup/CMakeLists.txt b/src/bin/pg_archivecleanup/CMakeLists.txt
new file mode 100644
index 0000000..7c79ac3
--- /dev/null
+++ b/src/bin/pg_archivecleanup/CMakeLists.txt
@@ -0,0 +1,20 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_archivecleanup pg_archivecleanup.c)
+
+target_link_libraries(pg_archivecleanup
+	port
+#	pq
+	pgcommon
+	${M_LIB}
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_archivecleanup bin)
+
+install(TARGETS pg_archivecleanup
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/bin/pg_basebackup/CMakeLists.txt b/src/bin/pg_basebackup/CMakeLists.txt
new file mode 100644
index 0000000..a985e08
--- /dev/null
+++ b/src/bin/pg_basebackup/CMakeLists.txt
@@ -0,0 +1,51 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+	${ZLIB_INCLUDE_DIRS}
+)
+if(ZLIB_FOUND)
+	include_directories(BEFORE ${ZLIB_INCLUDE_DIRS})
+endif()
+
+
+add_library(common_basebackup STATIC
+	receivelog.c
+	streamutil.c
+)
+
+target_link_libraries(common_basebackup
+	port
+	pq
+)
+
+set(basebackup_list 
+	pg_basebackup
+	pg_receivexlog
+	pg_recvlogical
+)
+
+foreach(loop_var IN ITEMS ${basebackup_list})
+	add_executable(${loop_var} ${loop_var}.c)
+
+	target_link_libraries(${loop_var}
+		pgfeutils
+		port
+		pq
+		pgcommon
+		${M_LIB}
+		common_basebackup
+	)
+	CMAKE_SET_TARGET_FOLDER(${loop_var} bin)
+	if(ZLIB_FOUND)
+		target_link_libraries(${loop_var} ${ZLIB_LIBRARIES})
+	endif()
+endforeach()
+
+install(TARGETS ${basebackup_list}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_basebackup "de;es;fr;it;ko;pl;pt_BR;ru;zh_CN")
+endif()
diff --git a/src/bin/pg_config/CMakeLists.txt b/src/bin/pg_config/CMakeLists.txt
new file mode 100644
index 0000000..c9cb6e4
--- /dev/null
+++ b/src/bin/pg_config/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_config
+	pg_config.c
+)
+
+target_link_libraries(pg_config
+	pgcommon
+	port
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_config bin)
+
+install(TARGETS pg_config
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_config "cs;de;es;fr;it;ja;ko;nb;pl;pt_BR;ro;ru;sv;ta;tr;zh_CN;zh_TW")
+endif()
diff --git a/src/bin/pg_controldata/CMakeLists.txt b/src/bin/pg_controldata/CMakeLists.txt
new file mode 100644
index 0000000..70a673c
--- /dev/null
+++ b/src/bin/pg_controldata/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_controldata
+	pg_controldata.c
+)
+
+target_link_libraries(pg_controldata
+	pgcommon
+	port
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_controldata bin)
+
+install(TARGETS pg_controldata
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_controldata "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;sv;zh_CN")
+endif()
diff --git a/src/bin/pg_ctl/CMakeLists.txt b/src/bin/pg_ctl/CMakeLists.txt
new file mode 100644
index 0000000..8636d38
--- /dev/null
+++ b/src/bin/pg_ctl/CMakeLists.txt
@@ -0,0 +1,25 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_ctl
+	pg_ctl.c
+)
+
+target_link_libraries(pg_ctl
+	pgcommon
+	port
+	pq
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_ctl bin)
+
+install(TARGETS pg_ctl
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_ctl "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;sv;zh_CN")
+endif()
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index efc0729..7715081 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -902,7 +902,7 @@ do_start(void)
 
 		snprintf(env_var, sizeof(env_var), "PG_GRANDPARENT_PID=%d",
 				 (int) getppid());
-		putenv(env_var);
+		pg_putenv_proxy(env_var);
 	}
 #endif
 
@@ -2208,7 +2208,7 @@ main(int argc, char **argv)
 						pgdata_D = pg_strdup(optarg);
 						canonicalize_path(pgdata_D);
 						env_var = psprintf("PGDATA=%s", pgdata_D);
-						putenv(env_var);
+						pg_putenv_proxy(env_var);
 
 						/*
 						 * We could pass PGDATA just in an environment
diff --git a/src/bin/pg_dump/CMakeLists.txt b/src/bin/pg_dump/CMakeLists.txt
new file mode 100644
index 0000000..e0da845
--- /dev/null
+++ b/src/bin/pg_dump/CMakeLists.txt
@@ -0,0 +1,88 @@
+include_directories(BEFORE
+ 	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+	${ZLIB_INCLUDE_DIRS}
+)
+
+if(ZLIB_FOUND)
+	include_directories(BEFORE ${ZLIB_INCLUDE_DIRS})
+endif()
+
+add_library(libdump STATIC
+	pg_backup_archiver.c
+	pg_backup_db.c
+	pg_backup_custom.c
+	pg_backup_null.c
+	pg_backup_tar.c
+	pg_backup_directory.c
+	pg_backup_utils.c
+	parallel.c
+	compress_io.c
+	dumputils.c
+)
+
+add_executable(pg_dump
+	pg_dump.c
+	common.c
+	pg_dump_sort.c
+)
+
+target_link_libraries(pg_dump
+	libdump
+	pgfeutils
+	pgcommon
+	port
+	pq
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_dump bin)
+
+if(ZLIB_FOUND)
+	target_link_libraries(pg_dump ${ZLIB_LIBRARIES})
+endif()
+
+add_executable(pg_restore
+	pg_restore.c
+)
+
+target_link_libraries(pg_restore
+	libdump
+	pgfeutils
+	pgcommon
+	port
+	pq
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_restore bin)
+
+if(ZLIB_FOUND)
+	target_link_libraries(pg_restore ${ZLIB_LIBRARIES})
+endif()
+
+add_executable(pg_dumpall
+	pg_dumpall.c
+	dumputils.c
+)
+
+target_link_libraries(pg_dumpall
+	libdump
+	pgfeutils
+	pgcommon
+	port
+	pq
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_dumpall bin)
+
+if(ZLIB_FOUND)
+	target_link_libraries(pg_dumpall ${ZLIB_LIBRARIES})
+endif()
+
+install(TARGETS pg_dump pg_restore pg_dumpall
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_dump "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;zh_CN")
+endif()
diff --git a/src/bin/pg_resetxlog/CMakeLists.txt b/src/bin/pg_resetxlog/CMakeLists.txt
new file mode 100644
index 0000000..e0d34ea
--- /dev/null
+++ b/src/bin/pg_resetxlog/CMakeLists.txt
@@ -0,0 +1,24 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_resetxlog
+	pg_resetxlog.c
+)
+
+target_link_libraries(pg_resetxlog
+	pgcommon
+	port
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_resetxlog bin)
+
+install(TARGETS pg_resetxlog
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_resetxlog "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;sv;zh_CN")
+endif()
diff --git a/src/bin/pg_rewind/CMakeLists.txt b/src/bin/pg_rewind/CMakeLists.txt
new file mode 100644
index 0000000..a81091f
--- /dev/null
+++ b/src/bin/pg_rewind/CMakeLists.txt
@@ -0,0 +1,37 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_rewind
+	pg_rewind.c
+	parsexlog.c
+	datapagemap.c
+	timeline.c
+	fetch.c
+	file_ops.c
+	copy_fetch.c
+	libpq_fetch.c
+	filemap.c
+	logging.c
+	${PROJECT_SOURCE_DIR}/src/backend/access/transam/xlogreader.c
+)
+
+target_link_libraries(pg_rewind
+	pgcommon
+	port
+	pq
+)
+
+target_compile_definitions(pg_rewind PRIVATE -DFRONTEND)
+
+CMAKE_SET_TARGET_FOLDER(pg_rewind bin)
+
+install(TARGETS pg_rewind
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pg_rewind "de;es;fr;it;ko;pl;ru;zh_CN")
+endif()
diff --git a/src/bin/pg_test_fsync/CMakeLists.txt b/src/bin/pg_test_fsync/CMakeLists.txt
new file mode 100644
index 0000000..4509380
--- /dev/null
+++ b/src/bin/pg_test_fsync/CMakeLists.txt
@@ -0,0 +1,22 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_test_fsync
+	pg_test_fsync.c
+	
+)
+
+target_link_libraries(pg_test_fsync
+	pgcommon
+	port
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_test_fsync bin)
+
+install(TARGETS pg_test_fsync
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
diff --git a/src/bin/pg_test_timing/CMakeLists.txt b/src/bin/pg_test_timing/CMakeLists.txt
new file mode 100644
index 0000000..dce9232
--- /dev/null
+++ b/src/bin/pg_test_timing/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_test_timing
+	pg_test_timing.c
+	
+)
+
+target_link_libraries(pg_test_timing
+	pgcommon
+	port
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_test_timing bin)
+
+install(TARGETS pg_test_timing
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+
diff --git a/src/bin/pg_upgrade/CMakeLists.txt b/src/bin/pg_upgrade/CMakeLists.txt
new file mode 100644
index 0000000..cd9a902
--- /dev/null
+++ b/src/bin/pg_upgrade/CMakeLists.txt
@@ -0,0 +1,39 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_upgrade
+	check.c
+	controldata.c
+	dump.c
+	exec.c
+	file.c
+	function.c
+	info.c
+	option.c
+	parallel.c
+	pg_upgrade.c
+	relfilenode.c
+	server.c
+	tablespace.c
+	util.c
+	version.c 
+)
+
+target_link_libraries(pg_upgrade
+	pgfeutils
+	pgcommon
+	port
+	pq
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_upgrade bin)
+
+install(TARGETS pg_upgrade
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+
+
diff --git a/src/bin/pg_upgrade/util.c b/src/bin/pg_upgrade/util.c
index 5b24ec0..3bf49fd 100644
--- a/src/bin/pg_upgrade/util.c
+++ b/src/bin/pg_upgrade/util.c
@@ -274,7 +274,7 @@ pg_putenv(const char *var, const char *val)
 		char	   *envstr;
 
 		envstr = psprintf("%s=%s", var, val);
-		putenv(envstr);
+		pg_putenv_proxy(envstr);
 
 		/*
 		 * Do not free envstr because it becomes part of the environment on
diff --git a/src/bin/pg_xlogdump/CMakeLists.txt b/src/bin/pg_xlogdump/CMakeLists.txt
new file mode 100644
index 0000000..f8b45a1
--- /dev/null
+++ b/src/bin/pg_xlogdump/CMakeLists.txt
@@ -0,0 +1,27 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_xlogdump
+	${PROJECT_SOURCE_DIR}/src/backend/access/transam/xlogreader.c
+	pg_xlogdump.c
+	compat.c
+	rmgrdesc.c
+	${rmgrdesc_SRC}
+)
+
+target_link_libraries(pg_xlogdump
+	pgcommon
+	port
+	pq
+)
+
+target_compile_definitions(pg_xlogdump PRIVATE -DFRONTEND)
+
+CMAKE_SET_TARGET_FOLDER(pg_xlogdump bin)
+
+install(TARGETS pg_xlogdump
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/bin/pgbench/CMakeLists.txt b/src/bin/pgbench/CMakeLists.txt
new file mode 100644
index 0000000..b10af2c
--- /dev/null
+++ b/src/bin/pgbench/CMakeLists.txt
@@ -0,0 +1,32 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+bison_target(PGBENCH_PARSER exprparse.y ${PROJECT_SOURCE_DIR}/src/bin/pgbench/exprparse.c)
+flex_target(PGBENCH_SCANNER exprscan.l  ${PROJECT_SOURCE_DIR}/src/bin/pgbench/exprscan.c)
+add_flex_bison_dependency(PGBENCH_SCANNER PGBENCH_PARSER)
+
+set_source_files_properties(exprparse.c PROPERTIES OBJECT_DEPENDS ${FLEX_PGBENCH_SCANNER_OUTPUTS})
+
+add_executable(pgbench
+	pgbench.c
+	${BISON_PGBENCH_PARSER_OUTPUTS}
+)
+
+target_link_libraries(pgbench
+	pgfeutils
+	port
+	pq
+	pgcommon
+	${M_LIB}
+)
+
+add_dependencies(pgbench postgres)
+
+CMAKE_SET_TARGET_FOLDER(pgbench bin)
+
+install(TARGETS pgbench
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/bin/psql/CMakeLists.txt b/src/bin/psql/CMakeLists.txt
new file mode 100644
index 0000000..ceccf23
--- /dev/null
+++ b/src/bin/psql/CMakeLists.txt
@@ -0,0 +1,72 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_custom_command(
+	MAIN_DEPENDENCY create_help.pl
+	OUTPUT ${PROJECT_SOURCE_DIR}/src/bin/psql/sql_help.c ${PROJECT_SOURCE_DIR}/src/bin/psql/sql_help.h
+	COMMAND ${PERL_EXECUTABLE} create_help.pl ../../../doc/src/sgml/ref sql_help
+	WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/src/bin/psql
+)
+
+flex_target(SCANSLASH_SCAN
+	psqlscanslash.l
+	${CMAKE_CURRENT_SOURCE_DIR}/psqlscanslash.c
+)
+
+set(psql_SRCS
+	command.c
+	common.c
+	help.c
+	help.h
+	input.c
+	stringutils.c
+	mainloop.c
+	copy.c
+	copy.h
+	startup.c
+	prompt.c
+	variables.c
+	large_obj.c
+	describe.c
+	tab-complete.c
+	../pg_dump/dumputils.c
+	sql_help.c
+	crosstabview.c
+	${FLEX_SCANSLASH_SCAN_OUTPUTS}
+)
+
+add_executable(psql
+	${psql_SRCS}
+)
+
+target_link_libraries(psql
+	pgfeutils
+	pgcommon
+	port
+	pq
+	${READLINE_LIBRARY}
+	${CMAKE_THREAD_LIBS_INIT}
+)
+
+if(OPENSSL_FOUND)
+	target_link_libraries(psql ${OPENSSL_LIBRARIES})
+endif()
+if(NOT MSVC)
+	target_link_libraries(psql m)
+endif()
+
+add_dependencies(psql postgres)
+
+CMAKE_SET_TARGET_FOLDER(psql bin)
+
+install(TARGETS psql
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pslq "cs;de;es;fr;it;ja;pl;pt_BR;ru;zh_CN;zh_TW")
+endif()
+
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index a9a2fdb..a9e6dfa 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1350,7 +1350,7 @@ exec_command(const char *cmd,
 			char	   *newval;
 
 			newval = psprintf("%s=%s", envvar, envval);
-			putenv(newval);
+			pg_putenv_proxy(newval);
 			success = true;
 
 			/*
diff --git a/src/bin/scripts/CMakeLists.txt b/src/bin/scripts/CMakeLists.txt
new file mode 100644
index 0000000..c06548a
--- /dev/null
+++ b/src/bin/scripts/CMakeLists.txt
@@ -0,0 +1,89 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+	"${PROJECT_SOURCE_DIR}/src/bin/psql"
+)
+
+add_library(common_dump STATIC
+	common.c
+	${PROJECT_SOURCE_DIR}/src/bin/pg_dump/dumputils.c
+)
+
+add_library(common_print STATIC
+	common.c
+)
+
+set(common_libs
+	pgfeutils
+	pgcommon
+	port
+	pq
+	${LIB_M}
+)
+
+add_executable(createlang
+	createlang.c
+)
+
+target_link_libraries(createlang
+	common_print
+	${common_libs}
+)
+
+CMAKE_SET_TARGET_FOLDER(createlang bin)
+
+add_executable(droplang
+	droplang.c
+)
+
+target_link_libraries(droplang
+	common_print
+	${common_libs}
+)
+
+CMAKE_SET_TARGET_FOLDER(droplang bin)
+
+add_executable(pg_isready
+	pg_isready.c
+	common.c
+)
+
+target_link_libraries(pg_isready
+	${common_libs}
+)
+
+CMAKE_SET_TARGET_FOLDER(pg_isready bin)
+
+set(dump_modules_list 
+	createdb
+	createuser
+	dropdb
+	dropuser
+	clusterdb
+	vacuumdb
+	reindexdb
+)
+
+foreach(loop_var IN ITEMS ${dump_modules_list})
+	add_executable(${loop_var}
+		${loop_var}.c
+		${dump_parser_src}
+	)
+
+	target_link_libraries(${loop_var}
+		common_dump
+		${common_libs}
+	)
+
+	CMAKE_SET_TARGET_FOLDER(${loop_var} bin)
+endforeach()
+
+
+install(TARGETS ${dump_modules_list} createlang droplang pg_isready
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(pgscripts "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;sv;zh_CN")
+endif()
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
new file mode 100644
index 0000000..7a24165
--- /dev/null
+++ b/src/common/CMakeLists.txt
@@ -0,0 +1,48 @@
+set(pgcommon_srv_SRCS
+	exec.c
+	pg_lzcompress.c
+	pgfnames.c
+	psprintf.c
+	relpath.c
+	rmtree.c
+	string.c
+	username.c
+	wait_error.c
+	controldata_utils.c
+	config_info.c
+	keywords.c
+	md5.c
+	ip.c
+)
+
+set(pgcommon_SRCS
+	${pgcommon_srv_SRCS}
+	fe_memutils.c
+	restricted_token.c
+	${PROJECT_SOURCE_DIR}/src/include/parser/gram.h
+)
+
+add_library(pgcommon_srv STATIC ${pgcommon_srv_SRCS})
+add_library(pgcommon STATIC ${pgcommon_SRCS})
+
+set_source_files_properties(
+	${PROJECT_SOURCE_DIR}/src/include/parser/gram.h
+	PROPERTIES GENERATED TRUE
+)
+
+add_dependencies(pgcommon_srv generate_parser_gram_h)
+add_dependencies(pgcommon_srv gen_errorcodes)
+add_dependencies(pgcommon gen_errorcodes)
+
+target_compile_definitions(pgcommon PRIVATE -DFRONTEND)
+
+target_compile_definitions(pgcommon PRIVATE -DVAL_CPPFLAGS="${CMAKE_CXX_FLAGS}")
+target_compile_definitions(pgcommon PRIVATE -DVAL_CFLAGS="${CMAKE_C_FLAGS} ${COMPILE_DEFINITIONS}")
+target_compile_definitions(pgcommon PRIVATE -DVAL_CC="${CMAKE_C_COMPILER}")
+target_compile_definitions(pgcommon PRIVATE -DVAL_LDFLAGS="${CMAKE_SHARED_LINKER_FLAGS}")
+
+# because port used in shared libraries
+set_property(TARGET pgcommon PROPERTY POSITION_INDEPENDENT_CODE TRUE)
+
+install(TARGETS pgcommon
+		ARCHIVE DESTINATION ${LIBDIR})
diff --git a/src/common/exec.c b/src/common/exec.c
index a2de071..df2b675 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -586,7 +586,7 @@ set_pglocale_pgservice(const char *argv0, const char *app)
 		canonicalize_path(env_path + 12);
 		dup_path = strdup(env_path);
 		if (dup_path)
-			putenv(dup_path);
+			pg_putenv_proxy(dup_path);
 	}
 #endif
 
@@ -599,7 +599,7 @@ set_pglocale_pgservice(const char *argv0, const char *app)
 		canonicalize_path(env_path + 13);
 		dup_path = strdup(env_path);
 		if (dup_path)
-			putenv(dup_path);
+			pg_putenv_proxy(dup_path);
 	}
 }
 
diff --git a/src/common/restricted_token.c b/src/common/restricted_token.c
index d155b5e..2f3b429 100644
--- a/src/common/restricted_token.c
+++ b/src/common/restricted_token.c
@@ -163,7 +163,7 @@ get_restricted_token(const char *progname)
 
 		cmdline = pg_strdup(GetCommandLine());
 
-		putenv("PG_RESTRICT_EXEC=1");
+		pg_putenv_proxy("PG_RESTRICT_EXEC=1");
 
 		if ((restrictedToken = CreateRestrictedProcess(cmdline, &pi, progname)) == 0)
 		{
diff --git a/src/fe_utils/CMakeLists.txt b/src/fe_utils/CMakeLists.txt
new file mode 100644
index 0000000..6bf3fd3
--- /dev/null
+++ b/src/fe_utils/CMakeLists.txt
@@ -0,0 +1,23 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+	"${PROJECT_SOURCE_DIR}/src/bin/psql"
+)
+
+set(psqlscan_flags, "-Cfe -p -p")
+
+flex_target(psqlscan
+	psqlscan.l
+	"${CMAKE_CURRENT_SOURCE_DIR}/psqlscan.c"
+	${psqlscan_flags}
+)
+
+add_library(pgfeutils STATIC
+	mbprint.c
+	print.c
+	psqlscan.c
+	simple_list.c
+	string_utils.c
+	${FLEX_psqlscan_OUTPUTS}
+)
diff --git a/src/include/CMakeLists.txt b/src/include/CMakeLists.txt
new file mode 100644
index 0000000..c084822
--- /dev/null
+++ b/src/include/CMakeLists.txt
@@ -0,0 +1,78 @@
+set(INCLUDEDIR_INTERNAL ${PKGINCLUDEDIR}/internal)
+
+# These headers are needed by the public headers of the interfaces.
+install(FILES
+	postgres_ext.h
+	pg_config.h
+	pg_config_ext.h
+	pg_config_os.h
+	pg_config_manual.h
+	DESTINATION ${INCLUDEDIR})
+install(FILES
+	libpq/libpq-fs.h
+	DESTINATION ${INCLUDEDIR}/libpq)
+
+# These headers are needed by the not-so-public headers of the interfaces.
+install(FILES
+	c.h
+	port.h
+	postgres_fe.h
+	DESTINATION ${INCLUDEDIR_INTERNAL})
+install(FILES
+	libpq/pqcomm.h
+	DESTINATION ${INCLUDEDIR_INTERNAL}/libpq)
+
+# These headers are needed for server-side development
+set(SUBDIRS
+	access
+	bootstrap
+	catalog
+	commands
+	common
+	datatype
+	executor
+	foreign
+	lib
+	libpq
+	mb
+	nodes
+	optimizer
+	parser
+	postmaster
+	regex
+	replication
+	rewrite
+	storage
+	tcop
+	snowball
+	tsearch
+	utils
+	port
+	portability
+)
+
+install(FILES
+	c.h
+	dynloader.h
+	fmgr.h
+	funcapi.h
+	getaddrinfo.h
+	getopt_long.h
+	miscadmin.h
+	pg_config_ext.h
+	pg_config.h
+	pg_config_manual.h
+	pg_config_os.h
+	pg_getopt.h
+	pgstat.h
+	pgtar.h
+	pgtime.h
+	pg_trace.h
+	port.h
+	postgres_ext.h
+	postgres_fe.h
+	postgres.h
+	rusagestub.h
+	windowapi.h
+	DESTINATION ${INCLUDEDIRSERVER})
+install(DIRECTORY ${SUBDIRS} DESTINATION ${INCLUDEDIRSERVER})
diff --git a/src/include/c.h b/src/include/c.h
index 4ab3f80..1299e5c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -317,8 +317,7 @@ typedef unsigned long long int uint64;
  *		There currently is only a limited support for the type. E.g. 128bit
  *		literals and snprintf are not supported; but math is.
  */
-#if defined(PG_INT128_TYPE)
-#define HAVE_INT128
+#if defined(HAVE_INT128)
 typedef PG_INT128_TYPE int128;
 typedef unsigned PG_INT128_TYPE uint128;
 #endif
diff --git a/src/include/pg_config_cmake.in b/src/include/pg_config_cmake.in
new file mode 100644
index 0000000..48685ae
--- /dev/null
+++ b/src/include/pg_config_cmake.in
@@ -0,0 +1,930 @@
+#ifndef AUTO_CONFIG_H
+#define AUTO_CONFIG_H
+
+/* Define to the type of arg 1 of 'accept' */
+#define ACCEPT_TYPE_ARG1 @ACCEPT_TYPE_ARG1@
+
+/* Define to the type of arg 2 of 'accept' */
+#define ACCEPT_TYPE_ARG2 @ACCEPT_TYPE_ARG2@
+
+/* Define to the type of arg 3 of 'accept' */
+#define ACCEPT_TYPE_ARG3 @ACCEPT_TYPE_ARG3@
+
+/* Define to the return type of 'accept' */
+#define ACCEPT_TYPE_RETURN @ACCEPT_TYPE_RETURN@
+
+/* The normal alignment of `double', in bytes. */
+#define ALIGNOF_DOUBLE @ALIGNOF_DOUBLE@
+
+/* The normal alignment of `int', in bytes. */
+#define ALIGNOF_INT @ALIGNOF_INT@
+
+/* The normal alignment of `long', in bytes. */
+#define ALIGNOF_LONG @ALIGNOF_LONG@
+
+/* The normal alignment of `long long int', in bytes. */
+#define ALIGNOF_LONG_LONG_INT @ALIGNOF_LONG_LONG_INT@
+
+/* The normal alignment of `short', in bytes. */
+#define ALIGNOF_SHORT @ALIGNOF_SHORT@
+
+/* Size of a disk block --- this also limits the size of a tuple. You can set
+   it bigger if you need bigger tuples (although TOAST should reduce the need
+   to have large tuples, since fields can be spread across multiple tuples).
+   BLCKSZ must be a power of 2. The maximum possible value of BLCKSZ is
+   currently 2^15 (32768). This is determined by the 15-bit widths of the
+   lp_off and lp_len fields in ItemIdData (see include/storage/itemid.h).
+   Changing BLCKSZ requires an initdb. */
+#define BLCKSZ @BLCKSZ@
+
+/* Define to the default TCP port number on which the server listens and to
+   which clients will try to connect. This can be overridden at run-time, but
+   it's convenient if your clients have the right default compiled in.
+   (-DPGPORT=PORTNUM) */
+#define DEF_PGPORT @PGPORT@
+
+/* Define to the default TCP port number as a string constant. */
+#define DEF_PGPORT_STR "@PGPORT@"
+
+/* Define to build with GSSAPI support. */
+#cmakedefine ENABLE_GSS
+
+#cmakedefine ENABLE_NLS
+
+#cmakedefine ENABLE_THREAD_SAFETY 1
+
+/* Define to nothing if C supports flexible array members, and to 1 if it does
+   not. That way, with a declaration like `struct s { int n; double
+   d[FLEXIBLE_ARRAY_MEMBER]; };', the struct hack can be used with pre-C99
+   compilers. When computing the size of such an object, don't use 'sizeof
+   (struct s)' as it overestimates the size. Use 'offsetof (struct s, d)'
+   instead. Don't use 'offsetof (struct s, d[0])', as this doesn't work with
+   MSVC and with C++ compilers. */
+#define FLEXIBLE_ARRAY_MEMBER @FLEXIBLE_ARRAY_MEMBER@
+
+/* float4 values are passed by value if 'true', by reference if 'false' */
+#define FLOAT4PASSBYVAL @FLOAT4PASSBYVAL@
+
+/* float8, int8, and related values are passed by value if 'true', by
+   reference if 'false' */
+#define FLOAT8PASSBYVAL @FLOAT8PASSBYVAL@
+
+#cmakedefine GETTIMEOFDAY_1ARG
+
+#ifdef GETTIMEOFDAY_1ARG
+#	define gettimeofday(a,b) gettimeofday(a)
+#endif
+
+/* Define if you have the `append_history' function. */
+#cmakedefine HAVE_APPEND_HISTORY
+
+/* Define if you want to use atomics if available. */
+#cmakedefine HAVE_ATOMICS
+
+/* Define to 1 if you have the <atomic.h> header file. */
+#cmakedefine HAVE_ATOMIC_H 1
+
+/* Define if you have the `cbrt' function. */
+#cmakedefine HAVE_CBRT 1
+
+/* Define to 1 if you have the `class' function. */
+#cmakedefine HAVE_CLASS 1
+
+/* Define to 1 if you have the <crtdefs.h> header file. */
+#cmakedefine HAVE_CRTDEFS_H 1
+
+/* Define if you have the `crypt' function. */
+#cmakedefine HAVE_CRYPT
+
+/* Define if you have the <crypt.h> header file. */
+#cmakedefine HAVE_CRYPT_H
+
+/* Define to 1 if you have the declaration of `fdatasync' */
+#cmakedefine HAVE_DECL_FDATASYNC 1
+
+/* Define to 1 if you have the declaration of `F_FULLFSYNC' */
+#cmakedefine HAVE_DECL_F_FULLFSYNC 1
+
+/* Define to 1 if you have the declaration of `posix_fadvise' */
+#cmakedefine HAVE_DECL_POSIX_FADVISE 1
+
+/* Define to 1 if you have the declaration of `snprintf', and to 0 if you
+   don't. */
+#define HAVE_DECL_SNPRINTF @HAVE_DECL_SNPRINTF@
+
+/* Define to 1 if you have the declaration of `strlcat', and to 0 if you
+   don't. */
+#define HAVE_DECL_STRLCAT @HAVE_DECL_STRLCAT@
+
+/* Define to 1 if you have the declaration of `strlcpy', and to 0 if you
+   don't. */
+#define HAVE_DECL_STRLCPY @HAVE_DECL_STRLCPY@
+
+/* Define to 1 if you have the declaration of `sys_siglist', and to 0 if you
+   don't. */
+#cmakedefine HAVE_DECL_SYS_SIGLIST 1
+
+/* Define to 1 if you have the declaration of `vsnprintf', and to 0 if you
+   don't. */
+#define HAVE_DECL_VSNPRINTF @HAVE_DECL_VSNPRINTF@
+
+/* Define to 1 if you have the <dld.h> header file. */
+#cmakedefine HAVE_DLD_H
+
+/* Define to 1 if you have the `dlopen' function. */
+#cmakedefine HAVE_DLOPEN 1
+
+/* Define to 1 if you have the <editline/history.h> header file. */
+#cmakedefine HAVE_EDITLINE_HISTORY_H
+
+/* Define to 1 if you have the <editline/readline.h> header file. */
+#cmakedefine HAVE_EDITLINE_READLINE_H
+
+/* Define to 1 if you have the `fdatasync' function. */
+#cmakedefine HAVE_FDATASYNC 1
+
+/* Define to 1 if you have the `fls' function. */
+#cmakedefine HAVE_FLS
+
+/* Define to 1 if you have the `fpclass' function. */
+#cmakedefine HAVE_FPCLASS 1
+
+/* Define to 1 if you have the `fp_class' function. */
+#cmakedefine HAVE_FP_CLASS 1
+
+/* Define to 1 if you have the `fp_class_d' function. */
+#cmakedefine HAVE_FP_CLASS_D 1
+
+/* Define to 1 if you have the <fp_class.h> header file. */
+#cmakedefine HAVE_FP_CLASS_H 1
+
+/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
+#cmakedefine HAVE_FSEEKO 1
+
+/* Define to 1 if your compiler understands __func__. */
+#cmakedefine HAVE_FUNCNAME__FUNC
+
+/* Define to 1 if your compiler understands __FUNCTION__. */
+#cmakedefine HAVE_FUNCNAME__FUNCTION
+
+/* Define to 1 if you have __atomic_compare_exchange_n(int *, int *, int). */
+#cmakedefine HAVE_GCC__ATOMIC_INT32_CAS 1
+
+/* Define to 1 if you have __atomic_compare_exchange_n(int64 *, int *, int64).
+   */
+#cmakedefine HAVE_GCC__ATOMIC_INT64_CAS 1
+
+/* Define to 1 if you have __sync_lock_test_and_set(char *) and friends. */
+#cmakedefine HAVE_GCC__SYNC_CHAR_TAS 1
+
+/* Define to 1 if you have __sync_compare_and_swap(int *, int, int). */
+#cmakedefine HAVE_GCC__SYNC_INT32_CAS 1
+
+/* Define to 1 if you have __sync_lock_test_and_set(int *) and friends. */
+#cmakedefine HAVE_GCC__SYNC_INT32_TAS 1
+
+/* Define to 1 if you have __sync_compare_and_swap(int64 *, int64, int64). */
+#cmakedefine HAVE_GCC__SYNC_INT64_CAS 1
+
+/* Define to 1 if you have the `getaddrinfo' function. */
+#cmakedefine HAVE_GETADDRINFO 1
+
+/* Define to 1 if you have the `gethostbyname_r' function. */
+#cmakedefine HAVE_GETHOSTBYNAME_R 1
+
+/* Define to 1 if you have the `getifaddrs' function. */
+#cmakedefine HAVE_GETIFADDRS 1
+
+/* Define to 1 if you have the `getopt' function. */
+#cmakedefine HAVE_GETOPT 1
+
+/* Define to 1 if you have the <getopt.h> header file. */
+#cmakedefine HAVE_GETOPT_H 1
+
+/* Define to 1 if you have the `getopt_long' function. */
+#cmakedefine HAVE_GETOPT_LONG 1
+
+/* Define to 1 if you have the `getpeereid' function. */
+#cmakedefine HAVE_GETPEEREID 1
+
+/* Define to 1 if you have the `getpeerucred' function. */
+#cmakedefine HAVE_GETPEERUCRED 1
+
+/* Define to 1 if you have the `getpwuid_r' function. */
+#cmakedefine HAVE_GETPWUID_R 1
+
+/* Define to 1 if you have the `getrlimit' function. */
+#cmakedefine HAVE_GETRLIMIT 1
+
+/* Define to 1 if you have the `getrusage' function. */
+#cmakedefine HAVE_GETRUSAGE 1
+
+/* Define to 1 if you have the `gettimeofday' function. */
+#cmakedefine HAVE_GETTIMEOFDAY 1
+
+/* Define to 1 if you have the <gssapi/gssapi.h> header file. */
+#cmakedefine HAVE_GSSAPI_GSSAPI_H 1
+
+/* Define to 1 if you have the <gssapi.h> header file. */
+#cmakedefine HAVE_GSSAPI_H 1
+
+/* Define to 1 if you have the <history.h> header file. */
+#cmakedefine HAVE_HISTORY_H 1
+
+/* Define to 1 if you have the `history_truncate_file' function. */
+#cmakedefine HAVE_HISTORY_TRUNCATE_FILE 1
+
+/* Define to 1 if you have the <ieeefp.h> header file. */
+#cmakedefine HAVE_IEEEFP_H @HAVE_IEEEFP_H@
+
+/* Define to 1 if you have the <ifaddrs.h> header file. */
+#cmakedefine HAVE_IFADDRS_H 1
+
+/* Define to 1 if you have the `inet_aton' function. */
+#cmakedefine HAVE_INET_ATON 1
+
+/* Define to 1 if the system has the type `int64'. */
+#cmakedefine HAVE_INT64 1
+
+/* Define to 1 if the system has the type `int8'. */
+#cmakedefine HAVE_INT8 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if the system has the type `intptr_t'. */
+/*#cmakedefine HAVE_INTPTR_T 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <inttypes.h> header file. */
+/*#cmakedefine HAVE_INTTYPES_H 1*/
+
+/* Define to 1 if you have the global variable 'int opterr'. */
+#cmakedefine HAVE_INT_OPTERR 1
+
+/* Define to 1 if you have the global variable 'int optreset'. */
+#cmakedefine HAVE_INT_OPTRESET 1
+
+/* Define to 1 if you have the global variable 'int timezone'. */
+#cmakedefine HAVE_INT_TIMEZONE 1
+
+/* Define to 1 if you have support for IPv6. */
+#cmakedefine HAVE_IPV6 1
+
+/* Define to 1 if you have isinf(). */
+#cmakedefine HAVE_ISINF 1
+
+/* Define to 1 if you have the <langinfo.h> header file. */
+#cmakedefine HAVE_LANGINFO_H 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <ldap.h> header file. */
+/*#cmakedefine HAVE_LDAP_H 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `crypto' library (-lcrypto). */
+/*#cmakedefine HAVE_LIBCRYPTO 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `ldap' library (-lldap). */
+/*#cmakedefine HAVE_LIBLDAP 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `ldap_r' library (-lldap_r). */
+/*#cmakedefine HAVE_LIBLDAP_R 1*/
+
+/* TODO BUT NOT NEED:Define to 1 if you have the `m' library (-lm). */
+/*#cmakedefine HAVE_LIBM*/
+
+/* TODO BUT NOT NEED:Define to 1 if you have the `pam' library (-lpam). */
+/*#cmakedefine HAVE_LIBPAM 1*/
+
+/* Define if you have a function readline library */
+#cmakedefine HAVE_LIBREADLINE 1
+
+/* TODO BUT NOT NEED:Define to 1 if you have the `selinux' library (-lselinux). */
+/*#cmakedefine HAVE_LIBSELINUX 1*/
+
+/* TODO BUT NOT NEED:Define to 1 if you have the `ssl' library (-lssl). */
+/*#cmakedefine HAVE_LIBSSL 1*/
+
+/* TODO BUT NOT NEED:Define to 1 if you have the `wldap32' library (-lwldap32). */
+/*#cmakedefine HAVE_LIBWLDAP32 1*/
+
+/* Define to 1 if you have the `xml2' library (-lxml2). */
+#cmakedefine HAVE_LIBXML2 1
+
+/* Define to 1 if you have the `xslt' library (-lxslt). */
+#cmakedefine HAVE_LIBXSLT 1
+
+/* Define to 1 if you have the `z' library (-lz). */
+#cmakedefine HAVE_LIBZ 1
+
+/* Define to 1 if constants of type 'long long int' should have the suffix LL.
+   */
+#cmakedefine HAVE_LL_CONSTANTS 1
+
+/* Define to 1 if the system has the type `locale_t'. */
+#cmakedefine HAVE_LOCALE_T
+
+/* Define to 1 if `long int' works and is 64 bits. */
+#cmakedefine HAVE_LONG_INT_64 1
+
+/* Define to 1 if the system has the type `long long int'. */
+#cmakedefine HAVE_LONG_LONG_INT 1
+
+/* Define to 1 if `long long int' works and is 64 bits. */
+#cmakedefine HAVE_LONG_LONG_INT_64 1
+
+/* Define to 1 if you have the <mbarrier.h> header file. */
+#cmakedefine HAVE_MBARRIER_H 1
+
+/* Define to 1 if you have the `mbstowcs_l' function. */
+#cmakedefine HAVE_MBSTOWCS_L 1
+
+/* Define to 1 if you have the `memmove' function. */
+#cmakedefine HAVE_MEMMOVE 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <memory.h> header file. */
+/*#cmakedefine HAVE_MEMORY_H 1*/
+
+/* Define to 1 if the system has the type `MINIDUMP_TYPE'. */
+#cmakedefine HAVE_MINIDUMP_TYPE 1
+
+/* Define to 1 if you have the `mkdtemp' function. */
+#cmakedefine HAVE_MKDTEMP 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <netinet/in.h> header file. */
+/*#cmakedefine HAVE_NETINET_IN_H 1*/
+
+/* Define to 1 if you have the <netinet/tcp.h> header file. */
+#cmakedefine HAVE_NETINET_TCP_H 1
+
+/* Define to 1 if you have the <net/if.h> header file. */
+#cmakedefine HAVE_NET_IF_H 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <ossp/uuid.h> header file. */
+/*#cmakedefine HAVE_OSSP_UUID_H 1*/
+
+/* Define to 1 if you have the <pam/pam_appl.h> header file. */
+#cmakedefine HAVE_PAM_PAM_APPL_H 1
+
+/* Define to 1 if you have the `poll' function. */
+#cmakedefine HAVE_POLL 1
+
+/* Define to 1 if you have the <poll.h> header file. */
+#cmakedefine HAVE_POLL_H 1
+
+/* Define to 1 if you have the `posix_fadvise' function. */
+#cmakedefine HAVE_POSIX_FADVISE 1
+
+/* Define to 1 if the assembler supports PPC's LWARX mutex hint bit. */
+#cmakedefine HAVE_PPC_LWARX_MUTEX_HINT 1
+
+/* Define to 1 if you have the `pstat' function. */
+#cmakedefine HAVE_PSTAT 1
+
+/* Define to 1 if the PS_STRINGS thing exists. */
+#cmakedefine HAVE_PS_STRINGS 1
+
+/* TODO BUT DO NOT NEED:Define if you have POSIX threads libraries and header files. */
+/*#cmakedefine HAVE_PTHREAD 1*/
+
+/* Define to 1 if you have the `pthread_is_threaded_np' function. */
+#cmakedefine HAVE_PTHREAD_IS_THREADED_NP 1
+
+/* TODO BUT DO NOT NEED:Have PTHREAD_PRIO_INHERIT. */
+/*#cmakedefine HAVE_PTHREAD_PRIO_INHERIT 1*/
+
+/* Define to 1 if you have the <pwd.h> header file. */
+#cmakedefine HAVE_PWD_H 1
+
+/* Define to 1 if you have the `random' function. */
+#cmakedefine HAVE_RANDOM 1
+
+/* Define to 1 if you have the <readline.h> header file. */
+#cmakedefine HAVE_READLINE_H 1
+
+/* Define to 1 if you have the <readline/history.h> header file. */
+#cmakedefine HAVE_READLINE_HISTORY_H 1
+
+/* Define to 1 if you have the <readline/readline.h> header file. */
+#cmakedefine HAVE_READLINE_READLINE_H 1
+
+/* Define to 1 if you have the `readlink' function. */
+#cmakedefine HAVE_READLINK 1
+
+/* Define to 1 if you have the `rint' function. */
+#cmakedefine HAVE_RINT 1
+
+/* Define to 1 if you have the global variable
+   'rl_completion_append_character'. */
+#cmakedefine HAVE_RL_COMPLETION_APPEND_CHARACTER 1
+
+/* Define to 1 if you have the `rl_completion_matches' function. */
+#cmakedefine HAVE_RL_COMPLETION_MATCHES 1
+
+/* Define to 1 if you have the `rl_filename_completion_function' function. */
+#cmakedefine HAVE_RL_FILENAME_COMPLETION_FUNCTION 1
+
+/* Define to 1 if you have the `rl_reset_screen_size' function. */
+#cmakedefine HAVE_RL_RESET_SCREEN_SIZE 1
+
+/* Define to 1 if you have the <security/pam_appl.h> header file. */
+#cmakedefine HAVE_SECURITY_PAM_APPL_H 1
+
+/* Define to 1 if you have the `setproctitle' function. */
+#cmakedefine HAVE_SETPROCTITLE 1
+
+/* Define to 1 if you have the `setsid' function. */
+#cmakedefine HAVE_SETSID 1
+
+/* Define to 1 if you have the `shm_open' function. */
+#cmakedefine HAVE_SHM_OPEN 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `snprintf' function. */
+/*#cmakedefine HAVE_SNPRINTF 1*/
+
+/* Define to 1 if you have spinlocks. */
+#cmakedefine HAVE_SPINLOCKS 1
+
+/* Define to 1 if you have the `srandom' function. */
+#cmakedefine HAVE_SRANDOM 1
+
+/* Define to 1 if you have the `SSL_get_current_compression' function. */
+#cmakedefine HAVE_SSL_GET_CURRENT_COMPRESSION 1
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#cmakedefine HAVE_STDINT_H 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <stdlib.h> header file. */
+/*#cmakedefine HAVE_STDLIB_H 1*/
+
+/* Define to 1 if you have the `strerror' function. */
+#cmakedefine HAVE_STRERROR 1
+
+/* Define to 1 if you have the `strerror_r' function. */
+#cmakedefine HAVE_STRERROR_R 1
+
+/* Define to 1 if you have the <strings.h> header file. */
+#cmakedefine HAVE_STRINGS_H 1
+
+/* Define to 1 if you have the <string.h> header file. */
+#cmakedefine HAVE_STRING_H 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `strlcat' function. */
+/*#cmakedefine HAVE_STRLCAT 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `strlcpy' function. */
+/*#cmakedefine HAVE_STRLCPY 1*/
+
+/* Define to 1 if you have the `strtoll' function. */
+#cmakedefine HAVE_STRTOLL 1
+
+/* Define to 1 if you have the `strtoq' function. */
+#cmakedefine HAVE_STRTOQ 1
+
+/* Define to 1 if you have the `strtoull' function. */
+#cmakedefine HAVE_STRTOULL 1
+
+/* Define to 1 if you have the `strtouq' function. */
+#cmakedefine HAVE_STRTOUQ 1
+
+/* Define to 1 if the system has the type `struct addrinfo'. */
+#cmakedefine HAVE_STRUCT_ADDRINFO 1
+
+/* Define to 1 if the system has the type `struct cmsgcred'. */
+#cmakedefine HAVE_STRUCT_CMSGCRED 1
+
+/* Define to 1 if the system has the type `struct option'. */
+#cmakedefine HAVE_STRUCT_OPTION 1
+
+/* Define to 1 if `sa_len' is a member of `struct sockaddr'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_SA_LEN 1
+
+/* Define to 1 if the system has the type `struct sockaddr_storage'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_STORAGE 1
+
+/* Define to 1 if `ss_family' is a member of `struct sockaddr_storage'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY 1
+
+/* Define to 1 if `ss_len' is a member of `struct sockaddr_storage'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN 1
+
+/* Define to 1 if `__ss_family' is a member of `struct sockaddr_storage'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_STORAGE___SS_FAMILY 1
+
+/* Define to 1 if `__ss_len' is a member of `struct sockaddr_storage'. */
+#cmakedefine HAVE_STRUCT_SOCKADDR_STORAGE___SS_LEN 1
+
+/* Define to 1 if `tm_zone' is a member of `struct tm'. */
+#cmakedefine HAVE_STRUCT_TM_TM_ZONE 1
+
+/* Define to 1 if you have the `symlink' function. */
+#cmakedefine HAVE_SYMLINK 1
+
+/* Define to 1 if you have the `sync_file_range' function. */
+#cmakedefine HAVE_SYNC_FILE_RANGE 1
+
+/* Define to 1 if you have the syslog interface. */
+#cmakedefine HAVE_SYSLOG 1
+
+/* Define to 1 if you have the <sys/epoll.h> header file. */
+#cmakedefine HAVE_SYS_EPOLL_H 1
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#cmakedefine HAVE_SYS_IOCTL_H 1
+
+/* Define to 1 if you have the <sys/ipc.h> header file. */
+#cmakedefine HAVE_SYS_IPC_H 1
+
+/* Define to 1 if you have the <sys/poll.h> header file. */
+#cmakedefine HAVE_SYS_POLL_H 1
+
+/* Define to 1 if you have the <sys/pstat.h> header file. */
+#cmakedefine HAVE_SYS_PSTAT_H 1
+
+/* Define to 1 if you have the <sys/resource.h> header file. */
+#cmakedefine HAVE_SYS_RESOURCE_H 1
+
+/* Define to 1 if you have the <sys/select.h> header file. */
+#cmakedefine HAVE_SYS_SELECT_H 1
+
+/* Define to 1 if you have the <sys/sem.h> header file. */
+#cmakedefine HAVE_SYS_SEM_H 1
+
+/* Define to 1 if you have the <sys/shm.h> header file. */
+#cmakedefine HAVE_SYS_SHM_H 1
+
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#cmakedefine HAVE_SYS_SOCKET_H 1
+
+/* Define to 1 if you have the <sys/sockio.h> header file. */
+#cmakedefine HAVE_SYS_SOCKIO_H 1
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#cmakedefine HAVE_SYS_STAT_H 1
+
+/* Define to 1 if you have the <sys/tas.h> header file. */
+#cmakedefine HAVE_SYS_TAS_H 1
+
+/* Define to 1 if you have the <sys/time.h> header file. */
+#cmakedefine HAVE_SYS_TIME_H 1
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#cmakedefine HAVE_SYS_TYPES_H 1
+
+/* Define to 1 if you have the <sys/ucred.h> header file. */
+#cmakedefine HAVE_SYS_UCRED_H 1
+
+/* Define to 1 if you have the <sys/un.h> header file. */
+#cmakedefine HAVE_SYS_UN_H 1
+
+/* Define to 1 if you have the <termios.h> header file. */
+#cmakedefine HAVE_TERMIOS_H 1
+
+/* Define to 1 if your `struct tm' has `tm_zone'. Deprecated, use
+   `HAVE_STRUCT_TM_TM_ZONE' instead. */
+#cmakedefine HAVE_TM_ZONE 1
+
+/* Define to 1 if you have the `towlower' function. */
+#cmakedefine HAVE_TOWLOWER 1
+
+/* Define to 1 if you have the external array `tzname'. */
+#cmakedefine HAVE_TZNAME 1
+
+/* Define to 1 if you have the <ucred.h> header file. */
+#cmakedefine HAVE_UCRED_H 1
+
+/* Define to 1 if the system has the type `uint64'. */
+#cmakedefine HAVE_UINT64 1
+
+/* Define to 1 if the system has the type `uint8'. */
+#cmakedefine HAVE_UINT8 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if the system has the type `uintptr_t'. */
+/*#cmakedefine HAVE_UINTPTR_T 1*/
+
+/* Define to 1 if the system has the type `union semun'. */
+#cmakedefine HAVE_UNION_SEMUN 1
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#cmakedefine HAVE_UNISTD_H 1
+
+/* Define to 1 if you have unix sockets. */
+#cmakedefine HAVE_UNIX_SOCKETS 1
+
+/* Define to 1 if you have the `unsetenv' function. */
+#cmakedefine HAVE_UNSETENV 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if the system has the type `unsigned long long int'. */
+/*#cmakedefine HAVE_UNSIGNED_LONG_LONG_INT 1*/
+
+/* Define to 1 if you have the `utime' function. */
+#cmakedefine HAVE_UTIME 1
+
+/* Define to 1 if you have the `utimes' function. */
+#cmakedefine HAVE_UTIMES 1
+
+/* Define to 1 if you have the <utime.h> header file. */
+#cmakedefine HAVE_UTIME_H 1
+
+/* Define to 1 if you have BSD UUID support. */
+#cmakedefine HAVE_UUID_BSD 1
+
+/* Define to 1 if you have E2FS UUID support. */
+#cmakedefine HAVE_UUID_E2FS 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <uuid.h> header file. */
+/*#cmakedefine HAVE_UUID_H 1*/
+
+/* Define to 1 if you have OSSP UUID support. */
+#cmakedefine HAVE_UUID_OSSP 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the <uuid/uuid.h> header file. */
+/*#cmakedefine HAVE_UUID_UUID_H 1*/
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the `vsnprintf' function. */
+/*#cmakedefine HAVE_VSNPRINTF 1*/
+
+/* Define to 1 if you have the <wchar.h> header file. */
+#cmakedefine HAVE_WCHAR_H 1
+
+/* Define to 1 if you have the `wcstombs' function. */
+#cmakedefine HAVE_WCSTOMBS 1
+
+/* Define to 1 if you have the `wcstombs_l' function. */
+#cmakedefine HAVE_WCSTOMBS_L 1
+
+/* Define to 1 if you have the <wctype.h> header file. */
+#cmakedefine HAVE_WCTYPE_H 1
+
+/* Define to 1 if you have the <winldap.h> header file. */
+#cmakedefine HAVE_WINLDAP_H 1
+
+/* Define to 1 if your compiler understands __builtin_bswap32. */
+#cmakedefine HAVE__BUILTIN_BSWAP32 1
+
+/* Define to 1 if your compiler understands __builtin_bswap64. */
+#cmakedefine HAVE__BUILTIN_BSWAP64 1
+
+/* Define to 1 if your compiler understands __builtin_constant_p. */
+#cmakedefine HAVE__BUILTIN_CONSTANT_P 1
+
+/* Define to 1 if your compiler understands __builtin_types_compatible_p. */
+#cmakedefine HAVE__BUILTIN_TYPES_COMPATIBLE_P 1
+
+/* Define to 1 if your compiler understands __builtin_unreachable. */
+#cmakedefine HAVE__BUILTIN_UNREACHABLE 1
+
+/* Define to 1 if you have __cpuid. */
+#cmakedefine HAVE__CPUID 1
+
+/* Define to 1 if you have __cpuid. */
+#cmakedefine HAVE__GET_CPUID 1
+
+/* Define to 1 if your compiler understands _Static_assert. */
+#cmakedefine HAVE__STATIC_ASSERT 1
+
+/* Define to 1 if your compiler understands __VA_ARGS__ in macros. */
+#cmakedefine HAVE__VA_ARGS 1
+
+/* Define to the appropriate snprintf length modifier for 64-bit ints. */
+#define INT64_MODIFIER "@LONG_LONG_INT_MODIFIER@"
+
+/* Define to 1 if `locale_t' requires <xlocale.h>. */
+#cmakedefine LOCALE_T_IN_XLOCALE 1
+
+/* Define as the maximum alignment requirement of any C data type. */
+#define MAXIMUM_ALIGNOF @MAXIMUM_ALIGNOF@
+
+/* Define bytes to use libc memset(). */
+#define MEMSET_LOOP_LIMIT @MEMSET_LOOP_LIMIT@
+
+/* Define to the address where bug reports for this package should be sent. */
+#define PACKAGE_BUGREPORT "@PACKAGE_BUGREPORT@"
+
+/* TODO BUT DO NOT NEED:Define to the full name of this package. */
+/*#define PACKAGE_NAME "@PACKAGE_NAME@"*/
+
+/* TODO:Define to the full name and version of this package. */
+/*#define PACKAGE_STRING "@PACKAGE_STRING@"*/
+
+/* TODO BUT DO NOT NEED:Define to the one symbol short name of this package. */
+/*#define PACKAGE_TARNAME "@PACKAGE_TARNAME@"*/
+
+/* TODO BUT DO NOT NEED:Define to the home page for this package. */
+/*#define PACKAGE_URL "@PACKAGE_URL@"*/
+
+/* TODO BUT DO NOT NEED:Define to the version of this package. */
+/*#define PACKAGE_VERSION "@PACKAGE_VERSION@"*/
+
+/* Define to the name of a signed 128-bit integer type. */
+#define PG_INT128_TYPE @PG_INT128_TYPE@
+
+/* Define to the name of a signed 64-bit integer type. */
+#define PG_INT64_TYPE @PG_INT64_TYPE@
+
+/* Define to the name of the default PostgreSQL service principal in Kerberos
+   (GSSAPI). */
+#define PG_KRB_SRVNAM "@PG_KRB_SRVNAM@"
+
+/* PostgreSQL major version as a string */
+#define PG_MAJORVERSION "@POSTGRES_MAJOR_VERSION@"
+
+/* Define to gnu_printf if compiler supports it, else printf. */
+#define PG_PRINTF_ATTRIBUTE @PG_PRINTF_ATTRIBUTE@
+
+/* PostgreSQL version as a string */
+#define PG_VERSION "@PG_VERSION@"
+
+/* PostgreSQL version as a number */
+#define PG_VERSION_NUM @PG_VERSION_NUM@
+
+/* A string containing the version number, platform, and C compiler */
+#define PG_VERSION_STR "PostgreSQL @PG_VERSION@ on @CMAKE_SYSTEM_NAME@, compiled by @CMAKE_C_COMPILER_ID@, @VOID_POINTER_SIZE_BIT@-bit"
+
+/* Define to 1 to allow profiling output to be saved separately for each
+   process. */
+#cmakedefine PROFILE_PID_DIR 1
+
+/* TODO BUT DO NOT NEED:Define to necessary symbol if this constant uses a non-standard name on
+   your system. */
+/*#cmakedefine PTHREAD_CREATE_JOINABLE*/
+
+/* RELSEG_SIZE is the maximum number of blocks allowed in one disk file. Thus,
+   the maximum size of a single file is RELSEG_SIZE * BLCKSZ; relations bigger
+   than that are divided into multiple files. RELSEG_SIZE * BLCKSZ must be
+   less than your OS' limit on file size. This is often 2 GB or 4GB in a
+   32-bit operating system, unless you have large file support enabled. By
+   default, we make the limit 1 GB to avoid any possible integer-overflow
+   problems within the OS. A limit smaller than necessary only means we divide
+   a large relation into more chunks than necessary, so it seems best to err
+   in the direction of a small limit. A power-of-2 value is recommended to
+   save a few cycles in md.c, but is not absolutely required. Changing
+   RELSEG_SIZE requires an initdb. */
+#define RELSEG_SIZE @RELSEG_SIZE@
+
+/* The size of `long', as computed by sizeof. */
+#define SIZEOF_LONG @SIZEOF_LONG@
+
+/* TODO BUT DO NOT NEED:The size of `off_t', as computed by sizeof. */
+/*#define SIZEOF_OFF_T @SIZEOF_OFF_T@*/
+
+/* The size of `size_t', as computed by sizeof. */
+#define SIZEOF_SIZE_T @SIZEOF_SIZE_T@
+
+/* The size of `void *', as computed by sizeof. */
+#define SIZEOF_VOID_P @CMAKE_SIZEOF_VOID_P@
+
+/* TODO BUT DO NOT NEED:Define to 1 if you have the ANSI C header files. */
+/*#cmakedefine STDC_HEADERS 1*/
+
+/* Define to 1 if strerror_r() returns a int. */
+#cmakedefine STRERROR_R_INT 1
+
+/* TODO BUT DO NOT NEED:Define to 1 if your <sys/time.h> declares `struct tm'. */
+/*#cmakedefine TM_IN_SYS_TIME 1*/
+
+/* Define to 1 to build with assertion checks. (--enable-cassert) */
+#cmakedefine USE_ASSERT_CHECKING 1
+
+/* Define to 1 to build with Bonjour support. (--with-bonjour) */
+#cmakedefine USE_BONJOUR 1
+
+/* Define to 1 to build with BSD Authentication support. (--with-bsd-auth) */
+#cmakedefine USE_BSD_AUTH 1
+
+/* Define to 1 if you want float4 values to be passed by value. */
+#cmakedefine USE_FLOAT4_BYVAL 1
+
+/* Define to 1 if you want float8, int8, etc values to be passed by value. */
+#cmakedefine USE_FLOAT8_BYVAL 1
+
+/* Define to 1 if you want 64-bit integer timestamp and interval support. */
+#cmakedefine USE_INTEGER_DATETIMES 1
+
+/* Define to 1 to build with LDAP support. (--with-ldap) */
+#cmakedefine USE_LDAP 1
+
+/* Define to 1 to build with XML support. (-DWITH_LIBXML) */
+#cmakedefine USE_LIBXML 1
+
+/* Define to 1 to use XSLT support when building contrib/xml2. */
+#cmakedefine USE_LIBXSLT 1
+
+/* Define to select named POSIX semaphores. */
+#cmakedefine USE_NAMED_POSIX_SEMAPHORES 1
+
+/* Define to build with OpenSSL support. (--with-openssl) */
+#cmakedefine USE_OPENSSL 1
+
+/* Define to 1 to build with PAM support. (--with-pam) */
+#cmakedefine USE_PAM 1
+
+/* Use replacement snprintf() functions. */
+#cmakedefine USE_REPL_SNPRINTF 1
+
+/* Define to 1 to use Intel SSE 4.2 CRC instructions with a runtime check. */
+#cmakedefine USE_SLICING_BY_8_CRC32C 1
+
+/* Define to 1 use Intel SSE 4.2 CRC instructions. */
+#cmakedefine USE_SSE42_CRC32C 1
+
+/* Define to 1 to use Intel SSSE 4.2 CRC instructions with a runtime check. */
+#cmakedefine USE_SSE42_CRC32C_WITH_RUNTIME_CHECK 1
+
+/* Define to build with systemd support. (--with-systemd) */
+#cmakedefine USE_SYSTEMD
+
+/* Define to select SysV-style semaphores. */
+#cmakedefine USE_SYSV_SEMAPHORES
+
+/* Define to select SysV-style shared memory. */
+#cmakedefine USE_SYSV_SHARED_MEMORY
+
+/* Define to select unnamed POSIX semaphores. */
+#cmakedefine USE_UNNAMED_POSIX_SEMAPHORES
+
+/* Define to select Win32-style semaphores. */
+#cmakedefine USE_WIN32_SEMAPHORES
+
+/* TODO BUT DO NOT NEED:Define to select Win32-style shared memory. */
+/*#cmakedefine USE_WIN32_SHARED_MEMORY*/
+
+/* Define to 1 if `wcstombs_l' requires <xlocale.h>. */
+#cmakedefine WCSTOMBS_L_IN_XLOCALE 1
+
+/* NOT NEED:Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
+   significant byte first (like Motorola and SPARC, unlike Intel). */
+/*#if defined AC_APPLE_UNIVERSAL_BUILD
+# if defined __BIG_ENDIAN__
+#  define WORDS_BIGENDIAN 1
+# endif
+#else
+# ifndef WORDS_BIGENDIAN
+#  undef WORDS_BIGENDIAN
+# endif
+#endif*/
+
+/* Size of a WAL file block. This need have no particular relation to BLCKSZ.
+   XLOG_BLCKSZ must be a power of 2, and if your system supports O_DIRECT I/O,
+   XLOG_BLCKSZ must be a multiple of the alignment requirement for direct-I/O
+   buffers, else direct I/O may fail. Changing XLOG_BLCKSZ requires an initdb.
+   */
+#define XLOG_BLCKSZ @XLOG_BLCKSZ@
+
+/* XLOG_SEG_SIZE is the size of a single WAL file. This must be a power of 2
+   and larger than XLOG_BLCKSZ (preferably, a great deal larger than
+   XLOG_BLCKSZ). Changing XLOG_SEG_SIZE requires an initdb. */
+#define XLOG_SEG_SIZE @XLOG_SEG_SIZE@
+
+/* TODO BUT DO NOT NEED:Number of bits in a file offset, on hosts where this is settable. */
+/*#undef _FILE_OFFSET_BITS*/
+
+/* TODO BUT DO NOT NEED:Define to 1 to make fseeko visible on some hosts (e.g. glibc 2.2). */
+/*#undef _LARGEFILE_SOURCE*/
+
+/* TODO BUT DO NOT NEED:Define for large files, on AIX-style hosts. */
+/*#undef _LARGE_FILES*/
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+   calls it, or to nothing if 'inline' is not supported under any name.  */
+#ifndef __cplusplus
+#undef inline
+#endif
+
+/* Define to the type of a signed integer type wide enough to hold a pointer,
+   if such a type exists, and if the system does not define it. */
+/*#undef intptr_t*/
+
+/* Define to empty if the C compiler does not understand signed types. */
+/*#undef signed*/
+
+/* Define to the type of an unsigned integer type wide enough to hold a
+   pointer, if such a type exists, and if the system does not define it. */
+/*#undef uintptr_t*/
+
+#cmakedefine HAVE_INT128
+
+#ifdef WIN32
+#undef PG_INT128_TYPE
+#endif
+
+#define LONG_LONG_INT_MODIFIER @LONG_LONG_INT_MODIFIER@
+
+#define POSTGRES_MAJOR_VERSION @POSTGRES_MAJOR_VERSION@
+
+#define POSTGRES_MINOR_VERSION @POSTGRES_MINOR_VERSION@
+
+#define POSTGRES_PATCH_VERSION @POSTGRES_PATCH_VERSION@
+
+#ifdef __APPLE__
+#	if __BIG_ENDIAN
+#		define WORDS_BIGENDIAN 1
+#	endif
+#else
+#cmakedefine WORDS_BIGENDIAN 1
+#endif
+
+#define DLSUFFIX "@CMAKE_SHARED_MODULE_SUFFIX@"
+
+#cmakedefine HAVE_MKSTEMP 1
+
+#cmakedefine HAVE_NBTOOL_CONFIG_H 1
+
+/*#include "@pgos_include_SRCS@"*/
+
+#endif  // AUTO_CONFIG_H
diff --git a/src/include/pg_config_ext_cmake.in b/src/include/pg_config_ext_cmake.in
new file mode 100644
index 0000000..a73f04b
--- /dev/null
+++ b/src/include/pg_config_ext_cmake.in
@@ -0,0 +1 @@
+#define PG_INT64_TYPE @PG_INT64_TYPE@
\ No newline at end of file
diff --git a/src/include/pg_config_paths_cmake.in b/src/include/pg_config_paths_cmake.in
new file mode 100644
index 0000000..f1b3c08
--- /dev/null
+++ b/src/include/pg_config_paths_cmake.in
@@ -0,0 +1,12 @@
+#define PGBINDIR "@PGBINDIR@"
+#define PGSHAREDIR "@PGSHAREDIR@"
+#define SYSCONFDIR "@SYSCONFDIR@" 
+#define INCLUDEDIR "@INCLUDEDIR@"
+#define PKGINCLUDEDIR "@PKGINCLUDEDIR@"
+#define INCLUDEDIRSERVER "@INCLUDEDIRSERVER@"
+#define LIBDIR "@LIBDIR@"
+#define PKGLIBDIR "@PKGLIBDIR@"
+#define LOCALEDIR "@LOCALEDIR@"
+#define DOCDIR "@DOCDIR@"
+#define HTMLDIR "@HTMLDIR@"
+#define MANDIR "@MANDIR@"
\ No newline at end of file
diff --git a/src/include/port.h b/src/include/port.h
index b81fa4a..dd63c44 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -470,4 +470,7 @@ extern char *escape_single_quotes_ascii(const char *src);
 /* port/wait_error.c */
 extern char *wait_result_to_str(int exit_status);
 
+extern int pg_putenv_proxy(char *envval);
+
+
 #endif   /* PG_PORT_H */
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index 4453c90..2c30268 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -418,7 +418,6 @@ extern void pgwin32_unsetenv(const char *);
 extern int	pgwin32_is_service(void);
 extern int	pgwin32_is_admin(void);
 
-#define putenv(x) pgwin32_putenv(x)
 #define unsetenv(x) pgwin32_unsetenv(x)
 
 /* Things that exist in MingW headers, but need to be added to MSVC & BCC */
diff --git a/src/interfaces/ecpg/compatlib/CMakeLists.txt b/src/interfaces/ecpg/compatlib/CMakeLists.txt
new file mode 100644
index 0000000..3214951
--- /dev/null
+++ b/src/interfaces/ecpg/compatlib/CMakeLists.txt
@@ -0,0 +1,27 @@
+include_directories(BEFORE
+	"${CMAKE_CURRENT_SOURCE_DIR}"
+	"${PROJECT_SOURCE_DIR}/src/port"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/ecpg/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+set(ecpg_compat_SRC
+	informix.c
+)
+if(USE_REPL_SNPRINTF)
+	set(ecpg_compat_SRC
+		${ecpg_compat_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/snprintf.c
+	)
+endif()
+
+add_library(ecpg_compat SHARED ${ecpg_compat_SRC})
+if (MSVC)
+	gen_def(ecpg_compat)
+endif()
+target_link_libraries(ecpg_compat ${LIB_M} pgtypes pq ecpg)
+target_compile_definitions(ecpg_compat PRIVATE -DFRONTEND)
+set_target_properties(ecpg_compat PROPERTIES VERSION "3.8")
+
+install(TARGETS ecpg_compat
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/interfaces/ecpg/ecpglib/CMakeLists.txt b/src/interfaces/ecpg/ecpglib/CMakeLists.txt
new file mode 100644
index 0000000..158ec62
--- /dev/null
+++ b/src/interfaces/ecpg/ecpglib/CMakeLists.txt
@@ -0,0 +1,76 @@
+include_directories(BEFORE
+	"${CMAKE_CURRENT_SOURCE_DIR}"
+	"${PROJECT_SOURCE_DIR}/src/port"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/ecpg/include"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+)
+set(ecpg_SRC
+	execute.c
+	typename.c
+	descriptor.c
+	sqlda.c
+	data.c
+	error.c
+	prepare.c
+	memory.c
+	connect.c
+	misc.c
+	${PROJECT_SOURCE_DIR}/src/port/path.c
+	${PROJECT_SOURCE_DIR}/src/port/pgstrcasecmp.c
+)
+
+if(USE_REPL_SNPRINTF)
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/snprintf.c
+	)
+endif()
+
+if(NOT HAVE_RINT)
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/rint.c
+	)
+endif()
+
+if(NOT HAVE_DECL_STRLCPY)
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/strlcpy.c
+	)
+endif()
+
+if(NOT HAVE_ISINF)
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/isinf.c
+	)
+endif()
+
+if(WIN32)
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/win32setlocale.c
+	)
+else()
+	set(ecpg_SRC
+		${ecpg_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/thread.c
+	)
+endif()
+
+add_library(ecpg SHARED ${ecpg_SRC})
+if (MSVC)
+	gen_def(ecpg)
+endif()
+target_link_libraries(ecpg ${LIB_M} pgtypes pq)
+target_compile_definitions(ecpg PRIVATE -DFRONTEND)
+set_target_properties(ecpg PROPERTIES VERSION "6.8")
+
+install(TARGETS ecpg
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(ecpg "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;tr;zh_CN")
+endif()
diff --git a/src/interfaces/ecpg/include/CMakeLists.txt b/src/interfaces/ecpg/include/CMakeLists.txt
new file mode 100644
index 0000000..63c42e8
--- /dev/null
+++ b/src/interfaces/ecpg/include/CMakeLists.txt
@@ -0,0 +1,25 @@
+set(INCLUDEDIR_INTERNAL ${PKGINCLUDEDIR}/internal)
+
+install(FILES
+	ecpgerrno.h
+	ecpglib.h
+	ecpgtype.h
+	sqlca.h
+	sql3types.h
+	ecpg_informix.h
+	pgtypes_error.h
+	pgtypes_numeric.h
+	pgtypes_timestamp.h
+	pgtypes_date.h
+	pgtypes_interval.h
+	sqlda.h
+	sqlda-compat.h
+	sqlda-native.h
+	ecpg_config.h
+	DESTINATION ${INCLUDEDIR})
+
+install(FILES
+	datetime.h
+	decimal.h
+	sqltypes.h
+	DESTINATION ${PKGINCLUDEDIR}/informix/esql)
diff --git a/src/interfaces/ecpg/include/ecpg_config_cmake.in b/src/interfaces/ecpg/include/ecpg_config_cmake.in
new file mode 100644
index 0000000..1f3fcb8
--- /dev/null
+++ b/src/interfaces/ecpg/include/ecpg_config_cmake.in
@@ -0,0 +1,19 @@
+/* Define to 1 if the system has the type `int64'. */
+#cmakedefine HAVE_INT64 1
+
+/* Define to 1 if `long int' works and is 64 bits. */
+#cmakedefine HAVE_LONG_INT_64 1
+
+/* Define to 1 if the system has the type `long long int'. */
+#cmakedefine HAVE_LONG_LONG_INT 1
+
+/* Define to 1 if `long long int' works and is 64 bits. */
+#cmakedefine HAVE_LONG_LONG_INT_64 1
+
+/* Define to 1 if you want 64-bit integer timestamp and interval support.
+   (--enable-integer-datetimes) */
+#cmakedefine USE_INTEGER_DATETIMES 1
+
+/* Define to 1 to build client libraries as thread-safe code.
+ *    (--enable-thread-safety) */
+#cmakedefine ENABLE_THREAD_SAFETY 1
diff --git a/src/interfaces/ecpg/pgtypeslib/CMakeLists.txt b/src/interfaces/ecpg/pgtypeslib/CMakeLists.txt
new file mode 100644
index 0000000..a36ddca
--- /dev/null
+++ b/src/interfaces/ecpg/pgtypeslib/CMakeLists.txt
@@ -0,0 +1,41 @@
+include_directories(BEFORE
+	"${CMAKE_CURRENT_SOURCE_DIR}"
+	"${PROJECT_SOURCE_DIR}/src/port"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/ecpg/include"
+)
+set(pgtypes_SRC
+	numeric.c
+	datetime.c
+	common.c
+	dt_common.c
+	timestamp.c
+	interval.c
+	${PROJECT_SOURCE_DIR}/src/port/pgstrcasecmp.c
+)
+if(USE_REPL_SNPRINTF)
+	set(pgtypes_SRC
+		${pgtypes_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/snprintf.c
+	)
+endif()
+
+if(NOT HAVE_RINT)
+	set(pgtypes_SRC
+		${pgtypes_SRC}
+		${PROJECT_SOURCE_DIR}/src/port/rint.c
+	)
+endif()
+
+add_library(pgtypes SHARED ${pgtypes_SRC})
+if (MSVC)
+	gen_def(pgtypes)
+endif()
+
+target_link_libraries(pgtypes ${LIB_M})
+target_compile_definitions(pgtypes PRIVATE -DFRONTEND)
+set_target_properties(pgtypes PROPERTIES VERSION "3.7")
+
+
+install(TARGETS pgtypes
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/interfaces/libpq/CMakeLists.txt b/src/interfaces/libpq/CMakeLists.txt
new file mode 100644
index 0000000..8afcc6c
--- /dev/null
+++ b/src/interfaces/libpq/CMakeLists.txt
@@ -0,0 +1,108 @@
+include_directories(BEFORE
+	"${CMAKE_CURRENT_SOURCE_DIR}"
+	"${PROJECT_SOURCE_DIR}/src/port"
+)
+
+if(USE_OPENSSL)
+	include_directories(BEFORE ${OPENSSL_INCLUDE_DIR})
+	set(pq_ssl_SRCS fe-secure-openssl.c)
+endif(USE_OPENSSL)
+
+set(pq_SRCS
+	fe-auth.c
+	fe-connect.c
+	fe-exec.c
+	fe-misc.c
+	fe-print.c
+	fe-lobj.c
+	fe-protocol2.c
+	fe-protocol3.c
+	pqexpbuffer.c
+	fe-secure.c
+	libpq-events.c
+
+	${PORT_DIR}/chklocale.c
+	${PORT_DIR}/inet_net_ntop.c
+	${PORT_DIR}/noblock.c
+	${PORT_DIR}/pgstrcasecmp.c
+	${PORT_DIR}/pqsignal.c
+	${PORT_DIR}/thread.c
+
+	${PROJECT_SOURCE_DIR}/src/common/ip.c
+	${PROJECT_SOURCE_DIR}/src/common/md5.c
+	${MB_UTILS_BACKEND_DIR}/encnames.c
+	${MB_UTILS_BACKEND_DIR}/wchar.c
+
+	${fallback_SRCS}
+	${pq_ssl_SRCS}
+)
+
+if(USE_REPL_SNPRINTF)
+	set(pq_SRCS ${pq_SRCS} ${PORT_DIR}/snprintf.c)
+endif()
+
+set(RES_FILES "")
+if(WIN32)
+	set(pq_SRCS ${pq_SRCS}
+		win32.c
+		${PORT_DIR}/win32error.c
+		${PORT_DIR}/win32setlocale.c
+		${PORT_DIR}/pgsleep.c
+		${PORT_DIR}/open.c
+		${PORT_DIR}/system.c
+		${PORT_DIR}/inet_aton.c
+		${PORT_DIR}/thread.c
+		${PORT_DIR}/getaddrinfo.c
+	)
+	if (Threads_FOUND)
+		set(pq_SRCS ${pq_SRCS} pthread-win32.c)
+	endif()
+	set(RES_FILES "libpq.rc")
+	set(CMAKE_RC_COMPILER_INIT windres)
+	ENABLE_LANGUAGE(RC)
+	SET(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
+endif()
+
+add_library(pq SHARED ${pq_SRCS} ${RES_FILES})
+target_link_libraries(pq
+	${CMAKE_THREAD_LIBS_INIT}
+	${LIBSOCKET_LIBRARIES}
+	${GSS_LIBS}
+)
+if(USE_OPENSSL)
+	target_link_libraries(pq ${OPENSSL_LIBRARIES})
+endif()
+if (MSVC)
+	gen_def(pq)
+endif()
+if(CRYPT_LIB)
+	target_link_libraries(pq ${CRYPT_LIB})
+endif()
+if(WIN32)
+	target_link_libraries(pq
+		#port
+		Secur32
+		ws2_32
+	)
+endif()
+
+if(LDAP_FOUND)
+	target_link_libraries(pq ${LDAP_LIBRARIES})
+endif()
+
+if(USE_OPENSSL)
+	target_include_directories(pq PUBLIC ${OPENSSL_INCLUDE_DIR})
+endif()
+
+
+target_compile_definitions(pq PRIVATE -DFRONTEND -DUNSAFE_STAT_OK)
+install(TARGETS pq
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+if(ENABLE_NLS)
+	MAKE_MO(libpq "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;tr;zh_CN;zh_TW")
+endif()
+
+#TODO 
+#set_target_properties(pq PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports.list")
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 9668b52..06964c6 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -28,6 +28,7 @@
 #include "pg_config_paths.h"
 
 #ifdef WIN32
+#include <WS2tcpip.h>
 #include "win32.h"
 #ifdef _WIN32_IE
 #undef _WIN32_IE
diff --git a/src/pl/plperl/CMakeLists.txt b/src/pl/plperl/CMakeLists.txt
new file mode 100644
index 0000000..ba73e7c
--- /dev/null
+++ b/src/pl/plperl/CMakeLists.txt
@@ -0,0 +1,100 @@
+if(WIN32)
+	add_definitions(-DPLPERL_HAVE_UID_GID)
+endif()
+
+execute_process(
+	COMMAND ${PERL_EXECUTABLE} -e "use List::Util qw(first); print first { -r \"$_/ExtUtils/xsubpp\" } @INC"
+	OUTPUT_VARIABLE XSUBPPDIR
+)
+execute_process(
+	COMMAND ${PERL_EXECUTABLE} -V:usemultiplicity
+	OUTPUT_VARIABLE PERL_MULTIPLICITY
+)
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/SPI.c"
+	MAIN_DEPENDENCY SPI.xs plperl_helpers.h
+	COMMAND ${PERL_EXECUTABLE} ${XSUBPPDIR}/ExtUtils/xsubpp -typemap ${PERL_PRIVLIB}/ExtUtils/typemap SPI.xs > "${CMAKE_CURRENT_SOURCE_DIR}/SPI.c"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/Util.c"
+	MAIN_DEPENDENCY Util.xs plperl_helpers.h
+	COMMAND ${PERL_EXECUTABLE} ${XSUBPPDIR}/ExtUtils/xsubpp -typemap ${PERL_PRIVLIB}/ExtUtils/typemap Util.xs > "${CMAKE_CURRENT_SOURCE_DIR}/Util.c"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/perlchunks.h"
+	MAIN_DEPENDENCY plc_perlboot.pl plc_trusted.pl
+	COMMAND ${PERL_EXECUTABLE} text2macro.pl "--strip='^(\\#.*|\\s*)$$'" plc_perlboot.pl plc_trusted.pl > "${CMAKE_CURRENT_SOURCE_DIR}/perlchunks.h"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/plperl_opmask.h"
+	MAIN_DEPENDENCY plperl_opmask.pl
+	COMMAND ${PERL_EXECUTABLE} plperl_opmask.pl "${CMAKE_CURRENT_SOURCE_DIR}/plperl_opmask.h"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+set(plname "plperl")
+
+set(plperl_SRCS
+	"${CMAKE_CURRENT_SOURCE_DIR}/perlchunks.h"
+	"${CMAKE_CURRENT_SOURCE_DIR}/plperl_opmask.h"
+	plperl.c
+	SPI.c
+	Util.c
+)
+
+include_directories(${PERL_INCLUDE_PATH})
+add_library(${plname} MODULE ${plperl_SRCS})
+target_link_libraries(${plname} postgres ${PERL_LIBRARY} ${PERL_LDFLAGS})
+if (MSVC)
+	gen_def(${plname})
+	target_link_libraries(${plname} port)
+endif()
+set_target_properties(${plname} PROPERTIES PREFIX "")
+add_dependencies(${plname} postgres)
+
+install(TARGETS ${plname}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+		plperl.control
+		plperl--1.0.sql
+		plperl--unpackaged--1.0.sql
+		plperlu.control
+		plperlu--1.0.sql
+		plperlu--unpackaged--1.0.sql
+		DESTINATION ${PGSHAREDIR}/extension)
+install(FILES
+	plperl.h
+	ppport.h
+	DESTINATION ${INCLUDEDIRSERVER})
+
+set(PL_TESTDB pl_regression)
+set(REGRESS_OPTS --dbname=${PL_TESTDB} --load-extension=plperl --load-extension=plperlu)
+set(plregres_files
+	plperl
+	plperl_lc
+	plperl_trigger
+	plperl_shared
+	plperl_elog
+	plperl_util
+	plperl_init
+	plperlu
+	plperl_array
+)
+
+if(PERL_MULTIPLICITY STREQUAL "usemultiplicity='define'")
+	set(plregres_files ${plregres_files} plperl_plperlu)
+endif()
+
+REGRESS_CHECK(plperl "${REGRESS_OPTS}" "${plregres_files}")
+
+if(ENABLE_NLS)
+	MAKE_MO(plperl "cs;de;es;fr;it;ja;ko;pl;pt_BR;ro;ru;sv;tr;zh_CN;zh_TW")
+endif()
diff --git a/src/pl/plpgsql/src/CMakeLists.txt b/src/pl/plpgsql/src/CMakeLists.txt
new file mode 100644
index 0000000..29efb6a
--- /dev/null
+++ b/src/pl/plpgsql/src/CMakeLists.txt
@@ -0,0 +1,37 @@
+BISON_TARGET(PLPGSQL_PARSER pl_gram.y ${CMAKE_CURRENT_SOURCE_DIR}/pl_gram.c COMPILE_FLAGS "-d")
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/plerrcodes.h"
+	MAIN_DEPENDENCY generate-plerrcodes.pl
+	COMMAND ${PERL_EXECUTABLE} "generate-plerrcodes.pl" "${PROJECT_SOURCE_DIR}/src/backend/utils/errcodes.txt" > "${CMAKE_CURRENT_SOURCE_DIR}/plerrcodes.h"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+set(plpgsql_SRCS
+	pl_handler.c
+	pl_comp.c
+	plerrcodes.h
+	pl_exec.c
+	pl_funcs.c
+	pl_scanner.c
+	${BISON_PLPGSQL_PARSER_OUTPUT_SOURCE}
+)
+
+add_library(plpgsql MODULE ${plpgsql_SRCS})
+target_link_libraries(plpgsql postgres)
+if (MSVC)
+	gen_def(plpgsql)
+	target_link_libraries(plpgsql port pgcommon)
+endif()
+set_target_properties(plpgsql PROPERTIES PREFIX "")
+add_dependencies(plpgsql postgres)
+
+install(TARGETS plpgsql
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES plpgsql.control plpgsql--1.0.sql plpgsql--unpackaged--1.0.sql
+		DESTINATION ${PGSHAREDIR}/extension)
+
+if(ENABLE_NLS)
+	MAKE_MO(plpgsql "cs;de;es;fr;it;ja;ko;pl;pt_BR;ro;ru;zh_CN;zh_TW")
+endif()
diff --git a/src/pl/plpython/CMakeLists.txt b/src/pl/plpython/CMakeLists.txt
new file mode 100644
index 0000000..e443e81
--- /dev/null
+++ b/src/pl/plpython/CMakeLists.txt
@@ -0,0 +1,92 @@
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/spiexceptions.h"
+	MAIN_DEPENDENCY generate-spiexceptions.pl
+	COMMAND ${PERL_EXECUTABLE} "generate-spiexceptions.pl" "${PROJECT_SOURCE_DIR}/src/backend/utils/errcodes.txt" > "${CMAKE_CURRENT_SOURCE_DIR}/spiexceptions.h"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+set(plname "plpython${PYTHON_VERSION_MAJOR}")
+
+set(plpython_SRCS
+	"${CMAKE_CURRENT_SOURCE_DIR}/spiexceptions.h"
+	plpy_cursorobject.c
+	plpy_elog.c
+	plpy_exec.c
+	plpy_main.c
+	plpy_planobject.c
+	plpy_plpymodule.c
+	plpy_procedure.c
+	plpy_resultobject.c
+	plpy_spi.c
+	plpy_subxactobject.c
+	plpy_typeio.c
+	plpy_util.c
+)
+
+include_directories(${PYTHON_INCLUDE_DIRS})
+add_library(${plname} MODULE ${plpython_SRCS})
+target_link_libraries(${plname} postgres ${PYTHON_LIBRARIES})
+if (MSVC)
+	gen_def(${plname})
+	target_link_libraries(${plname} port)
+endif()
+if(MINGW)
+	set_target_properties(${plname} PROPERTIES LINK_FLAGS -Wl,--out-implib=libmin${plname}.dll.a)
+endif()
+set_target_properties(${plname} PROPERTIES PREFIX "")
+add_dependencies(${plname} postgres)
+
+install(TARGETS ${plname}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+	plpython2u.control
+	plpython2u--1.0.sql
+	plpython2u--unpackaged--1.0.sql
+	plpython3u.control
+	plpython3u--1.0.sql
+	plpython3u--unpackaged--1.0.sql
+	plpythonu.control
+	plpythonu--1.0.sql
+	plpythonu--unpackaged--1.0.sql
+	DESTINATION ${PGSHAREDIR}/extension)
+
+set(PL_TESTDB pl_regression)
+set(REGRESS_OPTS --dbname=${PL_TESTDB})
+if (PYTHON_VERSION_MAJOR EQUAL 2)
+	set(REGRESS_OPTS ${REGRESS_OPTS} --load-extension=plpythonu)
+endif()
+
+set(plregres_files
+	plpython_schema
+	plpython_populate
+	plpython_test
+	plpython_do
+	plpython_global
+	plpython_import
+	plpython_spi
+	plpython_newline
+	plpython_void
+	plpython_params
+	plpython_setof
+	plpython_record
+	plpython_trigger
+	plpython_types
+	plpython_error
+	plpython_unicode
+	plpython_quote
+	plpython_composite
+	plpython_subtransaction
+	plpython_drop
+)
+
+if(PYTHON_VERSION_MAJOR EQUAL 3)
+	replace_python_files("${plregres_files}")
+	REGRESS_CHECK(plpython "${REGRESS_OPTS}" "${regress_files3}")
+else()
+	REGRESS_CHECK(plpython "${REGRESS_OPTS}" "${plregres_files}")
+endif()
+
+if(ENABLE_NLS)
+	MAKE_MO(plpython "cs;de;es;fr;it;ja;ko;pl;pt_BR;ru;zh_CN")
+endif()
diff --git a/src/pl/tcl/CMakeLists.txt b/src/pl/tcl/CMakeLists.txt
new file mode 100644
index 0000000..b75d44f
--- /dev/null
+++ b/src/pl/tcl/CMakeLists.txt
@@ -0,0 +1,49 @@
+set(plname "pltcl")
+
+include_directories(${TCL_INCLUDE_PATH})
+
+add_custom_command(
+	OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/pltclerrcodes.h"
+	MAIN_DEPENDENCY generate-pltclerrcodes.pl
+	COMMAND ${PERL_EXECUTABLE} "generate-pltclerrcodes.pl" "${PROJECT_SOURCE_DIR}/src/backend/utils/errcodes.txt" > "${CMAKE_CURRENT_SOURCE_DIR}/pltclerrcodes.h"
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_target(gen_pltclerrcodes DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/pltclerrcodes.h")
+
+add_library(${plname} MODULE
+	pltcl.c
+)
+
+target_link_libraries(${plname} postgres ${TCL_LIBRARY})
+if (MSVC)
+	gen_def(${plname})
+	target_link_libraries(${plname} port)
+endif()
+set_target_properties(${plname} PROPERTIES PREFIX "")
+add_dependencies(${plname} postgres gen_pltclerrcodes)
+
+install(TARGETS ${plname}
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+install(FILES
+		${plname}.control
+		${plname}--1.0.sql
+		${plname}--unpackaged--1.0.sql
+		${plname}u.control
+		${plname}u--1.0.sql
+		${plname}u--unpackaged--1.0.sql
+		DESTINATION ${PGSHAREDIR}/extension)
+
+set(PL_TESTDB pl_regression)
+set(REGRESS_OPTS --dbname=${PL_TESTDB} --load-extension=pltcl)
+set(plregres_files
+	pltcl_setup
+	pltcl_queries
+	pltcl_unicode
+)
+REGRESS_CHECK(pltcl "${REGRESS_OPTS}" "${plregres_files}")
+
+if(ENABLE_NLS)
+	MAKE_MO(pltcl "cs;de;es;fr;it;ja;ko;pl;pt_BR;ro;ru;tr;zh_CN;zh_TW")
+endif()
diff --git a/src/port/CMakeLists.txt b/src/port/CMakeLists.txt
new file mode 100644
index 0000000..eae5a31
--- /dev/null
+++ b/src/port/CMakeLists.txt
@@ -0,0 +1,165 @@
+if(USE_SSE42_CRC32C)
+	SET(PG_CRC32C_OBJS pg_crc32c_sse42.c)
+elseif(USE_SSE42_CRC32C_WITH_RUNTIME_CHECK)
+	SET(PG_CRC32C_OBJS
+		pg_crc32c_sse42.c
+		pg_crc32c_sb8.c
+		pg_crc32c_choose.c
+	)
+elseif(USE_SLICING_BY_8_CRC32C)
+	SET(PG_CRC32C_OBJS pg_crc32c_sb8.c)
+endif()
+
+set_source_files_properties(pg_crc32c_sse42.c PROPERTIES COMPILE_FLAGS "${CFLAGS_SSE42}")
+
+set(port_SRCS
+	chklocale.c
+	erand48.c
+	inet_net_ntop.c
+	noblock.c
+	path.c
+	pgcheckdir.c
+	pgmkdirp.c
+	pgsleep.c
+	pgstrcasecmp.c
+	pqsignal.c
+	qsort.c
+	qsort_arg.c
+	quotes.c
+	sprompt.c
+	tar.c
+	thread.c
+	pg_putenv_proxy.c
+	${fallback_SRCS}
+	${PG_CRC32C_OBJS}
+)
+
+if(NOT HAVE_FLS)
+	set(port_SRCS
+		${port_SRCS}
+		fls.c
+	)
+endif(NOT HAVE_FLS)
+
+if(NOT HAVE_ISINF)
+	set(port_SRCS
+		${port_SRCS}
+		isinf.c
+	)
+endif()
+
+if(NOT HAVE_MKDTEMP)
+	set(port_SRCS
+		${port_SRCS}
+		mkdtemp.c
+	)
+endif()
+
+if(NOT HAVE_GETOPT)
+	set(port_SRCS
+		${port_SRCS}
+		getopt.c
+	)
+endif()
+
+if(NOT (HAVE_STRUCT_OPTION AND HAVE_GETOPT_LONG))
+	set(port_SRCS
+		${port_SRCS}
+		getopt_long.c
+	)
+endif()
+
+if(NOT HAVE_GETTIMEOFDAY)
+	set(port_SRCS
+		${port_SRCS}
+		gettimeofday.c
+	)
+endif()
+
+if(NOT HAVE_INET_ATON)
+	set(port_SRCS
+		${port_SRCS}
+		inet_aton.c
+	)
+endif()
+
+if(NOT HAVE_RANDOM)
+	set(port_SRCS
+		${port_SRCS}
+		random.c
+	)
+endif()
+
+if(NOT HAVE_SRANDOM)
+	set(port_SRCS
+		${port_SRCS}
+		srandom.c
+	)
+endif()
+
+if(USE_REPL_SNPRINTF)
+	set(port_SRCS
+		${port_SRCS}
+		snprintf.c
+	)
+endif()
+
+if(NOT HAVE_RINT)
+	set(port_SRCS
+		${port_SRCS}
+		rint.c
+	)
+endif()
+
+if(MSVC OR MINGW)
+	set(port_SRCS
+		${port_SRCS}
+		dirmod.c
+		kill.c
+		open.c
+		getrusage.c
+		system.c
+		win32security.c
+		win32env.c
+		win32error.c
+		win32setlocale.c
+	)
+endif()
+
+if(MSVC)
+	set(port_SRCS
+		${port_SRCS}
+		dirent.c
+	)
+endif()
+
+if(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
+	# Solaris' getopt() doesn't do what we want for long options, so always use
+	# our version on that platform.
+	set(port_SRCS
+		${port_SRCS}
+		getopt.c
+	)
+endif()
+
+if(NOT HAVE_CRYPT)
+	set(port_SRCS
+		${port_SRCS}
+		crypt.c
+	)
+endif()
+
+add_library(port_srv STATIC ${port_SRCS})
+add_library(port STATIC ${port_SRCS})
+
+add_dependencies(port_srv gen_errorcodes)
+add_dependencies(port gen_errorcodes)
+
+# because port used in shared libraries
+set_property(TARGET port PROPERTY POSITION_INDEPENDENT_CODE TRUE)
+
+
+target_compile_definitions(port PRIVATE -DFRONTEND)
+set_source_files_properties(thread.c PROPERTIES COMPILE_FLAGS ${PTHREAD_CFLAGS})
+install(TARGETS port
+		ARCHIVE DESTINATION ${LIBDIR})
diff --git a/src/port/Makefile b/src/port/Makefile
index bc9b63a..40dcdab 100644
--- a/src/port/Makefile
+++ b/src/port/Makefile
@@ -33,7 +33,7 @@ LIBS += $(PTHREAD_LIBS)
 OBJS = $(LIBOBJS) $(PG_CRC32C_OBJS) chklocale.o erand48.o inet_net_ntop.o \
 	noblock.o path.o pgcheckdir.o pgmkdirp.o pgsleep.o \
 	pgstrcasecmp.o pqsignal.o \
-	qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o
+	qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o pg_putenv_proxy.o
 
 # foo_srv.o and foo.o are both built from foo.c, but only foo.o has -DFRONTEND
 OBJS_SRV = $(OBJS:%.o=%_srv.o)
diff --git a/src/port/pg_putenv_proxy.c b/src/port/pg_putenv_proxy.c
new file mode 100644
index 0000000..ae10852
--- /dev/null
+++ b/src/port/pg_putenv_proxy.c
@@ -0,0 +1,26 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_putenv_proxy.c
+ *	  pg_putenv_proxy for putenv
+ *
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *	  src/port/pg_putenv_proxy.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+int
+pg_putenv_proxy(char *envval)
+{
+#ifdef WIN32
+	return pgwin32_putenv(envval);
+#else
+	return putenv(envval);
+#endif
+}
\ No newline at end of file
diff --git a/src/port/unsetenv.c b/src/port/unsetenv.c
index bb7ab43..0c66ef3 100644
--- a/src/port/unsetenv.c
+++ b/src/port/unsetenv.c
@@ -44,7 +44,7 @@ unsetenv(const char *name)
 
 	/* Override the existing setting by forcibly defining the var */
 	sprintf(envstr, "%s=", name);
-	putenv(envstr);
+	pg_putenv_proxy(envstr);
 
 	/* Now we can clobber the variable definition this way: */
 	strcpy(envstr, "=");
@@ -53,5 +53,5 @@ unsetenv(const char *name)
 	 * This last putenv cleans up if we have multiple zero-length names as a
 	 * result of unsetting multiple things.
 	 */
-	putenv(envstr);
+	pg_putenv_proxy(envstr);
 }
diff --git a/src/test/isolation/CMakeLists.txt b/src/test/isolation/CMakeLists.txt
new file mode 100644
index 0000000..2b13ee4
--- /dev/null
+++ b/src/test/isolation/CMakeLists.txt
@@ -0,0 +1,64 @@
+include_directories(BEFORE
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/port"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+	"${PROJECT_SOURCE_DIR}/src/test/regress"
+)
+
+add_executable(pg_isolation_regress
+	${PROJECT_SOURCE_DIR}/src/test/regress/pg_regress.c
+	isolation_main.c
+)
+
+target_link_libraries(pg_isolation_regress
+	pq
+	pgcommon
+	port
+)
+
+target_compile_definitions(pg_isolation_regress PRIVATE
+	-DHOST_TUPLE="${HOST_TUPLE}"
+	-DSHELLPROG="$ENV{SHELL}"
+)
+
+bison_target(SPEC_PARSER specparse.y ${CMAKE_CURRENT_SOURCE_DIR}/specparse.c)
+flex_target(SPEC_SCANNER specscanner.l  ${CMAKE_CURRENT_SOURCE_DIR}/specscanner.c)
+add_flex_bison_dependency(SPEC_SCANNER SPEC_PARSER)
+set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/specparse.c PROPERTIES OBJECT_DEPENDS ${FLEX_SPEC_SCANNER_OUTPUTS})
+
+add_executable(isolationtester
+	isolationtester.c
+	${BISON_SPEC_PARSER_OUTPUTS}
+)
+
+target_link_libraries(isolationtester
+	pgcommon
+	port
+	pq
+)
+
+add_custom_target(isolation_check
+	COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/tmp_install
+	COMMAND make install DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/tmp_install
+	COMMAND make isolation_installcheck_tmp DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/
+	WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+)
+
+add_custom_target(isolation_installcheck
+	COMMAND ${pg_isolation_regress_check} ${REGRESS_OPTS} --schedule=${CMAKE_CURRENT_SOURCE_DIR}/isolation_schedule --dlpath=$ENV{DESTDIR}${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${EXTRA_TESTS}
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_target(isolation_installcheck_tmp
+	COMMAND ${pg_isolation_regress_check_tmp} ${REGRESS_OPTS} --schedule=${CMAKE_CURRENT_SOURCE_DIR}/isolation_schedule --dlpath=${CMAKE_CURRENT_BINARY_DIR}/tmp_install${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${EXTRA_TESTS}
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+install(TARGETS isolationtester
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+install(TARGETS pg_isolation_regress
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/test/regress/CMakeLists.txt b/src/test/regress/CMakeLists.txt
new file mode 100644
index 0000000..fdc1fc2
--- /dev/null
+++ b/src/test/regress/CMakeLists.txt
@@ -0,0 +1,81 @@
+include_directories(BEFORE
+ 	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/port"
+ 	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+ 	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+add_executable(pg_regress
+	pg_regress.c
+	pg_regress_main.c
+)
+
+target_link_libraries(pg_regress
+	pq
+	pgcommon
+	port
+)
+
+target_compile_definitions(pg_regress PRIVATE
+	-DHOST_TUPLE="${HOST_TUPLE}"
+	-DSHELLPROG="$ENV{SHELL}")
+
+
+add_library(regress MODULE regress.c)
+#target_link_libraries(regress
+#	pq
+#)
+if (MSVC)
+	gen_def(regress)
+endif()
+
+if(MSVC OR MINGW)
+	target_link_libraries(regress postgres port pgcommon)
+endif()
+
+set_target_properties(regress PROPERTIES PREFIX "")
+add_dependencies(regress postgres)
+
+add_custom_target(tablespace-setup
+	COMMAND ${CMAKE_COMMAND} -E remove_directory ./testtablespace
+	COMMAND ${CMAKE_COMMAND} -E make_directory ./testtablespace
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+if(CMAKE_GENERATOR STREQUAL "Ninja")
+	add_custom_target(check
+		COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/tmp_install
+		COMMAND DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/tmp_install ${check_make_command} install
+		COMMAND DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/ ${check_make_command} installcheck_tmp
+		DEPENDS tablespace-setup
+		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+	)
+else()
+	add_custom_target(check
+		COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_CURRENT_BINARY_DIR}/tmp_install
+		COMMAND ${check_make_command} install DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/tmp_install
+		COMMAND ${check_make_command} installcheck_tmp DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/
+		DEPENDS tablespace-setup
+		WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+	)
+endif()
+
+add_custom_target(installcheck
+	COMMAND ${pg_regress_check} ${REGRESS_OPTS} --schedule=${CMAKE_CURRENT_SOURCE_DIR}/parallel_schedule --dlpath=$ENV{DESTDIR}${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${EXTRA_TESTS}
+	DEPENDS tablespace-setup
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+add_custom_target(installcheck_tmp
+	COMMAND ${pg_regress_check_tmp} ${REGRESS_OPTS} --schedule=${CMAKE_CURRENT_SOURCE_DIR}/parallel_schedule --dlpath=${CMAKE_CURRENT_BINARY_DIR}/tmp_install${LIBDIR} ${MAXCONNOPT} ${TEMP_CONF} ${EXTRA_TESTS}
+	DEPENDS tablespace-setup
+	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+)
+
+install(TARGETS pg_regress
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
+
+install(TARGETS regress
+		RUNTIME DESTINATION ${PGBINDIR}
+		LIBRARY DESTINATION ${LIBDIR})
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 14c87c9..f34f32a 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -712,7 +712,7 @@ doputenv(const char *var, const char *val)
 	char	   *s;
 
 	s = psprintf("%s=%s", var, val);
-	putenv(s);
+	pg_putenv_proxy(s);
 }
 
 /*
@@ -721,7 +721,7 @@ doputenv(const char *var, const char *val)
 static void
 initialize_environment(void)
 {
-	putenv("PGAPPNAME=pg_regress");
+	pg_putenv_proxy("PGAPPNAME=pg_regress");
 
 	if (nolocale)
 	{
@@ -744,7 +744,7 @@ initialize_environment(void)
 		 * variables unset; see PostmasterMain().
 		 */
 #if defined(WIN32) || defined(__CYGWIN__) || defined(__darwin__)
-		putenv("LANG=C");
+		pg_putenv_proxy("LANG=C");
 #endif
 	}
 
@@ -756,7 +756,7 @@ initialize_environment(void)
 	 */
 	unsetenv("LANGUAGE");
 	unsetenv("LC_ALL");
-	putenv("LC_MESSAGES=C");
+	pg_putenv_proxy("LC_MESSAGES=C");
 
 	/*
 	 * Set encoding as requested
@@ -769,8 +769,8 @@ initialize_environment(void)
 	/*
 	 * Set timezone and datestyle for datetime-related tests
 	 */
-	putenv("PGTZ=PST8PDT");
-	putenv("PGDATESTYLE=Postgres, MDY");
+	pg_putenv_proxy("PGTZ=PST8PDT");
+	pg_putenv_proxy("PGDATESTYLE=Postgres, MDY");
 
 	/*
 	 * Likewise set intervalstyle to ensure consistent results.  This is a bit
@@ -786,7 +786,7 @@ initialize_environment(void)
 			old_pgoptions = "";
 		new_pgoptions = psprintf("PGOPTIONS=%s %s",
 								 old_pgoptions, my_pgoptions);
-		putenv(new_pgoptions);
+		pg_putenv_proxy(new_pgoptions);
 	}
 
 	if (temp_instance)
diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c
index e7826a4..b38e361 100644
--- a/src/test/regress/regress.c
+++ b/src/test/regress/regress.c
@@ -853,7 +853,7 @@ regress_putenv(PG_FUNCTION_ARGS)
 	envbuf = text_to_cstring((text *) PG_GETARG_POINTER(0));
 	MemoryContextSwitchTo(oldcontext);
 
-	if (putenv(envbuf) != 0)
+	if (pg_putenv_proxy(envbuf) != 0)
 		elog(ERROR, "could not set environment variable: %m");
 
 	PG_RETURN_VOID();
diff --git a/src/timezone/CMakeLists.txt b/src/timezone/CMakeLists.txt
new file mode 100644
index 0000000..4f60ce9
--- /dev/null
+++ b/src/timezone/CMakeLists.txt
@@ -0,0 +1,76 @@
+#TODO compile timezones
+
+include_directories(
+	"${PROJECT_SOURCE_DIR}/src/include/libpq"
+	"${PROJECT_SOURCE_DIR}/src/interfaces/libpq"
+	"${PROJECT_SOURCE_DIR}/src/bin/pg_dump"
+)
+
+
+
+set(tzdata 
+	${CMAKE_CURRENT_SOURCE_DIR}/data/africa
+	${CMAKE_CURRENT_SOURCE_DIR}/data/antarctica
+	${CMAKE_CURRENT_SOURCE_DIR}/data/asia
+	${CMAKE_CURRENT_SOURCE_DIR}/data/australasia
+	${CMAKE_CURRENT_SOURCE_DIR}/data/europe
+	${CMAKE_CURRENT_SOURCE_DIR}/data/northamerica
+	${CMAKE_CURRENT_SOURCE_DIR}/data/southamerica
+	${CMAKE_CURRENT_SOURCE_DIR}/data/pacificnew
+	${CMAKE_CURRENT_SOURCE_DIR}/data/etcetera
+	${CMAKE_CURRENT_SOURCE_DIR}/data/factory
+	${CMAKE_CURRENT_SOURCE_DIR}/data/backward
+	${CMAKE_CURRENT_SOURCE_DIR}/data/systemv
+)
+
+add_executable(zic
+	zic.c
+)
+target_link_libraries(zic
+	port
+)
+
+if(MSVC)
+	foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
+		string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
+		set_target_properties(zic PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_CURRENT_BINARY_DIR} )
+		set_target_properties(zic PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_CURRENT_BINARY_DIR} )
+		set_target_properties(zic PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_CURRENT_BINARY_DIR} )
+	endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
+endif(MSVC)
+
+
+install(FILES 
+	tznames/Africa.txt
+	tznames/America.txt
+	tznames/Antarctica.txt
+	tznames/Asia.txt
+	tznames/Atlantic.txt
+	tznames/Australia.txt
+	tznames/Etc.txt
+	tznames/Europe.txt
+	tznames/Indian.txt
+	tznames/Pacific.txt
+	tznames/Default
+	tznames/Australia
+	tznames/India
+DESTINATION ${PGSHAREDIR}/timezonesets)
+
+function(JOIN VALUES GLUE OUTPUT)
+  string (REGEX REPLACE "([^\\]|^);" "\\1${GLUE}" _TMP_STR "${VALUES}")
+  string (REGEX REPLACE "[\\](.)" "\\1" _TMP_STR "${_TMP_STR}") #fixes escaping
+  set (${OUTPUT} "${_TMP_STR}" PARENT_SCOPE)
+endfunction()
+
+if(MINGW)
+	string(SUBSTRING ${PGSHAREDIR} 2 -1 PGSHAREDIR_ZIC)
+else()
+	set(PGSHAREDIR_ZIC ${PGSHAREDIR})
+endif()
+
+JOIN("${tzdata}" " " tzdata_string)
+install(
+	CODE "message('${CMAKE_CURRENT_BINARY_DIR}/zic${CMAKE_EXECUTABLE_SUFFIX} \$ENV{DESTDIR}${PGSHAREDIR_ZIC}/timezone -p US/Eastern ${tzdata_string}')"
+	CODE "execute_process(COMMAND ${CMAKE_CURRENT_BINARY_DIR}/zic${CMAKE_EXECUTABLE_SUFFIX} -d \"\$ENV{DESTDIR}${PGSHAREDIR_ZIC}/timezone\" -p \"US/Eastern\" ${tzdata_string})"
+)
+
diff --git a/src/timezone/Makefile b/src/timezone/Makefile
index bed5727..348c79e 100644
--- a/src/timezone/Makefile
+++ b/src/timezone/Makefile
@@ -54,6 +54,7 @@ zic: $(ZICOBJS) | submake-libpgport
 
 install: all installdirs
 ifeq (,$(with_system_tzdata))
+	echo "-d '$(DESTDIR)$(datadir)/timezone' -p '$(POSIXRULES)' $(TZDATAFILES)"
 	$(ZIC) -d '$(DESTDIR)$(datadir)/timezone' -p '$(POSIXRULES)' $(TZDATAFILES)
 endif
 	$(MAKE) -C tznames $@
diff --git a/src/tools/msvc/gendef.pl b/src/tools/msvc/gendef.pl
index a6c43c2..e35ed5a 100644
--- a/src/tools/msvc/gendef.pl
+++ b/src/tools/msvc/gendef.pl
@@ -154,23 +154,21 @@ sub usage
 }
 
 usage()
-  unless scalar(@ARGV) == 2
-	  && (   ($ARGV[0] =~ /\\([^\\]+$)/)
-		  && ($ARGV[1] eq 'Win32' || $ARGV[1] eq 'x64'));
-my $defname  = uc $1;
-my $deffile  = "$ARGV[0]/$defname.def";
-my $platform = $ARGV[1];
+  unless scalar(@ARGV) == 3
+	  && ($ARGV[2] eq 'Win32' || $ARGV[2] eq 'x64');
+my $deffile  = $ARGV[1];
+my $platform = $ARGV[2];
 
 # if the def file exists and is newer than all input object files, skip
 # its creation
 if (-f $deffile
 	&& (-M $deffile > max(map { -M } <$ARGV[0]/*.obj>)))
 {
-	print "Not re-generating $defname.DEF, file already exists.\n";
+	print "Not re-generating .DEF, file already exists.\n";
 	exit(0);
 }
 
-print "Generating $defname.DEF from directory $ARGV[0], platform $platform\n";
+print "Generating .DEF from directory $ARGV[0], platform $platform\n";
 
 my %def = ();
 
