summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2020-05-03 19:59:19 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2020-06-05 07:14:25 +0100
commit13aa30e479a662086b09a90b611fb62426199656 (patch)
treea9697b1ee76ab5a94ed8a28870ce1540a51fc163
parent43f87fb93a782851f9cd5ce0ca9a0a2608994ccd (diff)
downloadlibgit2-13aa30e479a662086b09a90b611fb62426199656.tar.gz
libgit2client: a client "middleware" library
Introduce libgit2client, a client "middleware" library. This is an experimental set of utility functions and classes for client software that builds on top of libgit2. This library might contain - for example - code that invokes filters or other tools. This is incredibly useful to share and reuse among consumers but should be excluded from libgit2 itself. Users may, understandably, not want code that executes arbitrary other commands in libgit2 itself.
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/git2client.h14
-rw-r--r--include/git2client/global.h44
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/libgit2client/CMakeLists.txt104
-rw-r--r--src/libgit2client/global.c55
-rw-r--r--src/libgit2client/win32/precompiled.c1
-rw-r--r--src/libgit2client/win32/precompiled.h4
8 files changed, 224 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fb5202fd1..21230e30d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -38,6 +38,7 @@ OPTION(SONAME "Set the (SO)VERSION of the target" ON)
OPTION(BUILD_SHARED_LIBS "Build Shared Library (OFF for Static)" ON)
OPTION(THREADSAFE "Build libgit2 as threadsafe" ON)
OPTION(BUILD_CLAR "Build Tests using the Clar suite" ON)
+OPTION(BUILD_CLIENT "Build the client \"middleware\" library" ON)
OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF)
OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF)
OPTION(ENABLE_TRACE "Enables tracing support" ON)
diff --git a/include/git2client.h b/include/git2client.h
new file mode 100644
index 000000000..8fffefd06
--- /dev/null
+++ b/include/git2client.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#ifndef INCLUDE_git_client_h__
+#define INCLUDE_git_client_h__
+
+#include <git2.h>
+#include "git2client/global.h"
+
+#endif
diff --git a/include/git2client/global.h b/include/git2client/global.h
new file mode 100644
index 000000000..7d58230b4
--- /dev/null
+++ b/include/git2client/global.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_client_global_h__
+#define INCLUDE_git_client_global_h__
+
+#include "git2.h"
+
+GIT_BEGIN_DECL
+
+/**
+ * Init the global state
+ *
+ * This function must be called before any other libgit2 client function
+ * in order to set up libgit2 and libgit2client global state.
+ *
+ * This function may be called multiple times - it will return the number
+ * of times the initialization has been called (including this one) that have
+ * not subsequently been shutdown.
+ *
+ * @return the number of initializations of the library, or an error code.
+ */
+GIT_EXTERN(int) git_client_init(void);
+
+/**
+ * Shutdown the global state
+ *
+ * Clean up the global state and threading context after calling it as
+ * many times as `git_libgit2_init()` was called - it will return the
+ * number of remainining initializations that have not been shutdown
+ * (after this one).
+ *
+ * @return the number of remaining initializations of the library, or an
+ * error code.
+ */
+GIT_EXTERN(int) git_client_shutdown(void);
+
+/** @} */
+GIT_END_DECL
+#endif
+
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index eeaecc971..5e9a3aa16 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -281,3 +281,4 @@ configure_file(util/features.h.in generated/features.h)
add_subdirectory(util)
add_subdirectory(libgit2)
+add_subdirectory(libgit2client)
diff --git a/src/libgit2client/CMakeLists.txt b/src/libgit2client/CMakeLists.txt
new file mode 100644
index 000000000..c79a9c276
--- /dev/null
+++ b/src/libgit2client/CMakeLists.txt
@@ -0,0 +1,104 @@
+include(PkgBuildConfig)
+
+list(APPEND LIBGIT2CLIENT_INCLUDES
+ "${COMMON_INCLUDES}"
+ "${libgit2_BINARY_DIR}/src"
+ "${libgit2_SOURCE_DIR}/src/util"
+ "${libgit2_SOURCE_DIR}/src/libgit2client"
+ "${libgit2_SOURCE_DIR}/include")
+
+file(GLOB LIBGIT2CLIENT_SRC_H
+ "${libgit2_SOURCE_DIR}/include/git2.h"
+ "${libgit2_SOURCE_DIR}/include/git2/*.h"
+ "${libgit2_SOURCE_DIR}/include/git2/sys/*.h")
+list(SORT LIBGIT2CLIENT_SRC_H)
+
+if(WIN32 AND NOT CYGWIN)
+ if(MSVC)
+ set(LIBGIT2CLIENT_WIN_RC "win32/git2.rc")
+ endif()
+
+ file(GLOB LIBGIT2CLIENT_SRC_OS win32/*.c win32/*.h)
+ list(SORT LIBGIT2CLIENT_SRC_OS)
+else()
+ file(GLOB LIBGIT2CLIENT_SRC_OS unix/*.c unix/*.h)
+ list(SORT LIBGIT2CLIENT_SRC_OS)
+endif()
+
+file(GLOB LIBGIT2CLIENT_SRC_C *.c *.h)
+list(SORT LIBGIT2CLIENT_SRC_C)
+
+set(LIBGIT2CLIENT_SRC ${LIBGIT2CLIENT_SRC_H} ${LIBGIT2CLIENT_SRC_C} ${LIBGIT2CLIENT_SRC_OS})
+
+add_definitions(-DGIT_DEPRECATE_HARD)
+
+add_library(libgit2client OBJECT ${LIBGIT2CLIENT_SRC})
+set_target_properties(libgit2client PROPERTIES C_STANDARD 90)
+ide_split_sources(libgit2client)
+list(APPEND LIBGIT2CLIENT_OBJECTS ${COMMON_OBJECTS})
+list(APPEND LIBGIT2CLIENT_OBJECTS $<TARGET_OBJECTS:libgit2client>)
+list(APPEND LIBGIT2CLIENT_OBJECTS $<TARGET_OBJECTS:util>)
+
+target_include_directories(libgit2client PRIVATE ${LIBGIT2CLIENT_INCLUDES} PUBLIC ${libgit2_SOURCE_DIR}/include)
+target_include_directories(libgit2client SYSTEM PRIVATE ${LIBGIT2CLIENT_SYSTEM_INCLUDES})
+
+if(XCODE_VERSION)
+ # This is required for Xcode to actually link the libgit2 library
+ # when using only object libraries.
+ file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.c "")
+ list(APPEND LIBGIT2CLIENT_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/dummy.c)
+endif()
+
+# Compile and link libgit2
+add_library(libgit2client_meta ${WIN_RC} ${LIBGIT2CLIENT_OBJECTS})
+target_link_libraries(libgit2client_meta ${LIBGIT2CLIENT_LIBS})
+target_link_libraries(libgit2client_meta libgit2_meta)
+
+set_target_properties(libgit2client_meta PROPERTIES C_STANDARD 90)
+set_target_properties(libgit2client_meta PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
+set_target_properties(libgit2client_meta PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
+set_target_properties(libgit2client_meta PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${libgit2_BINARY_DIR})
+
+# Workaround for Cmake bug #0011240 (see http://public.kitware.com/Bug/view.php?id=11240)
+# Win64+MSVC+static libs = linker error
+if(MSVC AND GIT_ARCH_64 AND NOT BUILD_SHARED_LIBS)
+ set_target_properties(libgit2client_meta PROPERTIES STATIC_LIBRARY_FLAGS "/MACHINE:x64")
+endif()
+
+ide_split_sources(libgit2client_meta)
+
+if(SONAME)
+ set_target_properties(libgit2client_meta PROPERTIES VERSION ${LIBGIT2_VERSION_STRING})
+ set_target_properties(libgit2client_meta PROPERTIES SOVERSION ${LIBGIT2_SOVERSION})
+ set_target_properties(libgit2client_meta PROPERTIES OUTPUT_NAME git2client)
+endif()
+
+pkg_build_config(NAME libgit2client
+ VERSION ${LIBGIT2_VERSION_STRING}
+ DESCRIPTION "A library to support building Git clients."
+ LIBS_SELF libgit2client_meta
+ PRIVATE_LIBS ${LIBGIT2CLIENT_PC_LIBS}
+ REQUIRES ${LIBGIT2CLIENT_PC_REQUIRES})
+
+if(MSVC_IDE)
+ # Precompiled headers
+ set_target_properties(libgit2client_meta PROPERTIES COMPILE_FLAGS "/Yuprecompiled.h /FIprecompiled.h")
+ set_source_files_properties(win32/precompiled.c COMPILE_FLAGS "/Ycprecompiled.h")
+endif()
+
+# Install
+install(TARGETS libgit2client_meta
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
+
+install(DIRECTORY ${libgit2_SOURCE_DIR}/include/git2client DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install(FILES ${libgit2_SOURCE_DIR}/include/git2client.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+
+# export these variables for the test framework; it links our object
+# files directly (instead of linking to libgit2.so) so that it can
+# test private and internal functions.
+set_property(GLOBAL PROPERTY libgit2client_objects ${LIBGIT2CLIENT_OBJECTS})
+set_property(GLOBAL PROPERTY libgit2client_includes ${LIBGIT2CLIENT_INCLUDES})
+set_property(GLOBAL PROPERTY libgit2client_system_includes ${LIBGIT2CLIENT_SYSTEM_INCLUDES})
+set_property(GLOBAL PROPERTY libgit2client_libs ${LIBGIT2CLIENT_LIBS})
diff --git a/src/libgit2client/global.c b/src/libgit2client/global.c
new file mode 100644
index 000000000..0a1fcface
--- /dev/null
+++ b/src/libgit2client/global.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include <git2.h>
+#include <git2client.h>
+#include "git2_util.h"
+#include "runtime.h"
+#include "alloc.h"
+#include "hash.h"
+
+#ifdef GIT_WIN32
+# include "win32/w32_crtdbg_stacktrace.h"
+# include "win32/w32_stack.h"
+#endif
+
+static void libgit2_shutdown(void);
+
+static int libgit2_init(void)
+{
+ if (git_libgit2_init() < 0)
+ return -1;
+
+ return git_runtime_shutdown_register(libgit2_shutdown);
+}
+
+static void libgit2_shutdown(void)
+{
+ git_libgit2_shutdown();
+}
+
+int git_client_init(void)
+{
+ static git_runtime_init_fn init_fns[] = {
+ libgit2_init,
+
+#if defined(GIT_MSVC_CRTDBG)
+ git_win32__crtdbg_stacktrace_init,
+ git_win32__stack_init,
+#endif
+ git_allocator_global_init,
+ git_threads_global_init,
+ git_hash_global_init,
+ };
+
+ return git_runtime_init(init_fns, ARRAY_SIZE(init_fns));
+}
+
+int git_client_shutdown(void)
+{
+ return git_runtime_shutdown();
+}
diff --git a/src/libgit2client/win32/precompiled.c b/src/libgit2client/win32/precompiled.c
new file mode 100644
index 000000000..5f656a45d
--- /dev/null
+++ b/src/libgit2client/win32/precompiled.c
@@ -0,0 +1 @@
+#include "precompiled.h"
diff --git a/src/libgit2client/win32/precompiled.h b/src/libgit2client/win32/precompiled.h
new file mode 100644
index 000000000..0fc5c0acb
--- /dev/null
+++ b/src/libgit2client/win32/precompiled.h
@@ -0,0 +1,4 @@
+#include <git2.h>
+#include <git2client.h>
+
+#include "git2_util.h"