|
| 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 subprocess |
| 29 | +import tempfile |
| 30 | +import shutil |
| 31 | + |
| 32 | +def compile(tmp_dir, sketch, tools_dir, hardware_dir, ide_path, f, args): |
| 33 | + cmd = ide_path + '/arduino-builder ' |
| 34 | + cmd += '-compile -logger=human ' |
| 35 | + cmd += '-build-path "' + tmp_dir + '" ' |
| 36 | + cmd += '-tools "' + ide_path + '/tools-builder" ' |
| 37 | + if args.library_path: |
| 38 | + for lib_dir in args.library_path: |
| 39 | + cmd += '-libraries "' + lib_dir + '" ' |
| 40 | + cmd += '-hardware "' + ide_path + '/hardware" ' |
| 41 | + if args.hardware_dir: |
| 42 | + for hw_dir in args.hardware_dir: |
| 43 | + cmd += '-hardware "' + hw_dir + '" ' |
| 44 | + else: |
| 45 | + cmd += '-hardware "' + hardware_dir + '" ' |
| 46 | + # Debug=Serial,DebugLevel=Core____ |
| 47 | + cmd += '-fqbn=espressif:esp32:{board_name}:' \ |
| 48 | + 'FlashFreq={flash_freq},' \ |
| 49 | + 'UploadSpeed=921600' |
| 50 | + cmd += ' ' |
| 51 | + cmd += '-ide-version=10607 ' |
| 52 | + cmd += '-warnings={warnings} '.format(**vars(args)) |
| 53 | + if args.verbose: |
| 54 | + cmd += '-verbose ' |
| 55 | + cmd += sketch |
| 56 | + |
| 57 | + if args.verbose: |
| 58 | + print('Building: ' + cmd, file=f) |
| 59 | + |
| 60 | + cmds = cmd.split(' ') |
| 61 | + p = subprocess.Popen(cmds, stdout=f, stderr=subprocess.STDOUT) |
| 62 | + p.wait() |
| 63 | + return p.returncode |
| 64 | + |
| 65 | +def parse_args(): |
| 66 | + parser = argparse.ArgumentParser(description='Sketch build helper') |
| 67 | + parser.add_argument('-v', '--verbose', help='Enable verbose output', |
| 68 | + action='store_true') |
| 69 | + parser.add_argument('-i', '--ide_path', help='Arduino IDE path') |
| 70 | + parser.add_argument('-p', '--build_path', help='Build directory') |
| 71 | + parser.add_argument('-l', '--library_path', help='Additional library path', |
| 72 | + action='append') |
| 73 | + parser.add_argument('-d', '--hardware_dir', help='Additional hardware path', |
| 74 | + action='append') |
| 75 | + parser.add_argument('-b', '--board_name', help='Board name', default='esp32') |
| 76 | + parser.add_argument('-w', '--warnings', help='Compilation warnings level', |
| 77 | + default='none', choices=['none', 'all', 'more']) |
| 78 | + parser.add_argument('-o', '--output_binary', help='File name for output binary') |
| 79 | + parser.add_argument('-k', '--keep', action='store_true', |
| 80 | + help='Don\'t delete temporary build directory') |
| 81 | + parser.add_argument('--flash_freq', help='Flash frequency', default=40, |
| 82 | + type=int, choices=[40, 80]) |
| 83 | + parser.add_argument('sketch_path', help='Sketch file path') |
| 84 | + return parser.parse_args() |
| 85 | + |
| 86 | +def main(): |
| 87 | + args = parse_args() |
| 88 | + |
| 89 | + ide_path = args.ide_path |
| 90 | + if not ide_path: |
| 91 | + ide_path = os.environ.get('ARDUINO_IDE_PATH') |
| 92 | + if not ide_path: |
| 93 | + print("Please specify Arduino IDE path via --ide_path option" |
| 94 | + "or ARDUINO_IDE_PATH environment variable.", file=sys.stderr) |
| 95 | + return 2 |
| 96 | + |
| 97 | + sketch_path = args.sketch_path |
| 98 | + tmp_dir = args.build_path |
| 99 | + created_tmp_dir = False |
| 100 | + if not tmp_dir: |
| 101 | + tmp_dir = tempfile.mkdtemp() |
| 102 | + created_tmp_dir = True |
| 103 | + |
| 104 | + tools_dir = os.path.dirname(os.path.realpath(__file__)) + '/../tools' |
| 105 | + # this is not the correct hardware folder to add. |
| 106 | + hardware_dir = os.path.dirname(os.path.realpath(__file__)) + '/../cores' |
| 107 | + |
| 108 | + output_name = tmp_dir + '/' + os.path.basename(sketch_path) + '.bin' |
| 109 | + if args.verbose: |
| 110 | + print("Sketch: ", sketch_path) |
| 111 | + print("Build dir: ", tmp_dir) |
| 112 | + print("Output: ", output_name) |
| 113 | + |
| 114 | + if args.verbose: |
| 115 | + f = sys.stdout |
| 116 | + else: |
| 117 | + f = open(tmp_dir + '/build.log', 'w') |
| 118 | + |
| 119 | + res = compile(tmp_dir, sketch_path, tools_dir, hardware_dir, ide_path, f, args) |
| 120 | + if res != 0: |
| 121 | + return res |
| 122 | + |
| 123 | + if args.output_binary is not None: |
| 124 | + shutil.copy(output_name, args.output_binary) |
| 125 | + |
| 126 | + if created_tmp_dir and not args.keep: |
| 127 | + shutil.rmtree(tmp_dir, ignore_errors=True) |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + sys.exit(main()) |
0 commit comments