From e49247bfaf69a784e2bb705fc391084b1652dd52 Mon Sep 17 00:00:00 2001
From: Bruce Cowan <bruce@bcowan.me.uk>
Date: Tue, 31 Dec 2019 14:27:05 +0000
Subject: [PATCH] Make compatible with MSVC

---
 meson.build             |  8 ++++----
 src/angle.c             |  4 ++++
 src/kepler.c            |  6 +++++-
 src/list-test.c         |  2 +-
 src/meson.build         | 18 ++++++++++++------
 src/next.c              |  4 ++--
 src/openmp/meson.build  |  9 ++++++---
 src/pthread/meson.build |  2 +-
 8 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/meson.build b/meson.build
index 2ede9cb..ecee473 100644
--- a/meson.build
+++ b/meson.build
@@ -2,20 +2,20 @@ project('stdlib', 'c',
   default_options: ['warning_level=3', 'c_std=gnu99'])
 
 cc = meson.get_compiler('c')
-libm = cc.find_library('m')
+libm = cc.find_library('m', required: false)
 
 glib_dep = dependency('glib-2.0', version: '>= 2.16', required: false)
 openmp_dep = dependency('openmp', required: false)
-thread_dep = dependency('threads', required: false)
+pthreads_dep = dependency('pthreads', required: false)
 
 conf_data = configuration_data()
 
 cc = meson.get_compiler('c')
 
-has_reallocarray = cc.has_function('reallocarray')
 if cc.has_function('reallocarray')
   conf_data.set('HAVE_REALLOCARRAY', 1)
 endif
+has_strndup = cc.has_function('strndup')
 
 configure_file(configuration: conf_data, output: 'config.h')
 config_dep = declare_dependency(include_directories: '.')
@@ -29,6 +29,6 @@ endif
 subdir('lib')
 subdir('src')
 
-if glib_dep.found()
+if glib_dep.found() and cc.get_id() != 'msvc'
   subdir('test')
 endif
diff --git a/src/angle.c b/src/angle.c
index f06b968..daefc5c 100644
--- a/src/angle.c
+++ b/src/angle.c
@@ -1,3 +1,7 @@
+#ifdef _MSC_VER
+    #define _USE_MATH_DEFINES
+#endif
+
 #include <stdio.h>
 #include <tgmath.h>
 
diff --git a/src/kepler.c b/src/kepler.c
index 7da4f95..38aba90 100644
--- a/src/kepler.c
+++ b/src/kepler.c
@@ -1,6 +1,10 @@
+#ifdef _MSC_VER
+    #define _USE_MATH_DEFINES
+#endif
+
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <tgmath.h>
 
 #define ACC 1e-9
 
diff --git a/src/list-test.c b/src/list-test.c
index 31333b5..c2bd094 100644
--- a/src/list-test.c
+++ b/src/list-test.c
@@ -12,7 +12,7 @@ add_data (SList *list)
   printf ("Input the data ");
   scanf ("%s", buffer);
 
-  char *str = strdup (buffer);
+  char *str = strndup (buffer, sizeof (buffer));
   return slist_prepend (list, str);
 }
 
diff --git a/src/meson.build b/src/meson.build
index 46eeab5..a60d68d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -3,7 +3,6 @@ executable('angle', 'angle.c', dependencies: libm)
 executable('array-length', 'array-length.c', dependencies: types_dep)
 executable('binary', 'binary.c')
 executable('binary2', 'binary2.c', dependencies: utils_dep)
-executable('complex', 'complex.c', dependencies: libm)
 executable('doubles', 'doubles.c')
 executable('einstein', 'einstein.c')
 executable('fib', 'fib.c')
@@ -12,24 +11,31 @@ executable('fixed-sizeof', 'fixed-sizeof.c')
 executable('fgets', 'fgets.c')
 executable('gcd', 'gcd.c')
 executable('getchar', 'getchar.c')
-executable('hashtable-test', 'hashtable-test.c', dependencies: types_dep)
 executable('kepler', 'kepler.c', dependencies: libm)
-executable('list-test', 'list-test.c', dependencies: types_dep)
 executable('next', 'next.c', dependencies: libm)
-executable('parrot', 'parrot.c')
 executable('sizeof', 'sizeof.c')
 executable('snprintf', 'snprintf.c')
-executable('strtol', 'strtol.c')
 executable('taylor', 'taylor.c')
 executable('winter', 'winter.c')
 executable('world', 'world.c')
 
+if has_strndup
+    executable('hashtable-test', 'hashtable-test.c', dependencies: types_dep)
+    executable('list-test', 'list-test.c', dependencies: types_dep)
+endif
+
+if cc.get_id() != 'msvc'
+    executable('complex', 'complex.c', dependencies: libm)
+    executable('parrot', 'parrot.c')
+    executable('strtol', 'strtol.c')
+endif
+
 subdir('sockets')
 
 if openmp_dep.found()
     subdir('openmp')
 endif
 
-if thread_dep.found()
+if pthreads_dep.found()
     subdir('pthread')
 endif
diff --git a/src/next.c b/src/next.c
index 027b5bf..09a9efb 100644
--- a/src/next.c
+++ b/src/next.c
@@ -1,6 +1,6 @@
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <tgmath.h>
 
 int
 main (int argc, char **argv)
@@ -27,7 +27,7 @@ main (int argc, char **argv)
 
     do
     {
-        current = nextafter (current, end);
+        current = nextafterf (current, end);
         count++;
     } while (current != end);
 
diff --git a/src/openmp/meson.build b/src/openmp/meson.build
index 6031942..a77fd0a 100644
--- a/src/openmp/meson.build
+++ b/src/openmp/meson.build
@@ -1,4 +1,7 @@
-executable('pi', 'pi.c', dependencies: [openmp_dep, libm])
-executable('simd', 'simd.c', dependencies: openmp_dep)
-executable('taylor', 'taylor.c', dependencies: openmp_dep)
 executable('threads', 'threads.c', dependencies: openmp_dep)
+
+if cc.get_id() != 'msvc'
+    executable('pi', 'pi.c', dependencies: [openmp_dep, libm])
+    executable('simd', 'simd.c', dependencies: openmp_dep)
+    executable('taylor', 'taylor.c', dependencies: openmp_dep)
+endif
diff --git a/src/pthread/meson.build b/src/pthread/meson.build
index bc22c6f..f83fe22 100644
--- a/src/pthread/meson.build
+++ b/src/pthread/meson.build
@@ -1,2 +1,2 @@
 executable('thread-incr', 'thread-incr.c',
-           dependencies: [thread_dep, utils_dep])
+           dependencies: [pthreads_dep, utils_dep])
-- 
GitLab