-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoml2-scaffold.in
executable file
·1205 lines (1060 loc) · 34 KB
/
oml2-scaffold.in
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/ruby
#
# Copyright 2009-2015 National ICT Australia (NICTA), Australia
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# This program taken an OMF application definition and creates various files to
# simplify building applications which use OML.
#
PROG = 'oml2-scaffold'
VERSION_STRING = "#{PROG} @VERSION@"
COPYRIGHT = "Copyright 2009-2015, NICTA"
require 'stringio'
require 'optparse'
$integer_alias = :int32
$types = {
:boolean => {
:ctype => "uint8_t %s",
:setter => "bool",
:defval => "(val%5)",
:omltype => "OML_BOOL_VALUE",
:popttype => "POPT_ARG_NONE",
:poptctype => "int",
:poptdefval => "0",
},
:string => { # type used in the AppDescription
# Code generation
:ctype => "const char *%s", # type used for OML-injectable variables of this types
:setter => "string", # appended to "omlc_set_"
:defval => "\"foo\"", # example argument(s) passed to injection helper, can use the variable "val"
:omltype => "OML_STRING_VALUE", # OML type for Measurement Point definition
# Command line parsing
:popttype => "POPT_ARG_STRING", # type used for popt(3) command-line options
:poptctype => "char*", # type of the variable where popt(3) will store the value
:poptdefval => "\"\"", # default value for this variable if none is specified on the CLI
},
:long => {
:ctype => "long *%s",
:setter => "long",
:defval => "(long)val",
:omltype => "OML_LONG_VALUE",
:popttype => "POPT_ARG_LONG",
:poptctype => "int",
:poptdefval => "0",
},
:int32 => {
:ctype => "int32_t %s",
:setter => "int32",
:defval => "((int32_t)-val)",
:omltype => "OML_INT32_VALUE",
:popttype => "POPT_ARG_INT",
:poptctype => "int",
:poptdefval => "0",
},
:uint32 => {
:ctype => "uint32_t %s",
:setter => "uint32",
:defval => "((uint32_t)val)",
:omltype => "OML_UINT32_VALUE",
:popttype => "POPT_ARG_INT",
:poptctype => "unsigned int",
:poptdefval => "0",
},
:int64 => {
:ctype => "int64_t %s",
:setter => "int64",
:defval => "((int64_t)-val)",
:omltype => "OML_INT64_VALUE",
:popttype => "POPT_ARG_INT",
:poptctype => "int",
:poptdefval => "0",
},
:uint64 => {
:ctype => "uint64_t %s",
:setter => "uint64",
:defval => "((uint64_t)val)",
:omltype => "OML_UINT64_VALUE",
:popttype => "POPT_ARG_INT",
:poptctype => "unsigned int",
:poptdefval => "0",
},
:double => {
:ctype => "double %s",
:setter => "double",
:defval => "1.0/val",
:omltype => "OML_DOUBLE_VALUE",
:popttype => "POPT_ARG_DOUBLE",
:poptctype => "double",
:poptdefval => "0.",
},
:blob => {
:ctype => "const void *%s",
:setter => "blob",
:defval => "NULL, 0",
:omltype => "OML_BLOB_VALUE",
#:popttype => false, # no blob on the command line
#:poptctype => false,
#:poptdefval => false,
},
:guid => {
:ctype => "oml_guid_t %s",
:setter => "guid",
:defval => "OMLC_GUID_NULL",
:omltype => "OML_GUID_VALUE",
#:popttype => false, # no GUID on the command line
#:poptctype => false,
#:poptdefval => false,
},
:vector_double => {
:ctype => "const double %s[]",
:setter => "vector_double",
:defval => "",
:omltype => "OML_VECTOR_DOUBLE_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
:vector_int32 => {
:ctype => "const int32_t %s[]",
:setter => "vector_int32",
:defval => "NULL, 0",
:omltype => "OML_VECTOR_INT32_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
:vector_uint32 => {
:ctype => "const uint32_t %s[]",
:setter => "vector_uint32",
:defval => "",
:omltype => "OML_VECTOR_UINT32_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
:vector_int64 => {
:ctype => "const int64_t %s[]",
:setter => "vector_int64",
:defval => "",
:omltype => "OML_VECTOR_INT64_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
:vector_uint64 => {
:ctype => "const uint64_t %s[]",
:setter => "vector_uint64",
:defval => "",
:omltype => "OML_VECTOR_UINT64_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
:vector_bool => {
:ctype => "const bool %s[]",
:setter => "vector_bool",
:defval => "",
:omltype => "OML_VECTOR_BOOL_VALUE",
#:popttype => false,
#:poptctype => false,
#:poptdefval => false,
},
}
# List the currently supported types, without including backward compatibility
# elements
$ctypes = $types.keys.select { |k| k != :long && !$types[k][:ctype].nil? }
$popttypes = $types.keys.select { |k| !$types[k][:popttype].nil? }
# Backward-compatible mappings for deprecated types
# :integer and :int are added in run(), after the command-line parsing
$types[:float] = $types[:double]
$types[:real] = $types[:double]
$types[:flag] = $types[:boolean]
## The real logic of oml2-scaffold.
# Parse the command line arguments, and decide what to do
# \see AppDefinition, AppDefinition::template, Property, MeasurementPoint, Metric
def run()
$stderr.puts "INFO\t#{VERSION_STRING} #{COPYRIGHT}"
if File.basename($PROGRAM_NAME) == 'oml2_scaffold'
old = File.basename($PROGRAM_NAME)
new = old.gsub('_', '-')
$stderr.puts "WARN\t'#{old}' is a deprecated name for this script; use '#{new}' instead."
end
opts = OptionParser.new
opts.banner = %Q|
Generate C code for an OML application from an application definition file
Usage: #{$0} [OPTIONS] [app_file]
|
output = []
app_name = 'unknown'
create_app = false
force = false
opts.on('-a', "--app PROG_NAME",
"Create a skeleton OMF application definition file ") do |name|
create_app = true
app_name = name
end
opts.on('-f', "--force",
"Do not check if a file would be overwritten") do
force = true
end
opts.on('-l', "--long",
"Alias :int and :integer to underlying :long") do
$integer_alias = :long
end
opts.on('-i', "--int32",
"Alias :int and :integer and :long to underyling :int32 (default)") do
$integer_alias = :int32
end
opts.on(nil, "--oml",
"Create the oml header file defining all measurement points") do
output << :oml
end
opts.on(nil, "--opts",
"Create the popt header file for all application properties") do
output << :popt
end
opts.on("--make",
"Create a skeleton Makefile for this application") do
output << :make
end
opts.on("--main",
"Create a skeleton main file for this application") do
output << :main
end
opts.on_tail("-h", "--help", "Show this message") do $stderr.write(opts); exit end
opts.on_tail("-v", "--version", "Show the version\n") do
$stderr.puts VERSION_STRING
exit
end
begin
rest = opts.parse(ARGV)
rescue OptionParser::InvalidOption => ex
$stderr.puts ex
exit -1
rescue OptionParser::MissingArgument => ex
$stderr.puts ex
exit -1
end
if rest.size > 1
$stderr.puts "ERROR\tToo many arguments"
$stderr.write(opts.banner)
exit -1
end
app_file = rest[0]
# Backward-compatible mappings for deprecated integer types, others are set above
$types[:integer] = $types[$integer_alias]
$types[:int] = $types[$integer_alias]
if create_app
if app_file
$stderr.puts "ERROR\tDon't specify app_file when asking to create one"
exit -1
end
uri = "#{ENV['USER']}:app:#{app_name}"
ad = AppDefinition.new uri, app_name
app_file = "#{app_name}.rb"
if File.exists?(app_file) && !force
$stderr.puts "ERROR\tFile #{app_file} already exists. Use --force to overwrite"
exit -1
else
File.open(app_file, 'w') do |f|
ad.write_app_def(f, app_file)
end
end
$stderr.puts "INFO\tCreated #{app_file}"
$stderr.puts "INFO\tYou can now generate a skeleton main file with '#{$PROGRAM_NAME} --main #{app_file}' and its associated Makefile with '#{$PROGRAM_NAME} --make #{app_file}'"
AppDefinition.remove ad
exit
end
unless app_file
$stderr.puts "ERROR\tMissing app_file"
exit -1
end
app_file = File.expand_path(app_file)
unless File.readable?(app_file)
$stderr.puts "ERROR\tCan't find or open application description file #{app_file}"
exit -1
end
load(app_file)
AppDefinition.each do |app|
output.each do |type|
case type
when :oml
fname = "#{app.name}_oml.h"
File.open(fname, 'w') do |f|
app.write_oml_h(f)
end
$stderr.puts "INFO\tCreated #{fname}"
when :popt
fname = "#{app.name}_popt.h"
File.open(fname, 'w') do |f|
app.write_opts_h(f)
end
$stderr.puts "INFO\tCreated #{fname}"
when :make
fname = "Makefile"
if File.exists?(fname) && !force
$stderr.puts "ERROR\tFile #{fname} already exists. Use --force to overwrite"
exit -1
else
File.open(fname, 'w') do |f|
app.write_makefile(f)
end
$stderr.puts "INFO\tCreated #{fname}"
end
when :main
fname = "#{app.name}.c"
if (File.exists?(fname) || File.exists?("config.h")) && !force
$stderr.puts "ERROR\t#{fname} or config.h already exist. Use --force to overwrite"
exit -1
else
File.open(fname, 'w') do |f|
app.write_main(f)
end
File.open('config.h', 'w') do |f|
app.write_config(f)
end
$stderr.puts "INFO\tCreated #{fname} and config.h"
end
else
$stderr.puts "ERROR\tUnsupported output format #{type}"
exit -1
end
end
end
end
## A representation of a command-line option and the associated popt(3) code generation
class Property < Struct.new(:name, :description, :parameter, :options)
## Parse a defProperty
#
# Expects a valid OMF>=5.4 defProperty call
# (http://doc.mytestbed.net/doc/omf/OmfEc/Backward/AppDefinition.html#defProperty-instance_method),
# but try to be backward compatible with OMF<=5.3
# (http://oml.mytestbed.net/projects/omf53/wiki/OEDL-5-3-defProperty-App) in
# some corner cases (http://oml.mytestbed.net/issues/1159#Heuristic)
#
# If this changes, the documentation of defProperty MUST be updated to reflect
# it in OMF:omf_ec/lib/omf_ec/backward/app_definition.rb.
#
def initialize(name = :mandatory, description = nil, parameter = nil, options = {})
if parameter.nil? and options[:use_name] == false or
not parameter.nil? and parameter.length == 1
parameter, opts = pre54defProperty(name, description, parameter, options)
options = options.merge(opts)
options.delete(:impl)
end
options[:type] = options[:type].to_sym || :int32
if (m = /^--(.*)/.match(parameter))
options[:longName] = m[1]
elsif (m = /^-(.)/.match(parameter))
options[:shortName] = m[1]
end
options[:longName] ||= name
options[:shortName] ||= options[:mnemonic]
options[:var_name] ||= name.gsub(/-/, '_')
# Only call the superclass constructor when the options hash is good
super
end
# Parse an OMF<=5.3 defProperty, issuing update warnings
def pre54defProperty(long_name, descr, letter_name, opts = {})
letter_name = letter_name
if letter_name.nil?
parameter = "--#{long_name}"
else
parameter = "-#{letter_name}"
end
options = {}
options[:mnemonic] = letter_name
options[:type] = opts[:type].to_sym
options[:unit] = opts[:unit] if opts[:unit]
options[:default] = opts[:default] if opts[:default]
if opts[:impl]
# Only keep var_name if it is different from name
options[:var_name] = opts[:impl][:var_name] if opts[:impl][:var_name] != long_name
options[:default] = opts[:impl][:popt_val] if opts[:impl][:popt_val] != long_name
end
$stderr.puts "WARN\tObsolescent declaration of '#{long_name}'; consider updating as follows: " +
"app.defProperty('#{long_name}', '#{descr}', '#{parameter}'" +
(options[:type] ? ", :type => :#{options[:type]}" : "") +
(options[:unit] ? ", :unit => '#{options[:unit]}'" : "") +
(options[:mnemonic] ? ", :mnemonic => '#{options[:mnemonic]}'" : "") +
(options[:default] ? ", :default => '#{options[:default]}'" : "") +
(options[:var_name] ? ", :var_name => '#{options[:var_name]}'" : "") +
")"
return parameter, options
end
## Output code to define the variable containing the :var_name CLI option
# \param stream stream to write code to
def write_opts_t(stream)
stream.puts " #{$types[options[:type]][:poptctype]} #{options[:var_name]};"
end
## Generate the popt(3) API call to define this command-line option
def popt_def()
a = []
a << "\"#{options[:longName]}\""
a << (options[:shortName] ? "'#{options[:shortName]}'" : '0')
a << "#{$types[options[:type]][:popttype]}"
a << "&g_opts_storage.#{options[:var_name]}"
a << (options[:mnemonic] ? "'#{options[:mnemonic]}'" : '0')
a << "\"#{description}\""
a << "\"#{options[:unit]}\"" if options[:unit]
" { #{a.join(', ')}},"
end
## Get the default value for this property
# \return imlp[:popt_val] if defined, or the default value for the type otherwise
def popt_defval
options[:default] || $types[options[:type]][:poptdefval]
end
end
## A single metric, and associated code generation
class Metric < Struct.new(:name, :type, :opts)
# Keep track of the deprecation warning we already displayed (one for each type)
@@warnings = Hash.new
## Define a single Metric
# \param name identifier of the metric (field name)
# \param type type of the metric (Ruby/AppDef typology)
# \param opts optional arguments; none are really supported right now...
# \return a Metric
def initialize(name, type, opts = {})
super
self.type = type.to_sym
end
## Return the C type for a Ruby/AppDef type, issuing deprecation warning if needed.
def c_type
case type
when :int, :integer
if $integer_alias == :long and @@warnings[type] == nil
$stderr.puts "WARN\tCommand line switch --long makes :#{type} an alias for :long, but :long is deprecated; new applications should use :int32, :uint32, :int64, :uint64 instead"
@@warnings[type] = true
end
when :long
if @@warnings[type] == nil
$stderr.puts "WARN\tType :long is deprecated; new applications should use :int32, :uint32, :int64, :uint64 instead"
@@warnings[type] = true
end
when :float, :real
if @@warnings[type] == nil
$stderr.puts "WARN\tType #{type} is deprecated; new applications should use :double instead"
@@warnings[type] = true
end
when :flag
if @@warnings[type] == nil
$stderr.puts "WARN\tType #{type} is deprecated and incompatible with OMF; new applications should use :boolean instead"
@@warnings[type] = true
end
end
# Catch errors in case the type does not exist
begin
$types[type][:ctype]
rescue
$stderr.puts "ERROR\tUnknown type #{type}"
exit -1
end
end
def is_vector?
return /vector_\w+/ =~ type.to_s
end
## Write an example vector for this metric
# \param stream stream to write code snippet to
def write_example_vector(stream)
stream.print "\n static ", c_type % name, " = {\n"
case type
when :vector_double
stream.print " 0.0, M_E, M_PI, HUGE, 1.0/0.0\n"
when :vector_int32
stream.print " 0, -1, 1, INT32_MIN, INT32_MAX\n"
when :vector_uint32
stream.print " 0, 1, 2, UINT32_MAX -2, UINT32_MAX -1, UINT32_MAX\n"
when :vector_int64
stream.print " 0, -1, 1, INT32_MIN - UINT64_C(1), INT64_MIN, INT32_MAX + UINT64_C(1), INT64_MAX\n"
when :vector_uint64
stream.print " 0, 1, 2, UINT32_MAX + UINT64_C(1), UINT64_MAX -1, UINT64_MAX\n"
when :vector_bool
stream.print " true, false, true, false\n"
end
stream.print " };\n"
stream.print " static const size_t #{name}_len = sizeof(#{name})/sizeof(#{name}[0]);\n"
end
end
## A representation of a Measurement Point and the associated code generation
class MeasurementPoint
attr_reader :name
## Initialise a Measurement point
# \param name identifier for this MP
# \param block block defining one of more Metric using defMetric
# \return a MeasurementPoint
def initialize(name, &block)
@name = name
@ms = []
if block
block.call(self)
end
end
## Implementation of defMetric, simply instantiate the Metric with the same arguments
# \see Metric::initialize
def defMetric(name, type, opts = {})
@ms << Metric.new(name, type, opts)
end
## Write MP definition out into a stream
# \param stream stream to write header snippet into
# \see Metric::c_type
def write_oml_h(stream)
stream.puts "static OmlMPDef oml_#{@name}_def[] = {"
@ms.each do |m|
stream.puts " {\"#{m.name}\", #{$types[m.type][:omltype]}},"
end
stream.puts " {NULL, (OmlValueT)0}"
stream.puts "};"
end
## Write MP injection helper function into a stream
# \param stream stream to write header snippet into
# \see Metric::c_type
def write_oml_helper_function(stream)
stream.print "static inline int
oml_inject_#{@name}(OmlMP *mp"
@ms.each do |m|
stream.print ", #{m.c_type}" % m.name
# Blob and vector injection helpers also must pass the size blob/vector size
if m.type == :blob
stream.print ", size_t #{m.name}_len"
elsif m.is_vector?
stream.print ", size_t #{m.name}_len"
end
end
stream.puts ")\n{"
stream.puts " int ret = -1;\n\n"
stream.puts " OmlValueU v[#{@ms.length}];\n"
stream.puts " omlc_zero_array(v, #{@ms.length});\n\n"
i = 0
@ms.each do |m|
stream.print " omlc_set_#{$types[m.type][:setter]}(v[#{i}], #{m.name}"
if m.type == :blob
stream.print ", #{m.name}_len"
elsif m.is_vector?
stream.print ", #{m.name}_len"
end
stream.puts ");"
i = i + 1
end
stream.puts "\n ret = omlc_inject(mp, v);\n\n"
i = 0
@ms.each do |m|
# Strings, blobs and vectors need to be reset
# Metric.initialize() already converts strings "blah" to symbols :blah
if m.type == :string || m.type == :blob
stream.puts " omlc_reset_#{$types[m.type][:setter]}(v[#{i}]);"
elsif m.is_vector?
stream.puts " omlc_reset_vector(v[#{i}]);"
end
i = i + 1
end
stream.puts " return ret;\n"
stream.puts "}\n\n"
end
## Write an example injection code
# \param stream stream to write code snippet into
def write_inject_example(stream)
stream.print " if(oml_inject_#{@name}(oml_mps->#{@name}"
i = 0
@ms.each do |m|
if m.is_vector?
stream.print ", #{m.name}, #{m.name}_len"
else
stream.print ", #{$types[m.type][:defval]}"
end
i = i + 1
end
stream.print ") != 0) {\n"
stream.print " logwarn(\"Failed to inject data into MP '#{@name}'\\n\");\n"
stream.print " }\n"
end
## Write an example vector
# \param stream stream to write code snippet to
def write_example_vectors(stream)
@ms.each do |m|
if m.is_vector?
m.write_example_vector(stream)
end
end
end
end
## The definition of an application
class AppDefinition
@@instances = []
attr_accessor :uri, :name, :shortDescription, :description, :path
## Initialise a new application
# \param uri URI identifying both the application and its localisation in the filesystem (for OMF, use ':' as path component separators)
# \param name short name for the application
# \param block other definitions (various parameters, defProperty and defMeasurement)
def initialize(uri, name, &block)
@uri = uri
@name = name || @uri.split(':')[-1]
@properties = []
@mps = []
@@instances << self
block.call(self) if block
end
## Remove an application from the class property listing them
# XXX: Is it ever used?
def self.remove (app_def)
@@instances.delete app_def
end
## Process a specific block of code for each instance
def self.each(&block)
@@instances.each &block
end
## Define the version of an application
# \param major major version
# \param minor minor version, defaults to 0
# \param revision revision, default to 0
def version(major, minor = 0, revision = 0)
@version = [major, minor, revision]
end
## Instanciate a Property
# \see Property::initialize
def defProperty(name, description, parameter, options = {})
@properties << Property.new(name, description, parameter, options)
end
## Instanciate a MeasurementPoint
# \see MeasurementPoint::initialize
def defMeasurement(name, &block)
@mps << MeasurementPoint.new(name, &block)
end
# *_oml.h code generation
## Traverse the list of MPs, and write their C *declaration* into a stream
# \param stream stream to write header snippet into
def write_oml_mps_t(s)
@mps.each do |mp|
s.puts " OmlMP *#{mp.name};"
end
end
## Traverse the list of MPs, and write their C *definition* into a stream
# \param stream stream to write header snippet into
# \see MeasurementPoint::write_oml_h
def write_oml_decl(stream)
@mps.each do |mp|
stream.puts ""
mp.write_oml_h(stream)
end
end
## Write MP registration helper into a stream
# \param stream stream to write header snippet into
def write_oml_register(stream)
@mps.each do |mp|
n = mp.name
stream.puts " g_oml_mps_#{template(:app_cstring)}->#{n} = omlc_add_mp(\"#{n}\", oml_#{n}_def);"
end
end
## Traverse the list of MPs, and write their injection helper into a stream
# \param stream stream to write header snippet into
# \see MeasurementPoint::write_oml_helper_function
def write_oml_helper_functions(stream)
@mps.each do |mp|
mp.write_oml_helper_function(stream)
end
end
# *_opt.h code generation
## Prepare a list of default values for the command line argument
#
# This is used to give initial values to the global structure containing
# command-line parameter values.
#
# \param stream stream to write header snippet into
# \see Property::popt_defval
def write_opts_default(stream)
a = []
@properties.each do |p|
a << p.popt_defval()
end
stream.write a.join(', ')
end
## Write popt(3) declaration for each command line option into a stream
# \param stream stream to write header snippet into
# \see Property::popt_def
def write_opts_options(stream)
@properties.each do |p|
stream.puts p.popt_def()
end
end
## Prepare template code
# \param name name of the template to write
# \return a string with the template code
# \see AppDefinition::write_opts_default, AppDefinition::write_opts_options, AppDefinition::write_oml_decl, AppDefinition::write_oml_mps_t, AppDefinition::write_oml_register, AppDefinition::write_oml_helper_functions, Property::write_opts_t
def template(name)
s = StringIO.new
case name
when :app_name
return @name
when :app_cstring
return @name.gsub("-", "_")
when :app_urn
return "#{ENV['USER'] || 'XXX'}:app:#{@name}"
when :app_path
if @path.nil?
$stderr.puts "ERROR\tApplication path is empty; did you specify \'app.path\'?"
exit -1
end
return @path
when :options_t
@properties.each do |p|
p.write_opts_t(s)
end
when :options_default
write_opts_default(s)
when :options_list
write_opts_options(s)
when :oml_decl
write_oml_decl(s)
when :oml_mps_t
write_oml_mps_t(s)
when :oml_register
write_oml_register(s)
when :oml_helpers
write_oml_helper_functions(s)
else
raise "Undefined template #{name}"
end
s.string
end
## Write some boilerplate text
# \param stream stream to write in, defaults to STDOUT
# \param notice additional text to add on the second line, defaults to "Please do not edit."
def write_file_header(stream = $stdout, notice="Please do not edit.")
stream.puts %Q|/*
* This file was automatically generated by #{VERSION_STRING}
* for #{template(:app_name)} version #{@version[0]}.#{@version[1]}.#{@version[2]}.
* #{notice}
*/|
end
## Write the header containing OML-related declaration and helpers
# \param stream stream to write in, defaults to STDOUT
# \see AppDefinition::template
def write_oml_h(stream = $stdout)
hdrsentinel = template(:app_cstring).upcase
write_file_header(stream)
stream.puts %Q|
#ifndef #{hdrsentinel}_OML_H
#define #{hdrsentinel}_OML_H
#ifdef __cplusplus
extern "C" {
#endif
/* Define HUGE, etc.. */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <math.h>
/* Get size_t and NULL from <stddef.h> */
#include <stddef.h>
#include <oml2/omlc.h>
typedef struct {
#{template(:oml_mps_t)}
} oml_mps_t;
#ifdef OML_FROM_MAIN
/*
* Only declare storage once, usually from the main
* source, where OML_FROM_MAIN is defined
*/
#{template(:oml_decl)}
static oml_mps_t g_oml_mps_storage;
oml_mps_t* g_oml_mps_#{template(:app_cstring)} = &g_oml_mps_storage;
static inline void
oml_register_mps(void)
{
#{template(:oml_register)}
}
#else
/*
* Not included from the main source, only declare the global pointer
* to the MPs and injection helpers.
*/
extern oml_mps_t* g_oml_mps_#{template(:app_cstring)};
#endif /* OML_FROM_MAIN */
#{template(:oml_helpers)}
/* Compatibility with old applications */
#ifndef g_oml_mps
# define g_oml_mps g_oml_mps_#{template(:app_cstring)}
#endif
#ifdef __cplusplus
}
#endif
#endif /* #{hdrsentinel}_OML_H */
|
end
## Write the header containing popt(3)-related code
# \param stream stream to write in, defaults to STDOUT
# \see AppDefinition::template
def write_opts_h(stream = $stdout)
hdrsentinel = template(:app_cstring).upcase
write_file_header(stream)
stream.puts %Q|
#ifndef #{hdrsentinel}_OPTS_H
#define #{hdrsentinel}_OPTS_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
#{template(:options_t)}
} opts_t;
#ifndef USE_OPTS
opts_t* g_opts;
#else
static opts_t g_opts_storage = {#{template(:options_default)}};
opts_t* g_opts = &g_opts_storage;
/* Only the file containing the main() function should come through here */
#include <popt.h>
struct poptOption options[] = {
POPT_AUTOHELP
#{template(:options_list)}
{ NULL, 0, 0, NULL, 0 }
};
#endif /* USE_OPTS */
#ifdef __cplusplus
}
#endif
#endif /* #{hdrsentinel}_OPTS_H */
|
end
## Write example Makefile
# \param stream stream to write in, defaults to STDOUT
# \see AppDefinition::template
def write_makefile(stream = $stdout)
stream.puts %Q?# This file was automatically generated by #{VERSION_STRING}
PROGRAM = #{template(:app_name)}
SRCS = ${PROGRAM}.c
RUNARGS = --loop --delay 1
BINDIR = $(DESTDIR)#{File.dirname(template(:app_path))}/
CFLAGS = -Wall -Werror -g -I. # -I/usr/local/include
LDFLAGS = # -L/usr/local/lib
LIBS = -loml2 -locomm -lpopt
CCLD=$(CC)
SCAFFOLD = #{$PROGRAM_NAME}
OBJS = $(SRCS:%.c=%.o)
all: build
build: $(PROGRAM)
install: $(PROGRAM)
install -m 755 $(PROGRAM) $(BINDIR)
clean:
rm -rf $(PROGRAM)
rm -rf $(OBJS)
realclean: clean
rm -rf $(PROGRAM)_popt.h $(PROGRAM)_oml.h
run: $(PROGRAM)
./$(PROGRAM) $(RUNARGS) --oml-collect file:-
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
$(PROGRAM): $(OBJS)
$(CCLD) -o $@ $^ $(LDFLAGS) $(LIBS)
$(PROGRAM).o: config.h $(PROGRAM)_popt.h $(PROGRAM)_oml.h
$(PROGRAM)_popt.h: $(PROGRAM).rb
$(SCAFFOLD) --opts $<
$(PROGRAM)_oml.h: $(PROGRAM).rb
$(SCAFFOLD) --oml $<
.PHONY: all build install clean realclean
?
end
## Write example main file
# \param stream stream to write in, defaults to STDOUT
# \see AppDefinition::template
def write_main(stream = $stdout)
write_file_header(stream, "Please edit to suit your needs; the run() function should contain application code.")
stream.puts %Q|
#include <unistd.h> /* Needed for usleep(3) in run() */
#include <signal.h>
#include <string.h>
#include <popt.h>
#include <oml2/omlc.h>
#define USE_OPTS /* Include command line parsing code*/
#include "#{template(:app_name)}_popt.h"
#define OML_FROM_MAIN /* Define storage for some global variables; #define this in only one file */
#include "#{template(:app_name)}_oml.h"
#include "config.h"
int loop = 1;