[flang-commits] [flang] [flang] CYGWIN required _GNU_SOURCE to be defined. (PR #66747)

Carlo Bramini via flang-commits flang-commits at lists.llvm.org
Tue Sep 19 01:25:58 PDT 2023


https://github.com/carlo-bramini created https://github.com/llvm/llvm-project/pull/66747

Compiling flang on CYGWIN prints this error message on the console:

```
llvm-project/flang/include/flang/Parser/char-block.h: In member function ‘std::string Fortran::parser::CharBlock::NULTerminatedToString() const’: llvm-project/flang/include/flang/Parser/char-block.h:106:26: error: ‘strnlen’ was not declared in this scope; did you mean ‘strlen’?
  106 |         /*not in std::*/ strnlen(interval_.start(), interval_.size())};
      |                          ^~~~~~~
      |                          strlen
```

`strnlen()` exists into NEWLIB, but it requires to define `_GNU_SOURCE` macro somewhere for having its prototype available.
This patch adds this definition, by using the same code written here:

https://github.com/llvm/llvm-project/blob/53602e6193d98b1e814b76a27e99ef7e18c9769c/third-party/benchmark/CMakeLists.txt#L230

>From ac410549229b73b8b5c9c84d426299ffcca48db0 Mon Sep 17 00:00:00 2001
From: Carlo Bramini <30959007+carlo-bramini at users.noreply.github.com>
Date: Tue, 19 Sep 2023 10:23:11 +0200
Subject: [PATCH] [flang] CYGWIN required _GNU_SOURCE to be defined.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Compiling flang on CYGWIN prints this error message on the console:

llvm-project/flang/include/flang/Parser/char-block.h: In member function ‘std::string Fortran::parser::CharBlock::NULTerminatedToString() const’:
llvm-project/flang/include/flang/Parser/char-block.h:106:26: error: ‘strnlen’ was not declared in this scope; did you mean ‘strlen’?
  106 |         /*not in std::*/ strnlen(interval_.start(), interval_.size())};
      |                          ^~~~~~~
      |                          strlen

strnlen() exists into NEWLIB, but it requires to define _GNU_SOURCE macro somewhere for having its prototype available.
This patch adds this definition, by using the same code written here:

https://github.com/llvm/llvm-project/blob/53602e6193d98b1e814b76a27e99ef7e18c9769c/third-party/benchmark/CMakeLists.txt#L230
---
 flang/CMakeLists.txt | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index ac30da89995ed31..660ba8d49bb3966 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -326,6 +326,14 @@ else ()
   add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
 endif ()
 
+if (CYGWIN)
+  # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
+  # predefined macro, which turns on all of the wonderful libc extensions.
+  # However g++ doesn't do this in Cygwin so we have to define it ourselfs
+  # since we depend on GNU/POSIX/BSD extensions.
+  add_definitions(-D_GNU_SOURCE=1)
+endif()
+
 # Configure Flang's Version.inc file.
 configure_file(
   ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in



More information about the flang-commits mailing list