package builder import "core:flags" import "core:fmt" import "core:log" import os "core:os/os2" import "core:slice" Build_Variant :: enum { Hot_Reload, Desktop, Web, } Options :: struct { variant: Build_Variant `usage:"Variant of the build"`, optimize: bool `args:"name=opt",usage:"Enable compiler optimizations"`, debug: bool `usage:"Enable debug symbols"`, rebuild_deps: bool `usage:"When enabled dependencies will be cleaned and rebuilt"`, tracy: bool `usage:"Enable tracy profiler"`, } Error :: union #shared_nil { Run_Error, Copy_Error, os.Error, } build_deps :: proc(opts: Options) { log.infof("build_deps") force := opts.rebuild_deps shared := opts.variant == .Hot_Reload // Raylib { cwd := "./libs/raylib" out_dir := shared ? "zig-out-shared" : "zig-out-static" if force { remove_all(fmt.tprintf("./libs/raylib/%s", out_dir)) } target := opts.variant == .Web ? "wasm32-emscripten" : "native" handle_error( run_cmd( { "zig", "build", "-p", out_dir, fmt.tprintf("-Dshared=%v", shared), fmt.tprintf("-Dtarget=%s", target), }, cwd, ), ) } // Physfs { cwd := "./libs/physfs" file_name := shared ? "libphysfs.so" : "libphysfs.a" is_built := os.is_file(fmt.tprintf("./libs/physfs/%s", file_name)) if is_built && force { handle_error(run_cmd({"make", "clean"}, cwd)) } if !is_built || force { handle_error(run_cmd({"make", file_name}, cwd)) } } // Tracy if opts.tracy { cwd := "./libs/tracy" when ODIN_OS == .Windows { TRACY_NAME_SHARED :: "tracydll.lib" TRACY_NAME_STATIC :: "tracy.lib" } else when ODIN_OS == .Linux { TRACY_NAME_SHARED :: "tracy.so" TRACY_NAME_STATIC :: "tracy.a" } else when ODIN_OS == .Darwin { TRACY_NAME_SHARED :: "tracy.dynlib" TRACY_NAME_STATIC :: "tracy.a" } file_path := fmt.tprintf("./libs/tracy/%s", shared ? TRACY_NAME_SHARED : TRACY_NAME_STATIC) is_built := os.is_file(file_path) if is_built && force { remove_file(file_path) } if !is_built || force { handle_error( run_cmd( slice.concatenate( [][]string { { "zig", "c++", "-std=c++11", "-DTRACY_ENABLE", "-O2", "vendor/tracy/public/TracyClient.cpp", "-fPIC", }, shared ? {"-shared", "-o", TRACY_NAME_SHARED} : {"-c", "-o", "tracy.o"}, }, context.temp_allocator, ), cwd, ), ) if !shared { handle_error(run_cmd({"zig", "ar", "rc", TRACY_NAME_STATIC, "tracy.o"}, cwd)) } } } } COMMON_FLAGS :: []string { "-collection:libs=./libs", "-collection:common=./common", "-collection:game=./game", "-strict-style", "-vet", } main :: proc() { context.logger = log.create_console_logger() opts := Options { tracy = true, debug = true, } flags.parse_or_exit(&opts, os.args, .Unix, context.temp_allocator) if opts.variant == .Web { log.warnf("tracy is not supported on Web") opts.tracy = false } tracy_flag: []string = opts.tracy ? {"-define:TRACY_ENABLE=true"} : {} debug_flag: []string = opts.debug ? {"-debug"} : {} optimize_flag: []string = opts.optimize ? {"-o:speed"} : {} build_deps(opts) #partial switch opts.variant { case .Hot_Reload: cmd := slice.concatenate( [][]string { []string { "odin", "build", "game", "-define:RAYLIB_SHARED=true", "-define:PHYSFS_SHARED=true", "-build-mode:dll", "-out:game_tmp.so", }, tracy_flag, debug_flag, optimize_flag, COMMON_FLAGS, }, context.temp_allocator, ) handle_error(run_cmd(cmd, ".")) handle_error(os.rename("game_tmp.so", "game.so")) case .Desktop: cmd := slice.concatenate( [][]string { []string{"odin", "build", "main_release", "-out:game.bin"}, tracy_flag, debug_flag, optimize_flag, COMMON_FLAGS, }, context.temp_allocator, ) handle_error(run_cmd(cmd, ".")) case: cmd := slice.concatenate( [][]string { []string{"odin", "build", "main_web", "-build-mode:obj", "-out:game_web/game"}, COMMON_FLAGS, debug_flag, optimize_flag, }, ) handle_error(run_cmd(cmd, ".")) } }