#!/usr/bin/bash

# this script creates two directories, obj and lib.
BUILTINS_DIR=../compiler-rt/lib/builtins/

[ -d obj/armv6m ] && rm obj/armv6m/*.o
[ ! -d obj/armv6m ] && mkdir -p obj/armv6m

CFLAGS=-target\ armv6m-none-eabi\ -mthumb\ -mfloat-abi=soft\ -mfpu=none\ -std=gnu99\ -fPIC\ -Os\ -ffunction-sections\ -fdata-sections\ -fno-builtin\ -fvisibility=hidden\ -fomit-frame-pointer\ -ffreestanding\ -Wno-unknown-attributes\ -Wno-macro-redefined\ -Iinclude

for a in $BUILTINS_DIR/{gcc_personality_v0.c,cpu_model.c,int_util.c,arm/aeabi_*.S,*f.c,*i.c,*2.c,*3.c,*4.c}
do
    a_sub_pre_t="${a//$BUILTINS_DIR/obj\/armv6m\/}"
    a_sub_pre="${a_sub_pre_t//arm\/}"
    a_sub_suf_t="${a_sub_pre//\.c/\.o}"
    a_sub_suf="${a_sub_suf_t//\.S/\.o}"
    echo "clang $CFLAGS -c "$a" -o $a_sub_suf"
    clang $CFLAGS -c "$a" -o "$a_sub_suf"
done

echo "making directory lib/armv6-m and linking library there..."

mkdir -p lib/armv6-m

arm-none-eabi-ar -rc lib/armv6-m/libclang_rt.builtins-arm.a obj/armv6m/*.o
