Skip to content

Commit 42eb626

Browse files
committed
detect architecture
1 parent 63a3c89 commit 42eb626

File tree

2 files changed

+173
-63
lines changed

2 files changed

+173
-63
lines changed

CMakeLists.txt

+5-63
Original file line numberDiff line numberDiff line change
@@ -456,71 +456,13 @@ endif()
456456
# cflags = -DOPENSSL_USE_APPLINK
457457
# cpuid_obj += uplink.o uplink-x86.o
458458

459+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
460+
461+
include(OpenSslExternalCMakeCheckArchitecture)
462+
459463
# undocumented!
460464
if ( NOT OPENSSL_TARGET_ARCH )
461-
set(OPENSSL_TARGET_ARCH_GET ${CMAKE_SYSTEM_PROCESSOR})
462-
set(OPENSSL_TARGET_ARCH)
463-
464-
# !!! for some platform host can run multiple architecture.
465-
# e.g. Windows amd64 can run / build Windows x86 in NATIVE.
466-
# Linux amd64 can run / build Linux x86 in NATIVE.
467-
# macOS arm64 can run / build macOS x86_64 in NATIVE.
468-
# need to figure out all these conditions?
469-
470-
# for cross build on Apple platform
471-
if (APPLE AND ( NOT IOS ) AND CMAKE_OSX_ARCHITECTURES)
472-
list(LENGTH CMAKE_OSX_ARCHITECTURES OPENSSL_OSX_ARCHITECTURES_LENGTH)
473-
if (OPENSSL_OSX_ARCHITECTURES_LENGTH GREATER 1)
474-
if (OPENSSL_ASM)
475-
# TODO: complete this when CMake supports building different sets of file for different architectures
476-
# Currently only method for implementing it is to generate 2 set of libraries and use 'lipo' to combine them, since the compile flags are totally different.
477-
message(FATAL_ERROR "OPENSSL_ASM is incompatible with more than one CMAKE_OSX_ARCHITECTURES set on macOS, due to the file and compiler flags used for building it "
478-
"are totally different. "
479-
"If a multi-architecture build should be done, either build the all-in-one package with OPENSSL_ASM set to OFF or build them separately "
480-
"with OPENSSL_ASM set to ON and combine them using lipo provided by Apple.")
481-
endif()
482-
elseif (OPENSSL_OSX_ARCHITECTURES_LENGTH EQUAL 1)
483-
# the only target is the one from CMAKE_OSX_ARCHITECTURES
484-
list(GET CMAKE_OSX_ARCHITECTURES 0 OPENSSL_TARGET_ARCH_GET)
485-
else()
486-
# use CMAKE_SYSTEM_PROCESSOR directly since it is a fixed host build
487-
endif()
488-
elseif ( (NOT CMAKE_CROSSCOMPILING) OR ( CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME ) )
489-
# treat this is host build if CMAKE_CROSSCOMPILING is not set or CMAKE_SYSTEM_NAME equals CMAKE_HST_SYSTEM_NAME
490-
# Normally CMake treats current build is cross build if CMAKE_SYSTEM_NAME is manually set
491-
# but it's not the case for Apple platforms!!
492-
493-
if (MSVC)
494-
if ( ( DEFINED ENV{VSCMD_ARG_TGT_ARCH} ) AND ( NOT ( "x$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "x" ) ) )
495-
# MSVC 2017 and later sets environment variable VSCMD_ARG_TGT_ARCH on execution of vcvars*.bat
496-
set(OPENSSL_TARGET_ARCH_GET $ENV{VSCMD_ARG_TGT_ARCH})
497-
elseif ( ( DEFINED ENV{PLATFORM} ) AND ( NOT ( "x$ENV{PLATFORM}" STREQUAL "x" ) ) )
498-
# MSVC 2015 sets environment variable PLATFORM on execution of vcvars*.bat
499-
set(OPENSSL_TARGET_ARCH_GET $ENV{PLATFORM})
500-
else()
501-
message(FATAL_ERROR "Please run vcvars*.bat before building using MSVC.")
502-
endif()
503-
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
504-
# TODO: need to figure out if this is proper
505-
string(REGEX REPLACE "[\\-_]?64" "" OPENSSL_TARGET_ARCH_GET ${CMAKE_SYSTEM_PROCESSOR})
506-
string(TOUPPER ${OPENSSL_TARGET_ARCH_GET} OPENSSL_TARGET_ARCH_UPPER)
507-
if (OPENSSL_TARGET_ARCH_UPPER STREQUAL "AMD")
508-
set(OPENSSL_TARGET_ARCH_GET "x86")
509-
endif()
510-
endif()
511-
endif()
512-
513-
if ( ( OPENSSL_TARGET_ARCH_GET MATCHES "[Aa][Mm][Dd]64" ) OR ( OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]86[_\\-]64" ) OR ( OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]64" ))
514-
set(OPENSSL_TARGET_ARCH "x64")
515-
elseif ( ( OPENSSL_TARGET_ARCH_GET MATCHES "i[2-7]86" ) OR ( OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]86" ) )
516-
set(OPENSSL_TARGET_ARCH "x86")
517-
elseif ( ( OPENSSL_TARGET_ARCH_GET MATCHES "[Aa][Rr][Mm]") OR ( OPENSSL_TARGET_ARCH_GET MATCHES "aarch" ) )
518-
if (OPENSSL_TARGET_ARCH_GET MATCHES "64")
519-
set(OPENSSL_TARGET_ARCH "arm64")
520-
else()
521-
set(OPENSSL_TARGET_ARCH "arm32")
522-
endif()
523-
endif()
465+
openssl_external_cmake_detect_target_arch(OPENSSL_TARGET_ARCH)
524466
endif()
525467

526468
# 386 and SSE2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# SPDX-License-Identifier: Unlicense
2+
3+
# CMake's builtin method (${CMAKE_SYSTEM_PROCERSSOR}) does not provide an accurate information, so we'd do it ourselves.
4+
5+
include(CheckSourceCompiles)
6+
7+
macro(openssl_external_cmake_detect_target_arch arch)
8+
# Known conversions: Apple can define CMAKE_OSX_ARCHITECTURES, which will result in a multi-architecture build
9+
if (APPLE AND CMAKE_OSX_ARCHITECTURES)
10+
list(LENGTH CMAKE_OSX_ARCHITECTURES OPENSSL_OSX_ARCHITECTURES_LENGTH)
11+
if (OPENSSL_OSX_ARCHITECTURES_LENGTH GREATER 1)
12+
if (OPENSSL_ASM)
13+
# TODO: complete this when CMake supports building different sets of file for different architectures
14+
# Currently only method for implementing it is to generate 2 set of libraries and use 'lipo' to combine them, since the compile flags are totally different.
15+
message(FATAL_ERROR "OPENSSL_ASM is incompatible with more than one CMAKE_OSX_ARCHITECTURES set on macOS, due to the file and compiler flags used for building it "
16+
"are totally different. "
17+
"If a multi-architecture build should be done, either build the all-in-one package with OPENSSL_ASM set to OFF or build them separately "
18+
"with OPENSSL_ASM set to ON and combine them using lipo provided by Apple.")
19+
endif()
20+
unset("${arch}")
21+
endif()
22+
else()
23+
# https://github.com/cpredef/predef/blob/master/Architectures.md (2023/7/2)
24+
25+
set(__openssl_external_cmake_detect_target_arch_alpha_IDENTIFICATION __alpha__ __alpha _M_ALPHA)
26+
set(__openssl_external_cmake_detect_target_arch_x64_IDENTIFICATION __amd64__ __amd64 __x86_64__ __x86_64 _M_X64 _M_AMD64)
27+
# CONSIDER: arm versions?
28+
set(__openssl_external_cmake_detect_target_arch_arm32_IDENTIFICATION __arm__ __thumb__ __TARGET_ARCH_ARM __TARGET_ARCH_THUMB _ARM _M_ARM _M_ARMT __arm)
29+
set(__openssl_external_cmake_detect_target_arch_arm64_IDENTIFICATION __aarch64__ _M_ARM64)
30+
set(__openssl_external_cmake_detect_target_arch_parisc_IDENTIFICATION __hppa__ __HPPA__ __hppa)
31+
set(__openssl_external_cmake_detect_target_arch_x86_IDENTIFICATION i386 __i386 __i386__ __IA32__ _M_IX86 __X86__ _X86_ __THW_INTEL__ __I86__ __INTEL__ __386)
32+
set(__openssl_external_cmake_detect_target_arch_ia64_IDENTIFICATION __ia64__ _IA64 __IA64__ __ia64 _M_IA64 __itanium__)
33+
# CONSIDER: la464 support?
34+
set(__openssl_external_cmake_detect_target_arch_loongarch_IDENTIFICATION __loongarch__ _LOONGARCH_ARCH)
35+
set(__openssl_external_cmake_detect_target_arch_m68k_IDENTIFICATION __m68k__ M68000 __MC68K__)
36+
# CONSIDER: mips versions? documentation of mips is unclear, maybe I'd find resources on Wikipedia
37+
set(__openssl_external_cmake_detect_target_arch_mips_IDENTIFICATION __mips__ mips __mips __MIPS__)
38+
set(__openssl_external_cmake_detect_target_arch_ppc_IDENTIFICATION __powerpc __powerpc__ __powerpc64__ __POWERPC__ __ppc__ __ppc64__ __PPC__ __PPC64__ _ARCH_PPC _ARCH_PPC64 _M_PPC __ppc)
39+
# CONSIDER: RISCV: Bit is determined in other macro
40+
set(__openssl_external_cmake_detect_target_arch_riscv_IDENTIFICATION __riscv __riscv__)
41+
# CONSIDER: separate sparcv7/v8/v9
42+
set(__openssl_external_cmake_detect_target_arch_sparc_IDENTIFICATION __sparc__ __sparc)
43+
set(__openssl_external_cmake_detect_target_arch_sh_IDENTIFICATION __sh__)
44+
# CONSIDER: separate s370/s390/s390x
45+
set(__openssl_external_cmake_detect_target_arch_systemz_IDENTIFICATION __370__ __THW_370__ __s390__ __s390x__ __zarch__ __SYSC_ZARCH__)
46+
47+
# ARM64: must be checked before ARM (32-bit)
48+
# IA64, AMD64: must be checked before x86, although we'd not like to support ia64 soon
49+
# others are checked by priority, then popularity. LoongArch is prioritized for made by China!
50+
set(__openssl_external_cmake_detect_target_arch_IDENTIFICATION_LISTS x64 arm64 arm32 ia64 x86 loongarch riscv mips ppc parisc systemz sparc sh alpha m68k)
51+
unset(__openssl_external_cmake_detect_target_arch_IDENTIFIED_ARCH)
52+
foreach (__openssl_external_cmake_detect_target_arch_I IN LISTS __openssl_external_cmake_detect_target_arch_IDENTIFICATION_LISTS)
53+
set(__openssl_external_cmake_detect_target_arch_VAR "__openssl_external_cmake_detect_target_arch_${__openssl_external_cmake_detect_target_arch_I}_IDENTIFICATION")
54+
set(__openssl_external_cmake_detect_target_arch_SRC "#")
55+
foreach (__openssl_external_cmake_detect_target_arch_J IN LISTS ${__openssl_external_cmake_detect_target_arch_VAR})
56+
# The definitions are all macros so do not use the CheckSymbolExists
57+
set(__openssl_external_cmake_detect_target_arch_SRC "${__openssl_external_cmake_detect_target_arch_SRC}if defined(${__openssl_external_cmake_detect_target_arch_J})
58+
#el")
59+
endforeach()
60+
set(__openssl_external_cmake_detect_target_arch_SRC "${__openssl_external_cmake_detect_target_arch_SRC}se
61+
#error \"Macro for current architecture not found\"
62+
#endif
63+
64+
int main(int argc, char *argv[]) {
65+
(void)argc;
66+
(void)argv;
67+
return 0;
68+
}")
69+
unset(__openssl_external_cmake_detect_target_arch_SRC_COMPILES)
70+
unset(__openssl_external_cmake_detect_target_arch_SRC_COMPILES CACHE)
71+
check_source_compiles(C "${__openssl_external_cmake_detect_target_arch_SRC}" __openssl_external_cmake_detect_target_arch_SRC_COMPILES)
72+
if (__openssl_external_cmake_detect_target_arch_SRC_COMPILES)
73+
set(__openssl_external_cmake_detect_target_arch_IDENTIFIED_ARCH "${__openssl_external_cmake_detect_target_arch_I}")
74+
break()
75+
endif()
76+
endforeach()
77+
78+
if (DEFINED __openssl_external_cmake_detect_target_arch_IDENTIFIED_ARCH)
79+
set("${arch}" "${__openssl_external_cmake_detect_target_arch_IDENTIFIED_ARCH}")
80+
message(NOTICE "Detected architecture - ${__openssl_external_cmake_detect_target_arch_IDENTIFIED_ARCH}")
81+
# TODO: consider above:
82+
# CONSIDER: ARM: arm versions?
83+
# CONSIDER: LoongArch: la464 support?
84+
# CONSIDER: MIPS: versions? documentation of mips is unclear, maybe I'd find resources on Wikipedia
85+
# CONSIDER: RISCV: Bit is determined in other macro
86+
# CONSIDER: SPARC: separate sparcv7/v8/v9
87+
# CONSIDER: SYSTEMZ: separate s370/s390/s390x
88+
else()
89+
if (OPENSSL_ASM)
90+
message(FATAL_ERROR "No supported architecture found. Since ASM only works with a known architecture please specify -DOPENSSL_ASM=NO during configure")
91+
else()
92+
message(WARNING "No supported architecture found.")
93+
unset("${arch}")
94+
endif()
95+
endif()
96+
endif()
97+
endmacro()
98+
99+
# Legacy method: read CMAKE_SYSTEM_PROCESSOR and CMAKE_SIZEOF_VOID_P
100+
101+
macro(openssl_external_cmake_detect_target_arch_legacy arch)
102+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET ${CMAKE_SYSTEM_PROCESSOR})
103+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH)
104+
105+
# !!! for some platform host can run multiple architecture.
106+
# e.g. Windows amd64 can run / build Windows x86 in NATIVE.
107+
# Linux amd64 can run / build Linux x86 in NATIVE.
108+
# macOS arm64 can run / build macOS x86_64 in NATIVE.
109+
# need to figure out all these conditions?
110+
111+
# for cross build on Apple platform
112+
if (APPLE AND ( NOT IOS ) AND CMAKE_OSX_ARCHITECTURES)
113+
list(LENGTH CMAKE_OSX_ARCHITECTURES OPENSSL_OSX_ARCHITECTURES_LENGTH)
114+
if (OPENSSL_OSX_ARCHITECTURES_LENGTH GREATER 1)
115+
if (OPENSSL_ASM)
116+
# TODO: complete this when CMake supports building different sets of file for different architectures
117+
# Currently only method for implementing it is to generate 2 set of libraries and use 'lipo' to combine them, since the compile flags are totally different.
118+
message(FATAL_ERROR "OPENSSL_ASM is incompatible with more than one CMAKE_OSX_ARCHITECTURES set on macOS, due to the file and compiler flags used for building it "
119+
"are totally different. "
120+
"If a multi-architecture build should be done, either build the all-in-one package with OPENSSL_ASM set to OFF or build them separately "
121+
"with OPENSSL_ASM set to ON and combine them using lipo provided by Apple.")
122+
endif()
123+
elseif (OPENSSL_OSX_ARCHITECTURES_LENGTH EQUAL 1)
124+
# the only target is the one from CMAKE_OSX_ARCHITECTURES
125+
list(GET CMAKE_OSX_ARCHITECTURES 0 __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET)
126+
else()
127+
# use CMAKE_SYSTEM_PROCESSOR directly since it is a fixed host build
128+
endif()
129+
elseif ( (NOT CMAKE_CROSSCOMPILING) OR ( CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME ) )
130+
# treat this is host build if CMAKE_CROSSCOMPILING is not set or CMAKE_SYSTEM_NAME equals CMAKE_HST_SYSTEM_NAME
131+
# Normally CMake treats current build is cross build if CMAKE_SYSTEM_NAME is manually set
132+
# but it's not the case for Apple platforms!!
133+
134+
if (MSVC)
135+
if ( ( DEFINED ENV{VSCMD_ARG_TGT_ARCH} ) AND ( NOT ( "x$ENV{VSCMD_ARG_TGT_ARCH}" STREQUAL "x" ) ) )
136+
# MSVC 2017 and later sets environment variable VSCMD_ARG_TGT_ARCH on execution of vcvars*.bat
137+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET $ENV{VSCMD_ARG_TGT_ARCH})
138+
elseif ( ( DEFINED ENV{PLATFORM} ) AND ( NOT ( "x$ENV{PLATFORM}" STREQUAL "x" ) ) )
139+
# MSVC 2015 sets environment variable PLATFORM on execution of vcvars*.bat
140+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET $ENV{PLATFORM})
141+
else()
142+
message(FATAL_ERROR "Please run vcvars*.bat before building using MSVC.")
143+
endif()
144+
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
145+
# TODO: need to figure out if this is proper
146+
string(REGEX REPLACE "[\\-_]?64" "" __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET ${CMAKE_SYSTEM_PROCESSOR})
147+
string(TOUPPER ${__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET} __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_UPPER)
148+
if (__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_UPPER STREQUAL "AMD")
149+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET "x86")
150+
endif()
151+
endif()
152+
endif()
153+
154+
if ( ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "[Aa][Mm][Dd]64" ) OR ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]86[_\\-]64" ) OR ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]64" ))
155+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH "x64")
156+
elseif ( ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "i[2-7]86" ) OR ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "[Xx]86" ) )
157+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH "x86")
158+
elseif ( ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "[Aa][Rr][Mm]") OR ( __openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "aarch" ) )
159+
if (__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET MATCHES "64")
160+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH "arm64")
161+
else()
162+
set(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH "arm32")
163+
endif()
164+
endif()
165+
set(${arch} ${__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH})
166+
unset(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH_GET)
167+
unset(__openssl_external_cmake_detect_target_arch_OPENSSL_TARGET_ARCH)
168+
endmacro()

0 commit comments

Comments
 (0)