Skip to content

Commit 031a18c

Browse files
committed
Build sketches with arduino-builder
1 parent 678a8ff commit 031a18c

File tree

4 files changed

+157
-8
lines changed

4 files changed

+157
-8
lines changed

.travis.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ script:
1515
- export CXX="g++-4.8" CC="gcc-4.8" GCOV="gcov-4.8"
1616
- pushd $TRAVIS_BUILD_DIR/tests/host
1717
- make
18-
- bash <(curl -s https://codecov.io/bash) -X gcov
19-
- make clean
18+
- make clean-objects
2019
- popd
2120
- wget -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz
2221
- tar xf arduino.tar.xz
@@ -34,10 +33,12 @@ script:
3433
- which arduino
3534
- cd $TRAVIS_BUILD_DIR
3635
- source tests/common.sh
37-
- arduino --board esp8266com:esp8266:generic --save-prefs
38-
- arduino --get-pref sketchbook.path
3936
- install_libraries
40-
- build_sketches arduino $TRAVIS_BUILD_DIR
37+
- build_sketches $HOME/arduino_ide $TRAVIS_BUILD_DIR "python tools/build.py -l $HOME/Arduino/libraries -b generic -v"
38+
39+
after_success:
40+
- pushd $TRAVIS_BUILD_DIR/tests/host
41+
- bash <(curl -s https://codecov.io/bash) -X gcov
4142

4243
notifications:
4344
email:

tests/common.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ function build_sketches()
44
{
55
local arduino=$1
66
local srcpath=$2
7+
local build_cmd=$3
8+
echo $build_cmd
79
local sketches=$(find $srcpath -name *.ino)
10+
export ARDUINO_IDE_PATH=$arduino
811
for sketch in $sketches; do
912
local sketchdir=$(dirname $sketch)
1013
if [[ -f "$sketchdir/.test.skip" ]]; then
1114
echo -e "\n\n ------------ Skipping $sketch ------------ \n\n";
1215
continue
1316
fi
1417
echo -e "\n\n ------------ Building $sketch ------------ \n\n";
15-
$arduino --verify $sketch;
18+
# $arduino --verify $sketch;
19+
echo "$build_cmd $sketch"
20+
time $build_cmd $sketch
1621
local result=$?
1722
if [ $result -ne 0 ]; then
1823
echo "Build failed ($1)"
@@ -25,7 +30,7 @@ function install_libraries()
2530
{
2631
mkdir -p $HOME/Arduino/libraries
2732
pushd $HOME/Arduino/libraries
28-
33+
2934
# install ArduinoJson library
3035
wget https://github.com/bblanchon/ArduinoJson/releases/download/v4.6.1/ArduinoJson-v4.6.1.zip && unzip ArduinoJson-v4.6.1.zip
3136

tests/host/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ all: build-info $(OUTPUT_BINARY) test gcov
6262
test: $(OUTPUT_BINARY)
6363
$(OUTPUT_BINARY)
6464

65-
clean:
65+
clean: clean-objects clean-coverage
6666
rm -rf $(BINARY_DIRECTORY)
67+
68+
clean-objects:
6769
rm -rf $(OBJECTS)
70+
71+
clean-coverage:
6872
rm -rf $(COVERAGE_FILES) *.gcov
6973

7074
gcov: test

tools/build.py

+139
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# build.py — build a sketch using arduino-builder
5+
#
6+
# Wrapper script around arduino-builder which accepts some ESP8266-specific
7+
# options and translates them into FQBN
8+
#
9+
# Copyright © 2016 Ivan Grokhotkov
10+
#
11+
# Permission is hereby granted, free of charge, to any person obtaining a copy
12+
# of this software and associated documentation files (the "Software"), to deal
13+
# in the Software without restriction, including without limitation the rights
14+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15+
# copies of the Software, and to permit persons to whom the Software is
16+
# furnished to do so, subject to the following conditions:
17+
#
18+
# The above copyright notice and this permission notice shall be included in
19+
# all copies or substantial portions of the Software.
20+
#
21+
#
22+
23+
24+
from __future__ import print_function
25+
import sys
26+
import os
27+
import argparse
28+
import serial
29+
import subprocess
30+
import tempfile
31+
import shutil
32+
33+
def compile(tmp_dir, sketch, tools_dir, hardware_dir, ide_path, f, args):
34+
cmd = ide_path + '/arduino-builder '
35+
cmd += '-compile -logger=human '
36+
for lib_dir in args.library_path:
37+
cmd += '-libraries "' + lib_dir + '" '
38+
cmd += '-build-path "' + tmp_dir + '" '
39+
cmd += '-hardware "' + ide_path + '/hardware" '
40+
cmd += '-hardware ' + hardware_dir + ' '
41+
cmd += '-tools "' + ide_path + '/tools-builder" '
42+
# Debug=Serial,DebugLevel=Core____
43+
cmd += '-fqbn=esp8266com:esp8266:{board_name}:' \
44+
'UploadTool=esptool,' \
45+
'CpuFrequency={cpu_freq},' \
46+
'FlashFreq={flash_freq},' \
47+
'FlashMode={flash_mode},' \
48+
'UploadSpeed=921600,' \
49+
'FlashSize={flash_size},' \
50+
'ResetMethod=nodemcu'.format(**vars(args))
51+
if args.debug_port and args.debug_level:
52+
cmd += 'Debug={debug_port},DebugLevel={debug_level}'.format(**vars(args))
53+
cmd += ' '
54+
cmd += '-ide-version=10607 '
55+
cmd += '-warnings={warnings} '.format(**vars(args))
56+
if args.verbose:
57+
cmd += '-verbose '
58+
cmd += sketch
59+
60+
if args.verbose:
61+
print('Building: ' + cmd, file=f)
62+
63+
cmds = cmd.split(' ')
64+
p = subprocess.Popen(cmds, stdout=f, stderr=subprocess.STDOUT)
65+
p.wait()
66+
return p.returncode
67+
68+
def parse_args():
69+
parser = argparse.ArgumentParser(description='Sketch build helper')
70+
parser.add_argument('-v', '--verbose', help='Enable verbose output',
71+
action='store_true')
72+
parser.add_argument('-i', '--ide_path', help='Arduino IDE path')
73+
parser.add_argument('-p', '--build_path', help='Build directory')
74+
parser.add_argument('-l', '--library_path', help='Additional library path',
75+
action='append')
76+
parser.add_argument('-b', '--board_name', help='Board name', default='generic')
77+
parser.add_argument('-s', '--flash_size', help='Flash size', default='512K64',
78+
choices=['512K0', '512K64', '1M512', '4M1M', '4M3M'])
79+
parser.add_argument('-f', '--cpu_freq', help='CPU frequency', default=80,
80+
choices=[80, 160], type=int)
81+
parser.add_argument('-m', '--flash_mode', help='Flash mode', default='qio',
82+
choices=['dio', 'qio'])
83+
parser.add_argument('-w', '--warnings', help='Compilation warnings level',
84+
default='none', choices=['none', 'all', 'more'])
85+
parser.add_argument('-o', '--output_binary', help='File name for output binary')
86+
parser.add_argument('-k', '--keep', action='store_true',
87+
help='Don\'t delete temporary build directory')
88+
parser.add_argument('--flash_freq', help='Flash frequency', default=40,
89+
type=int, choices=[40, 80])
90+
parser.add_argument('--debug_port', help='Debug port',
91+
choices=['Serial', 'Serial1'])
92+
parser.add_argument('--debug_level', help='Debug level')
93+
parser.add_argument('sketch_path', help='Sketch file path')
94+
return parser.parse_args()
95+
96+
def main():
97+
args = parse_args()
98+
99+
ide_path = args.ide_path
100+
if not ide_path:
101+
ide_path = os.environ.get('ARDUINO_IDE_PATH')
102+
if not ide_path:
103+
print("Please specify Arduino IDE path via --ide_path option"
104+
"or ARDUINO_IDE_PATH environment variable.", file=sys.stderr)
105+
return 2
106+
107+
sketch_path = args.sketch_path
108+
tmp_dir = args.build_path
109+
created_tmp_dir = False
110+
if not tmp_dir:
111+
tmp_dir = tempfile.mkdtemp()
112+
created_tmp_dir = True
113+
114+
tools_dir = os.path.dirname(os.path.realpath(__file__)) + '/../tools'
115+
hardware_dir = os.path.dirname(os.path.realpath(__file__)) + '/../cores'
116+
117+
output_name = tmp_dir + '/' + os.path.basename(sketch_path) + '.bin'
118+
if args.verbose:
119+
print("Sketch: ", sketch_path)
120+
print("Build dir: ", tmp_dir)
121+
print("Output: ", output_name)
122+
123+
if args.verbose:
124+
f = sys.stdout
125+
else:
126+
f = open(tmp_dir + '/build.log', 'w')
127+
128+
res = compile(tmp_dir, sketch_path, tools_dir, hardware_dir, ide_path, f, args)
129+
if res != 0:
130+
return res
131+
132+
if args.output_binary is not None:
133+
shutil.copy(output_name, args.output_binary)
134+
135+
if created_tmp_dir and not args.keep:
136+
shutil.rmtree(tmp_dir, ignore_errors=True)
137+
138+
if __name__ == '__main__':
139+
sys.exit(main())

0 commit comments

Comments
 (0)