From 4f0b3ea9771e49fb0d5e5c323e7966ceff2c7ec2 Mon Sep 17 00:00:00 2001
From: ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069>
Date: Mon, 25 May 2020 16:03:24 +0000
Subject: [PATCH 1/2] Apply H.J. Lu's patch to pass -mshstk to the compiler
 when Intel CET is enabled. CMake version invented by PH, but only tested on
 non-CET system.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

git-svn-id: svn://vcs.exim.org/pcre2/code/trunk@1256 6239d852-aaf2-0410-a92c-79f79f948069
Petr Písař: Ported to 10.35.
---
 CMakeLists.txt | 19 +++++++++++++++++++
 Makefile.am    |  1 +
 configure.ac   | 15 +++++++++++++++

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 86b8896..5e8a763 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -92,6 +92,7 @@
 #            library versioning.
 # 2020-04-25 Carlo added function check for mkostemp used in ProtExecAllocator
 # 2020-04-28 PH added function check for memfd_create based on Carlo's patch
+# 2020-05-25 PH added a check for Intel CET
 
 PROJECT(PCRE2 C)
 
@@ -146,6 +147,24 @@ CHECK_C_SOURCE_COMPILES(
 )
 set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
 
+# Check whether Intel CET is enabled, and if so, adjust compiler flags. This
+# code was written by PH, trying to imitate the logic from the autotools 
+# configuration.
+
+CHECK_C_SOURCE_COMPILES(
+  "#ifndef __CET__
+   #error CET is not enabled
+   #endif
+   int main() { return 0; }"
+  INTEL_CET_ENABLED
+)
+
+IF (INTEL_CET_ENABLED)
+  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mshstk")
+ENDIF(INTEL_CET_ENABLED)   
+        
+
+
 # User-configurable options
 #
 # Note: CMakeSetup displays these in alphabetical order, regardless of
diff --git a/Makefile.am b/Makefile.am
index bb888f2..af6b92b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -391,6 +391,7 @@ nodist_libpcre2_8_la_SOURCES = \
 libpcre2_8_la_CFLAGS = \
   -DPCRE2_CODE_UNIT_WIDTH=8 \
   $(VISIBILITY_CFLAGS) \
+  $(CET_CFLAGS) \
   $(AM_CFLAGS)
 libpcre2_8_la_LIBADD =
 endif # WITH_PCRE2_8
diff --git a/configure.ac b/configure.ac
index 180d3dc..61b93ba 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1006,6 +1006,21 @@ fi # enable_coverage
 
 AM_CONDITIONAL([WITH_GCOV],[test "x$enable_coverage" = "xyes"])
 
+AC_MSG_CHECKING([whether Intel CET is enabled])
+AC_LANG_PUSH([C])
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,
+                   [[#ifndef __CET__
+# error CET is not enabled
+#endif]])],
+                   [pcre2_cc_cv_intel_cet_enabled=yes],
+                   [pcre2_cc_cv_intel_cet_enabled=no])
+AC_MSG_RESULT([$pcre2_cc_cv_intel_cet_enabled])
+if test "$pcre2_cc_cv_intel_cet_enabled" = yes; then
+  CET_CFLAGS="-mshstk"
+  AC_SUBST([CET_CFLAGS])
+fi
+AC_LANG_POP([C])
+
 # Produce these files, in addition to config.h.
 AC_CONFIG_FILES(
 	Makefile
-- 
2.25.4

