Skip to content

Commit 7a2f4bf

Browse files
authored
Skc: option to canonize paths in generated ll (#801)
The checked in bootstrap `.ll.gz` files contain absolute paths that only make sense for the person building them. Paths are found in `!DIFile` directives (both absolute CWD `directory` and absolute file path `filename`) and other positions (e.g. those found in messages for unreachableMethodCall calls). This PR: - makes `!DIFile`'s `filename` relative to the `directory`, _all the time_, from what I could find, this seems legit - makes all file names relative to `CWD`, **if** `--canonize-paths` is given Hence, to compile `skc` with canonized paths, use `skargo build --skcopt --canonize-paths`
2 parents f535e35 + 11a8041 commit 7a2f4bf

File tree

7 files changed

+88
-17
lines changed

7 files changed

+88
-17
lines changed

skiplang/compiler/Skargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ test-harness = "SkcTests.main"
88
std = { path = "../prelude" }
99
cli = { path = "../cli" }
1010
arparser = { path = "../arparser" }
11+
12+
[dev-dependencies]
1113
sktest = { path = "../sktest" }
1214

1315
[[bin]]

skiplang/compiler/src/AsmOutput.sk

+7-3
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ fun writeOutputFiles(
10211021
// arbitrarily pick some unique input file as the CU for this output file.
10221022
metadata = AsmOutput.Metadata::create(
10231023
defs,
1024+
config.cwd,
10241025
if (!inputFiles.isEmpty()) {
10251026
inputFiles[0]
10261027
} else {
@@ -1645,6 +1646,8 @@ mutable class Metadata{
16451646
// List of LLVM metadata
16461647
metadata: mutable Vector<String> = mutable Vector[],
16471648

1649+
cwd: Config.WorkingDirectory,
1650+
16481651
diFileCache: mutable UnorderedMap<Filename, DIFile> = mutable UnorderedMap[],
16491652

16501653
// TODO: Right now we only support a single generic subroutine type. Once we
@@ -1672,9 +1675,10 @@ mutable class Metadata{
16721675
} {
16731676
static fun create(
16741677
defs: readonly AsmDefIDToAsmDef,
1678+
cwd: Config.WorkingDirectory,
16751679
compilationUnit: String,
16761680
): mutable Metadata {
1677-
metadata = mutable Metadata{defs};
1681+
metadata = mutable Metadata{defs, cwd};
16781682

16791683
// Reserve the slot for kEmptyBlockIndex since we may have already
16801684
// handed it out.
@@ -1714,8 +1718,8 @@ mutable class Metadata{
17141718
str = mutable TextOutputStream.StringTextOutputStream{};
17151719
str.printf2(
17161720
"!DIFile(filename: %r, directory: %r)",
1717-
filename,
1718-
getcwd(),
1721+
this.cwd.makeRelative(filename),
1722+
this.cwd.toString(),
17191723
);
17201724
str.toString()
17211725
}),

skiplang/compiler/src/Config.sk

+40
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ private class Config{
6363
static_libraries: Array<String>,
6464
link_args: Array<String>,
6565
preambles: Array<String>,
66+
canonize_paths: Bool,
67+
cwd: WorkingDirectory,
6668
} {
6769
static fun make(results: Cli.ParseResults): this {
6870
optConfig = Optimize.Config::make(
@@ -213,6 +215,8 @@ private class Config{
213215
skcPreamble.split(Path.listSeparator).toArray()
214216
| _ -> Array[]
215217
};
218+
canonize_paths = results.getBool("canonize-paths");
219+
cwd = WorkingDirectory::create(canonize_paths);
216220
static{
217221
release,
218222
verbose,
@@ -248,6 +252,8 @@ private class Config{
248252
static_libraries,
249253
link_args,
250254
preambles,
255+
cwd,
256+
canonize_paths,
251257
}
252258
}
253259

@@ -256,4 +262,38 @@ private class Config{
256262
}
257263
}
258264

265+
value class WorkingDirectory{
266+
private abs_cwd: String,
267+
private rel_cwd: ?String,
268+
} {
269+
static fun create(anonymize: Bool): this {
270+
abs_cwd = getcwd();
271+
rel_cwd = if (anonymize) {
272+
Some(
273+
{
274+
repo = abs_cwd;
275+
while (!FileSystem.exists(Path.join(repo, ".git"))) {
276+
if (Path.isRoot(repo)) {
277+
break Path.currentDirectory
278+
};
279+
!repo = Path.parentname(repo);
280+
} else {
281+
Path.relativeTo{path => abs_cwd, base => repo}
282+
}
283+
},
284+
)
285+
} else {
286+
None()
287+
};
288+
static{abs_cwd, rel_cwd}
289+
}
290+
fun toString(): String {
291+
this.rel_cwd.default(this.abs_cwd)
292+
}
293+
/* Makes a path relative to this working directory. */
294+
fun makeRelative(path: String): String {
295+
Path.relativeTo{path, base => this.abs_cwd}
296+
}
297+
}
298+
259299
module end;

skiplang/compiler/src/compile.sk

+9
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,20 @@ fun compile(
358358
Array[TickConfigFile((context.tick.value, config))],
359359
);
360360

361+
make_input_source_path = (pkg_base_dir, src_path) ~> {
362+
res = Path.join(pkg_base_dir, src_path);
363+
if (config.canonize_paths) {
364+
!res = config.cwd.makeRelative(res);
365+
};
366+
res
367+
};
368+
361369
FileCache.writeFiles(
362370
context,
363371
fileNames,
364372
config.dependencies,
365373
config.lib_name,
374+
make_input_source_path,
366375
);
367376

368377
context.update()

skiplang/compiler/src/main.sk

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fun main(): void {
4949
// These are handled in skipUtils.sk
5050
// Just allow them to be ignored here
5151
.arg(Cli.Arg::bool("profile"))
52+
.arg(Cli.Arg::bool("canonize-paths").negatable())
5253
.arg(Cli.Arg::string("data"))
5354
.arg(Cli.Arg::string("init"))
5455
.arg(Cli.Arg::bool("check").about("Run the front end only."))

skiplang/compiler/src/skipParse.sk

+29-9
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ fun updatePackageFiles(
111111
name: ?String,
112112
pkg: InputPackageFiles,
113113
get_file_contents: String ~> String,
114+
make_input_source_path: (String, String) ~> String,
114115
): void {
115116
(modified_files, removed_files) = packageDir.unsafeMaybeGet(
116117
context,
@@ -122,14 +123,14 @@ fun updatePackageFiles(
122123
for ((pkg_base_dir, src_path) in modified_files) {
123124
fileDir.writeArray(
124125
context,
125-
InputSource(name, Path.join(pkg_base_dir, src_path)),
126+
InputSource(name, make_input_source_path(pkg_base_dir, src_path)),
126127
Array[SKStore.StringFile(get_file_contents(src_path))],
127128
)
128129
};
129130
for ((pkg_base_dir, src_path) in removed_files) {
130131
fileDir.writeArray(
131132
context,
132-
InputSource(name, Path.join(pkg_base_dir, src_path)),
133+
InputSource(name, make_input_source_path(pkg_base_dir, src_path)),
133134
Array[SKStore.StringFile("")],
134135
)
135136
};
@@ -141,6 +142,7 @@ fun writeFiles(
141142
file_names: Array<String>,
142143
dependencies: Map<String, (String, Sklib.Metadata)>,
143144
lib_name_opt: ?String,
145+
make_input_source_path: (String, String) ~> String,
144146
): void {
145147
// TODO: If `lib_name_opt` is `Some(_)`, ensure there is a
146148
// `Skargo.toml` in the cwd.
@@ -153,9 +155,15 @@ fun writeFiles(
153155
.map(fn -> (fn, FileSystem.getLastModificationTime(fn)))
154156
.collect(Array),
155157
);
156-
updatePackageFiles(context, lib_name_opt, pkg, src ~> {
157-
FileSystem.readTextFile(src)
158-
})
158+
updatePackageFiles(
159+
context,
160+
lib_name_opt,
161+
pkg,
162+
src ~> {
163+
FileSystem.readTextFile(src)
164+
},
165+
make_input_source_path,
166+
)
159167
};
160168

161169
// For each (transitive) dependency, invalidate files that were
@@ -172,14 +180,26 @@ fun writeFiles(
172180
dep_meta.pkg_dir,
173181
dep_meta.sources.map(s -> (s.i0, s.i1)).collect(Array),
174182
);
175-
updatePackageFiles(context, Some(dep_name), pkg, src ~> {
176-
dep_meta.sources.find(f -> f.i0 == src).fromSome().i2
177-
})
183+
updatePackageFiles(
184+
context,
185+
Some(dep_name),
186+
pkg,
187+
src ~> {
188+
dep_meta.sources.find(f -> f.i0 == src).fromSome().i2
189+
},
190+
make_input_source_path,
191+
)
178192
};
179193

180194
if (lib_name_opt is Some _) {
181195
// Invalidate non-package source files when building a package.
182-
updatePackageFiles(context, None(), InputPackageFiles("", Array[]), _ ~> "")
196+
updatePackageFiles(
197+
context,
198+
None(),
199+
InputPackageFiles("", Array[]),
200+
_ ~> "",
201+
make_input_source_path,
202+
)
183203
}
184204
}
185205

skiplang/compiler/src/syntaxError.sk

-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,4 @@ fun create(
3737
}
3838
}
3939

40-
// TODO: Remove this ...
41-
fun errorsToString(errors: Array<SyntaxError>): String {
42-
"Errors\n" + errors.map(error ~> error.toString()).join("\n") + "\n"
43-
}
44-
4540
module end;

0 commit comments

Comments
 (0)