Skip to content

Commit daa7fad

Browse files
committed
cli: Make --got-layout-file override the default GOT layout file location
1 parent db1140f commit daa7fad

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

compiler/plc_driver/src/cli.rs

Lines changed: 9 additions & 5 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",
@@ -405,8 +408,9 @@ impl CompileParameters {
405408
self.hardware_config.as_deref().and_then(get_config_format)
406409
}
407410

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

412416
/// 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
274274
context,
275275
compile_options.root.as_deref(),
276276
&unit.file_name,
277-
compile_options.got_layout_file.clone().zip(compile_options.got_layout_format),
277+
(compile_options.got_layout_file.clone(), compile_options.got_layout_format),
278278
compile_options.optimization,
279279
compile_options.debug_level,
280280
compile_options.online_change,

src/codegen.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub struct CodeGen<'ink> {
7373
/// Whether we are generating a hot-reloadable binary or not
7474
pub online_change: OnlineChange,
7575

76-
pub got_layout_file: Option<(String, ConfigFormat)>,
76+
pub got_layout_file: (String, ConfigFormat),
7777

7878
pub module_location: String,
7979
}
@@ -92,7 +92,7 @@ impl<'ink> CodeGen<'ink> {
9292
context: &'ink CodegenContext,
9393
root: Option<&Path>,
9494
module_location: &str,
95-
got_layout_file: Option<(String, ConfigFormat)>,
95+
got_layout_file: (String, ConfigFormat),
9696
optimization_level: OptimizationLevel,
9797
debug_level: DebugLevel,
9898
online_change: OnlineChange,
@@ -137,6 +137,7 @@ impl<'ink> CodeGen<'ink> {
137137
&index,
138138
&mut self.debug,
139139
self.got_layout_file.clone(),
140+
self.online_change,
140141
);
141142

142143
//Generate global variables

src/codegen/generators/variable_generator.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
codegen::{debug::Debug, llvm_index::LlvmTypedIndex, llvm_typesystem::cast_if_needed},
66
index::{get_initializer_name, Index, PouIndexEntry, VariableIndexEntry},
77
resolver::{AnnotationMap, AstAnnotations, Dependency},
8-
ConfigFormat,
8+
ConfigFormat, OnlineChange,
99
};
1010
use indexmap::IndexSet;
1111
use inkwell::{module::Module, types::BasicTypeEnum, values::GlobalValue};
@@ -65,7 +65,8 @@ pub struct VariableGenerator<'ctx, 'b> {
6565
annotations: &'b AstAnnotations,
6666
types_index: &'b LlvmTypedIndex<'ctx>,
6767
debug: &'b mut DebugBuilderEnum<'ctx>,
68-
got_layout_file: Option<(String, ConfigFormat)>,
68+
got_layout_file: (String, ConfigFormat),
69+
online_change: OnlineChange,
6970
}
7071

7172
impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
@@ -76,9 +77,19 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
7677
annotations: &'b AstAnnotations,
7778
types_index: &'b LlvmTypedIndex<'ctx>,
7879
debug: &'b mut DebugBuilderEnum<'ctx>,
79-
got_layout_file: Option<(String, ConfigFormat)>,
80+
got_layout_file: (String, ConfigFormat),
81+
online_change: OnlineChange,
8082
) -> Self {
81-
VariableGenerator { module, llvm, global_index, annotations, types_index, debug, got_layout_file }
83+
VariableGenerator {
84+
module,
85+
llvm,
86+
global_index,
87+
annotations,
88+
types_index,
89+
debug,
90+
got_layout_file,
91+
online_change,
92+
}
8293
}
8394

8495
pub fn generate_global_variables(
@@ -140,7 +151,9 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
140151
);
141152
}
142153

143-
if let Some((location, format)) = &self.got_layout_file {
154+
if self.online_change == OnlineChange::Enabled {
155+
let (location, format) = &self.got_layout_file;
156+
144157
let got_entries = read_got_layout(location.as_str(), *format)?;
145158
let mut new_globals = Vec::new();
146159
let mut new_got_entries = HashMap::new();

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)