Skip to content

Commit bad1ced

Browse files
authored
feat: make output sharing optional (#687)
2 parents 750a910 + 9d42ed2 commit bad1ced

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- [rspfile_content](./reference/context/rule/rspfile_content.md)
4242
- [pool](./reference/context/rule/pool.md)
4343
- [always](./reference/context/rule/always.md)
44+
- [shareable](./reference/context/rule/shareable.md)
4445
- [tasks]()
4546
- [cmd]()
4647
- [build]()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# shareable
2+
3+
This boolean flag controls output sharing. It defaults to `true`.
4+
See [object sharing][object-sharing] for details.
5+
6+
When enabled for a rule, this rule's output files will end up in a location
7+
that is context-independent:
8+
9+
${build-dir}/objects/<filestem>.<hash>.<ext>
10+
11+
E.g., a source file `some-module/foo.c` would end up in `${build-dir}/objects/some-module/foo.<hash>.o`.
12+
13+
When disabled, the output will end up in a builder and app specific location:
14+
15+
${bindir}/<file-path>/foo.o
16+
17+
The variable `${bindir}` defaults to `${build-dir}/out/${builder}/${app}`, so
18+
e.g., a source file `some-module/foo.c` built for builder `host` and app `bar`
19+
would end up in `${bindir}/out/host/bar/some-module/foo.o`.
20+
21+
Example:
22+
23+
```yaml
24+
context:
25+
- # ...
26+
rules:
27+
- name: MYRULE
28+
shareable: false # disable object sharing for this rule
29+
# ... possible other fields
30+
```
31+
32+
[object-sharing]: ../../../concepts/object_sharing.md

book/src/reference/context/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ contexts:
2929
- [`rspfile_content`](./rule/rspfile_content.md)
3030
- [`pool`](./rule/pool.md)
3131
- [`always`](./rule/always.md)
32+
- [`shareable`](./rule/shareable.md)

src/data.rs

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ pub struct YamlRule {
233233
#[serde(default = "default_as_false")]
234234
pub always: bool,
235235

236+
#[serde(default = "default_as_true", alias = "sharable")]
237+
pub shareable: bool,
238+
236239
#[serde(rename = "meta")]
237240
_meta: Option<Value>,
238241
}
@@ -253,6 +256,7 @@ impl From<YamlRule> for Rule {
253256
gcc_deps: yaml_rule.gcc_deps,
254257
rspfile: yaml_rule.rspfile,
255258
rspfile_content: yaml_rule.rspfile_content,
259+
shareable: yaml_rule.shareable,
256260
pool: yaml_rule.pool,
257261
description: yaml_rule.description,
258262
export: yaml_rule

src/generate.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -866,13 +866,23 @@ fn configure_build(
866866
let rule_hash = ninja_rule.get_hash(None);
867867

868868
// 3. determine output path (e.g., name of C object file)
869-
let out = srcpath.with_extension(format!(
870-
"{}.{}",
871-
rule_hash ^ build_deps_hash,
872-
&rule.out.as_ref().unwrap()
873-
));
869+
let out_ext = if rule.shareable {
870+
&format!(
871+
"{}.{}",
872+
rule_hash ^ build_deps_hash,
873+
&rule.out.as_ref().unwrap()
874+
)
875+
} else {
876+
rule.out.as_ref().unwrap()
877+
};
878+
879+
let out = srcpath.with_extension(out_ext);
874880

875881
let mut object = objdir.clone();
882+
if !rule.shareable {
883+
object.push(&builder.name);
884+
object.push(&binary.name);
885+
}
876886
object.push(out);
877887

878888
// 4. render ninja "build:" snippet and add to this build's

src/model/rule.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher};
55

66
use serde::{Deserialize, Serialize};
77

8-
use crate::serde_bool_helpers::default_as_false;
8+
use crate::serde_bool_helpers::{default_as_false, default_as_true};
99

1010
#[derive(Debug, Serialize, Deserialize, Eq, Clone)]
1111
#[serde(deny_unknown_fields)]
@@ -29,6 +29,8 @@ pub struct Rule {
2929

3030
#[serde(default = "default_as_false")]
3131
pub always: bool,
32+
#[serde(default = "default_as_true")]
33+
pub shareable: bool,
3234
}
3335

3436
impl Rule {

0 commit comments

Comments
 (0)