Add MG_ARCH_NEWLIB

This commit is contained in:
Sergey Lyubka 2022-05-01 13:47:33 +01:00
parent d621fbfab1
commit 38557aac7f
7 changed files with 54 additions and 45 deletions

View File

@ -40,14 +40,13 @@ examples:
test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt
$(CC) $(CFLAGS) test/pack.c -o pack $(CC) $(CFLAGS) test/pack.c -o pack
./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt -z 'gzip -c' test/data/range.txt > $@ ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@
DIR ?= test/data/ DIR ?= test/data/
OUT ?= gui.c OUT ?= gui.c
ZIP ?= -z 'gzip -c'
mkfs: mkfs:
$(CC) $(CFLAGS) test/pack.c -o pack $(CC) $(CFLAGS) test/pack.c -o pack
./pack $(ZIP) -s $(DIR) `find $(DIR) -type f` > $(OUT) ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT)
# find $(DIR) -type f | sed -e s,^$(DIR),,g -e s,^/,,g # find $(DIR) -type f | sed -e s,^$(DIR),,g -e s,^/,,g
# Check that all external (exported) symbols have "mg_" prefix # Check that all external (exported) symbols have "mg_" prefix
@ -70,7 +69,7 @@ unamalgamated: $(HDRS) Makefile test/packed_fs.c
$(CC) src/*.c test/packed_fs.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -g -o unit_test $(CC) src/*.c test/packed_fs.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -g -o unit_test
unpacked: unpacked:
$(CC) -I. mongoose.c test/unit_test.c -o unit_test $(CC) -I. mongoose.c test/unit_test.c -o $@
fuzzer: mongoose.c mongoose.h Makefile test/fuzz.c fuzzer: mongoose.c mongoose.h Makefile test/fuzz.c
clang++ mongoose.c test/fuzz.c $(WARN) $(INCS) $(TFLAGS) $(EXTRA) -DMG_ENABLE_LINES -DMG_ENABLE_LOG=0 -fsanitize=fuzzer,signed-integer-overflow,address -Wno-deprecated -o $@ clang++ mongoose.c test/fuzz.c $(WARN) $(INCS) $(TFLAGS) $(EXTRA) -DMG_ENABLE_LINES -DMG_ENABLE_LOG=0 -fsanitize=fuzzer,signed-integer-overflow,address -Wno-deprecated -o $@
@ -102,10 +101,10 @@ infer:
infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null
arm: mongoose.h $(SRCS) arm: mongoose.h $(SRCS)
$(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_CUSTOM $(OPTS) $(WARN) $(INCS) $(DEFS) -o unit_test -nostartfiles --specs nosys.specs -e 0 $(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_NEWLIB $(OPTS) $(WARN) $(INCS) $(DEFS) -o unit_test -nostartfiles --specs nosys.specs -e 0
riscv: mongoose.h $(SRCS) riscv: mongoose.h $(SRCS)
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_CUSTOM $(OPTS) $(WARN) $(INCS) $(DEFS) -o unit_test $(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) test/mongoose_custom.c -Itest -DMG_ARCH=MG_ARCH_NEWLIB $(OPTS) $(WARN) $(INCS) $(DEFS) -o unit_test
#vc98: VCFLAGS += -DMG_ENABLE_IPV6=1 #vc98: VCFLAGS += -DMG_ENABLE_IPV6=1
vc98: Makefile mongoose.h $(SRCS) vc98: Makefile mongoose.h $(SRCS)
@ -154,5 +153,5 @@ mongoose.h: $(HDRS) Makefile
(cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@ (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
clean: clean:
rm -rf $(PROG) *.o *.dSYM unit_test* valgrind_unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack rm -rf $(PROG) *.o *.dSYM unit_test* valgrind_unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack unpacked
@for X in $(EXAMPLES); do test -f $$X/Makefile && $(MAKE) -C $$X clean; done @for X in $(EXAMPLES); do test -f $$X/Makefile && $(MAKE) -C $$X clean; done

View File

@ -37,6 +37,7 @@ extern "C" {
#define MG_ARCH_AZURERTOS 7 #define MG_ARCH_AZURERTOS 7
#define MG_ARCH_RTX_LWIP 8 #define MG_ARCH_RTX_LWIP 8
#define MG_ARCH_ZEPHYR 9 #define MG_ARCH_ZEPHYR 9
#define MG_ARCH_NEWLIB 10
#if !defined(MG_ARCH) #if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
@ -79,6 +80,7 @@ extern "C" {
#if MG_ARCH == MG_ARCH_AZURERTOS #if MG_ARCH == MG_ARCH_AZURERTOS
#include <stdarg.h> #include <stdarg.h>
@ -292,6 +294,29 @@ struct timeval {
#endif // MG_ARCH == MG_ARCH_FREERTOS_TCP #endif // MG_ARCH == MG_ARCH_FREERTOS_TCP
#if MG_ARCH == MG_ARCH_NEWLIB
#define _POSIX_TIMERS
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#define MG_PATH_MAX 100
#define MG_ENABLE_SOCKET 0
#define MG_ENABLE_DIRLIST 0
#endif
#if MG_ARCH == MG_ARCH_RTX_LWIP #if MG_ARCH == MG_ARCH_RTX_LWIP
#include <ctype.h> #include <ctype.h>
@ -884,6 +909,7 @@ struct mg_mgr {
uint16_t mqtt_id; // MQTT IDs for pub/sub uint16_t mqtt_id; // MQTT IDs for pub/sub
void *active_dns_requests; // DNS requests in progress void *active_dns_requests; // DNS requests in progress
struct mg_timer *timers; // Active timers struct mg_timer *timers; // Active timers
void *priv; // Used by the experimental stack
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
SocketSet_t ss; // NOTE(lsm): referenced from socket struct SocketSet_t ss; // NOTE(lsm): referenced from socket struct
#endif #endif

View File

@ -10,6 +10,7 @@
#define MG_ARCH_AZURERTOS 7 #define MG_ARCH_AZURERTOS 7
#define MG_ARCH_RTX_LWIP 8 #define MG_ARCH_RTX_LWIP 8
#define MG_ARCH_ZEPHYR 9 #define MG_ARCH_ZEPHYR 9
#define MG_ARCH_NEWLIB 10
#if !defined(MG_ARCH) #if !defined(MG_ARCH)
#if defined(__unix__) || defined(__APPLE__) #if defined(__unix__) || defined(__APPLE__)
@ -47,6 +48,7 @@
#include "arch_esp8266.h" #include "arch_esp8266.h"
#include "arch_freertos_lwip.h" #include "arch_freertos_lwip.h"
#include "arch_freertos_tcp.h" #include "arch_freertos_tcp.h"
#include "arch_newlib.h"
#include "arch_unix.h" #include "arch_unix.h"
#include "arch_win32.h" #include "arch_win32.h"
#include "arch_zephyr.h" #include "arch_zephyr.h"

View File

@ -1,6 +1,6 @@
// Required by the test ARM build
#pragma once #pragma once
#if MG_ARCH == MG_ARCH_NEWLIB
#define _POSIX_TIMERS #define _POSIX_TIMERS
#include <ctype.h> #include <ctype.h>
@ -19,3 +19,5 @@
#define MG_PATH_MAX 100 #define MG_PATH_MAX 100
#define MG_ENABLE_SOCKET 0 #define MG_ENABLE_SOCKET 0
#define MG_ENABLE_DIRLIST 0 #define MG_ENABLE_DIRLIST 0
#endif

View File

@ -29,6 +29,7 @@ struct mg_mgr {
uint16_t mqtt_id; // MQTT IDs for pub/sub uint16_t mqtt_id; // MQTT IDs for pub/sub
void *active_dns_requests; // DNS requests in progress void *active_dns_requests; // DNS requests in progress
struct mg_timer *timers; // Active timers struct mg_timer *timers; // Active timers
void *priv; // Used by the experimental stack
#if MG_ARCH == MG_ARCH_FREERTOS_TCP #if MG_ARCH == MG_ARCH_FREERTOS_TCP
SocketSet_t ss; // NOTE(lsm): referenced from socket struct SocketSet_t ss; // NOTE(lsm): referenced from socket struct
#endif #endif

View File

@ -24,21 +24,12 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#if defined(_WIN32)
#undef popen
#undef pclose
#undef snprintf
#define snprintf _snprintf
#define popen(a, b) _popen((a), (b))
#define pclose(a) _pclose(a)
#endif
static const char *code = static const char *code =
"const char *mg_unpack(const char *path, size_t *size, time_t *mtime);\n"
"const char *mg_unlist(size_t no);\n" "const char *mg_unlist(size_t no);\n"
"const char *mg_unlist(size_t no) {\n" "const char *mg_unlist(size_t no) {\n"
" return packed_files[no].name;\n" " return packed_files[no].name;\n"
"}\n" "}\n"
"const char *mg_unpack(const char *path, size_t *size, time_t *mtime);\n"
"const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n" "const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n"
" const struct packed_file *p;\n" " const struct packed_file *p;\n"
" for (p = packed_files; p->name != NULL; p++) {\n" " for (p = packed_files; p->name != NULL; p++) {\n"
@ -52,7 +43,7 @@ static const char *code =
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int i, j, ch; int i, j, ch;
const char *zip_cmd = NULL, *strip_prefix = ""; const char *strip_prefix = "";
printf("%s", "#include <stddef.h>\n"); printf("%s", "#include <stddef.h>\n");
printf("%s", "#include <string.h>\n"); printf("%s", "#include <string.h>\n");
@ -60,27 +51,16 @@ int main(int argc, char *argv[]) {
printf("%s", "\n"); printf("%s", "\n");
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-z") == 0 && i + 1 < argc) { if (strcmp(argv[i], "-s") == 0) {
zip_cmd = argv[++i];
} else if (strcmp(argv[i], "-s") == 0) {
strip_prefix = argv[++i]; strip_prefix = argv[++i];
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
fprintf(stderr, "Usage: %s [-z ZIP_CMD] [-s STRIP_PREFIX] files...\n", fprintf(stderr, "Usage: %s[-s STRIP_PREFIX] files...\n", argv[0]);
argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else { } else {
char ascii[12], cmd[2048]; char ascii[12];
FILE *fp; FILE *fp = fopen(argv[i], "rb");
if (zip_cmd == NULL) {
fp = fopen(argv[i], "rb");
} else {
snprintf(cmd, sizeof(cmd), "%s %s", zip_cmd, argv[i]);
fp = popen(cmd, "r");
}
if (fp == NULL) { if (fp == NULL) {
fprintf(stderr, "Cannot open [%s]: %s\n", zip_cmd ? cmd : argv[i], fprintf(stderr, "Cannot open [%s]: %s\n", argv[i], strerror(errno));
strerror(errno));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -97,8 +77,7 @@ int main(int argc, char *argv[]) {
// as nul-terminated strings. // as nul-terminated strings.
// printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii); // printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii);
printf(" 0 // %.*s\n};\n", j, ascii); printf(" 0 // %.*s\n};\n", j, ascii);
if (zip_cmd == NULL) fclose(fp); fclose(fp);
if (zip_cmd != NULL) pclose(fp);
} }
} }
@ -107,7 +86,6 @@ int main(int argc, char *argv[]) {
printf("%s", " const unsigned char *data;\n"); printf("%s", " const unsigned char *data;\n");
printf("%s", " size_t size;\n"); printf("%s", " size_t size;\n");
printf("%s", " time_t mtime;\n"); printf("%s", " time_t mtime;\n");
printf("%s", " int zipped;\n");
printf("%s", "} packed_files[] = {\n"); printf("%s", "} packed_files[] = {\n");
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
@ -120,10 +98,9 @@ int main(int argc, char *argv[]) {
} }
stat(argv[i], &st); stat(argv[i], &st);
if (strncmp(name, strip_prefix, n) == 0) name += n; if (strncmp(name, strip_prefix, n) == 0) name += n;
printf(" {\"/%s\", v%d, sizeof(v%d), %lu, %d},\n", name, i, i, st.st_mtime, printf(" {\"/%s\", v%d, sizeof(v%d), %lu},\n", name, i, i, st.st_mtime);
zip_cmd == NULL ? 0 : 1);
} }
printf("%s", " {NULL, NULL, 0, 0, 0}\n"); printf("%s", " {NULL, NULL, 0, 0}\n");
printf("%s", "};\n\n"); printf("%s", "};\n\n");
printf("%s", code); printf("%s", code);

View File

@ -1680,18 +1680,20 @@ static void eh7(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
sopts.fs = &mg_fs_packed; sopts.fs = &mg_fs_packed;
mg_http_serve_dir(c, hm, &sopts); mg_http_serve_dir(c, hm, &sopts);
} }
(void) ev_data, (void) fn_data; (void) fn_data;
} }
static void test_packed(void) { static void test_packed(void) {
struct mg_mgr mgr; struct mg_mgr mgr;
const char *url = "http://127.0.0.1:12351"; const char *url = "http://127.0.0.1:12351";
char buf[FETCH_BUF_SIZE] = "", char buf[FETCH_BUF_SIZE],
*data = mg_file_read(&mg_fs_posix, "Makefile", NULL); *data = mg_file_read(&mg_fs_posix, "Makefile", NULL);
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_http_listen(&mgr, url, eh7, NULL); mg_http_listen(&mgr, url, eh7, NULL);
// Load top level file directly // Load top level file directly
fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n");
printf("---> %s\n", buf);
ASSERT(fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n") == 200); ASSERT(fetch(&mgr, buf, url, "GET /Makefile HTTP/1.0\n\n") == 200);
ASSERT(cmpbody(buf, data) == 0); ASSERT(cmpbody(buf, data) == 0);
free(data); free(data);
@ -1720,7 +1722,7 @@ static void eh6(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
(void) c, (void) ev_data; (void) c, (void) ev_data;
} }
#ifdef __arm__ #if defined(__arm__) || defined(__riscv)
int send(int sock, const void *buf, size_t len, int flags); int send(int sock, const void *buf, size_t len, int flags);
int send(int sock, const void *buf, size_t len, int flags) { int send(int sock, const void *buf, size_t len, int flags) {
(void) sock, (void) buf, (void) len, (void) flags; (void) sock, (void) buf, (void) len, (void) flags;