From 82ed0c32aacb7d1a9d4799e633c836fb86fdcfd6 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 31 Jan 2019 22:25:25 -0500 Subject: [PATCH] added compiler invocation and lib scanning --- acc | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/acc b/acc index c541c89..acdf06b 100755 --- 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}" -- 2.52.0