From 50ff7aa900b8d1f1e816deb724b655e0662806bd Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 28 Jun 2023 10:01:03 +0200 Subject: [PATCH 1/6] 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. --- Makefile | 14 ++++++++++++++ examples/beam-search/beam-search.cpp | 4 ---- examples/embd-input/embd-input-lib.cpp | 5 ----- examples/main/main.cpp | 5 ----- examples/simple/simple.cpp | 4 ---- examples/speculative/speculative.cpp | 4 ---- ggml-alloc.c | 5 ----- ggml.c | 1 - llama.cpp | 5 ----- 9 files changed, 14 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 4334761a44d27..4623a23c40f46 100644 --- a/Makefile +++ b/Makefile @@ -95,6 +95,20 @@ MK_CFLAGS = $(CPPFLAGS) $(OPT) -std=c11 -fPIC MK_CXXFLAGS = $(CPPFLAGS) $(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 + +# 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 + ifdef LLAMA_DEBUG MK_CFLAGS += -O0 -g MK_CXXFLAGS += -O0 -g diff --git a/examples/beam-search/beam-search.cpp b/examples/beam-search/beam-search.cpp index 4d021434b76e1..6b31aea78823e 100644 --- a/examples/beam-search/beam-search.cpp +++ b/examples/beam-search/beam-search.cpp @@ -1,7 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "common.h" #include "llama.h" #include "build-info.h" diff --git a/examples/embd-input/embd-input-lib.cpp b/examples/embd-input/embd-input-lib.cpp index 036bdb3987f34..53076eb0fb4e4 100644 --- a/examples/embd-input/embd-input-lib.cpp +++ b/examples/embd-input/embd-input-lib.cpp @@ -1,8 +1,3 @@ -// Defines sigaction on msys: -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "embd-input.h" #include diff --git a/examples/main/main.cpp b/examples/main/main.cpp index 9201b53bd9a74..e6c70cc63272f 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -1,8 +1,3 @@ -// Defines sigaction on msys: -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "common.h" #include "console.h" diff --git a/examples/simple/simple.cpp b/examples/simple/simple.cpp index 4ee85faca9f4a..ba5de0cc61e54 100644 --- a/examples/simple/simple.cpp +++ b/examples/simple/simple.cpp @@ -1,7 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "build-info.h" #include "common.h" diff --git a/examples/speculative/speculative.cpp b/examples/speculative/speculative.cpp index c6211ac79569c..822d7b529f01d 100644 --- a/examples/speculative/speculative.cpp +++ b/examples/speculative/speculative.cpp @@ -1,7 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "build-info.h" #include "common.h" diff --git a/ggml-alloc.c b/ggml-alloc.c index c1939a4b7817b..695bbfe102cda 100644 --- a/ggml-alloc.c +++ b/ggml-alloc.c @@ -1,8 +1,3 @@ -// defines MAP_ANONYMOUS -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "ggml-alloc.h" #include "ggml.h" #include diff --git a/ggml.c b/ggml.c index 50adf18ec7f42..1ea4e5a4c6ac3 100644 --- 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" diff --git a/llama.cpp b/llama.cpp index 2c9071a8f55c6..74ffbc8d1f558 100644 --- 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" From 5e472776330237afc4726cc2201ca54ffdf44631 Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 28 Jun 2023 10:16:41 +0200 Subject: [PATCH 2/6] make : enable Darwin extensions for macOS to expose RLIMIT_MEMLOCK --- Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Makefile b/Makefile index 4623a23c40f46..1647ac4c64fee 100644 --- a/Makefile +++ b/Makefile @@ -109,6 +109,13 @@ ifeq ($(UNAME_S),Linux) 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 +ifeq ($(UNAME_S),Darwin) + MK_CFLAGS += -D_DARWIN_C_SOURCE + MK_CXXFLAGS += -D_DARWIN_C_SOURCE +endif + ifdef LLAMA_DEBUG MK_CFLAGS += -O0 -g MK_CXXFLAGS += -O0 -g From 09b7e8000e6135a4fe85194f4451b6ab3be32961 Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 6 Sep 2023 00:15:23 +0200 Subject: [PATCH 3/6] make : enable BSD extensions for DragonFlyBSD to expose RLIMIT_MEMLOCK --- Makefile | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Makefile b/Makefile index 1647ac4c64fee..82c4cadd6d014 100644 --- a/Makefile +++ b/Makefile @@ -111,10 +111,15 @@ 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 ifdef LLAMA_DEBUG MK_CFLAGS += -O0 -g From d007288dd800558b692ae11aa8354d70e9e1c3bc Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 6 Sep 2023 00:18:17 +0200 Subject: [PATCH 4/6] make : use BSD-specific FTMs to enable alloca on BSDs --- Makefile | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Makefile b/Makefile index 82c4cadd6d014..3aeaf825d71bd 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,22 @@ ifeq ($(UNAME_S),DragonFly) 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 From 41997549fe809137a6e69d2db0ef000b60069bea Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 6 Sep 2023 00:21:16 +0200 Subject: [PATCH 5/6] make : fix OpenBSD build by exposing newer POSIX definitions --- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Makefile b/Makefile index 3aeaf825d71bd..5f9cc1d55daa4 100644 --- a/Makefile +++ b/Makefile @@ -102,6 +102,14 @@ MK_LDFLAGS = 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) From 39d1b769f50a7023d1930210a6300f13c3e59d73 Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Wed, 6 Sep 2023 00:12:36 +0200 Subject: [PATCH 6/6] cmake : follow recent FTM improvements from Makefile --- CMakeLists.txt | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d4ed6179ea7a7..dd26daed79d50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 #