changeset 558:a2443afe8a1f

cmake: check for non-portable stuff
author David Demelier <markand@malikania.fr>
date Wed, 08 Mar 2023 19:50:00 +0100
parents 944798a59b8a
children ba0cca39e4ca
files libmlk-util/CMakeLists.txt libmlk-util/mlk/util/fmemopen.c libmlk-util/mlk/util/openbsd/basename.c libmlk-util/mlk/util/openbsd/dirname.c libmlk-util/mlk/util/openbsd/strlcat.c libmlk-util/mlk/util/openbsd/strlcpy.c libmlk-util/mlk/util/sysconfig.cmake.h libmlk-util/mlk/util/util.c libmlk-util/mlk/util/util.h
diffstat 9 files changed, 186 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/libmlk-util/CMakeLists.txt	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/CMakeLists.txt	Wed Mar 08 19:50:00 2023 +0100
@@ -18,6 +18,10 @@
 
 project(libmlk-util)
 
+include(CheckFunctionExists)
+include(CheckIncludeFile)
+include(CheckSymbolExists)
+
 set(
 	SOURCES
 	${libmlk-util_SOURCE_DIR}/mlk/util/fmemopen.c
@@ -26,10 +30,26 @@
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/getopt.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/strlcat.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/openbsd/strlcpy.c
+	${libmlk-util_SOURCE_DIR}/mlk/util/sysconfig.cmake.h
 	${libmlk-util_SOURCE_DIR}/mlk/util/util.c
 	${libmlk-util_SOURCE_DIR}/mlk/util/util.h
 )
 
+check_symbol_exists("PATH_MAX" "limits.h" MLK_HAVE_PATH_MAX)
+
+check_include_file("libgen.h" MLK_HAVE_LIBGEN_H)
+
+check_function_exists(basename MLK_HAVE_BASENAME)
+check_function_exists(dirname MLK_HAVE_DIRNAME)
+check_function_exists(fmemopen MLK_HAVE_FMEMOPEN)
+check_function_exists(strlcat MLK_HAVE_STRLCAT)
+check_function_exists(strlcpy MLK_HAVE_STRLCPY)
+
+configure_file(
+	${libmlk-util_SOURCE_DIR}/mlk/util/sysconfig.cmake.h
+	${libmlk-util_BINARY_DIR}/sysconfig.h
+)
+
 mlk_library(
 	NAME libmlk-util
 	SOURCES ${SOURCES}
--- a/libmlk-util/mlk/util/fmemopen.c	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/fmemopen.c	Wed Mar 08 19:50:00 2023 +0100
@@ -16,9 +16,12 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include "sysconfig.h"
 #include "util.h"
 
-#if !defined(MLK_HAS_FMEMOPEN)
+#if !defined(MLK_HAVE_FMEMOPEN)
+
+#if defined(_WIN32)
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -62,14 +65,19 @@
 	return fp;
 }
 
-#else /* !MLK_HAS_FMEMOPEN */
-
-#include <stdio.h>
+#else
 
 FILE *
 mlk_util_fmemopen(void *buf, size_t len, const char *type)
 {
-	return fmemopen(buf, len, type);
+	(void)buf;
+	(void)len;
+	(void)type;
+
+	/* Unsupported platform. */
+	return NULL;
 }
 
 #endif
+
+#endif
--- a/libmlk-util/mlk/util/openbsd/basename.c	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/openbsd/basename.c	Wed Mar 08 19:50:00 2023 +0100
@@ -16,11 +16,13 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include "sysconfig.h"
+
 #include <errno.h>
 #include <limits.h>
 #include <string.h>
 
-#include <mlk/util/util.h>
+#if !defined(MLK_HAVE_BASENAME)
 
 char *
 mlk_util_basename(char *path)
@@ -64,3 +66,5 @@
 	bname[len] = '\0';
 	return (bname);
 }
+
+#endif
--- a/libmlk-util/mlk/util/openbsd/dirname.c	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/openbsd/dirname.c	Wed Mar 08 19:50:00 2023 +0100
@@ -16,11 +16,13 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include "sysconfig.h"
+
 #include <errno.h>
 #include <limits.h>
 #include <string.h>
 
-#include <mlk/util/util.h>
+#if !defined(MLK_HAVE_DIRNAME)
 
 char *
 mlk_util_dirname(char *path)
@@ -68,3 +70,5 @@
 	dname[len] = '\0';
 	return (dname);
 }
+
+#endif
--- a/libmlk-util/mlk/util/openbsd/strlcat.c	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/openbsd/strlcat.c	Wed Mar 08 19:50:00 2023 +0100
@@ -19,6 +19,10 @@
 #include <stddef.h>
 #include <string.h>
 
+#include "sysconfig.h"
+
+#if !defined(MLK_HAVE_STRLCAT)
+
 /*
  * Appends src to string dst of size dsize (unlike strncat, dsize is the
  * full size of dst, not space left).  At most dsize-1 characters
@@ -53,3 +57,5 @@
 
 	return(dlen + (src - osrc));	/* count does not include NUL */
 }
+
+#endif
--- a/libmlk-util/mlk/util/openbsd/strlcpy.c	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/openbsd/strlcpy.c	Wed Mar 08 19:50:00 2023 +0100
@@ -18,6 +18,10 @@
 
 #include <stddef.h>
 
+#include "sysconfig.h"
+
+#if !defined(MLK_HAVE_STRLCPY)
+
 /*
  * Copy string src to buffer dst of size dsize.  At most dsize-1
  * chars will be copied.  Always NUL terminates (unless dsize == 0).
@@ -47,3 +51,5 @@
 
 	return(src - osrc - 1);	/* count does not include NUL */
 }
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmlk-util/mlk/util/sysconfig.cmake.h	Wed Mar 08 19:50:00 2023 +0100
@@ -0,0 +1,32 @@
+/*
+ * sysconfig.cmake.h -- miscellaneous utilities and portability
+ *
+ * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef MLK_UTIL_SYSCONFIG_H
+#define MLK_UTIL_SYSCONFIG_H
+
+#cmakedefine MLK_HAVE_PATH_MAX
+
+#cmakedefine MLK_HAVE_LIBGEN_H
+
+#cmakedefine MLK_HAVE_BASENAME
+#cmakedefine MLK_HAVE_DIRNAME
+#cmakedefine MLK_HAVE_FMEMOPEN
+#cmakedefine MLK_HAVE_STRLCAT
+#cmakedefine MLK_HAVE_STRLCPY
+
+#endif /* !MLK_UTIL_SYSCONFIG_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmlk-util/mlk/util/util.c	Wed Mar 08 19:50:00 2023 +0100
@@ -0,0 +1,88 @@
+/*
+ * util.c -- miscellaneous utilities and portability
+ *
+ * Copyright (c) 2020-2023 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "sysconfig.h"
+
+#include <assert.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if defined(MLK_HAVE_LIBGEN_H)
+#       include <libgen.h>
+#endif
+
+void
+mlk_util_die(const char *fmt, ...)
+{
+	assert(fmt);
+
+	va_list ap;
+
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+	exit(1);
+}
+
+/*
+ * The following functions are just wrapper for the native system routine if they
+ * are available on the system, otherwise the symbol will be implemented in
+ * openbsd/<name>.c
+ */
+
+#if defined(MLK_HAVE_STRLCPY)
+
+size_t
+mlk_util_strlcpy(char *dst, const char *src, size_t dstsz)
+{
+	return strlcpy(dst, src, dstsz);
+}
+
+#endif
+
+#if defined(MLK_HAVE_STRLCAT)
+
+size_t
+mlk_util_strlcat(char *dst, const char *src, size_t dstsz)
+{
+	return strlcat(dst, src, dstsz);
+}
+
+#endif
+
+#if defined(MLK_HAVE_BASENAME)
+
+char *
+mlk_util_basename(char *path)
+{
+	return basename(path);
+}
+
+#endif
+
+#if defined(MLK_HAVE_DIRNAME)
+
+char *
+mlk_util_dirname(char *path)
+{
+	return dirname(path);
+}
+
+#endif
--- a/libmlk-util/mlk/util/util.h	Wed Mar 08 13:17:38 2023 +0100
+++ b/libmlk-util/mlk/util/util.h	Wed Mar 08 19:50:00 2023 +0100
@@ -24,65 +24,19 @@
  * \brief Miscellaneous utilities and portability
  */
 
+#include "sysconfig.h"
+
 #include <limits.h>
 #include <stdio.h>
 
-/*
- * This file helps finding what are the available features accross the various
- * operating system in the landscape.
- *
- * The following macros are automatically set depending on the operating
- * system:
- *
- * - MLK_OS_WINDOWS: running on any Windows machine
- * - MLK_OS_POSIX: every mostly POSIX systems
- *
- * The following macro will be automatically defined unless the user override
- * them:
- *
- * - MLK_HAS_FMEMOPEN: defined if fmemopen function is available.
- * - MLK_HAS_SSIZE_T: defined if ssize_t typedef is available.
- */
-
-#if defined(_WIN32)
-#       define MLK_OS_WINDOWS
-#elif defined(__FreeBSD__)
-#       define MLK_OS_POSIX
-#elif defined(__DragonFly__)
-#       define MLK_OS_POSIX
-#elif defined(__OpenBSD__)
-#       define MLK_OS_POSIX
-#elif defined(__NetBSD__)
-#       define MLK_OS_POSIX
-#elif defined(__linux__)
-#       define MLK_OS_POSIX
-#elif defined(__APPLE__)
-#       define MLK_OS_POSIX
-#       define MLK_OS_APPLE
+#if defined(__cplusplus)
+extern "C" {
 #endif
 
-#if defined(PATH_MAX)
+#if defined(MLK_HAVE_PATH_MAX)
 #       define MLK_PATH_MAX PATH_MAX
-#elif defined(_POSIX_PATH_MAX)
-#       define MLK_PATH_MAX _POSIX_PATH_MAX
 #else
-#       define MLK_PATH_MAX 4096
-#endif
-
-#if defined(MLK_OS_POSIX) && !defined(MLK_HAS_SSIZE_T)
-#       define MLK_HAS_SSIZE_T
-#endif
-
-#if !defined(MLK_HAS_SSIZE_T)
-typedef long long int ssize_t;
-#endif
-
-#if defined(MLK_OS_POSIX) && !defined(MLK_HAS_FMEMOPEN)
-#       define MLK_HAS_FMEMOPEN
-#endif
-
-#if defined(__cplusplus)
-extern "C" {
+#       define MLK_PATH_MAX 1024
 #endif
 
 /**
@@ -110,6 +64,11 @@
 size_t
 mlk_util_strlcat(char *dst, const char *src, size_t dstsz);
 
+/**
+ * Portable version of POSIX/C23 [fmemopen].
+ *
+ * [basename]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html
+ */
 FILE *
 mlk_util_fmemopen(void *, size_t, const char *);