Class: Mkduino::MakefileAm

Inherits:
Object
  • Object
show all
Defined in:
lib/mkduino.rb

Overview

Class that keeps up with the stuff needed for the Makefile.am file this is most of the stuff needed to generate the automake stuff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMakefileAm

Returns a new instance of MakefileAm.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mkduino.rb', line 69

def initialize
  @project_dir =  Dir.pwd
  @project_name = File.basename @project_dir
  @project_name.tr!('.','_')
  @source_files = []
  @header_files = []
  @project_includes = []
  @arduino_sources = []
  @arduino_includes = []
  @arduino_libraries = []
  @project_author = {}
  @git_project = nil
  @common_libraries = ['arduino', 'spi','wire']
  @libraries_to_skip = {
    'standard' => ['Esplora','GSM','Robot_Control','Robot_Motor','TFT','robot']
  }
  @board='standard'
  @project_author[:username] = ENV['USERNAME']
  git_exists = `which git`.chomp
  if git_exists &&  git_exists.length > 0
    @project_author[:name] = `git config --get user.name`.chomp
    @project_author[:email] = `git config --get user.email`.chomp
    @git_project = `git remote show -n origin 2> /dev/null | grep 'Fetch URL:' | cut -f 5 -d ' '`.chomp
  else
    @project_author[:name] = @project_author[:username]
    @project_author[:email]= @project_author[:username]
    @git_project = "no_git_project"
  end
end

Instance Attribute Details

#arduino_sourcesObject

Returns the value of attribute arduino_sources.



62
63
64
# File 'lib/mkduino.rb', line 62

def arduino_sources
  @arduino_sources
end

#boardObject

Returns the value of attribute board.



67
68
69
# File 'lib/mkduino.rb', line 67

def board
  @board
end

#common_includesObject

output the Makefile.am macro needed for some include files from libraries that are apparently always needed



140
141
142
# File 'lib/mkduino.rb', line 140

def common_includes
  @common_includes
end

#git_projectObject

future stuff



66
67
68
# File 'lib/mkduino.rb', line 66

def git_project
  @git_project
end

#header_filesObject

Returns the value of attribute header_files.



62
63
64
# File 'lib/mkduino.rb', line 62

def header_files
  @header_files
end

#project_authorObject

Returns the value of attribute project_author.



63
64
65
# File 'lib/mkduino.rb', line 63

def project_author
  @project_author
end

#project_dirObject

Returns the value of attribute project_dir.



63
64
65
# File 'lib/mkduino.rb', line 63

def project_dir
  @project_dir
end

#project_includesObject

Returns the value of attribute project_includes.



64
65
66
# File 'lib/mkduino.rb', line 64

def project_includes
  @project_includes
end

#project_nameObject

Returns the value of attribute project_name.



63
64
65
# File 'lib/mkduino.rb', line 63

def project_name
  @project_name
end

#source_filesObject

Returns the value of attribute source_files.



62
63
64
# File 'lib/mkduino.rb', line 62

def source_files
  @source_files
end

Instance Method Details

#add_arduino_library(library) ⇒ Object

As libraries are found, add them to our collection if they’re not already there



121
122
123
# File 'lib/mkduino.rb', line 121

def add_arduino_library library
  @arduino_libraries << library  if !arduino_library library
end

#add_header_file(file) ⇒ Object



105
106
107
# File 'lib/mkduino.rb', line 105

def add_header_file(file)
  @header_files << "../" + Pathname.new(file).relative_path_from(Pathname.new(@project_dir)).to_s
end

#add_include_path(file) ⇒ Object



109
110
111
112
113
114
# File 'lib/mkduino.rb', line 109

def add_include_path file
  pn = Pathname.new(file)
  puts "!! ******** File #{file} not found ******** " unless pn.exist?
  include_dir = pn.file? ? pn.dirname : file
  @project_includes << include_dir.to_s unless @project_includes.include? include_dir.to_s
end

#add_source_file(file) ⇒ Object

Add a source file that we found in this directory



102
103
104
# File 'lib/mkduino.rb', line 102

def add_source_file(file)
  @source_files << "../" + Pathname.new(file).relative_path_from(Pathname.new(@project_dir)).to_s
end

#arduino_library(library) ⇒ Object

fetch a library from our collection - nil if not there



129
130
131
132
133
134
# File 'lib/mkduino.rb', line 129

def arduino_library library
  @arduino_libraries.each do |l|
    return l if l.name == library
  end
  nil
end

#arduino_library_namesObject

output a list of all the libraries that are needed here for Makefile.am. The project will depend on these



159
160
161
162
163
# File 'lib/mkduino.rb', line 159

def arduino_library_names
  @arduino_libraries.collect do |l|
    l.library_name
  end
end

#arduino_linker_entriesObject

return the linker entries for all of the libraries that we know about



169
170
171
172
173
# File 'lib/mkduino.rb', line 169

def arduino_linker_entries
  @arduino_libraries.collect do |l|
    "-l#{l.linker_name}"
  end
end

#find_arduino_libraries(libraries_dir) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mkduino.rb', line 195

def find_arduino_libraries libraries_dir
  lib = nil
  Find.find(libraries_dir) do |path|
    if FileTest.directory?(path)
      if File.basename(path)[0] == ?. || File.basename(path) == 'examples' ||
          (@libraries_to_skip[@board] && @libraries_to_skip[@board].include?(File.basename(path)) )
        Find.prune       # Don't look any further into this directory.
      else
        if File.dirname(path) == libraries_dir
          lib_name = path.split('/')[-1]
          lib = arduino_library(lib_name) || ArduinoLibrary.new(lib_name)
          add_arduino_library lib
        end
        next
      end
    elsif path =~ source_file_pattern
      lib.add_source_file path
    elsif path =~ header_file_pattern
      lib.add_include_path path
    end
  end
end

#find_source_filesObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/mkduino.rb', line 218

def find_source_files
  #
  # Root around for some source file
  # and add them to the Makefile.am
  #
  Find.find(Dir.pwd) do |path|
    if FileTest.directory?(path)
      if File.basename(path)[0] == ?.
        Find.prune       # Don't look any further into this directory.
      else
        next
      end
    elsif path =~ source_file_pattern
      add_source_file path
    elsif path =~ header_file_pattern
      add_header_file path
      add_include_path path

    end
  end

  #
  # If no source files were found, make
  # the src/ directory and put in a
  # sample main.cpp file
  #
  if source_files.length < 1
    `mkdir src`  unless Dir.exist?('src')
    File.open('src/main.cpp',"w") do |f|
      f.puts "#include <Arduino.h>\n\nextern \"C\" void __cxa_pure_virtual(void) {\nwhile(1);\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println(\"Startup...\");\n}\n\nvoid loop() {\n}\n\n\n\nint main(void)\n{\n  init();\n  setup();\n  for (;;){\nloop();\n  }\n  return 0;\n}\n"
    end
    add_source_file project_dir + '/src/main.cpp'
  end
end

#header_file_patternObject



151
152
153
# File 'lib/mkduino.rb', line 151

def header_file_pattern
  /\.([h])(pp|)$/
end

#output_arduino_librariesObject

after finding all of the Arduino libraries, go through each one of them asking them to output themselves.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mkduino.rb', line 180

def output_arduino_libraries
  output = @arduino_libraries.collect do |l|
    l.makefile_am_output
  end.join("\n")
  #
  # After all of the library compile lines are output, output
  # a comprehensive list of all of the include directories associated
  # with the libraries.   Used for the source project
  #
  output += "\nLIBRARY_INCLUDES="
  output += @arduino_libraries.collect do |l|
    "$(lib#{l.name}_a_INCLUDES)"
  end.join(' ')
end

#source_file_patternObject



147
148
149
# File 'lib/mkduino.rb', line 147

def source_file_pattern
  /\.([c])(pp|)$/
end

#write_makefile_amObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/mkduino.rb', line 282

def write_makefile_am
  puts "Writing Makefile.am"
  File.open('Makefile.am',"w") do |f|
    f.puts "## Process this file with automake to produce Makefile.in\nbin_PROGRAMS=\#{self.project_name}\n# MCU=atmega1280\nMCU=atmega328p\nF_CPU=-DF_CPU=16000000\nARDUINO_VERSION=-DARDUINO=105\nARDUINO_INSTALL=/usr/share/arduino/hardware/arduino\nARDUINO_CORES=$(ARDUINO_INSTALL)/cores/arduino\nARDUINO_VARIANTS=$(ARDUINO_INSTALL)/variants/\#{self.board}\nARDUINO_COMMON_INCLUDES=\#{self.common_includes}\nARDUINO_INCLUDE_PATH=-I$(ARDUINO_VARIANTS) $(LIBRARY_INCLUDES)\nnodist_\#{self.project_name}_SOURCES=\#{self.source_files.join(' ')}\n\n\#{self.project_name}_CFLAGS=-Wall $(\#{self.project_name}_INCLUDES) $(ARDUINO_INCLUDE_PATH) -Wl,--gc-sections -ffunction-sections -fdata-sections -gstabs -mmcu=$(MCU) $(F_CPU) $(ARDUINO_VERSION) -D__AVR_LIBC_DEPRECATED_ENABLE__\n\#{self.project_name}_CXXFLAGS=-Wall $(\#{self.project_name}_INCLUDES) $(ARDUINO_INCLUDE_PATH) -Wl,--gc-sections -ffunction-sections -fdata-sections -gstabs -mmcu=$(MCU) $(F_CPU) $(ARDUINO_VERSION) -D__AVR_LIBC_DEPRECATED_ENABLE__\n\#{self.project_name}_LDFLAGS=-L.\n\#{self.project_name}_LDADD=\#{self.arduino_linker_entries.join(' ')} -lm\n\#{self.project_name}_INCLUDES=-I\#{self.project_includes.join(\"\\\\\\n                    -I\")}\n\nlib_LIBRARIES=\#{self.arduino_library_names.join(' ')}\n\#{self.output_arduino_libraries}\n\n\nAM_LDFLAGS=\nAM_CXXFLAGS=-g0 -Os\nAM_CFLAGS=-g0 -Os\nVPATH=/usr/share/arduino/hardware/arduino/cores/arduino\n\n# AVRDUDE_PORT=/dev/ttyACM0\nAVRDUDE_PORT=/dev/ttyUSB0\nAVRDUDE_PROGRAMMER = arduino\n# UPLOAD_RATE = 115200\nUPLOAD_RATE = 57600\nFORMAT=ihex\n\nAVRDUDE_WRITE_FLASH = -U flash:w:$(bin_PROGRAMS).hex\nAVRDUDE_FLAGS = -q -D -C/etc/avrdude/avrdude.conf -p$(MCU) -P$(AVRDUDE_PORT) -c$(AVRDUDE_PROGRAMMER) -b$(UPLOAD_RATE)\n\n\n.PHONY: upload\nupload: all-am\n  $(OBJCOPY) -S -O $(FORMAT) $(bin_PROGRAMS) $(bin_PROGRAMS).hex\n  $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH)\n"

  end
end