From 1d9f236dafaddf16e25ebb4806014ae823fc25ee Mon Sep 17 00:00:00 2001 From: Miguel Lechon Date: Thu, 21 Jan 2021 18:03:24 +0100 Subject: [PATCH 1/3] Tweaks to make clang happy. --- source/md_impl.c | 23 +++++++++++++++-------- source/md_malloc.c | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/source/md_impl.c b/source/md_impl.c index 6d72dce..602c1c2 100644 --- a/source/md_impl.c +++ b/source/md_impl.c @@ -989,7 +989,8 @@ MD_NodeTable_Insert(MD_NodeTable *table, MD_NodeTableCollisionRule collision_rul MD_FUNCTION_IMPL MD_Token MD_ZeroToken(void) { - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); return token; } @@ -1062,7 +1063,8 @@ MD_Parse_BumpNext(MD_ParseCtx *ctx) MD_FUNCTION_IMPL MD_Token MD_Parse_LexNext(MD_ParseCtx *ctx) { - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); MD_u8 *one_past_last = ctx->file_contents.str + ctx->file_contents.size; MD_u8 *first = ctx->at; @@ -1237,7 +1239,8 @@ MD_Parse_PeekSkipSome(MD_ParseCtx *ctx, MD_TokenGroups skip_groups) MD_b32 skip_whitespace = (skip_groups & MD_TokenGroup_Whitespace); MD_b32 skip_regular = (skip_groups & MD_TokenGroup_Regular); - MD_Token result = {0}; + MD_Token result; + _MD_MemoryZero(&result, sizeof(result)); loop: { @@ -1270,6 +1273,7 @@ MD_Parse_Require(MD_ParseCtx *ctx, MD_String8 string) int result = 0; MD_Token token_any = MD_Parse_PeekSkipSome(ctx, 0); + MD_Token token_regular; if(MD_StringMatch(token_any.string, string, 0)) { result = 1; @@ -1277,7 +1281,7 @@ MD_Parse_Require(MD_ParseCtx *ctx, MD_String8 string) goto end; } - MD_Token token_regular = MD_Parse_PeekSkipSome(ctx, MD_TokenGroup_Comment|MD_TokenGroup_Whitespace); + token_regular = MD_Parse_PeekSkipSome(ctx, MD_TokenGroup_Comment|MD_TokenGroup_Whitespace); if(MD_StringMatch(token_regular.string, string, 0)) { result = 1; @@ -1394,7 +1398,8 @@ _MD_ParseOneNode(MD_ParseCtx *ctx) MD_ParseResult result = {0}; result.node = MD_NilNode(); - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); MD_Node *first_tag = 0; MD_Node *last_tag = 0; @@ -1572,7 +1577,8 @@ _MD_ParseTagList(MD_ParseCtx *ctx, MD_Node **first_out, MD_Node **last_out) for(;MD_Parse_Require(ctx, MD_S8Lit("@"));) { - MD_Token name = {0}; + MD_Token name; + _MD_MemoryZero(&name, sizeof(name)); if(MD_Parse_RequireKind(ctx, MD_TokenKind_Identifier, &name)) { MD_Node *tag = _MD_MakeNodeFromToken_Ctx(ctx, MD_NodeKind_Tag, name); @@ -1903,7 +1909,7 @@ MD_TagCountFromNodeAndString(MD_Node *node, MD_String8 string, MD_StringMatchFla MD_FUNCTION_IMPL void MD_NodeMessage(MD_Node *node, MD_MessageKind kind, MD_String8 str) { - char *kind_name = kind == MD_MessageKind_Error ? "error" : "warning"; + const char *kind_name = kind == MD_MessageKind_Error ? "error" : "warning"; MD_CodeLoc loc = MD_CodeLocFromNode(node); fprintf(stderr, "%.*s(%i:%i): %s: %.*s\n", MD_StringExpand(loc.filename), @@ -2265,12 +2271,13 @@ MD_PRIVATE_FUNCTION_IMPL MD_Expr * _MD_ParseExpr_(_MD_NodeParseCtx *ctx, int precedence_in) { MD_Expr *expr = _MD_ParseUnaryExpr(ctx); + MD_ExprKind expr_kind; if(MD_ExprIsNil(expr)) { goto end_parse; } - MD_ExprKind expr_kind = MD_BinaryExprKindFromNode(ctx->at); + expr_kind = MD_BinaryExprKindFromNode(ctx->at); if(expr_kind != MD_ExprKind_Nil) { for(int precedence = MD_ExprPrecFromExprKind(expr_kind); diff --git a/source/md_malloc.c b/source/md_malloc.c index bcda86d..b3d59ec 100644 --- a/source/md_malloc.c +++ b/source/md_malloc.c @@ -15,5 +15,5 @@ MD_MALLOC_Alloc(void *ctx, MD_u64 size) static void* MD_MALLOC_GetCtx(void) { - return(MD_MALLOC_Alloc); + return((void *)MD_MALLOC_Alloc); } From 5ff1c5e0a505790b5c5024f423d1086f0b3268ab Mon Sep 17 00:00:00 2001 From: Miguel Lechon Date: Thu, 21 Jan 2021 18:08:03 +0100 Subject: [PATCH 2/3] Windows/Linux clang build scripts. --- build.sh | 50 +++++++++++++++++++++++++++++++++++++++++++ build_with_clang.bat | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100755 build.sh create mode 100755 build_with_clang.bat diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..c2adb2c --- /dev/null +++ b/build.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +CC=clang + +# NOTE(mal): Silent pushd/popd +pushd () { + command pushd "$@" > /dev/null +} +popd () { + command popd "$@" > /dev/null +} + +echo ~~~ Metadesk Build ~~~ +# TODO(mal): Review these warnings +accepted_clang_warnings="-Wno-return-type -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option" +compile_flags="-I../source/ $accepted_clang_warnings" + +mkdir -p build +pushd build +echo +echo ~~~ Build All Samples ~~~ +$CC $compile_flags ../samples/old_style_custom_layer.c -o old_style_custom_layer +$CC $compile_flags ../samples/static_site_generator/static_site_generator.c -o static_site_generator +$CC $compile_flags ../samples/output_parse/output_parse.c -o output_parse +echo +echo ~~~ Build All Tests ~~~ +$CC $compile_flags ../tests/sanity_tests.c -o sanity_tests +$CC $compile_flags ../tests/unicode_test.c -o unicode_test +clang++ $compile_flags ../tests/cpp_build_test.cpp +popd + +echo +echo ~~~ Running Sanity Tests ~~~ +pushd build +sanity_tests +popd + +echo +echo ~~~ Running Static Site Generator Sample ~~~ +mkdir -p samples/static_site_generator/example_site/generated +pushd samples/static_site_generator/example_site/generated +../../../../build/static_site_generator --siteinfo ../site_info.md --pagedir ../ +popd + +echo +echo ~~~ Running Output Parse Sample ~~~ +mkdir -p samples/output_parse/examples/output +pushd samples/output_parse/examples/output +../../../../build/output_parse ../example.md ../example2.md +popd diff --git a/build_with_clang.bat b/build_with_clang.bat new file mode 100755 index 0000000..e292eb9 --- /dev/null +++ b/build_with_clang.bat @@ -0,0 +1,51 @@ +@echo off + +echo ~~~ Metadesk Build ~~~ +set accepted_clang_warnings=-Wno-return-type -Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option +set compile_flags=-I../source/ %accepted_clang_warnings% + +if not exist build mkdir build +pushd build +echo. +echo ~~~ Build All Samples ~~~ +clang %compile_flags% ..\samples\old_style_custom_layer.c -o old_style_custom_layer.exe +clang %compile_flags% ..\samples\static_site_generator\static_site_generator.c -o static_site_generator.exe +clang %compile_flags% ..\samples\output_parse\output_parse.c -o output_parse.exe +echo. +echo ~~~ Build All Tests ~~~ +clang %compile_flags% ..\tests\sanity_tests.c -o sanity_tests.exe +clang %compile_flags% ..\tests\unicode_test.c -o unicode_test.exe +clang++ %compile_flags% ..\tests\cpp_build_test.cpp -o cpp_build_test.exe +popd + +echo. +echo ~~~ Running Sanity Tests ~~~ +pushd build +sanity_tests.exe +popd + +echo. +echo ~~~ Running Static Site Generator Sample ~~~ +pushd samples +pushd static_site_generator +pushd example_site +if not exist generated mkdir generated +pushd generated +..\..\..\..\build\static_site_generator.exe --siteinfo ..\site_info.md --pagedir ..\ +popd +popd +popd +popd + +echo. +echo ~~~ Running Output Parse Sample ~~~ +pushd samples +pushd output_parse +pushd examples +if not exist output mkdir output +pushd output +..\..\..\..\build\output_parse.exe ..\example.md ..\example2.md +popd +popd +popd +popd From 4d2565b3f319270adf11dbe4cd19c563877df066 Mon Sep 17 00:00:00 2001 From: Miguel Lechon Date: Thu, 21 Jan 2021 20:09:11 +0100 Subject: [PATCH 3/3] POSIX directory listing. --- build.sh | 2 +- build_with_clang.bat | 3 ++- source/md_posix.c | 54 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 5 deletions(-) mode change 100755 => 100644 build_with_clang.bat diff --git a/build.sh b/build.sh index c2adb2c..464bc20 100755 --- a/build.sh +++ b/build.sh @@ -12,7 +12,7 @@ popd () { echo ~~~ Metadesk Build ~~~ # TODO(mal): Review these warnings -accepted_clang_warnings="-Wno-return-type -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option" +accepted_clang_warnings="-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option" compile_flags="-I../source/ $accepted_clang_warnings" mkdir -p build diff --git a/build_with_clang.bat b/build_with_clang.bat old mode 100755 new mode 100644 index e292eb9..96090c9 --- a/build_with_clang.bat +++ b/build_with_clang.bat @@ -1,7 +1,8 @@ @echo off echo ~~~ Metadesk Build ~~~ -set accepted_clang_warnings=-Wno-return-type -Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option +rem TODO(mal): Review these warnings +set accepted_clang_warnings=-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option set compile_flags=-I../source/ %accepted_clang_warnings% if not exist build mkdir build diff --git a/source/md_posix.c b/source/md_posix.c index 52b3483..86ebc0c 100644 --- a/source/md_posix.c +++ b/source/md_posix.c @@ -1,5 +1,53 @@ +#include +#include + +#define MD_IMPL_FileIterIncrement MD_POSIX_FileIterIncrement + static MD_b32 -_MD_OS_IMPL_FileIter_Increment(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info) +MD_POSIX_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info) { - -} \ No newline at end of file + MD_b32 result = 0; + + DIR *dir = (DIR *)(it->state); + if(dir == 0) + { + dir = opendir((char*)path.str); + } + + struct dirent *dir_entry = 0; + if(dir && (dir_entry = readdir(dir))) + { + out_info->filename = MD_PushStringF("%s", dir_entry->d_name); + out_info->flags = 0; + + if(path.size > 1 && path.str[path.size-1] == '/') + { + path.size -= 1; + } + MD_String8 cfile_path = MD_PushStringF("%.*s/%s", MD_StringExpand(path), dir_entry->d_name); + + // NOTE(mal): On Linux, fstatat(2) would save us from doing path manipulation and avoid some race + // conditions but we would need to make space for an extra directory file descriptor + // inside MD_FileIter struct. We would also stop being POSIX-compliant. + struct stat st; + if(stat((char *)cfile_path.str, &st) == 0) + { + if((st.st_mode & S_IFMT) == S_IFDIR) + { + out_info->flags |= MD_FileFlag_Directory; + } + out_info->file_size = st.st_size; + } + result = 1; + } + else + { + closedir(dir); + dir = 0; + result = 0; + } + + it->state = *(MD_u64 *)(&dir); + + return result; +}