Improve Occlum GCC toolchain with new wrappers for binaries

This commit makes the toolchain easier to use in two folds:
1. When compiling C/C++ source files, no need to add "-fPIC -pie" flags manually;
2. When running executables generated by the Occlum toolchain on Linux, no
need to set the `LD_LIBRARY_PATH` manually.
This commit is contained in:
LI Qing 2019-11-21 01:57:05 +00:00 committed by Tate, Hongliang Tian
parent 2357f8ed1c
commit 1304f5388d
11 changed files with 38 additions and 44 deletions

@ -10,7 +10,7 @@ When completed, the resulting `hello-world` can be found in `examples/cpp-tutori
2. (Optional) Run `hello-world` on Linux 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 ./examples/cpp-tutorial/stage3/bazel-bin/main/hello-world
``` ```
3. Run `hello-world` on Occlum 3. Run `hello-world` on Occlum

@ -7,4 +7,4 @@ git clone https://github.com/bazelbuild/examples/ .
cd cpp-tutorial/stage3 cd cpp-tutorial/stage3
export CC=/opt/occlum/toolchains/gcc/bin/occlum-gcc export CC=/opt/occlum/toolchains/gcc/bin/occlum-gcc
export CXX=/opt/occlum/toolchains/gcc/bin/occlum-g++ export CXX=/opt/occlum/toolchains/gcc/bin/occlum-g++
bazel build --cxxopt=-std=c++11 --copt=-fPIC --linkopt=-pie //main:hello-world bazel build --cxxopt=-std=c++11 //main:hello-world

@ -1,12 +1,11 @@
CC := occlum-gcc CC := occlum-gcc
CFLAGS := -fPIC -pie
.PHONY: all clean .PHONY: all clean
all: hello_world all: hello_world
hello_world: hello_world.c hello_world: hello_world.c
$(CC) $(CFLAGS) $^ -o $@ $(CC) $^ -o $@
clean: clean:
rm -rf hello_world rm -rf hello_world

@ -9,7 +9,7 @@ make
Or you can build `hello_world` with CMake Or you can build `hello_world` with CMake
``` ```
mkdir build && cd build mkdir build && cd build
cmake ../ -DCMAKE_C_COMPILER=occlum-gcc -DCMAKE_C_FLAGS="-fPIC -pie" cmake ../ -DCMAKE_C_COMPILER=occlum-gcc
make make
cd .. cd ..
cp build/hello_world . cp build/hello_world .

@ -1,5 +1,5 @@
CXX := occlum-g++ CXX := occlum-g++
CXXFLAGS := -std=c++11 -fPIC -pie CXXFLAGS := -std=c++11
.PHONY: all clean .PHONY: all clean

@ -9,7 +9,7 @@ make
Or you can build `hello_world` with CMake Or you can build `hello_world` with CMake
``` ```
mkdir build && cd build mkdir build && cd build
cmake ../ -DCMAKE_CXX_COMPILER=occlum-g++ -DCMAKE_CXX_FLAGS="-fPIC -pie" cmake ../ -DCMAKE_CXX_COMPILER=occlum-g++
make make
cd .. cd ..
cp build/hello_world . cp build/hello_world .
@ -18,7 +18,7 @@ Either way, the resulting `hello_world` can be found in the current directory.
2. (Optional) Run `hello_world` on Linux 2. (Optional) Run `hello_world` on Linux
``` ```
LD_LIBRARY_PATH=/usr/local/occlum/x86_64-linux-musl/lib ./hello_world ./hello_world
``` ```
3. Run `hello_world` on Occlum 3. Run `hello_world` on Occlum

@ -10,8 +10,7 @@ CC=occlum-gcc ./config \
--prefix=/usr/local/occlum/x86_64-linux-musl \ --prefix=/usr/local/occlum/x86_64-linux-musl \
--openssldir=/usr/local/occlum/ssl \ --openssldir=/usr/local/occlum/ssl \
--with-rand-seed=rdcpu \ --with-rand-seed=rdcpu \
no-zlib no-async no-tests \ no-zlib no-async no-tests
-fPIC -pie
make -j make -j
sudo make install sudo make install
popd popd
@ -25,5 +24,5 @@ popd
# 3. Build the https server example in mongoose # 3. Build the https server example in mongoose
pushd mongoose_src/examples/simplest_web_server_ssl pushd mongoose_src/examples/simplest_web_server_ssl
CC=occlum-gcc CFLAGS_EXTRA="-Wno-format-truncation -fPIC -pie" make CC=occlum-gcc CFLAGS_EXTRA="-Wno-format-truncation" make
popd popd

@ -1,5 +1,4 @@
#!/bin/bash #!/bin/bash
export LD_LIBRARY_PATH=/usr/local/occlum/x86_64-linux-musl/lib:$LD_LIBRARY_PATH
https_server=simplest_web_server_ssl https_server=simplest_web_server_ssl
set -e set -e

@ -1,27 +1,8 @@
diff --git a/tensorflow/lite/tools/make/Makefile b/tensorflow/lite/tools/make/Makefile diff --git a/tensorflow/lite/tools/make/Makefile b/tensorflow/lite/tools/make/Makefile
index 73c50d3..3cef7fa 100644 index 73c50d3..4a6445a 100644
--- a/tensorflow/lite/tools/make/Makefile --- a/tensorflow/lite/tools/make/Makefile
+++ b/tensorflow/lite/tools/make/Makefile +++ b/tensorflow/lite/tools/make/Makefile
@@ -42,7 +42,7 @@ INCLUDES := \ @@ -80,6 +80,12 @@ BENCHMARK_BINARY_NAME := benchmark_model
-I$(OBJDIR)
# This is at the end so any globally-installed frameworks like protobuf don't
# override local versions in the source tree.
-INCLUDES += -I/usr/local/include
+INCLUDES += -I/usr/local/occlum/x86_64-linux-musl/include
# These are the default libraries needed, but they can be added to or
# overridden by the platform-specific settings in target makefiles.
@@ -59,7 +59,8 @@ CXXFLAGS := -O3 -DNDEBUG -fPIC
CXXFLAGS += $(EXTRA_CXXFLAGS)
CFLAGS := ${CXXFLAGS}
CXXFLAGS += --std=c++11
-LDOPTS := -L/usr/local/lib
+LDOPTS := -L/usr/local/occlum/x86_64-linux-musl/lib
+LDFLAGS := -pie $(LDOPTS)
ARFLAGS := -r
TARGET_TOOLCHAIN_PREFIX :=
CC_PREFIX :=
@@ -80,6 +81,12 @@ BENCHMARK_BINARY_NAME := benchmark_model
MINIMAL_SRCS := \ MINIMAL_SRCS := \
tensorflow/lite/examples/minimal/minimal.cc tensorflow/lite/examples/minimal/minimal.cc
@ -34,7 +15,7 @@ index 73c50d3..3cef7fa 100644
# What sources we want to compile, must be kept in sync with the main Bazel # What sources we want to compile, must be kept in sync with the main Bazel
# build files. # build files.
@@ -133,7 +140,8 @@ $(wildcard tensorflow/lite/*/*/*test.cc) \ @@ -133,7 +139,8 @@ $(wildcard tensorflow/lite/*/*/*test.cc) \
$(wildcard tensorflow/lite/*/*/*/*test.cc) \ $(wildcard tensorflow/lite/*/*/*/*test.cc) \
$(wildcard tensorflow/lite/kernels/*test_main.cc) \ $(wildcard tensorflow/lite/kernels/*test_main.cc) \
$(wildcard tensorflow/lite/kernels/*test_util.cc) \ $(wildcard tensorflow/lite/kernels/*test_util.cc) \
@ -44,7 +25,7 @@ index 73c50d3..3cef7fa 100644
BUILD_WITH_MMAP ?= true BUILD_WITH_MMAP ?= true
ifeq ($(BUILD_TYPE),micro) ifeq ($(BUILD_TYPE),micro)
@@ -209,6 +217,7 @@ include $(wildcard $(MAKEFILE_DIR)/targets/*_makefile.inc) @@ -209,6 +216,7 @@ include $(wildcard $(MAKEFILE_DIR)/targets/*_makefile.inc)
ALL_SRCS := \ ALL_SRCS := \
$(MINIMAL_SRCS) \ $(MINIMAL_SRCS) \
@ -52,7 +33,7 @@ index 73c50d3..3cef7fa 100644
$(PROFILER_SRCS) \ $(PROFILER_SRCS) \
$(PROFILER_SUMMARIZER_SRCS) \ $(PROFILER_SUMMARIZER_SRCS) \
$(TF_LITE_CC_SRCS) \ $(TF_LITE_CC_SRCS) \
@@ -225,14 +234,18 @@ LIB_PATH := $(LIBDIR)$(LIB_NAME) @@ -225,14 +233,18 @@ LIB_PATH := $(LIBDIR)$(LIB_NAME)
BENCHMARK_LIB := $(LIBDIR)$(BENCHMARK_LIB_NAME) BENCHMARK_LIB := $(LIBDIR)$(BENCHMARK_LIB_NAME)
BENCHMARK_BINARY := $(BINDIR)$(BENCHMARK_BINARY_NAME) BENCHMARK_BINARY := $(BINDIR)$(BENCHMARK_BINARY_NAME)
MINIMAL_BINARY := $(BINDIR)minimal MINIMAL_BINARY := $(BINDIR)minimal
@ -73,7 +54,7 @@ index 73c50d3..3cef7fa 100644
LIB_OBJS := $(addprefix $(OBJDIR), \ LIB_OBJS := $(addprefix $(OBJDIR), \
$(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(TF_LITE_CC_SRCS))))) $(patsubst %.cc,%.o,$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(TF_LITE_CC_SRCS)))))
@@ -252,7 +265,7 @@ $(OBJDIR)%.o: %.cpp @@ -252,7 +264,7 @@ $(OBJDIR)%.o: %.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@ $(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
# The target that's compiled if there's no command-line arguments. # The target that's compiled if there's no command-line arguments.
@ -82,7 +63,7 @@ index 73c50d3..3cef7fa 100644
# The target that's compiled for micro-controllers # The target that's compiled for micro-controllers
micro: $(LIB_PATH) micro: $(LIB_PATH)
@@ -276,6 +289,14 @@ $(MINIMAL_BINARY): $(MINIMAL_OBJS) $(LIB_PATH) @@ -276,6 +288,14 @@ $(MINIMAL_BINARY): $(MINIMAL_OBJS) $(LIB_PATH)
minimal: $(MINIMAL_BINARY) minimal: $(MINIMAL_BINARY)

@ -1,5 +1,4 @@
#!/bin/bash #!/bin/bash
export LD_LIBRARY_PATH=/usr/local/occlum/x86_64-linux-musl/lib:$LD_LIBRARY_PATH
set -e set -e
show_usage() { show_usage() {

@ -47,9 +47,26 @@ make install
# Remove all source code and build files # Remove all source code and build files
rm -rf ${BUILD_DIR} rm -rf ${BUILD_DIR}
# Link the toolchain directory # Generate the wrappers for executables
ln -sf ${INSTALL_DIR}/bin/${TARGET}-gcc ${INSTALL_DIR}/bin/occlum-gcc cat > ${INSTALL_DIR}/bin/occlum-gcc <<EOF
ln -sf ${INSTALL_DIR}/bin/${TARGET}-g++ ${INSTALL_DIR}/bin/occlum-g++ #!/bin/bash
ln -sf ${INSTALL_DIR}/bin/${TARGET}-ld ${INSTALL_DIR}/bin/occlum-ld ${INSTALL_DIR}/bin/${TARGET}-gcc -fPIC -pie -Wl,-rpath,${INSTALL_DIR}/${TARGET}/lib "\$@"
ln -sf ${INSTALL_DIR}/x86_64-linux-musl/lib/libc.so /lib/ld-musl-x86_64.so.1 EOF
cat > ${INSTALL_DIR}/bin/occlum-g++ <<EOF
#!/bin/bash
${INSTALL_DIR}/bin/${TARGET}-g++ -fPIC -pie -Wl,-rpath,${INSTALL_DIR}/${TARGET}/lib "\$@"
EOF
cat > ${INSTALL_DIR}/bin/occlum-ld <<EOF
#!/bin/bash
${INSTALL_DIR}/bin/${TARGET}-ld -pie -rpath ${INSTALL_DIR}/${TARGET}/lib "\$@"
EOF
chmod +x ${INSTALL_DIR}/bin/occlum-gcc
chmod +x ${INSTALL_DIR}/bin/occlum-g++
chmod +x ${INSTALL_DIR}/bin/occlum-ld
# Make symbolic links
ln -sf ${INSTALL_DIR}/${TARGET}/lib/libc.so /lib/ld-musl-x86_64.so.1
ln -sf ${INSTALL_DIR} /usr/local/occlum ln -sf ${INSTALL_DIR} /usr/local/occlum