From a0f9ab5f857c70df48623260646197bf4f269d66 Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Mon, 11 Sep 2023 16:48:26 +0200 Subject: [PATCH] Allow building apps in C++ mode --- samples/clock/build_cpp.sh | 62 +++++++++++++++++++++++++++++++ samples/ui/src/main.c | 16 ++++---- src/graphics/graphics.h | 7 ++++ src/libc-shim/include/string.h | 4 +- src/libc-shim/src/string.c | 6 +-- src/platform/platform.h | 6 ++- src/platform/platform_debug.h | 8 ++++ src/platform/platform_io.h | 10 ++++- src/platform/platform_io_dialog.h | 8 ++++ src/platform/platform_path.h | 10 ++++- src/ui/input_state.h | 8 ++++ src/ui/ui.h | 1 + src/util/algebra.h | 8 ++++ src/util/debug.h | 8 ++++ src/util/macros.h | 12 +++--- src/util/strings.h | 8 ++-- 16 files changed, 156 insertions(+), 26 deletions(-) create mode 100755 samples/clock/build_cpp.sh diff --git a/samples/clock/build_cpp.sh b/samples/clock/build_cpp.sh new file mode 100755 index 0000000..dfd5f5d --- /dev/null +++ b/samples/clock/build_cpp.sh @@ -0,0 +1,62 @@ +#!/bin/bash + +set -euo pipefail + +if [[ -x /usr/local/opt/llvm/bin/clang ]]; then + CLANG=/usr/local/opt/llvm/bin/clang +elif [[ -x /opt/homebrew/opt/llvm/bin/clang ]]; then + CLANG=/opt/homebrew/opt/llvm/bin/clang +else + echo "Could not find Homebrew clang; this script will probably not work." + CLANG=clang +fi + +if [[ -x /usr/local/opt/llvm/bin/clang++ ]]; then + CLANGPP=/usr/local/opt/llvm/bin/clang++ +elif [[ -x /opt/homebrew/opt/llvm/bin/clang++ ]]; then + CLANGPP=/opt/homebrew/opt/llvm/bin/clang++ +else + echo "Could not find Homebrew clang++; this script will probably not work." + CLANGPP=clang++ +fi + +ORCA_DIR=../.. +STDLIB_DIR=../../src/libc-shim + +wasmObjFlags="--target=wasm32 \ + -g \ + -O2 \ + -mbulk-memory \ + -D__ORCA__ \ + -I $ORCA_DIR/ext \ + -I $STDLIB_DIR/include \ + -I $ORCA_DIR/ext \ + -I $ORCA_DIR/src" + +wasmFlags="--target=wasm32 \ + --no-standard-libraries \ + -Wl,--no-entry \ + -Wl,--export-dynamic \ + -g \ + -O2 \ + -mbulk-memory \ + -D__ORCA__ \ + -I $ORCA_DIR/ext \ + -I $STDLIB_DIR/include \ + -I $ORCA_DIR/ext \ + -I $ORCA_DIR/src" + +if [ ! -e build ] ; then + mkdir build +fi + +$CLANG $wasmObjFlags -c -o ./build/orca.o ../../src/orca.c +for file in $STDLIB_DIR/src/*.c ; do + name=$(basename $file) + name=${name%.c} + $CLANG $wasmObjFlags -c -o ./build/$name.o $file +done + +$CLANGPP $wasmFlags -o ./module.wasm src/main.c ./build/*.o + +orca bundle --orca-dir ../.. --name Clock --resource-dir data module.wasm diff --git a/samples/ui/src/main.c b/samples/ui/src/main.c index 616e934..32dc6ae 100644 --- a/samples/ui/src/main.c +++ b/samples/ui/src/main.c @@ -31,7 +31,7 @@ ORCA_EXPORT void oc_on_init(void) oc_log_error("Couldn't open file OpenSansLatinSubset.ttf\n"); } u64 size = oc_file_size(file); - char* buffer = oc_arena_push(oc_scratch(), size); + char* buffer = (char*)oc_arena_push(oc_scratch(), size); oc_file_read(file, size, buffer); oc_file_close(file); oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN, @@ -95,7 +95,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) .layout.axis = OC_UI_AXIS_Y, .layout.align.x = OC_UI_ALIGN_CENTER, .layout.align.y = OC_UI_ALIGN_START, - .layout.spacing = 10}, + .layout.spacing = 10 }, OC_UI_STYLE_SIZE | OC_UI_STYLE_LAYOUT); @@ -109,7 +109,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) OC_UI_STYLE_SIZE | OC_UI_STYLE_LAYOUT_ALIGN_X | OC_UI_STYLE_LAYOUT_MARGINS); - oc_ui_container("title", 0) + oc_ui_container("title", OC_UI_FLAG_NONE) { oc_ui_style_next(&(oc_ui_style){ .fontSize = 26 }, OC_UI_STYLE_FONT_SIZE); oc_ui_label("Orca UI Demo"); @@ -157,13 +157,13 @@ ORCA_EXPORT void oc_on_frame_refresh(void) OC_UI_STYLE_SIZE | OC_UI_STYLE_LAYOUT_MARGINS); oc_ui_style_next(&(oc_ui_style){ .layout.axis = OC_UI_AXIS_X }, OC_UI_STYLE_LAYOUT_AXIS); - oc_ui_container("contents", 0) + oc_ui_container("contents", OC_UI_FLAG_NONE) { oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 0.5 }, .size.height = { OC_UI_SIZE_PARENT, 1 } }, OC_UI_STYLE_SIZE); - oc_ui_container("left", 0) + oc_ui_container("left", OC_UI_FLAG_NONE) { oc_ui_style_next(&(oc_ui_style){ .layout.axis = OC_UI_AXIS_X, .layout.spacing = 10, @@ -177,7 +177,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) | OC_UI_STYLE_LAYOUT_MARGIN_Y | OC_UI_STYLE_SIZE); - oc_ui_container("up", 0) + oc_ui_container("up", OC_UI_FLAG_NONE) { oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 0.5 }, .size.height = { OC_UI_SIZE_PARENT, 1 } }, @@ -222,7 +222,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) OC_UI_STYLE_LAYOUT_AXIS | OC_UI_STYLE_SIZE); - oc_ui_container("down", 0) + oc_ui_container("down", OC_UI_FLAG_NONE) { widget_view("Vertical Sliders") { @@ -230,7 +230,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) .layout.spacing = 10 }, OC_UI_STYLE_LAYOUT_AXIS | OC_UI_STYLE_LAYOUT_SPACING); - oc_ui_container("contents", 0) + oc_ui_container("contents", OC_UI_FLAG_NONE) { oc_ui_style_next(&(oc_ui_style){ .size.height = { OC_UI_SIZE_PIXELS, 200 } }, OC_UI_STYLE_SIZE_HEIGHT); diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 7d6f797..fadae40 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -12,6 +12,9 @@ #include "platform/platform.h" #include "util/typedefs.h" +#ifdef __cplusplus +extern "C" { +#endif //------------------------------------------------------------------------------------------ //SECTION: backends selection //------------------------------------------------------------------------------------------ @@ -364,4 +367,8 @@ ORCA_API void oc_text_fill(f32 x, f32 y, oc_str8 text); ORCA_API void oc_image_draw(oc_image image, oc_rect rect); ORCA_API void oc_image_draw_region(oc_image image, oc_rect srcRegion, oc_rect dstRegion); +#ifdef __cplusplus +} +#endif + #endif //__GRAPHICS_H_ diff --git a/src/libc-shim/include/string.h b/src/libc-shim/include/string.h index dc719bb..5204d64 100644 --- a/src/libc-shim/include/string.h +++ b/src/libc-shim/include/string.h @@ -1,13 +1,13 @@ #include "stb/stb_sprintf.h" void* memset(void* b, int c, size_t n); -void* memcpy(void* restrict dst, const void* restrict src, size_t n); +void* memcpy(void* __restrict dst, const void* __restrict src, size_t n); void* memmove(void* dst, const void* src, size_t n); int memcmp(const void* s1, const void* s2, size_t n); size_t strlen(const char* s); int strcmp(const char* s1, const char* s2); int strncmp(const char* s1, const char* s2, size_t n); -char* strcpy(char* restrict s1, const char* restrict s2); +char* strcpy(char* __restrict s1, const char* __restrict s2); #define vsnprintf stbsp_vsnprintf diff --git a/src/libc-shim/src/string.c b/src/libc-shim/src/string.c index 0597757..a108e22 100644 --- a/src/libc-shim/src/string.c +++ b/src/libc-shim/src/string.c @@ -12,7 +12,7 @@ void* memset(void* b, int c, size_t n) return (__builtin_memset(b, c, n)); } -void* memcpy(void* restrict dst, const void* restrict src, size_t n) +void* memcpy(void* __restrict dst, const void* __restrict src, size_t n) { return (__builtin_memcpy(dst, src, n)); } @@ -92,7 +92,7 @@ int strncmp(const char* s1, const char* s2, size_t n) return (res); } -char* strcpy(char* restrict s1, const char* restrict s2) +char* strcpy(char* __restrict s1, const char* __restrict s2) { size_t i = 0; while(s2[i] != '\0') @@ -104,7 +104,7 @@ char* strcpy(char* restrict s1, const char* restrict s2) return (s1); } -char* strncpy(char* restrict s1, const char* restrict s2, size_t len) +char* strncpy(char* __restrict s1, const char* __restrict s2, size_t len) { size_t i = 0; while(i < len && s2[i] != '\0') diff --git a/src/platform/platform.h b/src/platform/platform.h index d77d805..3ec268c 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -101,7 +101,11 @@ #define ORCA_IMPORT(f) __attribute__((import_name(#f))) f #if OC_COMPILER_CLANG - #define ORCA_EXPORT __attribute__((visibility("default"))) + #ifdef __cplusplus + #define ORCA_EXPORT __attribute__((visibility("default"))) extern "C" + #else + #define ORCA_EXPORT __attribute__((visibility("default"))) + #endif #else #error "Orca apps can only be compiled with clang for now" #endif diff --git a/src/platform/platform_debug.h b/src/platform/platform_debug.h index c8bba96..b7500bb 100644 --- a/src/platform/platform_debug.h +++ b/src/platform/platform_debug.h @@ -10,6 +10,10 @@ #include "platform.h" +#ifdef __cplusplus +extern "C" { +#endif + //---------------------------------------------------------------- // Assert / Abort //---------------------------------------------------------------- @@ -42,4 +46,8 @@ ORCA_API void oc_log_ext(oc_log_level level, const char* fmt, ...); +#ifdef __cplusplus +} +#endif + #endif //__PLATFORM_DEBUG_H_ diff --git a/src/platform/platform_io.h b/src/platform/platform_io.h index ac18203..53675c1 100644 --- a/src/platform/platform_io.h +++ b/src/platform/platform_io.h @@ -11,6 +11,10 @@ #include "util/strings.h" #include "util/typedefs.h" +#ifdef __cplusplus +extern "C" { +#endif + //---------------------------------------------------------------- // IO API //---------------------------------------------------------------- @@ -187,7 +191,7 @@ typedef enum oc_file_type typedef u16 oc_file_perm; -enum oc_file_perm +enum oc_file_perm_enum { OC_FILE_OTHER_EXEC = 1 << 0, OC_FILE_OTHER_WRITE = 1 << 1, @@ -237,4 +241,8 @@ ORCA_API u64 oc_file_size(oc_file file); ORCA_API oc_file oc_file_open_with_request(oc_str8 path, oc_file_access rights, oc_file_open_flags flags); +#ifdef __cplusplus +} +#endif + #endif //__PLATFORM_IO_H_ diff --git a/src/platform/platform_io_dialog.h b/src/platform/platform_io_dialog.h index d49c868..37adc6f 100644 --- a/src/platform/platform_io_dialog.h +++ b/src/platform/platform_io_dialog.h @@ -11,6 +11,10 @@ #include "platform_io.h" #include "app/app.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct oc_file_open_with_dialog_elt { oc_list_elt listElt; @@ -26,4 +30,8 @@ typedef struct oc_file_open_with_dialog_result ORCA_API oc_file_open_with_dialog_result oc_file_open_with_dialog(oc_arena* arena, oc_file_access rights, oc_file_open_flags flags, oc_file_dialog_desc* desc); +#ifdef __cplusplus +} +#endif + #endif //__PLATFORM_IO_DIALOG_H_ diff --git a/src/platform/platform_path.h b/src/platform/platform_path.h index d9f6f3a..fa22966 100644 --- a/src/platform/platform_path.h +++ b/src/platform/platform_path.h @@ -8,9 +8,13 @@ #ifndef __PLATFORM_PATH_H_ #define __PLATFORM_PATH_H_ -#include"platform.h" +#include "platform.h" #include "util/strings.h" +#ifdef __cplusplus +extern "C" { +#endif + /*NOTE: by convention, functions that take an arena and return a path allocated on that arena allocate 1 more character and null-terminate @@ -35,4 +39,8 @@ ORCA_API oc_str8 oc_path_canonical(oc_arena* arena, oc_str8 path); ORCA_API oc_str8 oc_path_executable_relative(oc_arena* arena, oc_str8 relPath); #endif +#ifdef __cplusplus +} +#endif + #endif //__PLATFORM_PATH_H_ diff --git a/src/ui/input_state.h b/src/ui/input_state.h index 2e034b1..c476f51 100644 --- a/src/ui/input_state.h +++ b/src/ui/input_state.h @@ -14,6 +14,10 @@ #include "util/typedefs.h" #include "util/utf8.h" +#ifdef __cplusplus +extern "C" { +#endif + typedef struct oc_key_state { u64 lastUpdate; @@ -113,4 +117,8 @@ ORCA_API oc_str8 oc_clipboard_pasted_text(oc_input_state* state); ORCA_API oc_keymod_flags oc_key_mods(oc_input_state* state); +#ifdef __cplusplus +} +#endif + #endif //__INPUT_STATE_H_ diff --git a/src/ui/ui.h b/src/ui/ui.h index 259ada1..65e4ace 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -464,6 +464,7 @@ typedef void (*oc_ui_box_draw_proc)(oc_ui_box* box, void* data); typedef enum { + OC_UI_FLAG_NONE = 0, OC_UI_FLAG_CLICKABLE = (1 << 0), OC_UI_FLAG_SCROLL_WHEEL_X = (1 << 1), OC_UI_FLAG_SCROLL_WHEEL_Y = (1 << 2), diff --git a/src/util/algebra.h b/src/util/algebra.h index baa5f97..d5f3088 100644 --- a/src/util/algebra.h +++ b/src/util/algebra.h @@ -10,6 +10,10 @@ #include "typedefs.h" +#ifdef __cplusplus +extern "C" { +#endif + bool oc_vec2_equal(oc_vec2 v0, oc_vec2 v1); oc_vec2 oc_vec2_mul(f32 f, oc_vec2 v); oc_vec2 oc_vec2_add(oc_vec2 v0, oc_vec2 v1); @@ -23,4 +27,8 @@ oc_mat2x3 oc_mat2x3_translate(f32 x, f32 y); //TODO: complete +#ifdef __cplusplus +} +#endif + #endif //__ALGEBRA_H_ diff --git a/src/util/debug.h b/src/util/debug.h index 0170a5b..bcedf8e 100644 --- a/src/util/debug.h +++ b/src/util/debug.h @@ -11,6 +11,10 @@ #include "platform/platform_debug.h" #include "util/macros.h" +#ifdef __cplusplus +extern "C" { +#endif + //---------------------------------------------------------------- // Logging //---------------------------------------------------------------- @@ -59,4 +63,8 @@ #endif #endif +#ifdef __cplusplus +} +#endif + #endif //__DEBUG_H_ diff --git a/src/util/macros.h b/src/util/macros.h index 0b2bf41..91a31ef 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -77,19 +77,19 @@ static inline u64 oc_next_pow2(u64 x) //NOTE(martin): 'hygienic' max/min/square/cube macros. #ifdef __cplusplus - //NOTE(martin): in C++ we use templates and decltype/declval - // (overloaded functions would be ambiguous because of the - // overload resolution and conversion/promotion rules) - #include +//NOTE(martin): in C++ we use templates and decltype/declval +// (overloaded functions would be ambiguous because of the +// overload resolution and conversion/promotion rules) +// #include template -inline decltype(std::declval() + std::declval()) oc_min(Ta a, Tb b) +inline decltype(Ta() + Tb()) oc_min(Ta a, Tb b) { return (a < b ? a : b); } template -inline decltype(std::declval() + std::declval()) oc_max(Ta a, Tb b) +inline decltype(Ta() + Tb()) oc_max(Ta a, Tb b) { return (a > b ? a : b); } diff --git a/src/util/strings.h b/src/util/strings.h index 693a226..46fb08d 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -38,12 +38,12 @@ typedef struct oc_str8 size_t len; } oc_str8; -#define OC_STR8(s) ((oc_str8){.ptr = (char*)s , .len = (s) ? strlen(s) : 0}) +#define OC_STR8(s) ((oc_str8){ .ptr = (char*)s, .len = (s) ? strlen(s) : 0 }) //NOTE: this only works with string literals, but is sometimes necessary to generate compile-time constants -#define OC_STR8_LIT(s) \ - { \ - s, sizeof(s) - 1 \ +#define OC_STR8_LIT(s) \ + { \ + (char*)(s), sizeof(s) - 1 \ } #define oc_str8_lp(s) ((s).len), ((s).ptr) -- 2.25.1