From 6b41b5be07d51d8b1e2103782bb28d4873a3ebfa Mon Sep 17 00:00:00 2001 From: Merry Date: Fri, 6 Jan 2023 14:27:06 +0000 Subject: [PATCH] CMakeLists: Support multi-architecture builds --- CMakeLists.txt | 2 +- CMakeModules/DetectArchitecture.cmake | 6 +++++ .../TargetArchitectureSpecificSources.cmake | 26 +++++++++++++++++++ ...tArchitectureSpecificSourcesWrapFile.cmake | 3 +++ externals/CMakeLists.txt | 6 ++--- src/dynarmic/CMakeLists.txt | 23 +++++++++------- .../backend/x64/a32_emit_x64_memory.cpp | 2 +- .../backend/x64/a64_emit_x64_memory.cpp | 2 +- tests/CMakeLists.txt | 8 +++--- 9 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 CMakeModules/TargetArchitectureSpecificSources.cmake create mode 100644 CMakeModules/impl/TargetArchitectureSpecificSourcesWrapFile.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 920cf2ba..7e36443a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,7 +134,7 @@ find_package(fmt 9 QUIET) find_package(mcl 0.1.12 EXACT QUIET) find_package(tsl-robin-map QUIET) -if (ARCHITECTURE STREQUAL "x86" OR ARCHITECTURE STREQUAL "x86_64") +if ("x86_64" IN_LIST ARCHITECTURE) find_package(xbyak 6 QUIET) find_package(Zydis 4 QUIET) endif() diff --git a/CMakeModules/DetectArchitecture.cmake b/CMakeModules/DetectArchitecture.cmake index ba6679cd..28bd1ef2 100644 --- a/CMakeModules/DetectArchitecture.cmake +++ b/CMakeModules/DetectArchitecture.cmake @@ -1,5 +1,11 @@ include(CheckSymbolExists) +if (CMAKE_OSX_ARCHITECTURES) + set(DYNARMIC_MULTIARCH_BUILD 1) + set(ARCHITECTURE "${CMAKE_OSX_ARCHITECTURES}") + return() +endif() + function(detect_architecture symbol arch) if (NOT DEFINED ARCHITECTURE) set(CMAKE_REQUIRED_QUIET YES) diff --git a/CMakeModules/TargetArchitectureSpecificSources.cmake b/CMakeModules/TargetArchitectureSpecificSources.cmake new file mode 100644 index 00000000..f08911c6 --- /dev/null +++ b/CMakeModules/TargetArchitectureSpecificSources.cmake @@ -0,0 +1,26 @@ +function(target_architecture_specific_sources project arch) + if (NOT DYNARMIC_MULTIARCH_BUILD) + target_sources("${project}" PRIVATE ${ARGN}) + return() + endif() + + foreach(input_file IN LISTS ARGN) + if(input_file MATCHES ".cpp$") + if(NOT IS_ABSOLUTE ${input_file}) + set(input_file "${CMAKE_CURRENT_SOURCE_DIR}/${input_file}") + endif() + + set(output_file "${CMAKE_CURRENT_BINARY_DIR}/arch_gen/${input_file}") + add_custom_command( + OUTPUT "${output_file}" + COMMAND ${CMAKE_COMMAND} "-Darch=${arch}" + "-Dinput_file=${input_file}" + "-Doutput_file=${output_file}" + -P "${CMAKE_CURRENT_FUNCTION_LIST_DIR}/impl/TargetArchitectureSpecificSourcesWrapFile.cmake" + DEPENDS "${input_file}" + VERBATIM + ) + target_sources(${project} PRIVATE "${output_file}") + endif() + endforeach() +endfunction() diff --git a/CMakeModules/impl/TargetArchitectureSpecificSourcesWrapFile.cmake b/CMakeModules/impl/TargetArchitectureSpecificSourcesWrapFile.cmake new file mode 100644 index 00000000..82b3078a --- /dev/null +++ b/CMakeModules/impl/TargetArchitectureSpecificSourcesWrapFile.cmake @@ -0,0 +1,3 @@ +string(TOUPPER "${arch}" arch) +file(READ "${input_file}" f_contents) +file(WRITE "${output_file}" "#include \n#if defined(MCL_ARCHITECTURE_${arch})\n${f_contents}\n#endif\n") diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 85fae1a4..c42cb6eb 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -40,7 +40,7 @@ endif() # oaknut if (NOT TARGET merry::oaknut) - if (ARCHITECTURE STREQUAL "arm64") + if ("arm64" IN_LIST ARCHITECTURE) add_subdirectory(oaknut EXCLUDE_FROM_ALL) endif() endif() @@ -54,7 +54,7 @@ endif() # xbyak if (NOT TARGET xbyak::xbyak) - if (ARCHITECTURE STREQUAL "x86" OR ARCHITECTURE STREQUAL "x86_64") + if ("x86_64" IN_LIST ARCHITECTURE) add_subdirectory(xbyak EXCLUDE_FROM_ALL) endif() endif() @@ -62,7 +62,7 @@ endif() # zydis if (NOT TARGET Zydis::Zydis) - if (ARCHITECTURE STREQUAL "x86" OR ARCHITECTURE STREQUAL "x86_64") + if ("x86_64" IN_LIST ARCHITECTURE) set(ZYDIS_BUILD_TOOLS OFF) set(ZYDIS_BUILD_EXAMPLES OFF) set(ZYDIS_BUILD_DOXYGEN OFF) diff --git a/src/dynarmic/CMakeLists.txt b/src/dynarmic/CMakeLists.txt index 6312af19..80c5a299 100644 --- a/src/dynarmic/CMakeLists.txt +++ b/src/dynarmic/CMakeLists.txt @@ -1,3 +1,5 @@ +include(TargetArchitectureSpecificSources) + add_library(dynarmic backend/exception_handler.h common/cast_util.h @@ -255,14 +257,14 @@ if ("A64" IN_LIST DYNARMIC_FRONTENDS) ) endif() -if (ARCHITECTURE STREQUAL "x86_64") +if ("x86_64" IN_LIST ARCHITECTURE) target_link_libraries(dynarmic PRIVATE xbyak::xbyak Zydis::Zydis ) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "x86_64" backend/x64/abi.cpp backend/x64/abi.h backend/x64/block_of_code.cpp @@ -309,7 +311,7 @@ if (ARCHITECTURE STREQUAL "x86_64") ) if ("A32" IN_LIST DYNARMIC_FRONTENDS) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "x86_64" backend/x64/a32_emit_x64.cpp backend/x64/a32_emit_x64.h backend/x64/a32_emit_x64_memory.cpp @@ -320,7 +322,7 @@ if (ARCHITECTURE STREQUAL "x86_64") endif() if ("A64" IN_LIST DYNARMIC_FRONTENDS) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "x86_64" backend/x64/a64_emit_x64.cpp backend/x64/a64_emit_x64.h backend/x64/a64_emit_x64_memory.cpp @@ -329,11 +331,12 @@ if (ARCHITECTURE STREQUAL "x86_64") backend/x64/a64_jitstate.h ) endif() +endif() -elseif(ARCHITECTURE STREQUAL "arm64") +if("arm64" IN_LIST ARCHITECTURE) target_link_libraries(dynarmic PRIVATE merry::oaknut) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "arm64" backend/arm64/a32_jitstate.cpp backend/arm64/a32_jitstate.h backend/arm64/a64_jitstate.h @@ -372,7 +375,7 @@ elseif(ARCHITECTURE STREQUAL "arm64") ) if ("A32" IN_LIST DYNARMIC_FRONTENDS) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "arm64" backend/arm64/a32_address_space.cpp backend/arm64/a32_address_space.h backend/arm64/a32_core.h @@ -381,7 +384,7 @@ elseif(ARCHITECTURE STREQUAL "arm64") endif() if ("A64" IN_LIST DYNARMIC_FRONTENDS) - target_sources(dynarmic PRIVATE + target_architecture_specific_sources(dynarmic "arm64" backend/arm64/a64_address_space.cpp backend/arm64/a64_address_space.h backend/arm64/a64_core.h @@ -481,9 +484,9 @@ target_compile_definitions(dynarmic PRIVATE FMT_USE_USER_DEFINED_LITERALS=1) if (DYNARMIC_USE_PRECOMPILED_HEADERS) set(PRECOMPILED_HEADERS "$<$:${CMAKE_CURRENT_SOURCE_DIR}/ir/ir_emitter.h>") - if (ARCHITECTURE STREQUAL "x86_64") + if ("x86_64" IN_LIST ARCHITECTURE) list(PREPEND PRECOMPILED_HEADERS "$<$:>") - elseif(ARCHITECTURE STREQUAL "arm64") + elseif("arm64" IN_LIST ARCHITECTURE) list(PREPEND PRECOMPILED_HEADERS "$<$:>") endif() target_precompile_headers(dynarmic PRIVATE ${PRECOMPILED_HEADERS}) diff --git a/src/dynarmic/backend/x64/a32_emit_x64_memory.cpp b/src/dynarmic/backend/x64/a32_emit_x64_memory.cpp index 210f347a..f2919485 100644 --- a/src/dynarmic/backend/x64/a32_emit_x64_memory.cpp +++ b/src/dynarmic/backend/x64/a32_emit_x64_memory.cpp @@ -132,7 +132,7 @@ void A32EmitX64::GenFastmemFallbacks() { } #define Axx A32 -#include "emit_x64_memory.cpp.inc" +#include "dynarmic/backend/x64/emit_x64_memory.cpp.inc" #undef Axx void A32EmitX64::EmitA32ReadMemory8(A32EmitContext& ctx, IR::Inst* inst) { diff --git a/src/dynarmic/backend/x64/a64_emit_x64_memory.cpp b/src/dynarmic/backend/x64/a64_emit_x64_memory.cpp index 5b6ab34d..450b16d0 100644 --- a/src/dynarmic/backend/x64/a64_emit_x64_memory.cpp +++ b/src/dynarmic/backend/x64/a64_emit_x64_memory.cpp @@ -280,7 +280,7 @@ void A64EmitX64::GenFastmemFallbacks() { } #define Axx A64 -#include "emit_x64_memory.cpp.inc" +#include "dynarmic/backend/x64/emit_x64_memory.cpp.inc" #undef Axx void A64EmitX64::EmitA64ReadMemory8(A64EmitContext& ctx, IR::Inst* inst) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 20f96588..e52f4da4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,5 @@ +include(TargetArchitectureSpecificSources) + add_executable(dynarmic_tests fp/FPToFixed.cpp fp/FPValue.cpp @@ -55,14 +57,14 @@ if (DYNARMIC_TESTS_USE_UNICORN) endif() endif() -if (ARCHITECTURE STREQUAL "x86_64") +if ("x86_64" IN_LIST ARCHITECTURE) target_link_libraries(dynarmic_tests PRIVATE xbyak::xbyak) - target_sources(dynarmic_tests PRIVATE + target_architecture_specific_sources(dynarmic_tests "x86_64" x64_cpu_info.cpp ) - if (NOT MSVC) + if (NOT MSVC AND NOT DYNARMIC_MULTIARCH_BUILD) target_sources(dynarmic_tests PRIVATE rsqrt_test.cpp rsqrt_test_fn.s