From 8dc4eca4d2940500de30dfb8fd2ec25f7c31eba7 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 9 Jul 2024 20:03:50 +0200 Subject: [PATCH 1/7] Allow using precompiled .res file. --- src/build_settings.cpp | 9 +++++---- src/linker.cpp | 41 +++++++++++++++++++++++------------------ src/main.cpp | 6 ++++-- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 28ca0f088..4d3e20a7a 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1805,10 +1805,11 @@ gb_internal bool init_build_paths(String init_filename) { #if defined(GB_SYSTEM_WINDOWS) if (bc->metrics.os == TargetOs_windows) { if (bc->resource_filepath.len > 0) { - bc->build_paths[BuildPath_RC] = path_from_string(ha, bc->resource_filepath); - bc->build_paths[BuildPath_RES] = path_from_string(ha, bc->resource_filepath); - bc->build_paths[BuildPath_RC].ext = copy_string(ha, STR_LIT("rc")); - bc->build_paths[BuildPath_RES].ext = copy_string(ha, STR_LIT("res")); + bc->build_paths[BuildPath_RES] = path_from_string(ha, bc->resource_filepath); + if (!string_ends_with(bc->resource_filepath, str_lit(".res"))) { + bc->build_paths[BuildPath_RC] = path_from_string(ha, bc->resource_filepath); + bc->build_paths[BuildPath_RC].ext = copy_string(ha, STR_LIT("rc")); + } } if (bc->pdb_filepath.len > 0) { diff --git a/src/linker.cpp b/src/linker.cpp index 890a74e5a..ac3e32851 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -305,30 +305,35 @@ gb_internal i32 linker_stage(LinkerData *gen) { defer (gb_free(heap_allocator(), windows_sdk_bin_path.text)); if (!build_context.use_lld) { // msvc - String res_path = {}; + String temp_res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]); + String temp_rc_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]); + defer (gb_free(heap_allocator(), temp_res_path.text)); + defer (gb_free(heap_allocator(), temp_rc_path.text)); + + String res_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_res_path, str_lit("\"")); + String rc_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_rc_path, str_lit("\"")); defer (gb_free(heap_allocator(), res_path.text)); + defer (gb_free(heap_allocator(), rc_path.text)); - // TODO(Jeroen): Add ability to reuse .res file instead of recompiling, if `-resource:file.res` is given. if (build_context.has_resource) { - String temp_res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]); - res_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_res_path, str_lit("\"")); - gb_free(heap_allocator(), temp_res_path.text); + if (temp_rc_path == "") { + debugf("Using precompiled resource %.*s\n", LIT(res_path)); + } else { + debugf("Compiling resource %.*s\n", LIT(res_path)); - String temp_rc_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]); - String rc_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_rc_path, str_lit("\"")); - gb_free(heap_allocator(), temp_rc_path.text); - defer (gb_free(heap_allocator(), rc_path.text)); + result = system_exec_command_line_app("msvc-link", + "\"%.*src.exe\" /nologo /fo %.*s %.*s", + LIT(windows_sdk_bin_path), + LIT(res_path), + LIT(rc_path) + ); - result = system_exec_command_line_app("msvc-link", - "\"%.*src.exe\" /nologo /fo %.*s %.*s", - LIT(windows_sdk_bin_path), - LIT(res_path), - LIT(rc_path) - ); - - if (result) { - return result; + if (result) { + return result; + } } + } else { + res_path = {}; } String linker_name = str_lit("link.exe"); diff --git a/src/main.cpp b/src/main.cpp index 7763ccd23..e6a0aecf0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1452,8 +1452,9 @@ gb_internal bool parse_build_flags(Array args) { String path = value.value_string; path = string_trim_whitespace(path); if (is_build_flag_path_valid(path)) { - if(!string_ends_with(path, str_lit(".rc"))) { - gb_printf_err("Invalid -resource path %.*s, missing .rc\n", LIT(path)); + bool is_resource = string_ends_with(path, str_lit(".rc")) || string_ends_with(path, str_lit(".res")); + if(!is_resource) { + gb_printf_err("Invalid -resource path %.*s, missing .rc or .res file\n", LIT(path)); bad_flags = true; break; } else if (!gb_file_exists((const char *)path.text)) { @@ -2552,6 +2553,7 @@ gb_internal void print_show_help(String const arg0, String const &command) { print_usage_line(2, "[Windows only]"); print_usage_line(2, "Defines the resource file for the executable."); print_usage_line(2, "Example: -resource:path/to/file.rc"); + print_usage_line(2, "or: -resource:path/to/file.res for a precompiled one."); print_usage_line(0, ""); print_usage_line(1, "-pdb-name:"); From cc73e06a46cbefd2afa4caa7ef95ba6d9ab64eb9 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 10 Jul 2024 15:09:13 +0200 Subject: [PATCH 2/7] Allow precompiled resources with -resource:foo.res And add them to the magic new feature. --- src/cached.cpp | 11 +++++++++++ src/linker.cpp | 11 +++-------- src/path.cpp | 7 +++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/cached.cpp b/src/cached.cpp index 02cb1ae96..36927496d 100644 --- a/src/cached.cpp +++ b/src/cached.cpp @@ -200,6 +200,17 @@ gb_internal bool try_cached_build(Checker *c, Array const &args) { } } + // Add Windows resource file to file list, if applicable + if (build_context.has_resource) { + String res_path = {}; + if (build_context.build_paths[BuildPath_RC].basename == "") { + res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]); + } else { + res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]); + } + array_add(&files, res_path); + } + for (auto const &entry : c->info.load_file_cache) { auto *cache = entry.value; if (!cache || !cache->exists) { diff --git a/src/linker.cpp b/src/linker.cpp index ac3e32851..046e72d0e 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -305,18 +305,13 @@ gb_internal i32 linker_stage(LinkerData *gen) { defer (gb_free(heap_allocator(), windows_sdk_bin_path.text)); if (!build_context.use_lld) { // msvc - String temp_res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]); - String temp_rc_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]); - defer (gb_free(heap_allocator(), temp_res_path.text)); - defer (gb_free(heap_allocator(), temp_rc_path.text)); - - String res_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_res_path, str_lit("\"")); - String rc_path = concatenate3_strings(heap_allocator(), str_lit("\""), temp_rc_path, str_lit("\"")); + String res_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RES]); + String rc_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RC]); defer (gb_free(heap_allocator(), res_path.text)); defer (gb_free(heap_allocator(), rc_path.text)); if (build_context.has_resource) { - if (temp_rc_path == "") { + if (build_context.build_paths[BuildPath_RC].basename == "") { debugf("Using precompiled resource %.*s\n", LIT(res_path)); } else { debugf("Compiling resource %.*s\n", LIT(res_path)); diff --git a/src/path.cpp b/src/path.cpp index 26ccb7cbf..2c08ddd98 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -152,6 +152,13 @@ gb_internal String path_to_string(gbAllocator a, Path path) { return res; } +gb_internal String quote_path(gbAllocator a, Path path) { + String temp = path_to_string(a, path); + String quoted = concatenate3_strings(a, str_lit("\""), temp, str_lit("\"")); + gb_free(a, temp.text); + return quoted; +} + // NOTE(Jeroen): Naively turns a Path into a string, then normalizes it using `path_to_full_path`. gb_internal String path_to_full_path(gbAllocator a, Path path) { String temp = path_to_string(heap_allocator(), path); From 4148c6d6c749eac62b7d1a978f820b82de7d3bea Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 10 Jul 2024 15:25:49 +0200 Subject: [PATCH 3/7] #if WINDOWS --- src/cached.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/cached.cpp b/src/cached.cpp index 36927496d..7f213ba21 100644 --- a/src/cached.cpp +++ b/src/cached.cpp @@ -200,16 +200,17 @@ gb_internal bool try_cached_build(Checker *c, Array const &args) { } } - // Add Windows resource file to file list, if applicable - if (build_context.has_resource) { - String res_path = {}; - if (build_context.build_paths[BuildPath_RC].basename == "") { - res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RES]); - } else { - res_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_RC]); + #if defined(GB_SYSTEM_WINDOWS) + if (build_context.has_resource) { + String res_path = {}; + if (build_context.build_paths[BuildPath_RC].basename == "") { + res_path = path_to_string(permanent_allocator(), build_context.build_paths[BuildPath_RES]); + } else { + res_path = path_to_string(permanent_allocator(), build_context.build_paths[BuildPath_RC]); + } + array_add(&files, res_path); } - array_add(&files, res_path); - } + #endif for (auto const &entry : c->info.load_file_cache) { auto *cache = entry.value; From b02291b20a111c19647c8a5f100d88f558355fd5 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 10 Jul 2024 19:26:35 +0200 Subject: [PATCH 4/7] Compile demo with -vet -strict-style. --- build.bat | 2 +- build_odin.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.bat b/build.bat index e07fdf4fb..bddde49c8 100644 --- a/build.bat +++ b/build.bat @@ -114,7 +114,7 @@ call build_vendor.bat if %errorlevel% neq 0 goto end_of_build rem If the demo doesn't run for you and your CPU is more than a decade old, try -microarch:native -if %release_mode% EQU 0 odin run examples/demo -- Hellope World +if %release_mode% EQU 0 odin run examples/demo -vet -strict-style -- Hellope World del *.obj > NUL 2> NUL diff --git a/build_odin.sh b/build_odin.sh index d2f865e24..125b9335a 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -144,7 +144,7 @@ build_odin() { } run_demo() { - ./odin run examples/demo/demo.odin -file -- Hellope World + ./odin run examples/demo -vet -strict-style -- Hellope World } if [ $# -eq 0 ]; then From c49afb0039a1527fbe42505af73000fcc7e3ad08 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Wed, 10 Jul 2024 19:58:29 +0200 Subject: [PATCH 5/7] Add hinstdll forward to runtime globals --- base/runtime/core.odin | 2 ++ base/runtime/entry_windows.odin | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/base/runtime/core.odin b/base/runtime/core.odin index 550daed3b..a758a2fdd 100644 --- a/base/runtime/core.odin +++ b/base/runtime/core.odin @@ -299,6 +299,8 @@ when ODIN_OS == .Windows { Thread_Detach = 3, } dll_forward_reason: DLL_Forward_Reason + + dll_instance: rawptr } // IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it) diff --git a/base/runtime/entry_windows.odin b/base/runtime/entry_windows.odin index 7020e9ea8..1e2dcc21a 100644 --- a/base/runtime/entry_windows.odin +++ b/base/runtime/entry_windows.odin @@ -10,8 +10,9 @@ when ODIN_BUILD_MODE == .Dynamic { DllMain :: proc "system" (hinstDLL: rawptr, fdwReason: u32, lpReserved: rawptr) -> b32 { context = default_context() - // Populate Windows DLL-specific global + // Populate Windows DLL-specific globals dll_forward_reason = DLL_Forward_Reason(fdwReason) + dll_instance = hinstDLL switch dll_forward_reason { case .Process_Attach: From 0dfc660c71f366c8863d982b758a5610de2fc2ed Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 11 Jul 2024 01:55:59 +0200 Subject: [PATCH 6/7] allow `core:mem/virtual` import on more targets by expanding the `other` implementation --- core/mem/virtual/{virtual_bsd.odin => virtual_other.odin} | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename core/mem/virtual/{virtual_bsd.odin => virtual_other.odin} (92%) diff --git a/core/mem/virtual/virtual_bsd.odin b/core/mem/virtual/virtual_other.odin similarity index 92% rename from core/mem/virtual/virtual_bsd.odin rename to core/mem/virtual/virtual_other.odin index 12e7818ef..96d9683c4 100644 --- a/core/mem/virtual/virtual_bsd.odin +++ b/core/mem/virtual/virtual_other.odin @@ -1,5 +1,7 @@ -//+build freebsd, openbsd, netbsd //+private +//+build !darwin +//+build !linux +//+build !windows package mem_virtual _reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) { From 65ca03a93054864e75ca3ba879a9a16465989a9f Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 11 Jul 2024 01:57:00 +0200 Subject: [PATCH 7/7] make instrumentation "work" on wasm Using instrumentation on WASM causes it to complain about the llvm.returnaddress instrinsic. This PR could be considered a "hack" but makes this work by just passing `nil` instead of the return address. --- src/llvm_backend_opt.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 6a6d2f802..e6ccc9a57 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -398,16 +398,20 @@ gb_internal LLVMValueRef lb_run_instrumentation_pass_insert_call(lbProcedure *p, LLVMValueRef args[3] = {}; args[0] = p->value; - LLVMValueRef returnaddress_args[1] = {}; + if (is_arch_wasm()) { + args[1] = LLVMConstPointerNull(lb_type(m, t_rawptr)); + } else { + LLVMValueRef returnaddress_args[1] = {}; - returnaddress_args[0] = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), 0, false); + returnaddress_args[0] = LLVMConstInt(LLVMInt32TypeInContext(m->ctx), 0, false); - char const *instrinsic_name = "llvm.returnaddress"; - unsigned id = LLVMLookupIntrinsicID(instrinsic_name, gb_strlen(instrinsic_name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", instrinsic_name); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(m->mod, id, nullptr, 0); - LLVMTypeRef call_type = LLVMIntrinsicGetType(m->ctx, id, nullptr, 0); - args[1] = LLVMBuildCall2(dummy_builder, call_type, ip, returnaddress_args, gb_count_of(returnaddress_args), ""); + char const *instrinsic_name = "llvm.returnaddress"; + unsigned id = LLVMLookupIntrinsicID(instrinsic_name, gb_strlen(instrinsic_name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s", instrinsic_name); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(m->mod, id, nullptr, 0); + LLVMTypeRef call_type = LLVMIntrinsicGetType(m->ctx, id, nullptr, 0); + args[1] = LLVMBuildCall2(dummy_builder, call_type, ip, returnaddress_args, gb_count_of(returnaddress_args), ""); + } Token name = {}; if (p->entity) {