mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Add platform-specific fixes file
Create a new file, platform.h, for platform-specific hacks for MSVC, this includes defining strncasecmp to _stricmp and strdup to _strdup, among other things like defining missing stat macros Change some things not supported in MSVC, like _Static_assert, to their counterparts (in this case, static_assert) Replace usage of VLAs with malloc and free Update getopt_long and use the getopt implementation from musl on Windows. Use comments to show which functions from platform.h are being used
This commit is contained in:
6
include/extern/getopt.h
vendored
6
include/extern/getopt.h
vendored
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2005-2019 Rich Felker, et al.
|
* Copyright © 2005-2020 Rich Felker, et al.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@@ -23,8 +23,8 @@
|
|||||||
|
|
||||||
/* This implementation was taken from musl and modified for RGBDS */
|
/* This implementation was taken from musl and modified for RGBDS */
|
||||||
|
|
||||||
#ifndef _GETOPT_H
|
#ifndef RGBDS_EXTERN_GETOPT_H
|
||||||
#define _GETOPT_H
|
#define RGBDS_EXTERN_GETOPT_H
|
||||||
|
|
||||||
extern char *optarg;
|
extern char *optarg;
|
||||||
extern int optind, opterr, optopt, optreset;
|
extern int optind, opterr, optopt, optreset;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
#define HASH_NB_BITS 32
|
#define HASH_NB_BITS 32
|
||||||
#define HALF_HASH_NB_BITS 16
|
#define HALF_HASH_NB_BITS 16
|
||||||
_Static_assert(HALF_HASH_NB_BITS * 2 == HASH_NB_BITS, "");
|
static_assert(HALF_HASH_NB_BITS * 2 == HASH_NB_BITS, "");
|
||||||
#define HASHMAP_NB_BUCKETS (1 << HALF_HASH_NB_BITS)
|
#define HASHMAP_NB_BUCKETS (1 << HALF_HASH_NB_BITS)
|
||||||
|
|
||||||
/* HashMapEntry is internal, please do not attempt to use it */
|
/* HashMapEntry is internal, please do not attempt to use it */
|
||||||
|
|||||||
35
include/platform.h
Normal file
35
include/platform.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of RGBDS.
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020 RGBDS contributors.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* platform-specific hacks */
|
||||||
|
|
||||||
|
#ifndef RGBDS_PLATFORM_H
|
||||||
|
#define RGBDS_PLATFORM_H
|
||||||
|
|
||||||
|
/* MSVC doesn't have strncasecmp, use a suitable replacement */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# include <string.h>
|
||||||
|
# define strncasecmp _strnicmp
|
||||||
|
#else
|
||||||
|
# include <strings.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* MSVC has deprecated strdup in favor of _strdup */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# define strdup _strdup
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* MSVC prefixes the names of S_* macros with underscores,
|
||||||
|
and doesn't define any S_IS* macros. Define them ourselves */
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# define S_IFMT _S_IFMT
|
||||||
|
# define S_IFDIR _S_IFDIR
|
||||||
|
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* RGBDS_PLATFORM_H */
|
||||||
@@ -15,7 +15,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
|
||||||
|
|
||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/charmap.h"
|
#include "asm/charmap.h"
|
||||||
@@ -34,6 +33,7 @@
|
|||||||
#include "extern/utf8decoder.h"
|
#include "extern/utf8decoder.h"
|
||||||
|
|
||||||
#include "linkdefs.h"
|
#include "linkdefs.h"
|
||||||
|
#include "platform.h" // strncasecmp, strdup
|
||||||
|
|
||||||
uint32_t nListCountEmpty;
|
uint32_t nListCountEmpty;
|
||||||
char *tzNewMacro;
|
char *tzNewMacro;
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
#include "asm/lexer.h"
|
#include "asm/lexer.h"
|
||||||
@@ -30,6 +29,7 @@
|
|||||||
|
|
||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|
||||||
|
#include "platform.h" // S_ISDIR (stat macro)
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
static struct sContext *pFileStack;
|
static struct sContext *pFileStack;
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
|
||||||
|
|
||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/fstack.h"
|
#include "asm/fstack.h"
|
||||||
@@ -27,6 +26,7 @@
|
|||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|
||||||
#include "asmy.h"
|
#include "asmy.h"
|
||||||
|
#include "platform.h" // strncasecmp, strdup
|
||||||
|
|
||||||
struct sLexString {
|
struct sLexString {
|
||||||
char *tzName;
|
char *tzName;
|
||||||
|
|||||||
@@ -30,6 +30,7 @@
|
|||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
|
||||||
#include "linkdefs.h"
|
#include "linkdefs.h"
|
||||||
|
#include "platform.h" // strdup
|
||||||
|
|
||||||
struct Patch {
|
struct Patch {
|
||||||
char tzFilename[_MAX_PATH + 1];
|
char tzFilename[_MAX_PATH + 1];
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#include "asm/warning.h"
|
#include "asm/warning.h"
|
||||||
|
|
||||||
#include "extern/err.h"
|
#include "extern/err.h"
|
||||||
|
#include "platform.h" // strdup
|
||||||
|
|
||||||
struct SectionStackEntry {
|
struct SectionStackEntry {
|
||||||
struct Section *pSection;
|
struct Section *pSection;
|
||||||
|
|||||||
138
src/extern/getopt.c
vendored
138
src/extern/getopt.c
vendored
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2005-2019 Rich Felker, et al.
|
* Copyright © 2005-2020 Rich Felker, et al.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@@ -26,40 +26,128 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <unistd.h>
|
#ifndef _MSC_VER
|
||||||
|
# include <unistd.h>
|
||||||
|
#endif
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
#include "extern/getopt.h"
|
#include "extern/getopt.h"
|
||||||
|
|
||||||
static int __optpos, __optreset;
|
#ifdef _MSC_VER
|
||||||
|
char *optarg;
|
||||||
|
int optind=1, opterr=1, optopt;
|
||||||
|
#endif
|
||||||
|
int optreset=0;
|
||||||
|
static int optpos;
|
||||||
|
|
||||||
void musl__getopt_msg(const char *a, const char *b, const char *c, size_t l)
|
static void musl_getopt_msg(const char *a, const char *b, const char *c, size_t l)
|
||||||
{
|
{
|
||||||
FILE *f = stderr;
|
FILE *f = stderr;
|
||||||
(void)(fputs(a, f)>=0
|
|
||||||
&& fwrite(b, strlen(b), 1, f)
|
if (fputs(a, f) >= 0 &&
|
||||||
&& fwrite(c, 1, l, f)==l
|
fwrite(b, strlen(b), 1, f) &&
|
||||||
&& putc('\n', f));
|
fwrite(c, 1, l, f) == l)
|
||||||
|
putc('\n', f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
static int getopt(int argc, char *argv[], const char *optstring)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
wchar_t c, d;
|
||||||
|
int k, l;
|
||||||
|
char *optchar;
|
||||||
|
|
||||||
|
if (!optind || optreset) {
|
||||||
|
optreset = 0;
|
||||||
|
optpos = 0;
|
||||||
|
optind = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optind >= argc || !argv[optind])
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (argv[optind][0] != '-') {
|
||||||
|
if (optstring[0] == '-') {
|
||||||
|
optarg = argv[optind++];
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!argv[optind][1])
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (argv[optind][1] == '-' && !argv[optind][2])
|
||||||
|
return optind++, -1;
|
||||||
|
|
||||||
|
if (!optpos) optpos++;
|
||||||
|
if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
|
||||||
|
k = 1;
|
||||||
|
c = 0xfffd; /* replacement char */
|
||||||
|
}
|
||||||
|
optchar = argv[optind]+optpos;
|
||||||
|
optpos += k;
|
||||||
|
|
||||||
|
if (!argv[optind][optpos]) {
|
||||||
|
optind++;
|
||||||
|
optpos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (optstring[0] == '-' || optstring[0] == '+')
|
||||||
|
optstring++;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
d = 0;
|
||||||
|
do {
|
||||||
|
l = mbtowc(&d, optstring+i, MB_LEN_MAX);
|
||||||
|
if (l>0) i+=l; else i++;
|
||||||
|
} while (l && d != c);
|
||||||
|
|
||||||
|
if (d != c || c == ':') {
|
||||||
|
optopt = c;
|
||||||
|
if (optstring[0] != ':' && opterr)
|
||||||
|
musl_getopt_msg(argv[0], ": unrecognized option: ", optchar, k);
|
||||||
|
return '?';
|
||||||
|
}
|
||||||
|
if (optstring[i] == ':') {
|
||||||
|
optarg = 0;
|
||||||
|
if (optstring[i+1] != ':' || optpos) {
|
||||||
|
optarg = argv[optind++] + optpos;
|
||||||
|
optpos = 0;
|
||||||
|
}
|
||||||
|
if (optind > argc) {
|
||||||
|
optopt = c;
|
||||||
|
if (optstring[0] == ':') return ':';
|
||||||
|
if (opterr) musl_getopt_msg(argv[0],
|
||||||
|
": option requires an argument: ",
|
||||||
|
optchar, k);
|
||||||
|
return '?';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
#endif /* _MSC_VER */
|
||||||
|
|
||||||
static void permute(char **argv, int dest, int src)
|
static void permute(char **argv, int dest, int src)
|
||||||
{
|
{
|
||||||
char **av = (char **)argv;
|
char *tmp = argv[src];
|
||||||
char *tmp = av[src];
|
|
||||||
int i;
|
int i;
|
||||||
for (i=src; i>dest; i--)
|
for (i=src; i>dest; i--)
|
||||||
av[i] = av[i-1];
|
argv[i] = argv[i-1];
|
||||||
av[dest] = tmp;
|
argv[dest] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int musl__getopt_long_core(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
|
static int musl_getopt_long_core(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly);
|
||||||
|
|
||||||
static int musl__getopt_long(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
static int musl_getopt_long(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
||||||
{
|
{
|
||||||
int ret, skipped, resumed;
|
int ret, skipped, resumed;
|
||||||
if (!optind || __optreset) {
|
if (!optind || optreset) {
|
||||||
__optreset = 0;
|
optreset = 0;
|
||||||
__optpos = 0;
|
optpos = 0;
|
||||||
optind = 1;
|
optind = 1;
|
||||||
}
|
}
|
||||||
if (optind >= argc || !argv[optind]) return -1;
|
if (optind >= argc || !argv[optind]) return -1;
|
||||||
@@ -73,7 +161,7 @@ static int musl__getopt_long(int argc, char **argv, const char *optstring, const
|
|||||||
optind = i;
|
optind = i;
|
||||||
}
|
}
|
||||||
resumed = optind;
|
resumed = optind;
|
||||||
ret = musl__getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
|
ret = musl_getopt_long_core(argc, argv, optstring, longopts, idx, longonly);
|
||||||
if (resumed > skipped) {
|
if (resumed > skipped) {
|
||||||
int i, cnt = optind-resumed;
|
int i, cnt = optind-resumed;
|
||||||
for (i=0; i<cnt; i++)
|
for (i=0; i<cnt; i++)
|
||||||
@@ -83,7 +171,7 @@ static int musl__getopt_long(int argc, char **argv, const char *optstring, const
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int musl__getopt_long_core(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
static int musl_getopt_long_core(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx, int longonly)
|
||||||
{
|
{
|
||||||
optarg = 0;
|
optarg = 0;
|
||||||
if (longopts && argv[optind][0] == '-' &&
|
if (longopts && argv[optind][0] == '-' &&
|
||||||
@@ -91,8 +179,8 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
|
|||||||
(argv[optind][1] == '-' && argv[optind][2])))
|
(argv[optind][1] == '-' && argv[optind][2])))
|
||||||
{
|
{
|
||||||
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
|
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
|
||||||
int i, cnt, match;
|
int i, cnt, match = 0;
|
||||||
char *arg, *opt, *start = argv[optind]+1;
|
char *arg = 0, *opt, *start = argv[optind]+1;
|
||||||
for (cnt=i=0; longopts[i].name; i++) {
|
for (cnt=i=0; longopts[i].name; i++) {
|
||||||
const char *name = longopts[i].name;
|
const char *name = longopts[i].name;
|
||||||
opt = start;
|
opt = start;
|
||||||
@@ -128,7 +216,7 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
|
|||||||
optopt = longopts[i].val;
|
optopt = longopts[i].val;
|
||||||
if (colon || !opterr)
|
if (colon || !opterr)
|
||||||
return '?';
|
return '?';
|
||||||
musl__getopt_msg(argv[0],
|
musl_getopt_msg(argv[0],
|
||||||
": option does not take an argument: ",
|
": option does not take an argument: ",
|
||||||
longopts[i].name,
|
longopts[i].name,
|
||||||
strlen(longopts[i].name));
|
strlen(longopts[i].name));
|
||||||
@@ -140,7 +228,7 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
|
|||||||
optopt = longopts[i].val;
|
optopt = longopts[i].val;
|
||||||
if (colon) return ':';
|
if (colon) return ':';
|
||||||
if (!opterr) return '?';
|
if (!opterr) return '?';
|
||||||
musl__getopt_msg(argv[0],
|
musl_getopt_msg(argv[0],
|
||||||
": option requires an argument: ",
|
": option requires an argument: ",
|
||||||
longopts[i].name,
|
longopts[i].name,
|
||||||
strlen(longopts[i].name));
|
strlen(longopts[i].name));
|
||||||
@@ -158,7 +246,7 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
|
|||||||
if (argv[optind][1] == '-') {
|
if (argv[optind][1] == '-') {
|
||||||
optopt = 0;
|
optopt = 0;
|
||||||
if (!colon && opterr)
|
if (!colon && opterr)
|
||||||
musl__getopt_msg(argv[0], cnt ?
|
musl_getopt_msg(argv[0], cnt ?
|
||||||
": option is ambiguous: " :
|
": option is ambiguous: " :
|
||||||
": unrecognized option: ",
|
": unrecognized option: ",
|
||||||
argv[optind]+2,
|
argv[optind]+2,
|
||||||
@@ -172,5 +260,5 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
|
|||||||
|
|
||||||
int musl_getopt_long_only(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx)
|
int musl_getopt_long_only(int argc, char **argv, const char *optstring, const struct option *longopts, int *idx)
|
||||||
{
|
{
|
||||||
return musl__getopt_long(argc, argv, optstring, longopts, idx, 1);
|
return musl_getopt_long(argc, argv, optstring, longopts, idx, 1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,9 +98,9 @@ int main(int argc, char *argv[])
|
|||||||
bool resize = false;
|
bool resize = false;
|
||||||
bool setversion = false;
|
bool setversion = false;
|
||||||
|
|
||||||
char *title; /* game title in ASCII */
|
char *title = NULL; /* game title in ASCII */
|
||||||
char *id; /* game ID in ASCII */
|
char *id = NULL; /* game ID in ASCII */
|
||||||
char *newlicensee; /* new licensee ID, two ASCII characters */
|
char *newlicensee = NULL; /* new licensee ID, two ASCII characters */
|
||||||
|
|
||||||
int licensee = 0; /* old licensee ID */
|
int licensee = 0; /* old licensee ID */
|
||||||
int cartridge = 0; /* cartridge hardware ID */
|
int cartridge = 0; /* cartridge hardware ID */
|
||||||
|
|||||||
@@ -454,9 +454,8 @@ void obj_ReadFile(char const *fileName)
|
|||||||
symbolList->next = symbolLists;
|
symbolList->next = symbolLists;
|
||||||
symbolLists = symbolList;
|
symbolLists = symbolList;
|
||||||
|
|
||||||
uint32_t nbSymPerSect[nbSections ? nbSections : 1];
|
uint32_t *nbSymPerSect = calloc(nbSections ? nbSections : 1,
|
||||||
|
sizeof(*nbSymPerSect));
|
||||||
memset(nbSymPerSect, 0, sizeof(nbSymPerSect));
|
|
||||||
|
|
||||||
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
|
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
|
||||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||||
@@ -475,7 +474,8 @@ void obj_ReadFile(char const *fileName)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* This file's sections, stored in a table to link symbols to them */
|
/* This file's sections, stored in a table to link symbols to them */
|
||||||
struct Section *fileSections[nbSections ? nbSections : 1];
|
struct Section **fileSections = malloc(sizeof(*fileSections)
|
||||||
|
* (nbSections ? nbSections : 1));
|
||||||
|
|
||||||
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
|
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
|
||||||
for (uint32_t i = 0; i < nbSections; i++) {
|
for (uint32_t i = 0; i < nbSections; i++) {
|
||||||
@@ -501,6 +501,8 @@ void obj_ReadFile(char const *fileName)
|
|||||||
sect_AddSection(fileSections[i]);
|
sect_AddSection(fileSections[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(nbSymPerSect);
|
||||||
|
|
||||||
/* Give symbols pointers to their sections */
|
/* Give symbols pointers to their sections */
|
||||||
for (uint32_t i = 0; i < nbSymbols; i++) {
|
for (uint32_t i = 0; i < nbSymbols; i++) {
|
||||||
int32_t sectionID = fileSymbols[i]->sectionID;
|
int32_t sectionID = fileSymbols[i]->sectionID;
|
||||||
@@ -539,6 +541,7 @@ void obj_ReadFile(char const *fileName)
|
|||||||
assertions = assertion;
|
assertions = assertion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(fileSections);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user