From 6df13969e6967dbed660271011e872c9c366037a Mon Sep 17 00:00:00 2001
From: Brennan Ashton <bashton@brennanashton.com>
Date: Tue, 28 Jan 2020 03:59:02 -0800
Subject: [PATCH] Fix static library builds

Signed-off-by: Brennan Ashton <bashton@brennanashton.com>
---
 CMakeLists.txt     |  3 ---
 src/CMakeLists.txt | 10 +++++++++-
 src/libaec.h       | 28 ++++++++++------------------
 src/szlib.h        |  6 +++---
 4 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d71d26c..4f38618 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -43,9 +43,6 @@ option(BUILD_SHARED_LIBS "Build Shared Libraries" ON)
 if(BUILD_SHARED_LIBS)
   set (LIB_TYPE SHARED)
 else(BUILD_SHARED_LIBS)
-  if(WIN32)
-    message(WARNING "You will have to modify libaec.h for static libs.")
-  endif(WIN32)
   set(LIB_TYPE STATIC)
 endif(BUILD_SHARED_LIBS)
 
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ed6053e..93a78f2 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,5 +1,13 @@
+include (GenerateExportHeader)
 add_library(aec ${LIB_TYPE} ${libaec_SRCS})
 set_target_properties(aec PROPERTIES VERSION 0.0.10 SOVERSION 0)
+generate_export_header(aec
+  BASE_NAME libaec
+  EXPORT_MACRO_NAME libaec_EXPORT
+  EXPORT_FILE_NAME ${PROJECT_BINARY_DIR}/libaec_Export.h
+  STATIC_DEFINE libaec_BUILT_AS_STATIC
+)
+
 add_library(sz ${LIB_TYPE} sz_compat.c)
 set_target_properties(sz PROPERTIES VERSION 2.0.1 SOVERSION 2)
 
@@ -41,7 +49,7 @@ else(WIN32)
     DESTINATION ${CMAKE_INSTALL_FULL_MANDIR}/man1 COMPONENT doc)
 endif(WIN32)
 
-install(FILES libaec.h szlib.h
+install(FILES libaec.h szlib.h ${PROJECT_BINARY_DIR}/libaec_Export.h
   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
   COMPONENT headers)
 install(TARGETS aec_client
diff --git a/src/libaec.h b/src/libaec.h
index bb43be9..44505e4 100644
--- a/src/libaec.h
+++ b/src/libaec.h
@@ -52,20 +52,12 @@
 
 #include <stddef.h>
 
+#include "libaec_Export.h"
+
 #ifdef __cplusplus
 extern "C"{
 #endif
 
-#if BUILDING_LIBAEC && HAVE_VISIBILITY
-#  define LIBAEC_DLL_EXPORTED __attribute__((__visibility__("default")))
-#elif BUILDING_LIBAEC && defined _MSC_VER
-#  define LIBAEC_DLL_EXPORTED __declspec(dllexport)
-#elif defined _MSC_VER
-#  define LIBAEC_DLL_EXPORTED __declspec(dllimport)
-#else
-#  define LIBAEC_DLL_EXPORTED
-#endif
-
 struct internal_state;
 
 struct aec_stream {
@@ -155,19 +147,19 @@ struct aec_stream {
 /*********************************************/
 /* Streaming encoding and decoding functions */
 /*********************************************/
-LIBAEC_DLL_EXPORTED int aec_encode_init(struct aec_stream *strm);
-LIBAEC_DLL_EXPORTED int aec_encode(struct aec_stream *strm, int flush);
-LIBAEC_DLL_EXPORTED int aec_encode_end(struct aec_stream *strm);
+libaec_EXPORT int aec_encode_init(struct aec_stream *strm);
+libaec_EXPORT int aec_encode(struct aec_stream *strm, int flush);
+libaec_EXPORT int aec_encode_end(struct aec_stream *strm);
 
-LIBAEC_DLL_EXPORTED int aec_decode_init(struct aec_stream *strm);
-LIBAEC_DLL_EXPORTED int aec_decode(struct aec_stream *strm, int flush);
-LIBAEC_DLL_EXPORTED int aec_decode_end(struct aec_stream *strm);
+libaec_EXPORT int aec_decode_init(struct aec_stream *strm);
+libaec_EXPORT int aec_decode(struct aec_stream *strm, int flush);
+libaec_EXPORT int aec_decode_end(struct aec_stream *strm);
 
 /***************************************************************/
 /* Utility functions for encoding or decoding a memory buffer. */
 /***************************************************************/
-LIBAEC_DLL_EXPORTED int aec_buffer_encode(struct aec_stream *strm);
-LIBAEC_DLL_EXPORTED int aec_buffer_decode(struct aec_stream *strm);
+libaec_EXPORT int aec_buffer_encode(struct aec_stream *strm);
+libaec_EXPORT int aec_buffer_decode(struct aec_stream *strm);
 
 #ifdef __cplusplus
 }
diff --git a/src/szlib.h b/src/szlib.h
index 1eaa577..18b6a3c 100644
--- a/src/szlib.h
+++ b/src/szlib.h
@@ -80,15 +80,15 @@ typedef struct SZ_com_t_s
     int pixels_per_scanline;
 } SZ_com_t;
 
-LIBAEC_DLL_EXPORTED int SZ_BufftoBuffCompress(
+libaec_EXPORT int SZ_BufftoBuffCompress(
     void *dest, size_t *destLen,
     const void *source, size_t sourceLen,
     SZ_com_t *param);
-LIBAEC_DLL_EXPORTED int SZ_BufftoBuffDecompress(
+libaec_EXPORT int SZ_BufftoBuffDecompress(
     void *dest, size_t *destLen,
     const void *source, size_t sourceLen,
     SZ_com_t *param);
 
-LIBAEC_DLL_EXPORTED int SZ_encoder_enabled(void);
+libaec_EXPORT int SZ_encoder_enabled(void);
 
 #endif /* SZLIB_H */
-- 
2.24.1

