#!/usr/bin/env bash
+#
+# Copyright 2019 Michael D. Lowis
+#-------------------------------------------------------------------------------
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# Wrapper script for system compiler that enables some flags by default and scans
+# for library dependencies.
+#
+# Compilation Features:
+# * Enables --std=c99 -pedantic
+# * Enables -Wall -Wextra -Werror
+# * Defines AUTOLIB() macro to specify dependencies in sources and headers
+# * Auto-includes commonly include headers such as stddef.h, stdio.h, etc.
+#
+# Linking Features:
+# * Scans objects for AUTOLIB()-ed libs, and adds the -l flags for them
# predeclare our variables
declare -a objects
declare -a libpaths
-declare -a libraries
compile=false
runtime='
#define _POSIX_C_SOURCE 200809L
#define _XOPEN_SOURCE 700
#define AUTOLIB(n) \
- int __autolib_##n __attribute__ ((weak));'
+ int __autolib_##n __attribute__ ((weak));
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <fcntl.h>
+' # end of runtime definition
# scan the rest of the options for lib paths, libs and objects
for i do
-L*) # Add libpaths to the search list
libpaths+=("${i#-L}") ;;
- -c) # Mark this as compilation only
+ -c|-E) # Mark this as compilation/preprocess only
compile=true ;;
esac
done
if $compile; then
genfile=$(mktemp)
printf '%s\n' "$runtime" > "$genfile"
- cc -include "$genfile" "$@"
+ cc -isystem "${genfile%/*}" -include "${genfile##*/}" --std=c99 -pedantic -Wall -Wextra -Werror "$@"
status=$?
rm "$genfile"
exit "$status"
# scan objects/libs for referenced libraries
scan_for_libs(){
- nm "$@" | sed -n '/__autolib/ s/.*_\+autolib_// p' | sort -u
+ [[ $# -ne 0 ]] && nm "$@" | sed -n '/__autolib/ s/.*_\+autolib_// p' | sort -u
}
# if we made it here, we must be linking. scan for dependencies