Skip to content

protobuf support #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ Cargo.lock

libsodium
openssl-wasm
boost
boost
*pb.cc
*pb.h
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export PATH=$WASI_SDK_PATH/bin:$PATH
4. Run `sh prepare-sodium.sh` to prepare `libsodium` for wasm env. You may need to install `zig` in advance.
5. Run `sh prepare-openssl.sh` to prepare `libssl` for wasm env. Also, in some of your C++ file you need to define `pid_t getpid(void) {return 1;}` since WASI lacks process identifiers, so we nede to define some stub.
6. Run `sh prepare-boost.sh` to prepare `boost` for wasm env.
7. Run `sh prepare-protobuf.sh` to prepare `protobuf` for wasm env. For protobuf to work we also need to use wasi-sdk with pthreads support, e.g. https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-20%2Bthreads, and then do :
```
export WASI_SDK_PATH=~/Downloads/wasi-sdk-20.0+threads
export PATH=$WASI_SDK_PATH/bin:$PATH
```

## Build

Expand All @@ -36,4 +41,4 @@ This should print the following:
[hello] Hello World from C++! (printf)
The sodium is initialized!
sodium randombytes: 714298213
```
```
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ fn main() {
let target = env::var("TARGET").unwrap();

let mut build = cc::Build::new();
// build.cpp(true) // this will cause rust-lld: error: unable to find library -lstdc++
// build.cpp(true); // this will cause rust-lld: error: unable to find library -lstdc++
build.file("cpp/add.cpp");
build.file("cpp/wallet.pb.cc");
build.file("cpp/my_code.cpp");

if target.contains("wasm32") {
Expand Down
12 changes: 12 additions & 0 deletions cpp/add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "add.h"

Add::Add(int x, int y)
{
gx = x;
gy = y;
}

int Add::getSum()
{
return gx + gy;
}
15 changes: 15 additions & 0 deletions cpp/add.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// add.h
#ifndef add_H
#define add_H

class Add
{
int gx;
int gy;

public:
Add(int x, int y);
int getSum();
};

#endif
18 changes: 16 additions & 2 deletions cpp/my_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@

#include <boost/algorithm/string.hpp>

#include "wallet.pb.h"

#include "add.h"

struct KeyPair
{
std::vector<uint8_t> public_key;
std::vector<uint8_t> private_key;
};

extern "C"
{
{
void hello()
{
// Test C++
Expand Down Expand Up @@ -82,9 +86,19 @@ extern "C"
std::string s = "Boost Libraries";
boost::to_upper(s);
printf("[hello] Boost Upper Case test: %s\n", s.c_str());

// Test external class
//
Add *a1 = new Add(5, 5);
printf("SUM1: %d\n", a1->getSum());

// Test protobuf
//
my_wallets::MaxSupply max_supply;
}

pid_t getpid(void) {
pid_t getpid(void)
{
return 1;
}
}
2 changes: 2 additions & 0 deletions prepare-protobuf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cd resources/proto
protoc --cpp_out=../../cpp/ *.proto
10 changes: 10 additions & 0 deletions resources/proto/wallet.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package my_wallets;

option optimize_for = SPEED;

message MaxSupply{
string max_supply = 1;
string circulation = 2;
}