Skip to content

Commit

Permalink
Add support for running on Windows
Browse files Browse the repository at this point in the history
In passing this updates the structure of the include files, as done in the
libpg_query source, and fixes a 32-bit compatibility issue.
  • Loading branch information
lfittl committed Jan 1, 2024
1 parent b0bb868 commit ce3b0ba
Show file tree
Hide file tree
Showing 441 changed files with 1,485 additions and 11,360 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
name: CI
on:
push:
branches: [ main ]
pull_request:
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2']
runs-on: ubuntu-latest
ruby: ['2.6', '2.7', '3.0', '3.1', '3.2'] # 3.3+ is not yet supported by google-protobuf
os: ['ubuntu-latest', 'windows-latest']
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
Expand Down
12 changes: 6 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'open-uri'

LIB_PG_QUERY_TAG = '16-5.0.0'.freeze
#LIB_PG_QUERY_TAG = '16-5.0.0'.freeze
LIB_PG_QUERY_TAG = 'windows-support'.freeze
LIB_PG_QUERY_SHA256SUM = '7cf26922cbc41668fa79332504f2d5638339baa9411912b8df4526ccd1d7029e'.freeze

Rake::ExtensionTask.new 'pg_query' do |ext|
Expand Down Expand Up @@ -43,9 +44,9 @@ task :update_source do

checksum = Digest::SHA256.hexdigest(File.read(filename))

if checksum != LIB_PG_QUERY_SHA256SUM
raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{LIB_PG_QUERY_SHA256SUM}"
end
#if checksum != LIB_PG_QUERY_SHA256SUM
# raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{LIB_PG_QUERY_SHA256SUM}"
#end
end

unless Dir.exist?(libdir)
Expand All @@ -62,11 +63,10 @@ task :update_source do
# Reduce everything down to one directory
system("mkdir -p #{extdir}")
system("cp -a #{libdir}/src/* #{extdir}/")
system("mv #{extdir}/postgres/include #{extdir}/include/postgres")
system("mv #{extdir}/postgres/* #{extdir}/")
system("rmdir #{extdir}/postgres")
system("cp -a #{libdir}/pg_query.h #{extdir}/include")
# Make sure every .c file in the top-level directory is its own translation unit
system("mv #{extdir}/*{_conds,_defs,_helper,.funcs}.c #{extdir}/include")
# Protobuf definitions
system("protoc --proto_path=#{libdir}/protobuf --ruby_out=#{File.join(__dir__, 'lib/pg_query')} #{libdir}/protobuf/pg_query.proto")
system("mkdir -p #{extdir}/include/protobuf")
Expand Down
18 changes: 14 additions & 4 deletions ext/pg_query/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@

$objs = Dir.glob(File.join(__dir__, '*.c')).map { |f| Pathname.new(f).sub_ext('.o').to_s }

$CFLAGS << " -fvisibility=hidden -O3 -Wall -fno-strict-aliasing -fwrapv -fstack-protector -Wno-unused-function -Wno-unused-variable -Wno-clobbered -Wno-sign-compare -Wno-discarded-qualifiers -Wno-unknown-warning-option -g"
if RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
$CFLAGS << " -fvisibility=hidden -O3 -Wall -fno-strict-aliasing -fwrapv -fstack-protector -Wno-unused-function -Wno-unused-variable -Wno-clobbered -Wno-sign-compare -Wno-discarded-qualifiers -Wno-unknown-warning-option -g"
end

$INCFLAGS = "-I#{File.join(__dir__, 'include')} " + "-I#{File.join(__dir__, 'include', 'postgres')} " + $INCFLAGS

$INCFLAGS = "-I#{File.join(__dir__, 'include')} " + $INCFLAGS
if RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/
$INCFLAGS = "-I#{File.join(__dir__, 'include', 'postgres', 'port', 'win32')} " + $INCFLAGS
end

if RUBY_PLATFORM =~ /mswin/
$INCFLAGS = "-I#{File.join(__dir__, 'include', 'postgres', 'port', 'win32_msvc')} " + $INCFLAGS
end

SYMFILE =
if RUBY_PLATFORM =~ /freebsd/
File.join(__dir__, 'pg_query_ruby_freebsd.sym')
else
elsif RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
File.join(__dir__, 'pg_query_ruby.sym')
end

if RUBY_PLATFORM =~ /darwin/
$DLDFLAGS << " -Wl,-exported_symbols_list #{SYMFILE}" unless defined?(::Rubinius)
else
elsif RUBY_PLATFORM !~ /cygwin|mswin|mingw|bccwin|wince|emx/
$DLDFLAGS << " -Wl,--retain-symbols-file=#{SYMFILE}"
end

Expand Down
Empty file removed ext/pg_query/guc-file.c
Empty file.
33 changes: 0 additions & 33 deletions ext/pg_query/include/common/ip.h

This file was deleted.

8 changes: 0 additions & 8 deletions ext/pg_query/include/pg_config_os.h

This file was deleted.

2 changes: 1 addition & 1 deletion ext/pg_query/include/pg_query_fingerprint_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5894,7 +5894,7 @@ _fingerprintRTEPermissionInfo(FingerprintContext *ctx, const RTEPermissionInfo *

if (node->requiredPerms != 0) {
char buffer[50];
sprintf(buffer, "%ld", node->requiredPerms);
sprintf(buffer, UINT64_FORMAT, node->requiredPerms);
_fingerprintString(ctx, "requiredPerms");
_fingerprintString(ctx, buffer);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/pg_query/include/pg_query_outfuncs_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ _outRTEPermissionInfo(OUT_TYPE(RTEPermissionInfo, RTEPermissionInfo) out, const
{
WRITE_UINT_FIELD(relid, relid, relid);
WRITE_BOOL_FIELD(inh, inh, inh);
WRITE_LONG_FIELD(required_perms, requiredPerms, requiredPerms);
WRITE_UINT64_FIELD(required_perms, requiredPerms, requiredPerms);
WRITE_UINT_FIELD(check_as_user, checkAsUser, checkAsUser);
WRITE_BITMAPSET_FIELD(selected_cols, selectedCols, selectedCols);
WRITE_BITMAPSET_FIELD(inserted_cols, insertedCols, insertedCols);
Expand Down
2 changes: 1 addition & 1 deletion ext/pg_query/include/pg_query_readfuncs_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ _readRTEPermissionInfo(OUT_TYPE(RTEPermissionInfo, RTEPermissionInfo) msg)
RTEPermissionInfo *node = makeNode(RTEPermissionInfo);
READ_UINT_FIELD(relid, relid, relid);
READ_BOOL_FIELD(inh, inh, inh);
READ_LONG_FIELD(required_perms, requiredPerms, requiredPerms);
READ_UINT64_FIELD(required_perms, requiredPerms, requiredPerms);
READ_UINT_FIELD(check_as_user, checkAsUser, checkAsUser);
READ_BITMAPSET_FIELD(selected_cols, selectedCols, selectedCols);
READ_BITMAPSET_FIELD(inserted_cols, insertedCols, insertedCols);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extern PGDLLIMPORT bool IsUnderPostmaster;
extern PGDLLIMPORT bool IsBackgroundWorker;
extern PGDLLIMPORT bool IsBinaryUpgrade;

extern PGDLLIMPORT __thread bool ExitOnAnyError;
extern PGDLLIMPORT __thread bool ExitOnAnyError;

extern PGDLLIMPORT char *DataDir;
extern PGDLLIMPORT int data_directory_mode;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ typedef enum
} BackslashQuoteType;

/* GUC variables in scan.l (every one of these is a bad idea :-() */
extern PGDLLIMPORT __thread int backslash_quote;
extern PGDLLIMPORT __thread bool escape_string_warning;
extern PGDLLIMPORT __thread bool standard_conforming_strings;
extern PGDLLIMPORT __thread int backslash_quote;
extern PGDLLIMPORT __thread bool escape_string_warning;
extern PGDLLIMPORT __thread bool standard_conforming_strings;


/* Primary entry point for the raw parsing functions */
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@
#define PG_VERSION_NUM 160001

/* A string containing the version number, platform, and C compiler */
#define PG_VERSION_STR "PostgreSQL 16.1 on aarch64-apple-darwin21.6.0, compiled by Apple clang version 14.0.0 (clang-1400.0.29.102), 64-bit"
#define PG_VERSION_STR "PostgreSQL 16.1 (libpg_query)"

/* Define to 1 to allow profiling output to be saved separately for each
process. */
Expand Down Expand Up @@ -826,18 +826,152 @@

/* Define to how the compiler spells `typeof'. */
/* #undef typeof */
/* 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
#undef PG_INT128_TYPE

/* 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(__clang__)
#define PG_PRINTF_ATTRIBUTE printf
#elif defined(__MINGW32__) || defined(__MINGW64__) || defined(__MSYS__) || defined(__CYGWIN__)
#define PG_PRINTF_ATTRIBUTE gnu_printf
#endif
#if defined(__MINGW32__) || defined(__MINGW64__) || defined(__MSYS__) || defined(__CYGWIN__)
#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
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions ext/pg_query/include/postgres/pg_config_os.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if defined(_WIN32) || defined(_WIN64)
#include "port/win32.h"
#undef PGDLLIMPORT
#undef PGDLLEXPORT
#ifdef __clang__
#undef __MINGW64__
#endif /* __clang__ */
#endif
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ extern PGDLLIMPORT PgStat_Counter pgStatActiveTime;
extern PGDLLIMPORT PgStat_Counter pgStatTransactionIdleTime;

/* updated by the traffic cop and in errfinish() */
extern PGDLLIMPORT __thread SessionEndType pgStatSessionEndCause;
extern PGDLLIMPORT __thread SessionEndType pgStatSessionEndCause;


/*
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ typedef union YYSTYPE
# define YYSTYPE_IS_TRIVIAL 1
#endif

extern __thread YYSTYPE plpgsql_yylval;
extern __thread YYSTYPE plpgsql_yylval;

#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED
typedef struct YYLTYPE
Expand All @@ -382,4 +382,4 @@ typedef struct YYLTYPE
# define YYLTYPE_IS_TRIVIAL 1
#endif

extern __thread YYLTYPE plpgsql_yylloc;
extern __thread YYLTYPE plpgsql_yylloc;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit ce3b0ba

Please sign in to comment.