Fix fallback unknown function build logic
authorCharles Giessen <charles@lunarg.com>
Mon, 5 Jun 2023 20:48:32 +0000 (14:48 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Wed, 7 Jun 2023 17:40:44 +0000 (11:40 -0600)
The build logic for enabling the unknown function support when ASM code doesn't
work or is disabled was broken. This fixes it by consolidating the various code
paths as well as making sure it is enabled when it should be.

This also fixes the fallback path for macOS by making sure to only modify the
names of functions if and only if the corresponding assembly code is active.

loader/CMakeLists.txt
loader/dev_ext_trampoline.c
loader/log.h
loader/phys_dev_ext.c

index ad2aecb8bfdc29976d96090c87e2ad9117f3ad83..33cd8cbf55912d9db31d4cd3df554fb90fced9c3 100644 (file)
@@ -189,11 +189,8 @@ if(WIN32)
         target_include_directories(loader-unknown-chain PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
         add_dependencies(loader-unknown-chain loader_asm_gen_files)
     else()
+        set(USE_ASSEMBLY_FALLBACK ON)
         message(WARNING "Could not find working MASM assembler\n${ASM_FAILURE_MSG}")
-        add_custom_target(loader_asm_gen_files)
-        add_library(loader-unknown-chain OBJECT unknown_ext_chain.c)
-        target_link_libraries(loader-unknown-chain loader_specific_options)
-        set_target_properties(loader-unknown-chain PROPERTIES CMAKE_C_FLAGS_DEBUG "${MODIFIED_C_FLAGS_DEBUG}")
     endif()
 elseif(UNIX) # i.e.: Linux & Apple
     option(USE_GAS "Use GAS" ON)
@@ -254,20 +251,30 @@ elseif(UNIX) # i.e.: Linux & Apple
             )
         endif()
         add_custom_target(loader_asm_gen_files DEPENDS gen_defines.asm)
+
+        if (APPLE)
+            set(MODIFY_UNKNOWN_FUNCTION_DECLS ON)
+        endif()
     else()
+        set(USE_ASSEMBLY_FALLBACK ON)
         if(USE_GAS)
             message(WARNING "Could not find working ${ASM_OFFSET_SYSTEM_PROCESSOR} GAS assembler\n${ASM_FAILURE_MSG}")
         else()
             message(WARNING "Assembly sources have been disabled\n${ASM_FAILURE_MSG}")
         endif()
+    endif()
+endif()
+
+if(USE_ASSEMBLY_FALLBACK)
+    add_custom_target(loader_asm_gen_files)
+    if (MSVC)
+        add_library(loader-unknown-chain OBJECT unknown_ext_chain.c)
+        target_link_libraries(loader-unknown-chain loader_specific_options)
+        set_target_properties(loader-unknown-chain PROPERTIES CMAKE_C_FLAGS_DEBUG "${MODIFIED_C_FLAGS_DEBUG}")
+    else()
         set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain.c)
-        add_custom_target(loader_asm_gen_files)
+        set_source_files_properties(${OPT_LOADER_SRCS} PROPERTIES COMPILE_FLAGS -O)
     endif()
-else()
-    # For other platforms, use the C code and force the compiler's tail-call optimization instead of using assembly code.
-    set(OPT_LOADER_SRCS ${OPT_LOADER_SRCS} unknown_ext_chain.c)
-    set_source_files_properties(${OPT_LOADER_SRCS} PROPERTIES COMPILE_FLAGS -O)
-    add_custom_target(loader_asm_gen_files) # This causes no assembly files to be generated.
 endif()
 
 if(WIN32)
@@ -282,10 +289,16 @@ if(WIN32)
         set(RC_FILE_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/loader.rc)
     endif()
 
+    if (MSVC)
+        set(LOADER_UNKNOWN_CHAIN_LIBRARY $<TARGET_OBJECTS:loader-unknown-chain>)
+    else()
+        set(LOADER_UNKNOWN_CHAIN_LIBRARY "")
+    endif()
+
     add_library(vulkan
                 SHARED
                 ${NORMAL_LOADER_SRCS}
-                $<TARGET_OBJECTS:loader-unknown-chain>
+                ${LOADER_UNKNOWN_CHAIN_LIBRARY}
                 ${CMAKE_CURRENT_SOURCE_DIR}/vulkan-1.def
                 ${RC_FILE_LOCATION})
 
@@ -356,6 +369,13 @@ else()
         target_link_libraries(vulkan-framework ${CMAKE_DL_LIBS} Threads::Threads -lm "-framework CoreFoundation")
         target_link_libraries(vulkan-framework loader_specific_options)
 
+        if (MODIFY_UNKNOWN_FUNCTION_DECLS)
+            # Modifies the names of functions as they appearin the assembly code so that the
+            # unknown function handling will work
+            target_compile_definitions(vulkan PRIVATE MODIFY_UNKNOWN_FUNCTION_DECLS)
+            target_compile_definitions(vulkan-framework PRIVATE MODIFY_UNKNOWN_FUNCTION_DECLS)
+        endif()
+
         # The FRAMEWORK_VERSION needs to be "A" here so that Xcode code-signing works when a user adds their framework to an Xcode
         # project and does "Sign on Copy". It would have been nicer to use "1" to denote Vulkan 1. Although Apple docs say that a
         # framework version does not have to be "A", this part of the Apple toolchain expects it.
index cd7e0ccc95a7735b7d344d5a5885cc6411af6a74..ae284671366770dfc340ae657956febc36f7f699 100644 (file)
@@ -25,7 +25,7 @@
 #endif
 
 // The asm declaration prevents name mangling which is necessary for macOS
-#if defined(__GNUC__) && defined(__clang__)
+#if defined(MODIFY_UNKNOWN_FUNCTION_DECLS)
 #define ASM_NAME(name) __asm(name)
 #else
 #define ASM_NAME(name)
index 246c00175bfc646619c903e0dc8e6e5fcb86f651..a1a34baa811a8027cef07d5d574a6b44b9dd8b5c 100644 (file)
@@ -57,7 +57,7 @@ void loader_set_global_debug_level(uint32_t new_loader_debug);
 uint32_t loader_get_global_debug_level(void);
 
 // The asm declaration prevents name mangling which is necessary for macOS
-#if defined(__GNUC__) && defined(__clang__)
+#if defined(MODIFY_UNKNOWN_FUNCTION_DECLS)
 #define ASM_NAME(name) __asm(name)
 #else
 #define ASM_NAME(name)
index e1a9fb7b9083a49855c3024393e8efcc041f894d..1bcfa5767b56ae7011ad2bcb4c1c8f8eb3dde775 100644 (file)
@@ -35,7 +35,7 @@
 #endif
 
 // The asm declaration prevents name mangling which is necessary for macOS
-#if defined(__GNUC__) && defined(__clang__)
+#if defined(MODIFY_UNKNOWN_FUNCTION_DECLS)
 #define ASM_NAME(name) __asm(name)
 #else
 #define ASM_NAME(name)