Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow making KSORT functions static #851

Merged
merged 1 commit into from
Apr 10, 2019
Merged

Conversation

jmarshall
Copy link
Member

Consider the following program:

#include "htslib/ksort.h"
#include "htslib/hts.h"

/* Compile and link with
        gcc -o foo foo.c libhts.a -lbz2 -llzma -lz
*/

typedef struct {
    int x;
} foo;

#define bar(a,b) ((a).x < (b).x)

KSORT_INIT_GENERIC(uint16_t)
KSORT_INIT(_off, foo, bar)
KSORT_INIT(_off_max, foo, bar)

int main() {
    hts_version();  // pull in hts.o
    errmod_cal(NULL, 0, 0, NULL, NULL);  // pull in errmod.o
}

This fails to link due to 24 duplicate ks_* functions, as the ksort usage in errmod.c and hts.c uses non-hts_-prefixed public namespace functions. It would be possible to make those HTSlib source files use ksort functions with names like …_htslib_internal_… which would not collide with sensible third-party code, but it's about as easy to add static support to htslib/ksort.h.

See also PacificBiosciences/pbmm2#11 (via this tweet); if static ksort had been available, this would have been a good way to fix this rather than renaming away from the collision.

Add KSORT_INIT2()/etc that allow specification of a scope to be used
for the functions generated. As the existing KSORT_INIT()/etc produce
shared global functions, also add KSORT_INIT_STATIC()/etc that produce
static functions -- which requires klib_unused, to avoid warnings about
unused static functions. (cf 5ffc4a2.)

`KSORT_INIT2(..., static klib_unused, ...)` would let `klib_unused` leak
into user code, so providing an extra KSORT_INIT_STATIC() is better.

Use this new static ksort in errmod.c and hts.c to avoid polluting
the non-hts_-prefixed namespace.
@daviesrob daviesrob merged commit 7a93ceb into samtools:develop Apr 10, 2019
@daviesrob
Copy link
Member

Thanks. All good for minimising our symbol leakage. Which reminds me that we should also put in a fix for #693 as well (that one is about kstring).

@jmarshall
Copy link
Member Author

Cheers. Re that kstring one: I reckon HTSlib is the 800-pound gorilla here, and that one should be worked around inside libbwa.

@jmarshall jmarshall deleted the ksort branch April 17, 2019 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants