miniaudio.jai/generate.jai
2025-01-17 17:14:04 -05:00

156 lines
5.1 KiB
Plaintext

// Jon said: "Static linking for runtime is actually the default (even if there is a dll). no_dll means you can't call it at compile-time because a dll is necessary for the compiler to load."
// The lib that is created with the DLL is
// just a stub lib. If you want static linking without any need for a DLL then
// build an actual static lib and use #library,no_static_library
// @incomplete I'm probably doing something wrong below, so review the above and play around with it.
AT_COMPILE_TIME :: true;
COMPILE :: true; // Enable to compile the miniaudio library from source before generating bindings.
COMPILE_DEBUG :: false; // Compile a debug or release version of miniaudio
MAKE_DYNAMIC_LIB :: false; // Will make a static lib if false.
// There are a bunch of defines that you can set to configure the library to
// your liking. Check out heading "2.7. Build Options" in the miniaudio source
// file for the full list.
//
// We're using the default configuration, with the option to enable debug
// logging and to specify which decoders you want to support. You can easily
// add your own defines by including them in the extra array in generate_bindings().
ENABLE_MINIAUDIO_DEBUG_LOGS :: false;
MINIAUDIO_SUPPORTED_DECODERS: Libs : .WAV | .FLAC | .MP3; // @consider making this a metaprogram/command line arg. Can then use this as a git submodule in a project and a script/build file can generate bindings for what the project requires.
Libs :: enum_flags {
WAV;
FLAC;
MP3;
}
SOURCE_PATH :: "source/extras/miniaudio_split";
#if AT_COMPILE_TIME {
#run {
set_build_options_dc(.{do_output=false});
if !generate_bindings() {
compiler_set_workspace_status(.FAILED);
}
}
}
else {
#import "System";
main :: () {
set_working_directory(path_strip_filename(get_path_of_running_executable()));
if !generate_bindings() {
exit(1);
}
}
}
generate_bindings :: () -> bool {
extra: [..] string;
array_add(*extra, "/DMINIAUDIO_IMPLEMENTATION");
#if ENABLE_MINIAUDIO_DEBUG_LOGS {
array_add(*extra, "/DMA_DEBUG_OUTPUT");
}
libs := type_info(Libs);
for libs.names {
if !(MINIAUDIO_SUPPORTED_DECODERS & (xx libs.values[it_index])) {
array_add(*extra, tprint("/DMA_NO_%", it));
}
}
#if COMPILE {
#import "BuildCpp";
#if COMPILE_DEBUG then array_add(*extra, "/Zi");
src_file := tprint("%/miniaudio.c", SOURCE_PATH);
success := false;
#if OS == .WINDOWS {
make_directory_if_it_does_not_exist("windows");
#if MAKE_DYNAMIC_LIB {
array_add(*extra, "/DMA_DLL");
success = build_cpp_dynamic_lib("windows/miniaudio", src_file, extra = extra, debug = COMPILE_DEBUG);
}
else {
success = build_cpp_static_lib("windows/miniaudio", src_file, extra = extra, debug = COMPILE_DEBUG);
if success {
// Remove existing DLL otherwise the bindings will have a "#library" directive that loads the DLL. The lib version is "#library,no_dll"
if file_exists("windows/miniaudio.dll") {
deleted := file_delete("windows/miniaudio.dll");
if !deleted {
log_error("Failed to remove existing miniaudio.dll from the windows/ folder.\n\n");
success = false;
}
}
}
}
}
else {
log_error("This OS isn't supported yet.\n");
}
if !success return false;
}
output_filename: string;
opts: Generate_Bindings_Options;
{
using opts;
#if OS == .WINDOWS {
array_add(*libpaths, "windows");
output_filename = "windows.jai";
}
else {
log_error("This OS isn't supported yet.\n");
return false;
}
array_add(*libpaths, ".");
array_add(*libnames, "miniaudio");
array_add(*system_include_paths, GENERATOR_DEFAULT_SYSTEM_INCLUDE_PATH);
array_add(*include_paths, SOURCE_PATH);
array_add(*source_files, tprint("%/miniaudio.h", SOURCE_PATH));
array_add(*extra_clang_arguments, "-x", "c++", "-DWIN32_LEAN_AND_MEAN");
#if MAKE_DYNAMIC_LIB {
array_add(*extra_clang_arguments, "-DMA_DLL");
}
#if ENABLE_MINIAUDIO_DEBUG_LOGS {
array_add(*extra_clang_arguments, "-DMA_DEBUG_OUTPUT");
}
libs := type_info(Libs);
for libs.names {
if !(MINIAUDIO_SUPPORTED_DECODERS & (xx libs.values[it_index])) {
array_add(*extra_clang_arguments, tprint("-DMA_NO_%", it));
}
}
strip_enum_prefixes = false;
auto_detect_enum_prefixes = false;
log_stripped_declarations = true; // Set to true if things aren't working...
generate_compile_time_struct_checks = true;
}
return generate_bindings(opts, output_filename);
}
#scope_file
#import "Basic";
#import "Bindings_Generator";
#import "Compiler";
#import "File";
#import "File_Utilities";