Skip to content

Commit

Permalink
Cairo v0.11.0 (pre).
Browse files Browse the repository at this point in the history
  • Loading branch information
liorgold2 committed Feb 22, 2023
1 parent de741b9 commit 12ca9e9
Show file tree
Hide file tree
Showing 312 changed files with 57,680 additions and 24,257 deletions.
12 changes: 12 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Put all output in build directory.
build --symlink_prefix=build/bazel

# Don't inherit the user environment as that trashes the cache.
build --incompatible_strict_action_env
build --profile=bazel_build.profile

# Prevent Bazel from adding empty __init__.py files. Those files prevent the packages from becoming
# a namespace package and can cause an import collision.
build --incompatible_default_to_explicit_init_py

build --test_output=errors
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ We recommend starting from [Setting up the environment](https://cairo-lang.org/d
# Installation instructions

You should be able to download the python package zip file directly from
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.10.3)
[github](https://github.com/starkware-libs/cairo-lang/releases/tag/v0.11.0)
and install it using ``pip``.
See [Setting up the environment](https://cairo-lang.org/docs/quickstart.html).

Expand Down Expand Up @@ -54,7 +54,7 @@ Once the docker image is built, you can fetch the python package zip file using:

```bash
> container_id=$(docker create cairo)
> docker cp ${container_id}:/app/cairo-lang-0.10.3.zip .
> docker cp ${container_id}:/app/cairo-lang-0.11.0.zip .
> docker rm -v ${container_id}
```

Empty file added bazel_utils/__init__.py
Empty file.
79 changes: 79 additions & 0 deletions bazel_utils/extract_artifacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

import json
import os
import subprocess
from argparse import ArgumentParser

BAD_BRANCH_IDENTIFIER = "BADB51"


def main():
parser = ArgumentParser()
parser.add_argument(
"--input_json",
type=str,
help="The path to the combined.json file.",
required=True,
)
parser.add_argument(
"--artifacts_dir",
type=str,
help="The directory of the resulted compiled contracts.",
required=True,
)
parser.add_argument("--source_dir", type=str, required=True)
parser.add_argument("--contracts", type=str, nargs="*", help="Contracts list.", required=True)
args = parser.parse_args()

with open(os.path.join(args.input_json)) as fp:
combined_json = json.load(fp)

git_commit = BAD_BRANCH_IDENTIFIER
try:
git_commit = (
subprocess.check_output("git rev-parse HEAD".split(), stderr=subprocess.DEVNULL)
.decode("ascii")
.strip()[:6]
)
except Exception:
pass

for path_and_name, val in combined_json["contracts"].items():
path, contract_name = path_and_name.split(":")
compiled_path = os.path.relpath(
os.path.join(os.path.dirname(path), f"{contract_name}.json"), start=args.source_dir
)
if compiled_path not in args.contracts:
continue

# 1. We cannot put "0x" in case of empty bin, as this would not prevent
# loading an empty (virtual) contract. (We want it to fail)
# 2. Note that we can't put an assert len(val['bin']) > 0 here, because some contracts
# are pure virtual and others lack external and public functions.
bytecode = None
if len(val["bin"]) > 0:
bytecode = "0x" + val["bin"]

# Support both solc-0.6 & solc-0.8 output format.
# In solc-0.6 the abi is a list in a json string,
# whereas in 0.8 it's a plain json.
try:
abi = json.loads(val["abi"])
except TypeError:
abi = val["abi"]

artifact = {
"contractName": contract_name,
"abi": abi,
"bytecode": bytecode,
"build_tag": git_commit,
}

destination_filename = os.path.join(args.artifacts_dir, compiled_path)
with open(destination_filename, "w") as fp:
json.dump(artifact, fp, indent=4)


if __name__ == "__main__":
main()
33 changes: 33 additions & 0 deletions bazel_utils/gen_python_exe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

"""
A helper script for python.bzl.
Generates an executable file that runs a python module.
"""

from argparse import ArgumentParser


def main():
parser = ArgumentParser(description="Generates an executable file for py_exe().")
parser.add_argument("--name", help="The name of the target", required=True)
parser.add_argument("--module", help="The name of the module to run", required=True)
args = parser.parse_args()

print(
f"""\
import os
import subprocess
import sys
cmd = [
sys.executable, "-u", "-m", {args.module!r}
] + sys.argv[1:]
proc = subprocess.run(cmd)
sys.exit(proc.returncode)
"""
)


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions bazel_utils/pytest_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys

import pytest

if __name__ == "__main__":
sys.exit(pytest.main(sys.argv[1:]))
31 changes: 31 additions & 0 deletions bazel_utils/python/stub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# This script runs either python or pypy depending on the name of the module that is being run.
# This script is needed because Bazel only supports one python interpreter.

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

set +e

python_interpreter="$(rlocation "python3_x86_64-unknown-linux-gnu/bin/python3")"
if [[ -x "$(rlocation "pypy3.9/bin/pypy3.9")" ]]; then
if [[ $1 == *_uses_pypy_.py ]]; then
python_interpreter="$(rlocation "pypy3.9/bin/pypy3.9")"
fi
fi

if [[ ! -x "$python_interpreter" ]]; then
echo "Could not find interpreter $python_interpreter" >&2
exit 1
fi

exec "$python_interpreter" "$@"
83 changes: 83 additions & 0 deletions bazel_utils/solc_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import shutil
import subprocess
import sys
import tempfile
from argparse import ArgumentParser


def main():
parser = ArgumentParser()

parser.add_argument(
"--solc",
type=str,
help="Solidity compiler path.",
required=True,
)
parser.add_argument(
"--optimize_runs",
type=str,
help=(
"Set for how many contract runs to optimize. Lower values will optimize more for "
"initial deployment cost, higher values will optimize more for high-frequency usage."
),
required=True,
)
parser.add_argument(
"--base_path",
type=str,
help=(
"Use the given path as the root of the source tree instead of the root of the "
"filesystem."
),
required=True,
)
parser.add_argument(
"--output",
type=str,
help="Output path for combined.json.",
required=True,
)
parser.add_argument(
"--srcs",
type=str,
help="Paths of the solidity source files.",
required=True,
nargs="+",
)
args = parser.parse_args()

files_to_compile = []

with tempfile.TemporaryDirectory() as tmp_dir:
for src in args.srcs:
if src.startswith("external/cairo-lang"):
dest_path = src.replace("external/cairo-lang/", "")
else:
dest_path = src
files_to_compile.append(dest_path)
os.makedirs(os.path.dirname(tmp_dir + "/" + dest_path), exist_ok=True)
shutil.copyfile(src, tmp_dir + "/" + dest_path)

subprocess.check_call(
[
os.path.join(os.getcwd(), args.solc),
"--optimize",
"--optimize-runs",
args.optimize_runs,
"--combined-json",
"abi,bin",
"--base-path",
args.base_path,
"-o",
os.path.join(os.getcwd(), args.output),
]
+ files_to_compile,
cwd=tmp_dir,
stdout=subprocess.DEVNULL,
)


if __name__ == "__main__":
sys.exit(main())
12 changes: 12 additions & 0 deletions docker_common_deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Install Clang
apt-get update
apt-get install -y libstdc++-8-dev libc6-dbg
apt-get install -y clang-12 clang-format-12 clang-tidy-6.0 libclang-12-dev llvm-12 g++-7
ln -sf /usr/bin/clang++-12 /usr/bin/clang++
ln -sf /usr/bin/clang-12 /usr/bin/clang

# Install bazel
apt-get install unzip -y
curl -L -o /tmp/bazel_install.sh https://github.com/bazelbuild/bazel/releases/download/5.2.0/bazel-5.2.0-installer-linux-x86_64.sh
chmod +x /tmp/bazel_install.sh
/tmp/bazel_install.sh
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "workspace",
"version": "1.0.0",
"private": true,
"description": "",
"dependencies": {
"ganache": "7.4.4"
},
"devDependencies": {},
"author": "",
"license": "ISC"
}
Empty file.
Empty file.
Empty file added scripts/pypy-requirements.txt
Empty file.
Loading

0 comments on commit 12ca9e9

Please sign in to comment.