Skip to content

Commit

Permalink
fix(linker): Avoid filename clashes when linking (PLC-lang#1086)
Browse files Browse the repository at this point in the history
Append .o extension to end of file name instead of replacing previous extension
  • Loading branch information
volsa authored Feb 9, 2024
1 parent fdd22b4 commit 4aa3aec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl AnnotatedProject {
let current_dir = env::current_dir()?;
let current_dir = compile_options.root.as_deref().unwrap_or(&current_dir);
let unit_location = PathBuf::from(&unit.file_name);
let unit_location = std::fs::canonicalize(unit_location)?;
let unit_location = fs::canonicalize(unit_location)?;
let output_name = if unit_location.starts_with(current_dir) {
unit_location.strip_prefix(current_dir).map_err(|it| {
Diagnostic::error(format!(
Expand All @@ -329,9 +329,9 @@ impl AnnotatedProject {
};

let output_name = match compile_options.output_format {
FormatOption::IR => output_name.with_extension("ll"),
FormatOption::Bitcode => output_name.with_extension("bc"),
_ => output_name.with_extension("o"),
FormatOption::IR => format!("{}.ll", output_name.to_string_lossy()),
FormatOption::Bitcode => format!("{}.bc", output_name.to_string_lossy()),
_ => format!("{}.o", output_name.to_string_lossy()),
};

let context = CodegenContext::create(); //Create a build location for the generated object files
Expand All @@ -340,7 +340,7 @@ impl AnnotatedProject {
module
.persist(
Some(&compile_directory),
&output_name.to_string_lossy(),
&output_name,
compile_options.output_format,
target,
compile_options.optimization,
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions tests/integration/data/linking/consts.st
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
VAR_GLOBAL CONSTANT
myValue : BOOL := TRUE;
END_VAR

FUNCTION main : DINT
END_FUNCTION
12 changes: 12 additions & 0 deletions tests/integration/linking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,15 @@ fn link_files_with_same_name() {
//Delete it
fs::remove_file(&out1).unwrap();
}

#[test]
fn link_files_with_same_name_but_different_extension() {
let file1 = get_test_file("linking/consts.st");
let file2 = get_test_file("linking/consts.dt");

// We want to make sure that generating object files for two or more files with the same name but different
// extensions works. Previously this would fail because both `const.st` and `const.dt` would persist to a
// `const.o` file, which causes linking issues and more specifically "duplicate symbol" errors. Hence we only
// check whether the compilation resulted in some Ok value here.
assert!(compile(&["plc", file1.as_str(), file2.as_str(), "--target", TARGET.unwrap()]).is_ok());
}

0 comments on commit 4aa3aec

Please sign in to comment.