Error when cross compiling for ARM
Hitting a GYP compilation problem when cross compiling:
$ npm install --loglevel=silly --arch=arm libpq
...
...
arm-poky-linux-gnueabi-g++ -march=armv7ve -mthumb -mfpu=neon -mfloat-abi=hard -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-securit
y --sysroot=my-arm-sysroot -shared -pthread -rdynamic -Wl,-
Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -L/usr/lib/x86_64-linux-gnu/mit-krb5 -Wl,--as-needed -Wl,-O1 -Wl,--hash-style=gnu -Wl,--as-needed -fstack-protector-strong -Wl,-z,re
lro,-z,now -Wl,-soname=addon.node -o Release/obj.target/addon.node -Wl,--start-group Release/obj.target/addon/src/connection.o Release/obj.target/addon/src/connect-async-worker
.o Release/obj.target/addon/src/addon.o -Wl,--end-group -lpq -L/usr/lib/x86_64-linux-gnu
my-arm-sysroot/usr/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/8.3.0/ld: warning: library search path "/usr/lib/x86_64-linux-gnu/mit-krb5" is unsafe for cross-compilation
my-arm-sysroot/usr/bin/arm-poky-linux-gnueabi/../../libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/8.3.0/ld: warning: library search path "/usr/lib/x86_64-linux-gnu" is unsafe for cross-compilation
/usr/lib/x86_64-linux-gnu/libpq.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
Note that it's looking for libpq.so in the wrong location -lpq -L/usr/lib/x86_64-linux-gnu - it's the host sysroot and not the target.
This can be traced back to https://github.com/brianc/node-libpq/blob/master/binding.gyp
Where:
['OS=="linux"', {
'variables' : {
# Find the pull path to the pg_config command, since iy may not be on the PATH
'pgconfig': '<!(which pg_config || find /usr/bin /usr/local/bin /usr/pg* /opt -executable -name pg_config -print -quit)'
}
}, {
#Default to assuming pg_config is on the PATH.
'variables': {
'pgconfig': 'pg_config'
},
}]
pg_config is picked up from /usr/bin - again the host.
The following
'libraries' : ['-lpq -L<!@(<(pgconfig) --libdir)'],
(https://github.com/brianc/node-libpq/blob/master/binding.gyp#L41) Returns:
-lpq -L/usr/lib/x86_64-linux-gnu
Given we don't have pg_config for the target (e.g. it doesn't exist , or - it can't be executed on the host) - discovery of the shared library to link against cannot be done with pg_config.
Any help will be appreciated!
I've been able to get around the problem by adding
$ export CXX="${CXX} -Lmy-arm-sysroot/usr/lib"
$ npm install --loglevel=silly --arch=arm libpq
That way it finds the right libpq.so for the target. It doesn't solve the actual problem though.