Skip to content

Commit 0a7a527

Browse files
committed
Add support for cross compiling.
Add support for not having host compiler as CMake cannot detect such a compiler. Add support for not using getarch. Successfully builds Android ARMV8. Any target can be added by supplying the TARGET_CORE config in prebuild.cmake.
1 parent 11911fd commit 0a7a527

File tree

2 files changed

+86
-11
lines changed

2 files changed

+86
-11
lines changed

cmake/prebuild.cmake

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,19 @@
3838
# CPUIDEMU = ../../cpuid/table.o
3939

4040

41-
# Cannot run getarch on target if we are cross-compiling
42-
if(NOT CMAKE_CROSSCOMPILING)
4341
if (DEFINED CPUIDEMU)
4442
set(EXFLAGS "-DCPUIDEMU -DVENDOR=99")
4543
endif ()
4644

45+
if(CMAKE_CROSSCOMPILING AND NOT DEFINED TARGET_CORE)
46+
# Detect target without running getarch
47+
if(AARCH64)
48+
set(TARGET_CORE "ARMV8")
49+
else()
50+
message(FATAL_ERROR "When cross compiling, a TARGET_CORE is required.")
51+
endif()
52+
endif()
53+
4754
if (DEFINED TARGET_CORE)
4855
# set the C flags for just this file
4956
set(GETARCH2_FLAGS "-DBUILD_KERNEL")
@@ -63,6 +70,73 @@ if (NOT NOFORTRAN)
6370
include("${PROJECT_SOURCE_DIR}/cmake/f_check.cmake")
6471
endif ()
6572

73+
# Cannot run getarch on target if we are cross-compiling
74+
if (CMAKE_CROSSCOMPILING)
75+
# Write to config as getarch would
76+
77+
# TODO: Set up defines that getarch sets up based on every other target
78+
# Perhaps this should be inside a different file as it grows larger
79+
file(APPEND ${TARGET_CONF_TEMP}
80+
"#define ${TARGET_CORE}\n"
81+
"#define CHAR_CORENAME \"${TARGET_CORE}\"\n")
82+
if ("${TARGET_CORE}" STREQUAL "ARMV8")
83+
file(APPEND ${TARGET_CONF_TEMP}
84+
"#define L1_DATA_SIZE\t32768\n"
85+
"#define L1_DATA_LINESIZE\t64\n"
86+
"#define L2_SIZE\t262144\n"
87+
"#define L2_LINESIZE\t64\n"
88+
"#define DTB_DEFAULT_ENTRIES\t64\n"
89+
"#define DTB_SIZE\t4096\n"
90+
"#define L2_ASSOCIATIVE\t32\n")
91+
set(SGEMM_UNROLL_M 4)
92+
set(SGEMM_UNROLL_N 4)
93+
elseif ("${TARGET_CORE}" STREQUAL "CORTEXA57")
94+
file(APPEND ${TARGET_CONF_TEMP}
95+
"#define L1_CODE_SIZE\t49152\n"
96+
"#define L1_CODE_LINESIZE\t64\n"
97+
"#define L1_CODE_ASSOCIATIVE\t3\n"
98+
"#define L1_DATA_SIZE\t32768\n"
99+
"#define L1_DATA_LINESIZE\t64\n"
100+
"#define L1_DATA_ASSOCIATIVE\t2\n"
101+
"#define L2_SIZE\t2097152\n"
102+
"#define L2_LINESIZE\t64\n"
103+
"#define L2_ASSOCIATIVE\t16\n"
104+
"#define DTB_DEFAULT_ENTRIES\t64\n"
105+
"#define DTB_SIZE\t4096\n"
106+
"#define HAVE_VFPV4\n"
107+
"#define HAVE_VFPV3\n"
108+
"#define HAVE_VFP\n"
109+
"#define HAVE_NEON\n")
110+
set(SGEMM_DEFAULT_UNROLL_M 16)
111+
set(SGEMM_DEFAULT_UNROLL_N 4)
112+
set(DGEMM_DEFAULT_UNROLL_M 8)
113+
set(DGEMM_DEFAULT_UNROLL_N 4)
114+
set(CGEMM_DEFAULT_UNROLL_M 8)
115+
set(CGEMM_DEFAULT_UNROLL_N 4)
116+
set(ZGEMM_DEFAULT_UNROLL_M 8)
117+
set(ZGEMM_DEFAULT_UNROLL_N 4)
118+
endif()
119+
120+
# Or should this actually be NUM_CORES?
121+
if (${NUM_THREADS} GREATER 0)
122+
file(APPEND ${TARGET_CONF_TEMP} "#define NUM_CORES\t${NUM_THREADS}\n")
123+
endif()
124+
125+
# GetArch_2nd
126+
foreach(float_char S;D;Q;C;Z;X)
127+
if (NOT DEFINED ${float_char}GEMM_UNROLL_M)
128+
set(${float_char}GEMM_UNROLL_M 2)
129+
endif()
130+
if (NOT DEFINED ${float_char}GEMM_UNROLL_N)
131+
set(${float_char}GEMM_UNROLL_N 2)
132+
endif()
133+
endforeach()
134+
file(APPEND ${TARGET_CONF_TEMP}
135+
"#define GEMM_MULTITHREAD_THRESHOLD\t${GEMM_MULTITHREAD_THRESHOLD}\n")
136+
# Move to where gen_config_h would place it
137+
file(RENAME ${TARGET_CONF_TEMP} "${PROJECT_BINARY_DIR}/config.h")
138+
139+
else()
66140
# compile getarch
67141
set(GETARCH_SRC
68142
${PROJECT_SOURCE_DIR}/getarch.c
@@ -161,4 +235,4 @@ if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
161235
endif ()
162236
endif ()
163237

164-
endif(NOT CMAKE_CROSSCOMPILING)
238+
endif(CMAKE_CROSSCOMPILING)

cmake/system.cmake

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ set(NETLIB_LAPACK_DIR "${PROJECT_SOURCE_DIR}/lapack-netlib")
1212
# TARGET_CORE will override TARGET which is used in DYNAMIC_ARCH=1.
1313
if (DEFINED TARGET_CORE)
1414
set(TARGET ${TARGET_CORE})
15+
if(NOT DEFINED CORE)
16+
set(CORE ${TARGET_CORE})
17+
endif()
1518
endif ()
1619

1720
# Force fallbacks for 32bit
@@ -72,8 +75,6 @@ else ()
7275
set(ONLY_CBLAS 0)
7376
endif ()
7477

75-
include("${PROJECT_SOURCE_DIR}/cmake/prebuild.cmake")
76-
7778
# N.B. this is NUM_THREAD in Makefile.system which is probably a bug -hpa
7879
if (NOT CMAKE_CROSSCOMPILING)
7980
if (NOT DEFINED NUM_CORES)
@@ -86,22 +87,22 @@ if (NOT CMAKE_CROSSCOMPILING)
8687
endif ()
8788
endif()
8889

90+
if (NOT DEFINED NUM_THREADS)
91+
set(NUM_THREADS 0)
92+
endif()
93+
8994
if (${NUM_THREADS} LESS 2)
9095
set(USE_THREAD 0)
9196
elseif(NOT DEFINED USE_THREAD)
9297
set(USE_THREAD 1)
9398
endif ()
9499

95-
# TODO: Fix. Isn't working. Was never working in CMake.
96-
# Undefined reference to get_num_procs, blas_thread_shutdown, ...
97-
if (UNIX)
98-
set(USE_THREAD 0)
99-
endif()
100-
101100
if (USE_THREAD)
102101
message(STATUS "Multi-threading enabled with ${NUM_THREADS} threads.")
103102
endif ()
104103

104+
include("${PROJECT_SOURCE_DIR}/cmake/prebuild.cmake")
105+
105106
if (NOT DEFINED NEED_PIC)
106107
set(NEED_PIC 1)
107108
endif ()

0 commit comments

Comments
 (0)