]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
build : do not use _GNU_SOURCE gratuitously (#2035)
authorPrzemysław Pawełczyk <redacted>
Fri, 8 Sep 2023 12:09:21 +0000 (14:09 +0200)
committerGitHub <redacted>
Fri, 8 Sep 2023 12:09:21 +0000 (15:09 +0300)
* Do not use _GNU_SOURCE gratuitously.

What is needed to build llama.cpp and examples is availability of
stuff defined in The Open Group Base Specifications Issue 6
(https://pubs.opengroup.org/onlinepubs/009695399/) known also as
Single Unix Specification v3 (SUSv3) or POSIX.1-2001 + XSI extensions,
plus some stuff from BSD that is not specified in POSIX.1.

Well, that was true until NUMA support was added recently,
so enable GNU libc extensions for Linux builds to cover that.

Not having feature test macros in source code gives greater flexibility
to those wanting to reuse it in 3rd party app, as they can build it with
FTMs set by Makefile here or other FTMs depending on their needs.

It builds without issues in Alpine (musl libc), Ubuntu (glibc), MSYS2.

* make : enable Darwin extensions for macOS to expose RLIMIT_MEMLOCK

* make : enable BSD extensions for DragonFlyBSD to expose RLIMIT_MEMLOCK

* make : use BSD-specific FTMs to enable alloca on BSDs

* make : fix OpenBSD build by exposing newer POSIX definitions

* cmake : follow recent FTM improvements from Makefile

CMakeLists.txt
Makefile
examples/beam-search/beam-search.cpp
examples/embd-input/embd-input-lib.cpp
examples/main/main.cpp
examples/simple/simple.cpp
examples/speculative/speculative.cpp
ggml-alloc.c
ggml.c
llama.cpp

index f8cee71c82c30029fd8b61300745be33b0a1dee0..0abf1df7b83a1a8693579641899b37d80442fe53 100644 (file)
@@ -551,6 +551,49 @@ else()
     message(STATUS "Unknown architecture")
 endif()
 
+# clock_gettime came in POSIX.1b (1993)
+# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
+# posix_memalign came in POSIX.1-2001 / SUSv3
+# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
+add_compile_definitions(_XOPEN_SOURCE=600)
+
+# Somehow in OpenBSD whenever POSIX conformance is specified
+# some string functions rely on locale_t availability,
+# which was introduced in POSIX.1-2008, forcing us to go higher
+IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+    remove_definitions(-D_XOPEN_SOURCE=600)
+    add_compile_definitions(_XOPEN_SOURCE=700)
+ENDIF()
+
+# Data types, macros and functions related to controlling CPU affinity and
+# some memory allocation are available on Linux through GNU extensions in libc
+IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
+    add_compile_definitions(_GNU_SOURCE)
+ENDIF()
+
+# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
+# and on macOS its availability depends on enabling Darwin extensions
+# similarly on DragonFly, enabling BSD extensions is necessary
+IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
+    add_compile_definitions(_DARWIN_C_SOURCE)
+ENDIF()
+IF (CMAKE_SYSTEM_NAME MATCHES "DragonFly")
+    add_compile_definitions(_DARWIN_C_SOURCE)
+ENDIF()
+
+# alloca is a non-standard interface that is not visible on BSDs when
+# POSIX conformance is specified, but not all of them provide a clean way
+# to enable it in such cases
+IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+    add_compile_definitions(__BSD_VISIBLE)
+ENDIF()
+IF (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
+    add_compile_definitions(_NETBSD_SOURCE)
+ENDIF()
+IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+    add_compile_definitions(_BSD_SOURCE)
+ENDIF()
+
 #
 # libraries
 #
index 86e36ba52a0c0e16f5a54340698a20de1f76e244..a774dc50f372d6f2738684cf72e0aad6615514a5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -106,6 +106,56 @@ MK_CFLAGS   = $(OPT) -std=c11   -fPIC
 MK_CXXFLAGS = $(OPT) -std=c++11 -fPIC
 MK_LDFLAGS  =
 
+# clock_gettime came in POSIX.1b (1993)
+# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
+# posix_memalign came in POSIX.1-2001 / SUSv3
+# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
+MK_CFLAGS   += -D_XOPEN_SOURCE=600
+MK_CXXFLAGS += -D_XOPEN_SOURCE=600
+
+# Somehow in OpenBSD whenever POSIX conformance is specified
+# some string functions rely on locale_t availability,
+# which was introduced in POSIX.1-2008, forcing us to go higher
+ifeq ($(UNAME_S),OpenBSD)
+       MK_CFLAGS   += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
+       MK_CXXFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
+endif
+
+# Data types, macros and functions related to controlling CPU affinity and
+# some memory allocation are available on Linux through GNU extensions in libc
+ifeq ($(UNAME_S),Linux)
+       MK_CFLAGS   += -D_GNU_SOURCE
+       MK_CXXFLAGS += -D_GNU_SOURCE
+endif
+
+# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
+# and on macOS its availability depends on enabling Darwin extensions
+# similarly on DragonFly, enabling BSD extensions is necessary
+ifeq ($(UNAME_S),Darwin)
+       MK_CFLAGS   += -D_DARWIN_C_SOURCE
+       MK_CXXFLAGS += -D_DARWIN_C_SOURCE
+endif
+ifeq ($(UNAME_S),DragonFly)
+       MK_CFLAGS   += -D__BSD_VISIBLE
+       MK_CXXFLAGS += -D__BSD_VISIBLE
+endif
+
+# alloca is a non-standard interface that is not visible on BSDs when
+# POSIX conformance is specified, but not all of them provide a clean way
+# to enable it in such cases
+ifeq ($(UNAME_S),FreeBSD)
+       MK_CFLAGS   += -D__BSD_VISIBLE
+       MK_CXXFLAGS += -D__BSD_VISIBLE
+endif
+ifeq ($(UNAME_S),NetBSD)
+       MK_CFLAGS   += -D_NETBSD_SOURCE
+       MK_CXXFLAGS += -D_NETBSD_SOURCE
+endif
+ifeq ($(UNAME_S),OpenBSD)
+       MK_CFLAGS   += -D_BSD_SOURCE
+       MK_CXXFLAGS += -D_BSD_SOURCE
+endif
+
 ifdef LLAMA_DEBUG
        MK_CFLAGS   += -O0 -g
        MK_CXXFLAGS += -O0 -g
index 4d021434b76e1e4e9d3e0d27a51ea24102e38a27..6b31aea78823ec374b8205dd4ae53586f3374f08 100644 (file)
@@ -1,7 +1,3 @@
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "common.h"
 #include "llama.h"
 #include "build-info.h"
index 87aac3479003cf653a8290c3556db4d30a82556c..ef12212ba7587722bd22c8d3e4313ccfe72183b8 100644 (file)
@@ -1,8 +1,3 @@
-// Defines sigaction on msys:
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "embd-input.h"
 
 #include <cassert>
index c9ca7719bf01f0ad86ead84723041858dc273811..be030fffbc1af315102ea5bd5dd9f0fe2e13d53b 100644 (file)
@@ -1,8 +1,3 @@
-// Defines sigaction on msys:
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "common.h"
 
 #include "console.h"
index 4ee85faca9f4a93c3a4b8b5e12e7a9ad8a04c365..ba5de0cc61e5499a6afe00ba6e5a4166a96f7ea0 100644 (file)
@@ -1,7 +1,3 @@
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "build-info.h"
 
 #include "common.h"
index c6211ac79569ccb813b8400c6936112f3908b890..822d7b529f01d096108ac00509d836a5528d3347 100644 (file)
@@ -1,7 +1,3 @@
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "build-info.h"
 
 #include "common.h"
index e2ac891d13589a535b87650605e24822567515c9..a1f6e7bf4f66eec4d9437c86624cb85d7d72bc39 100644 (file)
@@ -1,8 +1,3 @@
-// defines MAP_ANONYMOUS
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "ggml-alloc.h"
 #include "ggml.h"
 #include <assert.h>
diff --git a/ggml.c b/ggml.c
index a4b9781d572387ee15ad7ea8320c57917d7bb05d..d5ca0101a64b0bfcc99e5212eb200ec4353a60f4 100644 (file)
--- a/ggml.c
+++ b/ggml.c
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE // Defines CLOCK_MONOTONIC on Linux
 #define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
 
 #include "ggml.h"
index cab7156f44c32c75ed1c2079fb455f6b3aaeac41..3f11902214da9c7b693c2f6ecbd8783ec4aeebf0 100644 (file)
--- a/llama.cpp
+++ b/llama.cpp
@@ -1,8 +1,3 @@
-// Defines fileno on msys:
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
 #include "llama.h"
 
 #include "ggml.h"