Skip to content

Commit c1ae464

Browse files
Support ABI cfg flag in codegen (#2501)
* Use `ink_abi` `cfg` flag for ABI detection * Remove `abi` config arg from ink! contract attribute macro * tests: Update integration tests for `sol` ABI * chore: Derive `Copy` for ABI enum * ci: Use `cargo contract test` for contract integration tests * Update `contract-build` version * tests: Update ui tests for `sol` and `all` ABI * Update changelog * docs: Remove `abi` config arg from ink! contract attribute macro * docs: `trybuild` wrapper motivation * docs: lint suppression * fix: Remove deprecated `--skip-linting` flag from contract build calls * Update `Cargo.lock` * ci: separate job for Solidity ABI integration tests * tests: move Solidity ABI integration tests and refactor CI * Reuse `rustc` wrapper across contract builds in e2e tests
1 parent 5265209 commit c1ae464

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1009
-369
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,11 @@ jobs:
556556
with:
557557
# Run all tests with --all-features, which will run the `e2e-tests` feature if present.
558558
command: |
559-
scripts/for_all_contracts_exec.sh --path integration-tests --ignore internal/static-buffer --ignore internal/mapping \
560-
--ignore public/debugging-strategies --ignore public/wildcard-selector --partition ${{ matrix.partition }}/4 -- \
561-
cargo test --all-features --manifest-path {}
559+
scripts/for_all_contracts_exec.sh --path integration-tests --ignore internal/static-buffer \
560+
--ignore internal/mapping --ignore public/debugging-strategies --ignore public/wildcard-selector \
561+
--ignore solidity-abi/sol-cross-contract --ignore solidity-abi/sol-encoding \
562+
--ignore solidity-abi/solidity-calls-flipper --partition ${{ matrix.partition }}/4 -- \
563+
cargo contract test --all-features --manifest-path {}
562564
563565
examples-test-mapping:
564566
runs-on: ubuntu-latest
@@ -585,7 +587,7 @@ jobs:
585587
INK_STATIC_BUFFER_SIZE: 256
586588
with:
587589
command: |
588-
cargo test --all-features --manifest-path integration-tests/internal/mapping/Cargo.toml
590+
cargo contract test --all-features --manifest-path integration-tests/internal/mapping/Cargo.toml
589591
590592
# TODO: (@davidsemakula) Add back to `examples-test` job when flakiness is fixed.
591593
examples-test-debugging-strategies:
@@ -608,7 +610,7 @@ jobs:
608610
uses: ./.github/run-container-command
609611
with:
610612
command: |
611-
cargo test --all-features --manifest-path integration-tests/public/debugging-strategies/Cargo.toml
613+
cargo contract test --all-features --manifest-path integration-tests/public/debugging-strategies/Cargo.toml
612614
613615
614616
# TODO: (@davidsemakula) Add back to `examples-test` job when flakiness is fixed.
@@ -632,7 +634,7 @@ jobs:
632634
uses: ./.github/run-container-command
633635
with:
634636
command: |
635-
cargo test --all-features --manifest-path integration-tests/public/wildcard-selector/Cargo.toml
637+
cargo contract test --all-features --manifest-path integration-tests/public/wildcard-selector/Cargo.toml
636638
637639
examples-custom-test:
638640
runs-on: ubuntu-latest
@@ -666,7 +668,7 @@ jobs:
666668
--manifest-path integration-tests/public/flipper/Cargo.toml \
667669
--suri //Alice --args true -x -y --output-json | \
668670
jq -r .contract | xargs subkey inspect | grep -o "0x.*" | head -n1)
669-
CONTRACTS_NODE_URL=ws://127.0.0.1:9944 cargo test \
671+
CONTRACTS_NODE_URL=ws://127.0.0.1:9944 cargo contract test \
670672
--features e2e-tests \
671673
--manifest-path integration-tests/public/flipper/Cargo.toml \
672674
e2e_test_deployed_contract \
@@ -684,7 +686,32 @@ jobs:
684686
# the readme + test comments explain why `32`, in short because the tests write
685687
# content into the buffer and want to provoke an exhaustion of the buffer.
686688
cargo clean --manifest-path integration-tests/internal/static-buffer/Cargo.toml
687-
INK_STATIC_BUFFER_SIZE=32 cargo test --manifest-path integration-tests/internal/static-buffer/Cargo.toml --all-features
689+
INK_STATIC_BUFFER_SIZE=32 cargo contract test --manifest-path integration-tests/internal/static-buffer/Cargo.toml --all-features
690+
691+
# NOTE: Sharing build artifacts (i.e. via a shared cargo target directory) with other tests
692+
# that don't have the `ink_abi="sol"` `cfg` option set makes the Solidity ABI tests fail.
693+
examples-test-solidity-abi:
694+
runs-on: ubuntu-latest
695+
needs: ci-image
696+
steps:
697+
- name: Checkout
698+
uses: actions/checkout@v4
699+
with:
700+
fetch-depth: 1
701+
702+
- name: Initialize runner
703+
uses: ./.github/init
704+
with:
705+
cache: true
706+
cache-directories: ${{ github.workspace }}/${{ env.CARGO_TARGET_DIR }}
707+
cache-on-failure: true
708+
709+
- name: Test Solidity ABI Examples
710+
uses: ./.github/run-container-command
711+
with:
712+
command: |
713+
scripts/for_all_contracts_exec.sh --path integration-tests/solidity-abi -- \
714+
cargo contract test --all-features --manifest-path {}
688715
689716
examples-contract-build-riscv:
690717
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
[Unreleased]
88

9+
### Added
10+
- Support ABI `cfg` flag in codegen - [#2501](https://github.com/use-ink/ink/pull/2501)
11+
912
### Changed
1013

1114
- Use marker trait for finding ink! storage `struct` during code analysis - [2499](https://github.com/use-ink/ink/pull/2499)

Cargo.lock

Lines changed: 14 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ array-init = { version = "2.0", default-features = false }
3838
blake2 = { version = "0.10" }
3939
cargo_metadata = { version = "0.19.0" }
4040
cfg-if = { version = "1.0" }
41-
contract-build = { version = "6.0.0-alpha" }
41+
contract-build = { version = "6.0.0-alpha.1", git = "https://github.com/use-ink/cargo-contract", branch = "master" }
4242
darling = { version = "0.20.11" }
4343
derive_more = { version = "2.0.1", default-features = false }
4444
either = { version = "1.13", default-features = false }

crates/e2e/src/contract_build.rs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::log_info;
16-
use contract_build::{
17-
BuildArtifacts,
18-
BuildMode,
19-
ExecuteArgs,
20-
Features,
21-
ImageVariant,
22-
ManifestPath,
23-
MetadataSpec,
24-
Network,
25-
OutputType,
26-
UnstableFlags,
27-
Verbosity,
28-
};
29-
use itertools::Itertools;
3015
use std::{
3116
collections::{
3217
hash_map::Entry,
@@ -43,13 +28,43 @@ use std::{
4328
},
4429
};
4530

31+
use contract_build::{
32+
package_abi,
33+
util::rustc_wrapper,
34+
Abi,
35+
BuildArtifacts,
36+
BuildMode,
37+
ExecuteArgs,
38+
Features,
39+
ImageVariant,
40+
ManifestPath,
41+
MetadataSpec,
42+
Network,
43+
OutputType,
44+
UnstableFlags,
45+
Verbosity,
46+
};
47+
use itertools::Itertools;
48+
49+
use crate::log_info;
50+
4651
/// Builds the "root" contract (the contract in which the E2E tests are defined) together
4752
/// with any contracts which are a dependency of the root contract.
4853
///
4954
/// todo explain features
5055
pub fn build_root_and_contract_dependencies(features: Vec<String>) -> Vec<PathBuf> {
5156
let contract_project = ContractProject::new();
5257
let contract_manifests = contract_project.root_with_contract_dependencies();
58+
if contract_project.package_abi.is_some() {
59+
// Generates a custom `rustc` wrapper which passes compiler flags to `rustc`,
60+
// because `cargo` doesn't pass compiler flags to proc macros and build
61+
// scripts when the `--target` flag is set.
62+
// See `contract_build::util::rustc_wrapper::generate` docs for details.
63+
if let Ok(rustc_wrapper) = rustc_wrapper::generate(&contract_project.target_dir) {
64+
// SAFETY: The `rustc` wrapper is safe to reuse across all threads.
65+
env::set_var("INK_RUSTC_WRAPPER", rustc_wrapper);
66+
}
67+
}
5368
build_contracts(&contract_manifests, features)
5469
}
5570

@@ -58,15 +73,18 @@ pub fn build_root_and_contract_dependencies(features: Vec<String>) -> Vec<PathBu
5873
struct ContractProject {
5974
root_package: Option<PathBuf>,
6075
contract_dependencies: Vec<PathBuf>,
76+
package_abi: Option<Abi>,
77+
target_dir: PathBuf,
6178
}
6279

6380
impl ContractProject {
6481
fn new() -> Self {
6582
let mut cmd = cargo_metadata::MetadataCommand::new();
66-
if let Some(target_dir) = env::var_os("CARGO_TARGET_DIR")
67-
.filter(|target_dir| Path::new(&target_dir).is_absolute())
68-
{
69-
cmd.env("CARGO_TARGET_DIR", &target_dir);
83+
let env_target_dir = env::var_os("CARGO_TARGET_DIR")
84+
.map(|target_dir| PathBuf::from(target_dir))
85+
.filter(|target_dir| target_dir.is_absolute());
86+
if let Some(target_dir) = env_target_dir.as_ref() {
87+
cmd.env("CARGO_TARGET_DIR", target_dir);
7088
}
7189
let metadata = cmd
7290
.exec()
@@ -102,14 +120,26 @@ impl ContractProject {
102120
.iter()
103121
.filter_map(maybe_contract_package)
104122
.collect();
105-
106123
log_info(&format!(
107124
"found those contract dependencies: {:?}",
108125
contract_dependencies
109126
));
127+
128+
let package_abi = metadata
129+
.root_package()
130+
.and_then(|package| package_abi(package))
131+
.and_then(Result::ok);
132+
log_info(&format!("found root package abi: {:?}", package_abi));
133+
134+
let target_dir = env_target_dir
135+
.unwrap_or_else(|| metadata.target_directory.into_std_path_buf());
136+
log_info(&format!("found target dir: {:?}", target_dir));
137+
110138
Self {
111139
root_package,
112140
contract_dependencies,
141+
package_abi,
142+
target_dir,
113143
}
114144
}
115145

crates/ink/codegen/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ std = [
4848
"derive_more/std"
4949
]
5050

51-
# For the ui tests, which use this `Cargo.toml`
5251
[lints.rust.unexpected_cfgs]
5352
level = "warn"
5453
check-cfg = [
54+
# For the ui tests, which use this `Cargo.toml`
5555
'cfg(feature, values(any()))',
56+
# For ABI declaration.
57+
'cfg(ink_abi, values("ink", "sol", "all"))'
5658
]

0 commit comments

Comments
 (0)