From 213105c5bd617924e2b2cf95d7ea61c1397f5487 Mon Sep 17 00:00:00 2001 From: Martin Boehme Date: Tue, 16 Jun 2020 10:41:47 +0200 Subject: [PATCH 1/9] Fix two issues with the SwiftGlibc module map. The issues are: - Today, some submodules in `SwiftGlibc` fail to provide definitions that they should contain. As a consequence, Swift fails to import some code that compiles correctly with standalone Clang. As just one example, including `signal.h` should make the type `pid_t` available, but it currently does not. - `SwiftGlibc` is not compatible with the libc++ module map. Trying to include libc++ headers in a C++ module imported into Swift results in an error message about cyclic dependencies. This change fixes both of these issues by making it so that `SwiftGlibc` no actually longer defines a module map for the glibc headers but merely makes all of the symbols from those headers available in a module that can be imported into Swift. C / Objective-C / C++ code, on the other hand, will now include the glibc headers texually. For more context on the two issues and this fix, see this forum discussion: https://forums.swift.org/t/problems-with-swiftglibc-and-proposed-fix/37594 This change only modifies `glibc.modulemap.gyb` for the time being but leaves `bionic.modulemap.gyb` and `libc-openbsd.modulemap.gyb` unchanged. The intent is to fix these in the same way, but it will be easier to do this in separate PRs that can be tested individually. --- stdlib/public/Platform/CMakeLists.txt | 9 +- stdlib/public/Platform/SwiftGlibc.h.gyb | 108 ++++ stdlib/public/Platform/glibc.modulemap.gyb | 505 +----------------- test/Interop/C/stdlib/Inputs/include-signal.h | 3 + test/Interop/C/stdlib/Inputs/module.modulemap | 3 + test/Interop/C/stdlib/include-signal.swift | 3 + .../Cxx/stdlib/Inputs/include-string.h | 3 + .../Cxx/stdlib/Inputs/module.modulemap | 3 + test/Interop/Cxx/stdlib/include-string.swift | 4 + 9 files changed, 140 insertions(+), 501 deletions(-) create mode 100644 stdlib/public/Platform/SwiftGlibc.h.gyb create mode 100644 test/Interop/C/stdlib/Inputs/include-signal.h create mode 100644 test/Interop/C/stdlib/Inputs/module.modulemap create mode 100644 test/Interop/C/stdlib/include-signal.swift create mode 100644 test/Interop/Cxx/stdlib/Inputs/include-string.h create mode 100644 test/Interop/Cxx/stdlib/Inputs/module.modulemap create mode 100644 test/Interop/Cxx/stdlib/include-string.swift diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index c7e7bac10925d..7ffaa5122a022 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -119,6 +119,13 @@ foreach(sdk ${SWIFT_SDKS}) list(APPEND glibc_modulemap_target_list ${copy_glibc_modulemap_static}) endif() + set(glibc_header_out "${module_dir}/SwiftGlibc.h") + handle_gyb_source_single(glibc_header_target + SOURCE "SwiftGlibc.h.gyb" + OUTPUT "${glibc_header_out}" + FLAGS "-DCMAKE_SDK=${sdk}") + list(APPEND glibc_modulemap_target_list ${glibc_header_target}) + # If this SDK is a target for a non-native host, except if it's for Android # with its own native sysroot, create a native modulemap without a sysroot # prefix. This is the one we'll install instead. @@ -152,7 +159,7 @@ foreach(sdk ${SWIFT_SDKS}) COMPONENT sdk-overlay) if(SWIFT_BUILD_STATIC_STDLIB) - swift_install_in_component(FILES "${glibc_modulemap_out}" + swift_install_in_component(FILES "${glibc_header_out}" DESTINATION "lib/swift_static/${arch_subdir}" COMPONENT sdk-overlay) endif() diff --git a/stdlib/public/Platform/SwiftGlibc.h.gyb b/stdlib/public/Platform/SwiftGlibc.h.gyb new file mode 100644 index 0000000000000..c36225fc79069 --- /dev/null +++ b/stdlib/public/Platform/SwiftGlibc.h.gyb @@ -0,0 +1,108 @@ +%{ +headers = [ + # TODO: Do we really need these? + # 'stdc-predef.h', + # 'features.h', + + # C standard library + 'complex.h', + 'ctype.h', + 'errno.h', + 'fenv.h', + 'float.h', + 'inttypes.h', + 'iso646.h', + 'libutil.h', + 'limits.h', + 'locale.h', + 'math.h', + 'pty.h', + 'setjmp.h', + 'signal.h', + 'stdarg.h', + 'stdbool.h', + 'stddef.h', + 'stdint.h', + 'stdio.h', + 'stdlib.h', + 'string.h', + 'tgmath.h', + 'time.h', + 'utmp.h', + 'utmpx.h', + + # POSIX + 'aio.h', + 'arpa/inet.h', + 'bsd/ifaddrs.h', + 'bsd/pty.h', + 'cpio.h', + 'dirent.h', + 'dlfcn.h', + 'fcntl.h', + 'fmtmsg.h', + 'fnmatch.h', + 'ftw.h', + 'glob.h', + 'grp.h', + 'iconv.h', + 'ifaddrs.h', + 'langinfo.h', + 'libgen.h', + 'link.h', + 'monetary.h', + 'net/if.h', + 'netdb.h', + 'netinet/in.h', + 'netinet/tcp.h', + 'nl_types.h', + 'poll.h', + 'pthread.h', + 'pwd.h', + 'regex.h', + 'sched.h', + 'search.h', + 'semaphore.h', + 'spawn.h', + 'strings.h', + 'sys/event.h', + 'sys/file.h', + 'sys/inotify.h', + 'sys/ioctl.h', + 'sys/ipc.h', + 'sys/mman.h', + 'sys/msg.h', + 'sys/resource.h', + 'sys/select.h', + 'sys/sem.h', + 'sys/sendfile.h', + 'sys/shm.h', + 'sys/socket.h', + 'sys/stat.h', + 'sys/statvfs.h', + 'sys/time.h', + 'sys/times.h', + 'sys/types.h', + 'sys/uio.h', + 'sys/un.h', + 'sys/user.h', + 'sys/utsname.h', + 'sys/wait.h', + 'sysexits.h', + 'syslog.h', + 'tar.h', + 'termios.h', + 'ulimit.h', + 'unistd.h', + 'utime.h', + 'utmpx.h', + 'wait.h', + 'wordexp.h', +] +}% + +% for header in headers: +#if __has_include(<${header}>) +#include <${header}> +#endif +% end diff --git a/stdlib/public/Platform/glibc.modulemap.gyb b/stdlib/public/Platform/glibc.modulemap.gyb index 9a88a108c74c3..455dd111e2ce3 100644 --- a/stdlib/public/Platform/glibc.modulemap.gyb +++ b/stdlib/public/Platform/glibc.modulemap.gyb @@ -19,6 +19,9 @@ /// It's not named just Glibc so that it doesn't conflict in the event of a /// future official glibc modulemap. module SwiftGlibc [system] { +% if CMAKE_SDK == "LINUX": + link "m" +% end % if CMAKE_SDK in ["LINUX", "FREEBSD", "CYGWIN"]: link "pthread" // FIXME: util contains rarely used functions and not usually needed. Unfortunately @@ -36,506 +39,8 @@ module SwiftGlibc [system] { link "execinfo" % end - // C standard library - module C { -% if CMAKE_SDK in ["LINUX", "CYGWIN"]: - module features { -% if CMAKE_SDK == "LINUX": - header "${GLIBC_INCLUDE_PATH}/stdc-predef.h" -% end - header "${GLIBC_INCLUDE_PATH}/features.h" - export * - } -% end -% if CMAKE_SDK in ["LINUX", "FREEBSD", "CYGWIN", "HAIKU"]: - module complex { - header "${GLIBC_INCLUDE_PATH}/complex.h" - export * - } -% end -% if CMAKE_SDK in ["LINUX", "CYGWIN"]: - module pty { - header "${GLIBC_INCLUDE_PATH}/pty.h" - export * - } - module utmp { - header "${GLIBC_INCLUDE_PATH}/utmp.h" - export * - } -% end -% if CMAKE_SDK == "FREEBSD": - module pty { - header "${GLIBC_INCLUDE_PATH}/libutil.h" - export * - } - module utmp { - header "${GLIBC_INCLUDE_PATH}/utmpx.h" - export * - } -% end -% if CMAKE_SDK == "HAIKU": - module pty { - header "${GLIBC_INCLUDE_PATH}/../bsd/pty.h" - export * - } -% end - - module ctype { - header "${GLIBC_INCLUDE_PATH}/ctype.h" - export * - } - module errno { - header "${GLIBC_INCLUDE_PATH}/errno.h" - export * - } - - module fenv { - header "${GLIBC_INCLUDE_PATH}/fenv.h" - export * - } - - // note: supplied by compiler - // module float { - // header "${GLIBC_INCLUDE_PATH}/float.h" - // export * - // } - - module inttypes { - header "${GLIBC_INCLUDE_PATH}/inttypes.h" - export * - } - - // note: potentially supplied by compiler - // module iso646 { - // header "${GLIBC_INCLUDE_PATH}/iso646.h" - // export * - // } - // module limits { - // header "${GLIBC_INCLUDE_PATH}/limits.h" - // export * - // } - - module locale { - header "${GLIBC_INCLUDE_PATH}/locale.h" - export * - } - module math { -% if CMAKE_SDK == "LINUX": - link "m" -% end - header "${GLIBC_INCLUDE_PATH}/math.h" - export * - } -% if CMAKE_SDK != "WASI": - module setjmp { - header "${GLIBC_INCLUDE_PATH}/setjmp.h" - export * - } -% end - module signal { - header "${GLIBC_INCLUDE_PATH}/signal.h" - export * - } - - // note: supplied by the compiler - // module stdarg { - // header "${GLIBC_INCLUDE_PATH}/stdarg.h" - // export * - // } - // module stdbool { - // header "${GLIBC_INCLUDE_PATH}/stdbool.h" - // export * - // } - // module stddef { - // header "${GLIBC_INCLUDE_PATH}/stddef.h" - // export * - // } - // module stdint { - // header "${GLIBC_INCLUDE_PATH}/stdint.h" - // export * - // } - - module stdio { - header "${GLIBC_INCLUDE_PATH}/stdio.h" - export * - } - module stdlib { - header "${GLIBC_INCLUDE_PATH}/stdlib.h" - export * - export stddef - } - module string { - header "${GLIBC_INCLUDE_PATH}/string.h" - export * - } - - // note: supplied by the compiler - // explicit module tgmath { - // header "${GLIBC_INCLUDE_PATH}/tgmath.h" - // export * - // } - - module time { - header "${GLIBC_INCLUDE_PATH}/time.h" - export * - } - } - - // POSIX - module POSIX { -% if CMAKE_SDK in ["LINUX", "CYGWIN"]: - module wait { - header "${GLIBC_INCLUDE_PATH}/wait.h" - export * - } -% end - -% if CMAKE_SDK in ["LINUX", "FREEBSD"]: - module aio { - header "${GLIBC_INCLUDE_PATH}/aio.h" - export * - } - module cpio { - header "${GLIBC_INCLUDE_PATH}/cpio.h" - export * - } - module fmtmsg { - header "${GLIBC_INCLUDE_PATH}/fmtmsg.h" - export * - } - module nl_types { - header "${GLIBC_INCLUDE_PATH}/nl_types.h" - export * - } - module ulimit { - header "${GLIBC_INCLUDE_PATH}/ulimit.h" - export * - } -% end - -% if CMAKE_SDK in ["LINUX", "FREEBSD", "CYGWIN"]: - module ftw { - header "${GLIBC_INCLUDE_PATH}/ftw.h" - export * - } - module glob { - header "${GLIBC_INCLUDE_PATH}/glob.h" - export * - } - module iconv { - header "${GLIBC_INCLUDE_PATH}/iconv.h" - export * - } - module langinfo { - header "${GLIBC_INCLUDE_PATH}/langinfo.h" - export * - } - module monetary { - header "${GLIBC_INCLUDE_PATH}/monetary.h" - export * - } - module netdb { - header "${GLIBC_INCLUDE_PATH}/netdb.h" - export * - } - module ifaddrs { - header "${GLIBC_INCLUDE_PATH}/ifaddrs.h" - export * - } - module search { - header "${GLIBC_INCLUDE_PATH}/search.h" - export * - } - module spawn { - header "${GLIBC_INCLUDE_PATH}/spawn.h" - export * - } - module syslog { - header "${GLIBC_INCLUDE_PATH}/syslog.h" - export * - } - module tar { - header "${GLIBC_INCLUDE_PATH}/tar.h" - export * - } - module utmpx { - header "${GLIBC_INCLUDE_PATH}/utmpx.h" - export * - } - module wordexp { - header "${GLIBC_INCLUDE_PATH}/wordexp.h" - export * - } -% end - -% if CMAKE_SDK == "HAIKU": - module ftw { - header "${GLIBC_INCLUDE_PATH}/ftw.h" - export * - } - module glob { - header "${GLIBC_INCLUDE_PATH}/glob.h" - export * - } - module iconv { - header "${GLIBC_INCLUDE_PATH}/../iconv.h" - export * - } - module langinfo { - header "${GLIBC_INCLUDE_PATH}/langinfo.h" - export * - } - module monetary { - header "${GLIBC_INCLUDE_PATH}/monetary.h" - export * - } - module netdb { - header "${GLIBC_INCLUDE_PATH}/netdb.h" - export * - } - module ifaddrs { - header "${GLIBC_INCLUDE_PATH}/../bsd/ifaddrs.h" - export * - } - module search { - header "${GLIBC_INCLUDE_PATH}/search.h" - export * - } - module syslog { - header "${GLIBC_INCLUDE_PATH}/syslog.h" - export * - } - module tar { - header "${GLIBC_INCLUDE_PATH}/tar.h" - export * - } -% end - module arpa { - module inet { - header "${GLIBC_INCLUDE_PATH}/arpa/inet.h" - export * - } - export * - } - module dirent { - header "${GLIBC_INCLUDE_PATH}/dirent.h" - export * - } -% if CMAKE_SDK != "WASI": - module dl { - header "${GLIBC_INCLUDE_PATH}/link.h" - export * - } - module dlfcn { - header "${GLIBC_INCLUDE_PATH}/dlfcn.h" - export * - } -% end - module fcntl { - header "${GLIBC_INCLUDE_PATH}/fcntl.h" - export * - } - module fnmatch { - header "${GLIBC_INCLUDE_PATH}/fnmatch.h" - export * - } -% if CMAKE_SDK != "WASI": - module grp { - header "${GLIBC_INCLUDE_PATH}/grp.h" - export * - } -% end - module ioctl { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/ioctl.h" - export * - } - module libgen { - header "${GLIBC_INCLUDE_PATH}/libgen.h" - export * - } -% if CMAKE_SDK != "WASI": - module net { - module if { - header "${GLIBC_INCLUDE_PATH}/net/if.h" - export * - } - } -% end - module netinet { - module in { - header "${GLIBC_INCLUDE_PATH}/netinet/in.h" - export * - - exclude header "${GLIBC_INCLUDE_PATH}/netinet6/in6.h" - } - module tcp { - header "${GLIBC_INCLUDE_PATH}/netinet/tcp.h" - export * - } - } - module poll { - header "${GLIBC_INCLUDE_PATH}/poll.h" - export * - } -% if CMAKE_SDK != "WASI": - module pthread { - header "${GLIBC_INCLUDE_PATH}/pthread.h" - export * - } -% end - module pwd { - header "${GLIBC_INCLUDE_PATH}/pwd.h" - export * - } - module regex { - header "${GLIBC_INCLUDE_PATH}/regex.h" - export * - } - module sched { - header "${GLIBC_INCLUDE_PATH}/sched.h" - export * - } - module semaphore { - header "${GLIBC_INCLUDE_PATH}/semaphore.h" - export * - } - module strings { - header "${GLIBC_INCLUDE_PATH}/strings.h" - export * - } - - module sys { - export * - -% if CMAKE_SDK in ["LINUX", "FREEBSD", "CYGWIN", "HAIKU"]: - module file { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/file.h" - export * - } - module sem { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/sem.h" - export * - } -% if CMAKE_SDK != "HAIKU": - module shm { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/shm.h" - export * - } - module inotify { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/inotify.h" - export * - } -% end - module statvfs { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/statvfs.h" - export * - } -% end - -% if CMAKE_SDK != "WASI": - module ipc { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/ipc.h" - export * - } -% end - module mman { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/mman.h" - export * - } -% if CMAKE_SDK != "WASI": - module msg { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/msg.h" - export * - } -% end - module resource { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/resource.h" - export * - } - module select { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/select.h" - export * - } -% if CMAKE_SDK != "FREEBSD" and CMAKE_SDK != "HAIKU" and CMAKE_SDK != "WASI": - module sendfile { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/sendfile.h" - export * - } -% end - module socket { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/socket.h" - export * - } - module stat { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/stat.h" - export * - } - module time { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/time.h" - export * - } - module times { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/times.h" - export * - } - module types { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/types.h" - export * - } -% if CMAKE_SDK in ["FREEBSD"]: - module event { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/event.h" - export * - } -% end - module uio { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/uio.h" - export * - } - module un { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/un.h" - export * - } -% if CMAKE_SDK in ["LINUX"]: - module user { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/user.h" - export * - } -% end - module utsname { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/utsname.h" - export * - } -% if CMAKE_SDK != "WASI": - module wait { - header "${GLIBC_ARCH_INCLUDE_PATH}/sys/wait.h" - export * - } - } -% end -% if CMAKE_SDK in ["LINUX", "FREEBSD"]: - module sysexits { - header "${GLIBC_INCLUDE_PATH}/sysexits.h" - export * - } -% end -% if CMAKE_SDK != "WASI": - module termios { - header "${GLIBC_INCLUDE_PATH}/termios.h" - export * - } -% end - module unistd { - header "${GLIBC_INCLUDE_PATH}/unistd.h" - export * - } -% if CMAKE_SDK != "WASI": - module utime { - header "${GLIBC_INCLUDE_PATH}/utime.h" - export * - } - } -% end + header "SwiftGlibc.h" + export * } % if CMAKE_SDK != "WASI": diff --git a/test/Interop/C/stdlib/Inputs/include-signal.h b/test/Interop/C/stdlib/Inputs/include-signal.h new file mode 100644 index 0000000000000..7217db3cd4538 --- /dev/null +++ b/test/Interop/C/stdlib/Inputs/include-signal.h @@ -0,0 +1,3 @@ +#include + +pid_t returnPidT(); diff --git a/test/Interop/C/stdlib/Inputs/module.modulemap b/test/Interop/C/stdlib/Inputs/module.modulemap new file mode 100644 index 0000000000000..566cb2144e560 --- /dev/null +++ b/test/Interop/C/stdlib/Inputs/module.modulemap @@ -0,0 +1,3 @@ +module IncludeSignal { + header "include-signal.h" +} diff --git a/test/Interop/C/stdlib/include-signal.swift b/test/Interop/C/stdlib/include-signal.swift new file mode 100644 index 0000000000000..5c47541eba9f9 --- /dev/null +++ b/test/Interop/C/stdlib/include-signal.swift @@ -0,0 +1,3 @@ +// RUN: %target-typecheck-verify-swift -I %S/Inputs + +import IncludeSignal diff --git a/test/Interop/Cxx/stdlib/Inputs/include-string.h b/test/Interop/Cxx/stdlib/Inputs/include-string.h new file mode 100644 index 0000000000000..c1dcb3c31dbc4 --- /dev/null +++ b/test/Interop/Cxx/stdlib/Inputs/include-string.h @@ -0,0 +1,3 @@ +#include + +std::string returnString(); diff --git a/test/Interop/Cxx/stdlib/Inputs/module.modulemap b/test/Interop/Cxx/stdlib/Inputs/module.modulemap new file mode 100644 index 0000000000000..45900c7372e80 --- /dev/null +++ b/test/Interop/Cxx/stdlib/Inputs/module.modulemap @@ -0,0 +1,3 @@ +module IncludeString { + header "include-string.h" +} diff --git a/test/Interop/Cxx/stdlib/include-string.swift b/test/Interop/Cxx/stdlib/include-string.swift new file mode 100644 index 0000000000000..5298ad12ce62c --- /dev/null +++ b/test/Interop/Cxx/stdlib/include-string.swift @@ -0,0 +1,4 @@ +// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop +// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop -Xcc -stdlib=libc++ + +import IncludeString From 96ce908e964507aa709821386958ed8f3958af04 Mon Sep 17 00:00:00 2001 From: Martin Boehme Date: Mon, 22 Jun 2020 07:10:42 +0200 Subject: [PATCH 2/9] Remove test for `#include`ing . This fails on OS X with an error message saying that the header can't be found, and I haven't quite been able to figure out why. This error is obviously unrelated to this change, as OS X doesn't use `SwiftGlibc`. It would have been nice to demonstrate that including a libc++ header now works, but that isn't central to this change -- the important thing about this change is that everything still works and that, additionally, `pid_t` and similar types are now defined in the correct headers. --- test/Interop/Cxx/stdlib/Inputs/include-string.h | 3 --- test/Interop/Cxx/stdlib/Inputs/module.modulemap | 3 --- test/Interop/Cxx/stdlib/include-string.swift | 4 ---- 3 files changed, 10 deletions(-) delete mode 100644 test/Interop/Cxx/stdlib/Inputs/include-string.h delete mode 100644 test/Interop/Cxx/stdlib/Inputs/module.modulemap delete mode 100644 test/Interop/Cxx/stdlib/include-string.swift diff --git a/test/Interop/Cxx/stdlib/Inputs/include-string.h b/test/Interop/Cxx/stdlib/Inputs/include-string.h deleted file mode 100644 index c1dcb3c31dbc4..0000000000000 --- a/test/Interop/Cxx/stdlib/Inputs/include-string.h +++ /dev/null @@ -1,3 +0,0 @@ -#include - -std::string returnString(); diff --git a/test/Interop/Cxx/stdlib/Inputs/module.modulemap b/test/Interop/Cxx/stdlib/Inputs/module.modulemap deleted file mode 100644 index 45900c7372e80..0000000000000 --- a/test/Interop/Cxx/stdlib/Inputs/module.modulemap +++ /dev/null @@ -1,3 +0,0 @@ -module IncludeString { - header "include-string.h" -} diff --git a/test/Interop/Cxx/stdlib/include-string.swift b/test/Interop/Cxx/stdlib/include-string.swift deleted file mode 100644 index 5298ad12ce62c..0000000000000 --- a/test/Interop/Cxx/stdlib/include-string.swift +++ /dev/null @@ -1,4 +0,0 @@ -// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop -// RUN: %target-typecheck-verify-swift -I %S/Inputs -enable-cxx-interop -Xcc -stdlib=libc++ - -import IncludeString From 8042e3faf6aab57d86651b22e6c8b6c48cfd4feb Mon Sep 17 00:00:00 2001 From: zoecarver Date: Wed, 7 Oct 2020 10:41:50 -0700 Subject: [PATCH 3/9] [cxx-interop] Test importing Glibc with cxx-interop enabled. --- test/Interop/Cxx/stdlib/include-glibc.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 test/Interop/Cxx/stdlib/include-glibc.swift diff --git a/test/Interop/Cxx/stdlib/include-glibc.swift b/test/Interop/Cxx/stdlib/include-glibc.swift new file mode 100644 index 0000000000000..116dc88895783 --- /dev/null +++ b/test/Interop/Cxx/stdlib/include-glibc.swift @@ -0,0 +1,15 @@ +// RUN: %target-run-simple-swift(-Xfrontend -enable-cxx-interop) + +// REQUIRES: executable_test +// REQUIRES: OS=linux-gnu + +import Glibc +import StdlibUnittest + +var GlibcTests = TestSuite("GlibcTests") + +GlibcTests.test("abs") { + expectEqual(42, abs(-42)) +} + +runAllTests() From 0c5d925587cc103610dd8d674b9d936daf2b7f25 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Dec 2020 10:06:26 +0100 Subject: [PATCH 4/9] Rename stdlib -> libc --- test/Interop/Cxx/{stdlib => libc}/include-glibc.swift | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/Interop/Cxx/{stdlib => libc}/include-glibc.swift (100%) diff --git a/test/Interop/Cxx/stdlib/include-glibc.swift b/test/Interop/Cxx/libc/include-glibc.swift similarity index 100% rename from test/Interop/Cxx/stdlib/include-glibc.swift rename to test/Interop/Cxx/libc/include-glibc.swift From 3312a62033f98b741e5809d956fd7586becd8405 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Dec 2020 10:08:31 +0100 Subject: [PATCH 5/9] Do not modify behavior of stdc-predef.h/features.h in glibc module --- stdlib/public/Platform/SwiftGlibc.h.gyb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stdlib/public/Platform/SwiftGlibc.h.gyb b/stdlib/public/Platform/SwiftGlibc.h.gyb index c36225fc79069..3391eaee29934 100644 --- a/stdlib/public/Platform/SwiftGlibc.h.gyb +++ b/stdlib/public/Platform/SwiftGlibc.h.gyb @@ -1,8 +1,7 @@ %{ headers = [ - # TODO: Do we really need these? - # 'stdc-predef.h', - # 'features.h', + 'stdc-predef.h', + 'features.h', # C standard library 'complex.h', From 1be42658dfbac6b78d0232fbaec758a9de8ec90b Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Dec 2020 10:10:24 +0100 Subject: [PATCH 6/9] Rename stdlib -> libc --- test/Interop/C/{stdlib => libc}/Inputs/include-signal.h | 0 test/Interop/C/{stdlib => libc}/Inputs/module.modulemap | 0 test/Interop/C/{stdlib => libc}/include-signal.swift | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename test/Interop/C/{stdlib => libc}/Inputs/include-signal.h (100%) rename test/Interop/C/{stdlib => libc}/Inputs/module.modulemap (100%) rename test/Interop/C/{stdlib => libc}/include-signal.swift (100%) diff --git a/test/Interop/C/stdlib/Inputs/include-signal.h b/test/Interop/C/libc/Inputs/include-signal.h similarity index 100% rename from test/Interop/C/stdlib/Inputs/include-signal.h rename to test/Interop/C/libc/Inputs/include-signal.h diff --git a/test/Interop/C/stdlib/Inputs/module.modulemap b/test/Interop/C/libc/Inputs/module.modulemap similarity index 100% rename from test/Interop/C/stdlib/Inputs/module.modulemap rename to test/Interop/C/libc/Inputs/module.modulemap diff --git a/test/Interop/C/stdlib/include-signal.swift b/test/Interop/C/libc/include-signal.swift similarity index 100% rename from test/Interop/C/stdlib/include-signal.swift rename to test/Interop/C/libc/include-signal.swift From f66bae45285d2097d0a9e269594db3cfd96ee192 Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Wed, 2 Dec 2020 10:41:44 +0100 Subject: [PATCH 7/9] Add rationale to include-signal.swift --- test/Interop/C/libc/Inputs/include-signal.h | 5 ++++ test/Interop/C/libc/include-signal.swift | 30 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/test/Interop/C/libc/Inputs/include-signal.h b/test/Interop/C/libc/Inputs/include-signal.h index 7217db3cd4538..019db1a7f5097 100644 --- a/test/Interop/C/libc/Inputs/include-signal.h +++ b/test/Interop/C/libc/Inputs/include-signal.h @@ -1,3 +1,8 @@ +#ifndef TEST_INTEROP_C_LIBC_INPUTS_INCLUDE_SIGNAL_H +#define TEST_INTEROP_C_LIBC_INPUTS_INCLUDE_SIGNAL_H + #include pid_t returnPidT(); + +#endif // TEST_INTEROP_C_LIBC_INPUTS_INCLUDE_SIGNAL_H \ No newline at end of file diff --git a/test/Interop/C/libc/include-signal.swift b/test/Interop/C/libc/include-signal.swift index 5c47541eba9f9..18460b8efff91 100644 --- a/test/Interop/C/libc/include-signal.swift +++ b/test/Interop/C/libc/include-signal.swift @@ -1,3 +1,31 @@ // RUN: %target-typecheck-verify-swift -I %S/Inputs -import IncludeSignal +// Some types such as pid_t that glibc defines are defined using the following +// construct: +// +// #ifndef __pid_t_defined +// typedef __pid_t pid_t; +// #define __pid_t_defined +// #endif +// +// glibc defines pid_t in this way in multiple header files, as required by +// POSIX (e.g. signal.h, unistd.h, and sys/types.h). A number of other types +// use the same construct (e.g. ssize_t, intptr_t, uid_t, gid_t). +// +// Because Swift does not set the -fmodules-local-submodule-visibility flag, +// the __pid_t_defined macro can leak from the first submodule that defines it +// into all submodules that follow. +// +// As a consequence, a C header file may fail to compile when imported into +// Swift, even though it compiles cleanly outside Swift with the same system +// headers. +// +// This is a regression test for a bug that was once present in SwiftGlibc +// module layout. Originally SwiftGlibc defined one submodule per header, and +// types such as pid_t were defined by the first module that encountered the +// ifndef construct. +// +// See https://forums.swift.org/t/problems-with-swiftglibc-and-proposed-fix/37594 +// for further details. + +import IncludeSignal \ No newline at end of file From 3ff3ab746f9854be620ebebcf9fb8f63cd40e32c Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Thu, 3 Dec 2020 09:29:56 +0100 Subject: [PATCH 8/9] Only run include-signal test on linux --- test/Interop/C/libc/include-signal.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/Interop/C/libc/include-signal.swift b/test/Interop/C/libc/include-signal.swift index 18460b8efff91..3b420364f3ce8 100644 --- a/test/Interop/C/libc/include-signal.swift +++ b/test/Interop/C/libc/include-signal.swift @@ -28,4 +28,6 @@ // See https://forums.swift.org/t/problems-with-swiftglibc-and-proposed-fix/37594 // for further details. +// REQUIRES: OS=linux-gnu + import IncludeSignal \ No newline at end of file From cd8760d0f8467bf760ab5e00dd2eec8ebaeb101c Mon Sep 17 00:00:00 2001 From: Marcel Hlopko Date: Mon, 14 Dec 2020 13:19:41 +0100 Subject: [PATCH 9/9] Fix cmake script --- stdlib/public/Platform/CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/stdlib/public/Platform/CMakeLists.txt b/stdlib/public/Platform/CMakeLists.txt index 7ffaa5122a022..a7a60a063d9aa 100644 --- a/stdlib/public/Platform/CMakeLists.txt +++ b/stdlib/public/Platform/CMakeLists.txt @@ -157,8 +157,14 @@ foreach(sdk ${SWIFT_SDKS}) swift_install_in_component(FILES "${glibc_modulemap_out}" DESTINATION "lib/swift/${arch_subdir}" COMPONENT sdk-overlay) + swift_install_in_component(FILES "${glibc_header_out}" + DESTINATION "lib/swift/${arch_subdir}" + COMPONENT sdk-overlay) if(SWIFT_BUILD_STATIC_STDLIB) + swift_install_in_component(FILES "${glibc_modulemap_out}" + DESTINATION "lib/swift_static/${arch_subdir}" + COMPONENT sdk-overlay) swift_install_in_component(FILES "${glibc_header_out}" DESTINATION "lib/swift_static/${arch_subdir}" COMPONENT sdk-overlay)