Skip to content

Commit cb6c44c

Browse files
authored
build : do not use _GNU_SOURCE gratuitously (#2035)
* 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
1 parent a21baeb commit cb6c44c

File tree

10 files changed

+93
-33
lines changed

10 files changed

+93
-33
lines changed

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,49 @@ else()
551551
message(STATUS "Unknown architecture")
552552
endif()
553553

554+
# clock_gettime came in POSIX.1b (1993)
555+
# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
556+
# posix_memalign came in POSIX.1-2001 / SUSv3
557+
# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
558+
add_compile_definitions(_XOPEN_SOURCE=600)
559+
560+
# Somehow in OpenBSD whenever POSIX conformance is specified
561+
# some string functions rely on locale_t availability,
562+
# which was introduced in POSIX.1-2008, forcing us to go higher
563+
IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
564+
remove_definitions(-D_XOPEN_SOURCE=600)
565+
add_compile_definitions(_XOPEN_SOURCE=700)
566+
ENDIF()
567+
568+
# Data types, macros and functions related to controlling CPU affinity and
569+
# some memory allocation are available on Linux through GNU extensions in libc
570+
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
571+
add_compile_definitions(_GNU_SOURCE)
572+
ENDIF()
573+
574+
# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
575+
# and on macOS its availability depends on enabling Darwin extensions
576+
# similarly on DragonFly, enabling BSD extensions is necessary
577+
IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
578+
add_compile_definitions(_DARWIN_C_SOURCE)
579+
ENDIF()
580+
IF (CMAKE_SYSTEM_NAME MATCHES "DragonFly")
581+
add_compile_definitions(_DARWIN_C_SOURCE)
582+
ENDIF()
583+
584+
# alloca is a non-standard interface that is not visible on BSDs when
585+
# POSIX conformance is specified, but not all of them provide a clean way
586+
# to enable it in such cases
587+
IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
588+
add_compile_definitions(__BSD_VISIBLE)
589+
ENDIF()
590+
IF (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
591+
add_compile_definitions(_NETBSD_SOURCE)
592+
ENDIF()
593+
IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
594+
add_compile_definitions(_BSD_SOURCE)
595+
ENDIF()
596+
554597
#
555598
# libraries
556599
#

Makefile

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,56 @@ MK_CFLAGS = $(OPT) -std=c11 -fPIC
106106
MK_CXXFLAGS = $(OPT) -std=c++11 -fPIC
107107
MK_LDFLAGS =
108108

109+
# clock_gettime came in POSIX.1b (1993)
110+
# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
111+
# posix_memalign came in POSIX.1-2001 / SUSv3
112+
# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
113+
MK_CFLAGS += -D_XOPEN_SOURCE=600
114+
MK_CXXFLAGS += -D_XOPEN_SOURCE=600
115+
116+
# Somehow in OpenBSD whenever POSIX conformance is specified
117+
# some string functions rely on locale_t availability,
118+
# which was introduced in POSIX.1-2008, forcing us to go higher
119+
ifeq ($(UNAME_S),OpenBSD)
120+
MK_CFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
121+
MK_CXXFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
122+
endif
123+
124+
# Data types, macros and functions related to controlling CPU affinity and
125+
# some memory allocation are available on Linux through GNU extensions in libc
126+
ifeq ($(UNAME_S),Linux)
127+
MK_CFLAGS += -D_GNU_SOURCE
128+
MK_CXXFLAGS += -D_GNU_SOURCE
129+
endif
130+
131+
# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
132+
# and on macOS its availability depends on enabling Darwin extensions
133+
# similarly on DragonFly, enabling BSD extensions is necessary
134+
ifeq ($(UNAME_S),Darwin)
135+
MK_CFLAGS += -D_DARWIN_C_SOURCE
136+
MK_CXXFLAGS += -D_DARWIN_C_SOURCE
137+
endif
138+
ifeq ($(UNAME_S),DragonFly)
139+
MK_CFLAGS += -D__BSD_VISIBLE
140+
MK_CXXFLAGS += -D__BSD_VISIBLE
141+
endif
142+
143+
# alloca is a non-standard interface that is not visible on BSDs when
144+
# POSIX conformance is specified, but not all of them provide a clean way
145+
# to enable it in such cases
146+
ifeq ($(UNAME_S),FreeBSD)
147+
MK_CFLAGS += -D__BSD_VISIBLE
148+
MK_CXXFLAGS += -D__BSD_VISIBLE
149+
endif
150+
ifeq ($(UNAME_S),NetBSD)
151+
MK_CFLAGS += -D_NETBSD_SOURCE
152+
MK_CXXFLAGS += -D_NETBSD_SOURCE
153+
endif
154+
ifeq ($(UNAME_S),OpenBSD)
155+
MK_CFLAGS += -D_BSD_SOURCE
156+
MK_CXXFLAGS += -D_BSD_SOURCE
157+
endif
158+
109159
ifdef LLAMA_DEBUG
110160
MK_CFLAGS += -O0 -g
111161
MK_CXXFLAGS += -O0 -g

examples/beam-search/beam-search.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#ifndef _GNU_SOURCE
2-
#define _GNU_SOURCE
3-
#endif
4-
51
#include "common.h"
62
#include "llama.h"
73
#include "build-info.h"

examples/embd-input/embd-input-lib.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Defines sigaction on msys:
2-
#ifndef _GNU_SOURCE
3-
#define _GNU_SOURCE
4-
#endif
5-
61
#include "embd-input.h"
72

83
#include <cassert>

examples/main/main.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Defines sigaction on msys:
2-
#ifndef _GNU_SOURCE
3-
#define _GNU_SOURCE
4-
#endif
5-
61
#include "common.h"
72

83
#include "console.h"

examples/simple/simple.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#ifndef _GNU_SOURCE
2-
#define _GNU_SOURCE
3-
#endif
4-
51
#include "build-info.h"
62

73
#include "common.h"

examples/speculative/speculative.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#ifndef _GNU_SOURCE
2-
#define _GNU_SOURCE
3-
#endif
4-
51
#include "build-info.h"
62

73
#include "common.h"

ggml-alloc.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// defines MAP_ANONYMOUS
2-
#ifndef _GNU_SOURCE
3-
#define _GNU_SOURCE
4-
#endif
5-
61
#include "ggml-alloc.h"
72
#include "ggml.h"
83
#include <assert.h>

ggml.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define _GNU_SOURCE // Defines CLOCK_MONOTONIC on Linux
21
#define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
32

43
#include "ggml.h"

llama.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// Defines fileno on msys:
2-
#ifndef _GNU_SOURCE
3-
#define _GNU_SOURCE
4-
#endif
5-
61
#include "llama.h"
72

83
#include "ggml.h"

0 commit comments

Comments
 (0)