diff --git a/demo/hello_bazel/.gitignore b/demo/hello_bazel/.gitignore new file mode 100644 index 00000000..45341f38 --- /dev/null +++ b/demo/hello_bazel/.gitignore @@ -0,0 +1,2 @@ +examples +occlum_workspace diff --git a/demo/hello_bazel/README.md b/demo/hello_bazel/README.md new file mode 100644 index 00000000..715f8adf --- /dev/null +++ b/demo/hello_bazel/README.md @@ -0,0 +1,23 @@ +# C++ Sample Project with Bazel + +This project demonstrates how to use Bazel to build C++ projects for Occlum. To install Bazel on Ubuntu, follow the instructions [here](https://docs.bazel.build/versions/master/install-ubuntu.html). + +1. Download a Bazel sample project in C++ and build it with Occlum toolchain +``` +./build_bazel_sample.sh +``` +When completed, the resulting `hello-world` can be found in `examples/cpp-tutorial/stage3/bazel-bin/main` directory. + +2. (Optional) Run `hello-world` on Linux +``` +LD_LIBRARY_PATH=/usr/local/occlum/x86_64-linux-musl/lib ./examples/cpp-tutorial/stage3/bazel-bin/main/hello-world +``` + +3. Run `hello-world` on Occlum +``` +mkdir occlum_workspace && cd occlum_workspace +occlum init +cp ../examples/cpp-tutorial/stage3/bazel-bin/main/hello-world image/bin +occlum build +occlum run /bin/hello-world +``` diff --git a/demo/hello_bazel/build_bazel_sample.sh b/demo/hello_bazel/build_bazel_sample.sh new file mode 100755 index 00000000..430f8137 --- /dev/null +++ b/demo/hello_bazel/build_bazel_sample.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +mkdir examples +cd examples +git clone https://github.com/bazelbuild/examples/ . +cd cpp-tutorial/stage3 +export CC=/opt/occlum/toolchains/gcc/bin/occlum-gcc +export CXX=/opt/occlum/toolchains/gcc/bin/occlum-g++ +bazel build --cxxopt=-std=c++11 --copt=-fPIC --linkopt=-pie //main:hello-world diff --git a/demo/hello_world/.gitignore b/demo/hello_c/.gitignore similarity index 82% rename from demo/hello_world/.gitignore rename to demo/hello_c/.gitignore index 8a12aa82..0082ad43 100644 --- a/demo/hello_world/.gitignore +++ b/demo/hello_c/.gitignore @@ -1,2 +1,3 @@ hello_world +build occlum_workspace diff --git a/demo/hello_c/CMakeLists.txt b/demo/hello_c/CMakeLists.txt new file mode 100644 index 00000000..487f69fc --- /dev/null +++ b/demo/hello_c/CMakeLists.txt @@ -0,0 +1,6 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.5) +PROJECT(HELLO_WORLD LANGUAGES C VERSION 0.0.1) +SET(SRC_LIST "hello_world.c") +MESSAGE(STATUS "This is BINARY dir " ${HELLO_WORLD_BINARY_DIR}) +MESSAGE(STATUS "This is SOURCE dir " ${HELLO_WORLD_SOURCE_DIR}) +ADD_EXECUTABLE(hello_world ${SRC_LIST}) diff --git a/demo/hello_c/Makefile b/demo/hello_c/Makefile new file mode 100644 index 00000000..4feebba4 --- /dev/null +++ b/demo/hello_c/Makefile @@ -0,0 +1,12 @@ +CC := occlum-gcc +CFLAGS := -fPIC -pie + +.PHONY: all clean + +all: hello_world + +hello_world: hello_world.c + $(CC) $(CFLAGS) $^ -o $@ + +clean: + rm -rf hello_world diff --git a/demo/hello_c/README.md b/demo/hello_c/README.md new file mode 100644 index 00000000..e4c57b24 --- /dev/null +++ b/demo/hello_c/README.md @@ -0,0 +1,31 @@ +# C Sample Project with Makefile and CMake + +This project demonstrates how to use Makefile/CMake to build C projects for Occlum. + +1. Build `hello_world` with Makefile +``` +make +``` +Or you can build `hello_world` with CMake +``` +mkdir build && cd build +cmake ../ -DCMAKE_C_COMPILER=occlum-gcc -DCMAKE_C_FLAGS="-fPIC -pie" +make +cd .. +cp build/hello_world . +``` +Either way, the resulting `hello_world` can be found in the current directory. + +2. (Optional) Run `hello_world` on Linux +``` +./hello_world +``` + +3. Run `hello_world` on Occlum +``` +mkdir occlum_workspace && cd occlum_workspace +occlum init +cp ../hello_world image/bin +occlum build +occlum run /bin/hello_world +``` diff --git a/demo/hello_world/hello_world.c b/demo/hello_c/hello_world.c similarity index 100% rename from demo/hello_world/hello_world.c rename to demo/hello_c/hello_world.c diff --git a/demo/hello_cc/.gitignore b/demo/hello_cc/.gitignore new file mode 100644 index 00000000..0082ad43 --- /dev/null +++ b/demo/hello_cc/.gitignore @@ -0,0 +1,3 @@ +hello_world +build +occlum_workspace diff --git a/demo/hello_cc/CMakeLists.txt b/demo/hello_cc/CMakeLists.txt new file mode 100644 index 00000000..b7aae408 --- /dev/null +++ b/demo/hello_cc/CMakeLists.txt @@ -0,0 +1,7 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.5) +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +PROJECT(HELLO_WORLD LANGUAGES CXX VERSION 0.0.1) +SET(SRC_LIST "hello_world.cc") +MESSAGE(STATUS "This is BINARY dir " ${HELLO_WORLD_BINARY_DIR}) +MESSAGE(STATUS "This is SOURCE dir " ${HELLO_WORLD_SOURCE_DIR}) +ADD_EXECUTABLE(hello_world ${SRC_LIST}) diff --git a/demo/hello_cc/Makefile b/demo/hello_cc/Makefile new file mode 100644 index 00000000..3e5bcc83 --- /dev/null +++ b/demo/hello_cc/Makefile @@ -0,0 +1,12 @@ +CXX := occlum-g++ +CXXFLAGS := -std=c++11 -fPIC -pie + +.PHONY: all clean + +all: hello_world + +hello_world: hello_world.cc + $(CXX) $(CXXFLAGS) $^ -o $@ + +clean: + rm -rf hello_world diff --git a/demo/hello_cc/README.md b/demo/hello_cc/README.md new file mode 100644 index 00000000..f212b1e4 --- /dev/null +++ b/demo/hello_cc/README.md @@ -0,0 +1,31 @@ +# C++ Sample Project with Makefile and CMake + +This project demonstrates how to use Makefile/CMake to build C++ projects for Occlum. + +1. Build `hello_world` with Makefile +``` +make +``` +Or you can build `hello_world` with CMake +``` +mkdir build && cd build +cmake ../ -DCMAKE_CXX_COMPILER=occlum-g++ -DCMAKE_CXX_FLAGS="-fPIC -pie" +make +cd .. +cp build/hello_world . +``` +Either way, the resulting `hello_world` can be found in the current directory. + +2. (Optional) Run `hello_world` on Linux +``` +LD_LIBRARY_PATH=/usr/local/occlum/x86_64-linux-musl/lib ./hello_world +``` + +3. Run `hello_world` on Occlum +``` +mkdir occlum_workspace && cd occlum_workspace +occlum init +cp ../hello_world image/bin +occlum build +occlum run /bin/hello_world +``` diff --git a/demo/hello_cc/hello_world.cc b/demo/hello_cc/hello_world.cc new file mode 100644 index 00000000..fa5c04d2 --- /dev/null +++ b/demo/hello_cc/hello_world.cc @@ -0,0 +1,6 @@ +#include + +int main() { + std::cout << "Hello World" << std::endl; + return 0; +} diff --git a/demo/hello_world/Makefile b/demo/hello_world/Makefile deleted file mode 100644 index 7cb79a58..00000000 --- a/demo/hello_world/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -CC := occlum-gcc -CFLAGS := -fPIC -pie - -.PHONY: all test test-native clean - -all: hello_world - -hello_world: hello_world.c - $(CC) $(CFLAGS) $^ -o $@ - -# Run hello_world on Occlum inside an SGX enclave -test: hello_world - rm -rf occlum_workspace - mkdir occlum_workspace - cd occlum_workspace && \ - occlum init && \ - cp ../hello_world image/bin && \ - occlum build && \ - occlum run /bin/hello_world - -# Run hello_world on the native OS (e.g., Linux) -test-native: - ./hello_world - -clean: - rm -rf hello_world occlum_workspace diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile index fce8bdea..38a57ff3 100644 --- a/tools/docker/Dockerfile +++ b/tools/docker/Dockerfile @@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ debhelper \ expect \ + g++ \ gdb \ git-core \ jq \ @@ -31,10 +32,13 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ pkg-config \ protobuf-compiler \ python \ + python-pip \ sudo \ + unzip \ uuid-dev \ vim \ wget \ + zip \ && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*