rust-qt / ritual

Use C++ libraries from Rust
Apache License 2.0
1.22k stars 49 forks source link

-fPIC unsupported by libclang on x86_64-pc-windows-msvc #50

Closed Riateche closed 4 years ago

Riateche commented 7 years ago

From @o01eg on April 25, 2017 12:47

When I try to build Qt application on windows I get error:

   Compiling qt_core v0.1.3
     Running `rustc --crate-name build_script_build C:\Users\oleg_schelicalnov\.cargo\registry\src\github.com-1ecc6299db9ec823\qt_core-0.1.3\build.rs --crate-ty
pe bin --emit=dep-info,link -C debuginfo=2 -C metadata=923e9cfe594fc924 -C extra-filename=-923e9cfe594fc924 --out-dir D:\devel\qt002\target\debug\build\qt_core-
923e9cfe594fc924 -L dependency=D:\devel\qt002\target\debug\deps --extern qt_build_tools=D:\devel\qt002\target\debug\deps\libqt_build_tools-f3c031262549d90b.rlib
 --extern cpp_to_rust=D:\devel\qt002\target\debug\deps\libcpp_to_rust-6620496cc6f44dbc.rlib --cap-lints allow -L D:\devel\sqlite -L "C:\Program Files\LLVM\bin"
-L "C:\Program Files\LLVM\lib"`
     Running `D:\devel\qt002\target\debug\build\qt_core-923e9cfe594fc924\build-script-build`
error: failed to run custom build command for `qt_core v0.1.3`
process didn't exit successfully: `D:\devel\qt002\target\debug\build\qt_core-923e9cfe594fc924\build-script-build` (exit code: 1)
--- stdout
←[32mDetecting Qt directories...←[0m
←[32mExecuting command: "qmake" "-query" "QT_INSTALL_HEADERS"←[0m
←[32mQT_INSTALL_HEADERS = "D:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include"←[0m
←[32mExecuting command: "qmake" "-query" "QT_INSTALL_LIBS"←[0m
←[32mQT_INSTALL_LIBS = "D:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/lib"←[0m
←[32mReading input Cargo.toml←[0m
←[32mParsing C++ headers.←[0m
←[32mclang version 4.0.0 (tags/RELEASE_400/final)←[0m
←[32mInitializing clang...←[0m
←[32mclang arguments: ["-Xclang", "-detailed-preprocessing-record", "-fPIC", "-fcxx-exceptions", "-std=c++14", "-I", "D:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/includ
e", "-I", "D:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\\QtCore"]←[0m
←[35mDiagnostics:←[0m
←[35merror: unsupported option '-fPIC' for target 'x86_64-pc-windows-msvc'←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:107:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicro
soft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:163:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicro
soft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:304:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicro
soft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:1343:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicr
osoft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:1346:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicr
osoft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:1377:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicr
osoft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qnamespace.h:1594:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicr
osoft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qvariant.h:196:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicroso
ft-enum-value]←[0m
←[35mD:/devel/Qt/Qt5.8.0/5.8/msvc2015_64/include\QtCore/qtextcodec.h:89:9: warning: enumerator value is not representable in the underlying type 'int' [-Wmicros
oft-enum-value]←[0m
←[31mError:←[0m
←[31m   0: fatal clang error←[0m
←[31m   1: C++ parser failed←[0m

rust version:

rustc 1.16.0 (30cf806ef 2017-03-10)
binary: rustc
commit-hash: 30cf806ef8881c41821fbd43e5cf3699c5290c16
commit-date: 2017-03-10
host: x86_64-pc-windows-msvc
release: 1.16.0
LLVM version: 3.9

clang version:

clang version 4.0.0 (tags/RELEASE_400/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

_Copied from original issue: rust-qt/qtcore#2

Riateche commented 7 years ago

Original pull request to qt_build_tools old repo contained these changes to src/lib.rs:

     self.add_cpp_data_filter(Box::new(move |cpp_data| {
        fix_header_names::fix_header_names(cpp_data, &qt_this_lib_headers_dir)
      }));
 -    self.add_cpp_parser_flags(vec!["-fPIC", "-fcxx-exceptions"]);
 +    self.add_cpp_parser_flag("-fcxx-exceptions");
      if cpp_to_rust::utils::is_msvc() {
        self.add_cpp_parser_flag("-std=c++14");
      } else {
 -      self.add_cpp_parser_flag("-std=gnu++11");
 +      self.add_cpp_parser_flags(vec!["-fPIC", "-std=gnu++11"]);
        self.add_cpp_compiler_flags(vec!["-fPIC", "-std=gnu++11"]);
      }
      self.add_cpp_parser_blocked_name("qt_check_for_QGADGET_macro");
Riateche commented 7 years ago

Thanks for the pull request!

First of all, there have been some big architecture changes in cpp_to_rust. All code now lives in this repository, and all issues and pull requests should go there. I'm going to delete all outdated repositories when possible.

The main change related to this issue is that clang parser now only runs once when the crate is generated, and the result is used on all supported platforms without re-running the generator. So now it should be possible to run the parser on Linux and use the generated crate on Windows.

Nevertheless, the issue is still important because it should be possible to run the parser on Windows. My installation of clang and Appveyor's installation (see build log here, for example) clearly accept -fPIC argument. And IIRC it doesn't work without -fPIC, so I put it there deliberately. What is your version of clang and how was it built/installed? My best guess is that the behavior depends on the clang's target, but that would mean that Appveyor's clang doesn't use msvc target, which is already bad enough. We need to investigate it.

Copied from original pull request

o01eg commented 7 years ago

It looks like clang 3.8 accepted -fPIC flag.

o01eg commented 7 years ago

This code allowed to build compilable qt libraries on Linux with clang 3.9.1: cpp_to_rust/cpp_to_rust_generator/src/cpp_parser.rs

      TypeKind::Elaborated => {
          let canonical = type1.get_canonical_type();
          if canonical.get_kind() != TypeKind::Elaborated {
              self.parse_type(canonical, context_class, context_method)
          } else {
              Err(format!("Elaborated type: {:?}. Canonical: {:?}", type1, canonical).into())
          }
      }
      _ => Err(format!("Unsupported kind of type: {:?}. {:?}", type1.get_kind(), type1).into()),
o01eg commented 6 years ago

Same issue with -fPIC with clang 5.0.0. What if test clang version on windows or add command line option?

     Running `target\release\qt_generator.exe -c D:\devel\qt-cache -C 3 -l core
--debug-logging save -o D:\devel\qt-rust-test01\`
Preparing generator config for library: core
Executing command: "qmake" "-query" "QT_VERSION"
QT_VERSION = "5.7.1"
Detecting Qt directories
Executing command: "qmake" "-query" "QT_INSTALL_HEADERS"
QT_INSTALL_HEADERS = "D:/Qt/Qt5.7.1/5.7/msvc2015_64/include"
Executing command: "qmake" "-query" "QT_INSTALL_LIBS"
QT_INSTALL_LIBS = "D:/Qt/Qt5.7.1/5.7/msvc2015_64/lib"
Executing command: "qmake" "-query" "QT_INSTALL_DOCS"
QT_INSTALL_DOCS = "D:/Qt/Qt5.7.1/Docs/Qt-5.7"
Output Cargo.toml file will contain local paths of used dependencies (use --no-l
ocal-paths to disable).
Generating crate: qt_core
Running C++ parser
clang version 5.0.0 (tags/RELEASE_500/final)
Initializing clang...
Warning: CLANG_SYSTEM_INCLUDE_PATH environment variable is not set.
This may result in parse errors related to system header includes.
clang arguments: ["-Xclang", "-detailed-preprocessing-record", "-fPIC", "-fcxx-e
xceptions", "-std=c++14", "-I", "D:/Qt/Qt5.7.1/5.7/msvc2015_64/include", "-I", "
D:/Qt/Qt5.7.1/5.7/msvc2015_64/include\\QtCore"]
Error:
   0: fatal clang error:
error: unsupported option '-fPIC' for target 'x86_64-pc-windows-msvc'
   1: C++ parser failed
error: process didn't exit successfully: `target\release\qt_generator.exe -c D:\
devel\qt-cache -C 3 -l core --debug-logging save -o D:\devel\qt-rust-test01\` (e
xit code: 1)
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin
Riateche commented 6 years ago

It seems that support of -fPIC option depends on either clang version or its target (I'm not sure but I think you can have clang for different platforms on Windows, just like Rust). I can see three solutions here:

Riateche commented 4 years ago

Should be fixed now.