-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibbuildtool
More file actions
executable file
·322 lines (260 loc) · 9.76 KB
/
libbuildtool
File metadata and controls
executable file
·322 lines (260 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
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
278
279
280
281
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
#!/usr/bin/env ruby
# encoding: UTF-8
require 'optparse'
require 'ostruct'
require 'json'
require 'fileutils'
# Module used to namespace internals.
#
# Stuff that is internal to libbuildtool is added to this module
# to make the documentation cleaner.
module LBT
end
require_relative 'classes/arraystruct.rb'
require_relative 'classes/exec'
require_relative 'classes/library'
require_relative 'classes/licenses'
require_relative 'classes/os'
require_relative 'classes/platforms'
require_relative 'classes/step.rb'
require_relative 'classes/stepsfabricator.rb'
require_relative 'classes/steps.rb'
require_relative 'steps/builder'
require_relative 'steps/fetcher'
require_relative 'steps/installer'
require_relative 'steps/patcher'
require_relative 'steps/unpacker'
require_relative 'steps/verifier'
# Adding the StepMaker to the global scope
# This should add only make_step
include LBT::StepMaker
# Include the Steps namespace into the global scope.
include Steps
# Shows a fancy ASCII-art banner
# @return [void]
def show_banner
return unless $libbuildtool_params.show_banner
puts <<BANNER
.__ ._____. ___. .__.__ .___ __ .__
| | |__\\_ |__\\_ |__ __ __|__| | __| _// |_ ____ ____ | |
| | | || __ \\| __ \\| | \\ | | / __ |\\ __\\/ _ \\ / _ \\| |
| |_| || \\_\\ \\ \\_\\ \\ | / | |__/ /_/ | | | ( <_> | <_> ) |__
|____/__||___ /___ /____/|__|____/\\____ | |__| \\____/ \\____/|____/
\\/ \\/ \\/
BANNER
end
# Prints text centered in a banner
#
# @param text Text to print
# @param col Width of the banner
# @param char Character to use for the banner
#
# @return [void]
def puts_banner text, col = 40, char = '*'
puts ''.rjust(col, char)
puts "*#{text.center(col -2 )}*"
puts ''.rjust(col, char)
end
# FIXME : Properly handle from a base Platform class. Still allow those shortcuts.
# Shortcut for the Platform Name
$PLATFORM = "unknown"
# Shortcut for the Platform Hardware
$PLATFORM_HW = "unknown"
# FIXME : Move into the base Platform class.
# Discover the current platform
# Firt, try windows without MinGW stuff.
if not ENV['MSYSTEM'] and ENV['OS'] and ENV['OS'].match(/Windows.*/) then
$PLATFORM = ENV['OS'].strip
# For now, hardcode 32bits bitness
$PLATFORM_HW = "win32".strip
# Else, we assume uname availability.
# It should be quite a sure bet right now.
else
$PLATFORM = "#{`uname -s`}".strip
$PLATFORM_HW = "#{`uname -m`}".strip
end
#######################################
### Options and parameters handling ###
#######################################
# Default name of the libraries main file.
DEFAULT_LIBRARIES_LIST = 'libraries.rb'
$libbuildtool_params = OpenStruct.new(
:show_banner => true,
:libraries_list => nil,
:rebuild => false,
:install_dir => "#{Dir.pwd}/#{$PLATFORM}/#{$PLATFORM_HW}",
:work_dir => "#{Dir.pwd}/workdir",
:platform => nil,
:project_dir => Dir.pwd
)
@optionParser = OptionParser.new do |opts|
opts.banner = "Usage : #{File.basename $PROGRAM_NAME} [OPTION]"
opts.separator 'Execute the build libraries listed by the given libraries list.'
opts.separator ''
opts.separator 'Global options'
opts.on('-h', '--help', 'Shows this help.') do
show_banner
puts opts
if $libbuildtool_params.platform.respond_to? 'list_options'
$libbuildtool_params.platform.list_options
end
exit
end
opts.on('-r', '--rebuild', 'Rebuild all the lib instead of building the remaining libs.') do
$libbuildtool_params.rebuild = true
end
opts.on('--no-banner', 'Do not output the ASCII art banner.') do
$libbuildtool_params.show_banner = false
end
opts.on('-l', '--libraries-list', '=FILENAME', 'Libraries list to use.', "Default: #{DEFAULT_LIBRARIES_LIST}") do |file|
$libbuildtool_params.libraries_list = file
end
opts.on('-i', '--install-dir', '=DIR', 'Final output directory.', "Default: #{$libbuildtool_params.install_dir}") do |dir|
$libbuildtool_params.install_dir = dir
end
opts.on('--project-dir', '=DIR', 'Project directory.', "Default: #{$libbuildtool_params.project_dir}") do |dir|
$libbuildtool_params.project_dir = dir
end
opts.on('-w', '--work-dir', '=DIR', 'Temp work directory.', "Default: #{$libbuildtool_params.work_dir}") do |dir|
$libbuildtool_params.work_dir = dir
end
opts.separator ''
opts.separator 'Platform options'
opts.on('-P', '--list-platforms', 'Prints a list of available platforms.') do
puts 'List of available platforms:'
Platforms.list_classes.each do |g|
puts " #{g}"
end
exit 0
end
opts.on('-p', '--platform', '=PLATFORM', 'Name of the platform file to use.') do |platform|
$libbuildtool_params.platform = Platforms.get_platform platform
$libbuildtool_params.platform = $libbuildtool_params.platform.new
$libbuildtool_params.platform_name = platform
# FIXME : Re-query from platform; it might be different than the passed param... (eg: meta platforms)
$PLATFORM = platform
# FIXME : Platform class should have a way to query this.
$PLATFORM_HW = nil
end
opts.on('--list-platform-options', 'Lists the options of the platform.') do
unless $libbuildtool_params.platform
raise 'The platform has to be selected before listing its options.'
end
unless $libbuildtool_params.platform.respond_to? 'list_options'
raise 'The platform has no way to list its options.'
end
$libbuildtool_params.platform.list_options
exit 0
end
end
@optionParser.permute!
$libbuildtool_params.platform.permute! if $libbuildtool_params.platform
$libbuildtool_params.platform.validate! if $libbuildtool_params.platform
###############################
### Global state resolution ###
###############################
$global_state = OpenStruct.new()
$global_state.work_dir = $libbuildtool_params.work_dir
$global_state.build_cache_path = "#{$global_state.work_dir}/built.json"
$global_state.source_dir = "#{$global_state.work_dir}/src"
$global_state.build_dir = "#{$global_state.work_dir}/build"
$global_state.project_dir = $libbuildtool_params.project_dir
$global_state.libraries_list = $libbuildtool_params.libraries_list
$global_state.libraries_list = "#{$global_state.project_dir}/#{DEFAULT_LIBRARIES_LIST}" if $global_state.libraries_list.nil?
show_banner
$build_options = nil
if defined? $libbuildtool_params.platform.build_options and not $libbuildtool_params.platform.build_options.nil?
$build_options = $libbuildtool_params.platform.build_options
else
$build_options = ArrayStruct.new()
end
$build_options.install_dir = $libbuildtool_params.install_dir
#############################
### Directory Preparation ###
#############################
@build_cache = []
if Dir.exist? $global_state.work_dir
if $libbuildtool_params.rebuild
FileUtils.rm_rf $global_state.build_dir
else
@build_cache = JSON.parse(File.read($global_state.build_cache_path)) if File.exist? $global_state.build_cache_path
end
end
FileUtils.rm_rf $global_state.build_dir if Dir.exist? $global_state.build_dir
FileUtils.mkdir_p $libbuildtool_params.install_dir
FileUtils.mkdir_p $global_state.work_dir
FileUtils.mkdir_p $global_state.source_dir
FileUtils.mkdir_p $global_state.build_dir
#################################
### Libraries build iteration ###
#################################
# Builds a library from its description's filename
#
# @param desc_filename File to build.
# @return [void]
def build desc_filename
unless @build_cache.include? desc_filename
# FIXME : Scoping feels weird here. Find a better way to handle library, build_options scope.
begin
Dir.chdir $global_state.project_dir
# Used for the backtrace.
fname = "#{Dir.pwd}/#{desc_filename}"
# Holder for the library configuration.
# Pre-scope library
library = nil
begin
eval(File.open(desc_filename).read, nil, fname)
rescue Exception => e
puts "A problem occured while parsing/running #{desc_filename}..."
puts e
puts e.backtrace
abort
end
library.work_dir = "#{$global_state.build_dir}/#{library.name.downcase.tr(' ', '_')}"
FileUtils.rm_rf library.work_dir if Dir.exist? library.work_dir
FileUtils.mkdir_p library.work_dir
puts
puts_banner "#{library.name}"
# Here we're handling each steps registered.
steps_counter = 0
library.steps.each do |step|
steps_counter += 1
puts "\n→ Doing step (#{steps_counter}/#{library.steps.count}) #{step[:name].capitalize}"
STDOUT.flush
# Reset the directory for each step.
Dir.chdir $global_state.project_dir
# This is where the step is actually called.
unless step[:instance].run then
throw "Step (#{steps_counter}/#{library.steps.count}) #{step[:name].capitalize} failed."
end
end
@build_cache << desc_filename
File.open($global_state.build_cache_path, "w").write(JSON.pretty_generate(@build_cache))
rescue Exception => e
puts "A problem occured while building #{desc_filename}. Last working directory: #{Dir.pwd}"
puts e
puts e.backtrace
abort
end
end
end
#parsing libraries.desc
begin
eval(File.open($global_state.libraries_list).read,nil,$global_state.libraries_list)
puts
puts_banner 'Done!'
rescue Exception => e
puts "+--------------------------------------------------------+"
puts "| Something happened. |"
puts "+--------------------------------------------------------+"
puts "| Crashed while reading or executing the libraries list. |"
puts "| Read and copy the output before and after this text |"
puts "| when reporting issues. |"
puts "+--------------------------------------------------------+"
puts "| Ruby backtrace: |"
puts "+--------------------------------------------------------+"
puts e.backtrace
puts e
puts "----------------------------------------------------------"
exit 1
end