diff --git a/mbed-os-to-arduino b/mbed-os-to-arduino index f6654345..897addc5 100755 --- a/mbed-os-to-arduino +++ b/mbed-os-to-arduino @@ -2,17 +2,49 @@ set -ef +platform=${OSTYPE//[0-9.-]*/} + +# Default case for Linux sed +sed_exec="sed" +case "$platform" in + # For macOS, GNU sed + darwin) + sed_exec="gsed" + ;; + *) + + echo "Unknown Operating system $OSTYPE" + exit 1 +esac + +exists() { type -t "$1" > /dev/null 2>&1; } + check_tools () { - echo -n "Checking for prerequisites..." - if not hash jq &>/dev/null ; then + echo "Checking for prerequisites on $platform..." + if ! exists jq; + then echo "Please, install jq." exit 1 fi - if not hash rsync &>/dev/null ; then + if ! exists rsync; + then echo "Please, install rsync." exit 1 fi + + if ! exists tac; + then + echo "Please, install tac." + exit 1 + fi + + if ! exists $sed_exec; + then + echo "Please, install $sed_exec." + exit 1 + fi + echo " done." } @@ -126,7 +158,7 @@ mbed_compile () { generate_defines () { echo -n "Generating defines..." - cut -f2 -d":" < "$BOARDNAME".macros.txt | tr ' ' '\n' | sed 's#\"#\\"#g' | sort > "$ARDUINOVARIANT"/defines.txt + cut -f2 -d":" < "$BOARDNAME".macros.txt | tr ' ' '\n' | $sed_exec 's#\"#\\"#g' | sort > "$ARDUINOVARIANT"/defines.txt echo "-DMBED_NO_GLOBAL_USING_DIRECTIVE=1" >> "$ARDUINOVARIANT"/defines.txt if [ -f "$ARDUINOVARIANT"/variant.cpp ]; then echo '-DUSE_ARDUINO_PINOUT' >> "$ARDUINOVARIANT"/defines.txt @@ -138,14 +170,14 @@ generate_includes () { echo -n "Generating includes..." find ./BUILD/"$BOARDNAME"/GCC_ARM/ -type f -name '.include*' -print0 | xargs -0 cat \ - | tr ' ' '\n' | tr -d '"' | sed -e 's#-I./mbed-os#-iwithprefixbefore/mbed#g' \ - | sed '/^-I./d' | tac \ + | tr ' ' '\n' | tr -d '"' | $sed_exec -e 's#-I./mbed-os#-iwithprefixbefore/mbed#g' \ + | $sed_exec '/^-I./d' | tac \ > "$ARDUINOVARIANT"/includes.txt echo -n " copying to destination... " cut -d'/' -f3- < "$ARDUINOVARIANT"/includes.txt | grep '^targets/TARGET_' \ - | sed -e 's#\(.*\)#+ \1#' -e '$a+ targets/' -e '$a+ *.h' -e '$a- *' \ + | $sed_exec -e 's#\(.*\)#+ \1#' -e '$a+ targets/' -e '$a+ *.h' -e '$a- *' \ | rsync -avvz --include-from=- mbed-os/ "$ARDUINOCOREMBED"/ echo " done." @@ -187,18 +219,21 @@ copy_core_files () { patch_mbed_h () { echo -n "Patching 'mbed.h'..." - if [ x`uname` == xLinux ]; then - sed -i 's?#include "platform/mbed_version.h"?#include "platform/mbed_version.h"\n#include "mbed_config.h"?g' \ - "$ARDUINOCOREMBED"/mbed.h - else - ed "$ARDUINOCOREMBED"/mbed.h >/dev/null </dev/null <