Skip to content

Commit 0a217ca

Browse files
authored
build : do not use _GNU_SOURCE gratuitously (ggml-org#1129)
* Do not use _GNU_SOURCE gratuitously. What is needed to build whisper.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 in ggml, so enable GNU libc extensions for Linux builds to cover that. There is no need to penalize musl libc which simply follows standards. 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 minimal FTM (_XOPEN_SOURCE=600) or other FTM depending on their needs. It builds without issues in Alpine (musl libc), Ubuntu (glibc), MSYS2. * examples : include SDL headers before other headers Avoid macOS build error when _DARWIN_C_SOURCE is not defined, brought by SDL2 relying on Darwin extension memset_pattern4/8/16 (from string.h). * 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 8561b00 commit 0a217ca

File tree

8 files changed

+88
-17
lines changed

8 files changed

+88
-17
lines changed

CMakeLists.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,49 @@ else()
321321
endif()
322322
endif()
323323

324+
# clock_gettime came in POSIX.1b (1993)
325+
# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
326+
# posix_memalign came in POSIX.1-2001 / SUSv3
327+
# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
328+
add_compile_definitions(_XOPEN_SOURCE=600)
329+
330+
# Somehow in OpenBSD whenever POSIX conformance is specified
331+
# some string functions rely on locale_t availability,
332+
# which was introduced in POSIX.1-2008, forcing us to go higher
333+
IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
334+
remove_definitions(-D_XOPEN_SOURCE=600)
335+
add_compile_definitions(_XOPEN_SOURCE=700)
336+
ENDIF()
337+
338+
# Data types, macros and functions related to controlling CPU affinity
339+
# are available on Linux through GNU extensions in libc
340+
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
341+
add_compile_definitions(_GNU_SOURCE)
342+
ENDIF()
343+
344+
# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
345+
# and on macOS its availability depends on enabling Darwin extensions
346+
# similarly on DragonFly, enabling BSD extensions is necessary
347+
IF (CMAKE_SYSTEM_NAME MATCHES "Darwin")
348+
add_compile_definitions(_DARWIN_C_SOURCE)
349+
ENDIF()
350+
IF (CMAKE_SYSTEM_NAME MATCHES "DragonFly")
351+
add_compile_definitions(_DARWIN_C_SOURCE)
352+
ENDIF()
353+
354+
# alloca is a non-standard interface that is not visible on BSDs when
355+
# POSIX conformance is specified, but not all of them provide a clean way
356+
# to enable it in such cases
357+
IF (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
358+
add_compile_definitions(__BSD_VISIBLE)
359+
ENDIF()
360+
IF (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
361+
add_compile_definitions(_NETBSD_SOURCE)
362+
ENDIF()
363+
IF (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
364+
add_compile_definitions(_BSD_SOURCE)
365+
ENDIF()
366+
324367
if (WHISPER_PERF)
325368
set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_PERF)
326369
endif()

Makefile

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,55 @@ CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
4242
CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
4343
LDFLAGS =
4444

45-
# ref: https://github.com/ggerganov/whisper.cpp/issues/37
46-
ifneq ($(wildcard /usr/include/musl/*),)
47-
CFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
48-
CXXFLAGS += -D_POSIX_SOURCE -D_GNU_SOURCE
45+
# clock_gettime came in POSIX.1b (1993)
46+
# CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
47+
# posix_memalign came in POSIX.1-2001 / SUSv3
48+
# M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
49+
CFLAGS += -D_XOPEN_SOURCE=600
50+
CXXFLAGS += -D_XOPEN_SOURCE=600
51+
52+
# Somehow in OpenBSD whenever POSIX conformance is specified
53+
# some string functions rely on locale_t availability,
54+
# which was introduced in POSIX.1-2008, forcing us to go higher
55+
ifeq ($(UNAME_S),OpenBSD)
56+
CFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
57+
CXXFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
58+
endif
59+
60+
# Data types, macros and functions related to controlling CPU affinity
61+
# are available on Linux through GNU extensions in libc
62+
ifeq ($(UNAME_S),Linux)
63+
CFLAGS += -D_GNU_SOURCE
64+
CXXFLAGS += -D_GNU_SOURCE
4965
endif
5066

5167
# RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
5268
# and on macOS its availability depends on enabling Darwin extensions
69+
# similarly on DragonFly, enabling BSD extensions is necessary
5370
ifeq ($(UNAME_S),Darwin)
5471
CFLAGS += -D_DARWIN_C_SOURCE
5572
CXXFLAGS += -D_DARWIN_C_SOURCE
5673
endif
74+
ifeq ($(UNAME_S),DragonFly)
75+
CFLAGS += -D__BSD_VISIBLE
76+
CXXFLAGS += -D__BSD_VISIBLE
77+
endif
78+
79+
# alloca is a non-standard interface that is not visible on BSDs when
80+
# POSIX conformance is specified, but not all of them provide a clean way
81+
# to enable it in such cases
82+
ifeq ($(UNAME_S),FreeBSD)
83+
CFLAGS += -D__BSD_VISIBLE
84+
CXXFLAGS += -D__BSD_VISIBLE
85+
endif
86+
ifeq ($(UNAME_S),NetBSD)
87+
CFLAGS += -D_NETBSD_SOURCE
88+
CXXFLAGS += -D_NETBSD_SOURCE
89+
endif
90+
ifeq ($(UNAME_S),OpenBSD)
91+
CFLAGS += -D_BSD_SOURCE
92+
CXXFLAGS += -D_BSD_SOURCE
93+
endif
5794

5895
# OS specific
5996
# TODO: support Windows

examples/command/command.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
// ref: https://github.com/ggerganov/whisper.cpp/issues/171
77
//
88

9-
#include "common.h"
109
#include "common-sdl.h"
10+
#include "common.h"
1111
#include "whisper.h"
1212

1313
#include <sstream>

examples/stream/stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// A very quick-n-dirty implementation serving mainly as a proof of concept.
44
//
55

6-
#include "common.h"
76
#include "common-sdl.h"
7+
#include "common.h"
88
#include "whisper.h"
99

1010
#include <cassert>

examples/talk-llama/llama.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// Defines fileno on msys:
2-
#ifndef _GNU_SOURCE
3-
#define _GNU_SOURCE
4-
#include <cstddef>
5-
#include <cstdint>
6-
#include <cstdio>
7-
#endif
8-
91
#include "llama-util.h"
102
#include "llama.h"
113

examples/talk-llama/talk-llama.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Talk with AI
22
//
33

4-
#include "common.h"
54
#include "common-sdl.h"
5+
#include "common.h"
66
#include "whisper.h"
77
#include "llama.h"
88

examples/talk/talk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Talk with AI
22
//
33

4-
#include "common.h"
54
#include "common-sdl.h"
5+
#include "common.h"
66
#include "whisper.h"
77
#include "gpt-2.h"
88

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"

0 commit comments

Comments
 (0)