Skip to content

Commit

Permalink
Merge branch 'merge_1_2_0'
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentRX committed Sep 10, 2018
2 parents 4171e14 + bc3ef35 commit 60d2e95
Show file tree
Hide file tree
Showing 42 changed files with 565 additions and 2,743 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
*.out
*.app

# Mac OS
.DS_Store

# Build
build/*
1 change: 1 addition & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
[submodule "agrio_llvm"]
path = agrio_llvm
url = https://github.com/aggregion/llvm.git
branch = agrio
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ cmake_minimum_required(VERSION 3.5)
project(agrio_wasm_sdk)

set(VERSION_MAJOR 1)
set(VERSION_MINOR 1)
set(VERSION_PATCH 1)
set(VERSION_MINOR 2)
set(VERSION_PATCH 0)

if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
message(WARNING "CMAKE_INSTALL_PREFIX is set to default path of ${CMAKE_INSTALL_PREFIX}, resetting to ${CMAKE_INSTALL_PREFIX}/agrio.wasmsdk")
set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/agrio.wasmsdk")
message(WARNING "CMAKE_INSTALL_PREFIX is set to default path of ${CMAKE_INSTALL_PREFIX}, resetting to ${CMAKE_INSTALL_PREFIX}/agrio.cdt")
set(CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/agrio.cdt")
elseif ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr/local")
message(WARNING "CMAKE_INSTALL_PREFIX is explicitly set to /usr/local. This is not recommended.")
endif()
Expand Down Expand Up @@ -51,3 +51,6 @@ configure_file(${CMAKE_SOURCE_DIR}/modules/AgrioWasmToolchain.cmake.in ${CMAKE_B
install(FILES ${CMAKE_BINARY_DIR}/modules/AgrioWasmToolchain.cmake DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}/cmake)
configure_file(${CMAKE_SOURCE_DIR}/agrio.imports.in ${CMAKE_BINARY_DIR}/agrio.imports COPYONLY)
install(FILES ${CMAKE_BINARY_DIR}/agrio.imports DESTINATION ${CMAKE_INSTALL_PREFIX})

configure_file(${CMAKE_SOURCE_DIR}/scripts/ricardagr/ricardagr.py ${CMAKE_BINARY_DIR}/scripts/ricardagr.py COPYONLY)
install(FILES ${CMAKE_BINARY_DIR}/scripts/ricardagr.py DESTINATION ${CMAKE_INSTALL_PREFIX}/scripts)
2 changes: 1 addition & 1 deletion ClangExternalProject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(GNUInstallDirs)

ExternalProject_Add(
AgrioClang
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/llvm -DCMAKE_BUILD_TYPE=Release -DBOOST_ROOT=${BOOST_ROOT} -DSDK_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DAGRIO_VER_MAJOR=${VERSION_MAJOR} -DAGRIO_VER_MINOR=${VERSION_MINOR} -DAGRIO_VER_REVISION=${VERSION_PATCH} -DAGRIO_STACK_SIZE=8192
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}/llvm -DCMAKE_BUILD_TYPE=Release -DBOOST_ROOT=${BOOST_ROOT} -DAGRIO_VER_MAJOR=${VERSION_MAJOR} -DAGRIO_VER_MINOR=${VERSION_MINOR} -DAGRIO_VER_REVISION=${VERSION_PATCH} -DAGRIO_STACK_SIZE=8192

SOURCE_DIR "${CMAKE_SOURCE_DIR}/agrio_llvm"
UPDATE_COMMAND ""
Expand Down
3 changes: 3 additions & 0 deletions InstallClang.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ install(FILES ${CLANG_BIN_DIR}/agrio-cpp
install(FILES ${CLANG_BIN_DIR}/agrio-ld
DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
install(FILES ${CLANG_BIN_DIR}/agrio-abigen
DESTINATION ${CMAKE_INSTALL_FULL_BINDIR}
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
install(FILES ${CLANG_BIN_DIR}/../lib/LLVMAgrioApply${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
install(CODE "
Expand Down
158 changes: 136 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# WasmSDK
## Version : 1.1.1
# AGRIO.CDT (Contract Development Toolkit)
## Version : 1.2.0

WasmSDK is a toolchain for WebAssembly (WASM). In addition to being a general purpose WebAssembly toolchain, [AGRIO](https://github.com/agrio/agr) specific optimizations are available to support building AGRIO smart contracts. This new toolchain is built around [Clang 7](https://github.com/agrio/llvm), which means that the SDK has the most currently available optimizations and analyses from LLVM, but as the WASM target is still considered experimental, some optimizations are not available or incomplete.
AGRIO.CDT is a toolchain for WebAssembly (WASM) and set of tools to facilitate contract writing for the AGRIO platform. In addition to being a general purpose WebAssembly toolchain, [AGRIO](https://github.com/agrio/agr) specific optimizations are available to support building AGRIO smart contracts. This new toolchain is built around [Clang 7](https://github.com/agrio/llvm), which means that AGRIO.CDT has the most currently available optimizations and analyses from LLVM, but as the WASM target is still considered experimental, some optimizations are not available or incomplete.

## New Features from AGRIO
- Compile (-c) option flag will compile to a WASM-elf object file
- ranlib and ar support for static libraries for WASM
- \_\_FILE\_\_ and \_\_BASE\_FILE\_\_ will now only return the file name and not the fullpath. This eliminates any non-determinism from location of the compiled binary
- Global constructors and global destructors are now supported
- _agrio-cpp_, _agrio-cc_, _agrio-ld_, and _agrio-pp_ are set the core set of tools that you will interact with.
* These are the C++ compiler, C compiler, linker and postpass tools.
- A simple CMake interface to build AGRIO smart contracts against WasmSDK
- ABI generator (Coming Soon)

- _agrio-cpp_, _agrio-cc_, _agrio-ld_, _agrio-pp_, and _agrio_abigen_ are set the core set of tools that you will interact with.
* These are the C++ compiler, C compiler, linker, postpass tool and ABI generator.
- A simple CMake interface to build AGRIO smart contracts against AGRIO.CDT

### Guided Installation
First clone

```sh
$ git clone --recursive https://github.com/agrio/agrio.wasmsdk
$ cd agrio.wasmsdk
$ git clone --recursive https://github.com/agrio/agrio.cdt
$ cd agrio.cdt
```

Now run `build.sh` and give the core symbol for the AGRIO blockchain that intend to deploy to.
Expand All @@ -28,17 +28,112 @@ $ ./build.sh <CORE_SYMBOL>
```

Finally, install the build
*This install will install the core to ```/usr/local/agrio.wasmsdk``` and symlinks to the top level tools (compiler, ld, etc.) to ```/usr/local/bin```
*This install will install the core to ```/usr/local/agrio.cdt``` and symlinks to the top level tools (compiler, ld, etc.) to ```/usr/local/bin```
```sh
$ sudo ./install.sh
```

### Building your first smart contract
- Navigate to the hello folder in examples (./examples/hello).
- You should then see the hello.cpp file
- Now run the compiler
```sh
$ agrio-cpp hello.cpp -o hello.wasm
```
- As of this release abi generation is not available.

#### Optional, if you know cmake
- If you want to test out the CMake system, stay in the same directory as the previous manual build.
- Then create a build directory ```mkdir build``` and cd into that directory ```cd build```
- Then run ```cmake ../```, this will generate the cache and supporting files for CMake to do it's job.
- Now simply run ```make```.
- You should now have a `hello` file in the build directory, this is the wasm file, if you would like to use ```clagr set contract``` and you need to change the name to have the .wasm extension that is perfectly fine, or you can change the add_executable target name to ```hello.wasm```, or you can use ```clagr set code <path-to-compiled-wasm>/hello``` to load the file to the blockchain , without the .wasm extension.

### How to use agrio-abigen
#### using with agrio-cpp
To generate an abi with ```agrio-cpp```, the only flag you need to pass to ```agrio-cpp``` is `-abigen`, this will tell the compiler to run `agrio-abigen` after compilation and linking stages. If the output filename is specified as a '.wasm' file with the `-o` option (e.g. \<filename\>.wasm) then agrio-cpp will tell the abi generator to create the abi with the name \<filename\>.abi, if no '.wasm' suffix is used then the resulting output filename is still \<filename\>.abi

Example:
```bash
$ agrio-cpp hello.cpp -o hello.wasm --abigen
```
This will generate two files:
* The compiled binary wasm (hello.wasm)
* The generated abi file (hello.abi)

#### using agrio-abigen alone
To generate an abi with ```agrio-abigen```, only requires that you give the main '.cpp' file to compile and the output filename `--output`.

Example:
```bash
$ agrio-abigen hello.cpp --output=hello.abi
```

This will generate one file:
* The generated abi file (hello.abi)

### Difference from old abi generator
Unlike the old abi generator tool, the new tool uses C++11 or GNU style attributes to mark ```actions``` and ```tables```.

Example (four ways to declare an action for ABI generation):
```c++
// this is the C++11 and greater style attribute
[[agrio::action]]
void testa( account_name n ) {
// do something
}

// this is the GNU style attribute, this can be used in C code and prior to C++ 11
__attribute__((agrio_action))
void testa( account_name n ){
// do something
}

struct [[agrio::action]] testa {
account_name n;
AGRLIB_SERIALIZE( testa, (n) )
};

struct __attribute__((agrio_action)) testa {
account_name n;
AGRLIB_SERIALIZE( testa, (n) )
};
```
If your action name is not a valid [AGRIO name](https://developers.agr.io/agrio-cpp/docs/naming-conventions) you can explicitly specify the name in the attribute ```c++ [[agrio::action("<valid action name>")]]```

Example (Two ways to declare a table for abi generation):
```c++
struct [[agrio::table]] testtable {
uint64_t owner;
/* all other fields */
};

struct __attribute__((agrio_table)) testtable {
uint64_t owner;
/* all other fields */
};

typedef agrio::multi_index<N(tablename), testtable> testtable_t;
```
If you don't want to use the multi-index you can explicitly specify the name in the attribute ```c++ [[agrio::table("<valid action name>")]]```

For an example contract of abi generation please see the file ./examples/abigen_test/test.cpp, you can generate the abi for this file with `agrio-abigen test.cpp --output=test.abi`.

### Fixing an ABI, or Writing an ABI
- The sections to the abi are pretty simple to understand and the syntax is purely JSON, so we are going to write our own ABI file.
- Even after ABI generation is available, an important note should be made that the generator will never be completely perfect for every contract written, advanced features of the newest version of the ABI will require manual construction of the ABI, and odd and advanced C++ patterns could capsize the generators type deductions, so having a good knowledge of how to write an ABI should be an essential piece of knowledge of a smart contract writer.
- Please refer to [developers.agr.io "How to write an abi"](https://developers.agr.io/agrio-cpp/docs/how-to-write-an-abi), to learn about the different sections of an ABI.


### Installed Tools
---
* [agrio-cpp](#agrio-cpp)
* [agrio-cc](#agrio-cc)
* [agrio-ld](#agrio-ld)
* agrio-abigen
* agrio-pp (post processing pass for WASM, automatically runs with agrio-cpp and agrio-ld)
* agrio-wasm2wast
* agrio-wast2wasm
* agrio-ranlib
* agrio-ar
* agrio-objdump
Expand All @@ -53,13 +148,13 @@ For example:
cmake_minimum_required(VERSION 3.5)
project(test_example VERSION 1.0.0)
if(WASM_ROOT STREQUAL "" OR NOT WASM_ROOT)
set(WASM_ROOT ${CMAKE_INSTALL_PREFIX})
if(AGRIO_CDT_ROOT STREQUAL "" OR NOT AGRIO_CDT_ROOT)
set(AGRIO_CDT_ROOT "/usr/local/agrio.cdt")
endif()
list(APPEND CMAKE_MODULE_PATH ${WASM_ROOT}/lib/cmake)
list(APPEND CMAKE_MODULE_PATH ${AGRIO_CDT_ROOT}/lib/cmake)
include(AgrioWasmToolchain)
add_executable( test test.cpp )
add_executable( test.wasm test.cpp )
```
```test.cpp```
```
Expand All @@ -68,16 +163,17 @@ using namespace agrio;
class test : public agrio::contract {
public:
using contract::contract;
void test_action( account_name test ) {
[[agrio::action]]
void testact( account_name test ) {
}
};
AGRIO_ABI( test, (test_action))
AGRIO_ABI( test, (testact))
```

Since, AgrioWasmToolchain overwrites `cmake` to cross-compile WASM, standard cmake commands of _add\_executable/ add\_library_ can then be used. Also note, the __WASM_ROOT__ variable, this needs to be set if you decided to install to the non-default location.
Since, AgrioWasmToolchain overwrites `cmake` to cross-compile WASM, standard cmake commands of _add\_executable/ add\_library_ can then be used. Also note, the __AGRIO_CDT_ROOT__ variable, this needs to be set if you decided to install to the non-default location.

To manually compile source code:
Use ```agrio-cpp/agrio-cc``` and ```agrio-ld``` as if it were __clang__ and __lld__ , with all includes and options specific to AGRIO and WasmSDK being baked in.
Use ```agrio-cpp/agrio-cc``` and ```agrio-ld``` as if it were __clang__ and __lld__ , with all includes and options specific to AGRIO and CDT being baked in.

### agrio-cpp
---
Expand Down Expand Up @@ -157,10 +253,28 @@ agrio.ld options:
-lto-opt=<string> - LTO Optimization level (O0-O3)
-o=<string> - Write output to <file>
```
### Todos
---
- Add ABI generation to agrio-cpp
### agrio-abigen
---
```bash
USAGE: agrio-abigen [options] <source0> [... <sourceN>]
OPTIONS:
Generic Options:
-help - Display available options (-help-hidden for more)
-help-list - Display list of available options (-help-list-hidden for more)
-version - Display the version of this program
agrio-abigen:
generates an abi from C++ project input
-extra-arg=<string> - Additional argument to append to the compiler command line
-extra-arg-before=<string> - Additional argument to prepend to the compiler command line
-output=<string> - Set the output filename and fullpath
-p=<string> - Build path
```
License
----
Expand Down
2 changes: 1 addition & 1 deletion agrio_llvm
Submodule agrio_llvm updated 2 files
+2 −0 .gitmodules
+1 −1 tools/clang
20 changes: 17 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/bash

printf "\t=========== Building agrio.wasmsdk ===========\n\n"
printf "\t=========== Building agrio.cdt ===========\n\n"

RED='\033[0;31m'
NC='\033[0m'
Expand All @@ -10,6 +10,8 @@ txtrst=$(tput sgr0)

export DISK_MIN=10
export TEMP_DIR="/tmp"
TEMP_DIR='/tmp'
DISK_MIN=10

unamestr=`uname`
if [[ "${unamestr}" == 'Darwin' ]]; then
Expand Down Expand Up @@ -48,6 +50,10 @@ else
export ARCH="Ubuntu"
bash ./scripts/agrio_build_ubuntu.sh
;;
"Debian GNU/Linux")
export ARCH="Debian"
bash ./scripts/agrio_build_ubuntu.sh
;;
*)
printf "\\n\\tUnsupported Linux Distribution. Exiting now.\\n\\n"
exit 1
Expand All @@ -63,16 +69,24 @@ if [[ `uname` == 'Darwin' ]]; then
read -ra FREE_MEM <<< "$FREE_MEM"
FREE_MEM=$((${FREE_MEM[2]%?}*(4096))) # free pages * page size
else
FREE_MEM=`free | grep "Mem:" | awk '{print $4}'`
FREE_MEM=`LANG=C free | grep "Mem:" | awk '{print $4}'`
fi

CORES_AVAIL=`getconf _NPROCESSORS_ONLN`
MEM_CORES=$(( ${FREE_MEM}/4000000 )) # 4 gigabytes per core
MEM_CORES=$(( $MEM_CORES > 0 ? $MEM_CORES : 1 ))
CORES=$(( $CORES_AVAIL < $MEM_CORES ? $CORES_AVAIL : $MEM_CORES ))

#check submodules
if [ $(( $(git submodule status | grep -c "^[+\-]") )) -gt 0 ]; then
printf "\\n\\tgit submodules are not up to date.\\n"
printf "\\tPlease run the command 'git submodule update --init --recursive'.\\n"
exit 1
fi

mkdir -p build
pushd build &> /dev/null
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/agrio.wasmsdk -DBOOST_ROOT="${BOOST}" -DCORE_SYMBOL_NAME="${CORE_SYMBOL}" ../
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/agrio.cdt -DBOOST_ROOT="${BOOST}" -DCORE_SYMBOL_NAME="${CORE_SYMBOL}" ../
if [ $? -ne 0 ]; then
exit -1;
fi
Expand Down
16 changes: 16 additions & 0 deletions examples/abigen_test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.5)
project(abigen_test_example VERSION 1.0.0)

# if no wasm root is given use default path
if(AGRIO_CDT_ROOT STREQUAL "" OR NOT AGRIO_CDT_ROOT)
set(AGRIO_CDT_ROOT "/usr/local/agrio.cdt")
endif()

# append the path to the module to include
list(APPEND CMAKE_MODULE_PATH ${AGRIO_CDT_ROOT}/lib/cmake)

#include the toolchain cmake
include(AgrioWasmToolchain)

set(CMAKE_CXX_FLAGS " -abigen ${CMAKE_CXX_FLAGS} ")
add_executable( test.wasm test.cpp )
Loading

0 comments on commit 60d2e95

Please sign in to comment.