Skip to content
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
28 changes: 25 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,36 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: build-quickjs
run: |
QUICKJS_COMMIT=04be246001599f5995fa2f2d8c91a0f198d3f34c
rm -rf /tmp/quickjs
git init -q /tmp/quickjs
git -C /tmp/quickjs remote add origin https://github.com/bellard/quickjs.git
git -C /tmp/quickjs fetch -q --depth 1 origin ${QUICKJS_COMMIT}
git -C /tmp/quickjs checkout -q FETCH_HEAD
printf '\nCFLAGS_OPT+=-fPIC\n' >> /tmp/quickjs/Makefile
make -C /tmp/quickjs libquickjs.a -j$(nproc)
sudo mkdir -p /usr/local/include/quickjs /usr/local/lib/quickjs
sudo install -m644 /tmp/quickjs/quickjs.h /tmp/quickjs/quickjs-libc.h /usr/local/include/quickjs/
sudo install -m644 /tmp/quickjs/libquickjs.a /usr/local/lib/quickjs/
echo "QUICKJS_ROOT=/usr/local" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=${GITHUB_WORKSPACE}/lib:${LD_LIBRARY_PATH}" >> $GITHUB_ENV

- name: build
run: |
sudo apt update
sudo apt install libssl-dev libnghttp2-dev liblua5.4-dev libprotobuf-dev libprotoc-dev protobuf-compiler
./configure --with-openssl --with-nghttp2 --with-kcp --with-mqtt --with-redis --with-lua --with-rpc
make libhv evpp
sudo apt install libssl-dev libnghttp2-dev libprotobuf-dev libprotoc-dev protobuf-compiler liblua5.4-dev
./configure --with-openssl --with-nghttp2 --with-kcp --with-mqtt --with-redis --with-rpc --with-lua --with-js
make libhv evpp unittest
# hrpc = separate libhrpc (needs protobuf); apt installs protobuf under /usr
make libhrpc hrpc PROTOBUF_PREFIX=/usr
# hvlua = libhv + lua binding
make hvlua
bin/hvlua examples/lua/sleep.lua
# hvjs = libhv + js binding
make hvjs
bin/hvjs examples/js/sleep.js

- name: test
run: |
Expand Down
84 changes: 81 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

set(LIBHV_FIND_DEPENDENCY_OPENSSL FALSE)
set(LIBHV_FIND_DEPENDENCY_QUICKJS FALSE)

option(BUILD_SHARED "build shared library" ON)
option(BUILD_STATIC "build static library" ON)
Expand Down Expand Up @@ -36,6 +37,7 @@ option(WITH_GNUTLS "with gnutls library" OFF)
option(WITH_MBEDTLS "with mbedtls library" OFF)

option(WITH_LUA "with lua library" OFF)
option(WITH_JS "with quickjs library" OFF)

option(WITH_KCP "compile event/kcp" OFF)

Expand Down Expand Up @@ -213,6 +215,57 @@ if(WITH_LUA)
endif()
endif()

if(WITH_JS)
add_definitions(-DWITH_JS)
find_path(QUICKJS_INCLUDE_DIR
NAMES quickjs.h
HINTS
${QUICKJS_ROOT}/include/quickjs
${QUICKJS_ROOT}/include
/opt/homebrew/opt/quickjs/include/quickjs
/opt/homebrew/opt/quickjs/include
/usr/local/opt/quickjs/include/quickjs
/usr/local/opt/quickjs/include
/usr/local/include/quickjs
/usr/local/include
/usr/include/quickjs
/usr/include)
find_library(QUICKJS_LIBRARY
NAMES quickjs libquickjs
HINTS
${QUICKJS_ROOT}/lib/quickjs
${QUICKJS_ROOT}/lib
/opt/homebrew/opt/quickjs/lib/quickjs
/opt/homebrew/opt/quickjs/lib
/usr/local/opt/quickjs/lib/quickjs
/usr/local/opt/quickjs/lib
/usr/local/lib/quickjs
/usr/local/lib
/usr/lib/quickjs
/usr/lib)
if(NOT QUICKJS_INCLUDE_DIR OR NOT QUICKJS_LIBRARY)
message(FATAL_ERROR "WITH_JS requires QuickJS. Set QUICKJS_ROOT or QUICKJS_INCLUDE_DIR and QUICKJS_LIBRARY.")
endif()
if(NOT TARGET QuickJS::QuickJS)
add_library(QuickJS::QuickJS UNKNOWN IMPORTED)
set_target_properties(QuickJS::QuickJS PROPERTIES
IMPORTED_LOCATION "${QUICKJS_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${QUICKJS_INCLUDE_DIR}")
endif()
include_directories(${QUICKJS_INCLUDE_DIR})
set(LIBS ${LIBS} QuickJS::QuickJS)
set(LIBHV_FIND_DEPENDENCY_QUICKJS TRUE)
if(WITH_EVPP AND WITH_HTTP AND WITH_HTTP_CLIENT)
add_definitions(-DHVJS_WITH_HTTP)
endif()
if(WITH_EVPP AND WITH_REDIS)
add_definitions(-DHVJS_WITH_REDIS)
endif()
if(WITH_EVPP AND WITH_MQTT)
add_definitions(-DHVJS_WITH_MQTT)
endif()
endif()

if(WIN32 OR MINGW)
add_definitions(-DWIN32_LEAN_AND_MEAN -D_CRT_SECURE_NO_WARNINGS -D_WIN32_WINNT=0x0600)
set(LIBS ${LIBS} secur32 crypt32 winmm iphlpapi ws2_32)
Expand Down Expand Up @@ -243,7 +296,7 @@ if(APPLE)
endif()

# see Makefile
set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt)
set(ALL_SRCDIRS . base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt js)
set(CORE_SRCDIRS . base ssl event)
if(WIN32 OR MINGW)
if(WITH_WEPOLL)
Expand All @@ -263,7 +316,7 @@ if(WITH_PROTOCOL)
endif()

if(WITH_LUA)
set(LIBHV_HEADERS ${LIBHV_HEADERS} lua/hvlua.h lua/hvlua_json.h lua/hvlua_util.h)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${LUA_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} lua)
if(NOT WITH_EVPP)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS})
Expand All @@ -274,6 +327,10 @@ endif()
if(WITH_EVPP)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${CPPUTIL_HEADERS} ${EVPP_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} cpputil evpp)
if(WITH_JS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${JS_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} js)
endif()
if(WITH_REDIS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${REDIS_HEADERS})
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} redis)
Expand All @@ -286,8 +343,14 @@ if(WITH_EVPP)
endif()
if(WITH_HTTP_SERVER)
set(LIBHV_HEADERS ${LIBHV_HEADERS} ${HTTP_SERVER_HEADERS})
if(WITH_LUA OR WITH_JS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpScriptHandler.h)
endif()
if(WITH_LUA)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpScriptHandler.h http/server/HttpLuaHandler.h)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpLuaHandler.h)
endif()
if(WITH_JS)
set(LIBHV_HEADERS ${LIBHV_HEADERS} http/server/HttpJsHandler.h)
endif()
set(LIBHV_SRCDIRS ${LIBHV_SRCDIRS} http/server)
endif()
Expand All @@ -308,6 +371,15 @@ if(WITH_MQTT)
endif()

list_source_directories(LIBHV_SRCS ${LIBHV_SRCDIRS})
if(NOT WITH_LUA)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpLuaHandler\\.cpp$")
endif()
if(NOT WITH_JS)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpJsHandler\\.cpp$")
endif()
if(NOT WITH_LUA AND NOT WITH_JS)
list(FILTER LIBHV_SRCS EXCLUDE REGEX "(^|/)HttpScriptHandler\\.cpp$")
endif()
if(WIN32)
set(CMAKE_RC_FLAGS_DEBUG -D_DEBUG)
configure_file(${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.rc.in ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.rc)
Expand All @@ -322,6 +394,9 @@ if(BUILD_SHARED)
target_compile_definitions(hv PRIVATE HV_DYNAMICLIB)
target_include_directories(hv PRIVATE ${LIBHV_SRCDIRS}
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(WITH_JS)
target_include_directories(hv INTERFACE $<BUILD_INTERFACE:${QUICKJS_INCLUDE_DIR}>)
endif()
target_link_libraries(hv PUBLIC ${LIBS})
install(TARGETS hv
EXPORT libhvTargets
Expand All @@ -336,6 +411,9 @@ if(BUILD_STATIC)
target_compile_definitions(hv_static PUBLIC HV_STATICLIB)
target_include_directories(hv_static PRIVATE ${LIBHV_SRCDIRS}
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(WITH_JS)
target_include_directories(hv_static INTERFACE $<BUILD_INTERFACE:${QUICKJS_INCLUDE_DIR}>)
endif()
target_link_libraries(hv_static PUBLIC ${LIBS})
if(NOT (WIN32 AND BUILD_SHARED))
set_target_properties(hv_static PROPERTIES OUTPUT_NAME hv)
Expand Down
45 changes: 42 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include config.mk
include Makefile.vars

MAKEF=$(MAKE) -f Makefile.in
ALL_SRCDIRS=. base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt
ALL_SRCDIRS=. base ssl event event/kcp util cpputil evpp redis protocol http http/client http/server mqtt js
CORE_SRCDIRS=. base ssl event
ifeq ($(WITH_KCP), yes)
CORE_SRCDIRS += event/kcp
Expand All @@ -21,14 +21,21 @@ LIBHV_SRCDIRS += protocol
endif

ifeq ($(WITH_LUA), yes)
LIBHV_HEADERS += lua/hvlua.h lua/hvlua_json.h lua/hvlua_util.h
LIBHV_HEADERS += $(LUA_HEADERS)
LIBHV_SRCDIRS += lua
ifneq ($(WITH_EVPP), yes)
LIBHV_HEADERS += $(CPPUTIL_HEADERS)
LIBHV_SRCDIRS += cpputil
endif
endif

ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
LIBHV_HEADERS += $(JS_HEADERS)
LIBHV_SRCDIRS += js
endif
endif

ifeq ($(WITH_EVPP), yes)
LIBHV_HEADERS += $(CPPUTIL_HEADERS) $(EVPP_HEADERS)
LIBHV_SRCDIRS += cpputil evpp
Expand All @@ -49,8 +56,14 @@ endif
ifeq ($(WITH_HTTP_SERVER), yes)
LIBHV_HEADERS += $(HTTP_SERVER_HEADERS)
LIBHV_SRCDIRS += http/server
ifneq ($(filter yes,$(WITH_LUA) $(WITH_JS)),)
LIBHV_HEADERS += http/server/HttpScriptHandler.h
endif
ifeq ($(WITH_LUA), yes)
LIBHV_HEADERS += http/server/HttpScriptHandler.h http/server/HttpLuaHandler.h
LIBHV_HEADERS += http/server/HttpLuaHandler.h
endif
ifeq ($(WITH_JS), yes)
LIBHV_HEADERS += http/server/HttpJsHandler.h
endif
endif

Expand Down Expand Up @@ -115,6 +128,11 @@ ifeq ($(WITH_EVPP), yes)
EXAMPLES += hvlua
endif
endif
ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
EXAMPLES += hvjs
endif
endif

examples: $(EXAMPLES)
@echo "make examples done."
Expand Down Expand Up @@ -228,6 +246,9 @@ host: prepare
hvlua: prepare libhv
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_LUA $(LUA_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ilua -o bin/hvlua examples/hvlua.cpp -Llib -lhv -pthread $(LUA_LIBS)

hvjs: prepare libhv
$(MAKEF) TARGET=$@ SRCDIRS="$(LIBHV_SRCDIRS)" SRCS="examples/hvjs.cpp"

multi-acceptor-processes: prepare
$(MAKEF) TARGET=$@ SRCDIRS="$(CORE_SRCDIRS)" SRCS="examples/multi-thread/multi-acceptor-processes.c"

Expand Down Expand Up @@ -424,6 +445,24 @@ ifeq ($(WITH_REDIS), yes)
endif
endif
endif
ifeq ($(WITH_JS), yes)
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_HTTP), yes)
ifeq ($(WITH_HTTP_SERVER), yes)
ifeq ($(WITH_HTTP_CLIENT), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ijs -Ihttp -Ihttp/server -Ihttp/client -o bin/http_js_handler_test unittest/http_js_handler_test.cpp -Llib -lhv -pthread $(LDFLAGS) $(JS_LIBS)
ifeq ($(WITH_REDIS), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP -DHVJS_WITH_REDIS $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -Iredis -o bin/http_js_redis_test unittest/http_js_redis_test.cpp unittest/redis_test_server.cpp -Llib -lhv -pthread $(LDFLAGS) $(JS_LIBS)
endif
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -o bin/http_js_ws_test unittest/http_js_ws_test.cpp -Llib -lhv -pthread $(LDFLAGS) $(JS_LIBS)
ifeq ($(WITH_MQTT), yes)
$(CXX) -g -Wall -O0 -std=c++11 -DWITH_JS -DHVJS_WITH_HTTP -DHVJS_WITH_MQTT $(JS_CFLAGS) -I. -Ibase -Issl -Ievent -Icpputil -Ievpp -Ihttp -Ihttp/server -Ihttp/client -Imqtt -o bin/http_js_mqtt_test unittest/http_js_mqtt_test.cpp -Llib -lhv -pthread $(LDFLAGS) $(JS_LIBS)
endif
endif
endif
endif
endif
endif

run-unittest: unittest
bash scripts/unittest.sh
Expand Down
33 changes: 33 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ ifeq ($(ALL_SRCS), )
ALL_SRCS = $(wildcard *.c *.cc *.cpp)
endif
override SRCS += $(filter-out %_test.c %_test.cc %_test.cpp, $(ALL_SRCS))
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(WITH_LUA), yes)
override SRCS := $(filter-out %/HttpLuaHandler.cpp HttpLuaHandler.cpp, $(SRCS))
endif
ifneq ($(WITH_JS), yes)
override SRCS := $(filter-out %/HttpJsHandler.cpp HttpJsHandler.cpp, $(SRCS))
endif
ifeq ($(filter yes,$(WITH_LUA) $(WITH_JS)),)
override SRCS := $(filter-out %/HttpScriptHandler.cpp HttpScriptHandler.cpp, $(SRCS))
endif
endif
# OBJS += $(patsubst %.c, %.o, $(SRCS))
# OBJS += $(patsubst %.cc, %.o, $(SRCS))
# OBJS += $(patsubst %.cpp, %.o, $(SRCS))
Expand Down Expand Up @@ -185,6 +196,28 @@ endif
endif
endif

ifeq ($(WITH_JS), yes)
CPPFLAGS += -DWITH_JS $(JS_CFLAGS)
LDFLAGS += $(JS_LIBS)
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_HTTP), yes)
ifeq ($(WITH_HTTP_CLIENT), yes)
CPPFLAGS += -DHVJS_WITH_HTTP
endif
endif
endif
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_REDIS), yes)
CPPFLAGS += -DHVJS_WITH_REDIS
endif
endif
ifeq ($(WITH_EVPP), yes)
ifeq ($(WITH_MQTT), yes)
CPPFLAGS += -DHVJS_WITH_MQTT
endif
endif
endif

CPPFLAGS += $(addprefix -D, $(DEFINES))
CPPFLAGS += $(addprefix -I, $(INCDIRS))
CPPFLAGS += $(addprefix -I, $(SRCDIRS))
Expand Down
17 changes: 16 additions & 1 deletion Makefile.vars
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ INSTALL_INCDIR ?= $(PREFIX)/include/hv
INSTALL_LIBDIR ?= $(PREFIX)/lib

PKG_CONFIG ?= pkg-config

# lua
LUA_PKG_CONFIG ?= $(shell if command -v $(PKG_CONFIG) >/dev/null 2>&1 && $(PKG_CONFIG) --exists lua; then echo lua; fi)
LUA_PREFIX ?= $(shell for dir in /opt/homebrew/opt/lua /usr/local/opt/lua /usr; do if [ -f "$$dir/include/lua/lua.h" ] || [ -f "$$dir/include/lua.h" ]; then echo $$dir; break; fi; done)
LUA_INCLUDE_DIR ?= $(shell if [ -n "$(LUA_PREFIX)" ]; then for dir in "$(LUA_PREFIX)/include/lua" "$(LUA_PREFIX)/include/lua5.5" "$(LUA_PREFIX)/include/lua5.4" "$(LUA_PREFIX)/include/lua5.3" "$(LUA_PREFIX)/include"; do if [ -f "$$dir/lua.h" ]; then echo $$dir; break; fi; done; fi)
LUA_CFLAGS ?= $(shell if [ -n "$(LUA_PKG_CONFIG)" ]; then $(PKG_CONFIG) --cflags $(LUA_PKG_CONFIG); elif [ -n "$(LUA_INCLUDE_DIR)" ]; then echo -I$(LUA_INCLUDE_DIR); fi)
LUA_LIBS ?= $(shell if [ -n "$(LUA_PKG_CONFIG)" ]; then $(PKG_CONFIG) --libs $(LUA_PKG_CONFIG); elif [ -n "$(LUA_PREFIX)" ]; then echo -L$(LUA_PREFIX)/lib -llua; else echo -llua; fi)

# quickjs
QUICKJS_ROOT ?= $(shell for dir in /opt/homebrew/opt/quickjs /usr/local/opt/quickjs /usr; do if [ -f "$$dir/include/quickjs/quickjs.h" ] || [ -f "$$dir/include/quickjs.h" ]; then echo $$dir; break; fi; done)
QUICKJS_INCLUDE_DIR ?= $(shell if [ -n "$(QUICKJS_ROOT)" ]; then for dir in "$(QUICKJS_ROOT)/include/quickjs" "$(QUICKJS_ROOT)/include"; do if [ -f "$$dir/quickjs.h" ]; then echo $$dir; break; fi; done; fi)
QUICKJS_LIB_DIR ?= $(shell if [ -n "$(QUICKJS_ROOT)" ]; then for dir in "$(QUICKJS_ROOT)/lib/quickjs" "$(QUICKJS_ROOT)/lib"; do if [ -f "$$dir/libquickjs.a" ] || [ -f "$$dir/libquickjs.dylib" ] || [ -f "$$dir/libquickjs.so" ]; then echo $$dir; break; fi; done; fi)
JS_CFLAGS ?= $(shell if [ -n "$(QUICKJS_INCLUDE_DIR)" ]; then echo -I$(QUICKJS_INCLUDE_DIR); fi)
JS_LIBS ?= $(shell if [ -n "$(QUICKJS_LIB_DIR)" ]; then echo -L$(QUICKJS_LIB_DIR) -lquickjs; else echo -lquickjs; fi)

BASE_HEADERS = base/hplatform.h\
\
base/hdef.h\
Expand Down Expand Up @@ -119,4 +128,10 @@ HTTP_SERVER_HEADERS = http/server/HttpServer.h\
http/server/WebSocketServer.h\

MQTT_HEADERS = mqtt/mqtt_protocol.h\
mqtt/mqtt_client.h\
mqtt/mqtt_client.h

LUA_HEADERS = lua/hvlua.h\
lua/hvlua_util.h\
lua/hvlua_json.h

JS_HEADERS = js/hvjs.h
Loading
Loading