|
| 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