[Scons-users] How to simultaneously build pie executables and pic shared libraries?

Andrew C. Morrow andrew.c.morrow at gmail.com
Fri Oct 23 23:19:32 EDT 2015


Hi -

I think it is pretty straightforward to get PIE executables with SCons if
all linking is static:

env.AppendUnique(
    CCFLAGS=["-fpie"],
    LINKFLAGS=["-pie"],
)

How to make it work for a build with both executables and shared libraries
is not as clear. The problem is that object files destined for the shared
libraries need to still be built with -fpic and linked as normal, but
object files destined for the executable need to be built with -fpie and
then the executable must be linked with -pie:

Doing this:

env.AppendUnique(
    CCFLAGS=["-fpie"],
    SHCCFLAGS=["-fpic"],
    LINKFLAGS=["-pie"],
)

doesn't work because objects being compiled for shared libraries receive
both CCFLAGS and SHCCFLAGS. Similarly for linking, where the link step for
shared libraries gets both LINKFLAGS and SHLINKFLAGS. We definitely don't
want to pass -pie to our shared library builds, nor do we want both -fpie
and -fpic on the compile line when building objects for shared libraries.

It feels like there is a need for PROG[C|CC|CXX]FLAGS and PROGLINKFLAGS? In
other words, compiler and linker flags analogous to the SH prefixed
variants but that are only used when building objects for an executable or
linking an executable.

One idea I had was to modify CCCOM, CXXCOM, and LINKCOM to honor
PROGC*FLAGS and PROGLINKFLAGS, though I'm not very excited about it. Then I
could do this:

env.AppendUnique(
    PROGCCFLAGS=["-fpie"],
    SHCCFLAGS=["-fpic"],
    PROGLINKFLAGS=["-pie"],
)

Does this seem like a viable path to achieve PIE executables with PIC
shared libraries, or am I going to run into surprises? Any thoughts on
other ways to do this? Has anyone managed to make this work?

Thanks,
Andrew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20151023/7d90247b/attachment.html>


More information about the Scons-users mailing list