[PATCH] OpenAL Soft: Support building a static library on Windows

Martin Lambers <[email protected]>
Newsgroups gmane.comp.lib.openal
Message-ID <[email protected]>
Hi all,

the attached patch allows to build a static OpenAL Soft library on
Windows. This is useful for the Mingw-cross-env project
<http://www.nongnu.org/mingw-cross-env/>.

The patch does the following:

- Define the macro _WIN32_STATIC when 1) the system is windows and 2)
the CMake LIBTYPE is STATIC.

- On Win32, when _WIN32_STATIC is defined, do not use alc_init() and
alc_deinit() from within DllMain. Instead, use the gcc
constructor/destructor attributes, if available (they are available on
MinGW).

- In the al.h and alc.h headers, when building the a static library on
Windows, define the API macro to "extern" instead of "__declspec(dllexport)"

- In the al.h and alc.h headers, when not building the library (i.e.
when the header is included from an application), use
"__declspec(dllimport)" only when the compiler is MSVC and _WIN32_STATIC
is not defined. GCC does not need "__declspec(dllimport)" at all.

This allows to build and use a static OpenAL library with the gcc
compiler on Windows, without changes to the application source code or
build process. It should be possible to use this static library with
MSVC by defining _WIN32_STATIC before including the AL headers
(untested). It is not possible to build a static library using MSVC
because alc_init/alc_deinit will not be executed.

Martin

_______________________________________________
Openal mailing list
[email protected]
http://opensource.creative.com/mailman/listinfo/openal
openal.patch (text/x-patch, 2.6 KB)
diff --git a/Alc/ALc.c b/Alc/ALc.c
index 938f83f..92df86e 100644
--- a/Alc/ALc.c
+++ b/Alc/ALc.c
@@ -377,7 +377,7 @@ static FILE *LogFile;
 
 ///////////////////////////////////////////////////////
 // ALC Related helper functions
-#ifdef _WIN32
+#if defined(_WIN32) && !defined(_WIN32_STATIC)
 static void alc_init(void);
 static void alc_deinit(void);
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c629c42..8c7769f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -119,11 +119,11 @@ ELSE()
         "Flags used by the compiler during debug builds."
         FORCE)
 
+    CHECK_C_SOURCE_COMPILES("int foo() __attribute__((destructor));
+                             int main() {return 0;}" HAVE_GCC_DESTRUCTOR)
+
     # Set visibility options if available
     IF(NOT WIN32)
-        CHECK_C_SOURCE_COMPILES("int foo() __attribute__((destructor));
-                                 int main() {return 0;}" HAVE_GCC_DESTRUCTOR)
-
         CHECK_C_COMPILER_FLAG(-fvisibility=hidden HAVE_VISIBILITY_SWITCH)
         IF(HAVE_VISIBILITY_SWITCH)
             CHECK_C_SOURCE_COMPILES("int foo() __attribute__((visibility(\"default\")));
@@ -471,6 +471,9 @@ SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES DEFINE_SYMBOL AL_BUILD_LIBRARY
                                             SOVERSION ${LIB_MAJOR_VERSION})
 IF(WIN32)
     SET_TARGET_PROPERTIES(${LIBNAME} PROPERTIES PREFIX "")
+    IF(LIBTYPE STREQUAL "STATIC")
+        ADD_DEFINITIONS("-D_WIN32_STATIC")
+    ENDIF()
 ENDIF()
 
 TARGET_LINK_LIBRARIES(${LIBNAME} ${EXTRA_LIBS})
diff --git a/include/AL/al.h b/include/AL/al.h
index c409701..c373616 100644
--- a/include/AL/al.h
+++ b/include/AL/al.h
@@ -6,10 +6,12 @@ extern "C" {
 #endif
 
 #if defined(_WIN32) && !defined(_XBOX)
- #if defined(AL_BUILD_LIBRARY)
+ #if defined(AL_BUILD_LIBRARY) && !defined(_WIN32_STATIC)
   #define AL_API __declspec(dllexport)
- #else
+ #elif defined(_MSVC) && !defined(_WIN32_STATIC)
   #define AL_API __declspec(dllimport)
+ #else
+  #define AL_API extern
  #endif
 #else
  #if defined(AL_BUILD_LIBRARY) && defined(HAVE_GCC_VISIBILITY)
diff --git a/include/AL/alc.h b/include/AL/alc.h
index 4e84af4..b4fbb13 100644
--- a/include/AL/alc.h
+++ b/include/AL/alc.h
@@ -6,10 +6,12 @@ extern "C" {
 #endif
 
 #if defined(_WIN32) && !defined(_XBOX)
- #if defined(AL_BUILD_LIBRARY)
+ #if defined(AL_BUILD_LIBRARY) && !defined(_WIN32_STATIC)
   #define ALC_API __declspec(dllexport)
- #else
+ #elif defined(_MSVC) && !defined(_WIN32_STATIC)
   #define ALC_API __declspec(dllimport)
+ #else
+  #define ALC_API extern
  #endif
 #else
  #if defined(AL_BUILD_LIBRARY) && defined(HAVE_GCC_VISIBILITY)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.