Skip to content

Commit

Permalink
Provide a definition of ssize_t when compiling with MSVC
Browse files Browse the repository at this point in the history
Define `ssize_t` within the public headers that use it, enabling them
to be compiled with MSVC, which does not provide this type definition
in its <sys/types.h> or any other headers.

Having `ssize_t` as a macro may confuse other headers, so be sure to do
this only after all other #includes and to undefine it again at the end
of each header. User code or other libraries may also have workarounds
for this issue, so checking its not already #defined and removing our
`ssize_t` macro afterwards also avoids conflicting with other workarounds.

Under MSVC, including <stdint.h>/<string.h>/etc also causes <vcruntime.h>
to be included, which defines `size_t` and `intptr_t` as unsigned/signed
versions of the same type. So we define `ssize_t` as that, if it is not
already defined (by some other workaround) and if `_INTPTR_T_DEFINED` has
indeed been defined by <vcruntime.h> (to avoid confusing error messages).

Also define it (permanently) in <htslib/hts_os.h>, unless suppressed
by defining `HTS_NO_SSIZE_T` before including the header.
  • Loading branch information
jmarshall committed Jan 11, 2022
1 parent 965a90c commit 3e86272
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions htslib/bgzf.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

#include "hts_defs.h"

// Ensure ssize_t exists within this header. All #includes must precede this,
// and ssize_t must be undefined again at the end of this header.
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined ssize_t
#define HTSLIB_SSIZE_T
#define ssize_t intptr_t
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -450,4 +457,9 @@ typedef struct BGZF BGZF;
}
#endif

#ifdef HTSLIB_SSIZE_T
#undef HTSLIB_SSIZE_T
#undef ssize_t
#endif

#endif
12 changes: 12 additions & 0 deletions htslib/hfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ DEALINGS IN THE SOFTWARE. */

#include "hts_defs.h"

// Ensure ssize_t exists within this header. All #includes must precede this,
// and ssize_t must be undefined again at the end of this header.
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined ssize_t
#define HTSLIB_SSIZE_T
#define ssize_t intptr_t
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -368,4 +375,9 @@ int hfile_has_plugin(const char *name);
}
#endif

#ifdef HTSLIB_SSIZE_T
#undef HTSLIB_SSIZE_T
#undef ssize_t
#endif

#endif
6 changes: 6 additions & 0 deletions htslib/hts_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ extern int is_cygpty(int fd);
#define random rand
#endif

/* MSVC does not provide ssize_t in its <sys/types.h>. This ensures the type
is available (unless suppressed by defining HTS_NO_SSIZE_T first). */
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined HTS_NO_SSIZE_T && !defined ssize_t
#define ssize_t intptr_t
#endif

#endif // HTSLIB_HTS_OS_H
12 changes: 12 additions & 0 deletions htslib/knetfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
#define netclose(fd) closesocket(fd)
#endif

// Ensure ssize_t exists within this header. All #includes must precede this,
// and ssize_t must be undefined again at the end of this header.
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined ssize_t
#define HTSLIB_SSIZE_T
#define ssize_t intptr_t
#endif

// FIXME: currently I/O is unbuffered

#define KNF_TYPE_LOCAL 1
Expand Down Expand Up @@ -102,4 +109,9 @@ extern "C" {
}
#endif

#ifdef HTSLIB_SSIZE_T
#undef HTSLIB_SSIZE_T
#undef ssize_t
#endif

#endif
12 changes: 12 additions & 0 deletions htslib/kstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
#endif
#endif

// Ensure ssize_t exists within this header. All #includes must precede this,
// and ssize_t must be undefined again at the end of this header.
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined ssize_t
#define HTSLIB_SSIZE_T
#define ssize_t intptr_t
#endif

/* kstring_t is a simple non-opaque type whose fields are likely to be
* used directly by user code (but see also ks_str() and ks_len() below).
* A kstring_t object is initialised by either of
Expand Down Expand Up @@ -396,4 +403,9 @@ static inline int *ksplit(kstring_t *s, int delimiter, int *n)
return offsets;
}

#ifdef HTSLIB_SSIZE_T
#undef HTSLIB_SSIZE_T
#undef ssize_t
#endif

#endif
12 changes: 12 additions & 0 deletions htslib/sam.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ DEALINGS IN THE SOFTWARE. */
#include "hts.h"
#include "hts_endian.h"

// Ensure ssize_t exists within this header. All #includes must precede this,
// and ssize_t must be undefined again at the end of this header.
#if defined _MSC_VER && defined _INTPTR_T_DEFINED && !defined ssize_t
#define HTSLIB_SSIZE_T
#define ssize_t intptr_t
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -2266,4 +2273,9 @@ int bam_mods_at_qpos(const bam1_t *b, int qpos, hts_base_mod_state *state,
}
#endif

#ifdef HTSLIB_SSIZE_T
#undef HTSLIB_SSIZE_T
#undef ssize_t
#endif

#endif

0 comments on commit 3e86272

Please sign in to comment.