Browse Source

Add script to check supported compiler options on Linux

Eg Alpine may not have the sanitizers available, so check for them
before trying to use them.
Davin McCall 3 years ago
parent
commit
ac3f93503a
3 changed files with 101 additions and 1 deletions
  1. 5 0
      configs/mconfig.Linux
  2. 91 0
      configs/mconfig.Linux.sh
  3. 5 1
      src/Makefile

+ 5 - 0
configs/mconfig.Linux

@@ -1,3 +1,8 @@
+# This file can be used as a basis for manual customisation, by copying it
+# to the correct place (../mconfig) before editing. If this is not done,
+# Linux configuration will normally be generated by the shell script
+# in this directory (mconfig.Linux.sh).
+
 # Installation path options.
 
 SBINDIR=/sbin

+ 91 - 0
configs/mconfig.Linux.sh

@@ -0,0 +1,91 @@
+#!/bin/sh
+
+rm -f ../mconfig
+
+INST_PATH_OPTS=$(
+  echo "# Installation path options.";
+  echo "";
+  echo "SBINDIR=/sbin";
+  echo "MANDIR=/usr/share/man";
+  echo "SYSCONTROLSOCKET=/dev/dinitctl"
+)
+
+test_compiler_arg() {
+  "$1" -c "$2" testfile.cc -o testfile.o > /dev/null 2>&1
+  if test $? = 0; then
+    supported_opts="$supported_opts $2"
+    supported_opts=${supported_opts# }
+    return 0
+  else
+    return 1
+  fi
+}
+
+for compiler in g++ clang++ c++ ""; do
+  if test -z "$compiler"; then
+    break # none found
+  fi
+  type $compiler > /dev/null
+  if test $? = 0; then
+    break # found
+  fi
+done
+
+if test -z "$compiler"; then
+  echo "*** No compiler found ***"
+  exit 1
+fi
+
+echo "Compiler found          : $compiler"
+
+touch testfile.cc
+supported_opts=""
+test_compiler_arg "$compiler" -flto
+HAS_LTO=$?
+test_compiler_arg "$compiler" -fno-rtti
+test_compiler_arg "$compiler" -fno-plt
+BUILD_OPTS="-D_GLIBCXX_USE_CXX11_ABI=1 -std=c++11 -Os -Wall $supported_opts"
+if test $HAS_LTO = 0; then
+  LD_OPTS="-flto -Os"
+else
+  LD_OPTS=""
+fi
+
+echo "Using build options     : $supported_opts"
+
+supported_opts=""
+test_compiler_arg "$compiler" -fsanitize=address,undefined
+SANITIZE_OPTS="$supported_opts"
+
+echo "Sanitize options        : $SANITIZE_OPTS"
+
+rm testfile.cc
+
+GENERAL_BUILD_SETTINGS=$(
+  echo ""
+  echo ""
+  echo "# General build options."
+  echo ""
+  echo "# Linux (GCC). Note with GCC 5.x/6.x you must use the old ABI, with GCC 7.x you must use"
+  echo "# the new ABI. See BUILD.txt file for more information."
+  echo "CXX=$compiler"
+  echo "CXXOPTS=$BUILD_OPTS"
+  echo "LDFLAGS=$LD_OPTS"
+  echo "BUILD_SHUTDOWN=yes"
+  echo "SANITIZEOPTS=$SANITIZE_OPTS"
+  echo ""
+  echo "# Notes:"
+  echo "#   -D_GLIBCXX_USE_CXX11_ABI=1 : force use of new ABI, see above / BUILD.txt"
+  echo "#   -fno-rtti (optional) : Dinit does not require C++ Run-time Type Information"
+  echo "#   -fno-plt  (optional) : Recommended optimisation"
+  echo "#   -flto     (optional) : Perform link-time optimisation"
+  echo "#   -fsanitize=address,undefined :  Apply sanitizers (during unit tests)"
+)
+
+#echo "$INST_PATH_OPTS"
+#echo "$GENERAL_BUILD_SETTINGS"
+
+(
+  echo "$INST_PATH_OPTS"
+  echo "$GENERAL_BUILD_SETTINGS" 
+) >> ../mconfig

+ 5 - 1
src/Makefile

@@ -19,11 +19,15 @@ all: dinit dinitctl dinitcheck $(SHUTDOWN)
 # Look for a suitable build config file and use it.
 ../mconfig:
 	@UNAME=`uname`;\
-	if [ -f "../configs/mconfig.$$UNAME" ]; then \
+	if [ -f "../configs/mconfig.$$UNAME.sh" ]; then \
+	    echo "Found auto-configuration script for OS: $$UNAME"; \
+	    ( cd ../configs; sh "mconfig.$$UNAME.sh" ) \
+	elif [ -f "../configs/mconfig.$$UNAME" ]; then \
 	    echo "Found configuration for OS: $$UNAME"; \
 	    ln -sf "configs/mconfig.$$UNAME" ../mconfig; \
 	else \
 	    echo "No config available. Please create suitable mconfig file."; \
+	    exit 1; \
 	fi
 
 includes/mconfig.h: mconfig-gen