-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build.rogue
638 lines (557 loc) · 19.8 KB
/
Build.rogue
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
# To run this build file, install Rogue from github.com/brombres/Rogue then cd
# to this folder and type "rogo" at the command line, or "rogo help" for a list
# of supported commands.
$requireRogue "2.0"
augment Build
# You can create a Local.settings file with overrides for these values, e.g.:
# BUILD_MODE = release
PROPERTIES
PROJECT = "Froley"
LAUNCHER_NAME = PROJECT.to_lowercase : String
BUILD_MODE = "release"
LAUNCHER_FOLDER = "/usr/local/bin"
ROGUEC_FLAGS = ""
CC_FLAGS_UNIX = ""
CC_FLAGS_WINDOWS = ""
endAugment
routine rogo_default
# Builds and runs this project - equivalent to 'rogo build && rogo run'.
rogo_build
#rogo_install # Uncomment to automatically install a /usr/local/bin/froley launcher
rogo_run
endRoutine
routine rogo_run
# Runs this project.
execute exe_file.esc
endRoutine
routine rogo_debug
# Compiles in debug mode and runs.
rogo_build_debug
rogo_run
endRoutine
routine rogo_release
# Compiles in release mode and runs.
rogo_build_release
rogo_run
endRoutine
routine rogo_clean
# Deletes the Build/ and .rogo/ folders.
File( "Build" ).delete
File( ".rogo" ).delete
endRoutine
routine rogo_build
# Recompiles Rogue -> C -> Exe if needed. 'rogo rebuild' forces a build.
localize exe_file
#if (File("Source/$.froley"(Build.PROJECT)).is_newer_than(exe_file))
# rogo_froley
#endIf
local source_files = Files( "Source", "**/*.rogue" )
if (source_files.any_newer_than(exe_file))
local c_filepath = "Build/$-$.c" (Build.PROJECT,System.os)
if (source_files.any_newer_than(c_filepath))
compile_rogue
endIf
compile_c
endIf
endRoutine
routine rogo_froley
local froley_src = File( "Source/$.froley"(Build.PROJECT) )
execute "froley --main $ Source"(froley_src.esc)
endRoutine
routine rogo_build_debug
# Force-rebuilds in debug mode.
rogo_rebuild_debug
endRoutine
routine rogo_build_release
# Force-rebuilds in release mode.
rogo_rebuild_release
endRoutine
routine rogo_rebuild
# Force-rebuilds project.
#rogo_froley
compile_rogue
compile_c
endRoutine
routine rogo_rebuild_debug
# Force-rebuilds in debug mode.
Build.BUILD_MODE = "debug"
rogo_rebuild
endRoutine
routine rogo_rebuild_release
# Force-rebuilds in release mode.
Build.BUILD_MODE = "release"
rogo_rebuild
endRoutine
routine rogo_install
# Builds and installs the Exe to a system-appropriate location (see also 'rogo link').
rogo_build
local exe_filepath = exe_file.abs.filepath
if (System.is_windows)
local exe_folder = File(exe_filepath).parent.conventional_filepath
local path = System.env//PATH
if (not path.to_lowercase.contains(exe_folder.to_lowercase))
println
println ''Add the following to your system PATH then reopen this command prompt to be able''
println ''to launch $ by typing "$":'' (Build.PROJECT,Build.LAUNCHER_NAME)
println
println " ADD THIS TO YOUR PATH"
println " " + exe_folder
endIf
else
contingent
local result = Process.run( "which " + Build.LAUNCHER_NAME, &env )
necessary (result.success)
local launcher_filepath = result->String.trimmed
necessary launcher_filepath
necessary String( File(launcher_filepath) ).contains( exe_filepath )
unsatisfied
local launcher_filepath = "$/$" (launcher_folder,Build.LAUNCHER_NAME)
println "Creating launcher " + launcher_filepath
local sudo = ""
loop
try
if (not File(launcher_folder).exists) execute( "$mkdir $"(sudo,launcher_folder) )
File( "$.launcher"(Build.LAUNCHER_NAME) ).save( ''#!/bin/sh\nexec "$" "$@"\n''(exe_filepath,'$') )
execute( "$mv $.launcher $"(sudo,Build.LAUNCHER_NAME,launcher_filepath) )
execute( "$chmod a+x $"(sudo,launcher_filepath) )
escapeLoop
catch (err:Error)
if (sudo != "") throw err
sudo = "sudo "
endTry
endLoop
endContingent
endIf
endRoutine
routine rogo_link
# Builds and invokes 'morlock link ...' to add a launcher to Morlock's binpath.
rogo_build
execute "morlock link $ $"(Build.LAUNCHER_NAME,exe_file.esc)
endRoutine
routine rogo_uninstall
# Cleans the project and uninstalls the launcher from 'rogo install'.
rogo_clean
if (not System.is_windows) verbose_delete( "$/$"(launcher_folder,Build.LAUNCHER_NAME) )
endRoutine
routine rogo_update_version( version:String )
# Updates the version number and date in the project source and README.md.
version .= after_any("v")
if (version.count == 0) throw Error( "New version number expected, e.g. '1.0.2'." )
block file = File( "Source/Froley.rogue" )
if (file.exists)
local og_content = String(file)
local content = og_content
content = content.replacing_pattern(
''define VERSION "$"'',
''define VERSION "$"''(version)
)
content = content.replacing_pattern(
''define DATE "$"'',
''define DATE "$"''(Date.today->String(&verbose))
)
if (content != og_content)
println "Updating version and date in $"(file)
file.save( content )
endIf
endIf
endBlock
block file = File( "README.md" )
if (file.exists)
local og_content = String(file)
local content = String( og_content.count+20 )
forEach (line in LineReader(og_content))
if (line.contains_pattern("Version|$(I)"))
content.println( "$| $" (line.before_last('|'),version) )
elseIf (line.contains_pattern("Date|"))
local today = Date.today->String(&verbose)
content.println( "$| $" (line.before_last('|'),today) )
else
content.println( line )
endIf
endForEach
if (content != og_content)
println "Updating version and date in $"(file)
file.save( content )
endIf
endIf
endBlock
endRoutine
routine rogo_commit( new_version:String )
# Updates source and README version number and git-commits with a version number message.
new_version .= after_any("v")
if (not String.exists(new_version)) throw Error( "Expected version number after 'rogo commit'." )
rogo_update_version( new_version )
if (not System.find_executable("git")) throw Error( "Git must be installed." )
if (project_has_uncommitted_changes)
execute ''git commit -am "[v$]"'' (new_version)
else
println "No changes to commit."
endIf
endRoutine
routine project_has_uncommitted_changes->Logical
local result = Process.run( "git status --porcelain", &env )
if (not result.success)
Console.error.println result->String
System.exit 1
endIf
local lines = result->String.trimmed.split('\n').[discard($.begins_with("??"))]
lines.discard( $ == "" )
return not lines.is_empty
endRoutine
routine rogo_publish( new_version:String )
# Updates source and README version number, commits, and publishes a release to GitHub.
new_version .= after_any("v")
if (not String.exists(new_version)) throw Error( "Expected version number after 'rogo publish'." )
if (not System.find_executable("gh")) throw Error( "The GitHub command line tool 'gh' must be installed." )
rogo_commit( new_version )
# These can be hard-coded to suit your project
local cur_branch : String
local main_branch : String
if (not cur_branch)
local result = Process.run("git branch --show-current",&env)
if (result.success) cur_branch = result->String.trimmed
else throw Error( "Unable to detect current git branch." )
endIf
if (not main_branch)
local result = Process.run("git branch --list",&env)
if (result.success)
local branches = result->String.replacing('*',' ').split('\n').[modify($.trimmed)]
forEach (branch in ["main","master"])
if (branches.contains(branch)) main_branch=branch; escapeForEach
endForEach
endIf
if (not main_branch) throw Error( "Unable to detect name of main (release) branch." )
endIf
if (cur_branch != main_branch)
if (not Console.input("$ will be merged into $. Continue? "(cur_branch,main_branch)).to_lowercase.begins_with('y')) return
execute "git push origin $"(cur_branch)
execute "git checkout $"(main_branch)
execute "git pull"
execute "git merge $"(cur_branch)
execute "git push origin $"(main_branch)
execute "git checkout $"(cur_branch)
else
execute "git push origin $"(cur_branch)
endIf
if (not Console.input("Continue publishing release to GitHub? ").to_lowercase.begins_with('y')) return
execute ''gh release create v$ --title "v$" --notes ""''(new_version,new_version)
execute "git pull"
endRoutine
routine compile_rogue
local src_filepath = "Source/$.rogue" (Build.PROJECT)
local c_filepath = "Build/$-$" (Build.PROJECT,System.os)
local flags = Build.ROGUEC_FLAGS
if (Build.BUILD_MODE == "debug") flags = flags.with_suffix(' ') + "--debug"
local cmd = "roguec $ $ --main --output=$ --target=Console,C,$"...
(File(src_filepath).esc,flags,File(c_filepath).esc,System.os)
println "> " + cmd
local status = Process.run( cmd, &env )
local result = status->String
println result
if (not status.success) throw Error( "Build failed." )
if (result.contains("ROGO:"))
System.exit( execute("rogo".appending(System.command_line_arguments.join(' '))) )
endIf
endRoutine
routine compile_c
local c_filepath = File( "Build/$-$" (Build.PROJECT,System.os) ).esc
local exe_filepath = exe_file.esc
if (System.is_windows)
local flags = Build.CC_FLAGS_WINDOWS
if (flags != "") flags += " "
flags += which{ Build.BUILD_MODE=="release":"/Ob2ity /GF /Gy" || "/Od" }
local obj_filepath = File( "Build/$-Windows.obj"(Build.PROJECT) ).esc
local cc = "cl $ /EHsc /nologo $.c /Fo$ /Fe$" (flags,c_filepath,obj_filepath,exe_filepath)
cc .= appending( "$LIBRARY_FLAGS" )
execute cc
else
local flags = Build.CC_FLAGS_UNIX
if (flags != "") flags += " "
flags += which{ Build.BUILD_MODE=="release":"-O3" || "-O0" }
local cc = "cc $ -Wall -fno-strict-aliasing $.c -o $" (flags,File(c_filepath).esc,exe_file.esc)
cc .= appending( "$LIBRARY_FLAGS" )
execute cc
endIf
endRoutine
routine exe_file->File
if (System.is_windows) return File( "Build/$.exe"(Build.LAUNCHER_NAME) )
return File( "Build/$-$"(Build.PROJECT,System.os) )
endRoutine
routine launcher_folder->String
local result = Build.LAUNCHER_FOLDER
result = File(result).resolved.filepath
if (result.count > 1) result .= without_suffix('/')
return result
endRoutine
routine verbose_delete( filepath:String )
if (File(filepath).exists)
println "> Delete " + filepath
if (not File(filepath).delete)
println "*** Failed to delete - retrying with sudo"
local cmd = ''sudo rm -rf $'' (File(filepath).esc)
execute cmd
endIf
endIf
endRoutine
routine execute( commands:String, error_message=null:String, &suppress_error )->Logical
forEach (cmd in LineReader(commands))
print( "> " )
println( cmd )
if (0 != System.run(cmd))
if (suppress_error)
return false
else
if (not error_message) error_message = "Build failed."
throw Error( error_message )
endIf
endIf
endForEach
return true
endRoutine
#-------------------------------------------------------------------------------
# Introspection-based Launcher Framework
#-------------------------------------------------------------------------------
# Rogo is a "build your own build system" facilitator. At its core Rogo just
# recompiles build files if needed and then runs the build executable while
# forwarding any command line arguments. This file contains a default framework
# which uses introspection to turn command line arguments into parameterized
# routine calls.
# Example: to handle the command "rogo abc xyz 5", define
# "routine rogo_abc_xyz( n:Int32 )".
# "rogo_default" will run in the absence of any other command line argument.
# The following "comment directives" can be used in this file to control how
# RogueC compiles it and to manage automatic dependency installation and
# linking.
# Each of the following should be on a line beginning with the characters #$
# (preceding whitespace is fine). Sample args are given.
# ROGUEC = roguec # Path to roguec to compile this file with
# ROGUEC_ARGS = --whatever # Additional options to pass to RogueC
# CC = gcc
# CC_ARGS = -a -b -c # Additional C args
# CC_LINK = -lalpha -lbeta # Link this build file with these options
# CC_LINK(macOS) = ... # Options applying only to
# # System.os=="macOS" (use with any OS and
# # any comment directive)
# LINK_LIBS = true # Links following LIBRARIES with this Build
# # file (otherwise just installs them)
# LINK_LIBS = false # Linking turned off for following
# # LIBRARIES - info can still be obtained
# # from $LIBRARY_FLAGS or $LIBRARIES(libname,...)
# LIBRARIES = libalpha
# LIBRARIES = libbeta(library-name)
# LIBRARIES = libfreetype6-dev(freetype2)
# DEPENDENCIES = Library/Rogue/**/*.rogue
#
# LIBRARIES = name(package)
# LIBRARIES = name(package:<package> install:<install-cmd>
# link:<link-flags> which:<which-name>)
#
# The following macro is replaced within this file (Build.rogue) - the libraries
# should normally also be declared in #$ LIBRARIES:
#
# $LIBRARY_FLAGS(lib1,lib2) # sample macro
# ->
# -Ipath/to/lib1/include -Lpath/to/lib1/library -I ... # sample replacement
routine syntax( command:String, text:String )
Build.rogo_syntax[ command ] = text
endRoutine
routine description( command:String, text:String )
Build.rogo_descriptions[ command ] = text
endRoutine
routine help( command:String, description_text=null:String, syntax_text=null:String )
if (description_text) description( command, description_text )
if (syntax_text) syntax( command, syntax_text )
endRoutine
try
Build.launch
catch (err:Error)
Build.rogo_error = err
Build.on_error
endTry
class Build [singleton]
PROPERTIES
rogo_syntax = [String:String]
rogo_descriptions = [String:String]
rogo_prefix = "rogo_"
rogo_command = "default"
rogo_args = @[]
rogo_error : Error
LOCAL_SETTINGS_FILE = "Local.settings"
METHODS
method launch
rogo_args.add( forEach in System.command_line_arguments )
read_defs
on_launch
parse_args
dispatch_command
method dispatch_command
local m = find_command( rogo_command )
if (not m) throw Error( "No such routine rogo_$()" (rogo_command) )
local args = @[]
forEach (arg in rogo_args)
which (arg)
case "true": args.add( true )
case "false": args.add( false )
case "null": args.add( null )
others: args.add( arg )
endWhich
endForEach
m( args )
method find_command( name:String )->MethodInfo
return <<Routine>>.find_global_method( rogo_prefix + name )
method on_error
local w = Console.width.or_smaller( 80 )
Console.error.println "=" * w
Console.error.println rogo_error
Console.error.println "=" * w
on_exit
System.exit 1
method on_command_found
noAction
method on_command_not_found
local w = Console.width.or_smaller( 80 )
println "=" * w
println "ERROR: No such command '$'." (rogo_args.first)
println "=" * w
println
rogo_command = "help"
rogo_args.clear
on_command_found
method on_launch
noAction
method on_exit
noAction
method parse_args
block
if (rogo_args.count)
local parts = String[]
parts.add( forEach in rogo_args )
rogo_args.clear
while (parts.count)
local cmd = parts.join("_")
if (find_command(cmd))
rogo_command = cmd
on_command_found
escapeBlock
endIf
rogo_args.insert( parts.remove_last )
endWhile
on_command_not_found
endIf
# Use default command
on_command_found
endBlock
method read_defs
read_defs( LOCAL_SETTINGS_FILE )
method read_defs( defs_filepath:String )
# Attempt to read defs from Local.settings
local overrides = String[]
if (File(defs_filepath).exists)
forEach (line in LineReader(File(defs_filepath)))
if (line.contains("="))
local name = line.before_first('=').trimmed
local value = line.after_first('=').trimmed
if (value.begins_with('"') or value.begins_with('\''))
value = value.leftmost(-1).rightmost(-1)
endIf
local p = <<Build>>.find_property( name )
if (p)
overrides.add( "$ = $" (name,value) )
p.set_value( this, value )
endIf
endIf
endForEach
endIf
method _join( value:Variant )->String
local args = String[]
args.add( forEach in value )
return args.join( "_" )
endClass
routine rogo_help( command="":String )
# SYNTAX: rogo help [command]
# Displays help for a specified command or else all build commands.
command = Build._join( Build.rogo_args )
if (command.count)
local syntax = get_syntax( command )
local success = false
if (syntax)
println "SYNTAX"
println " " + syntax
println
success = true
endIf
local description = get_description( command )
if (description)
local max_w = Console.width - 2
println "DESCRIPTION"
forEach (line in LineReader(description.word_wrapped(max_w)))
print( " " )
println( line )
endForEach
println
success = true
endIf
if (success)
return
else
local w = Console.width.or_smaller( 80 )
println "=" * w
println "ERROR: No such command '$'." (command)
println "=" * w
println
endIf
endIf
println "USAGE"
local entries = CommandInfo[]
local max_len = 0
forEach (m in <<Routine>>.global_methods)
if (m.name.begins_with(Build.rogo_prefix))
local name = m.name.after_first( Build.rogo_prefix )
local entry = CommandInfo( name, get_syntax(name), get_description(name) )
max_len .= or_larger( entry.syntax.count )
entries.add entry
endIf
endForEach
entries.sort( $1.name < $2.name )
max_len += 2
local max_w = Console.width
forEach (entry in entries)
print " " + entry.syntax
if (entry.@description)
local description = entry.@description
loop (max_len - entry.syntax.count) print ' '
contingent
sufficient (max_len + description.count <= max_w)
if (description.contains(". "))
description = description.before_first( ". " ) + "."
sufficient (max_len + description.count <= max_w)
endIf
necessary (max_len + 10 <= max_w)
description = description.unright( (description.count - (max_w - max_len))+3 ) + "..."
satisfied
print description
endContingent
endIf
println
endForEach
println
endRoutine
routine get_syntax( m_name:String )->String
if (Build.rogo_syntax.contains(m_name))
return "rogo " + Build.rogo_syntax[ m_name ]
else
local m = <<Routine>>.find_global_method( Build.rogo_prefix + m_name )
if (not m) return null
local line = "rogo $" (m_name.replacing('_',' '))
line += " <$>" ((forEach in m.parameters).name)
return line
endIf
endRoutine
routine get_description( m_name:String )->String
if (Build.rogo_descriptions.contains(m_name))
return Build.rogo_descriptions[ m_name ]
else
return null
endIf
endRoutine
class CommandInfo( name:String, syntax:String, description:String );