cmake: fix windows build when building with static libraries

On Windows, __declspec(dllimport) changes the name of the symbol to what
would be provided by the .lib accompanying the .dll.  When generating a
static library, the .lib provides the original name, resulting in undefined
symbols during linking.

On Linux and MacOS, __attribute__((visibility ("default"))) changes the
visibility of the symbol and would cause it to be exported from any library
linking against libipt.a.  That's not necessarily the desired behaviour.

When using static libraries, we don't really need to export/import anything.

Change-Id: Ia4dab6d43feadf9a36446b1f6a3b315dc3826483
Signed-off-by: Markus Metzger <markus.t.metzger@intel.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4761da3..cf90b53 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -138,6 +138,17 @@
   )
 endif (PEVENT)
 
+if (NOT BUILD_SHARED_LIBS)
+  add_definitions(
+    # suppress libipt symbols import/export
+    #
+    -Dpt_export=
+
+    # suppress libipt-sb symbols import/export
+    #
+    -Dpt_sb_export=
+  )
+endif (NOT BUILD_SHARED_LIBS)
 
 function(add_cflag_if_available option guard)
 
diff --git a/libipt/CMakeLists.txt b/libipt/CMakeLists.txt
index 5e9cf5b..94f3361 100644
--- a/libipt/CMakeLists.txt
+++ b/libipt/CMakeLists.txt
@@ -69,11 +69,13 @@
 endif (CMAKE_HOST_UNIX)
 
 if (CMAKE_HOST_WIN32)
-  add_definitions(
-    # export libipt symbols
-    #
-    /Dpt_export=__declspec\(dllexport\)
-  )
+  if (BUILD_SHARED_LIBS)
+    add_definitions(
+      # export libipt symbols
+      #
+      /Dpt_export=__declspec\(dllexport\)
+    )
+  endif (BUILD_SHARED_LIBS)
 
   include_directories(
     internal/include/windows
diff --git a/sideband/CMakeLists.txt b/sideband/CMakeLists.txt
index 449b4e0..85ee06e 100644
--- a/sideband/CMakeLists.txt
+++ b/sideband/CMakeLists.txt
@@ -36,11 +36,13 @@
 )
 
 if (CMAKE_HOST_WIN32)
-  add_definitions(
-    # export libipt-sb symbols
-    #
-    /Dpt_sb_export=__declspec\(dllexport\)
-  )
+  if (BUILD_SHARED_LIBS)
+    add_definitions(
+      # export libipt-sb symbols
+      #
+      /Dpt_sb_export=__declspec\(dllexport\)
+    )
+  endif (BUILD_SHARED_LIBS)
 endif (CMAKE_HOST_WIN32)
 
 add_library(libipt-sb ${LIBSB_FILES})