From: Luca Bruno Date: Mon, 23 Aug 2010 16:21:31 +0000 (+0200) Subject: Move source file adding logic to CodeContext X-Git-Tag: 0.11.0~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a4e5ac478be74988afa411b60300bb0d3aaa5dfa;p=thirdparty%2Fvala.git Move source file adding logic to CodeContext --- diff --git a/compiler/valacompiler.vala b/compiler/valacompiler.vala index 90182279f..33cfc1949 100644 --- a/compiler/valacompiler.vala +++ b/compiler/valacompiler.vala @@ -238,12 +238,12 @@ class Vala.Compiler { context.includedir = includedir; context.output = output; if (basedir == null) { - context.basedir = realpath ("."); + context.basedir = CodeContext.realpath ("."); } else { - context.basedir = realpath (basedir); + context.basedir = CodeContext.realpath (basedir); } if (directory != null) { - context.directory = realpath (directory); + context.directory = CodeContext.realpath (directory); } else { context.directory = context.basedir; } @@ -337,7 +337,7 @@ class Vala.Compiler { if (fast_vapis != null) { foreach (string vapi in fast_vapis) { - var rpath = realpath (vapi); + var rpath = CodeContext.realpath (vapi); var source_file = new SourceFile (context, SourceFileType.FAST, rpath); context.add_source_file (source_file); } @@ -365,43 +365,11 @@ class Vala.Compiler { bool has_c_files = false; foreach (string source in sources) { - if (FileUtils.test (source, FileTest.EXISTS)) { - var rpath = realpath (source); - if (run_output || source.has_suffix (".vala") || source.has_suffix (".gs")) { - var source_file = new SourceFile (context, SourceFileType.SOURCE, rpath); - source_file.relative_filename = source; - - if (context.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); - context.root.add_using_directive (ns_ref); - } else if (context.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); - context.root.add_using_directive (ns_ref); - } else if (context.profile == Profile.DOVA) { - // import the Dova namespace by default (namespace of backend-specific standard library) - var ns_ref = new UsingDirective (new UnresolvedSymbol (null, "Dova", null)); - source_file.add_using_directive (ns_ref); - context.root.add_using_directive (ns_ref); - } - - context.add_source_file (source_file); - } else if (source.has_suffix (".vapi") || source.has_suffix (".gir")) { - var source_file = new SourceFile (context, SourceFileType.PACKAGE, rpath); - source_file.relative_filename = source; - - context.add_source_file (source_file); - } else if (source.has_suffix (".c")) { - context.add_c_source_file (rpath); - has_c_files = true; - } else { - Report.error (null, "%s is not a supported source file type. Only .vala, .vapi, .gs, and .c files are supported.".printf (source)); - } - } else { - Report.error (null, "%s not found".printf (source)); + if (!context.add_source_filename (source, run_output)) { + break; + } + if (source.has_suffix (".c")) { + has_c_files = true; } } sources = null; @@ -566,80 +534,6 @@ class Vala.Compiler { return quit (); } - private static bool ends_with_dir_separator (string s) { - return Path.is_dir_separator (s.offset (s.length - 1).get_char ()); - } - - /* ported from glibc */ - private 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, name.pointer_to_offset (start)); - } - - long root_len = rpath.pointer_to_offset (Path.skip_root (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; - } - - rpath += start.substring (0, len); - } - } - - 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; - } - static int run_source (string[] args) { int i = 1; if (args[i] != null && args[i].has_prefix ("-")) { diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala index 38e01e32d..0dbcf0b95 100644 --- a/vala/valacodecontext.vala +++ b/vala/valacodecontext.vala @@ -293,6 +293,58 @@ public class Vala.CodeContext { packages.add (pkg); } + /** + * 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 + * @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) { + 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); + 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); + } else if (profile == Profile.DOVA) { + // import the Dova namespace by default (namespace of backend-specific standard library) + var ns_ref = new UsingDirective (new UnresolvedSymbol (null, "Dova", 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); + source_file.relative_filename = filename; + + add_source_file (source_file); + } else if (filename.has_suffix (".c")) { + add_c_source_file (rpath); + } 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. * @@ -379,4 +431,78 @@ public class Vala.CodeContext { } stream.printf ("\n\n"); } + + private static bool ends_with_dir_separator (string s) { + return Path.is_dir_separator (s.offset (s.length - 1).get_char ()); + } + + /* ported from glibc */ + 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, name.pointer_to_offset (start)); + } + + long root_len = rpath.pointer_to_offset (Path.skip_root (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; + } + + rpath += start.substring (0, len); + } + } + + 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; + } }