summaryrefslogtreecommitdiff
path: root/neozip/cmake/detect-arch.cmake
blob: 991fe18296e83588f18349aad9946fb2f3e89982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# detect-arch.cmake -- Detect compiler architecture and set ARCH and BASEARCH
# Copyright (C) 2019 Hans Kristian Rosbach
# Licensed under the Zlib license, see LICENSE.md for details
if(CMAKE_OSX_ARCHITECTURES)
    # If multiple architectures are requested (universal build), pick only the first
    list(GET CMAKE_OSX_ARCHITECTURES 0 ARCH)
elseif(MSVC)
    set(ARCH ${MSVC_C_ARCHITECTURE_ID})
elseif(EMSCRIPTEN)
    set(ARCH "wasm32")
endif()

if(NOT ARCH OR ARCH STREQUAL "")
    # Compile detect-arch.c and read the architecture name from the binary
    try_compile(
        COMPILE_RESULT
        ${CMAKE_CURRENT_BINARY_DIR}
        ${CMAKE_CURRENT_LIST_DIR}/detect-arch.c
        CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
        COPY_FILE ${CMAKE_CURRENT_BINARY_DIR}/detect-arch.bin
    )
    if(COMPILE_RESULT)
        # Find archfound tag, and extract the arch word into ARCH variable
        file(STRINGS ${CMAKE_CURRENT_BINARY_DIR}/detect-arch.bin RAWOUTPUT REGEX "archfound [a-zA-Z0-9_]+")
        string(REGEX REPLACE ".*archfound ([a-zA-Z0-9_]+).*" "\\1" ARCH "${RAWOUTPUT}")

        # Find archversion tag, and extract the archversion word into ARCHVERSION variable
        file(STRINGS ${CMAKE_CURRENT_BINARY_DIR}/detect-arch.bin RAWOUTPUT REGEX "archversion [0-9]+")
        string(REGEX REPLACE ".*archversion ([0-9]+).*" "\\1" ARCHVERSION "${RAWOUTPUT}")
    endif()

    if(NOT ARCH)
        set(ARCH unknown)
    endif()
    if(NOT ARCHVERSION)
        set(ARCHVERSION 0)
    endif()
endif()

# Make sure we have ARCH set
if(NOT ARCH OR ARCH STREQUAL "unknown")
    set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
    message(STATUS "Arch not recognized, falling back to cmake arch: '${ARCH}'")
else()
    message(STATUS "Arch detected: '${ARCH}'")
endif()

# Convert ARCH to lowercase
string(TOLOWER "${ARCH}" ARCH)

# Base arch detection
if("${ARCH}" MATCHES "(x86(_32|_64)?|amd64|x64|i[3-6]86)")
    set(BASEARCH "x86")
    set(BASEARCH_X86_FOUND TRUE)
    if("${ARCH}" MATCHES "(x86_64|amd64|x64)")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "(aarch64|arm64(ec)?|aarch32|arm(v[0-9])?|cortex)")
    set(BASEARCH "arm")
    set(BASEARCH_ARM_FOUND TRUE)
    if("${ARCH}" MATCHES "(aarch64|arm64(ec)?)")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "(ppc|powerpc)(64)?(le)?")
    set(BASEARCH "ppc")
    set(BASEARCH_PPC_FOUND TRUE)
    if("${ARCH}" MATCHES "(ppc|powerpc)64(le)?")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "alpha")
    set(BASEARCH "alpha")
    set(BASEARCH_ALPHA_FOUND TRUE)
    set(ARCH_BITS 64)
elseif("${ARCH}" MATCHES "blackfin")
    set(BASEARCH "blackfin")
    set(BASEARCH_BLACKFIN_FOUND TRUE)
    set(ARCH_BITS 32)
elseif("${ARCH}" MATCHES "ia64")
    set(BASEARCH "ia64")
    set(BASEARCH_IA64_FOUND TRUE)
    set(ARCH_BITS 64)
elseif("${ARCH}" MATCHES "mips(isa)?(64)?")
    set(BASEARCH "mips")
    set(BASEARCH_MIPS_FOUND TRUE)
    if("${ARCH}" MATCHES "mips(isa)?64")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "m68k")
    set(BASEARCH "m68k")
    set(BASEARCH_M68K_FOUND TRUE)
    set(ARCH_BITS 32)
elseif("${ARCH}" MATCHES "sh")
    set(BASEARCH "sh")
    set(BASEARCH_SH_FOUND TRUE)
    set(ARCH_BITS 32)
elseif("${ARCH}" MATCHES "sparc(v)?[89]?(64)?")
    set(BASEARCH "sparc")
    set(BASEARCH_SPARC_FOUND TRUE)
    if("${ARCH}" MATCHES "(sparc64|sparc(v)?9)")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "s3[679]0x?")
    set(BASEARCH "s360")
    set(BASEARCH_S360_FOUND TRUE)
    if("${ARCH}" MATCHES "s3[679]0x")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "(parisc|hppa)(64)?")
    set(BASEARCH "parisc")
    set(BASEARCH_PARISC_FOUND TRUE)
    if("${ARCH}" MATCHES "(parisc|hppa)64")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "rs6000")
    set(BASEARCH "rs6000")
    set(BASEARCH_RS6000_FOUND TRUE)
    set(ARCH_BITS 32)
elseif("${ARCH}" MATCHES "riscv(32|64)")
    set(BASEARCH "riscv")
    set(BASEARCH_RISCV_FOUND TRUE)
    if("${ARCH}" MATCHES "riscv64")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "(loong64|loongarch64)")
    set(BASEARCH "loongarch")
    set(BASEARCH_LOONGARCH_FOUND TRUE)
    set(ARCH_BITS 64)
elseif("${ARCH}" MATCHES "wasm(32|64)")
    set(BASEARCH "wasm32")
    set(BASEARCH_WASM32_FOUND TRUE)
    if("${ARCH}" MATCHES "wasm64")
        set(ARCH_BITS 64)
    else()
        set(ARCH_BITS 32)
    endif()
elseif("${ARCH}" MATCHES "e2k")
    set(BASEARCH "e2k")
    set(BASEARCH_E2K_FOUND TRUE)
    set(ARCH_BITS 64)

    message(STATUS "Arch version ${BASEARCH}v${ARCHVERSION}.")
else()
    set(BASEARCH "x86")
    set(BASEARCH_X86_FOUND TRUE)
    set(ARCH_BITS 32)
    message(STATUS "Basearch '${ARCH}' not recognized, defaulting to 'x86'.")
endif()

if (ARCH_BITS EQUAL 64)
    set(ARCH_64BIT TRUE)
    set(ARCH_32BIT FALSE)
else()
    set(ARCH_64BIT FALSE)
    set(ARCH_32BIT TRUE)
endif()

message(STATUS "Basearch of '${ARCH}' (${ARCH_BITS}bit) has been detected as: '${BASEARCH}'")