Update coding style to match our own official coding guidelines

This commit is contained in:
mvandervoord
2020-03-18 19:16:58 -04:00
parent 9f8624a722
commit 67858837d1
27 changed files with 797 additions and 778 deletions
+10 -10
View File
@@ -1,4 +1,4 @@
HERE = File.expand_path(File.dirname(__FILE__)) + '/'
HERE = __dir__ + '/'
require 'rake'
require 'rake/clean'
@@ -7,36 +7,36 @@ require './rakefile_helper'
include RakefileHelpers
REQUIRED_DIRS = [ './build', './build/mocks' ]
REQUIRED_DIRS = ['./build', './build/mocks'].freeze
REQUIRED_DIRS.each do |v|
directory v
end
# Load default configuration, for now
DEFAULT_CONFIG_FILE = 'gcc.yml'
DEFAULT_CONFIG_FILE = 'gcc.yml'.freeze
configure_toolchain(DEFAULT_CONFIG_FILE)
task :unit do
run_tests(get_unit_test_files)
end
desc "Generate test summary"
desc 'Generate test summary'
task :summary do
report_summary
end
desc "Build and test Unity"
task :all => [:clean, :unit, :summary]
task :default => REQUIRED_DIRS + [:clobber, :all]
desc 'Build and test Unity'
task :all => %i[clean unit summary]
task :default => REQUIRED_DIRS + %i[clobber all]
task :ci => [:default]
task :cruise => [:default]
desc "Load configuration"
task :config, :config_file do |t, args|
desc 'Load configuration'
task :config, :config_file do |_t, args|
configure_toolchain(args[:config_file])
end
desc "Return error on Failures"
desc 'Return error on Failures'
task :strict do
$return_error_on_failures = true
end
+88 -92
View File
@@ -5,10 +5,9 @@ require '../../vendor/unity/auto/generate_test_runner'
require '../../vendor/unity/auto/colour_reporter'
module RakefileHelpers
$return_error_on_failures = false
C_EXTENSION = '.c'
C_EXTENSION = '.c'.freeze
def load_configuration(config_file)
$cfg_file = config_file
@@ -20,7 +19,7 @@ module RakefileHelpers
CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
end
def configure_toolchain(config_file=DEFAULT_CONFIG_FILE)
def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
config_file += '.yml' unless config_file =~ /\.yml$/
load_configuration(config_file)
configure_clean
@@ -34,8 +33,8 @@ module RakefileHelpers
def get_local_include_dirs
include_dirs = $cfg['compiler']['includes']['items'].dup
include_dirs.delete_if {|dir| dir.is_a?(Array)}
return include_dirs
include_dirs.delete_if { |dir| dir.is_a?(Array) }
include_dirs
end
def extract_headers(filename)
@@ -43,118 +42,120 @@ module RakefileHelpers
lines = File.readlines(filename)
lines.each do |line|
m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
if not m.nil?
unless m.nil?
includes << m[1]
end
end
return includes
includes
end
def find_source_file(header, paths)
paths.each do |dir|
src_file = dir + header.ext(C_EXTENSION)
if (File.exists?(src_file))
if File.exist?(src_file)
return src_file
end
end
return nil
nil
end
def tackit(strings)
case(strings)
when Array
"\"#{strings.join}\""
when /^-/
strings
when /\s/
"\"#{strings}\""
else
strings
case strings
when Array
"\"#{strings.join}\""
when /^-/
strings
when /\s/
"\"#{strings}\""
else
strings
end
end
def squash(prefix, items)
result = ''
items.each { |item| result += " #{prefix}#{tackit(item)}" }
return result
result
end
def build_compiler_fields
command = tackit($cfg['compiler']['path'])
if $cfg['compiler']['defines']['items'].nil?
defines = ''
else
defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'])
end
options = squash('', $cfg['compiler']['options'])
command = tackit($cfg['compiler']['path'])
defines = if $cfg['compiler']['defines']['items'].nil?
''
else
squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'])
end
options = squash('', $cfg['compiler']['options'])
includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
return {:command => command, :defines => defines, :options => options, :includes => includes}
{ :command => command, :defines => defines, :options => options, :includes => includes }
end
def compile(file, defines=[])
def compile(file, _defines = [])
compiler = build_compiler_fields
cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " +
cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " \
"#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
execute(cmd_str + obj_file)
return obj_file
obj_file
end
def build_linker_fields
command = tackit($cfg['linker']['path'])
if $cfg['linker']['options'].nil?
options = ''
else
options = squash('', $cfg['linker']['options'])
end
if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?)
includes = ''
else
includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
end
command = tackit($cfg['linker']['path'])
options = if $cfg['linker']['options'].nil?
''
else
squash('', $cfg['linker']['options'])
end
includes = if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?
''
else
squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
end
includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
return {:command => command, :options => options, :includes => includes}
{ :command => command, :options => options, :includes => includes }
end
def link_it(exe_name, obj_list)
linker = build_linker_fields
cmd_str = "#{linker[:command]}#{linker[:includes]} " +
(obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join +
$cfg['linker']['bin_files']['prefix'] + ' ' +
$cfg['linker']['bin_files']['destination'] +
exe_name + $cfg['linker']['bin_files']['extension'] + " #{linker[:options]}"
(obj_list.map { |obj| "#{$cfg['linker']['object_files']['path']}#{obj} " }).join +
$cfg['linker']['bin_files']['prefix'] + ' ' +
$cfg['linker']['bin_files']['destination'] +
exe_name + $cfg['linker']['bin_files']['extension'] + " #{linker[:options]}"
execute(cmd_str)
end
def build_simulator_fields
return nil if $cfg['simulator'].nil?
if $cfg['simulator']['path'].nil?
command = ''
else
command = (tackit($cfg['simulator']['path']) + ' ')
end
if $cfg['simulator']['pre_support'].nil?
pre_support = ''
else
pre_support = squash('', $cfg['simulator']['pre_support'])
end
if $cfg['simulator']['post_support'].nil?
post_support = ''
else
post_support = squash('', $cfg['simulator']['post_support'])
end
return {:command => command, :pre_support => pre_support, :post_support => post_support}
command = if $cfg['simulator']['path'].nil?
''
else
(tackit($cfg['simulator']['path']) + ' ')
end
pre_support = if $cfg['simulator']['pre_support'].nil?
''
else
squash('', $cfg['simulator']['pre_support'])
end
post_support = if $cfg['simulator']['post_support'].nil?
''
else
squash('', $cfg['simulator']['post_support'])
end
{ :command => command, :pre_support => pre_support, :post_support => post_support }
end
def execute(command_string, verbose=true, ok_to_fail=false)
def execute(command_string, verbose = true, ok_to_fail = false)
report command_string
output = `#{command_string}`.chomp
report(output) if (verbose && !output.nil? && (output.length > 0))
report(output) if verbose && !output.nil? && !output.empty?
unless (!$?.nil? && $?.exitstatus.zero?) || ok_to_fail
raise "Command failed. (Returned #{$?.exitstatus})"
end
return output
output
end
def report_summary
@@ -165,11 +166,10 @@ module RakefileHelpers
results = Dir[results_glob]
summary.targets = results
report summary.run
raise "There were failures" if (summary.failures > 0) && $return_error_on_failures
raise 'There were failures' if (summary.failures > 0) && $return_error_on_failures
end
def run_tests(test_files)
report 'Running system tests...'
# Tack on TEST define for compiling unit tests
@@ -185,23 +185,21 @@ module RakefileHelpers
obj_list = []
# Detect dependencies and build required required modules
header_list = (extract_headers(test) + ['cmock.h'] + [ $cfg[:cmock][:unity_helper_path] ]).compact.uniq
header_list = (extract_headers(test) + ['cmock.h'] + [$cfg[:cmock][:unity_helper_path]]).compact.uniq
header_list.each do |header|
# create mocks if needed
next unless header =~ /Mock/
#create mocks if needed
if (header =~ /Mock/)
require "../../lib/cmock.rb"
@cmock ||= CMock.new($cfg_file)
@cmock.setup_mocks([$cfg['compiler']['source_path']+header.gsub('Mock','')])
end
require '../../lib/cmock.rb'
@cmock ||= CMock.new($cfg_file)
@cmock.setup_mocks([$cfg['compiler']['source_path'] + header.gsub('Mock', '')])
end
#compile all mocks
# compile all mocks
header_list.each do |header|
#compile source file header if it exists
# compile source file header if it exists
src_file = find_source_file(header, include_dirs)
if !src_file.nil?
unless src_file.nil?
obj_list << compile(src_file, test_defines)
end
end
@@ -228,25 +226,24 @@ module RakefileHelpers
# Execute unit test and generate results file
simulator = build_simulator_fields
executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
if simulator.nil?
cmd_str = executable
else
cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
end
cmd_str = if simulator.nil?
executable
else
"#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
end
output = execute(cmd_str, true, true)
test_results = $cfg['compiler']['build_path'] + test_base
if output.match(/OK$/m).nil?
test_results += '.testfail'
else
test_results += '.testpass'
end
test_results += if output.match(/OK$/m).nil?
'.testfail'
else
'.testpass'
end
File.open(test_results, 'w') { |f| f.print output }
end
end
def build_application(main)
report "Building application..."
report 'Building application...'
obj_list = []
load_configuration($cfg_file)
@@ -256,7 +253,7 @@ module RakefileHelpers
include_dirs = get_local_include_dirs
extract_headers(main_path).each do |header|
src_file = find_source_file(header, include_dirs)
if !src_file.nil?
unless src_file.nil?
obj_list << compile(src_file)
end
end
@@ -268,5 +265,4 @@ module RakefileHelpers
# Create the executable
link_it(main_base, obj_list)
end
end