Skip to content

Commit

Permalink
Ensure we don't rely on x86 and ARM intrinsics (i.e. SSE 4.2, etc)
Browse files Browse the repository at this point in the history
Add three missing defines to always be unset to avoid this dependency:
- HAVE_X86_64_POPCNTQ
- USE_ARMV8_CRC32C
- USE_SSE42_CRC32C_WITH_RUNTIME_CHECK

Additionally, to help with generating the correct code on either ARM or
x86, make sure we unset these defines before we run the extract source
script. This now produces the identical output on x86 and ARM, besides
some minor pg_config differences before the undefs at the end that undo
those differences.

In passing, drop the "-msse4.2" flag passed in during source extraction,
as we do not want to target SSE 4.2.
  • Loading branch information
lfittl committed Dec 13, 2022
1 parent 397cbb9 commit 9b4a3bf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
45 changes: 25 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ $(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)
echo "#ifdef __FreeBSD__" >> $(PGDIR)/src/include/pg_config.h
echo "#define HAVE_STRCHRNUL" >> $(PGDIR)/src/include/pg_config.h
echo "#endif" >> $(PGDIR)/src/include/pg_config.h

extract_source: $(PGDIR)
-@ $(RM) -rf ./src/postgres/
Expand All @@ -136,26 +161,6 @@ extract_source: $(PGDIR)
cp $(PGDIR)/src/include/port/atomics/arch-arm.h ./src/postgres/include/port/atomics
cp $(PGDIR)/src/include/port/atomics/arch-ppc.h ./src/postgres/include/port/atomics
touch ./src/postgres/guc-file.c
# This causes compatibility problems on some Linux distros, with "xlocale.h" not being available
echo "#undef HAVE_LOCALE_T" >> ./src/postgres/include/pg_config.h
echo "#undef LOCALE_T_IN_XLOCALE" >> ./src/postgres/include/pg_config.h
echo "#undef WCSTOMBS_L_IN_XLOCALE" >> ./src/postgres/include/pg_config.h
# Support 32-bit systems without reconfiguring
echo "#undef PG_INT128_TYPE" >> ./src/postgres/include/pg_config.h
# Support gcc earlier than 4.6.0 without reconfiguring
echo "#undef HAVE__STATIC_ASSERT" >> ./src/postgres/include/pg_config.h
# Avoid problems with static asserts
echo "#undef StaticAssertDecl" >> ./src/postgres/include/c.h
echo "#define StaticAssertDecl(condition, errmessage)" >> ./src/postgres/include/c.h
# Avoid dependency on execinfo (requires extra library on musl-libc based systems)
echo "#undef HAVE_EXECINFO_H" >> ./src/postgres/include/pg_config.h
echo "#undef HAVE_BACKTRACE_SYMBOLS" >> ./src/postgres/include/pg_config.h
# Avoid dependency on cpuid.h (only supported on x86 systems)
echo "#undef HAVE__GET_CPUID" >> ./src/postgres/include/pg_config.h
# Ensure we don't fail on systems that have strchrnul support (FreeBSD)
echo "#ifdef __FreeBSD__" >> ./src/postgres/include/pg_config.h
echo "#define HAVE_STRCHRNUL" >> ./src/postgres/include/pg_config.h
echo "#endif" >> ./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
Expand Down
1 change: 0 additions & 1 deletion scripts/extract_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ def analyze_file(file)
'-I', '/usr/local/opt/openssl/include',
'-I', `xcrun --sdk macosx --show-sdk-path`.strip + '/usr/include',
'-DDLSUFFIX=".bundle"',
'-msse4.2',
'-g',
'-DUSE_ASSERT_CHECKING'
])
Expand Down
3 changes: 3 additions & 0 deletions src/postgres/include/pg_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,10 @@
#undef HAVE__STATIC_ASSERT
#undef HAVE_EXECINFO_H
#undef HAVE_BACKTRACE_SYMBOLS
#undef HAVE_X86_64_POPCNTQ
#undef HAVE__GET_CPUID
#undef USE_ARMV8_CRC32C
#undef USE_SSE42_CRC32C_WITH_RUNTIME_CHECK
#ifdef __FreeBSD__
#define HAVE_STRCHRNUL
#endif

0 comments on commit 9b4a3bf

Please sign in to comment.