Skip to content

Commit

Permalink
Merge pull request #3 from ElrondNetwork/move-v2-from-original-repo
Browse files Browse the repository at this point in the history
Move files of build-contract-rust:frozen-002 from elrond-sdk-images
  • Loading branch information
andreibancioiu authored Oct 11, 2022
2 parents 9baa5f2 + c665f9e commit 4f89a15
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 121 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
ignore = E501
11 changes: 9 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ jobs:
echo "Image not yet published. Will publish."
fi
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to Docker Hub
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
uses: docker/build-push-action@v3
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
file: ./Dockerfile
tags: elrondnetwork/build-contract-rust:${{ github.ref_name }}
20 changes: 6 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM ubuntu:20.04
FROM ubuntu:22.04

# Constants
ARG VERSION_RUST="nightly-2022-08-23"
ARG VERSION_BINARYEN="version_105"
ARG VERSION_WABT="1.0.27"
ARG VERSION_BINARYEN="105-1"
ARG VERSION_WABT="1.0.27-1"

RUN apt-get update && apt-get install wget -y
RUN apt-get update && apt-get install python3.8 python-is-python3 -y
RUN apt-get update && apt-get install python3.10 python-is-python3 -y
RUN apt-get update && apt-get install build-essential -y

# Install rust
Expand All @@ -16,17 +16,10 @@ RUN wget -O rustup.sh https://sh.rustup.rs && \
rm rustup.sh

# Install wasm-opt
RUN wget -O binaryen.tar.gz https://github.com/WebAssembly/binaryen/releases/download/${VERSION_BINARYEN}/binaryen-${VERSION_BINARYEN}-x86_64-linux.tar.gz && \
tar -xf binaryen.tar.gz && mv binaryen-${VERSION_BINARYEN}/bin/wasm-opt /usr/bin && \
rm binaryen.tar.gz && \
rm -rf binaryen-${VERSION_BINARYEN}
RUN apt-get update && apt-get install binaryen=${VERSION_BINARYEN}

# Install wabt
RUN wget -O wabt.tar.gz https://github.com/WebAssembly/wabt/releases/download/${VERSION_WABT}/wabt-${VERSION_WABT}-ubuntu.tar.gz && \
tar -xf wabt.tar.gz && mv wabt-${VERSION_WABT}/bin/wasm2wat /usr/bin && mv wabt-${VERSION_WABT}/bin/wasm-objdump /usr/bin && \
rm wabt.tar.gz && \
rm -rf wabt-${VERSION_WABT}

RUN apt-get update && apt-get install wabt=${VERSION_WABT}

COPY "./build_within_docker.py" "/build.py"

Expand All @@ -47,4 +40,3 @@ LABEL frozen="yes"
LABEL rust=${VERSION_RUST}
LABEL wasm-opt-binaryen=${VERSION_BINARYEN}
LABEL wabt=${VERSION_WABT}

14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Docker image (and wrappers) for reproducible contract builds (Rust).
## Build the Docker image

```
docker image build --no-cache . -t build-contract-rust:experimental -f ./Dockerfile
docker buildx build --no-cache . -t build-contract-rust:experimental -f ./Dockerfile
```

## Build contract using the wrapper
Expand Down Expand Up @@ -33,7 +33,6 @@ This is useful for useful for testing, debugging and reviewing the script.

```
export PROJECT=${HOME}/contracts/reproducible-contract-build-example
export PROJECT_ARCHIVE=${HOME}/contracts/adder.tar
export OUTPUT=${HOME}/contracts/output
export CARGO_TARGET_DIR=${HOME}/cargo-target-dir
export OWNER_ID=$(id -u)
Expand All @@ -43,20 +42,11 @@ export RUSTUP_HOME=${HOME}/elrondsdk/vendor-rust
export CARGO_HOME=${HOME}/elrondsdk/vendor-rust
```

Build a project directory:
Build a project:

```
python3 ./build_within_docker.py --project=${PROJECT} --output=${OUTPUT} \
--cargo-target-dir=${CARGO_TARGET_DIR} \
--output-owner-id=${OWNER_ID} \
--output-group-id=${GROUP_ID}
```

Build a project archive:

```
python3 ./build_within_docker.py --project=${PROJECT_ARCHIVE} --output=${OUTPUT} \
--cargo-target-dir=${CARGO_TARGET_DIR} \
--output-owner-id=${OWNER_ID} \
--output-group-id=${GROUP_ID}
```
31 changes: 19 additions & 12 deletions build_with_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,50 @@ def main(cli_args: List[str]):

parser = ArgumentParser()
parser.add_argument("--image", type=str, required=True)
parser.add_argument("--no-docker-interactive", action="store_true", default=False)
parser.add_argument("--no-docker-tty", action="store_true", default=False)
parser.add_argument("--project", type=str)
parser.add_argument("--output", type=str,
default=Path(os.getcwd()) / "output")
parser.add_argument("--output", type=str, default=Path(os.getcwd()) / "output")
# Providing this parameter
# (a) *might* (should, but it doesn't) speed up (subsequent) builds, but
# (b) *might* (with a *very low* probability) break build determinism.
# As of September 2022, both (a) and (b) are still open points.
parser.add_argument("--cargo-target-dir", type=str)
parser.add_argument("--no-wasm-opt", action="store_true", default=False,
help="do not optimize wasm files after the build (default: %(default)s)")
parser.add_argument("--no-wasm-opt", action="store_true", default=False, help="do not optimize wasm files after the build (default: %(default)s)")

parsed_args = parser.parse_args(cli_args)
image = parsed_args.image
docker_interactive = not parsed_args.no_docker_interactive
docker_tty = not parsed_args.no_docker_tty
project_path = Path(parsed_args.project).expanduser().resolve()
output_path = Path(parsed_args.output).expanduser().resolve()
cargo_target_dir = Path(parsed_args.cargo_target_dir).expanduser(
).resolve() if parsed_args.cargo_target_dir else None
cargo_target_dir = Path(parsed_args.cargo_target_dir).expanduser().resolve() if parsed_args.cargo_target_dir else None
no_wasm_opt = parsed_args.no_wasm_opt

output_path.mkdir(parents=True, exist_ok=True)

return_code = run_docker(
image, project_path, output_path, cargo_target_dir, no_wasm_opt)
return_code = run_docker(image, docker_interactive, docker_tty, project_path, output_path, cargo_target_dir, no_wasm_opt)
return return_code


def run_docker(image: str, project_path: Path, output_path: Path, cargo_target_dir: Union[Path, None], no_wasm_opt: bool):
def run_docker(image: str, docker_interactive: bool, docker_tty: bool, project_path: Path, output_path: Path, cargo_target_dir: Union[Path, None], no_wasm_opt: bool):
docker_mount_args = [
"--mount", f"type=bind,source={project_path},destination=/project",
"--mount", f"type=bind,source={output_path},destination=/output"
]

if cargo_target_dir:
docker_mount_args += ["--mount",
f"type=bind,source={cargo_target_dir},destination=/cargo-target-dir"]
docker_mount_args += ["--mount", f"type=bind,source={cargo_target_dir},destination=/cargo-target-dir"]

docker_args = ["docker", "run"] + docker_mount_args + ["--rm", image]
docker_args = ["docker", "run"]

if docker_interactive:
docker_args += ["--interactive"]
if docker_tty:
docker_args += ["--tty"]

docker_args += docker_mount_args
docker_args += ["--rm", image]

entrypoint_args = [
"--output-owner-id", str(os.getuid()),
Expand Down
Loading

0 comments on commit 4f89a15

Please sign in to comment.