Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang-format, Go Format check and commit-message hook #5

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 120
AlignEscapedNewlines: Left
AlignAfterOpenBracket: AlwaysBreak
#
# Bind * to the type rather than the name.
PointerAlignment: Left
#
# Put function name on separate line from return type.
AlwaysBreakAfterReturnType: All
#
# Put arguments either all on same line or on separate lines.
BinPackArguments: false
#
# Put function parameters on separate lines.
BinPackParameters: false
#
# Open brace goes on new line only when starting a new struct, enum, or func.
BreakBeforeBraces: Mozilla
#
# Don't sort includes in alphabetical order because Windows headers are odd.
SortIncludes: false
20 changes: 20 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
repos:
- repo: local
hooks:
- id: clang-format
name: clang-format
entry: ./git-hooks/clang-format-pre-commit.sh
language: system
files: \.(c|cpp|h|hpp)$
stages: [commit]
- id: check-commit-message
name: Check Commit Message
entry: ./git-hooks/check-commit-message.sh
language: script
stages: [commit-msg]
- id: go-format
name: go-format
entry: ./git-hooks/go-format-pre-commit.sh
language: system
files: \.(go)$
stages: [commit]
92 changes: 92 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Contributing

This project welcomes contributions and suggestions. Most contributions require you to
agree to a Contributor License Agreement (CLA) declaring that you have the right to,
and actually do, grant us the rights to use your contribution. For details, visit
https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need
to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the
instructions provided by the bot. You will only need to do this once across all repositories using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

# Contribution Guidelines

We welcome contributions from everyone. The following guidelines will help you understand our expectations and streamline the review process.

## Code Style

We follow the LLVM coding style for all C/C++ code. Please ensure your code conforms to this style. To automate this process, we use `clang-format`:

```sh
clang-format -i path/to/your/file.cpp
```

It is recommended to install the [Clang-Format extension](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format) in Visual Studio Code to format your code automatically.

For go code, we follow the standard Go formatting guidelines. You can format your code using the `gofmt` tool:

```sh
gofmt -w path/to/your/file.go
```

## Commit Messages

Please follow these guidelines for commit messages:

- Use the present tense ("Add feature" not "Added feature").
- Use the imperative mood ("Move cursor to..." not "Moves cursor to...").
- Limit the first line to 72 characters or less.
- Reference issues and pull requests liberally.

## Pre-commit Hooks

We use pre-commit hooks to ensure code quality and consistency. These hooks will check your code for proper formatting before allowing a commit.

### Setting up pre-commit hooks for the first time

1. Install the pre-commit package, for example, in Ubuntu you can run:
```sh
sudo apt install pre-commit
```

2. Install the pre-commit hooks:
```sh
pre-commit install --hook-type commit-msg
pre-commit install
```

3. Manually run the hooks on all files (optional but recommended for first-time setup):
```sh
pre-commit run --all-files
```

4. Auto Format when you commit:
```sh
export AUTO_FORMAT=1
```

## Submitting a Pull Request

1. Ensure your code follows the style guide and passes all tests.
2. Commit your changes with a descriptive commit message.
3. Push your changes to your fork:
```sh
git push origin your-branch
```
4. Create a pull request against the `main` branch of the upstream repository.

In your pull request, please include:
- A descriptive title and detailed description of the changes.
- Reference any relevant issues or pull requests.

## Additional Resources

- [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html)
- [Clang-Format Documentation](https://clang.llvm.org/docs/ClangFormat.html)
- [GitHub Guides: Forking Projects](https://guides.github.com/activities/forking/)
- [Pre-commit Documentation](https://pre-commit.com/)
- [Go Formatting Guidelines](https://golang.org/doc/effective_go.html#formatting)
11 changes: 10 additions & 1 deletion examples/first_example_ipc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ else ifeq ($(BUILD_TYPE),AddressSanitizer)
DEBUG_CFLAGS = -fsanitize=address
endif

CLANG_FORMAT_BIN ?= clang-format
AGENT_NAME := example_app
PRIMARY_NAME := example_collect_control
CODELET_NAME := example_codelet.o
Expand All @@ -22,7 +23,15 @@ CODELET_CFLAGS := -O2 -target bpf -Wall -DJBPF_DEBUG_ENABLED -D__x86_64__

.PHONY: all clean

all: clean schema codelet agent primary
all: clang-format clean schema codelet agent primary

clang-format:
@if command -v ${CLANG_FORMAT_BIN} > /dev/null; then \
echo "Running clang-format check..."; \
${CLANG_FORMAT_BIN} --style=file --dry-run --Werror ${AGENT_FILE} ${PRIMARY_FILE} ${CODELET_FILE}; \
else \
echo "clang-format not found, skipping..."; \
fi

codelet: ${CODELET_FILE}
${CODELET_CC} ${CODELET_CFLAGS} ${INCLUDES} -c ${CODELET_FILE} -o ${CODELET_NAME}
Expand Down
28 changes: 13 additions & 15 deletions examples/first_example_ipc/example_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,33 @@ DECLARE_JBPF_HOOK(
example,
struct jbpf_generic_ctx ctx,
ctx,
HOOK_PROTO(packet *p, int ctx_id),
HOOK_ASSIGN(ctx.ctx_id = ctx_id; ctx.data = (uint64_t)(void *)p; ctx.data_end = (uint64_t)(void *)(p + 1);))
HOOK_PROTO(packet* p, int ctx_id),
HOOK_ASSIGN(ctx.ctx_id = ctx_id; ctx.data = (uint64_t)(void*)p; ctx.data_end = (uint64_t)(void*)(p + 1);))

DEFINE_JBPF_HOOK(example)

bool done = false;

void sig_handler(int signo)
void
sig_handler(int signo)
{
done = true;
}

int handle_signal()
int
handle_signal()
{
if (signal(SIGINT, sig_handler) == SIG_ERR)
{
if (signal(SIGINT, sig_handler) == SIG_ERR) {
return 0;
}
if (signal(SIGTERM, sig_handler) == SIG_ERR)
{
if (signal(SIGTERM, sig_handler) == SIG_ERR) {
return 0;
}
return -1;
}

int main(int argc, char **argv)
int
main(int argc, char** argv)
{

struct jbpf_config jbpf_config = {0};
Expand All @@ -65,15 +66,13 @@ int main(int argc, char **argv)
"%s",
JBPF_DEFAULT_LCM_SOCKET);

if (!handle_signal())
{
if (!handle_signal()) {
std::cout << "Could not register signal handler" << std::endl;
return -1;
}

// Initialize jbpf
if (jbpf_init(&jbpf_config) < 0)
{
if (jbpf_init(&jbpf_config) < 0) {
return -1;
}

Expand All @@ -83,8 +82,7 @@ int main(int argc, char **argv)
int i = 0;

// Sample application code calling a hook every second
while (!done)
{
while (!done) {
packet p;
p.seq_no = i;
p.value = -i;
Expand Down
20 changes: 9 additions & 11 deletions examples/first_example_ipc/example_codelet.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ struct jbpf_load_map_def SEC("maps") counter = {

SEC("jbpf_generic")
uint64_t
jbpf_main(void *state)
jbpf_main(void* state)
{

void *c;
void* c;
int cnt;
struct jbpf_generic_ctx *ctx;
struct jbpf_generic_ctx* ctx;
packet *p, *p_end;
packet echo;
manual_ctrl_event resp = {0};
Expand All @@ -44,12 +44,12 @@ jbpf_main(void *state)
if (!c)
return 1;

cnt = *(int *)c;
cnt = *(int*)c;
cnt++;
*(uint32_t *)c = cnt;
*(uint32_t*)c = cnt;

p = (packet *)ctx->data;
p_end = (packet *)ctx->data_end;
p = (packet*)ctx->data;
p_end = (packet*)ctx->data_end;

if (p + 1 > p_end)
return 1;
Expand All @@ -58,13 +58,11 @@ jbpf_main(void *state)

// Copy the data that was passed to the codelet to the outmap ringbuffer
// and send them out.
if (jbpf_ringbuf_output(&outmap, &echo, sizeof(echo)) < 0)
{
if (jbpf_ringbuf_output(&outmap, &echo, sizeof(echo)) < 0) {
return 1;
}

if (jbpf_control_input_receive(&inmap, &resp, sizeof(resp)) == 1)
{
if (jbpf_control_input_receive(&inmap, &resp, sizeof(resp)) == 1) {
// Print a debug message. This helper function should NOT be used in production environments, due to
// its performance overhead. The helper function will be ignored, if *jbpf* has been built with the
// USE_JBPF_PRINTF_HELPER option set to OFF.
Expand Down
Loading