Skip to content

Commit

Permalink
feat: update config file load path
Browse files Browse the repository at this point in the history
Signed-off-by: h1994st <[email protected]>
  • Loading branch information
h1994st committed Dec 28, 2023
1 parent 3ec8cdb commit b3c2251
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.manifest
.*.o
!.gitignore
config.toml

# Created by https://www.toptal.com/developers/gitignore/api/rust,macos,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,macos,visualstudiocode
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ llvm-dis hello.bc # Obtain readable `hello.ll` file

### Configuration

The default configuration file `~/.rllvm/config.toml` will be automatically created, if it does not exist, with the following entries:
Users can specify the configuration file by setting the environment variable `RLLVM_CONFIG`.

```bash
export RLLVM_CONFIG=/absolute/path/to/config/file.toml
```

Otherwise, the default configuration file `~/.rllvm/config.toml` will be used. The configuration file will be automatically created, if it does not exist, with the following entries:

| Configuration Key | Required? | Notes |
| -------------------------- | --------- | --------------------------------------------------------------------------------------- |
Expand Down
17 changes: 14 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use log::Level;
use serde::{Deserialize, Serialize};

use crate::{
constants::DEFAULT_CONF_FILEPATH_UNDER_HOME,
constants::{
DEFAULT_CONF_FILEPATH_UNDER_HOME, DEFAULT_RLLVM_CONF_FILEPATH_ENV_NAME, HOME_ENV_NAME,
},
utils::{execute_llvm_config, find_llvm_config},
};

Expand Down Expand Up @@ -109,8 +111,17 @@ impl RLLVMConfig {

impl RLLVMConfig {
pub fn new() -> Self {
let config_filepath = PathBuf::from(env::var("HOME").unwrap_or("".into()))
.join(DEFAULT_CONF_FILEPATH_UNDER_HOME);
let config_filepath = env::var(DEFAULT_RLLVM_CONF_FILEPATH_ENV_NAME).map_or_else(
|_| {
// Default config file
PathBuf::from(env::var(HOME_ENV_NAME).unwrap_or("".into()))
.join(DEFAULT_CONF_FILEPATH_UNDER_HOME)
},
|x| {
// User-defined config file
PathBuf::from(x)
},
);
Self::load_path(config_filepath)
}

Expand Down
4 changes: 4 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub const DARWIN_SEGMENT_NAME: &str = "__RLLVM";
pub const DARWIN_SECTION_NAME: &str = "__llvm_bc";
pub const ELF_SECTION_NAME: &str = ".llvm_bc";

/// Environment variables
pub const DEFAULT_RLLVM_CONF_FILEPATH_ENV_NAME: &str = "RLLVM_CONFIG";
pub const HOME_ENV_NAME: &str = "HOME";

/// The default filepath of the configuration file
pub const DEFAULT_CONF_FILEPATH_UNDER_HOME: &str = ".rllvm/config.toml";

Expand Down

0 comments on commit b3c2251

Please sign in to comment.