ldc-developers / ldc2.snap

Snap package definition for LDC, the LLVM-based D compiler
11 stars 4 forks source link

Add -Wa,-mrelax-relocations=no to CMAKE_C_FLAGS_RELEASE for LDC build #20

Closed WebDrake closed 7 years ago

WebDrake commented 7 years ago

This should address the finnicky binutils issues observed when the snap is installed on Ubuntu 14.04. See: https://bugs.launchpad.net/snapcraft/+bug/1668424

Fixes ldc-developers/ldc2.snap#17.

WebDrake commented 7 years ago

This fixes 14.04's zlib issue but runs into -fPIC issues with 16.10. This suggests that the specified C_FLAGS are overriding some distro default :-\

dnadlinger commented 7 years ago

Do you mean when building on 16.10 or when using the 16.04-built package there? On 16.10, they IIRC patched in the equivalent of-fPIC by default, so when building there it shouldn't matter. And we simply use CMake's POSITION_INDEPENDENT_CODE target property now, so that shouldn't be affected by setting CMAKE_C_FLAGS either.

WebDrake commented 7 years ago

Well, this is the result I get when trying to build any program on 16.10 with a snap package built from the present commit:

$ ldc2.ldmd2 -run hello.d
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libdruntime-ldc.a(errno.c.o): relocation R_X86_64_PC32 against symbol `__errno_location@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
WebDrake commented 7 years ago

With another program that uses more of phobos, the list gets longer:

/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(zutil.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(inftrees.c.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(inffast.c.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(deflate.c.o): relocation R_X86_64_32S against symbol `zcalloc' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(crc32.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(inflate.c.o): relocation R_X86_64_32S against symbol `zcalloc' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(compress.c.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /snap/ldc2/x1/bin/../lib/libphobos2-ldc.a(trees.c.o): relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1
dnadlinger commented 7 years ago

(Updated comment – I had mistakenly sent it too early, sorry.)

WebDrake commented 7 years ago

Do you mean when building on 16.10 or when using the 16.04-built package there?

When using the 16.04-built package there. Nothing that I'm aware has changed in the snapcraft cleanbuild setup, and that was working fine with the previous package build.

And we simply use CMake's POSITION_INDEPENDENT_CODE target property now, so that shouldn't be affected by setting CMAKE_C_FLAGS either.

Right. This is why I found the results a little odd. Is it possible that setting CMAKE_C_FLAGS just overrides whatever C flags get implicitly defined by using POSITION_INDEPENDENT_CODE ... ?

dnadlinger commented 7 years ago

Weird – you could always just try patching in set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wa,…") to see whether that makes a difference, or look at the verbose make output to see how gcc is invoked.

WebDrake commented 7 years ago

Thanks. I'll look into this over the weekend and see what I can come up with ...

kinke commented 7 years ago

My CMake defaults to CMAKE_C_FLAGS= /DWIN32 /D_WINDOWS /W3 when targeting MSVC. POSITION_INDEPENDENT_CODE shouldn't affect it or be affected by it, but seems to be supported since CMake 2.8.9 only.

WebDrake commented 7 years ago

I followed up with notes on the original issue https://github.com/ldc-developers/ldc2.snap/issues/17, but the TL;DR seems to be, whatever I experienced with 'normal' builds of LDC on my machine, the snapcraft build appears to result in something different: the C flags are completely overwritten by whatever is provided by the -DCMAKE_C_FLAGS option. So I guess the best way forward is just to make sure that all C flags required are set in the snapcraft.yaml file.

WebDrake commented 7 years ago

Updated the patch:

diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index 20d04d7..6f6199d 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -28,9 +28,10 @@ parts:
     configflags:
     - -DD_COMPILER=../../ldc-bootstrap/build/bin/ldmd2
     - -DCMAKE_BUILD_TYPE=Release
-    - -DCMAKE_C_FLAGS="-Wa,-mrelax-relocations=no"
+    - -DCMAKE_C_FLAGS_RELEASE='-O3 -DNDEBUG -Wa,-mrelax-relocations=no'
     - -DLLVM_ROOT_DIR=../../llvm/install
     - -DLDC_INSTALL_LTOPLUGIN=ON
+    - -DCMAKE_VERBOSE_MAKEFILE=1
     stage:
     - -etc/ldc2.conf
     build-packages:

It looks like setting CMAKE_C_FLAGS_RELEASE allows the -fPIC flag to be automatically added by the build process. Using single quotes rather than double quotes also seems to make a difference to cmake's handling of the multiple flags (i.e. it does handle them correctly, rather than wrongly).

WebDrake commented 7 years ago

OK, this works on both 14.04 and 16.10. I've released revision 8 via the snap store with this fix.