You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

prebuild.cmake 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ##
  2. ## Author: Hank Anderson <hank@statease.com>
  3. ## Description: Ported from OpenBLAS/Makefile.prebuild
  4. ## This is triggered by system.cmake and runs before any of the code is built.
  5. ## Creates config.h and Makefile.conf by first running the c_check perl script (which creates those files).
  6. ## Next it runs f_check and appends some fortran information to the files.
  7. ## Then it runs getarch and getarch_2nd for even more environment information.
  8. ## Finally it builds gen_config_h for use at build time to generate config.h.
  9. # CMake vars set by this file:
  10. # CORE
  11. # LIBCORE
  12. # NUM_CORES
  13. # HAVE_MMX
  14. # HAVE_SSE
  15. # HAVE_SSE2
  16. # HAVE_SSE3
  17. # MAKE
  18. # SGEMM_UNROLL_M
  19. # SGEMM_UNROLL_N
  20. # DGEMM_UNROLL_M
  21. # DGEMM_UNROLL_M
  22. # QGEMM_UNROLL_N
  23. # QGEMM_UNROLL_N
  24. # CGEMM_UNROLL_M
  25. # CGEMM_UNROLL_M
  26. # ZGEMM_UNROLL_N
  27. # ZGEMM_UNROLL_N
  28. # XGEMM_UNROLL_M
  29. # XGEMM_UNROLL_N
  30. # CGEMM3M_UNROLL_M
  31. # CGEMM3M_UNROLL_N
  32. # ZGEMM3M_UNROLL_M
  33. # ZGEMM3M_UNROLL_M
  34. # XGEMM3M_UNROLL_N
  35. # XGEMM3M_UNROLL_N
  36. # CPUIDEMU = ../../cpuid/table.o
  37. if (DEFINED CPUIDEMU)
  38. set(EXFLAGS "-DCPUIDEMU -DVENDOR=99")
  39. endif ()
  40. if (BUILD_KERNEL)
  41. # set the C flags for just this file
  42. set(GETARCH2_FLAGS "-DBUILD_KERNEL")
  43. set(TARGET_CONF "config_kernel.h")
  44. set(TARGET_CONF_DIR ${PROJECT_BINARY_DIR}/kernel_config/${TARGET_CORE})
  45. else()
  46. set(TARGET_CONF "config.h")
  47. set(TARGET_CONF_DIR ${PROJECT_BINARY_DIR})
  48. endif ()
  49. set(TARGET_CONF_TEMP "${PROJECT_BINARY_DIR}/${TARGET_CONF}.tmp")
  50. # c_check
  51. set(FU "")
  52. if (APPLE OR (MSVC AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "Clang"))
  53. set(FU "_")
  54. endif()
  55. set(COMPILER_ID ${CMAKE_C_COMPILER_ID})
  56. if (${COMPILER_ID} STREQUAL "GNU")
  57. set(COMPILER_ID "GCC")
  58. endif ()
  59. string(TOUPPER ${ARCH} UC_ARCH)
  60. file(WRITE ${TARGET_CONF_TEMP}
  61. "#define OS_${HOST_OS}\t1\n"
  62. "#define ARCH_${UC_ARCH}\t1\n"
  63. "#define C_${COMPILER_ID}\t1\n"
  64. "#define __${BINARY}BIT__\t1\n"
  65. "#define FUNDERSCORE\t${FU}\n")
  66. if (${HOST_OS} STREQUAL "WINDOWSSTORE")
  67. file(APPEND ${TARGET_CONF_TEMP}
  68. "#define OS_WINNT\t1\n")
  69. endif ()
  70. # f_check
  71. if (NOT NOFORTRAN)
  72. include("${PROJECT_SOURCE_DIR}/cmake/f_check.cmake")
  73. endif ()
  74. # Cannot run getarch on target if we are cross-compiling
  75. if (DEFINED CORE AND CMAKE_CROSSCOMPILING AND NOT (${HOST_OS} STREQUAL "WINDOWSSTORE"))
  76. # Write to config as getarch would
  77. if (DEFINED TARGET_CORE)
  78. set(TCORE ${TARGET_CORE})
  79. else()
  80. set(TCORE ${CORE})
  81. endif()
  82. # TODO: Set up defines that getarch sets up based on every other target
  83. # Perhaps this should be inside a different file as it grows larger
  84. file(APPEND ${TARGET_CONF_TEMP}
  85. "#define ${TCORE}\n"
  86. "#define CHAR_CORENAME \"${TCORE}\"\n")
  87. if ("${TCORE}" STREQUAL "ARMV7")
  88. file(APPEND ${TARGET_CONF_TEMP}
  89. "#define L1_DATA_SIZE\t65536\n"
  90. "#define L1_DATA_LINESIZE\t32\n"
  91. "#define L2_SIZE\t512488\n"
  92. "#define L2_LINESIZE\t32\n"
  93. "#define DTB_DEFAULT_ENTRIES\t64\n"
  94. "#define DTB_SIZE\t4096\n"
  95. "#define L2_ASSOCIATIVE\t4\n"
  96. "#define HAVE_VFPV3\n"
  97. "#define HAVE_VFP\n")
  98. set(SGEMM_UNROLL_M 4)
  99. set(SGEMM_UNROLL_N 4)
  100. set(DGEMM_UNROLL_M 4)
  101. set(DGEMM_UNROLL_N 4)
  102. elseif ("${TCORE}" STREQUAL "ARMV8")
  103. file(APPEND ${TARGET_CONF_TEMP}
  104. "#define L1_DATA_SIZE\t32768\n"
  105. "#define L1_DATA_LINESIZE\t64\n"
  106. "#define L2_SIZE\t262144\n"
  107. "#define L2_LINESIZE\t64\n"
  108. "#define DTB_DEFAULT_ENTRIES\t64\n"
  109. "#define DTB_SIZE\t4096\n"
  110. "#define L2_ASSOCIATIVE\t32\n"
  111. "#define ARMV8\n")
  112. set(SGEMM_UNROLL_M 16)
  113. set(SGEMM_UNROLL_N 4)
  114. set(DGEMM_UNROLL_M 8)
  115. set(DGEMM_UNROLL_N 4)
  116. set(CGEMM_UNROLL_M 8)
  117. set(CGEMM_UNROLL_N 4)
  118. set(ZGEMM_UNROLL_M 4)
  119. set(ZGEMM_UNROLL_N 4)
  120. set(SYMV_P 16)
  121. elseif ("${TCORE}" STREQUAL "CORTEXA57" OR "${TCORE}" STREQUAL "CORTEXA53")
  122. file(APPEND ${TARGET_CONF_TEMP}
  123. "#define L1_CODE_SIZE\t32768\n"
  124. "#define L1_CODE_LINESIZE\t64\n"
  125. "#define L1_CODE_ASSOCIATIVE\t3\n"
  126. "#define L1_DATA_SIZE\t32768\n"
  127. "#define L1_DATA_LINESIZE\t64\n"
  128. "#define L1_DATA_ASSOCIATIVE\t2\n"
  129. "#define L2_SIZE\t262144\n"
  130. "#define L2_LINESIZE\t64\n"
  131. "#define L2_ASSOCIATIVE\t16\n"
  132. "#define DTB_DEFAULT_ENTRIES\t64\n"
  133. "#define DTB_SIZE\t4096\n"
  134. "#define HAVE_VFPV4\n"
  135. "#define HAVE_VFPV3\n"
  136. "#define HAVE_VFP\n"
  137. "#define HAVE_NEON\n"
  138. "#define ARMV8\n")
  139. set(SGEMM_UNROLL_M 16)
  140. set(SGEMM_UNROLL_N 4)
  141. set(DGEMM_UNROLL_M 8)
  142. set(DGEMM_UNROLL_N 4)
  143. set(CGEMM_UNROLL_M 8)
  144. set(CGEMM_UNROLL_N 4)
  145. set(ZGEMM_UNROLL_M 4)
  146. set(ZGEMM_UNROLL_N 4)
  147. set(SYMV_P 16)
  148. elseif ("${TCORE}" STREQUAL "CORTEXA72" OR "${TCORE}" STREQUAL "CORTEXA73")
  149. file(APPEND ${TARGET_CONF_TEMP}
  150. "#define L1_CODE_SIZE\t49152\n"
  151. "#define L1_CODE_LINESIZE\t64\n"
  152. "#define L1_CODE_ASSOCIATIVE\t3\n"
  153. "#define L1_DATA_SIZE\t32768\n"
  154. "#define L1_DATA_LINESIZE\t64\n"
  155. "#define L1_DATA_ASSOCIATIVE\t2\n"
  156. "#define L2_SIZE\t524288\n"
  157. "#define L2_LINESIZE\t64\n"
  158. "#define L2_ASSOCIATIVE\t16\n"
  159. "#define DTB_DEFAULT_ENTRIES\t64\n"
  160. "#define DTB_SIZE\t4096\n"
  161. "#define HAVE_VFPV4\n"
  162. "#define HAVE_VFPV3\n"
  163. "#define HAVE_VFP\n"
  164. "#define HAVE_NEON\n"
  165. "#define ARMV8\n")
  166. set(SGEMM_UNROLL_M 16)
  167. set(SGEMM_UNROLL_N 4)
  168. set(DGEMM_UNROLL_M 8)
  169. set(DGEMM_UNROLL_N 4)
  170. set(CGEMM_UNROLL_M 8)
  171. set(CGEMM_UNROLL_N 4)
  172. set(ZGEMM_UNROLL_M 4)
  173. set(ZGEMM_UNROLL_N 4)
  174. set(SYMV_P 16)
  175. elseif ("${TCORE}" STREQUAL "FALKOR")
  176. file(APPEND ${TARGET_CONF_TEMP}
  177. "#define L1_CODE_SIZE\t65536\n"
  178. "#define L1_CODE_LINESIZE\t64\n"
  179. "#define L1_CODE_ASSOCIATIVE\t3\n"
  180. "#define L1_DATA_SIZE\t32768\n"
  181. "#define L1_DATA_LINESIZE\t128\n"
  182. "#define L1_DATA_ASSOCIATIVE\t2\n"
  183. "#define L2_SIZE\t524288\n"
  184. "#define L2_LINESIZE\t64\n"
  185. "#define L2_ASSOCIATIVE\t16\n"
  186. "#define DTB_DEFAULT_ENTRIES\t64\n"
  187. "#define DTB_SIZE\t4096\n"
  188. "#define HAVE_VFPV4\n"
  189. "#define HAVE_VFPV3\n"
  190. "#define HAVE_VFP\n"
  191. "#define HAVE_NEON\n"
  192. "#define ARMV8\n")
  193. set(SGEMM_UNROLL_M 16)
  194. set(SGEMM_UNROLL_N 4)
  195. set(DGEMM_UNROLL_M 8)
  196. set(DGEMM_UNROLL_N 4)
  197. set(CGEMM_UNROLL_M 8)
  198. set(CGEMM_UNROLL_N 4)
  199. set(ZGEMM_UNROLL_M 4)
  200. set(ZGEMM_UNROLL_N 4)
  201. set(SYMV_P 16)
  202. elseif ("${TCORE}" STREQUAL "THUNDERX")
  203. file(APPEND ${TARGET_CONF_TEMP}
  204. "#define L1_CODE_SIZE\t32768\n"
  205. "#define L1_CODE_LINESIZE\t64\n"
  206. "#define L1_CODE_ASSOCIATIVE\t3\n"
  207. "#define L1_DATA_SIZE\t32768\n"
  208. "#define L1_DATA_LINESIZE\t128\n"
  209. "#define L1_DATA_ASSOCIATIVE\t2\n"
  210. "#define L2_SIZE\t167772164\n"
  211. "#define L2_LINESIZE\t128\n"
  212. "#define L2_ASSOCIATIVE\t16\n"
  213. "#define DTB_DEFAULT_ENTRIES\t64\n"
  214. "#define DTB_SIZE\t4096\n"
  215. "#define HAVE_VFPV4\n"
  216. "#define HAVE_VFPV3\n"
  217. "#define HAVE_VFP\n"
  218. "#define HAVE_NEON\n"
  219. "#define ARMV8\n")
  220. set(SGEMM_UNROLL_M 4)
  221. set(SGEMM_UNROLL_N 4)
  222. set(DGEMM_UNROLL_M 2)
  223. set(DGEMM_UNROLL_N 2)
  224. set(CGEMM_UNROLL_M 2)
  225. set(CGEMM_UNROLL_N 2)
  226. set(ZGEMM_UNROLL_M 2)
  227. set(ZGEMM_UNROLL_N 2)
  228. set(SYMV_P 16)
  229. elseif ("${TCORE}" STREQUAL "THUNDERX2T99")
  230. file(APPEND ${TARGET_CONF_TEMP}
  231. "#define L1_CODE_SIZE\t32768\n"
  232. "#define L1_CODE_LINESIZE\t64\n"
  233. "#define L1_CODE_ASSOCIATIVE\t8\n"
  234. "#define L1_DATA_SIZE\t32768\n"
  235. "#define L1_DATA_LINESIZE\t64\n"
  236. "#define L1_DATA_ASSOCIATIVE\t8\n"
  237. "#define L2_SIZE\t262144\n"
  238. "#define L2_LINESIZE\t64\n"
  239. "#define L2_ASSOCIATIVE\t8\n"
  240. "#define L3_SIZE\t33554432\n"
  241. "#define L3_LINESIZE\t64\n"
  242. "#define L3_ASSOCIATIVE\t32\n"
  243. "#define DTB_DEFAULT_ENTRIES\t64\n"
  244. "#define DTB_SIZE\t4096\n"
  245. "#define ARMV8\n")
  246. set(SGEMM_UNROLL_M 16)
  247. set(SGEMM_UNROLL_N 4)
  248. set(DGEMM_UNROLL_M 8)
  249. set(DGEMM_UNROLL_N 4)
  250. set(CGEMM_UNROLL_M 8)
  251. set(CGEMM_UNROLL_N 4)
  252. set(ZGEMM_UNROLL_M 4)
  253. set(ZGEMM_UNROLL_N 4)
  254. set(SYMV_P 16)
  255. endif()
  256. # Or should this actually be NUM_CORES?
  257. if (${NUM_THREADS} GREATER 0)
  258. file(APPEND ${TARGET_CONF_TEMP} "#define NUM_CORES\t${NUM_THREADS}\n")
  259. endif()
  260. # GetArch_2nd
  261. foreach(float_char S;D;Q;C;Z;X)
  262. if (NOT DEFINED ${float_char}GEMM_UNROLL_M)
  263. set(${float_char}GEMM_UNROLL_M 2)
  264. endif()
  265. if (NOT DEFINED ${float_char}GEMM_UNROLL_N)
  266. set(${float_char}GEMM_UNROLL_N 2)
  267. endif()
  268. endforeach()
  269. file(APPEND ${TARGET_CONF_TEMP}
  270. "#define GEMM_MULTITHREAD_THRESHOLD\t${GEMM_MULTITHREAD_THRESHOLD}\n")
  271. # Move to where gen_config_h would place it
  272. file(MAKE_DIRECTORY ${TARGET_CONF_DIR})
  273. file(RENAME ${TARGET_CONF_TEMP} "${TARGET_CONF_DIR}/${TARGET_CONF}")
  274. else(NOT CMAKE_CROSSCOMPILING)
  275. # compile getarch
  276. set(GETARCH_SRC
  277. ${PROJECT_SOURCE_DIR}/getarch.c
  278. ${CPUIDEMU}
  279. )
  280. if ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
  281. #Use generic for MSVC now
  282. message("MSVC")
  283. set(GETARCH_FLAGS ${GETARCH_FLAGS} -DFORCE_GENERIC)
  284. else()
  285. list(APPEND GETARCH_SRC ${PROJECT_SOURCE_DIR}/cpuid.S)
  286. endif ()
  287. if ("${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  288. # disable WindowsStore strict CRT checks
  289. set(GETARCH_FLAGS ${GETARCH_FLAGS} -D_CRT_SECURE_NO_WARNINGS)
  290. endif ()
  291. set(GETARCH_DIR "${PROJECT_BINARY_DIR}/getarch_build")
  292. set(GETARCH_BIN "getarch${CMAKE_EXECUTABLE_SUFFIX}")
  293. file(MAKE_DIRECTORY ${GETARCH_DIR})
  294. configure_file(${TARGET_CONF_TEMP} ${GETARCH_DIR}/${TARGET_CONF} COPYONLY)
  295. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  296. try_compile(GETARCH_RESULT ${GETARCH_DIR}
  297. SOURCES ${GETARCH_SRC}
  298. COMPILE_DEFINITIONS ${EXFLAGS} ${GETARCH_FLAGS} -I${GETARCH_DIR} -I"${PROJECT_SOURCE_DIR}" -I"${PROJECT_BINARY_DIR}"
  299. OUTPUT_VARIABLE GETARCH_LOG
  300. COPY_FILE ${PROJECT_BINARY_DIR}/${GETARCH_BIN}
  301. )
  302. if (NOT ${GETARCH_RESULT})
  303. MESSAGE(FATAL_ERROR "Compiling getarch failed ${GETARCH_LOG}")
  304. endif ()
  305. endif ()
  306. message(STATUS "Running getarch")
  307. # use the cmake binary w/ the -E param to run a shell command in a cross-platform way
  308. execute_process(COMMAND "${PROJECT_BINARY_DIR}/${GETARCH_BIN}" 0 OUTPUT_VARIABLE GETARCH_MAKE_OUT)
  309. execute_process(COMMAND "${PROJECT_BINARY_DIR}/${GETARCH_BIN}" 1 OUTPUT_VARIABLE GETARCH_CONF_OUT)
  310. message(STATUS "GETARCH results:\n${GETARCH_MAKE_OUT}")
  311. # append config data from getarch to the TARGET file and read in CMake vars
  312. file(APPEND ${TARGET_CONF_TEMP} ${GETARCH_CONF_OUT})
  313. ParseGetArchVars(${GETARCH_MAKE_OUT})
  314. set(GETARCH2_DIR "${PROJECT_BINARY_DIR}/getarch2_build")
  315. set(GETARCH2_BIN "getarch_2nd${CMAKE_EXECUTABLE_SUFFIX}")
  316. file(MAKE_DIRECTORY ${GETARCH2_DIR})
  317. configure_file(${TARGET_CONF_TEMP} ${GETARCH2_DIR}/${TARGET_CONF} COPYONLY)
  318. if (NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "WindowsStore")
  319. try_compile(GETARCH2_RESULT ${GETARCH2_DIR}
  320. SOURCES ${PROJECT_SOURCE_DIR}/getarch_2nd.c
  321. COMPILE_DEFINITIONS ${EXFLAGS} ${GETARCH_FLAGS} ${GETARCH2_FLAGS} -I${GETARCH2_DIR} -I"${PROJECT_SOURCE_DIR}" -I"${PROJECT_BINARY_DIR}"
  322. OUTPUT_VARIABLE GETARCH2_LOG
  323. COPY_FILE ${PROJECT_BINARY_DIR}/${GETARCH2_BIN}
  324. )
  325. if (NOT ${GETARCH2_RESULT})
  326. MESSAGE(FATAL_ERROR "Compiling getarch_2nd failed ${GETARCH2_LOG}")
  327. endif ()
  328. endif ()
  329. # use the cmake binary w/ the -E param to run a shell command in a cross-platform way
  330. execute_process(COMMAND "${PROJECT_BINARY_DIR}/${GETARCH2_BIN}" 0 OUTPUT_VARIABLE GETARCH2_MAKE_OUT)
  331. execute_process(COMMAND "${PROJECT_BINARY_DIR}/${GETARCH2_BIN}" 1 OUTPUT_VARIABLE GETARCH2_CONF_OUT)
  332. # append config data from getarch_2nd to the TARGET file and read in CMake vars
  333. file(APPEND ${TARGET_CONF_TEMP} ${GETARCH2_CONF_OUT})
  334. configure_file(${TARGET_CONF_TEMP} ${TARGET_CONF_DIR}/${TARGET_CONF} COPYONLY)
  335. ParseGetArchVars(${GETARCH2_MAKE_OUT})
  336. endif()