Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
tpaukrt committed Dec 19, 2022
0 parents commit a947336
Show file tree
Hide file tree
Showing 11 changed files with 1,407 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/OBJ*
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2016-2022 Tomas Paukrt

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2022 Tomas Paukrt

# list of static libraries to be built
STATIC_LIBS = library

# list of programs to be built
PROGRAMS = server tool

# list of additional dependencies
DEPENDS = Makefile Setup.mk

# target specific settings
library_NAME = libxbus
server_NAME = xbusd
tool_NAME = xbus
tool_LIBS = library

# build setup and rules
include Setup.mk
include Rules.mk
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Version history

## 1.0.0 (2022-12-19)

* Initial release
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Simple Interprocess Communication Bus

## Introduction

The xbus is a simple interprocess communication bus based on
publish/subscribe messaging over a UNIX domain socket that was
designed for use in embedded devices with limited memory.

It consists of a daemon that acts as a message broker, a tiny
library that provides basic functions for communication and
a command line tool that is useful for debugging as well as for
writing shell scripts.

## Examples

### Publisher

```C
#include "xbus.h"

int main(void)
{
xbus_publish("sms/incoming", "Hello, World!");

return 0;
}
```
### Subscriber
```C
#include <stdio.h>
#include "xbus.h"
int main(void)
{
char *topic, *payload;
xbus_subscribe("sms/*");
while (1) {
payload = xbus_receive(&topic);
printf("[%s]\n%s\n\n", topic, payload);
}
}
```

## Prerequisites

* GNU Make 3.81+
* GCC 4.9+

## Build instructions

Execute the following command:

```
make [DEBUG=1] [ASAN=1] [UBSAN=1] [V=1]
```

## Install instructions

Execute the following command:

```
make install [DESTDIR=<dir>]
```

## Directory structure

```
xbus
|
|--OBJ.* Output directories with built files
|
|--library IPC bus library (client API)
|
|--server IPC bus server (message broker)
|
|--tool IPC bus command line tool
|
|--LICENSE BSD 3-clause license
|--Makefile Main Makefile
|--NEWS.md Version history
|--README.md This file
|--Rules.mk Build rules
+--Setup.mk Build setup
```

## License

The code is available under the BSD 3-clause license.
See the `LICENSE` file for the full license text.
146 changes: 146 additions & 0 deletions Rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2022 Tomas Paukrt

# default subdirectory for object files
OBJDIR ?= OBJ

# default directories for installation
BINDIR ?= /usr/bin
LIBDIR ?= /usr/lib
INCDIR ?= /usr/include

# build verbosity
ifeq ($(V),1)
Q :=
E := @true
else
Q := @
E := @echo
endif

# build serialization
ifneq ($(filter clean%, $(MAKECMDGOALS)),)
.NOTPARALLEL:
endif

# default target
all: build

# target for building all binary files
build:

# target for installing all binary and header files
install:

# target for uninstalling all binary and header files
uninstall:

# target for cleaning all binary and temporary files
clean:
$(Q) rm -rf $(firstword $(subst ., ,$(OBJDIR)))*

# function to generate rules for building, installing and uninstalling a target binary file
define generate-rules
build: build-$(1)

install: install-$(1)

uninstall: uninstall-$(1)

build-$(1): $(OBJDIR)/$(1)/$($(1)_FILE)

install-$(1): build-$(1)
$(Q) $($(1)_PRE_INSTALL)
$(if $($(1)_PATH),
$(E) " CP $(1)/$($(1)_FILE)"
$(Q) install -D -m $($(1)_MODE) $(OBJDIR)/$(1)/$($(1)_FILE) $(DESTDIR)$($(1)_PATH)/$($(1)_FILE)
$(foreach FILE, $($(1)_HEADER_LIST),
$(E) " CP $(1)/$(FILE)"
$(Q) install -D -m 644 $(1)/$(FILE) $(DESTDIR)$($(1)_HEADER_PATH)/$(FILE)
)
)
$(Q) $($(1)_INSTALL)
$(Q) $($(1)_POST_INSTALL)

uninstall-$(1):
$(Q) $($(1)_PRE_UNINSTALL)
$(Q) $($(1)_UNINSTALL)
$(if $($(1)_PATH),
$(E) " RM $(1)/$($(1)_FILE)"
$(Q) rm -f $(DESTDIR)$($(1)_PATH)/$($(1)_FILE)
$(foreach FILE, $($(1)_HEADER_LIST),
$(E) " RM $(1)/$(FILE)"
$(Q) rm -f $(DESTDIR)$($(1)_HEADER_PATH)/$(FILE)
)
)
$(Q) $($(1)_POST_UNINSTALL)

clean-$(1):
$(Q) rm -rf $(OBJDIR)/$(1)

$($(1)_OBJECT_DIRS):
$(Q) mkdir -p $$@

$(OBJDIR)/$(1)/%.o: $(1)/%.c $(DEPENDS) | $($(1)_OBJECT_DIRS)
$(E) " CC $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) $(CCACHE) $($(1)_COMPILER) -MMD -MP $(CPPFLAGS) $($(1)_CPPFLAGS) -fPIC $(CFLAGS) $($(1)_CFLAGS) -c $$< -o $$@

$(OBJDIR)/$(1)/%.o: $(1)/%.cc $(DEPENDS) | $($(1)_OBJECT_DIRS)
$(E) " CC $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) $(CCACHE) $($(1)_COMPILER) -MMD -MP $(CPPFLAGS) $($(1)_CPPFLAGS) -fPIC $(CXXFLAGS) $($(1)_CXXFLAGS) -c $$< -o $$@

$(OBJDIR)/$(1)/%.o: $(1)/%.cpp $(DEPENDS) | $($(1)_OBJECT_DIRS)
$(E) " CC $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) $(CCACHE) $($(1)_COMPILER) -MMD -MP $(CPPFLAGS) $($(1)_CPPFLAGS) -fPIC $(CXXFLAGS) $($(1)_CXXFLAGS) -c $$< -o $$@

$(OBJDIR)/$(1)/$($(1)_NAME): $($(1)_OBJECT_LIST)
$(E) " LD $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) $(CCACHE) $($(1)_LINKER) -pie $(LDFLAGS) $($(1)_LDFLAGS) -o $$@ $$(filter %.o, $$^) $($(1)_LDLIBS) $(LDLIBS)

$(OBJDIR)/$(1)/$($(1)_NAME).so: $($(1)_OBJECT_LIST)
$(E) " LD $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) $(CCACHE) $($(1)_LINKER) -shared $(LDFLAGS) $($(1)_LDFLAGS) -o $$@ $$(filter %.o, $$^) $($(1)_LDLIBS) $(LDLIBS)

$(OBJDIR)/$(1)/$($(1)_NAME).a: $($(1)_OBJECT_LIST)
$(E) " AR $$(patsubst $(OBJDIR)/%,%,$$@)"
$(Q) rm -f $$@
$(Q) $(AR) rcs $$@ $$(filter %.o, $$^)

-include $($(1)_DEPEND_LIST)
endef

# auxiliary function to set basic properties of a target binary file
define set-properties
$(eval $(1)_NAME ?= $(1))
$(eval $(1)_PATH ?= $(2))
$(eval $(1)_MODE ?= $(3))
$(eval $(1)_FILE := $($(1)_NAME)$(4))
$(eval $(1)_HEADER_PATH ?= $(INCDIR))
$(eval $(1)_HEADER_LIST ?= $(if $(4), $(foreach EXT, h hh hpp, $(patsubst $(1)/%, %, $(foreach DIR, $(1) $(1)/*, $(wildcard $(DIR)/*.$(EXT)))))))
$(eval $(1)_SOURCE_LIST ?= $(foreach EXT, c cc cpp, $(patsubst $(1)/%, %, $(foreach DIR, $(1) $(1)/*, $(sort $(wildcard $(DIR)/*.$(EXT)))))))
$(eval $(1)_DEPEND_LIST := $(foreach EXT, c cc cpp, $(patsubst %.$(EXT), $(OBJDIR)/$(1)/%.d, $(filter %.$(EXT), $($(1)_SOURCE_LIST)))))
$(eval $(1)_OBJECT_LIST := $(foreach EXT, c cc cpp, $(patsubst %.$(EXT), $(OBJDIR)/$(1)/%.o, $(filter %.$(EXT), $($(1)_SOURCE_LIST)))))
$(eval $(1)_OBJECT_DIRS := $(sort $(dir $(addprefix $(OBJDIR)/$(1)/, $($(1)_SOURCE_LIST)))))
$(eval $(1)_COMPILER ?= $(if $(filter %.cc %.cpp, $($(1)_SOURCE_LIST)), $(CXX), $(CC)))
$(eval $(1)_LINKER ?= $($(1)_COMPILER))
endef

# auxiliary function to set dependencies of a target binary file on internal and external libraries
define set-dependencies
$(foreach LIB, $(filter $(wildcard *), $($(1)_LIBS)),
$(OBJDIR)/$(1)/$($(1)_FILE) : $(OBJDIR)/$(LIB)/$($(LIB)_FILE)
$(eval $(1)_CPPFLAGS += -I$(LIB))
$(eval $(1)_LDFLAGS += -L$(OBJDIR)/$(LIB))
$(eval $(1)_LDLIBS += $(patsubst lib%, -l%, $($(LIB)_NAME)))
)
$(foreach LIB, $(filter-out $(wildcard *), $($(1)_LIBS)),
$(eval $(1)_LDLIBS += $(patsubst lib%, -l%, $(LIB)))
)
endef

# generate auxiliary variables and build rules for each target binary file
$(foreach DIR, $(PROGRAMS), $(eval $(call set-properties,$(DIR),$(BINDIR),755)))
$(foreach DIR, $(SHARED_LIBS), $(eval $(call set-properties,$(DIR),$(LIBDIR),755,.so)))
$(foreach DIR, $(STATIC_LIBS), $(eval $(call set-properties,$(DIR),$(LIBDIR),644,.a)))
$(foreach DIR, $(STATIC_LIBS) $(SHARED_LIBS) $(PROGRAMS), $(eval $(call set-dependencies,$(DIR))))
$(foreach DIR, $(STATIC_LIBS) $(SHARED_LIBS) $(PROGRAMS), $(eval $(call generate-rules,$(DIR))))
36 changes: 36 additions & 0 deletions Setup.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2022 Tomas Paukrt

# ccache command
CCACHE := $(shell command -v ccache 2> /dev/null)

# subdirectory for object files
OBJDIR := OBJ

# basic compiler flags
CFLAGS += -Wall -Wextra -Wshadow -Wmissing-declarations -Wformat-security

# extra compiler flags for debug and release build
ifeq ($(DEBUG),1)
OBJDIR := $(OBJDIR).debug
CFLAGS += -O0 -ggdb
CPPFLAGS += -DDEBUG
else
OBJDIR := $(OBJDIR).release
CFLAGS += -O2
LDFLAGS += -s
endif

# extra compiler flags for address sanitizer
ifeq ($(ASAN),1)
OBJDIR := $(OBJDIR).asan
CFLAGS += -fsanitize=address -fno-omit-frame-pointer
LDFLAGS += -fsanitize=address
endif

# extra compiler flags for undefined behavior sanitizer
ifeq ($(UBSAN),1)
OBJDIR := $(OBJDIR).ubsan
CFLAGS += -fsanitize=undefined -fno-omit-frame-pointer
LDFLAGS += -fsanitize=undefined
endif
Loading

0 comments on commit a947336

Please sign in to comment.