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:
James Larrowe
2020-07-21 14:24:22 -04:00
parent 0793e9effe
commit e51701acaa
11 changed files with 167 additions and 39 deletions

View File

@@ -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
* a copy of this software and associated documentation files (the
@@ -23,8 +23,8 @@
/* This implementation was taken from musl and modified for RGBDS */
#ifndef _GETOPT_H
#define _GETOPT_H
#ifndef RGBDS_EXTERN_GETOPT_H
#define RGBDS_EXTERN_GETOPT_H
extern char *optarg;
extern int optind, opterr, optopt, optreset;

View File

@@ -15,7 +15,7 @@
#define HASH_NB_BITS 32
#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)
/* HashMapEntry is internal, please do not attempt to use it */

35
include/platform.h Normal file
View 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 */

View File

@@ -15,7 +15,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "asm/asm.h"
#include "asm/charmap.h"
@@ -34,6 +33,7 @@
#include "extern/utf8decoder.h"
#include "linkdefs.h"
#include "platform.h" // strncasecmp, strdup
uint32_t nListCountEmpty;
char *tzNewMacro;

View File

@@ -19,7 +19,6 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "asm/fstack.h"
#include "asm/lexer.h"
@@ -30,6 +29,7 @@
#include "extern/err.h"
#include "platform.h" // S_ISDIR (stat macro)
#include "types.h"
static struct sContext *pFileStack;

View File

@@ -13,7 +13,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "asm/asm.h"
#include "asm/fstack.h"
@@ -27,6 +26,7 @@
#include "extern/err.h"
#include "asmy.h"
#include "platform.h" // strncasecmp, strdup
struct sLexString {
char *tzName;

View File

@@ -30,6 +30,7 @@
#include "extern/err.h"
#include "linkdefs.h"
#include "platform.h" // strdup
struct Patch {
char tzFilename[_MAX_PATH + 1];

View File

@@ -14,6 +14,7 @@
#include "asm/warning.h"
#include "extern/err.h"
#include "platform.h" // strdup
struct SectionStackEntry {
struct Section *pSection;

138
src/extern/getopt.c vendored
View File

@@ -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
* a copy of this software and associated documentation files (the
@@ -26,40 +26,128 @@
#include <stddef.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <wchar.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;
(void)(fputs(a, f)>=0
&& fwrite(b, strlen(b), 1, f)
&& fwrite(c, 1, l, f)==l
&& putc('\n', f));
if (fputs(a, f) >= 0 &&
fwrite(b, strlen(b), 1, 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)
{
char **av = (char **)argv;
char *tmp = av[src];
char *tmp = argv[src];
int i;
for (i=src; i>dest; i--)
av[i] = av[i-1];
av[dest] = tmp;
argv[i] = argv[i-1];
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;
if (!optind || __optreset) {
__optreset = 0;
__optpos = 0;
if (!optind || optreset) {
optreset = 0;
optpos = 0;
optind = 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;
}
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) {
int i, cnt = optind-resumed;
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;
}
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;
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])))
{
int colon = optstring[optstring[0]=='+'||optstring[0]=='-']==':';
int i, cnt, match;
char *arg, *opt, *start = argv[optind]+1;
int i, cnt, match = 0;
char *arg = 0, *opt, *start = argv[optind]+1;
for (cnt=i=0; longopts[i].name; i++) {
const char *name = longopts[i].name;
opt = start;
@@ -128,7 +216,7 @@ static int musl__getopt_long_core(int argc, char **argv, const char *optstring,
optopt = longopts[i].val;
if (colon || !opterr)
return '?';
musl__getopt_msg(argv[0],
musl_getopt_msg(argv[0],
": option does not take an argument: ",
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;
if (colon) return ':';
if (!opterr) return '?';
musl__getopt_msg(argv[0],
musl_getopt_msg(argv[0],
": option requires an argument: ",
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] == '-') {
optopt = 0;
if (!colon && opterr)
musl__getopt_msg(argv[0], cnt ?
musl_getopt_msg(argv[0], cnt ?
": option is ambiguous: " :
": unrecognized option: ",
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)
{
return musl__getopt_long(argc, argv, optstring, longopts, idx, 1);
return musl_getopt_long(argc, argv, optstring, longopts, idx, 1);
}

View File

@@ -98,9 +98,9 @@ int main(int argc, char *argv[])
bool resize = false;
bool setversion = false;
char *title; /* game title in ASCII */
char *id; /* game ID in ASCII */
char *newlicensee; /* new licensee ID, two ASCII characters */
char *title = NULL; /* game title in ASCII */
char *id = NULL; /* game ID in ASCII */
char *newlicensee = NULL; /* new licensee ID, two ASCII characters */
int licensee = 0; /* old licensee ID */
int cartridge = 0; /* cartridge hardware ID */

View File

@@ -454,9 +454,8 @@ void obj_ReadFile(char const *fileName)
symbolList->next = symbolLists;
symbolLists = symbolList;
uint32_t nbSymPerSect[nbSections ? nbSections : 1];
memset(nbSymPerSect, 0, sizeof(nbSymPerSect));
uint32_t *nbSymPerSect = calloc(nbSections ? nbSections : 1,
sizeof(*nbSymPerSect));
verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
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 */
struct Section *fileSections[nbSections ? nbSections : 1];
struct Section **fileSections = malloc(sizeof(*fileSections)
* (nbSections ? nbSections : 1));
verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
for (uint32_t i = 0; i < nbSections; i++) {
@@ -501,6 +501,8 @@ void obj_ReadFile(char const *fileName)
sect_AddSection(fileSections[i]);
}
free(nbSymPerSect);
/* Give symbols pointers to their sections */
for (uint32_t i = 0; i < nbSymbols; i++) {
int32_t sectionID = fileSymbols[i]->sectionID;
@@ -539,6 +541,7 @@ void obj_ReadFile(char const *fileName)
assertions = assertion;
}
free(fileSections);
fclose(file);
}