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

[SR-5524] Importing a modulemapped C library in REPL throws an error #4626

Open
swift-ci opened this issue Jul 21, 2017 · 3 comments
Open

[SR-5524] Importing a modulemapped C library in REPL throws an error #4626

swift-ci opened this issue Jul 21, 2017 · 3 comments
Labels

Comments

@swift-ci
Copy link

Previous ID SR-5524
Radar None
Original Reporter MatiMax (JIRA User)
Type Bug

Attachment: Download

Environment

Ubuntu Linux 16.04 LTS, Swift 3.1.1, Swift 4.0.0-dev

Additional Detail from JIRA
Votes 4
Component/s LLDB for Swift
Labels Bug, Linux, REPL
Assignee None
Priority Medium

md5: 4ca55ff9257afe4db0384f0c0979d411

relates to:

  • SR-3648 Unable to Import Foundation
  • SR-2783 REPL crashes due to bad permissions CoreFoundation header files (Ubuntu)
  • SR-3794 import Glibc does not work from the swift 3.1-dev REPL
  • SR-7065 REPL broken on Linux (cannot import anything) as of 2018-02-22 Swift 4.1 snapshot

Issue Description:

A Swift package of type system-module throws an error when trying to import in REPL.

Swift 3.1.1

swift -I ./Clibssh -L /usr/lib/x86_64-linux-gnu
Welcome to Swift version 3.1.1 (swift-3.1.1-RELEASE). Type :help for assistance.
  1> import Clibssh 
error: repl.swift:1:8: error: could not build Objective-C module 'Clibssh'
import Clibssh
       ^

Swift 4.0.0-dev

swift -I ./Clibssh -L /usr/lib/x86_64-linux-gnu
Welcome to Swift version 4.0-dev (LLVM a15decabe3, Clang ae62debbb4, Swift 823d7e27bf). Type :help for assistance.
  1> import Clibssh 
error: repl.swift:1:8: error: could not build C module 'Clibssh'
import Clibssh
       ^

Module definition for Clibssh

The following sources were used to produce the system module for using libssh.so in Swift.

Package definition for the module
import PackageDescription

let package = Package(
    name: "Clibssh",
    pkgConfig: "libssh"
)
Module map definition
module Clibssh [system] {
  header "/usr/include/libssh/libssh.h"
  link "ssh"
  export *
}

Note 1: Importing the module in Swift 3.0.1 REPL works fine.
Note 2: Building an executable file with this module definition works flawlessly in Swift 3.1.1 and Swift 4.0.0

I'm linking this issue to SR-3794, SR-3648 and SR-2783 as it seems there are some similarities.

@swift-ci
Copy link
Author

Comment by Matthias M. Schneider (JIRA)

Temporary fixes

Missing read permissions for include files

Check the permissions of all include files in your Swift installation directory. They must have the read-flag set for group and other access. You can set the permissions using this command:

sudo find /opt/swift/ -name "*.h" ! -perm -go=r -exec chmod go+r "{}" \;

where /opt/swift/ might be your Swift installation directory.

Location of header files

Using ltrace I could track down the problem to locating missing header files which reside in /opt/swift/usr/lib/swift/clang/include/. This can be fixed in two ways.

Extending the command line

Calling Swift REPL with the following command line options will actually work for versions 3.1.1 and 4.0.0:

swift -I /opt/swift/usr/lib/swift/clang/include/

Adding the include path permanently

To add the required include files permanently without the need to add the search path to the swift command line you should declare two global exported variables in your environment:

  • C_INCLUDE_PATH

  • CPLUS_INCLUDE_PATH

You can add the following lines to your /etc/environment file:

export C_INCLUDE_PATH=/opt/swift/usr/lib/swift/clang/include/
export CPLUS_INCLUDE_PATH=$C_INCLUDE_PATH

@norio-nomura
Copy link

That workaround works only on REPL an lldb.
Setting C_INCLUDE_PATH and CPLUS_INCLUDE_PATH causes error: redefinition of module '_Builtin_intrinsics' on building Package that includes Clang modules.
e.g.

➜  11:40:12 git:(nn-SE-0166) docker run --privileged -it -v `pwd`:`pwd` -w `pwd` --name sourcekit --rm norionomura/swift:swift-4.0-branch
root@74b1b10a6bcb:/Users/norio/github/swift-dev/SourceKitten/Carthage/Checkouts/Yams# swift test
Compile CYaml src/writer.c
/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/module.modulemap:1:8: error: redefinition of module '_Builtin_intrinsics'
module _Builtin_intrinsics [system] [extern_c] {
       ^
/usr/lib/swift/clang/include/module.modulemap:24:8: note: previously defined here
module _Builtin_intrinsics [system] [extern_c] {
       ^
/usr/lib/llvm-3.8/bin/../lib/clang/3.8.0/include/module.modulemap:169:8: error: redefinition of module '_Builtin_stddef_max_align_t'
module _Builtin_stddef_max_align_t [system] [extern_c] {
       ^
/usr/lib/swift/clang/include/module.modulemap:159:8: note: previously defined here
module _Builtin_stddef_max_align_t [system] [extern_c] {
       ^
2 errors generated.
error: terminated(1): /usr/bin/swift-build-tool -f /Users/norio/github/swift-dev/SourceKitten/Carthage/Checkouts/Yams/.build/debug.yaml test

@swift-ci
Copy link
Author

swift-ci commented Aug 2, 2017

Comment by Matthias M. Schneider (JIRA)

@norio-nomura: Thanks for the feedback. In this case of symbol redefinitions in module maps one could suggest that you specify the -fno-implicit-modules parameter, and then include the relevant module maps with the -fmodule-file=<file> parameter which might be very cumbersome for a large number of module maps needed for the build.

Again, this is more like fighting the symptoms with YAW (yet another workaround 😉 ) than a real solution.

For more information on using module maps with clang, please see clang module map command line parameters.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants