Skip to content

Commit 8d8e94e

Browse files
committed
cli: Make --got-layout-file override the default GOT layout file location
1 parent d4adaee commit 8d8e94e

File tree

6 files changed

+44
-29
lines changed

6 files changed

+44
-29
lines changed

compiler/plc_driver/src/cli.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use encoding_rs::Encoding;
55
use plc_diagnostics::diagnostics::{diagnostics_registry::DiagnosticsConfiguration, Diagnostic};
66
use std::{env, ffi::OsStr, num::ParseIntError, path::PathBuf};
77

8-
use plc::{output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat, Target, Threads};
8+
use plc::output::FormatOption;
9+
use plc::{ConfigFormat, DebugLevel, ErrorFormat, Target, Threads, DEFAULT_GOT_LAYOUT_FILE};
910

1011
pub type ParameterError = clap::Error;
1112

@@ -116,9 +117,11 @@ pub struct CompileParameters {
116117
Save information about the generated custom GOT layout to the given file.
117118
Format is detected by extension.
118119
Supported formats : json, toml",
119-
parse(try_from_str = validate_config)
120+
default_value = DEFAULT_GOT_LAYOUT_FILE,
121+
parse(try_from_str = validate_config),
122+
requires = "online-change"
120123
) ]
121-
pub got_layout_file: Option<String>,
124+
pub got_layout_file: String,
122125

123126
#[clap(
124127
name = "optimization",
@@ -335,7 +338,7 @@ pub fn get_config_format(name: &str) -> Option<ConfigFormat> {
335338
impl CompileParameters {
336339
pub fn parse<T: AsRef<OsStr> + AsRef<str>>(args: &[T]) -> Result<CompileParameters, ParameterError> {
337340
CompileParameters::try_parse_from(args).and_then(|mut result| {
338-
result.got_layout_file = Some(String::from("tmp.json"));
341+
result.got_layout_file = String::from("tmp.json");
339342

340343
if result.sysroot.len() > result.target.len() {
341344
let mut cmd = CompileParameters::command();
@@ -407,8 +410,9 @@ impl CompileParameters {
407410
self.hardware_config.as_deref().and_then(get_config_format)
408411
}
409412

410-
pub fn got_layout_format(&self) -> Option<ConfigFormat> {
411-
self.got_layout_file.as_deref().and_then(get_config_format)
413+
pub fn got_layout_format(&self) -> ConfigFormat {
414+
// It is safe to unwrap here, since the provided argument to `--got-online-change` has been checked with `validate_config`
415+
get_config_format(&self.got_layout_file).unwrap()
412416
}
413417

414418
/// Returns the location where the build artifacts should be stored / output

compiler/plc_driver/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cli::{CompileParameters, ParameterError, SubCommands};
2020
use pipelines::AnnotatedProject;
2121
use plc::{
2222
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat,
23-
OnlineChange, OptimizationLevel, Target, Threads,
23+
OnlineChange, OptimizationLevel, Target, Threads, DEFAULT_GOT_LAYOUT_FILE,
2424
};
2525

2626
use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
@@ -50,8 +50,8 @@ pub struct CompileOptions {
5050
/// The name of the resulting compiled file
5151
pub output: String,
5252
pub output_format: FormatOption,
53-
pub got_layout_file: Option<String>,
54-
pub got_layout_format: Option<ConfigFormat>,
53+
pub got_layout_file: String,
54+
pub got_layout_format: ConfigFormat,
5555
pub optimization: OptimizationLevel,
5656
pub error_format: ErrorFormat,
5757
pub debug_level: DebugLevel,
@@ -66,8 +66,8 @@ impl Default for CompileOptions {
6666
build_location: None,
6767
output: String::new(),
6868
output_format: Default::default(),
69-
got_layout_file: None,
70-
got_layout_format: None,
69+
got_layout_file: String::from(DEFAULT_GOT_LAYOUT_FILE),
70+
got_layout_format: ConfigFormat::JSON,
7171
optimization: OptimizationLevel::None,
7272
error_format: ErrorFormat::None,
7373
debug_level: DebugLevel::None,

compiler/plc_driver/src/pipelines.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,13 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
321321
unit: &CompilationUnit,
322322
dependencies: &FxIndexSet<Dependency>,
323323
literals: &StringLiterals,
324-
got_layout: &Mutex<Option<HashMap<String, u64>>>,
324+
got_layout: &Mutex<HashMap<String, u64>>,
325325
) -> Result<GeneratedModule<'ctx>, Diagnostic> {
326326
let mut code_generator = plc::codegen::CodeGen::new(
327327
context,
328328
compile_options.root.as_deref(),
329329
&unit.file_name,
330-
compile_options.got_layout_file.clone().zip(compile_options.got_layout_format),
330+
(compile_options.got_layout_file.clone(), compile_options.got_layout_format),
331331
compile_options.optimization,
332332
compile_options.debug_level,
333333
compile_options.online_change,
@@ -388,12 +388,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
388388
ensure_compile_dirs(targets, &compile_directory)?;
389389
let targets = if targets.is_empty() { &[Target::System] } else { targets };
390390

391-
let got_layout = compile_options
392-
.got_layout_file
393-
.as_ref()
394-
.map(|path| read_got_layout(path, ConfigFormat::JSON))
395-
.transpose()?;
396-
391+
let got_layout = read_got_layout(&compile_options.got_layout_file, ConfigFormat::JSON)?;
397392
let got_layout = Mutex::new(got_layout);
398393

399394
let res = targets
@@ -456,9 +451,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
456451
})
457452
.collect::<Result<Vec<_>, Diagnostic>>()?;
458453

459-
compile_options.got_layout_file.as_ref().map(|path| {
460-
write_got_layout(got_layout.into_inner().unwrap().unwrap(), path, ConfigFormat::JSON)
461-
});
454+
write_got_layout(got_layout.into_inner().unwrap(), &compile_options.got_layout_file, ConfigFormat::JSON)?;
462455

463456
Ok(res)
464457
}

src/codegen.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct CodeGen<'ink> {
7878
/// Whether we are generating a hot-reloadable binary or not
7979
pub online_change: OnlineChange,
8080

81-
pub got_layout_file: Option<(String, ConfigFormat)>,
81+
pub got_layout_file: (String, ConfigFormat),
8282

8383
pub module_location: String,
8484
}
@@ -97,7 +97,7 @@ impl<'ink> CodeGen<'ink> {
9797
context: &'ink CodegenContext,
9898
root: Option<&Path>,
9999
module_location: &str,
100-
got_layout_file: Option<(String, ConfigFormat)>,
100+
got_layout_file: (String, ConfigFormat),
101101
optimization_level: OptimizationLevel,
102102
debug_level: DebugLevel,
103103
online_change: OnlineChange,
@@ -121,7 +121,7 @@ impl<'ink> CodeGen<'ink> {
121121
literals: &StringLiterals,
122122
dependencies: &FxIndexSet<Dependency>,
123123
global_index: &Index,
124-
got_layout: &Mutex<Option<HashMap<String, u64>>>,
124+
got_layout: &Mutex<HashMap<String, u64>>,
125125
) -> Result<LlvmTypedIndex<'ink>, Diagnostic> {
126126
let llvm = Llvm::new(context, context.create_builder());
127127
let mut index = LlvmTypedIndex::default();
@@ -135,8 +135,14 @@ impl<'ink> CodeGen<'ink> {
135135
)?;
136136
index.merge(llvm_type_index);
137137

138-
let mut variable_generator =
139-
VariableGenerator::new(&self.module, &llvm, global_index, annotations, &index, &mut self.debug);
138+
let mut variable_generator = VariableGenerator::new(
139+
&self.module,
140+
&llvm,
141+
global_index,
142+
annotations,
143+
&index,
144+
&mut self.debug,
145+
);
140146

141147
//Generate global variables
142148
let llvm_gv_index =
@@ -175,7 +181,10 @@ impl<'ink> CodeGen<'ink> {
175181
acc
176182
});
177183

178-
if let Some(got_entries) = &mut *got_layout.lock().unwrap() {
184+
185+
if self.online_change == OnlineChange::Enabled {
186+
let got_entries = &mut *got_layout.lock().unwrap();
187+
179188
let mut new_symbols = Vec::new();
180189
let mut new_got_entries = HashMap::new();
181190
let mut new_got = HashMap::new();

src/codegen/generators/variable_generator.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
3838
types_index: &'b LlvmTypedIndex<'ctx>,
3939
debug: &'b mut DebugBuilderEnum<'ctx>,
4040
) -> Self {
41-
VariableGenerator { module, llvm, global_index, annotations, types_index, debug }
41+
VariableGenerator {
42+
module,
43+
llvm,
44+
global_index,
45+
annotations,
46+
types_index,
47+
debug,
48+
}
4249
}
4350

4451
pub fn generate_global_variables(

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ impl FromStr for ConfigFormat {
143143
}
144144
}
145145

146+
pub const DEFAULT_GOT_LAYOUT_FILE: &str = "online_change_got.json";
147+
146148
#[derive(Debug, Copy, Clone, PartialEq, Eq, ArgEnum, Serialize, Deserialize, Default)]
147149
pub enum ErrorFormat {
148150
#[default]

0 commit comments

Comments
 (0)