Skip to content

Commit

Permalink
sdk: support window
Browse files Browse the repository at this point in the history
NUGU SDK supports window using msys2.

Signed-off-by: Jeantracker <hyojoong.kim.jean@gmail.com>
  • Loading branch information
JeanTracker committed Apr 25, 2024
1 parent f0bc6f5 commit d72b532
Show file tree
Hide file tree
Showing 23 changed files with 566 additions and 38 deletions.
57 changes: 40 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,23 @@ ELSE()
ENDIF()
ENDIF()

SET(BUILTIN_CURL_DEFAULT "OFF")
SET(PULSEAUDIO_DEFAULT "OFF")
SET(PORTAUDIO_PLUGIN_DEFAULT "OFF")

IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
SET(BUILTIN_CURL_DEFAULT "OFF")
SET(PULSEAUDIO_DEFAULT "OFF")
SET(ASAN_DEFAULT "OFF")

ADD_DEFINITIONS(
-DNUGU_PLUGIN_FILE_EXTENSION=".dylib"
)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
ADD_DEFINITIONS(
# Run-time buffer overflow detection
-D_FORTIFY_SOURCE=2

-DNUGU_PLUGIN_FILE_EXTENSION=".dll"
)
ELSE()
SET(BUILTIN_CURL_DEFAULT "ON")
SET(PULSEAUDIO_DEFAULT "ON")
Expand Down Expand Up @@ -208,6 +217,12 @@ ELSE()
SET(LDFLAG_SOCKET "")
ENDIF()

IF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
SET(LDFLAG_WINSOCK "-lws2_32")
ELSE()
SET(LDFLAG_WINSOCK "")
ENDIF()

SET(BUILD_LIBRARY ON)
SET(BUILD_PLUGINS ON)
SET(BUILD_EXAMPLES ON)
Expand Down Expand Up @@ -247,10 +262,16 @@ IF(VENDOR_PKGCONFIG)
pkg_check_modules(vendor_pkgs REQUIRED ${VENDOR_PKGCONFIG})
ENDIF()

IF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
SET(GIO_PKGCONFIG gio-windows-2.0)
ELSE()
SET(GIO_PKGCONFIG gio-unix-2.0)
ENDIF()

IF(BUILD_LIBRARY)
# Set NUGU SDK library default dependency
SET(DEFAULT_LIB_DEPENDENCY
glib-2.0 gio-2.0 gio-unix-2.0 gthread-2.0 openssl
glib-2.0 gio-2.0 gthread-2.0 openssl ${GIO_PKGCONFIG}
${CURL_PKGCONFIG} ${VENDOR_PKGCONFIG} ${RAPIDJSON_PKGCONFIG})

# Sets the option so that the built library has a higher link order than
Expand Down Expand Up @@ -342,25 +363,27 @@ IF (CMAKE_COMPILER_IS_GNUCC)
STRING(APPEND CMAKE_C_FLAGS " -Wno-analyzer-malloc-leak")
ENDIF()

ADD_COMPILE_OPTIONS(
# Non-executable stack
-Wa,--execstack
IF(NOT CMAKE_SYSTEM_NAME MATCHES "MINGW")
ADD_COMPILE_OPTIONS(
# Non-executable stack
-Wa,--execstack

# Eliminate unused code and data (with --gc-sections link option)
-Wl,--gc-sections)
# Eliminate unused code and data (with --gc-sections link option)
-Wl,--gc-sections)

LINK_LIBRARIES(
# Link only needed libraries
-Wl,--as-needed
LINK_LIBRARIES(
# Link only needed libraries
-Wl,--as-needed

# Enforces symbol resolution at build time
-Wl,-z,defs
# Enforces symbol resolution at build time
-Wl,-z,defs

# Non-executable stack
-Wl,-z,noexecstack
# Non-executable stack
-Wl,-z,noexecstack

# Debian packaging default link flags
-Wl,-Bsymbolic-functions -Wl,-z,relro)
# Debian packaging default link flags
-Wl,-Bsymbolic-functions -Wl,-z,relro)
ENDIF()

# RPATH is useful only for testing without installation.
# Please use the '-DPACKAGING' option for debian packaging.
Expand Down
2 changes: 1 addition & 1 deletion examples/capability_injection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FILE(GLOB_RECURSE SRC *.cc)
ADD_EXECUTABLE(nugu_capability_injection ${SRC})
TARGET_LINK_LIBRARIES(nugu_capability_injection ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_capability_injection ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_capability_injection libnugu)
INSTALL(TARGETS nugu_capability_injection RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/profiling/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ADD_EXECUTABLE(nugu_prof main_prof.cc)
TARGET_LINK_LIBRARIES(nugu_prof ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_prof ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_prof libnugu)
INSTALL(TARGETS nugu_prof RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
INSTALL(DIRECTORY testcases DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/nugu)
2 changes: 1 addition & 1 deletion examples/response_filter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ADD_EXECUTABLE(nugu_response_filter main.cc filter.cc)
TARGET_LINK_LIBRARIES(nugu_response_filter ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_response_filter ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_response_filter libnugu)
INSTALL(TARGETS nugu_response_filter RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/simple_asr/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ADD_EXECUTABLE(nugu_simple_asr main_asr.cc)
TARGET_LINK_LIBRARIES(nugu_simple_asr ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_simple_asr ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_simple_asr libnugu)
INSTALL(TARGETS nugu_simple_asr RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/simple_play/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ADD_EXECUTABLE(nugu_simple_play main_play.cc)
TARGET_LINK_LIBRARIES(nugu_simple_play ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_simple_play ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_simple_play libnugu)
INSTALL(TARGETS nugu_simple_play RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/simple_text/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ADD_EXECUTABLE(nugu_simple_text main_text.cc)
TARGET_LINK_LIBRARIES(nugu_simple_text ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_simple_text ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_simple_text libnugu)
INSTALL(TARGETS nugu_simple_text RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/simple_tts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ADD_EXECUTABLE(nugu_simple_tts main_tts.cc)
TARGET_LINK_LIBRARIES(nugu_simple_tts ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(nugu_simple_tts ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(nugu_simple_tts libnugu)
INSTALL(TARGETS nugu_simple_tts RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
2 changes: 1 addition & 1 deletion examples/standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ TARGET_INCLUDE_DIRECTORIES(${TARGET_APP} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
capability
clientkit)
TARGET_LINK_LIBRARIES(${TARGET_APP} ${pkgs_LDFLAGS} -lnugu)
TARGET_LINK_LIBRARIES(${TARGET_APP} ${pkgs_LDFLAGS} -lnugu -lstdc++)
ADD_DEPENDENCIES(${TARGET_APP} libnugu)

# install
Expand Down
3 changes: 3 additions & 0 deletions externals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ include(ExternalProject)
FILE(GLOB_RECURSE njson_files njson/src/*.cpp)
ADD_LIBRARY(njson STATIC ${njson_files})
TARGET_INCLUDE_DIRECTORIES(njson PRIVATE njson/include)
IF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
TARGET_COMPILE_OPTIONS(njson PRIVATE -Wno-class-memaccess)
ENDIF()
INSTALL(DIRECTORY njson/include/njson
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nugu/)

Expand Down
3 changes: 3 additions & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ MACRO(DEFINE_PLUGIN name)
ADD_LIBRARY(${name} SHARED ${name}.c)
TARGET_LINK_LIBRARIES(${name} ${pkgs_LDFLAGS} -lnugu)
SET_TARGET_PROPERTIES(${name} PROPERTIES PREFIX "" OUTPUT_NAME ${name})
IF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
SET_TARGET_PROPERTIES(${name} PROPERTIES SUFFIX ".dll")
ENDIF()
INSTALL(TARGETS ${name} LIBRARY DESTINATION ${plugindir} COMPONENT libnugu_component)
ADD_DEPENDENCIES(${name} libnugu)
ENDIF()
Expand Down
9 changes: 9 additions & 0 deletions plugins/gstreamer.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ static int _create(NuguPlayerDriver *driver, NuguPlayer *player)
goto error_out;
}

#ifdef __MSYS__
gh->audio_sink =
gst_element_factory_make("directsoundsink", audio_sink);
if (!gh->audio_sink) {
nugu_error("create gst_element for 'directsoundsink' failed");
goto error_out;
}
#else
#ifdef ENABLE_PULSEAUDIO
gh->audio_sink = gst_element_factory_make("pulsesink", audio_sink);
if (!gh->audio_sink) {
Expand All @@ -443,6 +451,7 @@ static int _create(NuguPlayerDriver *driver, NuguPlayer *player)
goto error_out;
}
#endif
#endif

#ifdef ENABLE_GSTREAMER_PLUGIN_VOLUME
gh->volume = gst_element_factory_make("volume", volume);
Expand Down
9 changes: 9 additions & 0 deletions plugins/gstreamer_pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,14 @@ static int _create_gst_elements(struct pa_audio_param *pcm_param)
}
#endif

#ifdef __MSYS__
pcm_param->audio_sink =
gst_element_factory_make("directsoundsink", audio_sink);
if (!pcm_param->audio_sink) {
nugu_error("create gst_element for 'directsoundsink' failed");
goto error_out;
}
#else
#ifdef ENABLE_PULSEAUDIO
pcm_param->audio_sink =
gst_element_factory_make("pulsesink", audio_sink);
Expand All @@ -439,6 +447,7 @@ static int _create_gst_elements(struct pa_audio_param *pcm_param)
goto error_out;
}
#endif
#endif

#ifdef ENABLE_GSTREAMER_PLUGIN_VOLUME
gst_bin_add_many(GST_BIN(pcm_param->pipeline),
Expand Down
15 changes: 11 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ ELSE()
ENDIF()

TARGET_LINK_LIBRARIES(libnugu PUBLIC
${CURL_LIBRARY} njson
${CURL_LIBRARY} njson -lstdc++
${pkgs_LDFLAGS}
${builtin_plugin_pkgs_LDFLAGS}
${BUILTIN_PLUGIN_LIBS}
${LDFLAG_SOCKET} ${LDFLAG_DL})
${LDFLAG_SOCKET}
${LDFLAG_WINSOCK}
${LDFLAG_DL}
${pkgs_LDFLAGS})

FOREACH(lib ${BUILTIN_PLUGIN_DEPS})
ADD_DEPENDENCIES(libnugu ${lib})
Expand All @@ -45,8 +48,12 @@ ELSE()
TARGET_LINK_LIBRARIES(libnugu PUBLIC ${gstreamer_pkgs_LDFLAGS})
ENDIF()

SET_TARGET_PROPERTIES(libnugu
PROPERTIES VERSION ${VERSION} SOVERSION ${VERSION_MAJOR} OUTPUT_NAME nugu)
IF(CMAKE_SYSTEM_NAME MATCHES "MINGW")
SET_TARGET_PROPERTIES(libnugu PROPERTIES PREFIX "" SUFFIX ".dll" OUTPUT_NAME libnugu)
ELSE()
SET_TARGET_PROPERTIES(libnugu
PROPERTIES VERSION ${VERSION} SOVERSION ${VERSION_MAJOR} OUTPUT_NAME nugu)
ENDIF()

INSTALL(TARGETS libnugu LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT libnugu_component)
3 changes: 2 additions & 1 deletion src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ ADD_LIBRARY(objhttp2 OBJECT
network/http2/v1_policies.cc
network/http2/v2_events.c
network/dg_registry.c
network/dg_server.c)
network/dg_server.c
network/nugu_winsock.cc)
TARGET_INCLUDE_DIRECTORIES(objhttp2 PRIVATE
network
${CMAKE_CURRENT_SOURCE_DIR}
Expand Down
56 changes: 55 additions & 1 deletion src/base/network/http2/http2_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#ifdef HAVE_EVENTFD
#include <sys/eventfd.h>
#elif defined(__MSYS__)
#include "network/nugu_winsock.h"
#else
#include <glib-unix.h>
#endif
Expand Down Expand Up @@ -80,6 +82,10 @@ struct _http2_network {

/* thread creation check */
ThreadSync *sync_init;

#ifdef __MSYS__
NuguWinSocket *wsock;
#endif
};

static void _curl_code_to_result(HTTP2Request *req, CURLcode code)
Expand Down Expand Up @@ -325,7 +331,9 @@ static void *_loop(void *data)
char *fake_p;
int still_running = 0;
int i;
#ifndef __MSYS__
struct curl_waitfd extra_fds[1];
#endif

#ifdef HAVE_PTHREAD_SETNAME_NP
#ifdef __APPLE__
Expand Down Expand Up @@ -364,7 +372,24 @@ static void *_loop(void *data)

_process_completed(net, curl_message);
}
#ifdef __MSYS__
mc = curl_multi_wait(net->handle, NULL, 0, 1000, &numfds);
if (mc != CURLM_OK) {
nugu_error("curl_multi_wait failed, code %d.", mc);
break;
}

if (nugu_winsock_check_for_data(net->wakeup_fds[0]) == 0) {
char ev;

if (nugu_winsock_read(net->wakeup_fds[0], &ev,
sizeof(ev)) < 0) {
nugu_error("error read");
continue;
}
_process_async_queue(net);
}
#else
/*
* wait for activity, timeout or "nothing"
*
Expand Down Expand Up @@ -398,6 +423,7 @@ static void *_loop(void *data)

_process_async_queue(net);
}
#endif
}

/* remove incomplete requests */
Expand Down Expand Up @@ -433,7 +459,7 @@ static void *_loop(void *data)
HTTP2Network *http2_network_new(void)
{
struct _http2_network *net;
#ifndef HAVE_EVENTFD
#if !defined(HAVE_EVENTFD) && !defined(__MSYS__)
GError *error = NULL;
#endif

Expand All @@ -446,6 +472,18 @@ HTTP2Network *http2_network_new(void)
net->wakeup_fds[0] = -1;
net->wakeup_fds[1] = -1;

#ifdef __MSYS__
net->wsock = nugu_winsock_create();
if (net->wsock == NULL) {
nugu_error("failed to create window socket");
free(net);
return NULL;
}
net->wakeup_fds[0] =
nugu_winsock_get_handle(net->wsock, NUGU_WINSOCKET_CLIENT);
net->wakeup_fds[1] =
nugu_winsock_get_handle(net->wsock, NUGU_WINSOCKET_SERVER);
#else
#ifdef HAVE_EVENTFD
net->wakeup_fds[0] = eventfd(0, EFD_CLOEXEC);
if (net->wakeup_fds[0] < 0) {
Expand All @@ -462,6 +500,7 @@ HTTP2Network *http2_network_new(void)
}
nugu_dbg("pipe fds[0] = %d", net->wakeup_fds[0]);
nugu_dbg("pipe fds[1] = %d", net->wakeup_fds[1]);
#endif
#endif

net->requests =
Expand Down Expand Up @@ -501,10 +540,14 @@ void http2_network_free(HTTP2Network *net)
if (net->requests)
g_async_queue_unref(net->requests);

#ifdef __MSYS__
nugu_winsock_remove(net->wsock);
#else
if (net->wakeup_fds[0] != -1)
close(net->wakeup_fds[0]);
if (net->wakeup_fds[1] != -1)
close(net->wakeup_fds[1]);
#endif

if (net->sync_init)
thread_sync_free(net->sync_init);
Expand Down Expand Up @@ -558,6 +601,16 @@ int http2_network_wakeup(HTTP2Network *net)

g_return_val_if_fail(net != NULL, -1);

#ifdef __MSYS__
if (net->wakeup_fds[1] != -1) {
char ev = '1';

written =
nugu_winsock_write(net->wakeup_fds[1], &ev, sizeof(ev));
if (written != sizeof(ev))
nugu_error("error wakeup");
}
#else
/* wakeup request using eventfd */
if (net->wakeup_fds[1] == -1) {
uint64_t ev = 1;
Expand All @@ -572,6 +625,7 @@ int http2_network_wakeup(HTTP2Network *net)
if (written != sizeof(ev))
nugu_error("write failed");
}
#endif

return 0;
}
Expand Down
Loading

0 comments on commit d72b532

Please sign in to comment.