diff mbox series

package/erlang-rebar: fix linking failure on shared library

Message ID 20211219070332.2642029-1-giulio.benetti@benettiengineering.com
State Accepted
Headers show
Series package/erlang-rebar: fix linking failure on shared library | expand

Commit Message

Giulio Benetti Dec. 19, 2021, 7:03 a.m. UTC
Add patch to fix linking failure while creating shared library. As
explained in the patch itself, there is no specific variable for when we
link a shared library and rebar itself rely on the default LDFLAGS. Since
by default every CFLAGS is filled with -fPIC we need to make sure that
every LDFLAGS is the same, so not having any other *_LDFLAGS variable to
fille with -fPIC let's add it to the main LDFLAGS.

Fixes:
http://autobuild.buildroot.net/results/602/60296a48210e7ffc6bc9fa50ee586441a8957e85/

Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
---
 ...ompiler-add-fPIC-to-LDFLAGS-by-defau.patch | 35 +++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch

Comments

Yann E. MORIN Dec. 19, 2021, 8:08 a.m. UTC | #1
Giulio, All,

On 2021-12-19 08:03 +0100, Giulio Benetti spake thusly:
> Add patch to fix linking failure while creating shared library. As
> explained in the patch itself, there is no specific variable for when we
> link a shared library and rebar itself rely on the default LDFLAGS. Since
> by default every CFLAGS is filled with -fPIC we need to make sure that
> every LDFLAGS is the same, so not having any other *_LDFLAGS variable to
> fille with -fPIC let's add it to the main LDFLAGS.

OK, I think I got the hang of it. But see below...

> Fixes:
> http://autobuild.buildroot.net/results/602/60296a48210e7ffc6bc9fa50ee586441a8957e85/
> 
> Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
> ---
>  ...ompiler-add-fPIC-to-LDFLAGS-by-defau.patch | 35 +++++++++++++++++++
>  1 file changed, 35 insertions(+)
>  create mode 100644 package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> 
> diff --git a/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> new file mode 100644
> index 0000000000..a9c1670a1f
> --- /dev/null
> +++ b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> @@ -0,0 +1,35 @@
> +From 7f54d48ee5db037778ead310e0b8278f3fe70b41 Mon Sep 17 00:00:00 2001
> +From: Giulio Benetti <giulio.benetti@benettiengineering.com>
> +Date: Sun, 19 Dec 2021 07:52:55 +0100
> +Subject: [PATCH] src/rebar_port_compiler: add -fPIC to LDFLAGS by default
> +
> +Since both DRV_CFLAGS and EXE_CFLAGS list -fPIC we need also the LDFLAGS
> +to follow them. Unfortunately adding -fPIC only to DRV_LDFLAGS and
> +EXE_LDFLAGS is not sufficient, since when linking as a library(.so) it
> +doesn't take into account those variables. Since -fPIC is needed by default
> +by any kind of linking, let's add it to the general -fPIC. Rebar seems to
> +link libraries without taking into account any variable listed in:
> +src/rebar_port_compiler.erl
> +this after testing and tracing for every variable.
> +
> +Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
> +---
> + src/rebar_port_compiler.erl | 2 ++
> + 1 file changed, 2 insertions(+)
> +
> +diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl
> +index 9679c80..bd08b21 100644
> +--- a/src/rebar_port_compiler.erl
> ++++ b/src/rebar_port_compiler.erl
> +@@ -645,6 +645,8 @@ default_env() ->
> +      {"OBJCOPY", get_tool(Arch, "objcopy", "objcopy")},
> +      {"OBJDUMP", get_tool(Arch, "objdump", "objdump")},
> + 
> ++     {"LDFLAGS", "-fPIC $LDFLAGS"},

Don't we want to match EXE_CFLAGS and DRV_CFLAGS, and use DRV_LDFLAGS
and EXE_LDFLAGS instwead of the generic LDFLAGS? This way, LDFLAGS is
left alone, like are CFLAGS and CXXFLAGS, and the internal variables
are used.

EXE_LDFLAGS and DRV_LDFLAGS are already defined, lines 665 and 667.

Regards,
Yann E. MORIN.

> +      {"DRV_CXX_TEMPLATE",
> +       "$CXX -c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
> +      {"DRV_CC_TEMPLATE",
> +-- 
> +2.25.1
> +
> -- 
> 2.25.1
>
Giulio Benetti Dec. 27, 2021, 12:08 a.m. UTC | #2
Hi Yann, All,

On 19/12/21 09:08, Yann E. MORIN wrote:

I've missed this e-mail :-/ ...

> Giulio, All,
> 
> On 2021-12-19 08:03 +0100, Giulio Benetti spake thusly:
>> Add patch to fix linking failure while creating shared library. As
>> explained in the patch itself, there is no specific variable for when we
>> link a shared library and rebar itself rely on the default LDFLAGS. Since
>> by default every CFLAGS is filled with -fPIC we need to make sure that
>> every LDFLAGS is the same, so not having any other *_LDFLAGS variable to
>> fille with -fPIC let's add it to the main LDFLAGS.
> 
> OK, I think I got the hang of it. But see below...
> 
>> Fixes:
>> http://autobuild.buildroot.net/results/602/60296a48210e7ffc6bc9fa50ee586441a8957e85/
>>
>> Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
>> ---
>>   ...ompiler-add-fPIC-to-LDFLAGS-by-defau.patch | 35 +++++++++++++++++++
>>   1 file changed, 35 insertions(+)
>>   create mode 100644 package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
>>
>> diff --git a/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
>> new file mode 100644
>> index 0000000000..a9c1670a1f
>> --- /dev/null
>> +++ b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
>> @@ -0,0 +1,35 @@
>> +From 7f54d48ee5db037778ead310e0b8278f3fe70b41 Mon Sep 17 00:00:00 2001
>> +From: Giulio Benetti <giulio.benetti@benettiengineering.com>
>> +Date: Sun, 19 Dec 2021 07:52:55 +0100
>> +Subject: [PATCH] src/rebar_port_compiler: add -fPIC to LDFLAGS by default
>> +
>> +Since both DRV_CFLAGS and EXE_CFLAGS list -fPIC we need also the LDFLAGS
>> +to follow them. Unfortunately adding -fPIC only to DRV_LDFLAGS and
>> +EXE_LDFLAGS is not sufficient, since when linking as a library(.so) it
>> +doesn't take into account those variables. Since -fPIC is needed by default
>> +by any kind of linking, let's add it to the general -fPIC. Rebar seems to
>> +link libraries without taking into account any variable listed in:
>> +src/rebar_port_compiler.erl
>> +this after testing and tracing for every variable.
>> +
>> +Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
>> +---
>> + src/rebar_port_compiler.erl | 2 ++
>> + 1 file changed, 2 insertions(+)
>> +
>> +diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl
>> +index 9679c80..bd08b21 100644
>> +--- a/src/rebar_port_compiler.erl
>> ++++ b/src/rebar_port_compiler.erl
>> +@@ -645,6 +645,8 @@ default_env() ->
>> +      {"OBJCOPY", get_tool(Arch, "objcopy", "objcopy")},
>> +      {"OBJDUMP", get_tool(Arch, "objdump", "objdump")},
>> +
>> ++     {"LDFLAGS", "-fPIC $LDFLAGS"},
> 
> Don't we want to match EXE_CFLAGS and DRV_CFLAGS, and use DRV_LDFLAGS
> and EXE_LDFLAGS instwead of the generic LDFLAGS? This way, LDFLAGS is
> left alone, like are CFLAGS and CXXFLAGS, and the internal variables
> are used.
> 
> EXE_LDFLAGS and DRV_LDFLAGS are already defined, lines 665 and 667.

...here the problem I've noticed is that nor EXE_LDFLAGS and neither 
DRV_LDFLAGS are used when linking a shared library. They are only used 
for the executables. I've tried in all the ways, and the solution seemed 
trivial like you point, but in the end the only way I've found for 
shared libraries to be linked with -fPIC is adding it to LDFLAGS.

So basically there had to be a specific LIB_LDFLAGS(for example) 
dedicated to it, but rebar is archived in favor of rebar3 and I see this 
as the easiest solution.

Kind regards
Yann E. MORIN Dec. 28, 2021, 9:49 p.m. UTC | #3
Giulio, All,

On 2021-12-19 08:03 +0100, Giulio Benetti spake thusly:
> Add patch to fix linking failure while creating shared library. As
> explained in the patch itself, there is no specific variable for when we
> link a shared library and rebar itself rely on the default LDFLAGS. Since
> by default every CFLAGS is filled with -fPIC we need to make sure that
> every LDFLAGS is the same, so not having any other *_LDFLAGS variable to
> fille with -fPIC let's add it to the main LDFLAGS.
> 
> Fixes:
> http://autobuild.buildroot.net/results/602/60296a48210e7ffc6bc9fa50ee586441a8957e85/
> 
> Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>

Even though I was not very happy with that solution, there is really no
better way, so: applied to master, thanks.

Regards,
Yann E. MORIN.

> ---
>  ...ompiler-add-fPIC-to-LDFLAGS-by-defau.patch | 35 +++++++++++++++++++
>  1 file changed, 35 insertions(+)
>  create mode 100644 package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> 
> diff --git a/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> new file mode 100644
> index 0000000000..a9c1670a1f
> --- /dev/null
> +++ b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
> @@ -0,0 +1,35 @@
> +From 7f54d48ee5db037778ead310e0b8278f3fe70b41 Mon Sep 17 00:00:00 2001
> +From: Giulio Benetti <giulio.benetti@benettiengineering.com>
> +Date: Sun, 19 Dec 2021 07:52:55 +0100
> +Subject: [PATCH] src/rebar_port_compiler: add -fPIC to LDFLAGS by default
> +
> +Since both DRV_CFLAGS and EXE_CFLAGS list -fPIC we need also the LDFLAGS
> +to follow them. Unfortunately adding -fPIC only to DRV_LDFLAGS and
> +EXE_LDFLAGS is not sufficient, since when linking as a library(.so) it
> +doesn't take into account those variables. Since -fPIC is needed by default
> +by any kind of linking, let's add it to the general -fPIC. Rebar seems to
> +link libraries without taking into account any variable listed in:
> +src/rebar_port_compiler.erl
> +this after testing and tracing for every variable.
> +
> +Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
> +---
> + src/rebar_port_compiler.erl | 2 ++
> + 1 file changed, 2 insertions(+)
> +
> +diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl
> +index 9679c80..bd08b21 100644
> +--- a/src/rebar_port_compiler.erl
> ++++ b/src/rebar_port_compiler.erl
> +@@ -645,6 +645,8 @@ default_env() ->
> +      {"OBJCOPY", get_tool(Arch, "objcopy", "objcopy")},
> +      {"OBJDUMP", get_tool(Arch, "objdump", "objdump")},
> + 
> ++     {"LDFLAGS", "-fPIC $LDFLAGS"},
> ++
> +      {"DRV_CXX_TEMPLATE",
> +       "$CXX -c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
> +      {"DRV_CC_TEMPLATE",
> +-- 
> +2.25.1
> +
> -- 
> 2.25.1
> 
> _______________________________________________
> buildroot mailing list
> buildroot@buildroot.org
> https://lists.buildroot.org/mailman/listinfo/buildroot
diff mbox series

Patch

diff --git a/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
new file mode 100644
index 0000000000..a9c1670a1f
--- /dev/null
+++ b/package/erlang-rebar/0001-src-rebar_port_compiler-add-fPIC-to-LDFLAGS-by-defau.patch
@@ -0,0 +1,35 @@ 
+From 7f54d48ee5db037778ead310e0b8278f3fe70b41 Mon Sep 17 00:00:00 2001
+From: Giulio Benetti <giulio.benetti@benettiengineering.com>
+Date: Sun, 19 Dec 2021 07:52:55 +0100
+Subject: [PATCH] src/rebar_port_compiler: add -fPIC to LDFLAGS by default
+
+Since both DRV_CFLAGS and EXE_CFLAGS list -fPIC we need also the LDFLAGS
+to follow them. Unfortunately adding -fPIC only to DRV_LDFLAGS and
+EXE_LDFLAGS is not sufficient, since when linking as a library(.so) it
+doesn't take into account those variables. Since -fPIC is needed by default
+by any kind of linking, let's add it to the general -fPIC. Rebar seems to
+link libraries without taking into account any variable listed in:
+src/rebar_port_compiler.erl
+this after testing and tracing for every variable.
+
+Signed-off-by: Giulio Benetti <giulio.benetti@benettiengineering.com>
+---
+ src/rebar_port_compiler.erl | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl
+index 9679c80..bd08b21 100644
+--- a/src/rebar_port_compiler.erl
++++ b/src/rebar_port_compiler.erl
+@@ -645,6 +645,8 @@ default_env() ->
+      {"OBJCOPY", get_tool(Arch, "objcopy", "objcopy")},
+      {"OBJDUMP", get_tool(Arch, "objdump", "objdump")},
+ 
++     {"LDFLAGS", "-fPIC $LDFLAGS"},
++
+      {"DRV_CXX_TEMPLATE",
+       "$CXX -c $CXXFLAGS $DRV_CFLAGS $PORT_IN_FILES -o $PORT_OUT_FILE"},
+      {"DRV_CC_TEMPLATE",
+-- 
+2.25.1
+