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

Not compiling with LTO #48

Closed
roland-5 opened this issue Nov 22, 2021 · 9 comments
Closed

Not compiling with LTO #48

roland-5 opened this issue Nov 22, 2021 · 9 comments

Comments

@roland-5
Copy link

Hi,

If I enable in makepkg in Arch, to build package with LTO, it throw this:

hck-0.7.0-1: parsing pkg list...
==> Making package: hck 0.7.0-1 (Mon 22 Nov 2021 09:45:12 AM CET)
==> Checking runtime dependencies...
==> Checking buildtime dependencies...
==> WARNING: Using existing $srcdir/ tree
==> Starting build()...
  Downloaded built v0.5.1
  Downloaded pin-project-internal v1.0.8
  Downloaded memmap2 v0.2.3
  Downloaded thiserror-impl v1.0.26
  Downloaded tinyvec v1.2.0
  Downloaded unicode-segmentation v1.7.1
  Downloaded syn v1.0.73
  Downloaded anyhow v1.0.41
  Downloaded thiserror v1.0.26
  Downloaded fnv v1.0.7
  Downloaded semver v1.0.3
  Downloaded flate2 v1.0.20
  Downloaded unicode-width v0.1.8
  Downloaded proc-macro2 v1.0.27
  Downloaded core_affinity v0.5.10
  Downloaded ripline v0.1.0
  Downloaded spin v0.9.2
  Downloaded futures-sink v0.3.16
  Downloaded jobserver v0.1.22
  Downloaded cc v1.0.68
  Downloaded nanorand v0.6.1
  Downloaded libdeflater v0.7.3
  Downloaded flume v0.10.9
  Downloaded bytecount v0.6.2
  Downloaded libdeflate-sys v0.7.3
  Downloaded futures-core v0.3.16
  Downloaded cmake v0.1.45
  Downloaded pkg-config v0.3.19
  Downloaded serde_derive v1.0.126
  Downloaded quote v1.0.9
  Downloaded memchr v2.4.0
  Downloaded globset v0.4.8
  Downloaded pin-project v1.0.8
  Downloaded grep-cli v0.1.6
  Downloaded matches v0.1.8
  Downloaded git2 v0.13.20
  Downloaded serde v1.0.126
  Downloaded gzp v0.9.2
  Downloaded libgit2-sys v0.12.21+1.1.0
  Downloaded cargo-lock v7.0.0
  Downloaded bstr v0.2.16
  Downloaded structopt v0.3.25
  Downloaded libc v0.2.97
  Downloaded structopt-derive v0.4.18
  Downloaded 44 crates (6.1 MB) in 0.62s (largest was `gzp` at 2.0 MB)
   Compiling libc v0.2.97
   Compiling proc-macro2 v1.0.27
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.73
   Compiling pkg-config v0.3.19
   Compiling log v0.4.14
   Compiling cfg-if v1.0.0
   Compiling bitflags v1.2.1
   Compiling matches v0.1.8
   Compiling serde_derive v1.0.126
   Compiling tinyvec_macros v0.1.0
   Compiling memchr v2.4.0
   Compiling serde v1.0.126
   Compiling version_check v0.9.3
   Compiling percent-encoding v2.1.0
   Compiling semver v1.0.3
   Compiling lazy_static v1.4.0
   Compiling scopeguard v1.1.0
   Compiling futures-core v0.3.16
   Compiling regex-automata v0.1.10
   Compiling regex-syntax v0.6.25
   Compiling crc32fast v1.2.1
   Compiling unicode-segmentation v1.7.1
   Compiling unicode-width v0.1.8
   Compiling anyhow v1.0.41
   Compiling fnv v1.0.7
   Compiling ansi_term v0.11.0
   Compiling futures-sink v0.3.16
   Compiling termcolor v1.1.2
   Compiling strsim v0.8.0
   Compiling vec_map v0.8.2
   Compiling humantime v2.1.0
   Compiling byteorder v1.4.3
   Compiling same-file v1.0.6
   Compiling bytecount v0.6.2
   Compiling bytes v1.1.0
   Compiling tinyvec v1.2.0
   Compiling unicode-bidi v0.3.5
   Compiling form_urlencoded v1.0.1
   Compiling lock_api v0.4.5
   Compiling textwrap v0.11.0
   Compiling heck v0.3.3
   Compiling spin v0.9.2
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling aho-corasick v0.7.18
   Compiling bstr v0.2.16
   Compiling unicode-normalization v0.1.19
   Compiling quote v1.0.9
   Compiling jobserver v0.1.22
   Compiling atty v0.2.14
   Compiling getrandom v0.2.3
   Compiling num_cpus v1.13.0
   Compiling memmap2 v0.2.3
   Compiling cc v1.0.68
   Compiling clap v2.33.3
   Compiling core_affinity v0.5.10
   Compiling nanorand v0.6.1
   Compiling ripline v0.1.0
   Compiling regex v1.5.4
   Compiling cmake v0.1.45
   Compiling idna v0.2.3
   Compiling url v2.2.2
   Compiling globset v0.4.8
   Compiling env_logger v0.9.0
   Compiling grep-cli v0.1.6
   Compiling libz-sys v1.1.3
   Compiling libgit2-sys v0.12.21+1.1.0
   Compiling libdeflate-sys v0.7.3
   Compiling libdeflater v0.7.3
   Compiling pin-project-internal v1.0.8
   Compiling thiserror-impl v1.0.26
   Compiling structopt-derive v0.4.18
   Compiling flate2 v1.0.20
   Compiling git2 v0.13.20
   Compiling thiserror v1.0.26
   Compiling structopt v0.3.25
   Compiling pin-project v1.0.8
   Compiling flume v0.10.9
   Compiling gzp v0.9.2
   Compiling toml v0.5.8
   Compiling cargo-lock v7.0.0
   Compiling built v0.5.1
   Compiling hck v0.7.0 (/home/roland/.cache/paru/clone/hck/src/hck-0.7.0)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/home/roland/.cache/paru/clone/hck/src/hck-0.7.0/target/release/deps/hck-2fb13a3aa70533e1.hck.3cad90ea-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/home/roland/.cache/paru/clone/hck/src/hck-0.7.0/target/release/deps" "-L" "/home/roland/.cache/paru/clone/hck/src/hck-0.7.0/target/release/build/libz-sys-f72b9bca0fe9a64d/out/lib" "-L" "/home/roland/.cache/paru/clone/hck/src/hck-0.7.0/target/release/build/libdeflate-sys-8a0eed30405bcf8f/out/lib" "-L" "/home/roland/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/tmp/rustcQmlgX7/liblibdeflate_sys-85dff7a5270dc40e.rlib" "/tmp/rustcQmlgX7/liblibz_sys-dbc238c55867ed1d.rlib" "-Wl,--start-group" "-Wl,--end-group" "/home/roland/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-02203e01b7df4fdd.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/roland/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/roland/.cache/paru/clone/hck/src/hck-0.7.0/target/release/deps/hck-2fb13a3aa70533e1" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs"
  = note: /usr/bin/ld: /tmp/rustcQmlgX7/liblibdeflate_sys-85dff7a5270dc40e.rlib: error adding symbols: file format not recognized
          collect2: error: ld returned 1 exit status


error: could not compile `hck` due to previous error
==> ERROR: A failure occurred in build().
    Aborting...
error: failed to build 'hck-0.7.0-1':
@sstadick
Copy link
Owner

Hi! Thanks for making an issue. Could you include the architecture of the machine your building on as well as the makepkg command?

@sstadick
Copy link
Owner

Seems related to this: rust-lang/cc-rs#354 (comment)

@roland-5
Copy link
Author

roland-5 commented Nov 22, 2021

Oh, I'm so sorry, my bad. Yes, this is my configuration:

OS: Arch Linux x86_64
Kernel: 5.15.4-arch1-1
Shell: fish 3.3.1
CPU: AMD Ryzen 7 4800H with Radeon Graphics (16) @ 2.900GHz
Memory: 5476MiB / 31468MiB

My /etc/makepkg:

#!/hint/bash
#
# /etc/makepkg.conf
#

#########################################################################
# SOURCE ACQUISITION
#########################################################################
#
#-- The download utilities that makepkg should use to acquire sources
#  Format: 'protocol::agent'
DLAGENTS=('file::/usr/bin/curl -qgC - -o %o %u'
          'ftp::/usr/bin/curl -qgfC - --ftp-pasv --retry 3 --retry-delay 3 -o %o %u'
          'http::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
          'https::/usr/bin/curl -qgb "" -fLC - --retry 3 --retry-delay 3 -o %o %u'
          'rsync::/usr/bin/rsync --no-motd -z %u %o'
          'scp::/usr/bin/scp -C %u %o')

# Other common tools:
# /usr/bin/snarf
# /usr/bin/lftpget -c
# /usr/bin/wget

#-- The package required by makepkg to download VCS sources
#  Format: 'protocol::package'
VCSCLIENTS=('bzr::bzr'
            'fossil::fossil'
            'git::git'
            'hg::mercurial'
            'svn::subversion')

#########################################################################
# ARCHITECTURE, COMPILE FLAGS
#########################################################################
#
CARCH="x86_64"
CHOST="x86_64-pc-linux-gnu"

#-- Compiler and Linker Flags
export CC=clang
export CXX=clang++

#CPPFLAGS=""
CFLAGS="-march=native -mtune=native -O3 -pipe -fno-plt -fexceptions \
        -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
        -fstack-clash-protection -fcf-protection"
CXXFLAGS="$CFLAGS -Wp,-D_GLIBCXX_ASSERTIONS"
LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now"
RUSTFLAGS="-C opt-level=3 -C target-cpu=native"
#-- Make Flags: change this for DistCC/SMP systems
MAKEFLAGS="-j$(nproc)"
#-- Debugging flags
DEBUG_CFLAGS="-g -fvar-tracking-assignments"
DEBUG_CXXFLAGS="-g -fvar-tracking-assignments"
#DEBUG_RUSTFLAGS="-C debuginfo=2"

#########################################################################
# BUILD ENVIRONMENT
#########################################################################
#
# Makepkg defaults: BUILDENV=(!distcc !color !ccache check !sign)
#  A negated environment option will do the opposite of the comments below.
#
#-- distcc:   Use the Distributed C/C++/ObjC compiler
#-- color:    Colorize output messages
#-- ccache:   Use ccache to cache compilation
#-- check:    Run the check() function if present in the PKGBUILD
#-- sign:     Generate PGP signature file
#
BUILDENV=(!distcc color ccache check !sign)
#
#-- If using DistCC, your MAKEFLAGS will also need modification. In addition,
#-- specify a space-delimited list of hosts running in the DistCC cluster.
#DISTCC_HOSTS=""
#
#-- Specify a directory for package building.
#BUILDDIR=/tmp/makepkg

#########################################################################
# GLOBAL PACKAGE OPTIONS
#   These are default values for the options=() settings
#########################################################################
#
# Makepkg defaults: OPTIONS=(!strip docs libtool staticlibs emptydirs !zipman !purge !debug !lto)
#  A negated option will do the opposite of the comments below.
#
#-- strip:      Strip symbols from binaries/libraries
#-- docs:       Save doc directories specified by DOC_DIRS
#-- libtool:    Leave libtool (.la) files in packages
#-- staticlibs: Leave static library (.a) files in packages
#-- emptydirs:  Leave empty directories in packages
#-- zipman:     Compress manual (man and info) pages in MAN_DIRS with gzip
#-- purge:      Remove files specified by PURGE_TARGETS
#-- debug:      Add debugging flags as specified in DEBUG_* variables
#-- lto:        Add compile flags for building with link time optimization
#
OPTIONS=(strip docs !libtool !staticlibs emptydirs zipman purge !debug lto)

#-- File integrity checks to use. Valid: md5, sha1, sha224, sha256, sha384, sha512, b2
INTEGRITY_CHECK=(sha256)
#-- Options to be used when stripping binaries. See `man strip' for details.
STRIP_BINARIES="--strip-all"
#-- Options to be used when stripping shared libraries. See `man strip' for details.
STRIP_SHARED="--strip-unneeded"
#-- Options to be used when stripping static libraries. See `man strip' for details.
STRIP_STATIC="--strip-debug"
#-- Manual (man and info) directories to compress (if zipman is specified)
MAN_DIRS=({usr{,/local}{,/share},opt/*}/{man,info})
#-- Doc directories to remove (if !docs is specified)
DOC_DIRS=(usr/{,local/}{,share/}{doc,gtk-doc} opt/*/{doc,gtk-doc})
#-- Files to be removed from all packages (if purge is specified)
PURGE_TARGETS=(usr/{,share}/info/dir .packlist *.pod)
#-- Directory to store source code in for debug packages
DBGSRCDIR="/usr/src/debug"

#########################################################################
# PACKAGE OUTPUT
#########################################################################
#
# Default: put built package and cached source in build directory
#
#-- Destination: specify a fixed directory where all packages will be placed
#PKGDEST=/home/packages
#-- Source cache: specify a fixed directory where source files will be cached
#SRCDEST=/home/sources
#-- Source packages: specify a fixed directory where all src packages will be placed
#SRCPKGDEST=/home/srcpackages
#-- Log files: specify a fixed directory where all log files will be placed
#LOGDEST=/home/makepkglogs
#-- Packager: name/email of the person or organization building packages
#PACKAGER="John Doe <[email protected]>"
#-- Specify a key to use for package signing
#GPGKEY=""

#########################################################################
# COMPRESSION DEFAULTS
#########################################################################
#
COMPRESSGZ=(gzip -c -f -n)
COMPRESSBZ2=(bzip2 -c -f)
COMPRESSXZ=(xz -c -z -)
COMPRESSZST=(zstd -c -z -q --threads=0 -)
COMPRESSLRZ=(lrzip -q)
COMPRESSLZO=(lzop -q)
COMPRESSZ=(compress -c -f)
COMPRESSLZ4=(lz4 -q)
COMPRESSLZ=(lzip -c -f)

#########################################################################
# EXTENSION DEFAULTS
#########################################################################
#
PKGEXT='.pkg.tar.zst'
SRCEXT='.src.tar.gz'

#########################################################################
# OTHER
#########################################################################
#
#-- Command used to run pacman as root, instead of trying sudo and su
#PACMAN_AUTH=()

rustup -V

rustup 1.24.3 (2021-11-09)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.56.1 (59eed8a2a 2021-11-01)`

Command to install hck from AUR I use

paru -S hck

@sstadick
Copy link
Owner

Unrelated, but https://aur.archlinux.org/packages/hck/ is out of date at the moment and I am not the maintainer of that.

Does cloning the repo and runing cargo build --release work or provide a different error message?

This and this seem to indicate that the issue is linker related (which the error message confirms!). Could you try using the lld linker instead? / indicate which linker is being used by your build?

Total shot in the dark, but it might help to update your version of rust as well with rustup update stable.

@sstadick
Copy link
Owner

Also, could you install and run the following (or any other way of getting the same info):

cargo install default-target
default-target

to show what target rust thinks it's compiling for.

@roland-5
Copy link
Author

roland-5 commented Nov 22, 2021

Unrelated, but https://aur.archlinux.org/packages/hck/ is out of date at the moment and I am not the maintainer of that.

Isn't 0.7.0 latest version?

Does cloning the repo and runing cargo build --release work or provide a different error message?

It build it without problem. If I disable lto in /etc/make.pkg, AUR hck package is building too, without problems.

This and this seem to indicate that the issue is linker related (which the error message confirms!). Could you try using the lld linker instead? / indicate which linker is being used by your build?

How can I do this?

Total shot in the dark, but it might help to update your version of rust as well with rustup update stable.

I do rustup update everyday. :P

Also, could you install and run the following (or any other way of getting the same info):

cargo install default-target
default-target

to show what target rust thinks it's compiling for.

roland@blackrainbow hck on  master is  v0.7.1-alpha.0 via  v1.56.1 ❯ cargo install default-target                                                                                                           00:18
    Updating crates.io index
  Downloaded default-target v0.1.0
  Downloaded 1 crate (3.3 KB) in 0.98s
  Installing default-target v0.1.0
   Compiling default-target v0.1.0
    Finished release [optimized] target(s) in 3.07s
  Installing /home/roland/.cargo/bin/default-target
   Installed package `default-target v0.1.0` (executable `default-target`)
roland@blackrainbow hck on  master is  v0.7.1-alpha.0 via  v1.56.1 ❯ default-target                                                                                                                      3s/00:20
x86_64-unknown-linux-gnu
roland@blackrainbow hck on  master is  v0.7.1-alpha.0 via  v1.56.1 ❯ rustup update stable                                                                                                                   00:21
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'

  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.56.1 (59eed8a2a 2021-11-01)

@sstadick
Copy link
Owner

  • Yep, that's embarrassing, 0.7.0 is latest!
  • Just to be clear, when you build from source after cloning the repo, lto is "fat" in the Cargo.toml file?
    • If so this is a bit weirder
  • To swap linkers you can do either of the following:
# Get these env vars into your AUR config
CARGO_PROFILE_RELEASE_LTO=true
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
RUSTFLAGS="-Clinker-plugin-lto -Clink-arg=-flto -Clink-arg=-fuse-ld=lld"

source

Or, maybe, in a ~/.cargo/config.toml

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]

From what I'm gathering in various other tickets and threads, the default linker on Arch doesn't mix well with Rust projects and LTO when there are C deps in the Rust project.

The last option here would be for me to add feature flags so that libdeflater can be opt-opt and a pure rust backend could be used instead.

@roland-5
Copy link
Author

  • Yep, that's embarrassing, 0.7.0 is latest!

    • Just to be clear, when you build from source after cloning the repo, lto is "fat" in the Cargo.toml file?

      • If so this is a bit weirder

Yeah, I saw yesterday lto = "fat" in Cargo.toml in cloned hck repo, and was curious why it builds here, but not from AUR.

* To swap linkers you can do either of the following:
# Get these env vars into your AUR config
CARGO_PROFILE_RELEASE_LTO=true
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=clang
RUSTFLAGS="-Clinker-plugin-lto -Clink-arg=-flto -Clink-arg=-fuse-ld=lld"

source

Or, maybe, in a ~/.cargo/config.toml

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=lld"]

From what I'm gathering in various other tickets and threads, the default linker on Arch doesn't mix well with Rust projects and LTO when there are C deps in the Rust project.

The last option here would be for me to add feature flags so that libdeflater can be opt-opt and a pure rust backend could be used instead.

I tried both options, and they work without problem! Oh, my god. I love you! I can finally die with peace in my heart.
Sorry for all this trouble, but I tried many things, and they didn't work earlier.
I even tried to fuse my RUSTFLAG with yours, and

RUSTFLAGS="-Copt-level=3 -Ctarget-cpu=native -Clinker-plugin-lto -Clink-arg=-flto -Clink-arg=-fuse-ld=lld"

works without problem too!
Thank you so much.

@sstadick
Copy link
Owner

Woohoo! That's awesome and good to hear. Thanks for making a ticket so that others will find this eventually as well 👍

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

No branches or pull requests

2 participants