Allow building apps in C++ mode

This commit is contained in:
Martin Fouilleul 2023-09-11 16:48:26 +02:00 committed by MartinFouilleul
parent 0f3f7b5052
commit a0f9ab5f85
16 changed files with 156 additions and 26 deletions

62
samples/clock/build_cpp.sh Executable file
View File

@ -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

View File

@ -31,7 +31,7 @@ ORCA_EXPORT void oc_on_init(void)
oc_log_error("Couldn't open file OpenSansLatinSubset.ttf\n"); oc_log_error("Couldn't open file OpenSansLatinSubset.ttf\n");
} }
u64 size = oc_file_size(file); 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_read(file, size, buffer);
oc_file_close(file); oc_file_close(file);
oc_unicode_range ranges[5] = { OC_UNICODE_BASIC_LATIN, 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.axis = OC_UI_AXIS_Y,
.layout.align.x = OC_UI_ALIGN_CENTER, .layout.align.x = OC_UI_ALIGN_CENTER,
.layout.align.y = OC_UI_ALIGN_START, .layout.align.y = OC_UI_ALIGN_START,
.layout.spacing = 10}, .layout.spacing = 10 },
OC_UI_STYLE_SIZE OC_UI_STYLE_SIZE
| OC_UI_STYLE_LAYOUT); | OC_UI_STYLE_LAYOUT);
@ -109,7 +109,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
OC_UI_STYLE_SIZE OC_UI_STYLE_SIZE
| OC_UI_STYLE_LAYOUT_ALIGN_X | OC_UI_STYLE_LAYOUT_ALIGN_X
| OC_UI_STYLE_LAYOUT_MARGINS); | 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_style_next(&(oc_ui_style){ .fontSize = 26 }, OC_UI_STYLE_FONT_SIZE);
oc_ui_label("Orca UI Demo"); 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_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_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 }, oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 0.5 },
.size.height = { OC_UI_SIZE_PARENT, 1 } }, .size.height = { OC_UI_SIZE_PARENT, 1 } },
OC_UI_STYLE_SIZE); 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, oc_ui_style_next(&(oc_ui_style){ .layout.axis = OC_UI_AXIS_X,
.layout.spacing = 10, .layout.spacing = 10,
@ -177,7 +177,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
| OC_UI_STYLE_LAYOUT_MARGIN_Y | OC_UI_STYLE_LAYOUT_MARGIN_Y
| OC_UI_STYLE_SIZE); | 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 }, oc_ui_style_next(&(oc_ui_style){ .size.width = { OC_UI_SIZE_PARENT, 0.5 },
.size.height = { OC_UI_SIZE_PARENT, 1 } }, .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_LAYOUT_AXIS
| OC_UI_STYLE_SIZE); | OC_UI_STYLE_SIZE);
oc_ui_container("down", 0) oc_ui_container("down", OC_UI_FLAG_NONE)
{ {
widget_view("Vertical Sliders") widget_view("Vertical Sliders")
{ {
@ -230,7 +230,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
.layout.spacing = 10 }, .layout.spacing = 10 },
OC_UI_STYLE_LAYOUT_AXIS OC_UI_STYLE_LAYOUT_AXIS
| OC_UI_STYLE_LAYOUT_SPACING); | 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_next(&(oc_ui_style){ .size.height = { OC_UI_SIZE_PIXELS, 200 } },
OC_UI_STYLE_SIZE_HEIGHT); OC_UI_STYLE_SIZE_HEIGHT);

View File

@ -12,6 +12,9 @@
#include "platform/platform.h" #include "platform/platform.h"
#include "util/typedefs.h" #include "util/typedefs.h"
#ifdef __cplusplus
extern "C" {
#endif
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//SECTION: backends selection //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(oc_image image, oc_rect rect);
ORCA_API void oc_image_draw_region(oc_image image, oc_rect srcRegion, oc_rect dstRegion); ORCA_API void oc_image_draw_region(oc_image image, oc_rect srcRegion, oc_rect dstRegion);
#ifdef __cplusplus
}
#endif
#endif //__GRAPHICS_H_ #endif //__GRAPHICS_H_

View File

@ -1,13 +1,13 @@
#include "stb/stb_sprintf.h" #include "stb/stb_sprintf.h"
void* memset(void* b, int c, size_t n); 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); void* memmove(void* dst, const void* src, size_t n);
int memcmp(const void* s1, const void* s2, size_t n); int memcmp(const void* s1, const void* s2, size_t n);
size_t strlen(const char* s); size_t strlen(const char* s);
int strcmp(const char* s1, const char* s2); int strcmp(const char* s1, const char* s2);
int strncmp(const char* s1, const char* s2, size_t n); 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 #define vsnprintf stbsp_vsnprintf

View File

@ -12,7 +12,7 @@ void* memset(void* b, int c, size_t n)
return (__builtin_memset(b, c, 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)); return (__builtin_memcpy(dst, src, n));
} }
@ -92,7 +92,7 @@ int strncmp(const char* s1, const char* s2, size_t n)
return (res); return (res);
} }
char* strcpy(char* restrict s1, const char* restrict s2) char* strcpy(char* __restrict s1, const char* __restrict s2)
{ {
size_t i = 0; size_t i = 0;
while(s2[i] != '\0') while(s2[i] != '\0')
@ -104,7 +104,7 @@ char* strcpy(char* restrict s1, const char* restrict s2)
return (s1); 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; size_t i = 0;
while(i < len && s2[i] != '\0') while(i < len && s2[i] != '\0')

View File

@ -101,7 +101,11 @@
#define ORCA_IMPORT(f) __attribute__((import_name(#f))) f #define ORCA_IMPORT(f) __attribute__((import_name(#f))) f
#if OC_COMPILER_CLANG #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 #else
#error "Orca apps can only be compiled with clang for now" #error "Orca apps can only be compiled with clang for now"
#endif #endif

View File

@ -10,6 +10,10 @@
#include "platform.h" #include "platform.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------- //----------------------------------------------------------------
// Assert / Abort // Assert / Abort
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -42,4 +46,8 @@ ORCA_API void oc_log_ext(oc_log_level level,
const char* fmt, const char* fmt,
...); ...);
#ifdef __cplusplus
}
#endif
#endif //__PLATFORM_DEBUG_H_ #endif //__PLATFORM_DEBUG_H_

View File

@ -11,6 +11,10 @@
#include "util/strings.h" #include "util/strings.h"
#include "util/typedefs.h" #include "util/typedefs.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------- //----------------------------------------------------------------
// IO API // IO API
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -187,7 +191,7 @@ typedef enum oc_file_type
typedef u16 oc_file_perm; typedef u16 oc_file_perm;
enum oc_file_perm enum oc_file_perm_enum
{ {
OC_FILE_OTHER_EXEC = 1 << 0, OC_FILE_OTHER_EXEC = 1 << 0,
OC_FILE_OTHER_WRITE = 1 << 1, 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); 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_ #endif //__PLATFORM_IO_H_

View File

@ -11,6 +11,10 @@
#include "platform_io.h" #include "platform_io.h"
#include "app/app.h" #include "app/app.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct oc_file_open_with_dialog_elt typedef struct oc_file_open_with_dialog_elt
{ {
oc_list_elt listElt; 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); 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_ #endif //__PLATFORM_IO_DIALOG_H_

View File

@ -8,9 +8,13 @@
#ifndef __PLATFORM_PATH_H_ #ifndef __PLATFORM_PATH_H_
#define __PLATFORM_PATH_H_ #define __PLATFORM_PATH_H_
#include"platform.h" #include "platform.h"
#include "util/strings.h" #include "util/strings.h"
#ifdef __cplusplus
extern "C" {
#endif
/*NOTE: /*NOTE:
by convention, functions that take an arena and return a path by convention, functions that take an arena and return a path
allocated on that arena allocate 1 more character and null-terminate 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); ORCA_API oc_str8 oc_path_executable_relative(oc_arena* arena, oc_str8 relPath);
#endif #endif
#ifdef __cplusplus
}
#endif
#endif //__PLATFORM_PATH_H_ #endif //__PLATFORM_PATH_H_

View File

@ -14,6 +14,10 @@
#include "util/typedefs.h" #include "util/typedefs.h"
#include "util/utf8.h" #include "util/utf8.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct oc_key_state typedef struct oc_key_state
{ {
u64 lastUpdate; 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); ORCA_API oc_keymod_flags oc_key_mods(oc_input_state* state);
#ifdef __cplusplus
}
#endif
#endif //__INPUT_STATE_H_ #endif //__INPUT_STATE_H_

View File

@ -464,6 +464,7 @@ typedef void (*oc_ui_box_draw_proc)(oc_ui_box* box, void* data);
typedef enum typedef enum
{ {
OC_UI_FLAG_NONE = 0,
OC_UI_FLAG_CLICKABLE = (1 << 0), OC_UI_FLAG_CLICKABLE = (1 << 0),
OC_UI_FLAG_SCROLL_WHEEL_X = (1 << 1), OC_UI_FLAG_SCROLL_WHEEL_X = (1 << 1),
OC_UI_FLAG_SCROLL_WHEEL_Y = (1 << 2), OC_UI_FLAG_SCROLL_WHEEL_Y = (1 << 2),

View File

@ -10,6 +10,10 @@
#include "typedefs.h" #include "typedefs.h"
#ifdef __cplusplus
extern "C" {
#endif
bool oc_vec2_equal(oc_vec2 v0, oc_vec2 v1); bool oc_vec2_equal(oc_vec2 v0, oc_vec2 v1);
oc_vec2 oc_vec2_mul(f32 f, oc_vec2 v); oc_vec2 oc_vec2_mul(f32 f, oc_vec2 v);
oc_vec2 oc_vec2_add(oc_vec2 v0, oc_vec2 v1); 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 //TODO: complete
#ifdef __cplusplus
}
#endif
#endif //__ALGEBRA_H_ #endif //__ALGEBRA_H_

View File

@ -11,6 +11,10 @@
#include "platform/platform_debug.h" #include "platform/platform_debug.h"
#include "util/macros.h" #include "util/macros.h"
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------- //----------------------------------------------------------------
// Logging // Logging
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -59,4 +63,8 @@
#endif #endif
#endif #endif
#ifdef __cplusplus
}
#endif
#endif //__DEBUG_H_ #endif //__DEBUG_H_

View File

@ -77,19 +77,19 @@ static inline u64 oc_next_pow2(u64 x)
//NOTE(martin): 'hygienic' max/min/square/cube macros. //NOTE(martin): 'hygienic' max/min/square/cube macros.
#ifdef __cplusplus #ifdef __cplusplus
//NOTE(martin): in C++ we use templates and decltype/declval //NOTE(martin): in C++ we use templates and decltype/declval
// (overloaded functions would be ambiguous because of the // (overloaded functions would be ambiguous because of the
// overload resolution and conversion/promotion rules) // overload resolution and conversion/promotion rules)
#include <utility> // #include <utility>
template <typename Ta, typename Tb> template <typename Ta, typename Tb>
inline decltype(std::declval<Ta>() + std::declval<Tb>()) oc_min(Ta a, Tb b) inline decltype(Ta() + Tb()) oc_min(Ta a, Tb b)
{ {
return (a < b ? a : b); return (a < b ? a : b);
} }
template <typename Ta, typename Tb> template <typename Ta, typename Tb>
inline decltype(std::declval<Ta>() + std::declval<Tb>()) oc_max(Ta a, Tb b) inline decltype(Ta() + Tb()) oc_max(Ta a, Tb b)
{ {
return (a > b ? a : b); return (a > b ? a : b);
} }

View File

@ -38,12 +38,12 @@ typedef struct oc_str8
size_t len; size_t len;
} oc_str8; } 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 //NOTE: this only works with string literals, but is sometimes necessary to generate compile-time constants
#define OC_STR8_LIT(s) \ #define OC_STR8_LIT(s) \
{ \ { \
s, sizeof(s) - 1 \ (char*)(s), sizeof(s) - 1 \
} }
#define oc_str8_lp(s) ((s).len), ((s).ptr) #define oc_str8_lp(s) ((s).len), ((s).ptr)