Add "make format" and "make format-check" to check format for c/c++ files

This commit is contained in:
Hui, Chunyang 2020-05-26 07:52:22 +00:00
parent cd2f13ae54
commit 03ba13aec7
16 changed files with 145 additions and 8 deletions

22
.astylerc Normal file

@ -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

@ -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=$?

@ -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=$?

1
.gitignore vendored

@ -1,6 +1,7 @@
*.o
*.a
*.so
*.orig
build/
build_sim/
.DS_Store

@ -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"

@ -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

@ -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

@ -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)

@ -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)

@ -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

@ -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

@ -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

13
tools/c_formatter Executable file

@ -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

@ -4,6 +4,7 @@ LABEL maintainer="He Sun <bochang.sh@antfin.com>"
RUN yum update -y && \
yum install -y \
astyle \
autoconf \
automake \
ca-certificates \

@ -4,6 +4,7 @@ LABEL maintainer="Qing Li <geding.lq@alibaba-inc.com>"
RUN apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
alien \
astyle \
autoconf \
automake \
build-essential \

@ -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*