about summary refs log tree commit diff
path: root/make/lib/compile-c.sh
blob: 04d2595def24806ab680d60ac2dd3023721d0dec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
. $stdenv/setup

mainName=$(basename $main | cut -c34-)

echo "compiling \`$mainName'..."

# Turn $localIncludes into an array.
localIncludes=($localIncludes)

# Determine how many `..' levels appear in the header file references.
# E.g., if there is some reference `../../foo.h', then we have to
# insert two extra levels in the directory structure, so that `a.c' is
# stored at `dotdot/dotdot/a.c', and a reference from it to
# `../../foo.h' resolves to `dotdot/dotdot/../../foo.h' == `foo.h'.
n=0
maxDepth=0
for ((n = 0; n < ${#localIncludes[*]}; n += 2)); do
    target=${localIncludes[$((n + 1))]}

    # Split the target name into path components using some IFS magic.
    savedIFS="$IFS"
    IFS=/
    components=($target)
    depth=0
    for ((m = 0; m < ${#components[*]}; m++)); do
        c=${components[m]}
        if test "$c" = ".."; then
            depth=$((depth + 1))
        fi
    done
    IFS="$savedIFS"

    if test $depth -gt $maxDepth; then
        maxDepth=$depth;
    fi
done

# Create the extra levels in the directory hierarchy.
prefix=
for ((n = 0; n < maxDepth; n++)); do
    prefix="dotdot/$prefix"
done

# Create symlinks to the header files.
for ((n = 0; n < ${#localIncludes[*]}; n += 2)); do
    source=${localIncludes[n]}
    target=${localIncludes[$((n + 1))]}

    # Create missing directories.  We use IFS magic to split the path
    # into path components.
    savedIFS="$IFS"
    IFS=/
    components=($prefix$target)
    fullPath=(.)
    for ((m = 0; m < ${#components[*]} - 1; m++)); do
        fullPath=("${fullPath[@]}" ${components[m]})
        if ! test -d "${fullPath[*]}"; then
            mkdir "${fullPath[*]}"
        fi
    done
    IFS="$savedIFS"
    
    ln -sf $source $prefix$target
done

# Create a symlink to the main file.
if ! test "$(readlink $prefix$mainName)" = $main; then
    ln -s $main $prefix$mainName
fi

mkdir $out
test "$prefix" && cd $prefix
ls -l
gcc -Wall $cFlags -c $mainName -o $out/$mainName.o