fix: build on Windows
This commit is contained in:
15
Makefile
15
Makefile
@@ -5,20 +5,21 @@ SRC = src/pasm.c \
|
||||
src/api.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
NAME = pasm
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -Iinclude
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -Iinclude -s -Os -fno-ident -fno-asynchronous-unwind-tables
|
||||
CLIBS =
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
lib: $(OBJ)
|
||||
mkdir build
|
||||
@-mkdir build
|
||||
ar rc build/lib$(NAME).a $(OBJ)
|
||||
|
||||
$(NAME): fclean
|
||||
$(NAME): lib
|
||||
$(NAME): CLIBS += build/lib$(NAME).a
|
||||
$(NAME):
|
||||
gcc tests/interpreter.c $(CFLAGS) $(CLIBS) -o build/$(NAME)
|
||||
$(CC) tests/interpreter.c $(CFLAGS) $(CLIBS) -o build/$(NAME)
|
||||
|
||||
interpreter: $(NAME)
|
||||
|
||||
@@ -27,12 +28,12 @@ debug: CFLAGS += -DDEBUG -g3
|
||||
debug: $(NAME)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
cd tests && $(MAKE) clean
|
||||
@-rm -f $(OBJ)
|
||||
@-cd tests && $(MAKE) clean
|
||||
|
||||
fclean: clean
|
||||
rm -rf build/
|
||||
cd tests && $(MAKE) fclean
|
||||
@-rm -rf build/
|
||||
@-cd tests && $(MAKE) fclean
|
||||
|
||||
re: fclean
|
||||
re: $(NAME)
|
||||
|
||||
@@ -28,7 +28,7 @@ void api_put() {
|
||||
|
||||
void api_getasynckeystate() {
|
||||
#ifdef _WIN32
|
||||
eax = GetAsyncKeyState(state->STACK[STACK_IDX--]);
|
||||
state->registers->eax = GetAsyncKeyState(state->STACK[state->STACK_IDX--]);
|
||||
#else
|
||||
state->STACK_IDX--;
|
||||
state->registers->eax = 1;
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
|
||||
FILE *fstream = NULL;
|
||||
void show_error(size_t line, char *line_) {
|
||||
#ifdef _WIN32
|
||||
int wrote = fprintf(fstream, "%ud| ", line + 1);
|
||||
#else
|
||||
int wrote = fprintf(fstream, "%ld| ", line + 1);
|
||||
#endif
|
||||
fprintf(fstream, "%s\n", line_);
|
||||
fprintf(fstream, "%*s\n", wrote + 1, "^");
|
||||
fprintf(fstream, "%*s\n", wrote + 1, "|");
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
SRC = lib_use.c
|
||||
SRC = lib_use.c \
|
||||
fmemopen.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
NAME = lib_use
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -I../include
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -Wpedantic -Iinclude -s -Os -fno-ident -fno-asynchronous-unwind-tables
|
||||
CLIBS = ../build/libpasm.a
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
-cd .. && $(MAKE) lib
|
||||
gcc $(SRC) $(CFLAGS) $(CLIBS) -o $(NAME)
|
||||
$(MAKE) -C .. lib
|
||||
$(CC) $(OBJ) $(CFLAGS) $(CLIBS) -o $(NAME)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ)
|
||||
@-rm -f $(OBJ)
|
||||
|
||||
fclean: clean
|
||||
rm -f $(NAME)
|
||||
@-rm -f $(NAME)
|
||||
|
||||
re: fclean
|
||||
re: $(NAME)
|
||||
re:
|
||||
$(MAKE) -C .. fclean && $(MAKE) -C .. lib
|
||||
$(CC) $(SRC) $(CFLAGS) $(CLIBS) -o $(NAME)
|
||||
|
||||
.PHONY : all $(NAME) clean fclean re
|
||||
|
||||
41
tests/fmemopen.c
Normal file
41
tests/fmemopen.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#include "fmemopen.h"
|
||||
#include <share.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
/* https://github.com/Arryboom/fmemopen_windows */
|
||||
|
||||
FILE *fmemopen(void *buf, size_t len, __attribute__((unused)) const char *type)
|
||||
{
|
||||
int fd;
|
||||
FILE *fp;
|
||||
char tp[MAX_PATH - 13];
|
||||
char fn[MAX_PATH + 1];
|
||||
int * pfd = &fd;
|
||||
int retner = -1;
|
||||
char tfname[] = "MemTF_";
|
||||
if (!GetTempPathA(sizeof(tp), tp))
|
||||
return NULL;
|
||||
if (!GetTempFileNameA(tp, tfname, 0, fn))
|
||||
return NULL;
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration" //msys not happy
|
||||
retner = _sopen_s(pfd, fn, _O_CREAT | _O_SHORT_LIVED | _O_TEMPORARY | _O_RDWR | _O_BINARY | _O_NOINHERIT, _SH_DENYRW, _S_IREAD | _S_IWRITE);
|
||||
#pragma GCC diagnostic pop
|
||||
if (retner != 0)
|
||||
return NULL;
|
||||
if (fd == -1)
|
||||
return NULL;
|
||||
fp = _fdopen(fd, "wb+");
|
||||
if (!fp) {
|
||||
_close(fd);
|
||||
return NULL;
|
||||
}
|
||||
/*File descriptors passed into _fdopen are owned by the returned FILE * stream.If _fdopen is successful, do not call _close on the file descriptor.Calling fclose on the returned FILE * also closes the file descriptor.*/
|
||||
fwrite(buf, len, 1, fp);
|
||||
rewind(fp);
|
||||
return fp;
|
||||
}
|
||||
#endif
|
||||
5
tests/fmemopen.h
Normal file
5
tests/fmemopen.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#ifdef _WIN32
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
FILE *fmemopen(void *buf, size_t len, const char *type);
|
||||
#endif
|
||||
@@ -1,11 +1,21 @@
|
||||
#include "../include/pasm.h"
|
||||
#include "fmemopen.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
char output[1024] = {0};
|
||||
FILE *memfile = fmemopen(output, sizeof(output), "w");
|
||||
FILE *memfile = fmemopen(output, sizeof(output), "w+");
|
||||
|
||||
if (memfile != NULL) {
|
||||
pasm_run_script("../examples/test.pasm", 0, 0, memfile);
|
||||
fclose(memfile);
|
||||
#ifdef _WIN32
|
||||
fseek(memfile, 0, SEEK_SET);
|
||||
fread(output, sizeof(char), sizeof(output), memfile);
|
||||
#endif
|
||||
printf("%s", output);
|
||||
fclose(memfile);
|
||||
} else {
|
||||
printf("null\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user