Skip to content

Commit

Permalink
Add bazel cpp example
Browse files Browse the repository at this point in the history
  • Loading branch information
advay-modal committed Apr 26, 2024
1 parent 45f75b7 commit 2264a8b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bazel_cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv
bazel-*
__pycache__
MODULE.*
15 changes: 15 additions & 0 deletions bazel_cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Installation (currently only works on linux)

* Install Bazelisk
* `wget https://github.com/bazelbuild/bazelisk/releases/download/v1.19.0/bazelisk-linux-amd64`
* `chmod +x bazelisk-linux-amd64`
* `sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel`
* `python3.11 -m venv venv --prompt modal-cpp-example`
* `source venv/bin/activate`
* `pip install modal`

# Running example

* `bazel build //main:hello-world --sandbox_add_mount_pair=/tmp`
* `source venv/bin/activate`
* `modal run run_binary_modal.py`
21 changes: 21 additions & 0 deletions bazel_cpp/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "aspect_gcc_toolchain",
sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e",
strip_prefix = "gcc-toolchain-0.4.2",
urls = [
"https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz",
],
)

load("@aspect_gcc_toolchain//toolchain:repositories.bzl", "gcc_toolchain_dependencies")

gcc_toolchain_dependencies()

load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS")

gcc_register_toolchain(
name = "gcc_toolchain_x86_64",
target_arch = ARCHS.x86_64,
)
6 changes: 6 additions & 0 deletions bazel_cpp/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
)
22 changes: 22 additions & 0 deletions bazel_cpp/main/hello-world.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <ctime>
#include <string>
#include <iostream>

std::string get_greet(const std::string& who) {
return "Hello " + who;
}

void print_localtime() {
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result));
}

int main(int argc, char** argv) {
std::string who = "world";
if (argc > 1) {
who = argv[1];
}
std::cout << get_greet(who) << std::endl;
print_localtime();
return 0;
}
13 changes: 13 additions & 0 deletions bazel_cpp/run_binary_modal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from modal import App, Mount
import os

app = App("example-modal-bazel-cpp")

CWD = os.getcwd()
LOCAL_REL_PATH= "bazel-bin/main/hello-world"
REMOTE_PATH = "/root/my-binary"

@app.function(mounts=[Mount.from_local_file(local_path=f"{CWD}/{LOCAL_REL_PATH}", remote_path=REMOTE_PATH)])
def f():
import subprocess
subprocess.run(REMOTE_PATH)

0 comments on commit 2264a8b

Please sign in to comment.