diff --git a/.astylerc b/.astylerc new file mode 100644 index 00000000..80762262 --- /dev/null +++ b/.astylerc @@ -0,0 +1,22 @@ +# Bracket style +style=java # +j # Add braces to one-line conditional statements +keep-one-line-blocks # Don't break blocks that are on one line + +# Indent style +indent-switches +indent=spaces + +# Padding +pad-oper # Put spaces around operators +pad-header # Put spaces between if/while/for etc. and the first paren + +# Pointers +align-pointer=name # Align the * next to the variable name + +# Line wrapping +max-code-length=90 # 90 character line limit +break-after-logical # For if statements, wrap to the next line after logical operator + +# Line endings +lineend=linux # LF line endings diff --git a/.githooks/post-commit b/.githooks/post-commit index a17b4796..346ff708 100755 --- a/.githooks/post-commit +++ b/.githooks/post-commit @@ -1,4 +1,21 @@ #!/bin/bash + +if ! hash astyle; then + echo "You do not have astyle installed so your code style is not being checked!" + exit 1 +fi + +info=$(make format-check) +if [ -n "$info" ]; then + echo "Format Error detected:" + echo + echo "$info" + echo + echo "Please run \`make format\` before next commit." + echo + exit 1 +fi + cd src/libos output=$(cargo fmt -- --check 2>&1) retval=$? diff --git a/.githooks/pre-push b/.githooks/pre-push index c65d3cde..a658b3ef 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -19,6 +19,22 @@ # This sample shows how to prevent push of commits where the log message starts # with "WIP" (work in progress). +if ! hash astyle; then + echo "You do not have astyle installed so your code style is not being checked!" + exit 0 +fi + +info=$(make format-check) +if [ -n "$info" ]; then + echo "Format Error detected:" + echo + echo "$info" + echo + echo "Please run \`make format\` before next commit." + echo + exit 1 +fi + cd src/libos output=$(cargo fmt -- --check 2>&1) retval=$? diff --git a/.gitignore b/.gitignore index 12be6184..953bf531 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.o *.a *.so +*.orig build/ build_sim/ .DS_Store diff --git a/.travis.yml b/.travis.yml index 85c67ce0..2aa8e4f8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,8 @@ services: - docker script: -- docker run -v /home/travis/build/occlum/occlum:/root/occlum occlum/occlum:0.12.0-ubuntu18.04 /bin/bash -c "cd /root/occlum; make submodule; SGX_MODE=SIM make; SGX_MODE=SIM make test" +- docker run -v /home/travis/build/occlum/occlum:/root/occlum occlum/occlum:0.12.0-ubuntu18.04 /bin/bash -c "cd /root/occlum; \ + info=$(make format-check); \ + if [ -n "$info" ]; then echo "Format error detected."; exit 1; \ + fi; \ + make submodule; SGX_MODE=SIM make; SGX_MODE=SIM make test" diff --git a/Makefile b/Makefile index e3d97760..ba95d470 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all submodule githooks src test tools install clean +.PHONY: all submodule githooks src test tools install format format-check clean all: src @@ -84,6 +84,16 @@ install: install -d $(OCCLUM_PREFIX)/etc/template/ install -t $(OCCLUM_PREFIX)/etc/template/ -m 444 etc/template/* +format: + @$(MAKE) --no-print-directory -C test format + @$(MAKE) --no-print-directory -C tools format + @$(MAKE) --no-print-directory -C src format + +format-check: + @$(MAKE) --no-print-directory -C test format-check + @$(MAKE) --no-print-directory -C tools format-check + @$(MAKE) --no-print-directory -C src format-check + clean: @$(MAKE) --no-print-directory -C src clean @$(MAKE) --no-print-directory -C test clean diff --git a/src/Makefile b/src/Makefile index cf18b5e6..e9e97ae9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean +.PHONY: all format format-check clean all: @$(MAKE) --no-print-directory -C libos @@ -6,6 +6,14 @@ all: @$(MAKE) --no-print-directory -C run @$(MAKE) --no-print-directory -C exec +format: + @$(MAKE) --no-print-directory -C pal format + @$(MAKE) --no-print-directory -C run format + +format-check: + @$(MAKE) --no-print-directory -C pal format-check + @$(MAKE) --no-print-directory -C run format-check + clean: @$(MAKE) --no-print-directory -C libos clean @$(MAKE) --no-print-directory -C pal clean diff --git a/src/pal/Makefile b/src/pal/Makefile index a092284b..cbb4c412 100644 --- a/src/pal/Makefile +++ b/src/pal/Makefile @@ -11,6 +11,7 @@ C_SRCS := $(sort $(wildcard src/*.c src/*/*.c)) C_OBJS := $(addprefix $(BUILD_DIR)/src/pal/,$(C_SRCS:.c=.o)) CXX_SRCS := $(sort $(wildcard src/*.cpp src/*/*.cpp)) CXX_OBJS := $(addprefix $(BUILD_DIR)/src/pal/,$(CXX_SRCS:.cpp=.o)) +HEADER_FILES := $(sort $(wildcard include/*.h include/*/*.h)) C_COMMON_FLAGS := -I$(BUILD_DIR)/src/pal/src -Iinclude -Iinclude/edl C_FLAGS := $(C_COMMON_FLAGS) $(SGX_CFLAGS_U) @@ -20,7 +21,7 @@ LINK_FLAGS += -Wl,--version-script=pal.lds ALL_BUILD_SUBDIRS := $(sort $(patsubst %/,%,$(dir $(LIBOCCLUM_PAL_SO) $(EDL_C_OBJS) $(C_OBJS) $(CXX_OBJS)))) -.PHONY: all clean +.PHONY: all format format-check clean all: $(ALL_BUILD_SUBDIRS) $(LIBOCCLUM_PAL_SO) @@ -55,5 +56,11 @@ $(LIBSGX_USTDC_A): @cp $(RUST_SGX_SDK_DIR)/sgx_ustdc/libsgx_ustdc.a $(LIBSGX_USTDC_A) @echo "GEN <= $@" +format: $(C_SRCS) $(CXX_SRCS) $(HEADER_FILES) + @$(C_FORMATTER) $^ + +format-check: $(C_SRCS) $(CXX_SRCS) $(HEADER_FILES) + @$(C_FORMATTER) --check $^ + clean: @-$(RM) -f $(LIBOCCLUM_PAL_SO) $(LIBSGX_USTDC_A) $(C_OBJS) $(CXX_OBJS) $(EDL_C_OBJS) $(EDL_C_SRCS) diff --git a/src/run/Makefile b/src/run/Makefile index ad2b99fa..1cdadd85 100644 --- a/src/run/Makefile +++ b/src/run/Makefile @@ -11,7 +11,7 @@ LINK_FLAGS := $(SGX_LFLAGS_U) -L$(BUILD_DIR)/lib -lsgx_uprotected_fs -locclum-pa ALL_BUILD_SUBDIRS := $(sort $(patsubst %/,%,$(dir $(BIN) $(C_OBJS)))) -.PHONY: all clean +.PHONY: all format format-check clean all: $(ALL_BUILD_SUBDIRS) $(BIN) @@ -26,5 +26,11 @@ $(BUILD_DIR)/src/run/%.o: %.c @$(CC) $(C_FLAGS) -c $< -o $@ @echo "CC <= $@" +format: $(C_SRCS) $(CXX_SRCS) + @$(C_FORMATTER) $^ + +format-check: $(C_SRCS) $(CXX_SRCS) + @$(C_FORMATTER) --check $^ + clean: @-$(RM) -f $(BIN) $(C_OBJS) diff --git a/src/sgxenv.mk b/src/sgxenv.mk index 61181057..83394472 100644 --- a/src/sgxenv.mk +++ b/src/sgxenv.mk @@ -7,6 +7,8 @@ SGX_SDK ?= /opt/intel/sgxsdk SGX_MODE ?= HW SGX_ARCH ?= x64 +C_FORMATTER := $(PROJECT_DIR)/tools/c_formatter + ifneq ($(SGX_MODE), HW) BUILD_DIR := $(PROJECT_DIR)/build_sim else diff --git a/test/Makefile b/test/Makefile index 16c1e1a5..866bb51e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,6 +2,11 @@ CUR_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) PROJECT_DIR := $(realpath $(CUR_DIR)/../) SGX_MODE ?= HW +C_SRCS := $(wildcard */*.c */*.h) +CXX_SRCS := $(wildcard */*.cc) + +C_FORMATTER := $(PROJECT_DIR)/tools/c_formatter + ifneq ($(SGX_MODE), HW) BUILD_DIR := $(PROJECT_DIR)/build_sim else @@ -25,7 +30,7 @@ BENCHES := spawn_and_exit_latency pipe_throughput unix_socket_throughput BUILD_TARGETS := $(TEST_DEPS) $(TESTS) $(BENCHES) TEST_TARGETS := $(TESTS:%=test-%) BENCH_TARGETS := $(BENCHES:%=bench-%) -.PHONY: all prebuild build postbuild test clean $(BUILD_TARGETS) $(TEST_TARGETS) $(BENCH_TARGETS) +.PHONY: all prebuild build postbuild test clean $(BUILD_TARGETS) $(TEST_TARGETS) $(BENCH_TARGETS) format format-check # Use echo program instead of built-in echo command in shell. This ensures # that echo can recognize escaped sequences (with -e argument) regardless of @@ -117,5 +122,11 @@ $(BENCH_TARGETS): bench-%: % # Misc ############################################################################# +format: $(C_SRCS) $(CXX_SRCS) + @$(C_FORMATTER) $^ + +format-check: $(C_SRCS) $(CXX_SRCS) + @$(C_FORMATTER) --check $^ + clean: @$(RM) -rf $(BUILD_DIR)/test diff --git a/tools/Makefile b/tools/Makefile index 64f1af89..32a4188c 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -6,7 +6,7 @@ else BUILD_DIR := build endif -.PHONY: all clean +.PHONY: all format format-check clean all: @mkdir -p ../$(BUILD_DIR)/bin/ @@ -15,5 +15,11 @@ all: @ln -s -f ../../tools/occlum-gen-default-occlum-json ../$(BUILD_DIR)/bin/occlum-gen-default-occlum-json @$(MAKE) --no-print-directory -C protect-integrity +format: + @$(MAKE) --no-print-directory -C protect-integrity format + +format-check: + @$(MAKE) --no-print-directory -C protect-integrity format-check + clean: @$(MAKE) --no-print-directory -C protect-integrity clean diff --git a/tools/c_formatter b/tools/c_formatter new file mode 100755 index 00000000..bd93cfbb --- /dev/null +++ b/tools/c_formatter @@ -0,0 +1,13 @@ +#!/bin/bash + +current_dir=$(pwd) +script_dir=$(dirname $0) +project_dir="$script_dir/.." +astyle_cfg="$project_dir/.astylerc" +relative_path=$(realpath --relative-to=$project_dir $current_dir) + +if [ $1 == "--check" ]; then + astyle --options=$astyle_cfg --dry-run ${@:2} 2>&1 | sed -e "s|Formatted |Format Error in $relative_path/|g" | grep --color=always "^Format.*" || true +else + astyle --options=$astyle_cfg $@ 2>&1 | sed -e "s|Formatted |Formatted $relative_path/|g" | grep --color=always "^Formatted.*" || true +fi diff --git a/tools/docker/Dockerfile.centos7.5 b/tools/docker/Dockerfile.centos7.5 index adfa5220..696348cf 100644 --- a/tools/docker/Dockerfile.centos7.5 +++ b/tools/docker/Dockerfile.centos7.5 @@ -4,6 +4,7 @@ LABEL maintainer="He Sun " RUN yum update -y && \ yum install -y \ + astyle \ autoconf \ automake \ ca-certificates \ diff --git a/tools/docker/Dockerfile.ubuntu18.04 b/tools/docker/Dockerfile.ubuntu18.04 index 792ee9f0..f9266704 100644 --- a/tools/docker/Dockerfile.ubuntu18.04 +++ b/tools/docker/Dockerfile.ubuntu18.04 @@ -4,6 +4,7 @@ LABEL maintainer="Qing Li " RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ alien \ + astyle \ autoconf \ automake \ build-essential \ diff --git a/tools/protect-integrity/Makefile b/tools/protect-integrity/Makefile index 58928456..28f2ad34 100644 --- a/tools/protect-integrity/Makefile +++ b/tools/protect-integrity/Makefile @@ -8,6 +8,8 @@ SGX_MODE ?= HW SGX_ARCH ?= x64 SGX_DEBUG ?= 1 +C_FORMATTER := $(PROJECT_DIR)/tools/c_formatter + ifneq ($(SGX_MODE), HW) BUILD_DIR := $(PROJECT_DIR)/build_sim else @@ -57,6 +59,7 @@ endif APP_C_FILES := App/Enclave_u.c APP_CPP_FILES := App/App.cpp +APP_HEADER_FILES := App/App.h APP_INCLUDE_PATHS := -IInclude -IApp -I$(SGX_SDK)/include -I$(OBJS_DIR)/App APP_C_FLAGS := $(SGX_COMMON_CFLAGS) -fPIC -Wno-attributes $(APP_INCLUDE_PATHS) @@ -101,6 +104,7 @@ CRYPTO_LIBRARY_NAME := sgx_tcrypto ENCLAVE_C_FILES := Enclave/Enclave_t.c ENCLAVE_CPP_FILES := Enclave/Enclave.cpp +ENCLAVE_HEADER_FILES := Enclave/Enclave.h ENCLAVE_INCLUDE_PATHS := -IInclude -IEnclave -I$(SGX_SDK)/include -I$(SGX_SDK)/include/tlibc -I$(SGX_SDK)/include/stlport -I$(OBJS_DIR)/Enclave ENCLAVE_C_FLAGS := $(SGX_COMMON_CFLAGS) -nostdinc -fvisibility=hidden -fpie -fPIC -fstack-protector $(ENCLAVE_INCLUDE_PATHS) @@ -140,7 +144,7 @@ endif endif -.PHONY: all test clean +.PHONY: all test format format-check clean ifeq ($(BUILD_MODE), HW_RELEASE) all: $(APP_NAME) $(ENCLAVE_NAME) @@ -218,5 +222,13 @@ test: all random.txt random.txt: @base64 /dev/urandom | head -c 10000000 > random.txt + +format: $(APP_HEADER_FILES) $(APP_CPP_FILES) $(ENCLAVE_HEADER_FILES) $(ENCLAVE_CPP_FILES) + @$(C_FORMATTER) $^ + +format-check: $(APP_HEADER_FILES) $(APP_CPP_FILES) $(ENCLAVE_HEADER_FILES) $(ENCLAVE_CPP_FILES) + @$(C_FORMATTER) --check $^ + + clean: @rm -f $(APP_NAME) $(ENCLAVE_NAME) $(SIGNED_ENCLAVE_NAME) $(APP_OBJS) $(OBJS_DIR)/App/Enclave_u.* $(ENCLAVE_OBJS) $(OBJS_DIR)/Enclave/Enclave_t.* *.test.txt random.txt*