2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
9 public class TestCompiler : Vala.CodeVisitor {
11 Vala.CodeContext context;
13 public TestCompiler(string file) {
19 public void dumpCode(string str) {
20 var ls = str.split("\n");
21 for (var i=0;i < ls.length; i++) {
22 print("%d : %s\n", i+1, ls[i]);
29 public Gee.HashMap<int,string> checkString( )
32 var valac = "valac " ;
34 context = new Vala.CodeContext ();
35 Vala.CodeContext.push (context);
37 context.experimental = false;
38 context.experimental_non_null = false;
50 for (int i = 2; i <= ver; i += 2) {
51 context.add_define ("VALA_0_%d".printf (i));
62 // or context.get_vapi_path("glib-2.0"); // should return path..
63 //context.vapi_directories = vapidirs;
64 context.report.enable_warnings = true;
65 context.metadata_directories = { };
66 context.gir_directories = {};
67 context.thread = true;
70 this.report = new ValaSourceReport(this.file);
71 context.report = this.report;
74 context.basedir = "/tmp"; //Posix.realpath (".");
76 context.directory = context.basedir;
79 // add default packages:
80 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
81 context.profile = Vala.Profile.GOBJECT;
83 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
84 context.root.add_using_directive (ns_ref);
86 var source_file = new Vala.SourceFile (
88 Vala.SourceFileType.SOURCE,
91 source_file.add_using_directive (ns_ref);
92 context.add_source_file (source_file);
94 // add all the files (except the current one) - this.file.path
95 var pr = ((Project.Gtk)this.file.project);
96 if (this.file.build_module.length > 0) {
97 var cg = pr.compilegroups.get(this.file.build_module);
98 for (var i = 0; i < cg.sources.size; i++) {
99 var path = pr.resolve_path(
100 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
102 if (!FileUtils.test(path, FileTest.EXISTS)) {
106 if (path == this.file.path.replace(".bjs", ".vala")) {
110 if (FileUtils.test(path, FileTest.IS_DIR)) {
113 //print("Add source file %s\n", path);
117 if (Regex.match_simple("\\.c$", path)) {
118 context.add_c_source_file(path);
123 var xsf = new Vala.SourceFile (
125 Vala.SourceFileType.SOURCE,
128 xsf.add_using_directive (ns_ref);
129 context.add_source_file(xsf);
133 // default.. packages..
134 context.add_external_package ("glib-2.0");
135 context.add_external_package ("gobject-2.0");
136 // user defined ones..
138 var dcg = pr.compilegroups.get("_default_");
139 for (var i = 0; i < dcg.packages.size; i++) {
140 valac += " --pkg " + dcg.packages.get(i);
141 context.add_external_package (dcg.packages.get(i));
144 //Vala.Config.PACKAGE_SUFFIX.substring (1)
146 // add the modules...
150 //context.add_external_package ("libvala-0.24");
152 this.report.compile_notice("START", "", 0, "");
155 //add_documented_files (context, settings.source_files);
157 Vala.Parser parser = new Vala.Parser ();
158 parser.parse (context);
159 //gir_parser.parse (context);
160 if (context.report.get_errors () > 0) {
161 print("parse got errors");
162 ((ValaSourceReport)context.report).dump();
164 Vala.CodeContext.pop ();
165 this.report.compile_notice("END", "", 0, "");
166 return this.report.line_errors;
173 if (context.report.get_errors () > 0) {
174 print("check got errors");
175 ((ValaSourceReport)context.report).dump();
176 Vala.CodeContext.pop ();
177 this.report.compile_notice("END", "", 0, "");
178 return this.report.line_errors;
182 //context.codegen = new Vala.GDBusServerModule ();
185 context.output = "/tmp/testbuild";
186 valac += " -o " +context.output;
187 //context.codegen.emit (context);
189 var ccompiler = new Vala.CCodeCompiler ();
190 var cc_command = Environment.get_variable ("CC");
191 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
193 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
195 ccompiler.compile (context, cc_command, new string[] { });
199 Vala.CodeContext.pop ();
200 //(new Vala.CodeNode()).get_error_types().clear();
201 //(new Vala.NullType()).get_type_arguments().clear();
202 (new Vala.NullType(null)).get_type_arguments().clear();
204 this.report.compile_notice("END", "", 0, "");
205 print("%s\n", valac);
207 return this.report.line_errors;
216 int main (string[] args) {
218 var a = new ValaSource(file);
219 a.create_valac_tree();