Skip to content

Commit

Permalink
Document features (#1257)
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Krawczyk <71193923+fmkra@users.noreply.github.com>
Co-authored-by: maciektr <mtratnowiecki@gmail.com>
  • Loading branch information
fmkra and maciektr authored Apr 18, 2024
1 parent 3e49198 commit 57a6e93
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
61 changes: 61 additions & 0 deletions website/docs/reference/conditional-compilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,64 @@ Example values:

- `'lib'`
- `'starknet-contract'`

## Features

Features in Scarb provide a way to conditionally compile specific parts of the code during the build process.

### `[features]` section

A package defines a set of named features in the `[features]` section of `Scarb.toml` file. Each defined feature can list other features that should be enabled with it.

For example, a package supporting various hash functions might define features like this:

```toml
[features]
poseidon = []
pedersen = []
keccak = []
```

With these features set, conditional compilation (`cfg`) attributes can be used to selectively include code to support requested features during compile time. For instance:

```rust
// Conditionally include a module
#[cfg(feature: 'poseidon')]
mod poseidon;

// Conditionally define a function
#[cfg(feature: 'pedersen')]
fn hash_pedersen(value: felt252) -> felt252 {
// ...
}
```

To enable specific features, use the `--features` flag followed by a comma-separated list of features. For example, to build with only the `poseidon` and `pedersen` features enabled:

```
scarb build --features poseidon,pedersen
```

Enabling all features can be done with the `--all-features` flag.

### `default` features

By default, all features are disabled unless explicitly enabled. However, this behaviour can be changed by specifying a default feature in the `[features]` section, like so:

```toml
[features]
default = ["poseidon", "pedersen"]
poseidon = []
pedersen = []
keccak = []
```

When the package is built, the default feature is enabled which in turn enables the listed features.

To disable the default feature, use the `--no-default-features` flag.

For example, in the provided scenario:

- Running `scarb build` would enable `poseidon` and `pedersen` features.
- `scarb build --features keccak` would enable `poseidon`, `pedersen`, and `keccak` features.
- `scarb build --no-default-features --features keccak` would enable only the `keccak` feature.
4 changes: 4 additions & 0 deletions website/docs/reference/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,7 @@ Each field can accept any valid toml value including a table.
## `[workspace]`

See [Workspaces](./workspaces) page.

## `[features]`

See [Features](./conditional-compilation#features) page.

0 comments on commit 57a6e93

Please sign in to comment.