Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lfittl committed Jan 1, 2024
1 parent 3192a2a commit eff3257
Show file tree
Hide file tree
Showing 22 changed files with 338 additions and 1,831 deletions.
79 changes: 17 additions & 62 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ PGDIRBZ2 = $(root_dir)/tmp/postgres.tar.bz2

PG_VERSION = 16.1
PG_VERSION_MAJOR = $(call word-dot,$(PG_VERSION),1)
PG_VERSION_NUM = 160001
PROTOC_VERSION = 25.1

VERSION = 5.0.0
Expand Down Expand Up @@ -126,68 +127,14 @@ $(PGDIR):
cd $(PGDIR); make -C src/port pg_config_paths.h
cd $(PGDIR); make -C src/backend generated-headers
cd $(PGDIR); make -C src/backend parser-recursive # Triggers copying of includes to where they belong, as well as generating gram.c/scan.c
# This causes compatibility problems on some Linux distros, with "xlocale.h" not being available
echo "#undef HAVE_LOCALE_T" >> $(PGDIR)/src/include/pg_config.h
echo "#undef LOCALE_T_IN_XLOCALE" >> $(PGDIR)/src/include/pg_config.h
echo "#undef WCSTOMBS_L_IN_XLOCALE" >> $(PGDIR)/src/include/pg_config.h
# Support 32-bit systems without reconfiguring
echo "#undef PG_INT128_TYPE" >> $(PGDIR)/src/include/pg_config.h
# Support gcc earlier than 4.6.0 without reconfiguring
echo "#undef HAVE__STATIC_ASSERT" >> $(PGDIR)/src/include/pg_config.h
# Avoid problems with static asserts
echo "#undef StaticAssertDecl" >> $(PGDIR)/src/include/c.h
echo "#define StaticAssertDecl(condition, errmessage)" >> $(PGDIR)/src/include/c.h
# Avoid dependency on execinfo (requires extra library on musl-libc based systems)
echo "#undef HAVE_EXECINFO_H" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_BACKTRACE_SYMBOLS" >> $(PGDIR)/src/include/pg_config.h
# Avoid dependency on hardware popcount instructions (POPQNTQ) on x86
echo "#undef HAVE_X86_64_POPCNTQ" >> $(PGDIR)/src/include/pg_config.h
# Avoid dependency on cpuid.h (only supported on x86 systems)
echo "#undef HAVE__GET_CPUID" >> $(PGDIR)/src/include/pg_config.h
# Avoid CRC extension usage to ensure we are not architecture-dependent
echo "#undef USE_ARMV8_CRC32C" >> $(PGDIR)/src/include/pg_config.h
echo "#undef USE_SSE42_CRC32C_WITH_RUNTIME_CHECK" >> $(PGDIR)/src/include/pg_config.h
# Ensure we don't fail on systems that have strchrnul support (FreeBSD and NetBSD)
echo "#include <stdlib.h>" >> $(PGDIR)/src/include/pg_config.h
echo "#if defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 38) || __GLIBC__ > 2))" >> $(PGDIR)/src/include/pg_config.h
echo "#define HAVE_STRCHRNUL" >> $(PGDIR)/src/include/pg_config.h
echo "#endif" >> $(PGDIR)/src/include/pg_config.h
# Optionally support 32-bit
echo "#if defined(_WIN32) || __SIZEOF_POINTER__ == 4" >> $(PGDIR)/src/include/pg_config.h
echo "#undef ALIGNOF_DOUBLE" >> $(PGDIR)/src/include/pg_config.h
echo "#define ALIGNOF_DOUBLE 4" >> $(PGDIR)/src/include/pg_config.h
echo "#undef ALIGNOF_LONG" >> $(PGDIR)/src/include/pg_config.h
echo "#define ALIGNOF_LONG 4" >> $(PGDIR)/src/include/pg_config.h
echo "#define ALIGNOF_LONG_LONG_INT 4" >> $(PGDIR)/src/include/pg_config.h
echo "#undef ALIGNOF_PG_INT128_TYPE" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_LONG_INT_64" >> $(PGDIR)/src/include/pg_config.h
echo "#define HAVE_LONG_LONG_INT_64 1" >> $(PGDIR)/src/include/pg_config.h
echo "#undef INT64_MODIFIER" >> $(PGDIR)/src/include/pg_config.h
echo "#define INT64_MODIFIER \"ll\"" >> $(PGDIR)/src/include/pg_config.h
echo "#undef PG_INT128_TYPE" >> $(PGDIR)/src/include/pg_config.h
echo "#undef PG_INT64_TYPE" >> $(PGDIR)/src/include/pg_config.h
echo "#define PG_INT64_TYPE long long int" >> $(PGDIR)/src/include/pg_config.h
echo "#undef SIZEOF_LONG" >> $(PGDIR)/src/include/pg_config.h
echo "#define SIZEOF_LONG 4" >> $(PGDIR)/src/include/pg_config.h
echo "#undef SIZEOF_SIZE_T" >> $(PGDIR)/src/include/pg_config.h
echo "#define SIZEOF_SIZE_T 4" >> $(PGDIR)/src/include/pg_config.h
echo "#undef SIZEOF_VOID_P" >> $(PGDIR)/src/include/pg_config.h
echo "#define SIZEOF_VOID_P 4" >> $(PGDIR)/src/include/pg_config.h
echo "#endif" >> $(PGDIR)/src/include/pg_config.h
# Windows
echo "#if defined(_WIN32) || defined(_WIN64)" >> $(PGDIR)/src/include/pg_config.h
echo "#define WIN32 1" >> $(PGDIR)/src/include/pg_config.h
echo "#define __thread __declspec( thread )" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_STRINGS_H" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_VISIBILITY_ATTRIBUTE" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE__BUILTIN_CONSTANT_P" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE__BUILTIN_TYPES_COMPATIBLE_P" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_TYPEOF" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_GETOPT_H" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_SYSLOG" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_PTHREAD" >> $(PGDIR)/src/include/pg_config.h
echo "#undef HAVE_PTHREAD_IS_THREADED_NP" >> $(PGDIR)/src/include/pg_config.h
echo "#endif" >> $(PGDIR)/src/include/pg_config.h
# Add pg_config.h overrides
cat scripts/pg_config_overrides.h >> $(PGDIR)/src/include/pg_config.h
# Only define strlcpy when needed
sed -i "" '$(shell echo 's/\#include "c.h"/#include "c.h"\n#if HAVE_DECL_STRLCPY == 0/')' $(PGDIR)/src/port/strlcpy.c
echo "#endif // HAVE_DECL_STRLCPY == 0" >> $(PGDIR)/src/port/strlcpy.c

extract_source: $(PGDIR)
-@ $(RM) -rf ./src/postgres/
Expand All @@ -203,10 +150,18 @@ extract_source: $(PGDIR)
cp $(PGDIR)/src/include/port/win32_port.h ./src/postgres/include/port
cp -a $(PGDIR)/src/include/port/win32 ./src/postgres/include/port
cp -a $(PGDIR)/src/include/port/win32_msvc ./src/postgres/include/port
# Override OS-specific pg_config_os.h to only load Win32 logic (the primary port logic that matters for libpg_query), if needed
echo "#if defined(_WIN32) || defined(_WIN64)" > ./src/postgres/include/pg_config_os.h
echo "#include \"port/win32.h\"" >> ./src/postgres/include/pg_config_os.h
# Load windows.h early so ERROR is defined so win_32.port.h can undef it (loaded via postgres.h/c.h)
echo "#include <windows.h>" >> ./src/postgres/include/pg_config_os.h
echo "#endif" >> ./src/postgres/include/pg_config_os.h
# Adjust version string to ignore differences in build environments
sed -i "" '$(shell echo 's/\#define PG_VERSION_STR .*/#define PG_VERSION_STR "PostgreSQL $(PG_VERSION) \(libpg_query\)"/')' ./src/postgres/include/pg_config.h
# Copy version information so its easily accessible
sed -i "" '$(shell echo 's/\#define PG_MAJORVERSION .*/'`grep "\#define PG_MAJORVERSION " ./src/postgres/include/pg_config.h`'/')' pg_query.h
sed -i "" '$(shell echo 's/\#define PG_VERSION .*/'`grep "\#define PG_VERSION " ./src/postgres/include/pg_config.h`'/')' pg_query.h
sed -i "" '$(shell echo 's/\#define PG_VERSION_NUM .*/'`grep "\#define PG_VERSION_NUM " ./src/postgres/include/pg_config.h`'/')' pg_query.h
sed -i "" '$(shell echo 's/\#define PG_MAJORVERSION .*/#define PG_MAJORVERSION "$(PG_VERSION_MAJOR)"/')' pg_query.h
sed -i "" '$(shell echo 's/\#define PG_VERSION .*/#define PG_VERSION "$(PG_VERSION)"/')' pg_query.h
sed -i "" '$(shell echo 's/\#define PG_VERSION_NUM .*/#define PG_VERSION_NUM $(PG_VERSION_NUM)/')' pg_query.h
# Copy regress SQL files so we can use them in tests
rm -f ./test/sql/postgres_regress/*.sql
cp $(PGDIR)/src/test/regress/sql/*.sql ./test/sql/postgres_regress/
Expand Down
39 changes: 34 additions & 5 deletions scripts/extract_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def run
@basepath + 'src/port/win32env.c', # Win32 only
@basepath + 'src/port/win32security.c', # Win32 only
@basepath + 'src/port/gettimeofday.c', # Win32 only
@basepath + 'src/port/strlcpy.c', # Not needed and conflicts with available function
@basepath + 'src/port/strnlen.c', # Not needed and conflicts with available function
@basepath + 'src/port/strlcat.c', # Not needed and conflicts with available function
@basepath + 'src/port/unsetenv.c', # Not needed and conflicts with available function
@basepath + 'src/port/getaddrinfo.c', # Not needed and conflicts with available function
Expand Down Expand Up @@ -214,15 +214,30 @@ def self.analysis_filename(filename, basepath)

def analyze_file(file)
index = FFI::Clang::Index.new(true, true)
translation_unit = index.parse_translation_unit(file, [
flags = [
'-I', @basepath + 'src/include',
'-I', '/usr/local/opt/openssl/include',
'-I', `xcrun --sdk macosx --show-sdk-path`.strip + '/usr/include',
'-DDLSUFFIX=".bundle"',
'-g',
'-DUSE_ASSERT_CHECKING',
'-DEXEC_BACKEND' # Ensure we remove any unnecessary EXEC_BACKEND (Win32) code
])
# EXEC_BACKEND is used on Windows, and can always be safely set during code analysis
'-DEXEC_BACKEND',
]

# For certain files, use WIN32 define - we can't always do this since it pulls unnecessary code in other cases
if file == @basepath + 'src/backend/utils/error/elog.c' || file == @basepath + 'src/backend/utils/mb/mbutils.c'
flags << '-DWIN32'
flags << '-D__CYGWIN__' # Avoid pulling in win32_port.h (which includes a bunch of system headers we don't actually have)
end

# To override built-ins, we must avoid pulling in any system headers, so pretend we already defined c.h
if file == @basepath + 'src/port/strlcpy.c'
flags << '-DC_H'
end

translation_unit = index.parse_translation_unit(file, flags)

cursor = translation_unit.cursor

func_cursor = nil
Expand Down Expand Up @@ -535,6 +550,20 @@ def write_out
}
)) # We're always working with fn_rettype = VOIDOID, due to our use of plpgsql_compile_inline

# Mocks REQUIRED for Windows support
runner.mock('write_stderr', %(
void
write_stderr(const char *fmt,...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fflush(stderr);
va_end(ap);
}
)) # Avoid pulling in write_console/write_eventlog, and instead always output to stderr (like on POSIX)
runner.mock('should_output_to_client', 'static inline bool should_output_to_client(int elevel) { return false; }') # Avoid pulling in postmaster.c, which has a bunch of Windows-specific code hidden behind a define

## ---

# SQL Parsing
Expand Down Expand Up @@ -593,8 +622,8 @@ def write_out
runner.deep_resolve('pg_leftmost_one_pos')
runner.deep_resolve('pg_rightmost_one_pos')
runner.deep_resolve('pg_number_of_ones')
runner.deep_resolve('strlcpy')
runner.deep_resolve('GetMessageEncoding')
runner.deep_resolve('__builtin___strlcpy_chk') # aka "strlcpy" on Windows

runner.write_out

Expand Down
145 changes: 145 additions & 0 deletions scripts/pg_config_overrides.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* This causes compatibility problems on some Linux distros, with "xlocale.h" not being available */
#undef HAVE_LOCALE_T
#undef LOCALE_T_IN_XLOCALE
#undef WCSTOMBS_L_IN_XLOCALE

/* Support gcc earlier than 4.6.0 and MSVC */
#undef HAVE__STATIC_ASSERT

/* Avoid dependency on execinfo (requires extra library on musl-libc based systems, not supported on Windows) */
#undef HAVE_EXECINFO_H
#undef HAVE_BACKTRACE_SYMBOLS

/* Avoid dependency on hardware popcount instructions (POPQNTQ) on x86 */
#undef HAVE_X86_64_POPCNTQ

/* Avoid dependency on cpuid.h (only supported on x86 systems) */
#undef HAVE__GET_CPUID

/* Avoid CRC extension usage to ensure we are not architecture-dependent */
#undef USE_ARMV8_CRC32C
#undef USE_SSE42_CRC32C_WITH_RUNTIME_CHECK

/* Ensure we do not fail on systems that have strchrnul support (FreeBSD, NetBSD and newer glibc) */
#include <stdlib.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 38) || __GLIBC__ > 2))
#define HAVE_STRCHRNUL
#endif

/* 32-bit */
#if defined(_WIN32) || __SIZEOF_POINTER__ == 4
#undef ALIGNOF_DOUBLE
#define ALIGNOF_DOUBLE 4
#undef ALIGNOF_LONG
#define ALIGNOF_LONG 4
#define ALIGNOF_LONG_LONG_INT 4
#undef ALIGNOF_PG_INT128_TYPE
#undef HAVE_LONG_INT_64
#define HAVE_LONG_LONG_INT_64 1
#undef INT64_MODIFIER
#define INT64_MODIFIER "ll"
#undef PG_INT128_TYPE
#undef PG_INT64_TYPE
#define PG_INT64_TYPE long long int
#undef SIZEOF_LONG
#define SIZEOF_LONG 4
#undef SIZEOF_OFF_T
#define SIZEOF_OFF_T 4
#undef SIZEOF_SIZE_T
#define SIZEOF_SIZE_T 4
#undef SIZEOF_VOID_P
#define SIZEOF_VOID_P 4
#endif

/* Windows */
#if defined(_WIN32) || defined(_WIN64)
#undef HAVE_COPYFILE
#undef HAVE_COPYFILE_H
#undef HAVE_DECL_F_FULLFSYNC
#define HAVE_DECL_F_FULLFSYNC 0
#undef HAVE_DECL_PREADV
#define HAVE_DECL_PREADV 0
#undef HAVE_DECL_PWRITEV
#define HAVE_DECL_PWRITEV 0
#undef HAVE_DECL_STRLCAT
#define HAVE_DECL_STRLCAT 0
#undef HAVE_DECL_STRLCPY
#define HAVE_DECL_STRLCPY 0
#undef HAVE_GETIFADDRS
#undef HAVE_GETPEEREID
#undef HAVE_IFADDRS_H
#undef HAVE_INET_ATON
#undef HAVE_INT_OPTRESET
#undef HAVE_KQUEUE
#undef HAVE_LANGINFO_H
#undef HAVE_MEMSET_S
#undef HAVE_MKDTEMP
#undef HAVE_PTHREAD
#undef HAVE_PTHREAD_BARRIER_WAIT
#undef HAVE_PTHREAD_IS_THREADED_NP
#undef HAVE_PTHREAD_PRIO_INHERIT
#undef HAVE_STRERROR_R
#undef HAVE_STRLCAT
#undef HAVE_STRLCPY
#undef HAVE_STRSIGNAL
#undef HAVE_STRUCT_SOCKADDR_SA_LEN
#undef HAVE_STRUCT_TM_TM_ZONE
#undef HAVE_SYSLOG
#undef HAVE_SYS_EVENT_H
#undef HAVE_SYS_UCRED_H
#undef HAVE_TERMIOS_H
#undef HAVE_UNION_SEMUN
#undef HAVE_USELOCALE
#define HAVE__CONFIGTHREADLOCALE 1
#undef STRERROR_R_INT
#undef USE_SYSV_SEMAPHORES
#undef USE_SYSV_SHARED_MEMORY
#define USE_WIN32_SEMAPHORES 1
#define USE_WIN32_SHARED_MEMORY 1
#undef PG_PRINTF_ATTRIBUTE
#if defined(__MINGW32__) || defined(__MINGW64__)
#define PG_PRINTF_ATTRIBUTE gnu_printf
#undef HAVE_MBSTOWCS_L
#undef HAVE_WCSTOMBS_L
#define HAVE_CRTDEFS_H 1
#define HAVE_PTHREAD_BARRIER_WAIT 1
#endif
#endif

/* Microsoft Visual Studio Compiler */
#ifdef _MSC_VER
#undef HAVE_COMPUTED_GOTO
#undef HAVE_GCC__ATOMIC_INT32_CAS
#undef HAVE_GCC__ATOMIC_INT64_CAS
#undef HAVE_GCC__SYNC_CHAR_TAS
#undef HAVE_GCC__SYNC_INT32_CAS
#undef HAVE_GCC__SYNC_INT32_TAS
#undef HAVE_GCC__SYNC_INT64_CAS
#undef HAVE_GETOPT
#undef HAVE_GETOPT_H
#undef HAVE_GETOPT_LONG
#undef HAVE_INTTYPES_H
#undef HAVE_INT_OPTERR
#undef HAVE_LIBM
#undef HAVE_STRINGS_H
#undef HAVE_STRUCT_OPTION
#undef HAVE_TYPEOF
#undef HAVE_VISIBILITY_ATTRIBUTE
#undef HAVE__BOOL
#undef HAVE__BUILTIN_BSWAP16
#undef HAVE__BUILTIN_BSWAP32
#undef HAVE__BUILTIN_BSWAP64
#undef HAVE__BUILTIN_CLZ
#undef HAVE__BUILTIN_CONSTANT_P
#undef HAVE__BUILTIN_CTZ
#undef HAVE__BUILTIN_FRAME_ADDRESS
#undef HAVE__BUILTIN_OP_OVERFLOW
#undef HAVE__BUILTIN_POPCOUNT
#undef HAVE__BUILTIN_TYPES_COMPATIBLE_P
#undef HAVE__BUILTIN_UNREACHABLE
#ifndef __cplusplus
#define inline __inline
#endif
#undef restrict
#define __thread __declspec( thread )
#endif
33 changes: 0 additions & 33 deletions src/postgres/include/common/ip.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/postgres/include/nodes/nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ typedef struct Node
* Fortunately, this macro isn't recursive so we just define
* a global variable for this purpose.
*/
extern PGDLLIMPORT Node *newNodeMacroHolder;
extern PGDLLIMPORT __thread Node *newNodeMacroHolder;

#define newNode(size, tag) \
( \
Expand Down
Loading

0 comments on commit eff3257

Please sign in to comment.