Improve libsyncthing

* Do *not* build Syncthing in addition as standalone executalbe
* Explose instead all of Syncthing's commands via the library
  interface
* Make all of Syncthing's commands (including `serve`) accessible
  via Syncthing Tray's executable
* Remove copied code for directory creation and instead use
  upstream's code directly
This commit is contained in:
Martchus 2023-05-29 17:29:41 +02:00
parent 7358c25d28
commit b9853043fb
6 changed files with 56 additions and 27 deletions

View File

@ -123,7 +123,7 @@ file(
"${SYNCTHING_PATH}/c-bindings/*.go"
"${SYNCTHING_PATH}/c-bindings/*.h"
"${SYNCTHING_PATH}/c-bindings/*.c"
"${SYNCTHING_PATH}/cmd/syncthing/cli/*.go"
"${SYNCTHING_PATH}/cmd/syncthing/*.go"
"${SYNCTHING_PATH}/lib/*.go"
"${SYNCTHING_PATH}/lib/*.h"
"${SYNCTHING_PATH}/lib/*.c")
@ -187,19 +187,6 @@ add_custom_command(
WORKING_DIRECTORY "${SYNCTHING_PATH}"
COMMENT "Building Syncthing itself (as library)")
# add target to compile Syncthing as standalone executable (helpful for testing)
set(SYNCTHING_EXECUTABLE_PATH "${CMAKE_CURRENT_BINARY_DIR}/syncthing")
add_custom_target(
syncthing_executable
COMMAND
"CC=${CMAKE_C_COMPILER}" "CXX=${CMAKE_CXX_COMPILER}" "AR=${CMAKE_C_COMPILER_AR}" "GOOS=${GO_TARGET_OS}"
"CGO_CFLAGS=${CGO_CFLAGS}" "CGO_CXXFLAGS=${CGO_CXXFLAGS}" "CGO_LDFLAGS=${CGO_LDFLAGS}" "GOARCH=${GO_TARGET_ARCH}"
"CGO_ENABLED=1" "GO111MODULE=on" "GOPATH=${GO_DEVELOPMENT_PATH}" "GOFLAGS=${GO_FLAGS}" "${GO_BIN}" build -v
-buildmode exe -o "${SYNCTHING_EXECUTABLE_PATH}" -ldflags "${GO_LINKER_FLAGS}" ./cmd/syncthing
DEPENDS ${SRC_FILES_SYNCTHING}
WORKING_DIRECTORY "${SYNCTHING_PATH}"
COMMENT "Building Syncthing itself (as executable, ${SYNCTHING_EXECUTABLE_PATH})")
# find c++utilities to use CMake modules and headers from it privately
find_package(${PACKAGE_NAMESPACE_PREFIX}c++utilities${CONFIGURATION_PACKAGE_SUFFIX} 5.0.0 REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${CPP_UTILITIES_MODULE_DIRS})

@ -1 +1 @@
Subproject commit a2a6a3d3f0fda2b665ca97168608e6d2a390cc20
Subproject commit b3d2cc90542c2d6a617f41152d6897294df15577

View File

@ -213,15 +213,37 @@ string longSyncthingVersion()
}
/*!
* \brief Runs Syncthing's CLI with the specified \a args.
* \brief Sets the \a command and arguments to be run via ::libst_run_cli().
*/
long long runCli(const std::vector<const char *> &args)
static void setArguments(
const char *command, std::vector<const char *>::const_iterator argumentsBegin, std::vector<const char *>::const_iterator argumentsEnd)
{
::libst_clear_cli_args();
for (const auto *const arg : args) {
::libst_append_cli_arg(gostr(arg));
::libst_clear_cli_args(gostr(command));
for (; argumentsBegin != argumentsEnd; ++argumentsBegin) {
::libst_append_cli_arg(gostr(*argumentsBegin));
}
}
/*!
* \brief Runs Syncthing's top-level command "cli" with the specified \a arguments.
*/
long long runCli(const std::vector<const char *> &arguments)
{
setArguments("cli", arguments.cbegin(), arguments.cend());
return ::libst_run_cli();
}
/*!
* \brief Runs the Syncthing command using the specified \a arguments.
*/
long long runCommand(const std::vector<const char *> &arguments)
{
if (arguments.empty()) {
setArguments("--help", std::vector<const char *>::const_iterator(), std::vector<const char *>::const_iterator());
} else {
setArguments(arguments.front(), ++arguments.begin(), arguments.end());
}
return ::libst_run_main();
}
} // namespace LibSyncthing

View File

@ -59,7 +59,8 @@ LIB_SYNCTHING_EXPORT std::int64_t stopSyncthing();
LIB_SYNCTHING_EXPORT std::string ownDeviceId();
LIB_SYNCTHING_EXPORT std::string syncthingVersion();
LIB_SYNCTHING_EXPORT std::string longSyncthingVersion();
LIB_SYNCTHING_EXPORT long long runCli(const std::vector<const char *> &args);
LIB_SYNCTHING_EXPORT long long runCli(const std::vector<const char *> &arguments);
LIB_SYNCTHING_EXPORT long long runCommand(const std::vector<const char *> &arguments);
} // namespace LibSyncthing

View File

@ -31,7 +31,8 @@ class InterfaceTests : public TestFixture {
CPPUNIT_TEST(testVersion);
CPPUNIT_TEST(testRunWithoutConfig);
CPPUNIT_TEST(testRunWithConfig);
CPPUNIT_TEST(testCli);
CPPUNIT_TEST(testRunCli);
CPPUNIT_TEST(testRunCommand);
CPPUNIT_TEST_SUITE_END();
public:
@ -41,7 +42,8 @@ public:
void testVersion();
void testRunWithoutConfig();
void testRunWithConfig();
void testCli();
void testRunCli();
void testRunCommand();
void setUp() override;
void tearDown() override;
@ -254,7 +256,15 @@ void InterfaceTests::testRunWithConfig()
/*!
* \brief Tests running Syncthing's CLI.
*/
void InterfaceTests::testCli()
void InterfaceTests::testRunCli()
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("run arbitrary CLI command", 0ll, runCli({ "config", "version", "--help" }));
}
/*!
* \brief Tests running Syncthing command.
*/
void InterfaceTests::testRunCommand()
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("run arbitrary CLI command", 0ll, runCommand({ "--help" }));
}

View File

@ -181,16 +181,21 @@ static int runApplication(int argc, const char *const *argv)
widgetsGuiArg.addSubArguments({ &windowedArg, &showWebUiArg, &triggerArg, &waitForTrayArg, &connectionArg, &configPathArg, &singleInstanceArg,
&newInstanceArg, &replaceArg, &showWizardArg, &assumeFirstLaunchArg, &wipArg });
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
auto cliArg = OperationArgument("cli", 'c', "run Syncthing's CLI");
auto cliHelp = ConfigValueArgument("help", 'h', "show help for Syncthing's CLI");
auto cliArg = OperationArgument("cli", 'c', "runs Syncthing's CLI");
auto cliHelp = ConfigValueArgument("help", 'h', "shows help for Syncthing's CLI");
cliArg.setRequiredValueCount(Argument::varValueCount);
cliArg.setFlags(Argument::Flags::Greedy, true);
cliArg.setSubArguments({ &cliHelp });
auto syncthingArg = OperationArgument("syncthing", '\n', "runs Syncthing");
auto syncthingHelp = ConfigValueArgument("help", 'h', "lists Syncthing's top-level commands");
syncthingArg.setRequiredValueCount(Argument::varValueCount);
syncthingArg.setFlags(Argument::Flags::Greedy, true);
syncthingArg.setSubArguments({ &syncthingHelp });
#endif
parser.setMainArguments({ &qtConfigArgs.qtWidgetsGuiArg(),
#ifdef SYNCTHINGTRAY_USE_LIBSYNCTHING
&cliArg,
&cliArg, &syncthingArg,
#endif
&parser.noColorArg(), &parser.helpArg(), &quitArg });
@ -201,6 +206,10 @@ static int runApplication(int argc, const char *const *argv)
CMD_UTILS_START_CONSOLE;
return static_cast<int>(LibSyncthing::runCli(cliArg.values()));
}
if (syncthingArg.isPresent()) {
CMD_UTILS_START_CONSOLE;
return static_cast<int>(LibSyncthing::runCommand(syncthingArg.values()));
}
#endif
// quit already running application if quit is present