]> git.mdlowis.com Git - proto/acc.git/commitdiff
added compiler invocation and lib scanning
authorMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Feb 2019 03:25:25 +0000 (22:25 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Fri, 1 Feb 2019 03:25:25 +0000 (22:25 -0500)
acc

diff --git a/acc b/acc
index c541c898f575a999b214bc6e6d3afee190123620..acdf06b3de0d7a1b7e8613cae3ab40aa307c462f 100755 (executable)
--- a/acc
+++ b/acc
@@ -1,10 +1,15 @@
-#!/bin/bash
+#!/usr/bin/env bash
 
 # 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));'
 
 # scan the rest of the options for lib paths, libs and objects
 for i do
@@ -13,25 +18,28 @@ for i do
             objects+=("$i") ;;
 
         -L*) # Add libpaths to the search list
-            libpaths+=("$i") ;;
+            libpaths+=("${i#-L}") ;;
 
         -c) # Mark this as compilation only
             compile=true ;;
     esac
 done
 
-# generate the autolib include file
-gendir="/tmp/$USER"
-genfile="autolib.h"
-genmacro="#define AUTOLIB(n) static int __autolib_##n = 1"
-if [[ ! -f "$gendir/$genfile" ]]; then
-    mkdir -p "$gendir"
-    printf '%s' "$genmacro" > "$gendir/$genfile"
-fi
-
-# execute the compiler
+# if we're compiling,  generate the header, compile, and exit
 if $compile; then
-    cc -include "$gendir/$genfile" "$@"
-else
-    echo cc "$@"
+    genfile=$(mktemp)
+    printf '%s\n' "$runtime" > "$genfile"
+    cc -include "$genfile" "$@"
+    status=$?
+    rm "$genfile"
+    exit "$status"
 fi
+
+# scan objects/libs for referenced libraries
+scan_for_libs(){
+    nm "$@" | sed -n '/__autolib/ s/.*_\+autolib_// p' | sort -u
+}
+
+# if we made it here, we must be linking. scan for dependencies
+mapfile -t libraries < <(scan_for_libs "${objects[@]}")
+cc "$@" "${libraries[@]/#/-l}"