[apparmor] [PATCH 4/9] Add testing for C++11 unordered sets and maps to the makefile

John Johansen john.johansen at canonical.com
Tue Sep 15 03:11:16 UTC 2015


C++11 adds support for unordered sets and maps to the stl containers
but we can not rely on all platforms providing this support. Have
the make file test for it and define a MACRO so that these features
can be used if detected.

Signed-off-by: John Johansen <john.johansen at canonical.com>
---
 parser/Makefile              | 17 +++++++++++++++++
 parser/parser.h              |  8 +++++++-
 parser/test_unordered_map.cc | 16 ++++++++++++++++
 parser/test_unordered_set.cc | 16 ++++++++++++++++
 4 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 parser/test_unordered_map.cc
 create mode 100644 parser/test_unordered_set.cc

diff --git a/parser/Makefile b/parser/Makefile
index ab9f9bd..b748d99 100644
--- a/parser/Makefile
+++ b/parser/Makefile
@@ -38,6 +38,17 @@ CXX_WARNINGS = ${WARNINGS} $(shell for warning in ${EXTRA_WARNINGS} ; do \
 				echo "$${warning}"; \
 			fi ; \
 		done)
+
+# check for support of c++11 unorder sets and maps
+# we don't just use __cplusplus == 201103L or __cpluscplus > 199711L as
+# compilers can and do lie
+CXX11_UNORDERED_MAP = $(shell if ${CXX} -o /dev/null -std=c++0x test_unordered_map.cc 2>&1 ; then \
+				echo "CXX11_UNORDERED_MAP"; \
+			fi)
+CXX11_UNORDERED_SET = $(shell if ${CXX} -o /dev/null -std=c++0x test_unordered_set.cc 2>&1 ; then \
+				echo "CXX11_UNORDERED_SET"; \
+			fi)
+
 CPP_WARNINGS =
 ifndef CXXFLAGS
 CXXFLAGS	= -g -O2 -pipe
@@ -52,6 +63,12 @@ endif #CXXFLAGS
 
 EXTRA_CXXFLAGS += $(CXXFLAGS) ${CPPFLAGS} ${CXX_WARNINGS} -std=gnu++0x -D_GNU_SOURCE
 
+ifdef CXX11_UNORDERED_MAP
+ifdef CXX11_UNORDERED_SET
+EXTRA_CXXFLAGS += -std=c++0x -D CXX11_UNORDERED_CONTAINERS
+endif
+endif
+
 #LEXLIB	:= -lfl
 
 # override this on the make command to point to where the immunix.h file is
diff --git a/parser/parser.h b/parser/parser.h
index 58bd00a..340a565 100644
--- a/parser/parser.h
+++ b/parser/parser.h
@@ -37,10 +37,16 @@
 #include "libapparmor_re/aare_rules.h"
 
 #include <string>
+#include <set>
+
+// check for C++11
+#if __cplusplus > 199711L
+// If we are using C++11 gcc does not define typeof()
+#define typeof(X) __typeof__(X)
+#endif
 
 using namespace std;
 
-#include <set>
 class Profile;
 class rule_t;
 
diff --git a/parser/test_unordered_map.cc b/parser/test_unordered_map.cc
new file mode 100644
index 0000000..b4979fd
--- /dev/null
+++ b/parser/test_unordered_map.cc
@@ -0,0 +1,16 @@
+/*
+ *
+ * Simple test to see if unordered_maps are available
+ * Build with
+ *   g++ -std=c++11 -o test_unordered_map test_unordered_map.cc
+ */
+#include <iostream>
+#include <string>
+#include <unordered_map>
+
+int main()
+{
+	std::unordered_map<std::string,std::string> first;
+
+	return 0;
+}
diff --git a/parser/test_unordered_set.cc b/parser/test_unordered_set.cc
new file mode 100644
index 0000000..2330472
--- /dev/null
+++ b/parser/test_unordered_set.cc
@@ -0,0 +1,16 @@
+/*
+ *
+ * Simple test to see if unordered_sets are available
+ * Build with
+ *   g++ -std=c++11 -o test_unordered_set test_unordered_set.cc
+ */
+#include <iostream>
+#include <string>
+#include <unordered_set>
+
+int main()
+{
+	std::unordered_set<std::string> first;
+
+	return 0;
+}
-- 
2.1.4




More information about the AppArmor mailing list