AVRSack/AVRsack/BuildProject

146 lines
4.5 KiB
Plaintext
Raw Normal View History

2014-11-28 13:18:53 +00:00
#!/usr/bin/ruby
#
# BuildProject board=... mcu=... -- FILE1 FILE2 FILE3
#
# AVRsack
#
# Created by Matthias Neeracher on 11/26/14.
# Copyright © 2014 Aere Perennius. All rights reserved.
#
require 'fileutils.rb'
2014-11-30 21:32:14 +00:00
require 'rake'
2014-11-28 13:18:53 +00:00
BUILD = {
'board' => 'uno',
'mcu' => 'atmega328p',
'f_cpu' => 16000000,
'core' => 'arduino',
'variant' => 'standard',
}
def parseArguments
while ARGV.length > 0 do
param = ARGV.shift
break if param == '--'
param =~ /(\S+?)=(\S*)/
BUILD[$1] = $2
end
end
def createBuildDirectory
$BUILD_DIR = "build/#{BUILD['board']}"
$SKETCH_DIR = "#{$BUILD_DIR}/sketch"
FileUtils::mkdir_p "#{$SKETCH_DIR}", :verbose => true
end
2014-11-30 21:32:14 +00:00
HEADERMAP = {}
def buildHeaderMap
paths = Rake::FileList.new(BUILD['libs'].split(':').reverse)
paths.each do |path|
libs = Rake::FileList.new(path+"/*")
libs.each do |lib|
headers = Rake::FileList.new(lib+"/**/*.h")
headers.each do |h|
name = h[lib.length+1..-1]
next if name =~ %r|^utility/|
if !HEADERMAP[name] or HEADERMAP[name].pathmap('%f') != name.pathmap('%X')
HEADERMAP[name] = lib
end
end
end
end
p HEADERMAP
end
2014-11-28 13:18:53 +00:00
def parseInoFiles
$LIBRARIES = []
2014-11-30 21:32:14 +00:00
$CORES = [BUILD['core_path']]
if BUILD['variant_path']
$CORES << BUILD['variant_path']
end
2014-11-28 13:18:53 +00:00
ARGV.each_index do |arg|
if ARGV[arg] =~ /\.ino$/
outName = "#{$SKETCH_DIR}/"+File.basename(ARGV[arg], '.ino')+".cpp"
outFile = File.open(outName, 'w')
File.open(ARGV[arg], 'r') do |ino|
contents = ino.read
# Find protypes:
prototypes = contents.dup
# - Strip comments, quoted strings, and preprocessor directives
prototypes.gsub!(%r{'(?:[^']|\\')+'|"(?:[^"]|\\")*"|//.*?$|/\*.*?\*/|^\s*?#.*?$}m, ' ')
# Collapse braces
while prototypes.sub!(/(\{)(?:[^{}]+|\{[^{}]*\})/m, '\1') do
end
existingProto = {}
prototypes.scan(/[\w\[\]\*]+\s+[&\[\]\*\w\s]+\([&,\[\]\*\w\s]*\)(?=\s*;)/) {|p|
existingProto[smashSpaces(p)] = true
}
proto = []
prototypes.scan(/[\w\[\]\*]+\s+[&\[\]\*\w\s]+\([&,\[\]\*\w\s]*\)(?=\s*{)/) {|p|
p = smashSpaces(p)
2014-11-28 23:35:46 +00:00
proto << p+";\n" unless existingProto[p]
2014-11-28 13:18:53 +00:00
}
contents.each_line do |line|
if line =~ /^\s*#include\s+[<"](.*)[">]\s*(#.*)?$/
addLibrary($1)
end
end
2014-11-28 23:35:46 +00:00
%r{(?<preamble>(?:\s+|//.*$|/\*.*?\*/)*)(?<rest>.*)}m =~ contents
outFile.print preamble
outFile.puts '#include "Arduino.h"'
outFile.print proto.join('')
outFile.puts "#line #{1+preamble.count("\n")}"
outFile.print rest
2014-11-28 13:18:53 +00:00
end
outFile.close
ARGV[arg] = outName
end
end
end
def smashSpaces(s)
return s.gsub(/(\W)\s+(\W)/, '\1\2').gsub(/\s+/, ' ')
end
def addLibrary(header)
2014-11-30 21:32:14 +00:00
lib = HEADERMAP[header]
if lib && !$LIBRARIES.include?(lib)
$LIBRARIES << lib
2014-11-28 13:18:53 +00:00
end
end
parseArguments
createBuildDirectory
2014-11-30 21:32:14 +00:00
buildHeaderMap
2014-11-28 13:18:53 +00:00
parseInoFiles
File.open("#{$BUILD_DIR}/Rakefile", 'w') do |rakeFile|
2014-11-30 21:32:14 +00:00
SOURCES = FileList.new(ARGV).select {|f| f =~ /\.(c|cpp|cp|cxx|S)$/}
INCLUDES= ($CORES+$LIBRARIES).map {|l| " +\n \" -I'#{l}'\""}.join('')
2014-11-28 13:18:53 +00:00
rakeFile.print <<END_RAKE
2014-11-30 21:32:14 +00:00
CC = '/usr/local/CrossPack-AVR/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=#{BUILD['mcu']} -DF_CPU=#{BUILD['f_cpu']} -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I ../..'#{INCLUDES}
AR = '/usr/local/CrossPack-AVR/bin/avr-ar crsv'
LIBRARIES = [#{$LIBRARIES.map{|l| "'"+l+"'"}.join(' ')}]
def compile(dest, *src)
directory dest
archive = dest.ext('.a')
sources = Rake::FileList.new(src)
objects = sources.pathmap(dest+"/%n.o")
objects.each_index do |i|
file objects[i] => sources[i] do
sh "%s '%s' -o '%s'" % [CC, sources[i], objects[i]]
end
end
file archive => objects do
sh ("%s '%s' " % [AR, archive])+objects.map {|o| "'"+o+"'"}.join(" ")
end
end
compile('sketch', #{SOURCES.map{|f| "'../../"+f+"'"}.join(', ')})
2014-11-28 13:18:53 +00:00
END_RAKE
end