Przemysław Pawełczyk commited on
Commit
beefa34
·
unverified ·
1 Parent(s): 2cfc05a

build : do not use _GNU_SOURCE gratuitously (#1129)

Browse files

* 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

CMakeLists.txt CHANGED
@@ -321,6 +321,49 @@ else()
321
  endif()
322
  endif()
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  if (WHISPER_PERF)
325
  set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_PERF)
326
  endif()
 
321
  endif()
322
  endif()
323
 
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
+
367
  if (WHISPER_PERF)
368
  set(WHISPER_EXTRA_FLAGS ${WHISPER_EXTRA_FLAGS} -DGGML_PERF)
369
  endif()
Makefile CHANGED
@@ -42,18 +42,55 @@ CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
42
  CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
43
  LDFLAGS =
44
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  endif
50
 
51
  # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
52
  # and on macOS its availability depends on enabling Darwin extensions
 
53
  ifeq ($(UNAME_S),Darwin)
54
  CFLAGS += -D_DARWIN_C_SOURCE
55
  CXXFLAGS += -D_DARWIN_C_SOURCE
56
  endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # OS specific
59
  # TODO: support Windows
 
42
  CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
43
  LDFLAGS =
44
 
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
65
  endif
66
 
67
  # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
68
  # and on macOS its availability depends on enabling Darwin extensions
69
+ # similarly on DragonFly, enabling BSD extensions is necessary
70
  ifeq ($(UNAME_S),Darwin)
71
  CFLAGS += -D_DARWIN_C_SOURCE
72
  CXXFLAGS += -D_DARWIN_C_SOURCE
73
  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
94
 
95
  # OS specific
96
  # TODO: support Windows
examples/command/command.cpp CHANGED
@@ -6,8 +6,8 @@
6
  // ref: https://github.com/ggerganov/whisper.cpp/issues/171
7
  //
8
 
9
- #include "common.h"
10
  #include "common-sdl.h"
 
11
  #include "whisper.h"
12
 
13
  #include <sstream>
 
6
  // ref: https://github.com/ggerganov/whisper.cpp/issues/171
7
  //
8
 
 
9
  #include "common-sdl.h"
10
+ #include "common.h"
11
  #include "whisper.h"
12
 
13
  #include <sstream>
examples/stream/stream.cpp CHANGED
@@ -3,8 +3,8 @@
3
  // A very quick-n-dirty implementation serving mainly as a proof of concept.
4
  //
5
 
6
- #include "common.h"
7
  #include "common-sdl.h"
 
8
  #include "whisper.h"
9
 
10
  #include <cassert>
 
3
  // A very quick-n-dirty implementation serving mainly as a proof of concept.
4
  //
5
 
 
6
  #include "common-sdl.h"
7
+ #include "common.h"
8
  #include "whisper.h"
9
 
10
  #include <cassert>
examples/talk-llama/llama.cpp CHANGED
@@ -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
-
9
  #include "llama-util.h"
10
  #include "llama.h"
11
 
 
 
 
 
 
 
 
 
 
1
  #include "llama-util.h"
2
  #include "llama.h"
3
 
examples/talk-llama/talk-llama.cpp CHANGED
@@ -1,8 +1,8 @@
1
  // Talk with AI
2
  //
3
 
4
- #include "common.h"
5
  #include "common-sdl.h"
 
6
  #include "whisper.h"
7
  #include "llama.h"
8
 
 
1
  // Talk with AI
2
  //
3
 
 
4
  #include "common-sdl.h"
5
+ #include "common.h"
6
  #include "whisper.h"
7
  #include "llama.h"
8
 
examples/talk/talk.cpp CHANGED
@@ -1,8 +1,8 @@
1
  // Talk with AI
2
  //
3
 
4
- #include "common.h"
5
  #include "common-sdl.h"
 
6
  #include "whisper.h"
7
  #include "gpt-2.h"
8
 
 
1
  // Talk with AI
2
  //
3
 
 
4
  #include "common-sdl.h"
5
+ #include "common.h"
6
  #include "whisper.h"
7
  #include "gpt-2.h"
8
 
ggml.c CHANGED
@@ -1,4 +1,3 @@
1
- #define _GNU_SOURCE // Defines CLOCK_MONOTONIC on Linux
2
  #define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
3
 
4
  #include "ggml.h"
 
 
1
  #define _CRT_SECURE_NO_DEPRECATE // Disables ridiculous "unsafe" warnigns on Windows
2
 
3
  #include "ggml.h"