Scons LIBS 链接,如何删除重复的 LIBS?

Scons LIBS linking, how to remove duplicate LIBS?

我们在指定库依赖时使用间接,例如,

env.Program(TARGET, SOURCE, LIBS = [ $LIBS_A, $LIBS_B, $LIBS_C ])

LIBS_A、LIBS_B 和 LIBS_C 在其他 SConscript 文件中指定。

假设:

LIBS_A/Sconscript has  LIBS_A = [ foo, bar, baz ]
LIBS_B/Sconscript has  LIBS_B = [ foo, bar, bat ]
LIBS_C/Sconscript has  LIBS_C = [ foo, bar, ben ]

那么最后的链接器行将有:

-lfoo -lbar -lbaz -lfoo -lbar -lbat -lfoo -lbar -lben

我们如何删除重复项并读取链接器行:

-lfoo -lbar -lbaz -lbat -lben

怎么样。文件完全列出。

main.cxx

#include <iostream>

int main()
{
  std::cout << "Hello World!";
}

S构造

env=Environment()

Export('env')

for l in ['A','B','C']:
    env.SConscript("LIBS_%s/SConscript"%l)

env.Program('myprogram',['main.cxx'], LIBS = list(set(env['LIBS_A'] + env['LIBS_B'] + env['LIBS_C'])))

LIBS_A/SConscript 导入('env')

env['LIBS_A'] = ['foo','bar','baz']

LIBS_B/SConscript 导入('env')

env['LIBS_B'] = ['foo','bar','bat']

LIBS_C/SConscript 导入('env')

env['LIBS_C'] = ['foo','bar','ben']

产量:

g++ -o myprogram main.o -lbaz -lbat -lfoo -lbar -lben

如果其中 none 适合您,这里有一个更复杂的方法:

class foo(object):
    def __init__(self, arg):
        self.arg = arg

    def __call__(self, target, source, env, for_signature):
        print "LIBS_A: %s  LIBS_B: %s"%(env['LIBS_A'],env['LIBS_B'])
        rc = list(set(env['LIBS_A']+env['LIBS_B']))
        print "Unique: %s"%rc
        print "SIG:%s"%for_signature
        return rc

# Will call our foo method to uniquify LIBS_*
env=Environment(FOO=foo, BAR="${FOO('$LIBS_A $LIBS_B')}")

env.Command('foo.XXXXX','foo.in',action="echo LIBS: $LIBS",LIBS="$BAR",
            LIBS_A=['a','b','c','d','e','f','f','b'],
            LIBS_B=['b','d','e','g','x'])

这对我有用。非常感谢@bdbaddog

class uniquify_libs(object):
  def __init__(self, arg):
    self.arg = arg 

  def __call__(self, target, source, env, for_signature):
    rc = list(set(env.subst(env[self.arg])[0].split()))
    return rc

LIBS = ['$LIBS_A', '$LIBS_B'] ## defined in other SConscripts

Program(
  target  = TARGET,
  source  = SOURCE,
  UNIQUE  = uniquify_libs,
  MYLIBS  = LIBS,
  LIBS    = "${UNIQUE('MYLIBS')}",
)