thenakulchawla
thenakulchawla

Reputation: 5254

How to link a static library using WAF?

I'm using OpenSSL in my C++ program, and I need to link crypto and ssl with it. If it were for example gcc, I would just pass:

-lcrypto -lssl

I am adding this dependency in Network-Simulator 3. But I don't know how to do this in WAF. How should I add them as a dependency?

Upvotes: 4

Views: 3322

Answers (2)

thenakulchawla
thenakulchawla

Reputation: 5254

I tried adding CRYPTOPP instead of Crypto because I could do the same work with CRYPTOPP.

This is how it worked for me. The below code needs to be added/edited in wscript found in ns-3.xx directory.

def configure(conf):
    #other parameters removed
    env = conf.env
    conf.env['cryptopp'] = conf.check(mandatory=True, lib='cryptopp', uselib_store='CRYPTOPP')
    conf.env['sph'] = conf.check(mandatory=True, lib='sph', uselib_store='SPH')
    conf.env.append_value('CXXDEFINES', 'ENABLE_CRYPTOPP')
    conf.env.append_value('CCDEFINES', 'ENABLE_CRYPTOPP')

    conf.env['lssl'] = conf.check(mandatory=True, lib='ssl', uselib_store='OPENSSL')
    conf.env.append_value('CXXDEFINES', 'ENABLE_SSL')
    conf.env.append_value('CCDEFINES', 'ENABLE_SSL')

.....

def create_ns3_program(bld, name, dependencies=('core',)):
    program = bld(features='cxx cxxprogram')
    #other parameters removed
    program.use = program.ns3_module_dependencies
    if program.env['ENABLE_STATIC_NS3']:
        if sys.platform == 'darwin':
            program.env.STLIB_MARKER = '-Wl,-all_load,-lcryptopp,-lsph'
        else:
            program.env.STLIB_MARKER = '-Wl,-Bstatic,--whole-archive,-lcryptopp,-lsph'
            program.env.SHLIB_MARKER = '-Wl,-Bdynamic,--no-whole-archive,-lcryptopp,-lsph'
    else:
        if program.env.DEST_BINFMT == 'elf':
            # All ELF platforms are impacted but only the gcc compiler has a flag to fix it.
            if 'gcc' in (program.env.CXX_NAME, program.env.CC_NAME): 
                program.env.append_value ('SHLIB_MARKER', '-Wl,--no-as-needed,-lcryptopp,-lsph')

    return program

.......

def add_scratch_programs(bld):
    all_modules = [mod[len("ns3-"):] for mod in bld.env['NS3_ENABLED_MODULES']]
    try:
        for filename in os.listdir("scratch"):
            if filename.startswith('.') or filename == 'CVS':
                continue
            if os.path.isdir(os.path.join("scratch", filename)):
                obj = bld.create_ns3_program(filename, all_modules)
                obj.path = obj.path.find_dir('scratch').find_dir(filename)
                obj.source = obj.path.ant_glob('*.cc')
                obj.target = filename
                obj.name = obj.target
                obj.install_path = None
                #Add the below paramters
                obj.uselib = 'CRYPTOPP'
                obj.uselib = 'SPH'
                obj.uselib = 'OPENSSL'
            elif filename.endswith(".cc"):
                name = filename[:-len(".cc")]
                obj = bld.create_ns3_program(name, all_modules)
                obj.path = obj.path.find_dir('scratch')
                obj.source = filename
                obj.target = name
                obj.name = obj.target
                obj.install_path = None
                #Add the below paramters
                obj.uselib = 'CRYPTOPP' 
                obj.uselib = 'SPH' 
                obj.uselib = 'OPENSSL' 
    except OSError:
        return

This basically adds all paramters at runtime to the simulator you are running from scratch.

PS: This script is specific to ns-3.

Upvotes: 0

Rakete1111
Rakete1111

Reputation: 48918

First you need to check in configure if the library is available, then you can build it.

def configure(cnf):
    # other parameters omitted for brevity
    cnf.check(lib=["crypto", "ssl"])

def build(bld):
    # other parameters omitted for brevity
    bld(use=["crypto", "ssl"])

You could also use the uselib_store parameter if you don't want to repeat the libraries:

cnf.check(lib=["crypto", "ssl"], uselib_store=["libs"])
bld(use=["libs"])

Upvotes: 3

Related Questions