Skip to content

Commit

Permalink
Detect arch on mingw
Browse files Browse the repository at this point in the history
  • Loading branch information
clementperon committed Sep 4, 2023
1 parent 5168110 commit 9dbf732
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(PcapPlusPlusUtils)
include(GNUInstallDirs)
include(TargetArch)

# Setup CMake
pcapp_detect_compiler(PCAPPP_TARGET)

# Get architecture
target_architecture(PCAPP_TARGET_ARCHITECTURE)

# LINUX is set Only since 3.25 see: https://cmake.org/cmake/help/latest/variable/LINUX.html
if(UNIX
AND NOT APPLE
Expand Down Expand Up @@ -354,7 +358,8 @@ if(PCAPPP_PACKAGE)
elseif(MINGW)
# Check MinGW before WIN32 as MinGW defines both
set(CPACK_GENERATOR "ZIP")
set(CPACK_SYSTEM_NAME "windows-mingw64-${PCAPPP_TARGET_COMPILER}-${PCAPP_CXX_COMPILER_VERSION}")
set(CPACK_SYSTEM_NAME
"windows-mingw64-${PCAPP_TARGET_ARCHITECTURE}-${PCAPPP_TARGET_COMPILER}-${PCAPP_CXX_COMPILER_VERSION}")
set(_PCAPPP_PACKAGE_README_CUSTOM_PATH "${_PCAPPP_PACKAGE_README_PATH}/README.release.win.mingw")
elseif(WIN32)
set(CPACK_GENERATOR "ZIP")
Expand Down
93 changes: 93 additions & 0 deletions cmake/modules/TargetArch.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Based on the Qt 5 processor detection code, so should be very accurate
# https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h Currently handles arm (v5,
# v6, v7), x86 (32/64), ia64, and ppc (32/64)

set(archdetect_c_code
"
#if defined(__arm__) || defined(__TARGET_ARCH_ARM)
#if defined(__ARM_ARCH_7__) \\
|| defined(__ARM_ARCH_7A__) \\
|| defined(__ARM_ARCH_7R__) \\
|| defined(__ARM_ARCH_7M__) \\
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
#error cmake_ARCH armv7
#elif defined(__ARM_ARCH_6__) \\
|| defined(__ARM_ARCH_6J__) \\
|| defined(__ARM_ARCH_6T2__) \\
|| defined(__ARM_ARCH_6Z__) \\
|| defined(__ARM_ARCH_6K__) \\
|| defined(__ARM_ARCH_6ZK__) \\
|| defined(__ARM_ARCH_6M__) \\
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
#error cmake_ARCH armv6
#elif defined(__ARM_ARCH_5TEJ__) \\
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
#error cmake_ARCH armv5
#else
#error cmake_ARCH arm
#endif
#elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
#error cmake_ARCH i386
#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
#error cmake_ARCH x86_64
#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
#error cmake_ARCH ia64
#elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\
|| defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\
|| defined(_M_MPPC) || defined(_M_PPC)
#if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
#error cmake_ARCH ppc64
#else
#error cmake_ARCH ppc
#endif
#endif
#error cmake_ARCH unknown
")

# Set ppc_support to TRUE before including this file or ppc and ppc64 will be treated as invalid architectures since
# they are no longer supported by Apple

function(target_architecture output_var)
file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}")

enable_language(C)

# Detect the architecture in a rather creative way... This compiles a small C program which is a series of ifdefs that
# selects a particular #error preprocessor directive whose message string contains the target architecture. The
# program will always fail to compile (both because file is not a valid C program, and obviously because of the
# presence of the #error preprocessor directives... but by exploiting the preprocessor in this way, we can detect the
# correct target architecture even when cross-compiling, since the program itself never needs to be run (only the
# compiler/preprocessor)
try_run(
run_result_unused
compile_result_unused
"${CMAKE_BINARY_DIR}"
"${CMAKE_BINARY_DIR}/arch.c"
COMPILE_OUTPUT_VARIABLE ARCH
CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES})

# Parse the architecture name from the compiler output
string(
REGEX MATCH
"cmake_ARCH ([a-zA-Z0-9_]+)"
ARCH
"${ARCH}")

# Get rid of the value marker leaving just the architecture name
string(
REPLACE "cmake_ARCH "
""
ARCH
"${ARCH}")

# If we are compiling with an unknown architecture this variable should already be set to "unknown" but in the case
# that it's empty (i.e. due to a typo in the code), then set it to unknown
if(NOT ARCH)
set(ARCH unknown)
endif()

set(${output_var}
"${ARCH}"
PARENT_SCOPE)
endfunction()

0 comments on commit 9dbf732

Please sign in to comment.