Merge pull request #13 from utzig/travis-updates

Add Zephyr support to CI
diff --git a/.travis.yml b/.travis.yml
index 49ed667..1040613 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -35,19 +35,28 @@
           packages:
             - gcc-7-multilib
             - linux-libc-dev:i386
+      env:
+        - OS=mynewt
+    - os: linux
+      language: python
+      python:
+        - "3.5"
+      addons:
+        apt:
+          packages:
+            - gperf
+      env:
+        - OS=zephyr
 
 before_install:
   - printenv
-  - go version
 
 install:
-  - mkdir -p $HOME/bin
-  - export PATH=$HOME/bin:$PATH
-  - ./ci/install.sh
-
-before_script:
-  - newt version
-  - gcc --version
+  - ./ci/${OS}_install.sh
 
 script:
-  - newt test test
+  - ./ci/${OS}_run.sh
+
+cache:
+  directories:
+  - $HOME/zephyr_sdk_download
diff --git a/ci/mynewt_install.sh b/ci/mynewt_install.sh
new file mode 100755
index 0000000..69ba927
--- /dev/null
+++ b/ci/mynewt_install.sh
@@ -0,0 +1,40 @@
+#!/bin/bash -x
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# Debugging
+go version
+
+mkdir -p $HOME/bin
+export PATH=$HOME/bin:$PATH
+
+pushd $HOME
+git clone --depth=1 https://github.com/apache/mynewt-newt
+[[ $? -ne 0 ]] && exit 1
+
+pushd mynewt-newt && ./build.sh
+[[ $? -ne 0 ]] && exit 1
+
+cp newt/newt $HOME/bin
+popd
+popd
+
+ln -s /usr/bin/gcc-7 $HOME/bin/gcc
+
+mkdir -p repos/apache-mynewt-core
+git clone --depth=1 https://github.com/apache/mynewt-core repos/apache-mynewt-core
diff --git a/ci/mynewt_run.sh b/ci/mynewt_run.sh
new file mode 100755
index 0000000..72e5075
--- /dev/null
+++ b/ci/mynewt_run.sh
@@ -0,0 +1,29 @@
+#!/bin/bash -x
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+export PATH=$HOME/bin:$PATH
+
+# Debugging information
+newt version
+[[ $? -ne 0 ]] && exit 1
+
+gcc --version
+[[ $? -ne 0 ]] && exit 1
+
+newt test test
diff --git a/ci/zephyr_install.sh b/ci/zephyr_install.sh
new file mode 100755
index 0000000..cb7e078
--- /dev/null
+++ b/ci/zephyr_install.sh
@@ -0,0 +1,33 @@
+#!/bin/bash -x
+
+PWD=$(pwd)
+
+ZEPHYR_SDK_RELEASE="0.9.5"
+ZEPHYR_SDK="zephyr-sdk-${ZEPHYR_SDK_RELEASE}-setup.run"
+ZEPHYR_SDK_URL="https://github.com/zephyrproject-rtos/meta-zephyr-sdk/releases/download/${ZEPHYR_SDK_RELEASE}/${ZEPHYR_SDK}"
+ZEPHYR_SDK_DIR="${HOME}/zephyr-sdk"
+
+export ZEPHYR_BASE="$HOME/zephyr"
+ZEPHYR_NFFS_DIR="${ZEPHYR_BASE}/ext/fs/nffs"
+
+export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
+
+git clone --depth=1 https://github.com/zephyrproject-rtos/zephyr.git $ZEPHYR_BASE
+source ${ZEPHYR_BASE}/zephyr-env.sh
+
+download_dir=$HOME/zephyr_sdk_download
+mkdir -p ${download_dir}
+
+# TODO: cache zephyr-sdk
+wget -O ${download_dir}/${ZEPHYR_SDK} -c ${ZEPHYR_SDK_URL}
+[[ $? -ne 0 ]] && exit 1
+
+chmod +x ${download_dir}/${ZEPHYR_SDK}
+${download_dir}/${ZEPHYR_SDK} --quiet -- -d "${ZEPHYR_SDK_DIR}"
+[[ $? -ne 0 ]] && exit 1
+
+pip install PyYAML pyelftools
+
+# Overwrite bundled Zephyr NFFS with current dev version
+cp src/* ${ZEPHYR_NFFS_DIR}/src
+cp include/nffs/* ${ZEPHYR_NFFS_DIR}/include/nffs
diff --git a/ci/zephyr_run.sh b/ci/zephyr_run.sh
new file mode 100755
index 0000000..2be666d
--- /dev/null
+++ b/ci/zephyr_run.sh
@@ -0,0 +1,45 @@
+#!/bin/bash -x
+
+export ZEPHYR_TOOLCHAIN_VARIANT=zephyr
+export ZEPHYR_SDK_INSTALL_DIR=$HOME/zephyr-sdk
+export ZEPHYR_BASE=$HOME/zephyr
+
+mkdir zephyr-build && pushd zephyr-build
+
+cmake -DBOARD=qemu_x86 $ZEPHYR_BASE/tests/subsys/fs/nffs_fs_api/basic
+[[ $? -ne 0 ]] && exit 1
+
+make
+[[ $? -ne 0 ]] && exit 1
+
+# Run qemu in background, capture output and kill when test is finished
+output="${HOME}/zephyr-results.txt"
+(make run | tee ${output}) &
+
+# qemu output strings in case of failure
+failed1="ASSERTION FAIL"
+failed2="CPU Page Fault"
+succeded="PROJECT EXECUTION SUCCESSFUL"
+MAX_LOOPS=10
+count=1
+while true; do
+    [[ $count == $MAX_LOOPS ]] && break
+    grep -q -e "${failed1}" -e "${failed2}" -e "${succeded}" ${output}
+    [[ $? -eq 0 ]] && break
+
+    sleep 3
+    count=$((count + 1))
+done
+
+[[ $count == $MAX_LOOPS ]] && exit 1
+
+kill $(pgrep qemu)
+sleep 5  # give some time for qemu to close
+
+cat ${output}
+
+grep -q -e "${failed1}" -e "${failed2}" ${output}
+[[ $? -eq 0 ]] && exit 1
+
+# If the grep above fails, the test succeeded, so force success exit!
+exit 0