Add C/C++ Hello World with Makefile, CMake, and Bazel
This commit is contained in:
		
							parent
							
								
									ba7db98e49
								
							
						
					
					
						commit
						5f26bfc1fb
					
				
							
								
								
									
										2
									
								
								demo/hello_bazel/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										2
									
								
								demo/hello_bazel/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
examples
 | 
			
		||||
occlum_workspace
 | 
			
		||||
							
								
								
									
										23
									
								
								demo/hello_bazel/README.md
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										23
									
								
								demo/hello_bazel/README.md
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										10
									
								
								demo/hello_bazel/build_bazel_sample.sh
									
									
									
									
									
										Executable file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										10
									
								
								demo/hello_bazel/build_bazel_sample.sh
									
									
									
									
									
										Executable file
									
								
							@ -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
 | 
			
		||||
@ -1,2 +1,3 @@
 | 
			
		||||
hello_world
 | 
			
		||||
build
 | 
			
		||||
occlum_workspace
 | 
			
		||||
							
								
								
									
										6
									
								
								demo/hello_c/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										6
									
								
								demo/hello_c/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@ -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})
 | 
			
		||||
							
								
								
									
										12
									
								
								demo/hello_c/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										12
									
								
								demo/hello_c/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
							
								
								
									
										31
									
								
								demo/hello_c/README.md
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										31
									
								
								demo/hello_c/README.md
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										3
									
								
								demo/hello_cc/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										3
									
								
								demo/hello_cc/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
hello_world
 | 
			
		||||
build
 | 
			
		||||
occlum_workspace
 | 
			
		||||
							
								
								
									
										7
									
								
								demo/hello_cc/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										7
									
								
								demo/hello_cc/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							@ -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})
 | 
			
		||||
							
								
								
									
										12
									
								
								demo/hello_cc/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										12
									
								
								demo/hello_cc/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
							
								
								
									
										31
									
								
								demo/hello_cc/README.md
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										31
									
								
								demo/hello_cc/README.md
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										6
									
								
								demo/hello_cc/hello_world.cc
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										6
									
								
								demo/hello_cc/hello_world.cc
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    std::cout << "Hello World" << std::endl;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
@ -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
 | 
			
		||||
@ -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/*
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user