|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Arduino-mk Makefile and project initialiser |
| 4 | +
|
| 5 | +This script can be used in its basic form create a project specific Makefile |
| 6 | +for use with Arduino-mk. Addionally, it can be used to create a template |
| 7 | +Arduino source file and a traditional boilerplate project file structure. |
| 8 | +
|
| 9 | +Example: |
| 10 | + * Run prompted within current working directory (requires Clint): `ardmk-init --cli` |
| 11 | + * Create Arduino Uno Makefile (useful within a library example): `ardmk-init -b uno` |
| 12 | + * Create boilerplate Arduino Uno project in current working directory of same |
| 13 | + name: `ardmk-init -b uno --project` |
| 14 | + * Create Arduino-mk nano Makefile in current working directory with template .ino: |
| 15 | + `ardmk-init -b nano -u atmega328 -tn my-project` |
| 16 | +
|
| 17 | +See `armk-init --help` for CLI arguments |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import print_function |
| 21 | +import os |
| 22 | +import argparse |
| 23 | + |
| 24 | +## Global Vars |
| 25 | +VERSION = "1.1" |
| 26 | +ARD_TEMPLATE = "\n\ |
| 27 | +#include <Arduino.h>\n\ |
| 28 | +#include <Wire.h>\n\ |
| 29 | +\n\ |
| 30 | +\n\ |
| 31 | +void setup() {\n\ |
| 32 | +}\n\ |
| 33 | +\n\ |
| 34 | +void loop() {\n\ |
| 35 | +}\n\ |
| 36 | +" |
| 37 | + |
| 38 | +## Command Parser |
| 39 | +PARSER = argparse.ArgumentParser(prog='ardmk-init', |
| 40 | + description='Arduino Makefile and boilerplate project generator.\ |
| 41 | + For use with Ard-Makefile: https://github.com/sudar/Arduino-Makefile.\ |
| 42 | + Script created by John Whittington https://github.com/tuna-f1sh 2017\ |
| 43 | + \n\nVersion: ' + VERSION) |
| 44 | +PARSER.add_argument('-v', '--verbose', action='store_true', |
| 45 | + help="print file contents during creation") |
| 46 | +PARSER.add_argument('-d', '--directory', default=os.getcwd(), help='directory to run generator, default cwd') |
| 47 | +PARSER.add_argument('-b', '--board', default='uno', help='board tag') |
| 48 | +PARSER.add_argument('-u', '--micro', default='AUTO', help='microcontroller on board') |
| 49 | +PARSER.add_argument('-f', '--freq', default='AUTO', help='clock frequency') |
| 50 | +PARSER.add_argument('-p', '--port', default='AUTO', help='monitor port') |
| 51 | +PARSER.add_argument('-n', '--name', default=os.path.basename(os.getcwd()), help='project name') |
| 52 | +PARSER.add_argument('--cli', action='store_true', help='run with user prompts (requires "Clint" module), rather than args') |
| 53 | +PARSER.add_argument('-P', '--project', action='store_true', |
| 54 | + help='create boilerplate project with src, lib and bin folder structure') |
| 55 | +PARSER.add_argument('-t', '--template', action='store_true', |
| 56 | + help='create bare minimum Arduino source file') |
| 57 | +PARSER.add_argument('-V', '--version', action='version', version='%(prog)s '+ VERSION) |
| 58 | +ARGS = PARSER.parse_args() |
| 59 | + |
| 60 | +try: |
| 61 | + from clint.textui import prompt, validators |
| 62 | +except ImportError: |
| 63 | + if ARGS.cli: |
| 64 | + print("Python module 'clint' is required for running prompted. Install the module or run with arguments only") |
| 65 | + quit() |
| 66 | + |
| 67 | + |
| 68 | +def generate_makefile(): |
| 69 | + """ |
| 70 | + Generate the Makefile content using prompts or parsed arguments |
| 71 | + """ |
| 72 | + # Header |
| 73 | + file_content = "# Generated by ard-make version " + VERSION + "\n\n" |
| 74 | + |
| 75 | + # Basic |
| 76 | + if ARGS.cli: |
| 77 | + print("Generating Arduino Ard-Makefile project in " |
| 78 | + + os.path.abspath(ARGS.directory)) |
| 79 | + btag = prompt.query('Board tag?', default='uno') |
| 80 | + if btag != 'uno': |
| 81 | + bsub = prompt.query('Board sub micro?', default='atmega328') |
| 82 | + f_cpu = prompt.query('Board frequency', default='16000000L') |
| 83 | + else: |
| 84 | + bsub = 'AUTO' |
| 85 | + f_cpu = 'AUTO' |
| 86 | + monitor_port = prompt.query('Arduino port?', default='AUTO') |
| 87 | + else: |
| 88 | + btag = ARGS.board |
| 89 | + bsub = ARGS.micro |
| 90 | + f_cpu = ARGS.freq |
| 91 | + monitor_port = ARGS.port |
| 92 | + |
| 93 | + file_content += check_define('BOARD_TAG', btag) |
| 94 | + file_content += check_define('BOARD_SUB', bsub) |
| 95 | + file_content += check_define('F_CPU', f_cpu) |
| 96 | + file_content += check_define('MONITOR_PORT', monitor_port) |
| 97 | + |
| 98 | + # Extended |
| 99 | + if ARGS.cli: |
| 100 | + if not prompt.yn('Extended options?', default='n'): |
| 101 | + if not prompt.yn('Define local folders?', default='n'): |
| 102 | + src_dir = prompt.query('Sources folder (Makefile will be created here)?', |
| 103 | + default='', validators=[]) |
| 104 | + userlibs = prompt.query('Library folder (will create if does not exist) - AUTO is Sketchbook directory?', |
| 105 | + default='AUTO', validators=[]) |
| 106 | + obj_dir = prompt.query('Output directory?', default='AUTO', validators=[]) |
| 107 | + else: |
| 108 | + src_dir = '' |
| 109 | + userlibs = 'AUTO' |
| 110 | + obj_dir = 'AUTO' |
| 111 | + boards_txt = prompt.query('Boards file?', default='AUTO') |
| 112 | + isp_prog = prompt.query('ISP programmer?', default='atmelice_isp') |
| 113 | + isp_port = prompt.query('ISP port?', default='AUTO') |
| 114 | + if not prompt.yn('Quiet make?', default='n'): |
| 115 | + file_content += "ARDUINO_QUIET = 1\n" |
| 116 | + |
| 117 | + file_content += check_define('ISP_PROG', isp_prog) |
| 118 | + file_content += check_define('ISP_PORT', isp_port) |
| 119 | + file_content += check_define('BOARDS_TXT', boards_txt) |
| 120 | + |
| 121 | + # Check andd create folders |
| 122 | + check_create_folder(src_dir) |
| 123 | + check_create_folder(userlibs) |
| 124 | + check_create_folder(obj_dir) |
| 125 | + |
| 126 | + # Makefile will be in src_dir so lib and bin must be relative |
| 127 | + if src_dir: |
| 128 | + userlibs = "../" + userlibs |
| 129 | + obj_dir = "../" + obj_dir |
| 130 | + |
| 131 | + file_content += check_define('USER_LIB_PATH', userlibs) |
| 132 | + file_content += check_define('OBJDIR', obj_dir) |
| 133 | + else: |
| 134 | + src_dir = '' |
| 135 | + |
| 136 | + if ARGS.template or not prompt.yn('Create template Arduino source?', default='n'): |
| 137 | + source_filename = prompt.query('Name of project?', |
| 138 | + default=os.path.basename(os.getcwd())) |
| 139 | + if src_dir: |
| 140 | + write_template(src_dir + "/" + source_filename) |
| 141 | + else: |
| 142 | + write_template(source_filename) |
| 143 | + file_content += check_define('TARGET', source_filename) |
| 144 | + |
| 145 | + else: |
| 146 | + if ARGS.project: |
| 147 | + src_dir = 'src' |
| 148 | + userlibs = 'lib' |
| 149 | + obj_dir = 'bin' |
| 150 | + else: |
| 151 | + src_dir = '' |
| 152 | + userlibs = 'AUTO' |
| 153 | + obj_dir = 'AUTO' |
| 154 | + |
| 155 | + # Check andd create folders |
| 156 | + check_create_folder(src_dir) |
| 157 | + check_create_folder(userlibs) |
| 158 | + check_create_folder(obj_dir) |
| 159 | + |
| 160 | + # Makefile will be in src_dir so lib and bin must be relative |
| 161 | + if src_dir: |
| 162 | + userlibs = "../" + userlibs |
| 163 | + obj_dir = "../" + obj_dir |
| 164 | + |
| 165 | + file_content += check_define('USER_LIB_PATH', userlibs) |
| 166 | + file_content += check_define('OBJDIR', obj_dir) |
| 167 | + |
| 168 | + if ARGS.project or ARGS.template: |
| 169 | + if src_dir: |
| 170 | + write_template(src_dir + "/" + ARGS.name) |
| 171 | + else: |
| 172 | + write_template(ARGS.name) |
| 173 | + file_content += check_define('TARGET', ARGS.name) |
| 174 | + |
| 175 | + if not "ARDMK_DIR" in os.environ: |
| 176 | + if not ARGS.cli: |
| 177 | + print("Warning: ARDMK_DIR environment variable not defined. \ |
| 178 | + Must be defined for Makefile to work") |
| 179 | + else: |
| 180 | + ardmk = prompt.query('Arduino Makefile path?', |
| 181 | + default='/usr/share/arduino', |
| 182 | + validators=[validators.PathValidator()]) |
| 183 | + ardmk = "ARDMK_DIR := " + ardmk + "\n" |
| 184 | + |
| 185 | + file_content += "\ninclude $(ARDMK_DIR)/Arduino.mk" |
| 186 | + |
| 187 | + # Add forward slash if source directory exists |
| 188 | + if src_dir: |
| 189 | + write_to_makefile(file_content, (src_dir + "/")) |
| 190 | + else: |
| 191 | + write_to_makefile(file_content, "") |
| 192 | + |
| 193 | + return file_content |
| 194 | + |
| 195 | +def write_to_makefile(file_content, path): |
| 196 | + """ |
| 197 | + Write the Makefile file |
| 198 | + """ |
| 199 | + makefile = open(path + "Makefile", 'w') |
| 200 | + print("Writing Makefile...") |
| 201 | + if ARGS.verbose: |
| 202 | + print(file_content) |
| 203 | + makefile.write(file_content) |
| 204 | + makefile.close() |
| 205 | + |
| 206 | +def write_template(filename): |
| 207 | + """ |
| 208 | + Write template Arduino .ino source |
| 209 | + """ |
| 210 | + print("Writing " + os.path.abspath(filename) + ".ino...") |
| 211 | + if os.path.isfile(filename + '.ino'): |
| 212 | + if not ARGS.cli: |
| 213 | + print(filename + '.ino' + ' already exists! Stopping.') |
| 214 | + return |
| 215 | + print(filename + '.ino' + ' already exists! Overwrite?') |
| 216 | + if prompt.yn('Continue?', default='n'): |
| 217 | + return |
| 218 | + src = open((filename + ".ino"), 'w') |
| 219 | + if ARGS.verbose: |
| 220 | + print(ARD_TEMPLATE) |
| 221 | + src.write("/* Project: " + filename + " */\n" + ARD_TEMPLATE) |
| 222 | + src.close() |
| 223 | + |
| 224 | +def check_create_folder(folder): |
| 225 | + """ |
| 226 | + Check if folder exists and make it if it doesn't and hasn't been set to AUTO |
| 227 | + """ |
| 228 | + if folder and not folder == 'AUTO': |
| 229 | + if not os.path.exists(folder): |
| 230 | + print("Creating " + os.path.abspath(folder) + " folder") |
| 231 | + os.makedirs(folder) |
| 232 | + |
| 233 | +def check_define(define, user): |
| 234 | + """ |
| 235 | + Check whether user has set define and return Makefile formatted string if they have |
| 236 | + """ |
| 237 | + # Return is empty unless user has passed value |
| 238 | + string = "" |
| 239 | + |
| 240 | + # Set define only if not empty or set to AUTO |
| 241 | + if user and not user == 'AUTO': |
| 242 | + string = define + " = " + user + "\n" |
| 243 | + |
| 244 | + return string |
| 245 | + |
| 246 | +def check_args(): |
| 247 | + """ |
| 248 | + Check input args will work with Makefile |
| 249 | + """ |
| 250 | + # Micro should be defined for non uno boards |
| 251 | + if ARGS.board != 'uno' and ARGS.micro == 'AUTO': |
| 252 | + print('\n!!! Warning: --micro should be defined and not left AUTO for non-Uno boards\n') |
| 253 | + |
| 254 | + |
| 255 | +if __name__ == '__main__': |
| 256 | + # Create directory if not exist |
| 257 | + check_create_folder(ARGS.directory) |
| 258 | + # Check input args |
| 259 | + check_args() |
| 260 | + # Change to dir so all commands are run relative |
| 261 | + os.chdir(ARGS.directory) |
| 262 | + if os.path.isfile('Makefile'): |
| 263 | + if not ARGS.cli: |
| 264 | + print('Makefile in ' + os.path.abspath(ARGS.directory) |
| 265 | + + ' already exists! Please remove before generating. Stopping.') |
| 266 | + quit() |
| 267 | + |
| 268 | + # Confirm with user if not quiet mode |
| 269 | + print('Makefile in ' + os.path.abspath(ARGS.directory) |
| 270 | + + ' already exists! Overwrite?') |
| 271 | + if prompt.yn('Continue?', default='n'): |
| 272 | + quit() |
| 273 | + # Run it |
| 274 | + generate_makefile() |
0 commit comments