Skip to content

Commit

Permalink
C++ support (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
bates64 authored Dec 1, 2024
2 parents edb9370 + 0a7f245 commit ca4fe00
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 13 deletions.
8 changes: 8 additions & 0 deletions include/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "stdlib/stdarg.h"
#include "libc/xstdio.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

f32 fabsf(f32 f);
f64 fabs(f64 f);
f32 cosine(s16 arg0);
Expand Down Expand Up @@ -1096,4 +1100,8 @@ void restore_map_collision_data(void);
void mdl_load_all_textures(struct ModelNode* model, s32 romOffset, s32 size);
void mdl_calculate_model_sizes(void);

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
6 changes: 6 additions & 0 deletions include/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@

#define NAME_SUFFIX
#define NAME_PREFIX
#ifdef _LANGUAGE_C_PLUS_PLUS
// use C++ namespaces instead of these macros!
#define A(sym) sym
#define N(sym) sym
#else
#define A(sym) NS(AREA, NAME_PREFIX, sym, NAME_SUFFIX)
#define N(sym) NS(NAMESPACE, NAME_PREFIX, sym, NAME_SUFFIX)
#endif

#define ARRAY_COUNT(arr) (s32)(sizeof(arr) / sizeof(arr[0]))

Expand Down
8 changes: 8 additions & 0 deletions include/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "enums.h"
#include "script_api/map.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

// battle and stage are optional in overloaded NPC_GROUP macros
#define NPC_GROUP(args...) VFUNC(NPC_GROUP, args)
#define NPC_GROUP1(npcs) { sizeof(npcs) / sizeof(NpcData), (NpcData*) &npcs, 0, 0 }
Expand Down Expand Up @@ -657,4 +661,8 @@ Enemy* get_enemy_safe(s32 npcID);

void set_npc_sprite(Npc* npc, s32 anim, AnimID* extraAnimList);

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
8 changes: 8 additions & 0 deletions include/script_api/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include "effects.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

API_CALLABLE(EnablePartnerBlur);
API_CALLABLE(DisablePartnerBlur);
API_CALLABLE(UseBattleCamPreset);
Expand Down Expand Up @@ -378,4 +382,8 @@ extern EvtScript Rumble_Unused_2;
extern EvtScript Rumble_Unused_3;
extern EvtScript Rumble_Unused_4;

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
8 changes: 8 additions & 0 deletions include/script_api/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "../common.h"
#include "macros.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

/// @{
/// @name Map

Expand Down Expand Up @@ -1373,4 +1377,8 @@ API_CALLABLE(DemoJoystickXY);
extern EvtScript EnemyNpcHit;
extern EvtScript EnemyNpcDefeat;

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
8 changes: 8 additions & 0 deletions include/script_api/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "evt.h"
#include "stdlib/stdarg.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

/****** EXPRESSIONS ***************************************************************************************************/

/// Expressions in EVT instructions should be one of the following types:
Expand Down Expand Up @@ -830,4 +834,8 @@
#define PlayEffect14(effect, subtype, a, b, c, d, e, f, g, h, i, j, k, l) \
Call(PlayEffect_impl, effect, subtype, a, b, c, d, e, f, g, h, i, j, k, l)

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
8 changes: 8 additions & 0 deletions include/script_api/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include "script_api/common.h"

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

API_CALLABLE(MakeNpcs);
API_CALLABLE(BasicAI_Main);
API_CALLABLE(ResetFromLava);
Expand Down Expand Up @@ -56,4 +60,8 @@ extern EvtScript BaseEnterDoor;
extern EvtScript EnterPostPipe;
extern EvtScript EVS_ShopOwnerDialog;

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
10 changes: 10 additions & 0 deletions src/dx/debug_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include "dx/config.h"
#if DX_DEBUG_MENU || defined(DX_QUICK_LAUNCH_BATTLE)

#ifdef _LANGUAGE_C_PLUS_PLUS
extern "C" {
#endif

#define DX_DEBUG_DUMMY_ID 0xDEAD

typedef enum DebugCheat {
Expand All @@ -25,6 +29,8 @@ void dx_debug_set_battle_info(s32 battleID, char* stageName);

void dx_debug_begin_battle_with_IDs(s16 battle, s16 stage);

void dx_hashed_debug_printf(char* filename, s32 line, char* fmt, ...);

#define debug_print(text) dx_hashed_debug_printf(__FILE__,__LINE__,text)
#define debug_printf(fmt, args...) dx_hashed_debug_printf(__FILE__,__LINE__,fmt,##args)

Expand Down Expand Up @@ -68,4 +74,8 @@ API_CALLABLE(_dxDebugFloatPrintf);
#define DebugFloatPrintf8(text, a, b, c, d, e, f, g) \
Call(_dxDebugFloatPrintf, Ref(__FILE__), __LINE__, Ref(text), a, b, c, d, e, f, g)

#ifdef _LANGUAGE_C_PLUS_PLUS
}
#endif

#endif
38 changes: 25 additions & 13 deletions tools/build/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ def write_ninja_rules(
cc_egcs_dir = f"{BUILD_TOOLS}/cc/egcs/"
cc_egcs = f"{cc_egcs_dir}/gcc"
cxx = f"{BUILD_TOOLS}/cc/gcc/g++"
cxx_modern = f"{cross}g++"

BFDNAME = "elf32-tradbigmips"

CPPFLAGS_COMMON = (
"-Iver/$version/include -Iver/$version/build/include -Iinclude -Isrc -Iassets/$version -D_LANGUAGE_C -D_FINALROM "
"-Iver/$version/include -Iver/$version/build/include -Iinclude -Isrc -Iassets/$version -D_FINALROM "
"-DVERSION=$version -DF3DEX_GBI_2 -D_MIPS_SZLONG=32"
)

Expand All @@ -80,9 +81,9 @@ def write_ninja_rules(

CPPFLAGS = "-w " + CPPFLAGS_COMMON + " -nostdinc"

cflags = f"-c -G0 -O2 -gdwarf-2 -x c -B {BUILD_TOOLS}/cc/gcc/ {extra_cflags}"
cflags = f"-c -G0 -O2 -gdwarf-2 -B {BUILD_TOOLS}/cc/gcc/ {extra_cflags}"

cflags_modern = f"-c -G0 -O2 -gdwarf-2 -fdiagnostics-color=always -fno-builtin-bcopy -fno-tree-loop-distribute-patterns -funsigned-char -mgp32 -mfp32 -mabi=32 -mfix4300 -march=vr4300 -mno-gpopt -fno-toplevel-reorder -mno-abicalls -fno-pic -fno-exceptions -fno-stack-protector -fno-zero-initialized-in-bss -Wno-builtin-declaration-mismatch -x c {extra_cflags}"
cflags_modern = f"-c -G0 -O2 -gdwarf-2 -fdiagnostics-color=always -fno-builtin-bcopy -fno-tree-loop-distribute-patterns -funsigned-char -mgp32 -mfp32 -mabi=32 -mfix4300 -march=vr4300 -mno-gpopt -fno-toplevel-reorder -mno-abicalls -fno-pic -fno-exceptions -fno-stack-protector -fno-zero-initialized-in-bss -Wno-builtin-declaration-mismatch {extra_cflags}"

cflags_272 = f"-c -G0 -mgp32 -mfp32 -mips3 {extra_cflags}"
cflags_272 = cflags_272.replace("-ggdb3", "-g1")
Expand Down Expand Up @@ -147,7 +148,7 @@ def write_ninja_rules(
ninja.rule(
"cc_modern",
description="gcc_modern $in",
command=f"{ccache}{cc_modern} {cflags_modern} $cflags {CPPFLAGS} {extra_cppflags} $cppflags -MD -MF $out.d $in -o $out",
command=f"{ccache}{cc_modern} {cflags_modern} $cflags {CPPFLAGS} {extra_cppflags} $cppflags -D_LANGUAGE_C -MD -MF $out.d $in -o $out",
depfile="$out.d",
deps="gcc",
)
Expand Down Expand Up @@ -178,6 +179,14 @@ def write_ninja_rules(
deps="gcc",
)

ninja.rule(
"cxx_modern",
description="cxx_modern $in",
command=f"{ccache}{cxx_modern} {cflags_modern} $cflags {CPPFLAGS} {extra_cppflags} $cppflags -std=c++20 -D_LANGUAGE_C_PLUS_PLUS -MD -MF $out.d $in -o $out",
depfile="$out.d",
deps="gcc",
)

ninja.rule(
"dead_cc_fix",
description="dead_cc_fix $in",
Expand Down Expand Up @@ -543,7 +552,7 @@ def build(
implicit = []
order_only = []

if task in ["cc", "cxx", "cc_modern"]:
if task in ["cc", "cxx", "cc_modern", "cxx_modern"]:
order_only.append("generated_code_" + self.version)
order_only.append("inc_img_bins_" + self.version)
if task == "cc_modern" and object_paths[0].suffixes[-1] != ".gch":
Expand Down Expand Up @@ -698,19 +707,22 @@ def build(
task = "cxx"

if modern_gcc:
task = "cc_modern"
if task == "cxx":
task = "cxx_modern"
else:
task = "cc_modern"

if entry.src_paths[0].suffixes[-1] == ".s":
task = "as"
elif "gcc_272" in cflags:
task = "cc_272"
#task = "cc_272"
cflags = cflags.replace("gcc_272", "")
elif "egcs" in cflags:
if sys.platform == "darwin" and non_matching:
print(f"warning: using default compiler for {seg.name} because egcs is not supported on macOS")
else:
task = "cc_egcs"
cflags = cflags.replace("egcs", "")
if sys.platform == "darwin" and non_matching:
print(f"warning: using default compiler for {seg.name} because egcs is not supported on macOS")
else:
#task = "cc_egcs"
cflags = cflags.replace("egcs", "")
elif "gcc_modern" in cflags:
task = "cc_modern"
cflags = cflags.replace("gcc_modern", "")
Expand Down Expand Up @@ -1488,7 +1500,7 @@ def make_current(self, ninja: ninja_syntax.Writer):
if args.shift:
extra_cppflags += " -DSHIFT"

extra_cflags += " -Wmissing-braces -Wimplicit -Wredundant-decls -Wstrict-prototypes -Wno-redundant-decls"
extra_cflags += " -Wmissing-braces -Wredundant-decls -Wno-redundant-decls"

# add splat to python import path
sys.path.insert(0, str((ROOT / args.splat / "src").resolve()))
Expand Down

0 comments on commit ca4fe00

Please sign in to comment.