f54ff1d
From 70fcff23652339438cabef86f2d3d9c645678687 Mon Sep 17 00:00:00 2001
f54ff1d
From: "H.J. Lu" <hjl.tools@gmail.com>
f54ff1d
Date: Sun, 20 Dec 2020 13:55:40 -0800
f54ff1d
Subject: [PATCH 2/2] Pass -mshstk to the compiler when Intel CET is enable
f54ff1d
f54ff1d
Copied from PCRE2.
f54ff1d
---
f54ff1d
 CMakeLists.txt | 18 ++++++++++++++++++
f54ff1d
 Makefile.am    |  3 +++
f54ff1d
 configure.ac   | 15 +++++++++++++++
f54ff1d
 3 files changed, 36 insertions(+)
f54ff1d
f54ff1d
diff --git a/CMakeLists.txt b/CMakeLists.txt
f54ff1d
index 858a34b..74c27e9 100644
f54ff1d
--- a/CMakeLists.txt
f54ff1d
+++ b/CMakeLists.txt
f54ff1d
@@ -112,6 +112,24 @@ CHECK_FUNCTION_EXISTS(_strtoi64 HAVE__STRTOI64)
f54ff1d
 CHECK_TYPE_SIZE("long long"             LONG_LONG)
f54ff1d
 CHECK_TYPE_SIZE("unsigned long long"    UNSIGNED_LONG_LONG)
f54ff1d
 
f54ff1d
+# Check whether Intel CET is enabled, and if so, adjust compiler flags. This
f54ff1d
+# code was written by PH, trying to imitate the logic from the autotools
f54ff1d
+# configuration.
f54ff1d
+
f54ff1d
+CHECK_C_SOURCE_COMPILES(
f54ff1d
+  "#ifndef __CET__
f54ff1d
+   #error CET is not enabled
f54ff1d
+   #endif
f54ff1d
+   int main() { return 0; }"
f54ff1d
+  INTEL_CET_ENABLED
f54ff1d
+)
f54ff1d
+
f54ff1d
+IF (INTEL_CET_ENABLED)
f54ff1d
+  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mshstk")
f54ff1d
+ENDIF(INTEL_CET_ENABLED)
f54ff1d
+
f54ff1d
+
f54ff1d
+
f54ff1d
 # User-configurable options
f54ff1d
 #
f54ff1d
 # (Note: CMakeSetup displays these in alphabetical order, regardless of
f54ff1d
diff --git a/Makefile.am b/Makefile.am
f54ff1d
index 22b6947..984c686 100644
f54ff1d
--- a/Makefile.am
f54ff1d
+++ b/Makefile.am
f54ff1d
@@ -250,6 +250,7 @@ libpcre_la_SOURCES = \
f54ff1d
 
f54ff1d
 libpcre_la_CFLAGS = \
f54ff1d
   $(VISIBILITY_CFLAGS) \
f54ff1d
+  $(CET_CFLAGS) \
f54ff1d
   $(AM_CFLAGS)
f54ff1d
 
f54ff1d
 libpcre_la_LIBADD =
f54ff1d
@@ -289,6 +290,7 @@ libpcre16_la_SOURCES = \
f54ff1d
 
f54ff1d
 libpcre16_la_CFLAGS = \
f54ff1d
   $(VISIBILITY_CFLAGS) \
f54ff1d
+  $(CET_CFLAGS) \
f54ff1d
   $(AM_CFLAGS)
f54ff1d
 
f54ff1d
 libpcre16_la_LIBADD =
f54ff1d
@@ -328,6 +330,7 @@ libpcre32_la_SOURCES = \
f54ff1d
 
f54ff1d
 libpcre32_la_CFLAGS = \
f54ff1d
   $(VISIBILITY_CFLAGS) \
f54ff1d
+  $(CET_CFLAGS) \
f54ff1d
   $(AM_CFLAGS)
f54ff1d
 
f54ff1d
 libpcre32_la_LIBADD =
f54ff1d
diff --git a/configure.ac b/configure.ac
f54ff1d
index 6a3e5f3..15f37d1 100644
f54ff1d
--- a/configure.ac
f54ff1d
+++ b/configure.ac
f54ff1d
@@ -1043,6 +1043,21 @@ fi # enable_coverage
f54ff1d
 
f54ff1d
 AM_CONDITIONAL([WITH_GCOV],[test "x$enable_coverage" = "xyes"])
f54ff1d
 
f54ff1d
+AC_MSG_CHECKING([whether Intel CET is enabled])
f54ff1d
+AC_LANG_PUSH([C])
f54ff1d
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,
f54ff1d
+                   [[#ifndef __CET__
f54ff1d
+# error CET is not enabled
f54ff1d
+#endif]])],
f54ff1d
+                   [intel_cet_enabled=yes],
f54ff1d
+                   [intel_cet_enabled=no])
f54ff1d
+AC_MSG_RESULT([$intel_cet_enabled])
f54ff1d
+if test "$intel_cet_enabled" = yes; then
f54ff1d
+  CET_CFLAGS="-mshstk"
f54ff1d
+  AC_SUBST([CET_CFLAGS])
f54ff1d
+fi
f54ff1d
+AC_LANG_POP([C])
f54ff1d
+
f54ff1d
 # Produce these files, in addition to config.h.
f54ff1d
 AC_CONFIG_FILES(
f54ff1d
 	Makefile
f54ff1d
-- 
f54ff1d
2.29.2
f54ff1d