-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.rb
More file actions
1788 lines (1573 loc) · 62.9 KB
/
options.rb
File metadata and controls
1788 lines (1573 loc) · 62.9 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
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 -w
# Ruby-based build system
# Author: ram (Munagala V. Ramanath)
#
# Classes associated with various assembler, compiler and linker options
#
%w{ set common.rb }.each { |f| require f }
class Build # main class and namespace
# GCC has many different kinds of options:
# 1. Some start with a double hyphen (e.g. --param) and some with a single hyphen
# e.g. -static
#
# 2. Some are aliases, e.g. -foptimize-register-move, -fregmove
#
# 3. Some have different strings attached to the option name (call them parameters);
# the paramters can be either numbers or strings, e.g. -O2, -Ofast
#
# 4. The parameter may be run together with the option (e.g. -UFoo) or may have a
# separating space (e.g. -include foo)
#
# 5. In some cases, the parameter may be negated with a "no-" prefix
# (e.g. -Wshadow, -Wno-shadow)
#
# 6. Sometimes these parameters are attached to the option with a separating '='
# e.g. -std=gnu99
#
# 7. Some of these parameters have additional strings attached to them (call them
# values) with a separaing '=', e.g. -Werror=shadow
#
# 8. Sometimes order is relevant (e.g. -Ia, -Ib) and sometimes not, e.g. -include is
# processed after all -D and -U options regardless of where it appears.
#
# 9. Some consecutive options form groups that must be kept together,
# e.g. -Wl,-rpath -Wl,/opt/foo/lib
#
# Semantically we have the following oddities:
#
# 1. The same option name is used for different purposes, e.g.:
# -fdollars-in-identifiers is a pre-processor option
# -fdce is an optimization option
# -fno-diagnostics-show-option is a language-independent opton
# 2. The various option groups listed in the gcc man page are neither complete
# (the set shown under pre-processor options) nor mutually exclusive, e.g.
# -- some of the overall options (e.g. -E) are pre-processor options
# -- some options apply to both compiler and and linker (e.g. -fPIC, -flto)
# -- some options mean different things on different platforms (e.g. ...)
#
# base class for various kinds of options (useful because we can define hash, ==, etc.
# here for the whole hierarchy)
#
class Option
# name
# Name of option including any leading hyphens, e.g. '-v', '--param', etc.
#
# option_kind
# Kind of option: :preprocessor, :compiler, :linker, :other
#
# param_kind
# Whether a parameter is needed and what kind
# :none, :required, :optional, :single, :multi
#
# param
# Entire parameter if any including 'no-' prefix if present; this makes
# comparisons easier -- we need to compare only the name and the param
#
# neg
# true if param has 'no-' prefix; :value if the value has 'no-' prefix; undefined
# or nil if there is no negation involved.
#
# key, value
# There are 2 cases:
# Case A:
# No '=' present in param; here, key is undefined if the param does NOT
# take a 'no-' prefix. If it does, key is param with the 'no-' prefix, if any,
# stripped (so key may equal param)
# Case B:
# key=value parts of param if any (undefined otherwise); key has any 'no-'
# prefix removed; value is unmodified (negation prefix is not normally allowed
# in the value).
# Double negatives are not allowed so we will never have the
# 'no-' present in both (so -Wno-error=no-shadow is not permitted). If a value
# is present, it always has a preceding '=' to separate it from the key.
#
# sep
# required separator character, if any, between name and parameter; set in
# derived classes if present. Currently, either undefined, blank or '=' (see below)
#
# Examples:
# ========
# For option '-Dxyz=abc':
# name is '-D', param is 'xyz=abc', key is 'xfyz', value is 'abc', sep is undefined
#
# For option '-std=gnu99':
# name is '-std', param is 'gnu99', key/value are undefined, sep is '='
#
# For option '-Wno-error=shadow':
# name is '-W', param is 'no-error=shadow', neg is true,
# key is 'error', value is 'shadow'
#
# For option '-Werror=shadow':
# name is '-W', param is 'error=shadow', neg is false,
# key is 'error', value is 'shadow'
#
# For option '--param max-inline-insns-single=1800':
# name is '--param', param is 'max-inline-insns-single=1800',
# key is 'max-inline-insns-single', value = '1800', sep is ' '
#
attr :name, :option_kind, :param_kind, :param, :sep, :key, :value
# since we use gcc to assemble .S files, we use the :compiler for assembler options
OPTION_KINDS = Set[:preprocessor, :assembler, :compiler, :linker, :other]
# (currently unused)
# we follow the groups in the gcc docs
OPTION_GROUPS = Set[
:overall, # overall
:c, # C language
:cxx, # C++ language
:objc, # Objective-C/Objective-C++ language
:diagnostics, # language independent options
:warn, # warning
:warn_c_objc, # warning options for C/Objective-C only
:debug, # debug
:opt, # optimization
:pre_processor, # pre-processor
:assembler, # assembler
:linker, # linker
:directory, # directory
# these are subgroups of "Machine Dependent Options"
:machine_darwin, # darwin (Mac OS)
:machine_linux, # GNU/Linux
:machine_i386, # i386/x86-64
:code_gen, # code generation
]
# whether the option needs parameters
PARAM_KINDS = Set[:none, :required, :optional, :single, :multi]
def self.[]( a_name, a_o_kind, a_p_kind, a_param = nil,
a_neg = nil, a_sep = nil, a_key = nil, a_val = nil )
new a_name, a_o_kind, a_p_kind, a_param, a_neg, a_sep, a_key, a_val
end
# a_name -- name of option
# a_o_kind -- kind of option
# a_p_kind -- kind of parameter
# a_param -- full parameter string if any
# a_sep -- separator between name and param if any
# a_key -- key if any
# a_val -- value if any
#
def initialize a_name, a_o_kind, a_p_kind, a_param = nil,
a_neg = nil, a_sep = nil, a_key = nil, a_val = nil
raise "Need name" if !a_name
s = a_name.strip
raise "Empty name" if s.empty?
raise "Need option_kind" if !a_o_kind
raise "Invalid option kind: #{a_o_kind}" if !OPTION_KINDS.include? a_o_kind
raise "Need param_kind" if !a_p_kind
raise "Invalid param kind: #{a_p_kind}" if !PARAM_KINDS.include? a_p_kind
raise "Need parameter" if !a_param && :required == a_p_kind
raise "Parameter not allowed" if a_param && :none == a_p_kind
# for -Wno-inline, key is 'inline' but there is no value
#raise "Key/value must be both present or both absent" if a_key.nil != a_val.nil?
raise "Unusual separator: #{a_sep}" if a_sep && '=' != a_sep && ' ' != a_sep
@name, @option_kind, @param_kind = s, a_o_kind, a_p_kind
@param = a_param if a_param
@neg = a_neg if a_neg
@sep = a_sep if a_sep
@key = a_key if a_key
@value = a_val if a_val
end # initialize
# dup each field that can be duplicated. Fixnum, TrueClass, FalseClass, Symbol etc.
# cannot (and need not) be duplicated
#
# When options are changed on a per-target basis, we need to duplicate option sets.
#
def initialize_copy( orig )
instance_variables.each { |v|
w = instance_variable_get( v )
# Symbol responds to dup but says "can't dup Symbol" if you invoke it;
# similarly TrueClass and FalseClass
#
next if !w.respond_to?( :dup ) || w.is_a?( Symbol ) || true == w || false == w
instance_variable_set( v, w.dup )
}
end # initialize_copy
def to_s # convert to string
s = @name
return s if !defined?( @param ) || @param.nil?
# We have a parameter; the default behavior is to have no space separating the
# name from the parameter; this is appropriate for many options,
# e.g. -Dfoo, -Ufoo, -Wfoo
# But some need a blank separator of a blank (e.g. "--param foo=bar") or a '='
# (e.g. "-std=gnu99")
# We will never have _two_ equal signs in an option: -fbaz=foo=baz
#
s += @sep if defined? @sep
s += @param # key/value, if any, are part of param
end # to_s
# define <=> if needed
def is_cpp # return true iff this is a pre-processor option
[OptionDefine, OptionUndefine, OptionInclude].include? self.class
end # hash
# comparing options -- used for example when user tries to add an option
def hash
instance_variables.inject( 17 ) { |m, v| 37 * m + instance_variable_get( v ).hash }
end # hash
def == other
return false if self.class != other.class || self.hash != other.hash
idx = instance_variables.index { |v|
instance_variable_get( v ) != other.instance_variable_get( v )
}
return idx.nil?
end # ==
def eql? other
self == other
end # eql?
end # Option
class OptionWarning < Option # -Wfoo or -Wno-foo
# We currently use these warning options for C in this order:
# -Wempty-body -Wtype-limits -fdiagnostics-show-option -Wall -Wpointer-arith
# -Wshadow -Wstrict-prototypes -Werror
# options in -Wextra (same as plain -W)
EXTRA = %w[
clobbered
empty-body
ignored-qualifiers
missing-field-initializers
missing-parameter-type # C only
old-style-declaration # C only
override-init
sign-compare
type-limits
uninitialized
unused-parameter # only with -Wunused or -Wall
unused-but-set-parameter # only with -Wunused or -Wall
].to_set
# options in -Wall
ALL = %w[
address
array-bounds # only with -O2
c++11-compat
char-subscripts
enum-compare # in C/Objc; this is on by default in C++
implicit-int # C and Objective-C only)
implicit-function-declaration # C and Objective-C only
comment
format
main # only for C/ObjC and unless -ffreestanding)
maybe-uninitialized
missing-braces
nonnull
parentheses
pointer-sign
reorder
return-type
sequence-point
sign-compare # only in C++
strict-aliasing
strict-overflow=1
switch
trigraphs
uninitialized
unknown-pragmas
unused-function
unused-label
unused-value
unused-variable
volatile-register-var
].to_set
# all known warning options
PARAM_W = %w[
abi
address
aggregate-return
all
array-bounds
assign-intercept
attributes
bad-function-cast
builtin-macro-redefined
cast-align
cast-qual
char-subscripts
clobbered
comment
comments
conversion
conversion-null
ctor-dtor-privacy
declaration-after-statement
delete-non-virtual-dtor
deprecated
deprecated-declarations
disabled-optimization
div-by-zero
double-promotion
effc++
empty-body
endif-labels
enum-compare
error # takes optional "=<foo>"
extra
fatal-errors
float-equal
format
format-contains-nul
format-extra-args
format-nonliteral
format-nonliteral
format-security
format-y2k
format-zero-length
format=2 # treat this literally since only 2 is allowed
frame-larger-than
free-nonheap-object
ignored-qualifiers
implicit
implicit-function-declaration
implicit-int
init-self
inline
int-to-pointer-cast
invalid-offsetof
invalid-pch
jump-misses-init
larger-than # takes "=<len>"
logical-op
long-long
main
maybe-uninitialized
missing-braces
missing-declarations
missing-field-initializers
missing-format-attribute
missing-include-dirs
missing-parameter-type
missing-prototypes
multichar
narrowing
nested-externs
non-virtual-dtor
nonnull
normalized # takes "=<none|id|nfc|nfkc>"
old-style-cast
old-style-declaration
old-style-definition
overflow
overlength-strings
overloaded-virtual
override-init
packed
packed-bitfield-compat
padded
parentheses
pedantic-ms-format
pmf-conversions
pointer-arith
pointer-sign
pointer-to-int-cast
pragmas
protocol
redundant-decls
reorder
return-type
selector
sequence-point
shadow
sign-compare
sign-conversion
sign-promo
stack-protector
stack-usage
strict-aliasing # takes optional "=n" with n in 1..3
strict-null-sentinel
strict-overflow # takes optional "=n" with n in 1..5
strict-prototypes
strict-selector-match
suggest-attribute # takes "=const|noreturn|pure"
switch
switch-default
switch-enum
sync-nand
system-headers
traditional
traditional-conversion
trampolines
trigraphs
type-limits
undeclared-selector
undef
uninitialized
unknown-pragmas
unsafe-loop-optimizations
unsuffixed-float-constants
unused
unused-but-set-parameter
unused-but-set-variable
unused-function
unused-label
unused-local-typedefs
unused-macros
unused-parameter
unused-result
unused-value
unused-variable
useless-cast
variadic-macros
vector-operation-performance
vla
volatile-register-var
write-strings
zero-as-null-pointer-constant
].to_set
def self.[]( a_param )
raise "Missing warning type" if !a_param
# add checks for =arg in a_param
a = a_param.split '='
if 2 == a.size # have an argument
k, v = a[ 0 ].strip, a[ 1 ].strip
raise "Empty key in #{a.param}" if k.empty?
raise "Empty value in #{a.param}" if v.empty?
# get positive form of key by removng any leading negation
kp = k.sub( /^no-/o, '' )
negated = k.size != kp.size
raise "Unknown warning: #{a_param}" if !PARAM_W.include? kp
# These warnings currently take an argument:
# error -- argument is name of warning
# format -- treat 'format=2' literally since only 2 is allowed as an argument
# larger-than -- argument is integer
# normalized -- argument is one of none|id|nfc|nfkc
# strict-aliasing -- argument is in 1..3
# strict-overflow -- argument is in 1..5
# suggest-attribute -- argument is one of const|noreturn|pure
#
case kp
when 'error' # NOTE: double negatives are not valid: -Wno-error=no-shadow
raise "Unknown warning: #{v}" if !PARAM_W.include? v
when 'format'
raise "Bad value #{v} in #{a_param}" if '2' != v
when 'larger-than'
when 'normalized'
raise "Bad value #{v} in #{a_param}" if v !~ /^(?:none|id|nfc|nfkc)$/o
when 'strict-aliasing'
vn = v.to_i
raise "Bad value #{v} in #{a_param}" if !(1..3).include? vn
when 'strict-overflow'
vn = v.to_i
raise "Bad value #{v} in #{a_param}" if !(1..5).include? vn
when 'suggest-attribute'
raise "Bad value #{v} in #{a_param}" if v !~ /^(?:const|noreturn|pure)$/o
else raise "Warning #{kp} does not take an argument"
end # case
new a_param, kp, negated, v
else # no "<key>=<value>" suffix
# get positive form of param by removng any leading negation
nm = a_param.sub( /^no-/o, '' )
negated = a_param.size != nm.size
raise "Unknown warning: #{a_param}" if !PARAM_W.include? nm
new a_param, nm, negated
end
end # self.[]
# a_param -- full parameter, e.g. 'no-error=unused'; may be identical to a_key
# a_key -- type of warning excluding 'no-' prefix, e.g. 'error'
# a_neg -- true iff we got the negated form, e.g. 'no-inline'
# a_val -- for -Wno-foo=bar, a_key is 'foo', a_val is 'bar'
#
def initialize( a_param, a_key, a_neg, a_val = nil )
# ctor may be invoked directly so we do full checking here
raise "a_key is void" if a_key.nil? || a_key.empty?
raise "Unknown warning: #{a_param}" if !PARAM_W.include? a_key
raise "a_param is void" if a_param.nil? || a_param.empty?
raise "a_neg is not Boolean" if ![true, false].include? a_neg
super '-W', :compiler, :required, a_param, a_neg, nil, a_key, a_val
end # initialize
end # OptionWarning
# DESIGN: Define one class per group of similar options; sometimes we create a class
# for a single option (e.g. -fPIC). Might have to refactor later.
# Pre-processor option to define/undefine symbols: -DXYZ=abc or -UFOO
class OptionDefine < Option
def self.[]( a_name, a_param )
if '-D' == a_name
raise "Need symbol name to define" if !a_param || a_param.empty?
a = a_param.split '='
raise "Bad define option: #{a_param}" if a.size < 1 || a.size > 2
k = a[ 0 ].strip
raise "Empty symbol in #{a_param}" if k.empty?
if 2 == a.size
v = a[ 1 ].strip
raise "Empty value in #{a_param}" if v.empty?
else
v = nil
end
elsif '-U' == a_name
raise "Need symbol name to undefine" if !a_param || a_param.empty?
k, v = nil, nil
else raise "Bad macro option: #{a_name}"
end
new a_name, :preprocessor, :required, a_param, k, v
end # []
def initialize a_name, a_o_kind, a_p_kind, a_param, a_key, a_value
super a_name, a_o_kind, a_p_kind, a_param, nil, nil, a_key, a_value
end # initialize
end # OptionDefine
# Include directory path: -Ix/y/z (NOTE: order of these options is important)
class OptionInclude < Option
def self.[]( a_param )
raise "Need include path" if !a_param || a_param.empty?
# directory may not exist when this option is created; it may be generated later
#File.check_dir a_param
new '-I', :preprocessor, :required, a_param
end
end # OptionInclude
# Include a file: -include x/y/foo.h (NOTE: order of these options is important)
class OptionIncludeFile < Option
def self.[]( a_param )
raise "Need include path" if !a_param || a_param.empty?
new '-include', :preprocessor, :required, a_param, nil, ' '
end
end # OptionIncludeFile
class OptionMachine < Option # -mfoo (compiler, assembler, or linker)
PARAM_M = Set[ 'sse2', '64', '32']
def self.[]( a_param, c_or_a_or_l = :compiler )
raise "Need machine parameter" if !a_param || a_param.empty?
raise "Bad machine parameter: #{a_param}" if !PARAM_M.include? a_param
new '-m', c_or_a_or_l, :required, a_param
end
end # OptionMachine
# -fPIC is both compiler and linker option!
class OptionPIC < Option
def self.[]( c_or_l )
raise "Bad PIC type: #{c_or_l}" if c_or_l != :compiler && c_or_l != :linker
new '-fPIC', c_or_l, :none
end
end # OptionPIC
# -fsigned-char is a compiler option
class OptionSignedChar < Option
def self.[]()
new '-fsigned-char', :compiler, :none
end
end # OptionSignedChar
# -funsigned-char is a compiler option
class OptionUnsignedChar < Option
def self.[]()
new '-funsigned-char', :compiler, :none
end
end # OptionUnsignedChar
# -fno-common is compiler option on OSX
class OptionNoCommon < Option
def self.[]()
new '-fno-common', :compiler, :none
end
end # OptionNoCommon
# -pedantic is compiler option
class OptionPedantic < Option
def self.[]()
new '-pedantic', :compiler, :none
end
end # OptionPedantic
# -pthread is compiler option
class OptionPthread < Option
def self.[]()
new '-pthread', :compiler, :none
end
end # OptionPthread
# -pipe is compiler option
class OptionPipe < Option
def self.[]()
new '-pipe', :compiler, :none
end
end # OptionPipe
# -flto is both compiler and linker option!
class OptionLTO < Option
def self.[]( c_or_l )
raise "Bad lto type: #{c_or_l}" if c_or_l != :compiler && c_or_l != :linker
new '-flto', c_or_l, :none
end
end # OptionLTO
class OptionStd < Option # -std=...
PARAM_STD = Set[ 'c89', 'c90', 'iso9899:1990', 'iso9899:199409',
'c99', 'c9X', 'iso9899:1999', 'iso9899:199x',
'c11', 'c1x', 'iso9899:2011',
'gnu89', 'gnu90', 'gnu99', 'gnu9x', 'gnu11', 'gnu1x',
'c++11', 'c++0x', 'c++1y',
'gnu++11', 'gnu++0x', 'gnu++1y'
]
def self.[]( a_param )
raise "Need std parameter" if !a_param || a_param.empty?
raise "Bad std parameter: #{a_param}" if !PARAM_STD.include? a_param
new a_param
end
def initialize a_param
super '-std', :compiler, :required, a_param, nil, '='
end # initialize
end # OptionStd
class OptionDebug < Option # -g, -g0, -gstabs, ...
# Assembler may also need this option
def self.[]( a_kind = :compiler, a_param = nil )
p = a_param ? a_param.strip : nil
new '-g', a_kind, :optional, p
end
end # OptionDebug
class OptionOptimize < Option # -O? or other -f options like -fno-default-inline etc.
PARAM_O = Set['0', '1', '2', '3', 's' 'fast']
# we currently use these options (add more as needed):
# -fdiagnostics-show-option (handled as OptionDiagnostic below)
# -finline-functions
# -fno-strict-aliasing
#
PARAM_f = Set[ 'inline-functions', 'no-inline-functions',
'strict-aliasing', 'no-strict-aliasing',
'function-sections', 'data-sections',
'omit-frame-pointer', 'reorder-blocks',
'no-rtti', 'no-exceptions']
def self.[]( a_name, a_param, a_kind = :compiler )
if '-O' == a_name
raise "Bad -O parameter: #{a_param}" if a_param && !PARAM_O.include?( a_param )
new a_name, a_kind, :optional, a_param
elsif '-f' == a_name
raise "Missing -f parameter" if !a_param
raise "Bad -f parameter: #{a_param}" if !PARAM_f.include?( a_param )
new a_name, a_kind, :required, a_param
else
raise "Bad optimize option: #{a_name}"
end
end
end # OptionOptimize
class OptionOptimizeParam < Option # --param key=value
# we currently use (add more as needed):
# max-inline-insns-single
#
PARAM_NAME = Set['max-inline-insns-single']
def self.[]( a_param )
raise "Need param parameter" if !a_param || a_param.empty?
a = a_param.split '='
raise "Bad param option: #{a.param}" if a.size != 2
a[ 0 ].strip!; a[ 1 ].strip!
raise "Empty name in #{a.param}" if a[ 0 ].empty?
raise "Unrecognized name: #{a[0]}" if !PARAM_NAME.include?( a[ 0 ] )
new a_param, a[ 0 ], a[ 1 ]
end
def initialize a_param, a_key, a_val
super '--param', :compiler, :required, a_param, nil, ' ', a_key, a_val
end # initialize
end # OptionOptimizeParam
# Options to Control Diagnostic Messages Formatting
#
class OptionDiagnostic < Option
# we currently use these (add more as needed):
# -fdiagnostics-show-option
#
PARAM_W = Set['diagnostics-show-option']
def self.[]( a_param )
raise "Missing diagnostic type" if !a_param
p = a_param.sub( /^no-/o, '' )
negated = a_param.size != p.size
raise "Unknown diag option: #{a_param}" if !PARAM_W.include? p
new '-f', :compiler, :required, a_param, negated, nil, p
end
end # OptionDiagnostic
# Special case of assembler pass-through
# -Wa,--noexecstack
#
class OptionNoExecStack < Option
def self.[]
new '-Wa,--noexecstack', :compiler, :none
end
end # OptionNoExecStack
# --------------------------------------------------
# Linker-only options -- BEGIN
# NOTE: Some options are for both compiling and linking (e.g. -fPIC); they are
# defined above
# --------------------------------------------------
# shared libraries
class OptionShared < Option
def self.[]
new '-shared', :linker, :none
end
end # OptionShared
class OptionNoStdLib < Option
def self.[]
new '-nostdlib', :linker, :none
end
end # OptionNoStdLib
# OSX dynamic libraries
class OptionDynamicLib < Option
def self.[]
new '-dynamiclib', :linker, :none
end
end # OptionDynamicLib
# static libraries
class OptionStatic < Option
def self.[]
new '-static', :linker, :none
end
end # OptionStatic
# strip libraries
class OptionStrip < Option
def self.[]
new '-s', :linker, :none
end
end # OptionStrip
# libraries: -lpthread, -lrt, etc.
# argument is the part after '-l'
# NOTE: Order of these options is important
#
class OptionLib < Option
def self.[]( a_param )
raise "Missing library name" if !a_param
p = a_param.strip
raise "Empty library name" if p.empty?
new '-l', :linker, :required, p
end
end # OptionLib
# library paths: -L/x/y/z
# NOTE: Order of these options is important
#
class OptionLibPath < Option
def self.[]( a_param )
raise "Missing path" if !a_param
p = a_param.strip
raise "Empty path" if p.empty?
new '-L', :linker, :required, p
end
end # OptionLibPath
# (OSX only) framework: -framework AGL|Carbon|QuickTime|OpenGL
#
class OptionFramework < Option
def self.[]( a_param )
raise "Missing framework name" if !a_param
p = a_param.strip
raise "Empty framework name" if p.empty?
new '-framework', :linker, :required, p, nil, ' '
end
end # OptionFramework
# (OSX only) install_name: -install_name /opt/foo/lib/libbaz.2.dylib
#
class OptionInstallName < Option
def self.[]( a_param )
raise "Missing install name" if !a_param
p = a_param.strip
raise "Empty install name" if p.empty?
new '-install_name', :linker, :required, p, nil, ' '
end
end # OptionInstallName
# (OSX only) compatibility_version: -compatibility_version 3
#
class OptionCompatVersion < Option
def self.[]( a_param )
raise "Missing compatibility version" if !a_param
p = a_param.strip
raise "Empty compatibility version" if p.empty?
new '-compatibility_version', :linker, :required, p, nil, ' '
end
end # OptionCompatVersion
# (OSX only) current version: -current_version 3.0
#
class OptionCurrentVersion < Option
def self.[]( a_param )
raise "Missing current version" if !a_param
p = a_param.strip
raise "Empty current version" if p.empty?
new '-current_version', :linker, :required, p, nil, ' '
end
end # OptionCurrentVersion
# Special case of linker pass-through. This is a pair of options specifying the rpath:
# -Wl,-rpath -Wl,/opt/foo/lib
# Could be specified as a single argument like this: -Wl,-rpath,/opt/foo/lib
# but we don't do this since it may cause problems with pkg-config
#
class OptionRPath < Option
def self.[]( a_param ) # argument is the actual path
raise "Missing rpath" if !a_param
p = a_param.strip
raise "Empty rpath" if p.empty?
new '-Wl,', :linker, :required, p
end
def to_s # convert to string
'-Wl,-rpath ' + super
end # to_s
end # OptionRPath
# Special case of linker pass-through. This is a pair of options for the soname:
# -Wl,-soname -Wl,libFoo.so
# Could be specified as a single argument like this: -Wl,-soname,libFoo.so
# but we don't do this since it may cause problems with pkgconfig
#
class OptionSOName < Option
def self.[]( a_param )
raise "Missing soname" if !a_param
p = a_param.strip
raise "Empty soname" if p.empty?
new '-Wl,', :linker, :required, p
end
def to_s # convert to string
'-Wl,-soname ' + super
end # to_s
end # OptionSOName
# Linker pass-through options other than rpath and soname.
# argument is string following '-Wl,'
# NOTE: Order of these options may be important
#
class OptionLinkerPassthru < Option
def self.[]( a_param )
raise "Missing linker option" if !a_param
p = a_param.strip
raise "Empty linker option" if p.empty?
raise "-rpath should use OptionRPath" if '-rpath' == p
raise "-soname should use OptionSOName" if '-soname' == p
new '-Wl,', :linker, :required, p
end
end # OptionLinkerPassThru
# --------------------------------------------------
# Linker-only options -- END
# --------------------------------------------------
# base class for a set of related options such as pre-processor, compiler etc.
#
class OptionSet
DIAG = 'diagnostics-show-option'
OPTIMIZE_VALUES = Set[ '1', '2', '3', 's', 'fast' ] # -O suffixes
# options -- array of options in this set
# bld -- item from Build::BUILD_TYPES
#
attr :options, :bld
def initialize a_bld
raise "Bad build type: #{a_bld}" if !Build::BUILD_TYPES.include? a_bld
@bld = a_bld
# use an array since order is important for some options (e.g. -I, -L, etc.)
@options = []
end # initialize
# When options are changed on a per-target basis, we need to duplicate option sets.
#
def initialize_copy( orig )
@options = @options.map( &:dup ) # assumes each option can be duplicated
# @bld is a symbol so no need to clone
end # initialize_copy
# helper routines to return index of given option if it is present, or nil if absent
# override in derived classes should not be needed
#
def get_opt name, param = nil
return @options.index{ |opt| # no need to compare all fields
opt.name == name && opt.param == param
}
end # get_opt
# Add single option after error checking; argument is an Option object
# if option is already present in set, 'replace' determines the action: if true,
# nothing happens; if false an exception is raised
# Return index if option is already present, nil if it was added
#
# Override in derived classes to do more elaborate checking for conflicting options.
#
def add_opt opt, replace = false
raise "Argument not an Option: #{opt.class.name} (#{opt.to_s})" if !opt.is_a? Option
# check for exact match
idx = get_opt opt.name, opt.param
if idx
msg = "%s already present at position %d" % [opt.to_s, idx]
raise msg if !replace
Build.logger.warn msg
return idx
end
@options << opt
nil
end # add_opt
# add array of option strings
def add slist, replace = false
list = parse slist # convert option strings to option objects
list.each { |opt| add_opt opt, replace }
return self
end # add
# remove option from @options or @options_post according as pre is true or not
# (pre=false only happens for linker options)
# first argument is an Option object; return deleted option if found;
# otherwise,
# -- raise exception if err is true
# -- return nil if err is false
#
def del opt, err = true, pre = true
raise "Argument not an Option: #{opt.class.name}" if !opt.is_a? Option
raise "err must be true or false" if ![true, false].include? err
raise "pre must be true or false" if ![true, false].include? pre
# check for exact match
if pre
idx = get_opt opt.name, opt.param
return @options.delete_at( idx ) if idx # found
else # linker option
raise "Argument not a linker option" if opt.option_kind != :linker
idx = get_post opt.name, opt.param
return @options_post.delete_at( idx ) if idx # found
end
if err
dump
raise "Option '%s%s' not found in #{self.class.name}" % [opt.name, opt.param]
end
return nil
end # del
# delete list of options; slist is an array option strings
# if err is true an exception is raised if the option is not found
#
def delete slist, err = true
list = parse slist
list.each { |opt| del opt, err }
return self
end # delete
def to_s
@options.join ' '
end # to_s
# comparing option sets -- used for example when we want to compare the set read
# from the persistence DB with the current set