From 629a7494cf0a6fd4fed1a18724ef466cfe9582f4 Mon Sep 17 00:00:00 2001 From: Henrique Date: Sun, 27 Jul 2025 01:57:45 -0300 Subject: [PATCH] Don't export API symbols on static build --- CMakeLists.txt | 4 ++++ include/git2/common.h | 22 +++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 335901d1fb0..1717318c611 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,10 @@ if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) endif() +if(NOT BUILD_SHARED_LIBS) + add_definitions(-DGIT_STATIC_BUILD=1) +endif() + # Modules diff --git a/include/git2/common.h b/include/git2/common.h index 0be84fa77bd..a6784a23787 100644 --- a/include/git2/common.h +++ b/include/git2/common.h @@ -37,14 +37,22 @@ typedef size_t size_t; #endif /** Declare a public function exported for application use. */ -#if __GNUC__ >= 4 -# define GIT_EXTERN(type) extern \ - __attribute__((visibility("default"))) \ - type -#elif defined(_MSC_VER) -# define GIT_EXTERN(type) __declspec(dllexport) type __cdecl +#if !defined(GIT_STATIC_BUILD) +# if __GNUC__ >= 4 +# define GIT_DECL_EXTERN extern __attribute__((visibility("default"))) +# elif defined(_MSC_VER) +# define GIT_DECL_EXTERN __declspec(dllexport) +# else +# define GIT_DECL_EXTERN extern +# endif +#else +# define GIT_DECL_EXTERN /* empty */ +#endif + +#if defined(_MSC_VER) +# define GIT_EXTERN(type) GIT_DECL_EXTERN type __cdecl #else -# define GIT_EXTERN(type) extern type +# define GIT_EXTERN(type) GIT_DECL_EXTERN type #endif /** Declare a callback function for application use. */