Appearance
| 1 | namespace Source is | |
| 2 | use Collections | |
| 3 | use IO.Path | |
| 4 | ||
| 5 | // A source library declared on the command line with --library-source: | |
| 6 | // an identity - a name and an opaque version string, compared for | |
| 7 | // equality only - and the directory root its files live under, as a | |
| 8 | // full path. Which files under the root are the library's is the | |
| 9 | // caller's concern: the compiler is only ever handed the files to | |
| 10 | // compile, and uses the root to say which library each one belongs to. | |
| 11 | class SOURCE_LIBRARY(name: string public, version: string public, root: string public) is | |
| 12 | // Whether a full path names one of this library's files: it lies | |
| 13 | // under the root, below it rather than beside it. | |
| 14 | contains(full_path: string) -> bool => is_under(full_path, root) | |
| 15 | ||
| 16 | // Whether one path lies under another, below it rather than beside | |
| 17 | // it. The boundary has to be a separator, so a sibling directory | |
| 18 | // whose name extends the root's (/lib beside /library) is not | |
| 19 | // under it. | |
| 20 | is_under(path: string, root: string) -> bool static is | |
| 21 | // A root is not under itself: there is no next character to be | |
| 22 | // the separator | |
| 23 | if path.length <= root.length \/ !path.starts_with(root) then | |
| 24 | return false | |
| 25 | fi | |
| 26 | ||
| 27 | // A root carries a trailing separator only when it is a | |
| 28 | // filesystem root itself ("/", "C:\"), where everything after | |
| 29 | // it is below it | |
| 30 | if ends_with_separator(root) then | |
| 31 | return path.length > root.length | |
| 32 | fi | |
| 33 | ||
| 34 | let rest = path.substring(root.length) | |
| 35 | ||
| 36 | return is_separator(rest[0]) | |
| 37 | si | |
| 38 | ||
| 39 | // Whether a path ends in a directory separator | |
| 40 | ends_with_separator(path: string) -> bool static => path.length > | |
| 41 | 0 /\ | |
| 42 | is_separator(path[path.length - 1]) | |
| 43 | ||
| 44 | // Either separator .NET spells a directory boundary with: the | |
| 45 | // alternate is the same character as the primary on Linux, and a | |
| 46 | // forward slash on Windows, where both spell one path | |
| 47 | is_separator(c: char) -> bool static => c =~ | |
| 48 | Path.directory_separator_char \/ | |
| 49 | c =~ Path.alt_directory_separator_char | |
| 50 | si | |
| 51 | ||
| 52 | // The source libraries declared with --library-source, in declaration | |
| 53 | // order. Answers which library a source file belongs to, and applies | |
| 54 | // the include-once rule: a library reached through two dependency | |
| 55 | // paths is declared twice, and only the files under its first root are | |
| 56 | // compiled - compiling both would define its every symbol twice. | |
| 57 | class SOURCE_LIBRARIES is | |
| 58 | // One entry per declaration: the library as declared, and whether | |
| 59 | // its root is the one the library is compiled from | |
| 60 | _declarations: LIST[(library: SOURCE_LIBRARY, is_included: bool)] | |
| 61 | ||
| 62 | init() is | |
| 63 | _declarations = LIST[(library: SOURCE_LIBRARY, is_included: bool)]() | |
| 64 | si | |
| 65 | ||
| 66 | // Records one declaration, resolving the root to a full path. | |
| 67 | // Answers the text of an error when the declaration conflicts with | |
| 68 | // one already recorded - the same library name at another version, | |
| 69 | // or a root that overlaps another declaration's - and records | |
| 70 | // nothing. Null means the declaration stands, as the one a library | |
| 71 | // is compiled from or as a later root of one already included. | |
| 72 | add(name: string, version: string, root: string) -> string? is | |
| 73 | let full_root = Path.get_full_path(root) | |
| 74 | ||
| 75 | let seen_name mut = false | |
| 76 | ||
| 77 | for declaration in _declarations do | |
| 78 | let other = declaration.library | |
| 79 | ||
| 80 | if other.name =~ name then | |
| 81 | if other.version !~ version then | |
| 82 | return "library {name} included at versions {other.version} and {version}" | |
| 83 | fi | |
| 84 | ||
| 85 | // The same declaration written twice records nothing | |
| 86 | // the second time. Any conflict between this root and | |
| 87 | // another declaration's was reported when the other | |
| 88 | // was added. | |
| 89 | if other.root =~ full_root then | |
| 90 | return null | |
| 91 | fi | |
| 92 | ||
| 93 | seen_name = true | |
| 94 | fi | |
| 95 | ||
| 96 | // Another library at the very same root overlaps it as | |
| 97 | // surely as one nested inside it | |
| 98 | if other.root =~ full_root \/ | |
| 99 | SOURCE_LIBRARY.is_under(full_root, other.root) \/ | |
| 100 | SOURCE_LIBRARY.is_under(other.root, full_root) then | |
| 101 | return "library roots {other.root} and {full_root} overlap" | |
| 102 | fi | |
| 103 | od | |
| 104 | ||
| 105 | _declarations.add( | |
| 106 | (library = SOURCE_LIBRARY(name, version, full_root), is_included = !seen_name) | |
| 107 | ) | |
| 108 | ||
| 109 | return null | |
| 110 | si | |
| 111 | ||
| 112 | // The library a full source path belongs to, or null when the file | |
| 113 | // is the program's own rather than a library's. Roots never | |
| 114 | // overlap, so at most one declaration's root contains it. | |
| 115 | library_for(full_path: string) -> SOURCE_LIBRARY? is | |
| 116 | for declaration in _declarations do | |
| 117 | if declaration.library.contains(full_path) then | |
| 118 | return declaration.library | |
| 119 | fi | |
| 120 | od | |
| 121 | ||
| 122 | return null | |
| 123 | si | |
| 124 | ||
| 125 | // Whether the file `file_name` can declare what the compiler gives | |
| 126 | // special meaning to: a file of one of the libraries, or any file | |
| 127 | // where the build declares none and the program is its own core. | |
| 128 | can_declare_core(file_name: string) -> bool => | |
| 129 | _declarations.count == 0 \/ library_for(Path.get_full_path(file_name))? | |
| 130 | ||
| 131 | // Whether a full source path sits under a root the build does not | |
| 132 | // compile from: its library was already declared under an earlier | |
| 133 | // root, and the file is skipped rather than compiled again. | |
| 134 | is_skipped(full_path: string) -> bool is | |
| 135 | for declaration in _declarations do | |
| 136 | if declaration.library.contains(full_path) then | |
| 137 | return !declaration.is_included | |
| 138 | fi | |
| 139 | od | |
| 140 | ||
| 141 | return false | |
| 142 | si | |
| 143 | si | |
| 144 | si |