forked from libbitcoin/libbitcoin-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.gsl
666 lines (583 loc) · 22.2 KB
/
utilities.gsl
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
.template 0
###############################################################################
# Copyright (c) 2014-2015 libbitcoin developers (see COPYING).
#
# GSL libbitcoin utilities in the 'global' scope.
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/imatix/gsl for details.
###############################################################################
# Functions
###############################################################################
#------------------------------------------------------------------------------
# Node utilities.
#------------------------------------------------------------------------------
function global.match_name(element, name)
define my.element = match_name.element
require(my.element, "element", "name")
return my.element.name = my.name
endfunction
function global.is_optional_element(element)
define my.element = is_optional_element.element
return defined(my.element.option)
endfunction
function global.is_private_element(element)
define my.element = is_private_element.element
return is_true(my.element.private)
endfunction
#------------------------------------------------------------------------------
# Collection utilities.
#------------------------------------------------------------------------------
function global.set_cursor_begin(element)
define my.element = set_cursor_begin.element
for my.element as _element
new cursor as _cursor
move _cursor to _element
return _cursor
endnew
endfor
return set_cursor_end(my.element)
endfunction
# We insert before a dummy element (cursor) at the end of the element.
# This provides a simple workaround for lack of reverse iteration in GSL,
# allowing order preservation and insertion at the end of the list.
# Fortunately extending a list while iterating it does not alter the iteration.
function global.set_cursor_end(element)
define my.element = set_cursor_end.element
new cursor as _cursor
move _cursor to my.element
return _cursor
endnew
endfunction
function global.clear_cursor(cursor)
define my.cursor = clear_cursor.cursor
delete my.cursor
endfunction
#------------------------------------------------------------------------------
# I/O utilities.
#------------------------------------------------------------------------------
# Open and validate a directory.
function global.create_directory(folder)
define my.result = directory.create(my.folder)
if (my.result = 0)
return
endif
abort "Directory creation failure: $(error_text)"
endfunction
# Open and validate a directory.
function global.open_directory(folder)
define my.directory = directory.open(my.folder, error_text)?
if (defined(my.directory))
return my.directory
endif
abort "Directory open failure: $(error_text)"
endfunction
# Test a directory and return the normalized path.
function global.normalize_directory(folder)
define my.directory = open_directory(my.folder)
return my.directory.path
endfunction
#------------------------------------------------------------------------------
# Library utilities.
#------------------------------------------------------------------------------
# Get the product with the given library name.
function global.find_library_product(name, make)
define my.make = find_library_product.make
return my.make->product(is_library(product) &\
match_name(product, my.name))
endfunction
# Accepts [lib]bitcoin[-include] and returns include.
function global.bitcoin_to_include(library)
define my.prefix = "bitcoin"
define my.libprefix = "lib$(my.prefix)"
if (my.library = my.prefix) | (my.library = my.libprefix)
return my.prefix
endif
my.prefix += "-"
if (starts_with(my.library, my.prefix))
return difference(my.library, string.length(my.prefix))
endif
my.libprefix += "-"
if (starts_with(my.library, my.libprefix))
return difference(my.library, string.length(my.libprefix))
endif
abort "The name is not a bitcoin library: $(my.library)"
endfunction
# Accepts include and returns bitcoin[-include].
function global.include_to_bitcoin(include)
define my.prefix = "bitcoin"
if (my.include = my.prefix)
return my.include
endif
return "$(my.prefix)-$(my.include)"
endfunction
#------------------------------------------------------------------------------
# Dependency utilities.
#------------------------------------------------------------------------------
# Get the dependency with the given name.
function global.find_dependency_by_name(name, configure)
define my.configure = find_dependency_by_name.configure
return my.configure->dependency(match_name(dependency, my.name))
endfunction
function global.is_function_dependency(dependency)
define my.dependency = is_function_dependency.dependency
return defined(my.dependency.function)
endfunction
function global.is_package_dependency(dependency)
define my.dependency = is_package_dependency.dependency
return defined(my.dependency.version) &\
!is_boost_dependency(my.dependency) &\
!is_boost_lib_dependency(my.dependency) &\
!is_python_dependency(my.dependency)
endfunction
function global.is_boost_dependency(dependency)
define my.dependency = is_boost_dependency.dependency
return match_name(my.dependency, "boost")
endfunction
function global.is_boost_lib_dependency(dependency)
define my.dependency = is_boost_lib_dependency.dependency
require(my.dependency, "dependency", "name")
return starts_with(my.dependency.name, "boost_")
endfunction
function global.is_java_dependency(dependency)
define my.dependency = is_java_dependency.dependency
return match_name(my.dependency, "java")
endfunction
function global.is_python_dependency(dependency)
define my.dependency = is_python_dependency.dependency
return match_name(my.dependency, "python")
endfunction
function global.is_pthread_dependency(dependency)
define my.dependency = is_pthread_dependency.dependency
return match_name(my.dependency, "pthread")
endfunction
function global.is_iconv_dependency(dependency)
define my.dependency = is_iconv_dependency.dependency
return match_name(my.dependency, "iconv")
endfunction
function global.is_bitcoin_dependency(dependency)
define my.dependency = is_bitcoin_dependency.dependency
require(my.dependency, "dependency", "name")
return starts_with(my.dependency.name, "bitcoin")
endfunction
# Test for a flag context.
function global.match_flag_context(flag, context)
define my.flag = match_flag_context.flag?
return (my.flag.context ? "") = my.context
endfunction
# Test flag context value.
function global.is_ccc_flag(flag)
define my.flag = is_ccc_flag.flag?
return match_flag_context(my.flag, "c")
endfunction
function global.is_cxx_flag(flag)
define my.flag = is_cxx_flag.flag?
return match_flag_context(my.flag, "c++")
endfunction
function global.is_cpp_flag(flag)
define my.flag = is_cpp_flag.flag?
return match_flag_context(my.flag, "") |\
match_flag_context(my.flag, "preprocessor")
endfunction
function global.is_link_flag(flag)
define my.flag = is_link_flag.flag?
return match_flag_context(my.flag, "link")
endfunction
# Return CPPFLAGS|CFLAGS|CXXFLAGS|LDFLAGS
function get_flags_variable(flag)
define my.flag = get_flags_variable.flag
if (is_cpp_flag(my.flag))
return "CPPFLAGS"
elsif (is_ccc_flag(my.flag))
return "CFLAGS"
elsif (is_cxx_flag(my.flag))
return "CXXFLAGS"
elsif (is_link_flag(my.flag))
return "LDFLAGS"
endif
define my.context = my.flag.context ? ""
abort "Invalid flag context: '$(my.context)'."
endfunction
#------------------------------------------------------------------------------
# Option utilities.
#------------------------------------------------------------------------------
function global.get_option_symbol(option)
define my.option = get_option_symbol.option
if (defined(my.option))
require(my.option, "option", "type")
require(my.option, "option", "name")
define my.prefix = ""
if (!is_true(my.option.unprefixed))
my.prefix = "$(my.option.type:c)_"
endif
return "$(my.prefix)$(my.option.name:c)"
endif
endfunction
function global.find_option_symbol(element, configure)
define my.element = find_option_symbol.element
define my.configure = find_option_symbol.configure
if (is_optional_element(my.element))
define my.opt = my.configure->option(option.name = my.element.option)?
if (defined(my.opt))
return get_option_symbol(my.opt)
endif
abort "Symbol not defined for option: $(my.element.option)"
endif
endfunction
#------------------------------------------------------------------------------
# Matrix utilities.
#------------------------------------------------------------------------------
function global.is_coverage_job(job)
define my.job = is_coverage_job.job
return defined(my.job) & my.job.system = "linux" & my.job.compiler = "gcc" &\
my.job.link = "static" & is_true(my.job.coverage)
endfunction
function global.count_matrix_coverage_job(matrix)
define my.matrix = count_matrix_coverage_job.matrix?
if (defined(my.matrix))
return count(my.matrix->job(system = "linux" & compiler = "gcc" &\
link = "static" & is_true(coverage)))
endif
endfunction
#------------------------------------------------------------------------------
# Job utilities.
#------------------------------------------------------------------------------
# Get the value part of a standard form (e.g. "--prefix=value") job option.
function global.job_option(job, option)
define my.job = job_option.job
define my.assigner = "--$(my.option)="
define my.option = my.job->option(starts_with(option.value, my.assigner))?
if defined(my.option)
return difference(my.option.value, string.length(my.assigner))
endif
endfunction
#------------------------------------------------------------------------------
# Build utilities.
#------------------------------------------------------------------------------
function global.is_icu_build(build)
define my.build = is_icu_build.build
return starts_with(my.build.name, "icu")
endfunction
function global.is_zlib_build(build)
define my.build = is_zlib_build.build
return starts_with(my.build.name, "zlib")
endfunction
function global.is_png_build(build)
define my.build = is_png_build.build
return starts_with(my.build.name, "png")
endfunction
function global.is_qrencode_build(build)
define my.build = is_qrencode_build.build
return starts_with(my.build.name, "qrencode")
endfunction
function global.is_boost_build(build)
define my.build = is_boost_build.build
return my.build.name = "boost"
endfunction
function global.is_github_build(build)
define my.build = is_github_build.build
return !is_empty(my.build.github)
endfunction
#------------------------------------------------------------------------------
# Product utilities.
#------------------------------------------------------------------------------
function global.match_prefix(product, prefix)
define my.product = match_prefix.product
require(my.product, "product", "prefix")
return my.product.prefix = my.prefix
endfunction
# Test prefix for primary type (this is a simplified mapping).
function global.is_data(product)
define my.product = is_data.product
return match_prefix(my.product, "bash_completion") |\
match_prefix(my.product, "doc") |\
match_prefix(my.product, "pkgconfig") |\
match_prefix(my.product, "jarexec") |\
match_prefix(my.product, "sysconf")
endfunction
function global.is_headers(product)
define my.product = is_headers.product
return match_prefix(my.product, "include")
endfunction
function global.is_info(product)
define my.product = is_info.product
return match_prefix(my.product, "info")
endfunction
function global.is_java(product)
define my.product = is_java.product
return match_prefix(my.product, "javaexec")
endfunction
function global.is_jar(product)
define my.product = is_jar.product
# jar uses DATA but we wire it up as a product.
return match_prefix(my.product, "jarexec")
endfunction
function global.is_library(product)
define my.product = is_library.product
return match_prefix(my.product, "lib") |\
match_prefix(my.product, "pkgpyexec")
endfunction
function global.is_manual(product)
define my.product = is_manual.product
return match_prefix(my.product, "man")
endfunction
function global.is_program(product)
define my.product = is_program.product
return match_prefix(my.product, "bin")
endfunction
function global.is_python(product)
define my.product = is_python.product
return match_prefix(my.product, "pkgpython")
endfunction
function global.is_script(product)
define my.product = is_script.product
return match_prefix(my.product, "sbin")
endfunction
function global.is_bitcoin_headers(product)
define my.product = is_bitcoin_headers.product
return is_headers(my.product) & ((my.product.container ? "") = "bitcoin")
endfunction
# Is the product allowed to be active only once?
function global.is_singleton(product)
define my.product = is_singleton.product
return is_java(my.product) | is_jar(my.product)
endfunction
# Does the product assign to the CLEANFILES variable.
function global.is_clean_files(product)
define my.product = is_clean_files.product
return is_java(my.product) | is_jar(my.product)
endfunction
# Is the product built from c/c++?
function global.is_cpp(product)
define my.product = is_cpp.product
return is_program(my.product) | is_library(my.product)
endfunction
# Is the product executable?
function global.is_executable(product)
define my.product = is_executable.product
return is_program(my.product) | is_script(my.product)
endfunction
# Is the product compiled?
function global.is_compiled(product)
define my.product = is_compiled.product
return is_cpp(my.product) | is_java(my.product)
endfunction
# Is the non-compiled product explicitly set to distribute?
function global.is_distribute(product)
define my.product = is_distribute.product
return defined(my.product.distribute) & is_true(my.product.distribute)
endfunction
# Is the non-compiled product explicitly set to not distribute?
function global.is_no_distribute(product)
define my.product = is_no_distribute.product
return defined(my.product.distribute) & !is_true(my.product.distribute)
endfunction
# Is the product explicitly set to not install?
function global.is_no_install(product)
define my.product = is_no_install.product
return defined(my.product.install) & !is_true(my.product.install)
endfunction
# Is the product a test executable?
function global.is_test(product)
define my.product = is_test.product
if (!is_true(my.product.test) | is_executable(my.product))
return is_true(my.product.test)
endif
abort "Tests must be executable: '$(my.product.prefix)'"
endfunction
# Is the product a check target.
function global.is_not_covered(product)
define my.product = is_not_covered.product
return (is_test(my.product) | is_no_install(my.product)) &\
is_cpp(my.product)
endfunction
# Is the product a check target.
function global.is_check(product)
define my.product = is_check.product
return is_true(my.product.check) | is_test(my.product)
endfunction
# Is the product a custom target.
function global.is_custom_target(product)
define my.product = is_custom_target.product
return is_optional_element(my.product) & !is_test(my.product)
endfunction
# Is the path extension that of a c/c++ interface implementation file.
function global.is_cpp_implementation_file(path)
return ends_with(my.path, ".ipp")
endfunction
# Is the path extension that of a c/c++ header file.
function global.is_cpp_header_file(path, use_implementation)
return ends_with(my.path, ".hpp") | ends_with(my.path, ".h") |\
((my.use_implementation ? 0) & is_cpp_implementation_file(my.path))
endfunction
# Is the path extension that of a c/c++ source file.
function global.is_cpp_source_file(path)
return ends_with(my.path, ".cpp") | ends_with(my.path, ".c")
endfunction
# Does the file extension conform to the required prefix pattern?
function global.is_product_file(product, path, use_implementation)
define my.product = is_product_file.product
require(my.product, "product", "prefix")
# doc expects no type and bash_completion and manual have no extension.
if (is_data(my.product))
if (match_prefix(my.product, "bash_completion"))
return ends_with(my.path, "")
elsif (match_prefix(my.product, "doc"))
return ends_with(my.path, "")
elsif (match_prefix(my.product, "pkgconfig"))
return ends_with(my.path, ".pc")
elsif (match_prefix(my.product, "sysconf"))
return ends_with(my.path, ".cfg")
endif
elsif (is_cpp(my.product))
return is_cpp_source_file(my.path) |\
is_cpp_header_file(my.path, my.use_implementation)
elsif (is_headers(my.product))
return is_cpp_header_file(my.path, my.use_implementation)
elsif (is_info(my.product))
return ends_with(my.path, ".texi")
elsif (is_java(my.product))
return ends_with(my.path, ".java")
elsif (is_manual(my.product))
return ends_with(my.path, "")
elsif (is_python(my.product))
return ends_with(my.path, ".py")
elsif (is_script(my.product))
return ends_with(my.path, ".sh")
endif
abort "Unknown product type for prefix: '$(my.product.prefix)'"
endfunction
# Get the relative files in the directory.
function global.get_product_files(files, folder, trim, product,\
use_implementation)
define my.files = get_product_files.files
define my.product = get_product_files.product
define my.directory = open_directory(my.folder)
for my.directory.file as _file by _file.name\
where is_product_file(my.product, _file.name, my.use_implementation)
define my.path = "$(_file.path)$(_file.name)"
define my.relative = difference(my.path, my.trim)
table_add(my.files, my.relative, "")
endfor
endfunction
# Get the relative files in the directory hierarchy.
function global.get_all_product_files(files, folder, trim, product)
define my.files = get_all_product_files.files
define my.product = get_all_product_files.product
get_product_files(my.files, my.folder, my.trim, my.product, 1)
define my.directory = open_directory(my.folder)
for my.directory.directory as _directory by _directory.name
define my.subfolder = "$(_directory.path)$(_directory.name)"
get_all_product_files(my.files, my.subfolder, my.trim, my.product)
endfor
endfunction
#------------------------------------------------------------------------------
# Package utilities.
#------------------------------------------------------------------------------
function global.get_package_name(package)
define my.package = get_package_name.package
require(my.package, "package", "library")
return "lib$(my.package.library)"
endfunction
#------------------------------------------------------------------------------
# Debugging utilities.
#------------------------------------------------------------------------------
function global.notify(filename)
echo "Generating $(my.filename)..."
endfunction
function global.require(node, context, attribute)
if (!defined(require.node))
abort "Required node undefined for $(my.context).$(my.attribute)."
endif
define my.node = require.node
if (!defined(my.node.$(my.attribute)))
abort "Required attribute missing: $(my.context).$(my.attribute)"
endif
endfunction
function global.trace1(text)
trace(my.text, 1)
endfunction
function global.trace2(text)
trace(my.text, 2)
endfunction
function global.trace3(text)
trace(my.text, 3)
endfunction
function global.trace(text, context)
if ((global.trace ? 0) = my.context ? 1)
echo defined(my.text) ?? "---> $(my.text)" ? "undefined trace"
endif
endfunction
function global.shebang(interpreter)
write_line("#!/bin/$(my.interpreter)")
endfunction
function global.batch_no_echo()
write_line("@echo off")
endfunction
# Write one include.
function global.write_include(path)
write_line("#include <$(my.path)>")
endfunction
###############################################################################
# Macros
###############################################################################
.endtemplate
.template 1
.
.# Write a line to the template.
.macro global.write_line(text)
$(my.text ? "")
.endmacro
.
.# Write a single indented line to the template.
.macro global.write_line1(text)
$(my.text ? "")
.endmacro
.
.# Write a double indented line to the template.
.macro global.write_line2(text)
$(my.text ? "")
.endmacro
.
.macro global.heading1(text)
# $(my.text)
#==============================================================================
.endmacro
.
.macro global.heading2(text)
# $(my.text)
#------------------------------------------------------------------------------
.endmacro
.
.macro global.heading3(text)
# $(my.text)
.endmacro
.
.macro global.copyleft(name)
###############################################################################
# Copyright (c) 2014-2015 $(my.name) developers (see COPYING).
#
# GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY
#
###############################################################################
.endmacro
.
.macro global.c_copyleft(name)
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2014-2015 $(my.name) developers (see COPYING).
//
// GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY
//
///////////////////////////////////////////////////////////////////////////////
.endmacro
.
.macro global.bat_copyleft(name)
REM ###########################################################################
REM # Copyright (c) 2014-2015 $(my.name) developers (see COPYING).
REM #
REM # GENERATED SOURCE CODE, DO NOT EDIT EXCEPT EXPERIMENTALLY
REM #
REM ###########################################################################
.endmacro
.
.endtemplate