Skip to content

Commit

Permalink
Add a script to run Travis CI locally.
Browse files Browse the repository at this point in the history
This isn't quite Travis, but close enough for local testing.
  • Loading branch information
iphydf committed May 3, 2020
1 parent 88b90c8 commit 94c51a5
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 36 deletions.
6 changes: 6 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
restylers:
- astyle:
arguments: ["--options=other/astyle/astylerc"]
- autopep8
- black
- prettier-yaml
- reorder-python-imports
- shellharden
- shfmt
- yapf
22 changes: 11 additions & 11 deletions .travis/cmake-linux
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ACTION="$1"
set -eu

CACHEDIR="$HOME/cache"
NPROC=`nproc`
NPROC=$(nproc)
ASTYLE="$CACHEDIR/astyle/build/gcc/bin/astyle"
ASTYLE_VERSION=3.1
TRAVIS_TOOL="https://raw.githubusercontent.com/TokTok/ci-tools/master/bin/travis-haskell"
Expand All @@ -21,7 +21,7 @@ travis_install() {
# Work around https://github.com/eddyxu/cpp-coveralls/issues/108 by manually
# installing the pyOpenSSL module and injecting it into urllib3 as per
# https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl-py2
sed -i -e '/^import sys$/a import urllib3.contrib.pyopenssl\nurllib3.contrib.pyopenssl.inject_into_urllib3()' `which coveralls`
sed -i -e '/^import sys$/a import urllib3.contrib.pyopenssl\nurllib3.contrib.pyopenssl.inject_into_urllib3()' "$(which coveralls)"
}

# Install astyle (version in ubuntu-precise too old).
Expand All @@ -34,15 +34,15 @@ travis_install() {
}

run_static_analysis() {
pylint -E other/analysis/check_recursion
pylint3 -E other/analysis/check_recursion

export CPPFLAGS="-isystem $CACHEDIR/include"
export LDFLAGS="-L$CACHEDIR/lib"
cat toxav/*.c toxcore/*.c toxencryptsave/*.c \
| clang `pkg-config --cflags libsodium opus vpx` \
-Itoxav -Itoxcore -Itoxencryptsave -S -emit-llvm -xc - -o- \
| opt -analyze -print-callgraph 2>&1 \
| other/analysis/check_recursion
cat toxav/*.c toxcore/*.c toxencryptsave/*.c |
clang "$(pkg-config --cflags libsodium opus vpx)" \
-Itoxav -Itoxcore -Itoxencryptsave -S -emit-llvm -xc - -o- |
opt -analyze -print-callgraph 2>&1 |
other/analysis/check_recursion
other/analysis/run-clang
other/analysis/run-clang-analyze
}
Expand Down Expand Up @@ -85,10 +85,10 @@ travis_script() {

cmake --build _build --parallel "$NPROC" --target install -- -k 0

cd _build # pushd
ctest -j50 --output-on-failure || \
cd _build # pushd
ctest -j50 --output-on-failure ||
ctest -j50 --output-on-failure --rerun-failed
cd - # popd
cd - # popd
}

if [ "-z" "$ACTION" ]; then
Expand Down
12 changes: 7 additions & 5 deletions other/analysis/check_recursion
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#!/usr/bin/env python
#!/usr/bin/env python3
"""
Tool to check for recursive calls in toxcore C code.
Usage:
Usage:
cat toxav/*.c toxcore/*.c toxencryptsave/*.c \
| clang `pkg-config --cflags libsodium opus vpx` \
-Itoxav -Itoxcore -Itoxencryptsave -S -emit-llvm -xc - -o- \
| opt -analyze -print-callgraph 2>&1 \
| other/analysis/check_recursion
"""

from __future__ import print_function

import collections
import fileinput
import re
import sys
import time


def load_callgraph():
"""
Parses the output from opt -print-callgraph from stdin or argv.
Expand All @@ -39,6 +37,7 @@ def load_callgraph():

return {k: sorted(v) for k, v in graph.items()}


def walk(visited, callgraph, cycles, stack, cur):
"""
Detects cycles in the callgraph and adds them to the cycles parameter.
Expand All @@ -54,12 +53,14 @@ def walk(visited, callgraph, cycles, stack, cur):
visited.add(callee)
stack.pop()


def get_time():
"""
Return the current time in milliseconds.
"""
return int(round(time.time() * 1000))


def find_recursion(expected):
"""
Main function: detects cycles and prints them.
Expand Down Expand Up @@ -98,6 +99,7 @@ def find_recursion(expected):
if expected or cycles:
sys.exit(1)


find_recursion(expected={
"add_to_closest -> add_to_closest",
"add_to_list -> add_to_list",
Expand Down
49 changes: 29 additions & 20 deletions other/astyle/format-source
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ FROM_JSON='s/\\"/"/g;s/^"(.*)"$/$1/;s/\\\\/\\/g;s/\\n/\n/g'
apidsl_request() {
TMPFILE=$(mktemp /tmp/apidsl.XXXXXX)
curl -s -o "$TMPFILE" -X POST --data @<(
echo '["Request",';
cat $2;
echo ']') https://apidsl.herokuapp.com/$1
if grep '\[1,"' "$TMPFILE" > /dev/null; then
echo '["Request",'
cat "$2"
echo ']'
) "https://apidsl.herokuapp.com/$1"
if grep '\[1,"' "$TMPFILE" >/dev/null; then
echo "Error: $(grep -o '".*"' /tmp/apidsl-$$ | perl -0777 -pe "$FROM_JSON")" >&2
rm "$TMPFILE"
exit 1
Expand All @@ -52,34 +53,42 @@ apidsl_curl() {
echo "apidsl_curl $*" >&2
apidsl_request "c" <(
apidsl_request "parse" <(
perl -0777 -pe "$TO_JSON" $1)) | perl -0777 -pe "$FROM_JSON"
perl -0777 -pe "$TO_JSON" "$1"
)
) | perl -0777 -pe "$FROM_JSON"
}

# Check if apidsl generated sources are up to date.
set +x
$APIDSL toxcore/LAN_discovery.api.h > toxcore/LAN_discovery.h &
$APIDSL toxcore/crypto_core.api.h > toxcore/crypto_core.h &
$APIDSL toxcore/ping.api.h > toxcore/ping.h &
$APIDSL toxcore/ping_array.api.h > toxcore/ping_array.h &
$APIDSL toxcore/tox.api.h > toxcore/tox.h &
$APIDSL toxav/toxav.api.h > toxav/toxav.h &
$APIDSL toxencryptsave/toxencryptsave.api.h > toxencryptsave/toxencryptsave.h &
"$APIDSL" toxcore/LAN_discovery.api.h >toxcore/LAN_discovery.h &
"$APIDSL" toxcore/crypto_core.api.h >toxcore/crypto_core.h &
"$APIDSL" toxcore/ping.api.h >toxcore/ping.h &
"$APIDSL" toxcore/ping_array.api.h >toxcore/ping_array.h &
"$APIDSL" toxcore/tox.api.h >toxcore/tox.h &
"$APIDSL" toxav/toxav.api.h >toxav/toxav.h &
"$APIDSL" toxencryptsave/toxencryptsave.api.h >toxencryptsave/toxencryptsave.h &
set -x

wait; wait; wait; wait; wait; wait; wait
wait
wait
wait
wait
wait
wait
wait

if grep '<unresolved>' ./*/*.h; then
echo "error: some apidsl references were unresolved"
exit 1
fi

CC_SOURCES=$(find . '(' -name '*.cc' ')')
CC_SOURCES="$CC_SOURCES toxcore/crypto_core.c"
CC_SOURCES="$CC_SOURCES toxcore/ping_array.c"
CC_SOURCES=("$(find . '(' -name '*.cc' ')')")
CC_SOURCES+=(toxcore/crypto_core.c)
CC_SOURCES+=(toxcore/ping_array.c)

for bin in clang-format-6.0 clang-format-5.0 clang-format; do
if which "$bin"; then
"$bin" -i -style='{BasedOnStyle: Google, ColumnLimit: 100}' $CC_SOURCES
"$bin" -i -style='{BasedOnStyle: Google, ColumnLimit: 100}' "${CC_SOURCES[@]}"
break
fi
done
Expand All @@ -91,8 +100,8 @@ FIND="$FIND -and -not -wholename './super_donators/*'"
FIND="$FIND -and -not -wholename './third_party/*'"
FIND="$FIND -and -not -wholename './toxencryptsave/crypto_pwhash*'"

C_SOURCES=$(eval "$FIND")
C_SOURCES=("$(eval "$FIND")")

$ASTYLE -n --options=other/astyle/astylerc $C_SOURCES
"$ASTYLE" -n --options=other/astyle/astylerc "${C_SOURCES[@]}"

git diff --exit-code
git diff --color=always --exit-code
62 changes: 62 additions & 0 deletions other/docker/Dockerfile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This Docker build emulates roughly what Travis CI is doing. It is not exactly
# the same (different tool versions) and success in this image may not
# necessarily mean success on Travis. This image is also not automatically
# tested, so it may get out of date. Send PRs if you use it and it's broken.
#
# For one, we use bionic, not xenial, because xenial's clang is way too old.
FROM ubuntu:18.04

# Travis environment.
RUN apt-get update && apt-get install --no-install-recommends -y \
build-essential \
ca-certificates \
clang \
clang-format \
curl \
git \
llvm \
pkg-config \
python-pip \
python-setuptools \
python3 \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Bionic's cmake is too old.
RUN pip install --upgrade pip cmake

# .travis.yml
RUN apt-get update && apt-get install --no-install-recommends -y \
libconfig-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \
libvpx-dev \
ninja-build \
pylint3 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Set up travis user.
RUN groupadd -r -g 1000 travis \
&& useradd --no-log-init -r -g travis -u 1000 travis \
&& mkdir -p /src/workspace /home/travis \
&& chown travis:travis /home/travis
USER travis

# Set up environment.
ENV CC=gcc CXX=g++ \
PATH=/home/travis/.local/bin:$PATH \
TRAVIS_REPO_SLUG=TokTok/c-toxcore

# Copy minimal files to run "cmake-linux install", so we can avoid rebuilding
# astyle and other things when only source files change.
RUN mkdir -p /home/travis/build/c-toxcore /home/travis/cache
WORKDIR /home/travis/build/c-toxcore
COPY --chown=travis:travis c-toxcore/.travis/ /home/travis/build/c-toxcore/.travis/
RUN .travis/cmake-linux install

# Now copy the rest of the sources and run the build.
COPY --chown=travis:travis . /home/travis/build/
RUN .travis/cmake-linux script
15 changes: 15 additions & 0 deletions other/docker/run-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -eu

FILES=$(git ls-files | sed -e 's,^,c-toxcore/,')

if [ -f .git ]; then
cd ..
tar -c "$FILES" "c-toxcore/.git" ".git/modules/c-toxcore" |
docker build -f c-toxcore/other/docker/Dockerfile.ci -
else
cd ..
tar -c "$FILES" "c-toxcore/.git" |
docker build -f c-toxcore/other/docker/Dockerfile.ci -
fi

0 comments on commit 94c51a5

Please sign in to comment.