From 917774de0d42632827671acc821888e15e87fc2b Mon Sep 17 00:00:00 2001
From: Piotr Wójcik <chocimier@tlen.pl>
Date: Sat, 9 Sep 2023 13:31:56 +0200
Subject: oss-version-detection


diff --git a/CMakeLists.txt b/CMakeLists.txt
index ca9d7af..5c7c81d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -112,21 +112,21 @@ if (HAVE_SOUNDCARD_H OR HAVE_SYS_SOUNDCARD_H)
     add_definitions(-DHAVE_SYS_SOUNDCARD_H)
   endif ()
 
-  # Running a program is hopefully more reliable than trying to
-  # grep through header files to find a definition.  Need try_run()
-  # here so as to be able to read the output result.
-  try_run(TEST_RUN_RESULT TEST_COMPILE_RESULT
+  # Comparing with preprocessor is hopefully more reliable than trying to
+  # grep through header files to find a definition.  Unlike try_run(),
+  # causes no problem for cross-compilation.
+  try_compile(OSS3_COMPILE_RESULT
           ${CMAKE_CURRENT_BINARY_DIR}
-          SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ossversion.c
-          COMPILE_DEFINITIONS "${TEST_COMPILE_FLAGS}"
-          RUN_OUTPUT_VARIABLE TEST_RESULT_VERSION)
-  #message(STATUS "TEST_RUN_RESULT= ${TEST_RUN_RESULT}")
-  #message(STATUS "TEST_COMPILE_RESULT= ${TEST_COMPILE_RESULT}")
-  #message(STATUS "TEST_RESULT_VERSION= ${TEST_RESULT_VERSION}")
-
-  if (${TEST_COMPILE_RESULT} AND (${TEST_RUN_RESULT} EQUAL 0))
-    message(STATUS "Detected OSS version ${TEST_RESULT_VERSION}")
-    if (${TEST_RESULT_VERSION} GREATER_EQUAL 0x040000)
+          SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ossversion3.c
+          COMPILE_DEFINITIONS "${TEST_COMPILE_FLAGS}")
+  try_compile(OSS4_COMPILE_RESULT
+          ${CMAKE_CURRENT_BINARY_DIR}
+          SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ossversion4.c
+          COMPILE_DEFINITIONS "${TEST_COMPILE_FLAGS}")
+  #message(STATUS "OSS3_COMPILE_RESULT= ${OSS3_COMPILE_RESULT}")
+  #message(STATUS "OSS4_COMPILE_RESULT= ${OSS4_COMPILE_RESULT}")
+
+  if (${OSS4_COMPILE_RESULT})
       message(STATUS "Building with OSS 4 support")
       set(HAVE_OSS_4 true)
       add_definitions(-DHAVE_OSS_4)
@@ -137,20 +137,19 @@ if (HAVE_SOUNDCARD_H OR HAVE_SYS_SOUNDCARD_H)
       # to have sys/soundcard.h and not soundcard.h installed.
       check_struct_has_member(oss_mixerinfo devnode sys/soundcard.h HAVE_MIXERINFO_DEVNODE)
       if (HAVE_MIXERINFO_DEVNODE)
-	add_definitions(-DHAVE_MIXERINFO_DEVNODE)
+        add_definitions(-DHAVE_MIXERINFO_DEVNODE)
       endif ()
-    else ()
+    elseif (${OSS3_COMPILE_RESULT})
       message(STATUS "Building with OSS 3 support")
       set(HAVE_OSS_3 true)
       add_definitions(-DHAVE_OSS_3)
       set(SUPPORTED_BACKENDS ${SUPPORTED_BACKENDS} "OSS")
+    else ()
+      message(STATUS "Unable to get OSS version, assuming 3")
+      set(HAVE_OSS_3 true)
+      add_definitions(-DHAVE_OSS_3)
+      set(SUPPORTED_BACKENDS ${SUPPORTED_BACKENDS} "OSS")
     endif ()
-  else ()
-    message(STATUS "Unable to get OSS version, assuming 3")
-    set(HAVE_OSS_3 true)
-    add_definitions(-DHAVE_OSS_3)
-    set(SUPPORTED_BACKENDS ${SUPPORTED_BACKENDS} "OSS")
-  endif ()
 endif ()
 
 # PulseAudio, optional
diff --git a/ossversion.c b/cmake/ossversion3.c
similarity index 83%
rename from ossversion.c
rename to cmake/ossversion3.c
index 8ab612e..e3a5ff1 100644
--- a/ossversion.c
+++ b/cmake/ossversion3.c
@@ -2,6 +2,7 @@
  * KMix -- KDE's full featured mini mixer
  *
  * Copyright (C) 2023 Jonathan Marten <jonathan.marten@kdemail.net>
+ * Copyright (C) 2023 Piotr Wójcik <chocimier@tlen.pl>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -27,8 +28,14 @@
 #endif
 #endif
 
+
+#if SOUND_VERSION < 0x030000
+#error "SOUND_VERSION < 0x030000"
+#endif
+#if 0x040000 <= SOUND_VERSION
+#error "0x040000 <= SOUND_VERSION"
+#endif
+
 int main()
 {
-	printf("0x%06X", SOUND_VERSION);
-	return (0);
 }
diff --git a/cmake/ossversion4.c b/cmake/ossversion4.c
new file mode 100644
index 0000000..637d894
--- /dev/null
+++ b/cmake/ossversion4.c
@@ -0,0 +1,41 @@
+/*
+ * KMix -- KDE's full featured mini mixer
+ *
+ * Copyright (C) 2023 Jonathan Marten <jonathan.marten@kdemail.net>
+ * Copyright (C) 2023 Piotr Wójcik <chocimier@tlen.pl>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this program; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include <stdio.h>
+#ifdef HAVE_SOUNDCARD_H
+#include <soundcard.h>
+#else
+#ifdef HAVE_SYS_SOUNDCARD_H
+#include <sys/soundcard.h>
+#endif
+#endif
+
+
+#if SOUND_VERSION < 0x040000
+#error "SOUND_VERSION < 0x040000"
+#endif
+#if 0x050000 <= SOUND_VERSION
+#error "0x050000 <= SOUND_VERSION"
+#endif
+
+int main()
+{
+}
-- 
2.41.0

