Skip to content

Commit 475aee8

Browse files
committed
[cmake] Add a target arch related info file.
- This is taken from https://github.com/axr/solar-cmake/blob/master/TargetArch.cmake It seems to be a good way to detect the arch of the target we are compiling for. I will move more arch related variables here later. They are right now scattered around in the CMake files.
1 parent f55cf54 commit 475aee8

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
## Taken from https://github.com/axr/solar-cmake/blob/master/TargetArch.cmake with love.
2+
## Modified to define OMC_TARGET_ARCH here. Very minor change.
3+
##
4+
## Hopefully can detect arch properly.
5+
6+
# Based on the Qt 5 processor detection code, so should be very accurate
7+
# https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h
8+
# Currently handles arm (v5, v6, v7), x86 (32/64), ia64, and ppc (32/64)
9+
10+
# Regarding POWER/PowerPC, just as is noted in the Qt source,
11+
# "There are many more known variants/revisions that we do not handle/detect."
12+
13+
set(archdetect_c_code "
14+
#if defined(__arm__) || defined(__TARGET_ARCH_ARM)
15+
#if defined(__ARM_ARCH_7__) \\
16+
|| defined(__ARM_ARCH_7A__) \\
17+
|| defined(__ARM_ARCH_7R__) \\
18+
|| defined(__ARM_ARCH_7M__) \\
19+
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
20+
#error cmake_ARCH armv7
21+
#elif defined(__ARM_ARCH_6__) \\
22+
|| defined(__ARM_ARCH_6J__) \\
23+
|| defined(__ARM_ARCH_6T2__) \\
24+
|| defined(__ARM_ARCH_6Z__) \\
25+
|| defined(__ARM_ARCH_6K__) \\
26+
|| defined(__ARM_ARCH_6ZK__) \\
27+
|| defined(__ARM_ARCH_6M__) \\
28+
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
29+
#error cmake_ARCH armv6
30+
#elif defined(__ARM_ARCH_5TEJ__) \\
31+
|| (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
32+
#error cmake_ARCH armv5
33+
#else
34+
#error cmake_ARCH arm
35+
#endif
36+
#elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
37+
#error cmake_ARCH i386
38+
#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
39+
#error cmake_ARCH x86_64
40+
#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
41+
#error cmake_ARCH ia64
42+
#elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\
43+
|| defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\
44+
|| defined(_M_MPPC) || defined(_M_PPC)
45+
#if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
46+
#error cmake_ARCH ppc64
47+
#else
48+
#error cmake_ARCH ppc
49+
#endif
50+
#endif
51+
#error cmake_ARCH unknown
52+
")
53+
54+
# Set ppc_support to TRUE before including this file or ppc and ppc64
55+
# will be treated as invalid architectures since they are no longer supported by Apple
56+
57+
function(target_architecture output_var)
58+
if(APPLE AND CMAKE_OSX_ARCHITECTURES)
59+
# On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
60+
# First let's normalize the order of the values
61+
62+
# Note that it's not possible to compile PowerPC applications if you are using
63+
# the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
64+
# disable it by default
65+
# See this page for more information:
66+
# http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4
67+
68+
# Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
69+
# On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise.
70+
71+
foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES})
72+
if("${osx_arch}" STREQUAL "ppc" AND ppc_support)
73+
set(osx_arch_ppc TRUE)
74+
elseif("${osx_arch}" STREQUAL "i386")
75+
set(osx_arch_i386 TRUE)
76+
elseif("${osx_arch}" STREQUAL "x86_64")
77+
set(osx_arch_x86_64 TRUE)
78+
elseif("${osx_arch}" STREQUAL "ppc64" AND ppc_support)
79+
set(osx_arch_ppc64 TRUE)
80+
else()
81+
message(FATAL_ERROR "Invalid OS X arch name: ${osx_arch}")
82+
endif()
83+
endforeach()
84+
85+
# Now add all the architectures in our normalized order
86+
if(osx_arch_ppc)
87+
list(APPEND ARCH ppc)
88+
endif()
89+
90+
if(osx_arch_i386)
91+
list(APPEND ARCH i386)
92+
endif()
93+
94+
if(osx_arch_x86_64)
95+
list(APPEND ARCH x86_64)
96+
endif()
97+
98+
if(osx_arch_ppc64)
99+
list(APPEND ARCH ppc64)
100+
endif()
101+
else()
102+
file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}")
103+
104+
enable_language(C)
105+
106+
# Detect the architecture in a rather creative way...
107+
# This compiles a small C program which is a series of ifdefs that selects a
108+
# particular #error preprocessor directive whose message string contains the
109+
# target architecture. The program will always fail to compile (both because
110+
# file is not a valid C program, and obviously because of the presence of the
111+
# #error preprocessor directives... but by exploiting the preprocessor in this
112+
# way, we can detect the correct target architecture even when cross-compiling,
113+
# since the program itself never needs to be run (only the compiler/preprocessor)
114+
try_run(
115+
run_result_unused
116+
compile_result_unused
117+
"${CMAKE_BINARY_DIR}"
118+
"${CMAKE_BINARY_DIR}/arch.c"
119+
COMPILE_OUTPUT_VARIABLE ARCH
120+
CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
121+
)
122+
123+
# Parse the architecture name from the compiler output
124+
string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}")
125+
126+
# Get rid of the value marker leaving just the architecture name
127+
string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}")
128+
129+
# If we are compiling with an unknown architecture this variable should
130+
# already be set to "unknown" but in the case that it's empty (i.e. due
131+
# to a typo in the code), then set it to unknown
132+
if (NOT ARCH)
133+
set(ARCH unknown)
134+
endif()
135+
endif()
136+
137+
set(${output_var} "${ARCH}" PARENT_SCOPE)
138+
endfunction()
139+
140+
# Use the function to set OMC_TARGET_ARCH
141+
target_architecture(OMC_TARGET_ARCH)
142+
omc_add_to_report(OMC_TARGET_ARCH)

OMCompiler/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
2222

2323
# include utility macros.
2424
include(.cmake/omc_utils.cmake)
25+
include(.cmake/omc_target_info.cmake)
2526
include(.cmake/omc_check_exists.cmake)
2627

2728
# Add the compiler ids to the report for convenience.

0 commit comments

Comments
 (0)