Skip to content

Instantly share code, notes, and snippets.

@PIlin
Created January 26, 2013 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PIlin/4641886 to your computer and use it in GitHub Desktop.
Save PIlin/4641886 to your computer and use it in GitHub Desktop.
This script is analogue of this script for cmake (https://gist.github.com/4641812), but it works with ino tool for arduino.
# Get file compile ooptions from ino database
#
# Usage within SublimeClang:
# "sublimeclang_options_script": "python ${project_path:ino_compile_options.py}",
# "sublimeclang_options":
# [
# "-Wno-attributes",
# "-Wno-builtin-requires-header",
# "-Wno-unused-command-line-argument"
# ]
#
import pickle
# from pprint import pprint
import os
import sys
import re
def find_dffile(path):
# recursive walk up in folder tree
dbfile = path + r'/.build/environment.pickle'
if os.path.isfile(dbfile):
return dbfile
else:
newpath = os.path.dirname(path)
if path == newpath:
raise Exception('Unable to find .build/environment.pickle')
return find_dffile(newpath)
def load_db(filename):
db = {}
with open(filename) as dbfile:
l = pickle.load(dbfile)
for r in l:
db[r[0]] = r[1]
pass
pass
# pprint(db)
return db
def parse_db(db):
opts = []
# cflags and cxxflags
opts.extend(db['cflags'])
opts.extend(db['cxxflags'])
# Recovering the path avr include directories
avr = db['cc'] # .../avr/bin/avr-gcc
avr = os.path.dirname(avr) # .../avr/bin
avr = os.path.dirname(avr) # .../avr
opts.append("-I" + avr + r'/avr-4/include/')
opts.append("-I" + avr + r'/lib/gcc/avr/4.3.2/include')
# Recovering MCU type from linker command
recpu = re.compile(r"-mmcu=atmega(.+)")
for o in opts:
m = recpu.match(o)
if m:
mm = m.group(1)
opts.append("-D__AVR_ATmega{0}__".format(mm.upper()))
return opts
def file_specific(srcfile):
ext = os.path.splitext(srcfile)[1]
opts = []
if ext == '.ino':
# .ino files need Arduino.h to be included
opts.append('-includeArduino.h')
return opts
srcfile = sys.argv[1]
dbpath = find_dffile(os.path.dirname(srcfile))
db = load_db(dbpath)
for line in parse_db(db):
print line
for line in file_specific(srcfile):
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment