Skip to content
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

Fix linker not finding pthreads or dl #435

Closed
wants to merge 1 commit into from

Conversation

RLovelett
Copy link
Contributor

On Arch Linux the clang++ linker is ignoring/not seeing the -lpthread or -ldl linker flags. This patch resolves it by adding -pthreads flag to the clang++ arguments and by specifying it again inside of stdlib. I won't claim that this is the best solution. I'll just say it is a solution that I have found to work. I'm just beginning to learn cmake so I wasn't sure how to just move the -lpthread and -ldl flags to either the beginning or the end of the argument list.

Ultimately the thing that has me the most puzzled is why clang cannot see the flags without the patch... They are clearly there in the arguments (checkout allout.txt for a full build log). What is more is if I manually move them to either the beginning or the end of the argument list then it links. I'm by no means a clang expert so this may be expected behavior.

Arch clang++ version:

$ clang++ --version
clang version 3.7.0 (tags/RELEASE_370/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Motivation

I first started questioning the issue on the mailing list.

The original error that this attempts to solve:

FAILED: : && /usr/bin/clang++  -fPIC -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3  -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-z,defs   -target x86_64-unknown-linux-gnu -isysroot / -lpthread -ldl -Xlinker -T -Xlinker /home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/swift-linux-x86_64/./lib/swift/linux/x86_64/swift.ld -L/home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/swift-linux-x86_64/./lib/swift/linux/x86_64 -L/home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/swift-linux-x86_64/./bin/../lib/swift/linux/x86_64 -L/home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/swift-linux-x86_64/./bin/../lib/swift/linux -shared -Wl,-soname,libswiftCore.so -o lib/swift/linux/x86_64/libswiftCore.so stdlib/public/core/linux/x86_64/Swift.o -L/home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/llvm-linux-x86_64/lib  -L/home/ryan/Source/swift/src/build/Ninja-ReleaseAssert/llvm-linux-x86_64/./lib lib/swift/linux/x86_64/libswiftRuntime.a lib/swift/linux/x86_64/libswiftStdlibStubs.a -licuuc -licui18n -lbsd -Wl,-rpath,"\$ORIGIN:/usr/lib/swift/linux" && :
lib/swift/linux/x86_64/libswiftRuntime.a(Casting.cpp.o): In function `swift_getTypeName':
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text.swift_getTypeName+0xde): undefined reference to `pthread_rwlock_rdlock'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text.swift_getTypeName+0x17c): undefined reference to `pthread_rwlock_unlock'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text.swift_getTypeName+0x184): undefined reference to `pthread_rwlock_wrlock'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text.swift_getTypeName+0x23a): undefined reference to `pthread_rwlock_unlock'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text.swift_getTypeName+0x2c8): undefined reference to `pthread_rwlock_unlock'
lib/swift/linux/x86_64/libswiftRuntime.a(Casting.cpp.o): In function `_addImageProtocolConformances(dl_phdr_info*, unsigned long, void*)':
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text._ZL29_addImageProtocolConformancesP12dl_phdr_infomPv+0x1e): undefined reference to `dlopen'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text._ZL29_addImageProtocolConformancesP12dl_phdr_infomPv+0x30): undefined reference to `dlsym'
/home/ryan/Source/swift/src/swift/stdlib/public/runtime/Casting.cpp:(.text._ZL29_addImageProtocolConformancesP12dl_phdr_infomPv+0x55): undefined reference to `dlclose'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

@gribozavr
Copy link
Collaborator

@RLovelett Thank you for working on this!

I think the issue is that the current CMake scripts are set up in such a way that the results produced by _add_variant_link_flags() are not fed into target_link_libraries(), instead they are used with set_property(LINK_FLAGS). (You can find this in _add_swift_library_single().)

May I ask you to try changing _add_variant_link_flags() so that it returns two lists -- one for target_link_libraries(), and another for set_property(LINK_FLAGS), and updating _add_swift_library_single()? This would be a much cleaner solution than duplicating flags.

@RLovelett
Copy link
Contributor Author

Your enthusiasm makes me think that this was something you were aware of. Glad I'm not barking up the wrong tree completely.

I think I've got your suggestions working. I'm building from scratch in a VM right now (it is slow). I'm new to CMake; who knew functions don't really return anything? That was a head scratcher for a few minutes.

@jinmingjian
Copy link

I just find the current master is OK to build under my arch(sync to latest) (with py2 lib patching). No flag patching is need any more.

@RLovelett
Copy link
Contributor Author

I can confirm that I am seeing the same thing. I want to run git bisect to figure out which commit fixed it. Then I'll report back and close this issue.

@RLovelett
Copy link
Contributor Author

Ok I give up. I cannot even reproduce the compiler error anymore going back to swift-2.2-SNAPSHOT-2015-12-01-b. I'm just going to close this.

@RLovelett RLovelett closed this Dec 13, 2015
@RLovelett RLovelett deleted the linking-pthreads-and-dl branch December 13, 2015 15:51
@RLovelett RLovelett restored the linking-pthreads-and-dl branch December 19, 2015 21:51
@RLovelett
Copy link
Contributor Author

Like a phoenix rising from the ashes this bug is back. When trying to build commit 48ec67c I am getting this error again.

So I resurrected my patches from before with @gribozavr's suggestions and it is compiling again. Voila!

Discussion

As @gribozavr suggested I made _add_variant_link_flags() return two variables. The first, link_libs, are the libraries that will eventually be sent to the target_link_libraries. The second, link_flags, is more or less the same flag that existed before the patch. The only difference is that it no longer contains the link_libs.

While debugging I made two CMake MESSAGE statements to dump the contents of the new variables. As well as logged the actual lines that I was hoping to see change. A comparison is shown below.

Before

-- LINK_FLAGS -target x86_64-unknown-linux-gnu -isysroot / -lpthread -ldl -Xlinker -T -Xlinker /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64/swift.ld
[12/43] cd /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/stdlib/public/core && /usr/bin/cmake -E copy /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/lib/swift_static/linux/x86_64/libswiftCore.a /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift_static/linux/libswiftCore.a
FAILED: : && /usr/bin/clang++  -fPIC -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3  -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-z,defs   -target x86_64-unknown-linux-gnu -isysroot / -Xlinker -T -Xlinker /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64/swift.ld  -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64 -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./bin/../lib/swift/linux/x86_64 -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./bin/../lib/swift/linux -shared -Wl,-soname,libswiftCore.so -o lib/swift/linux/x86_64/libswiftCore.so stdlib/public/core/linux/x86_64/Swift.o -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/llvm-linux-x86_64/lib  -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/llvm-linux-x86_64/./lib lib/swift/linux/x86_64/libswiftRuntime.a lib/swift/linux/x86_64/libswiftStdlibStubs.a -licuuc -licui18n -lbsd -Wl,-rpath,"\$ORIGIN:/usr/lib/swift/linux" &&

After

-- LINK LIBRARIES -lpthread;-ldl
-- LINK_FLAGS -target x86_64-unknown-linux-gnu -isysroot / -Xlinker -T -Xlinker /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64/swift.ld
[10/43] : && /usr/bin/clang++  -fPIC -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3  -Wl,-O1,--sort-common,--as-needed,-z,relro -Wl,-z,defs   -target x86_64-unknown-linux-gnu -isysroot / -Xlinker -T -Xlinker /home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64/swift.ld  -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux/x86_64 -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./bin/../lib/swift/linux/x86_64 -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/swift-linux-x86_64/./bin/../lib/swift/linux -shared -Wl,-soname,libswiftCore.so -o lib/swift/linux/x86_64/libswiftCore.so stdlib/public/core/linux/x86_64/Swift.o -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/llvm-linux-x86_64/lib  -L/home/ryan/Source/swift-aur/src/build/buildbot_linux/llvm-linux-x86_64/./lib -lpthread -ldl lib/swift/linux/x86_64/libswiftRuntime.a lib/swift/linux/x86_64/libswiftStdlibStubs.a -licuuc -licui18n -lbsd -lpthread -ldl -Wl,-rpath,"\$ORIGIN:/usr/lib/swift/linux" && :

@RLovelett RLovelett reopened this Dec 19, 2015
@RLovelett RLovelett force-pushed the linking-pthreads-and-dl branch 2 times, most recently from 758a2f2 to 9756d23 Compare December 19, 2015 22:00
@RLovelett
Copy link
Contributor Author

@gribozavr I still don't think this is a complete patch because there are at least two other uses of _add_variant_link_flags() (are there more that I'm not seeing?). Here and Here.

I'm pretty sure I know how to fix them; duplicate the code that I already wrote for those two places. The problem is I don't know how to test it. Do you have any suggestions?

@RLovelett RLovelett force-pushed the linking-pthreads-and-dl branch 2 times, most recently from 94cb2d7 to aa91ecd Compare December 20, 2015 17:32
@@ -1119,6 +1122,8 @@ function(_add_swift_library_single target name)
set_property(TARGET "${target}" APPEND_STRING PROPERTY
LINK_FLAGS " ${link_flags} -L${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR} -L${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
target_link_libraries("${target}" PRIVATE
${link_libs})
target_link_libraries("${target}" PRIVATE
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can do:

target_link_libraries("${target}" PRIVATE
  ${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES}
  ${link_libs})

@gribozavr
Copy link
Collaborator

@RLovelett It looks like we are not using add_swift_llvm_loadable_module and the whole function can be removed (as a separate PR).

The other function is used out-of-tree. Just use the same pattern, and I will test before merging.

Thanks!

@RLovelett RLovelett force-pushed the linking-pthreads-and-dl branch 2 times, most recently from 36f6bcc to acaa5ea Compare December 23, 2015 01:17
@RLovelett
Copy link
Contributor Author

I think I've incorporated the feedback and I have rebased on the latest master.

EDIT: I also made merge request #740 that removes add_swift_llvm_loadable_module.

RLovelett added a commit to RLovelett/swift that referenced this pull request Dec 23, 2015
This function was discovered to be unused during the review of apple#435. It
was requested that it be removed as a subsequent pull.
@RLovelett RLovelett force-pushed the linking-pthreads-and-dl branch 2 times, most recently from ebbae91 to 0141340 Compare December 23, 2015 02:35
@RLovelett
Copy link
Contributor Author

Sadly I no longer thing this is working right. EDIT: I still think it solves this problem I just think it introduces another one. One that is completely beyond my depth to solve.

On my latest build after applying this patch to master I get this segfault:

[403/546] Linking CXX executable bin/swift-demangle
FAILED: : && /usr/bin/clang++   -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3  -Wl,-O1,--sort-common,--as-needed,-z,relro    -L/home/jenkins/workspace/swift/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux tools/swift-demangle/CMakeFiles/swift-demangle.dir/swift-demangle.cpp.o  -o bin/swift-demangle -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib  -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib -rdynamic -target -lx86_64-unknown-linux-gnu -isysroot -O2 -momit-leaf-frame-pointer -g0 -UNDEBUG -target -lx86_64-unknown-linux-gnu -isysroot lib/libswiftBasic.a /home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib/libLLVMSupport.a -lrt -ldl -lcurses -lpthread -lz -lm -luuid -lpthread -ldl -Wl,-rpath,/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib:/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib: && :
0  libLLVM.so.3.7  0x00007fa8616b96b0 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 64
1  libLLVM.so.3.7  0x00007fa8616b8491
2  libpthread.so.0 0x00007fa860f44d60
3  libc.so.6       0x00007fa860688e90
4  clang++         0x000000000087e00b clang::driver::Command::Command(clang::driver::Action const&, clang::driver::Tool const&, char const*, llvm::SmallVector<char const*, 16u> const&) + 123
5  clang++         0x00000000008c5ad5
6  clang++         0x000000000087ae22 clang::driver::Driver::BuildJobsForAction(clang::driver::Compilation&, clang::driver::Action const*, clang::driver::ToolChain const*, char const*, bool, bool, char const*, clang::driver::InputInfo&) const + 1954
7  clang++         0x000000000087b451 clang::driver::Driver::BuildJobs(clang::driver::Compilation&) const + 481
8  clang++         0x000000000087c75c clang::driver::Driver::BuildCompilation(llvm::ArrayRef<char const*>) + 3148
9  clang++         0x00000000006533c1 main + 4433
10 libc.so.6       0x00007fa860618610 __libc_start_main + 240
11 clang++         0x0000000000655279 _start + 41
Stack dump:
0.  Program arguments: /usr/bin/clang++ -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3 -Wl,-O1,--sort-common,--as-needed,-z,relro -L/home/jenkins/workspace/swift/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux tools/swift-demangle/CMakeFiles/swift-demangle.dir/swift-demangle.cpp.o -o bin/swift-demangle -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib -rdynamic -target -lx86_64-unknown-linux-gnu -isysroot -O2 -momit-leaf-frame-pointer -g0 -UNDEBUG -target -lx86_64-unknown-linux-gnu -isysroot lib/libswiftBasic.a /home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib/libLLVMSupport.a -lrt -ldl -lcurses -lpthread -lz -lm -luuid -lpthread -ldl -Wl,-rpath,/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib:/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib: 
1.  Compilation construction
2.  Building compilation jobs
3.  Building compilation jobs
/bin/sh: line 1:  8618 Segmentation fault      (core dumped) /usr/bin/clang++ -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -ffunction-sections -fdata-sections -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3 -Wl,-O1,--sort-common,--as-needed,-z,relro -L/home/jenkins/workspace/swift/src/build/buildbot_linux/swift-linux-x86_64/./lib/swift/linux tools/swift-demangle/CMakeFiles/swift-demangle.dir/swift-demangle.cpp.o -o bin/swift-demangle -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib -L/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib -rdynamic -target -lx86_64-unknown-linux-gnu -isysroot -O2 -momit-leaf-frame-pointer -g0 -UNDEBUG -target -lx86_64-unknown-linux-gnu -isysroot lib/libswiftBasic.a /home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib/libLLVMSupport.a -lrt -ldl -lcurses -lpthread -lz -lm -luuid -lpthread -ldl -Wl,-rpath,/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/lib:/home/jenkins/workspace/swift/src/build/buildbot_linux/llvm-linux-x86_64/./lib:
ninja: build stopped: subcommand failed.
/home/jenkins/workspace/swift/src/swift/utils/build-script: command terminated with a non-zero exit status 1, aborting
/home/jenkins/workspace/swift/src/swift/utils/build-script: using preset 'buildbot_linux_1510', which expands to /home/jenkins/workspace/swift/src/swift/utils/build-script --assertions --no-swift-stdlib-assertions --llbuild --swiftpm --xctest --build-subdir=buildbot_linux --lldb --release --test --validation-test --foundation -- --swift-enable-ast-verifier=0 --install-swift --install-lldb --install-llbuild --install-swiftpm --install-xctest --install-prefix=/usr '--swift-install-components=compiler;clang-builtin-headers;stdlib;sdk-overlay;license' --build-swift-static-stdlib=1 --skip-test-lldb=1 --test-installable-package=1 --install-destdir=/tmp/swift-install --installable-package=/tmp/swift.tar.gz --install-foundation --reconfigure
/home/jenkins/workspace/swift/src/swift/utils/build-script: command terminated with a non-zero exit status 1, aborting

If I go back to my original patch:

diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake
index c523924..a5accca 100644
--- a/cmake/modules/AddSwift.cmake
+++ b/cmake/modules/AddSwift.cmake
@@ -150,7 +150,7 @@ function(_add_variant_link_flags
       result)

   if("${sdk}" STREQUAL "LINUX")
-    list(APPEND result "-lpthread" "-ldl")
+    list(APPEND result "-pthread" "-lpthread" "-ldl")
   elseif("${sdk}" STREQUAL "FREEBSD")
     # No extra libraries required.
   else()
diff --git a/stdlib/public/core/CMakeLists.txt b/stdlib/public/core/CMakeLists.txt
index 5e44d5c..2de407a 100644
--- a/stdlib/public/core/CMakeLists.txt
+++ b/stdlib/public/core/CMakeLists.txt
@@ -162,6 +162,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
   find_package(BSD REQUIRED)
   list(APPEND swift_core_private_link_libraries
       ${BSD_LIBRARIES})
+  list(APPEND swift_core_private_link_libraries "-lpthread" "-ldl")
 endif()

 option(SWIFT_CHECK_ESSENTIAL_STDLIB

I don't get the segfault. 😞

@jinmingjian
Copy link

I have done a fresh build yesterday. I continue to get building done successfully without any patching...

It is best that to make cmake to find the right options by itself, if we really have to patch.

but again, do we really get the exact reason the bug back? or some mistake?

On Arch linux the linker was not seeing the `-lpthread` or `-ldl` linker
flags. This patch resolves it by specifically seperating link libraries
from linker flags returned by _add_variant_link_flags method. Then
applying the link libraries using CMake's target_link_libraries
function.
@RLovelett
Copy link
Contributor Author

Progress!

I tried running this branch on OS X. There is good news and bad news. The bad news is that this branch is now confirmed to not work on OS X too. The good news is the segmentation fault does not occur with the OS X version of clang++; instead it actually give an error message.

FAILED: : && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++   -fno-stack-protector -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wcovered-switch-default -Wnon-virtual-dtor -Werror=date-time -std=c++11 -fcolor-diagnostics -Wdocumentation -Wimplicit-fallthrough -Wunreachable-code -Woverloaded-virtual -O3 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/Users/ryan/Source/build/Ninja-ReleaseAssert/swift-macosx-x86_64/./lib/swift/macosx -Xlinker -rpath -Xlinker @executable_path/../lib/swift/macosx tools/swift-demangle/CMakeFiles/swift-demangle.dir/swift-demangle.cpp.o  -o bin/swift-demangle -L/Users/ryan/Source/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/lib  -L/Users/ryan/Source/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/./lib lib/libswiftBasic.a -target -lx86_64-apple-macosx10.9 -isysroot -arch -lx86_64 -F -mmacosx-version-min=10.9 -O2 -momit-leaf-frame-pointer -g0 -UNDEBUG -target -lx86_64-apple-macosx10.9 -isysroot -arch -lx86_64 -F -mmacosx-version-min=10.9 /Users/ryan/Source/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/lib/libLLVMSupport.a -lcurses -lpthread -lz -lm -lobjc -Wl,-rpath,/Users/ryan/Source/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/lib -Wl,-rpath,/Users/ryan/Source/build/Ninja-ReleaseAssert/llvm-macosx-x86_64/./lib && :
clang: warning: no such sysroot directory: '-arch'
ld: unknown/unsupported architecture name for: -arch
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Unfortunately, I do not know what that means or how to solve it. But now I at least have something I can search the internet for.

@RLovelett RLovelett closed this Jan 6, 2016
@gparker42
Copy link
Contributor

That looks like the build was supposed to pass -isysroot SOME_PATH but the configuration chose an empty value for SOME_PATH somewhere. I see that your Linux link command above passed -isysroot -O2 which also looks like a missing path for -isysroot.

@RLovelett
Copy link
Contributor Author

The impetus for this patch initially was the not finding pthreads and ld. The patch came second and was merely a means to an end.

I've continued to run this to ground to determine the root cause and I believe I've found the smoking gun.

I'll spare all the gory details and just cut to the chase. @jinmingjian was right there is no compilation issue on Arch. It's more precise to say there is a linker issue when building in a package build.

Basically if you the build inside of a PKGBUILD or makepkg environment you get this weird linker issue; if you run it outside you don't. I traced this back to the /etc/makepkg.conf file which includes a number of predefined environment variables to be used during the build. The one of note was the LDFLAGS definition which looks like LDFLAGS="-Wl,-01,--sort-common,--as-needed,-z,relro".

Once removing this definition the build succeeds as expected. Or conversely adding this to the environment causes it to fail. Perhaps this is a bug? (If it is I'm not sure if is in Arch, Swift or both)

Not sure what this means but I thought I'd continue to document this.

@ahti
Copy link
Contributor

ahti commented Jan 12, 2016

I've run into this issue as well. I talked to someone more knowledgeable about the Arch build system than me and might not relate this correctly, so take all this with a grain of salt:

The default LDFLAGS contain --as-needed which might cause libraries to not be linked if they were not used by any object or library that preceded the flag on the command line (for more, see man ld). Passing --as-needed seems to be the standard among Linux Distros (this article talks about this on Debian).

I think this might be the cause of the problem, and should probably be fixed in the Swift cmake files, although I don't know where and how, since I am not very familiar with cmake.

@gribozavr
Copy link
Collaborator

I would be happy to merge the fix, if someone can reliably reproduce the issue and test the fix.

ahti referenced this pull request in RLovelett/swift-aur Jan 12, 2016
freak4pc pushed a commit to freak4pc/swift that referenced this pull request Sep 28, 2022
Update hash for Base64CoderSwiftUI, remove xfail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants