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
|
/* valacodecontext.vala
*
* Copyright (C) 2006-2012 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
/**
* The root of the code tree.
*/
public class Vala.CodeContext {
/**
* Enable run-time checks for programming errors.
*/
public bool assert { get; set; }
/**
* Enable additional run-time checks such as type checks.
*/
public bool checking { get; set; }
/**
* Do not warn when using deprecated features.
*/
public bool deprecated { get; set; }
/**
* Hide the symbols marked as internal
*/
public bool hide_internal { get; set; }
/**
* Do not check whether used symbols exist in local packages.
*/
public bool since_check { get; set; }
/**
* Do not warn when using experimental features.
*/
public bool experimental { get; set; }
/**
* Enable experimental enhancements for non-null types.
*/
public bool experimental_non_null { get; set; }
/**
* Enable GObject creation tracing.
*/
public bool gobject_tracing { get; set; }
/**
* Output C code, don't compile to object code.
*/
public bool ccode_only { get; set; }
/**
* Command to run pkg-config.
*/
public string pkg_config_command { get; set; default = "pkg-config"; }
/**
* Enable support for ABI stability.
*/
public bool abi_stability { get; set; }
/**
* Output C header file.
*/
public string? header_filename { get; set; }
/**
* Output internal C header file.
*/
public string? internal_header_filename { get; set; }
public bool use_header { get; set; }
/**
* Base directory used for header_filename in the VAPIs.
*/
public string? includedir { get; set; }
/**
* Output symbols file.
*/
public string? symbols_filename { get; set; }
/**
* Compile but do not link.
*/
public bool compile_only { get; set; }
/**
* Output filename.
*/
public string output { get; set; }
/**
* Base source directory.
*/
public string basedir { get; set; }
/**
* Code output directory.
*/
public string directory { get; set; }
/**
* List of directories where to find .vapi files.
*/
public string[] vapi_directories { get; set; default = {}; }
/**
* List of directories where to find .gir files.
*/
public string[] gir_directories { get; set; default = {}; }
/**
* List of directories where to find .metadata files for .gir files.
*/
public string[] metadata_directories { get; set; default = {}; }
/**
* Produce debug information.
*/
public bool debug { get; set; }
/**
* Optimization level.
*/
public int optlevel { get; set; }
/**
* Enable memory profiler.
*/
public bool mem_profiler { get; set; }
/**
* Specifies the optional module initialization method.
*/
public Method module_init_method { get; set; }
/**
* Keep temporary files produced by the compiler.
*/
public bool save_temps { get; set; }
public Profile profile { get; set; }
public bool verbose_mode { get; set; }
public bool version_header { get; set; }
public bool nostdpkg { get; set; }
public bool use_fast_vapi { get; set; }
/**
* Include comments in generated vapi.
*/
public bool vapi_comments { get; set; }
/**
* Returns true if the target version of glib is greater than or
* equal to the specified version.
*/
public bool require_glib_version (int major, int minor) {
return (target_glib_major > major) || (target_glib_major == major && target_glib_minor >= minor);
}
public bool save_csources {
get { return save_temps; }
}
public Report report { get; set; default = new Report ();}
public Method? entry_point { get; set; }
public string entry_point_name { get; set; }
public bool run_output { get; set; }
public string[] gresources { get; set; default = {}; }
public string[] gresources_directories { get; set; default = {}; }
private List<SourceFile> source_files = new ArrayList<SourceFile> ();
private List<string> c_source_files = new ArrayList<string> ();
private Namespace _root = new Namespace (null);
private List<string> packages = new ArrayList<string> (str_equal);
private Set<string> defines = new HashSet<string> (str_hash, str_equal);
static StaticPrivate context_stack_key = StaticPrivate ();
int target_glib_major;
int target_glib_minor;
/**
* The root namespace of the symbol tree.
*/
public Namespace root {
get { return _root; }
}
public SymbolResolver resolver { get; private set; }
public SemanticAnalyzer analyzer { get; private set; }
public FlowAnalyzer flow_analyzer { get; private set; }
/**
* The selected code generator.
*/
public CodeGenerator codegen { get; set; }
/**
* Mark attributes used by the compiler and report unused at the end.
*/
public UsedAttr used_attr { get; set; }
public CodeContext () {
add_default_defines ();
resolver = new SymbolResolver ();
analyzer = new SemanticAnalyzer ();
flow_analyzer = new FlowAnalyzer ();
used_attr = new UsedAttr ();
}
/**
* Return the topmost context from the context stack.
*/
public static CodeContext get () {
List<CodeContext>* context_stack = context_stack_key.get ();
return context_stack->get (context_stack->size - 1);
}
/**
* Push the specified context to the context stack.
*/
public static void push (CodeContext context) {
ArrayList<CodeContext>* context_stack = context_stack_key.get ();
if (context_stack == null) {
context_stack = new ArrayList<CodeContext> ();
context_stack_key.set (context_stack, null);
}
context_stack->add (context);
}
/**
* Remove the topmost context from the context stack.
*/
public static void pop () {
List<CodeContext>* context_stack = context_stack_key.get ();
context_stack->remove_at (context_stack->size - 1);
}
/**
* Returns a copy of the list of source files.
*
* @return list of source files
*/
public List<SourceFile> get_source_files () {
return source_files;
}
/**
* Returns a copy of the list of C source files.
*
* @return list of C source files
*/
public List<string> get_c_source_files () {
return c_source_files;
}
/**
* Adds the specified file to the list of source files.
*
* @param file a source file
*/
public void add_source_file (SourceFile file) {
source_files.add (file);
}
/**
* Adds the specified file to the list of C source files.
*
* @param file a C source file
*/
public void add_c_source_file (string file) {
c_source_files.add (file);
}
/**
* Returns a copy of the list of used packages.
*
* @return list of used packages
*/
public List<string> get_packages () {
return packages;
}
/**
* Returns whether the specified package is being used.
*
* @param pkg a package name
* @return true if the specified package is being used
*/
public bool has_package (string pkg) {
return packages.contains (pkg);
}
/**
* Adds the specified package to the list of used packages.
*
* @param pkg a package name
*/
public void add_package (string pkg) {
packages.add (pkg);
}
/**
* Pull the specified package into the context.
* The method is tolerant if the package has been already loaded.
*
* @param pkg a package name
* @return false if the package could not be loaded
*
*/
public bool add_external_package (string pkg) {
if (has_package (pkg)) {
// ignore multiple occurrences of the same package
return true;
}
// first try .vapi
var path = get_vapi_path (pkg);
if (path == null) {
// try with .gir
path = get_gir_path (pkg);
}
if (path == null) {
Report.error (null, "Package `%s' not found in specified Vala API directories or GObject-Introspection GIR directories".printf (pkg));
return false;
}
add_package (pkg);
add_source_file (new SourceFile (this, SourceFileType.PACKAGE, path));
if (verbose_mode) {
stdout.printf ("Loaded package `%s'\n", path);
}
var deps_filename = Path.build_path ("/", Path.get_dirname (path), pkg + ".deps");
if (!add_packages_from_file (deps_filename)) {
return false;
}
return true;
}
/**
* Read the given filename and pull in packages.
* The method is tolerant if the file does not exist.
*
* @param filename a filename
* @return false if an error occurs while reading the file or if a package could not be added
*/
public bool add_packages_from_file (string filename) {
if (!FileUtils.test (filename, FileTest.EXISTS)) {
return true;
}
try {
string contents;
FileUtils.get_contents (filename, out contents);
foreach (string package in contents.split ("\n")) {
package = package.strip ();
if (package != "") {
add_external_package (package);
}
}
} catch (FileError e) {
Report.error (null, "Unable to read dependency file: %s".printf (e.message));
return false;
}
return true;
}
/**
* Add the specified source file to the context. Only .vala, .vapi, .gs,
* and .c extensions are supported.
*
* @param filename a filename
* @param is_source true to force adding the file as .vala or .gs
* @param cmdline true if the file came from the command line.
* @return false if the file is not recognized or the file does not exist
*/
public bool add_source_filename (string filename, bool is_source = false, bool cmdline = false) {
if (!FileUtils.test (filename, FileTest.EXISTS)) {
Report.error (null, "%s not found".printf (filename));
return false;
}
var rpath = realpath (filename);
if (is_source || filename.has_suffix (".vala") || filename.has_suffix (".gs")) {
var source_file = new SourceFile (this, SourceFileType.SOURCE, rpath, null, cmdline);
source_file.relative_filename = filename;
if (profile == Profile.POSIX) {
// import the Posix namespace by default (namespace of backend-specific standard library)
var ns_ref = new UsingDirective (new UnresolvedSymbol (null, "Posix", null));
source_file.add_using_directive (ns_ref);
root.add_using_directive (ns_ref);
} else if (profile == Profile.GOBJECT) {
// import the GLib namespace by default (namespace of backend-specific standard library)
var ns_ref = new UsingDirective (new UnresolvedSymbol (null, "GLib", null));
source_file.add_using_directive (ns_ref);
root.add_using_directive (ns_ref);
}
add_source_file (source_file);
} else if (filename.has_suffix (".vapi") || filename.has_suffix (".gir")) {
var source_file = new SourceFile (this, SourceFileType.PACKAGE, rpath, null, cmdline);
source_file.relative_filename = filename;
add_source_file (source_file);
} else if (filename.has_suffix (".c")) {
add_c_source_file (rpath);
} else if (filename.has_suffix (".h")) {
/* Ignore */
} else {
Report.error (null, "%s is not a supported source file type. Only .vala, .vapi, .gs, and .c files are supported.".printf (filename));
return false;
}
return true;
}
/**
* Visits the complete code tree file by file.
* It is possible to add new source files while visiting the tree.
*
* @param visitor the visitor to be called when traversing
*/
public void accept (CodeVisitor visitor) {
root.accept (visitor);
// support queueing new source files
int index = 0;
while (index < source_files.size) {
var source_file = source_files[index];
source_file.accept (visitor);
index++;
}
}
/**
* Resolve and analyze.
*/
public void check () {
resolver.resolve (this);
if (report.get_errors () > 0) {
return;
}
analyzer.analyze (this);
if (report.get_errors () > 0) {
return;
}
flow_analyzer.analyze (this);
if (report.get_errors () > 0) {
return;
}
used_attr.check_unused (this);
}
public void add_define (string define) {
if (is_defined (define)) {
Report.warning (null, "`%s' is already defined".printf (define));
if (/VALA_0_\d+/.match_all (define)) {
Report.warning (null, "`VALA_0_XX' defines are automatically added up to current compiler version in use");
} else if (/GLIB_2_\d+/.match_all (define)) {
Report.warning (null, "`GLIB_2_XX' defines are automatically added up to targeted glib version");
}
}
defines.add (define);
}
public bool is_defined (string define) {
return (define in defines);
}
void add_default_defines () {
int api_major = 0;
int api_minor = 0;
if (API_VERSION.scanf ("%d.%d", out api_major, out api_minor) != 2
|| api_major > 0
|| api_minor % 2 != 0) {
Report.error (null, "Invalid format for Vala.API_VERSION");
return;
}
for (int i = 2; i <= api_minor; i += 2) {
defines.add ("VALA_0_%d".printf (i));
}
target_glib_major = 2;
target_glib_minor = 40;
for (int i = 16; i <= target_glib_minor; i += 2) {
defines.add ("GLIB_2_%d".printf (i));
}
}
/**
* Set the target version of glib for code generation.
*
* This may be called once or not at all.
*
* @param target_glib a string of the format "%d.%d"
*/
public void set_target_glib_version (string target_glib) {
int glib_major = target_glib_major;
int glib_minor = target_glib_minor;
if (target_glib != null && target_glib.scanf ("%d.%d", out glib_major, out glib_minor) != 2
|| glib_minor % 2 != 0) {
Report.error (null, "Invalid format or version for target GLib");
}
if (glib_major != 2) {
Report.error (null, "This version of valac only supports GLib 2");
}
if (glib_minor <= target_glib_minor) {
// no additional defines needed
return;
}
for (int i = target_glib_major + 2; i <= glib_minor; i += 2) {
defines.add ("GLIB_2_%d".printf (i));
}
target_glib_major = glib_minor;
target_glib_minor = glib_major;
}
public string? get_vapi_path (string pkg) {
var path = get_file_path (pkg + ".vapi", "vala" + Config.PACKAGE_SUFFIX + "/vapi", "vala/vapi", vapi_directories);
if (path == null) {
/* last chance: try the package compiled-in vapi dir */
var filename = Path.build_path ("/", Config.PACKAGE_DATADIR, "vapi", pkg + ".vapi");
if (FileUtils.test (filename, FileTest.EXISTS)) {
path = filename;
}
}
return path;
}
public string? get_gir_path (string gir) {
return get_file_path (gir + ".gir", "gir-1.0", null, gir_directories);
}
public string? get_gresource_path (string gresource, string resource) {
var filename = get_file_path (resource, null, null, { Path.get_dirname (gresource) });
if (filename == null) {
filename = get_file_path (resource, null, null, gresources_directories);
}
return filename;
}
/*
* Returns the .metadata file associated with the given .gir file.
*/
public string? get_metadata_path (string gir_filename) {
var basename = Path.get_basename (gir_filename);
var metadata_basename = "%s.metadata".printf (basename.substring (0, basename.length - ".gir".length));
// look into metadata directories
var metadata_filename = get_file_path (metadata_basename, null, null, metadata_directories);
if (metadata_filename != null) {
return metadata_filename;
}
// look into the same directory of .gir
metadata_filename = Path.build_path ("/", Path.get_dirname (gir_filename), metadata_basename);
if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
return metadata_filename;
}
return null;
}
string? get_file_path (string basename, string? versioned_data_dir, string? data_dir, string[] directories) {
string filename = null;
if (directories != null) {
foreach (unowned string dir in directories) {
filename = Path.build_path ("/", dir, basename);
if (FileUtils.test (filename, FileTest.EXISTS)) {
return filename;
}
}
}
if (data_dir != null) {
foreach (unowned string dir in Environment.get_system_data_dirs ()) {
filename = Path.build_path ("/", dir, data_dir, basename);
if (FileUtils.test (filename, FileTest.EXISTS)) {
return filename;
}
}
}
if (versioned_data_dir != null) {
foreach (unowned string dir in Environment.get_system_data_dirs ()) {
filename = Path.build_path ("/", dir, versioned_data_dir, basename);
if (FileUtils.test (filename, FileTest.EXISTS)) {
return filename;
}
}
}
return null;
}
public void write_dependencies (string filename) {
var stream = FileStream.open (filename, "w");
if (stream == null) {
Report.error (null, "unable to open `%s' for writing".printf (filename));
return;
}
stream.printf ("%s:", filename);
foreach (var src in source_files) {
if (src.file_type == SourceFileType.FAST && src.used) {
stream.printf (" %s", src.filename);
}
}
stream.printf ("\n\n");
}
private static bool ends_with_dir_separator (string s) {
return Path.is_dir_separator (s.get_char (s.length - 1));
}
/**
* Returns canonicalized absolute pathname
* ported from glibc
*
* @param name the path being checked
* @return a canonicalized absolute pathname
*/
public static string realpath (string name) {
string rpath;
// start of path component
weak string start;
// end of path component
weak string end;
if (!Path.is_absolute (name)) {
// relative path
rpath = Environment.get_current_dir ();
start = end = name;
} else {
// set start after root
start = end = Path.skip_root (name);
// extract root
rpath = name.substring (0, (int) ((char*) start - (char*) name));
}
long root_len = (long) ((char*) Path.skip_root (rpath) - (char*) rpath);
for (; start.get_char () != 0; start = end) {
// skip sequence of multiple path-separators
while (Path.is_dir_separator (start.get_char ())) {
start = start.next_char ();
}
// find end of path component
long len = 0;
for (end = start; end.get_char () != 0 && !Path.is_dir_separator (end.get_char ()); end = end.next_char ()) {
len++;
}
if (len == 0) {
break;
} else if (len == 1 && start.get_char () == '.') {
// do nothing
} else if (len == 2 && start.has_prefix ("..")) {
// back up to previous component, ignore if at root already
if (rpath.length > root_len) {
do {
rpath = rpath.substring (0, rpath.length - 1);
} while (!ends_with_dir_separator (rpath));
}
} else {
if (!ends_with_dir_separator (rpath)) {
rpath += Path.DIR_SEPARATOR_S;
}
// don't use len, substring works on bytes
rpath += start.substring (0, (long)((char*)end - (char*)start));
}
}
if (rpath.length > root_len && ends_with_dir_separator (rpath)) {
rpath = rpath.substring (0, rpath.length - 1);
}
if (Path.DIR_SEPARATOR != '/') {
// don't use backslashes internally,
// to avoid problems in #include directives
string[] components = rpath.split ("\\");
rpath = string.joinv ("/", components);
}
return rpath;
}
public bool pkg_config_exists (string package_name) {
string pc = pkg_config_command + " --exists " + package_name;
int exit_status;
try {
Process.spawn_command_line_sync (pc, null, null, out exit_status);
return (0 == exit_status);
} catch (SpawnError e) {
Report.error (null, e.message);
return false;
}
}
public string? pkg_config_modversion (string package_name) {
string pc = pkg_config_command + " --silence-errors --modversion " + package_name;
string? output = null;
int exit_status;
try {
Process.spawn_command_line_sync (pc, out output, null, out exit_status);
if (exit_status != 0) {
output = output[0:-1];
if (output == "") {
output = null;
}
}
} catch (SpawnError e) {
output = null;
}
return output;
}
public string? pkg_config_compile_flags (string package_name) {
string pc = pkg_config_command + " --cflags";
if (!compile_only) {
pc += " --libs";
}
pc += package_name;
string? output = null;
int exit_status;
try {
Process.spawn_command_line_sync (pc, out output, null, out exit_status);
if (exit_status != 0) {
Report.error (null, "%s exited with status %d".printf (pkg_config_command, exit_status));
return null;
}
} catch (SpawnError e) {
Report.error (null, e.message);
output = null;
}
return output;
}
}
|