Class: Mkduino::MakefileAm

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMakefileAm

Returns a new instance of MakefileAm.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/makefile_am.rb', line 15

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.



8
9
10
# File 'lib/makefile_am.rb', line 8

def arduino_sources
  @arduino_sources
end

#boardObject

Returns the value of attribute board.



13
14
15
# File 'lib/makefile_am.rb', line 13

def board
  @board
end

#common_includesObject

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



86
87
88
# File 'lib/makefile_am.rb', line 86

def common_includes
  @common_includes
end

#git_projectObject

future stuff



12
13
14
# File 'lib/makefile_am.rb', line 12

def git_project
  @git_project
end

#header_filesObject

Returns the value of attribute header_files.



8
9
10
# File 'lib/makefile_am.rb', line 8

def header_files
  @header_files
end

#project_authorObject

Returns the value of attribute project_author.



9
10
11
# File 'lib/makefile_am.rb', line 9

def project_author
  @project_author
end

#project_dirObject

Returns the value of attribute project_dir.



9
10
11
# File 'lib/makefile_am.rb', line 9

def project_dir
  @project_dir
end

#project_includesObject

Returns the value of attribute project_includes.



10
11
12
# File 'lib/makefile_am.rb', line 10

def project_includes
  @project_includes
end

#project_nameObject

Returns the value of attribute project_name.



9
10
11
# File 'lib/makefile_am.rb', line 9

def project_name
  @project_name
end

#source_filesObject

Returns the value of attribute source_files.



8
9
10
# File 'lib/makefile_am.rb', line 8

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



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

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

#add_header_file(file) ⇒ Object



51
52
53
# File 'lib/makefile_am.rb', line 51

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



55
56
57
58
59
60
# File 'lib/makefile_am.rb', line 55

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



48
49
50
# File 'lib/makefile_am.rb', line 48

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



75
76
77
78
79
80
# File 'lib/makefile_am.rb', line 75

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



105
106
107
108
109
# File 'lib/makefile_am.rb', line 105

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



115
116
117
118
119
# File 'lib/makefile_am.rb', line 115

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

#find_arduino_libraries(libraries_dir) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/makefile_am.rb', line 141

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/makefile_am.rb', line 164

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



97
98
99
# File 'lib/makefile_am.rb', line 97

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/makefile_am.rb', line 126

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



93
94
95
# File 'lib/makefile_am.rb', line 93

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

#write_makefile_amObject



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
278
# File 'lib/makefile_am.rb', line 228

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