Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[LLVM][Cygwin] Define _GNU_SOURCE on Cygwin as well. #138329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2025

Conversation

jeremyd2019
Copy link
Contributor

Without it, certain functions such as dladdr are not make available by the headers.

@llvmbot llvmbot added the cmake Build system in general and CMake in particular label May 2, 2025
@jeremyd2019
Copy link
Contributor Author

jeremyd2019 commented May 2, 2025

@mstorsjo
I'm not sure about the _FILE_OFFSET_BITS define - I don't see that Cygwin uses it to change the size of off_t, so I could add LLVM_USING_GLIBC to its if, but I also don't see how it would hurt anything, and 32-bit Cygwin is barely on life support at this point anyway.

(It's already unsupported upstream, I personally only plan to try to support it until Windows 10 end-of-support, and Git-for-Windows needs to support some tiny subset until 2029 IIRC. I'm sure llvm isn't part of that since it hasn't been usable up to now).

@mstorsjo
Copy link
Member

mstorsjo commented May 2, 2025

@mstorsjo I'm not sure about the _FILE_OFFSET_BITS define - I don't see that Cygwin uses it to change the size of off_t, so I could add LLVM_USING_GLIBC to its if, but I also don't see how it would hurt anything, and 32-bit Cygwin is barely on life support at this point anyway.

(It's already unsupported upstream, I personally only plan to try to support it until Windows 10 end-of-support, and Git-for-Windows needs to support some tiny subset until 2029 IIRC. I'm sure llvm isn't part of that since it hasn't been usable up to now).

Yeah I don't have a very strong opinion either way. Theoretically, cygwin could start using it, but it's of course unlikely. Then again, it doesn't hurt keeping it like this either.

But perhaps we could split the if and just do the _GNU_SOURCE for cygwin anyway - that makes the intent clearer, so that future readers will see that we didn't expect _FILE_OFFSET_BITS to have any effect at the time and didn't need/intend for that to be defined.

@jeremyd2019
Copy link
Contributor Author

so

if (CYGWIN)
  add_compile_definitions(_GNU_SOURCE)
  list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
endif ()

right above, to go with the SunOS and AIX checks, and leave the LLVM_USING_GLIBC if alone?

@mstorsjo
Copy link
Member

mstorsjo commented May 2, 2025

so

if (CYGWIN)
  add_compile_definitions(_GNU_SOURCE)
  list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
endif ()

right above, to go with the SunOS and AIX checks, and leave the LLVM_USING_GLIBC if alone?

Nah, I'd split the current LLVM_USING_GLIBC into two, one if (LLVM_USING_GLIBC OR CYGWIN) and one if (LLVM_USING_CYGWIN). The comment above seems mostly relevant and on point for cygwin too, so we can keep the code in place where it is right now.

Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
@jeremyd2019 jeremyd2019 force-pushed the llvm-cygwin-gnu-source branch from 31e9552 to 3fc3075 Compare May 2, 2025 19:58
Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@mstorsjo mstorsjo merged commit 9633f87 into llvm:main May 3, 2025
9 of 11 checks passed
@jeremyd2019 jeremyd2019 deleted the llvm-cygwin-gnu-source branch May 3, 2025 22:32
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 3, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
@jeremyd2019
Copy link
Contributor Author

cygwin's packaging builds clang standalone, and in that configuration it seems that _GNU_SOURCE is not defined despite this change. Any thoughts?

@mstorsjo
Copy link
Member

mstorsjo commented May 4, 2025

cygwin's packaging builds clang standalone, and in that configuration it seems that _GNU_SOURCE is not defined despite this change. Any thoughts?

Hmm. I don't really have any experience with building clang standalone in that way; does the clang source tree have any form of local corresponding code that sets _GNU_SOURCE for glibc there? I presume you've tested this after rebuilding llvm with these changes, so it's not carried on via some of the llvm cmake files? (I'm not sure how the standalone builds work if they inherit things from such files, or if it configures things up from scratch.)

@jeremyd2019
Copy link
Contributor Author

jeremyd2019 commented May 4, 2025

cygwin's packaging builds clang standalone, and in that configuration it seems that _GNU_SOURCE is not defined despite this change. Any thoughts?

Hmm. I don't really have any experience with building clang standalone in that way; does the clang source tree have any form of local corresponding code that sets _GNU_SOURCE for glibc there?

just

# This check requires _GNU_SOURCE on linux
check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
if( CLANG_HAVE_DLFCN_H )
include(CheckLibraryExists)
include(CheckSymbolExists)
check_library_exists(dl dlopen "" HAVE_LIBDL)
if( HAVE_LIBDL )
list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
endif()
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
if( HAVE_LIBDL )
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
endif()
endif()

I presume you've tested this after rebuilding llvm with these changes, so it's not carried on via some of the llvm cmake files? (I'm not sure how the standalone builds work if they inherit things from such files, or if it configures things up from scratch.)

Yep, I'm trying to build and integrate the patches I've been contributing here into Cygwin's package in addition to MSYS2's.

Actually:

/usr/lib/cmake/llvm/LLVMConfig.cmake:set(LLVM_DEFINITIONS "-D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS")

This is odd, the other -D__STDC flags were present but _GNU_SOURCE was not. None of them are referenced in clang's tree. Something strange is going on...

@mstorsjo
Copy link
Member

mstorsjo commented May 5, 2025

cygwin's packaging builds clang standalone, and in that configuration it seems that _GNU_SOURCE is not defined despite this change. Any thoughts?

Hmm. I don't really have any experience with building clang standalone in that way; does the clang source tree have any form of local corresponding code that sets _GNU_SOURCE for glibc there?

just

# This check requires _GNU_SOURCE on linux
check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
if( CLANG_HAVE_DLFCN_H )
include(CheckLibraryExists)
include(CheckSymbolExists)
check_library_exists(dl dlopen "" HAVE_LIBDL)
if( HAVE_LIBDL )
list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
endif()
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
if( HAVE_LIBDL )
list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
endif()
endif()

Ok, I see. I guess if it strictly is necessary, then the if(CLANG_BUILT_STANDALONE) block at the top, which contains things like set(CMAKE_CXX_STANDARD 17 ... and other things matching what the toplevel LLVM CMakeLists.txt does, could take such things. But I would somehow expect that find_package(LLVM ... would pull in the LLVMConfig.cmake which you found out contained set(LLVM_DEFINITIONS "-D_GNU_SOURCE ..., so I guess that's the primary question now, why that's not happening.

(In case you're building in an existing build tree, things like this might not be picked up due to CMakeCache.txt.)

@jeremyd2019
Copy link
Contributor Author

also, llvm-config --cppflags contains it

Perhaps it needs to explicitly add LLVM_DEFINITONS to add_definitions and CMAKE_REQUIRED_DEFINITIONS?

@jeremyd2019
Copy link
Contributor Author

https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source says use

find_package(LLVM REQUIRED CONFIG)

separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
include_directories(${LLVM_INCLUDE_DIRS})

clang/CMakeLists.txt has

  find_package(LLVM REQUIRED HINTS "${LLVM_CMAKE_DIR}")
...
  include_directories(${LLVM_INCLUDE_DIRS})
  link_directories("${LLVM_LIBRARY_DIR}")

So I'm thinking the separate_arguments and add_definitions should be plugged in there?

@jeremyd2019
Copy link
Contributor Author

jeremyd2019 commented May 5, 2025

this seems to work

--- clang-20.1.4-1.x86_64/origsrc/clang-20.1.4.src/CMakeLists.txt       2025-04-29 16:05:17.000000000 -0700
+++ clang-20.1.4-1.x86_64/src/clang-20.1.4.src/CMakeLists.txt   2025-05-05 13:58:28.443602600 -0700
@@ -68,6 +68,10 @@
   option(CLANG_ENABLE_BOOTSTRAP "Generate the clang bootstrap target" OFF)
   option(LLVM_ENABLE_LIBXML2 "Use libxml2 if available." ON)

+  separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
+  add_definitions(${LLVM_DEFINITIONS_LIST})
+  list(APPEND CMAKE_REQUIRED_DEFINITIONS ${LLVM_DEFINITIONS_LIST})
+
   include(AddLLVM)
   include(TableGen)
   include(HandleLLVMOptions)
@@ -181,20 +185,19 @@
 check_include_file(sys/resource.h CLANG_HAVE_RLIMITS)

 # This check requires _GNU_SOURCE on linux
+include(CMakePushCheckState)
 check_include_file(dlfcn.h CLANG_HAVE_DLFCN_H)
 if( CLANG_HAVE_DLFCN_H )
   include(CheckLibraryExists)
   include(CheckSymbolExists)
   check_library_exists(dl dlopen "" HAVE_LIBDL)
+  cmake_push_check_state()
   if( HAVE_LIBDL )
     list(APPEND CMAKE_REQUIRED_LIBRARIES dl)
   endif()
-  list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
+  #list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
   check_symbol_exists(dladdr dlfcn.h CLANG_HAVE_DLADDR)
-  list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
-  if( HAVE_LIBDL )
-    list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES dl)
-  endif()
+  cmake_pop_check_state()
 endif()

 set(CLANG_RESOURCE_DIR "" CACHE STRING

Issues I ran into:

  • the list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) wipes out all occurences in the list, not just the one it just added. Better to push/pop (used in other llvm cmake files).
  • HandleLLVMOptions.cmake nukes the LLVM_DEFINITIONS variable, so I had to add them before including that.

jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 5, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in Clang's CMakeLists.txt.

Get rid of the list(REMOVE CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
later, as list(REMOVE) is documented to remove *all* occurences of the
item, not just the one that was just added.  Instead, make use of
cmake_push_check_state() and cmake_pop_check_state(), which is already
used in other cmake files.
@jeremyd2019
Copy link
Contributor Author

#138587

jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 5, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 5, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in Clang's CMakeLists.txt.

Get rid of the list(REMOVE CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
later, as list(REMOVE) is documented to remove *all* occurences of the
item, not just the one that was just added.  Instead, make use of
cmake_push_check_state() and cmake_pop_check_state(), which is already
used in other cmake files.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 7, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in standalone projects' cmakes.
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 14, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 14, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in standalone projects' cmakes.
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 15, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in standalone projects' cmakes.
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 15, 2025
Without it, certain functions such as dladdr are not make available by
the headers.

Signed-off-by: Jeremy Drake <github@jdrake.com>
jeremyd2019 added a commit to jeremyd2019/llvm-project that referenced this pull request May 15, 2025
In llvm#138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen.  Follow the documentation in
https://llvm.org/docs/CMake.html#developing-llvm-passes-out-of-source
and add the LLVM_DEFINITIONS in standalone projects' cmakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.