compiler-specific.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. # This file is included from the def.cmake CMakeLists.txt file.
  2. # It sets up the compiler specific flags.
  3. # Define the common flags and options for GCC
  4. option(PROFILE "Enable profiling" OFF)
  5. add_library(common_compiler_flags INTERFACE)
  6. function(set_if_empty var value)
  7. if(DEFINED ENV{${var}} AND NOT "$ENV{${var}}" STREQUAL "")
  8. set(${var}
  9. ${value}
  10. PARENT_SCOPE
  11. )
  12. set(${var} "$ENV{${var}}")
  13. else()
  14. set(${var}
  15. ${value}
  16. PARENT_SCOPE
  17. )
  18. endif()
  19. endfunction()
  20. # Define the flags for the C compiler
  21. if(TARGET_ARCH MATCHES "x86_64")
  22. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  23. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  24. target_compile_options(common_compiler_flags INTERFACE -funroll-loops -Wcast-align)
  25. # If GCC version is greater than 4.2, enable the following flags
  26. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  27. set_if_empty(CPUTYPE "generic")
  28. target_compile_options(
  29. common_compiler_flags INTERFACE -m64 -minline-all-stringops -falign-loops -ftree-vectorize
  30. -fno-strict-overflow -mtune=${CPUTYPE}
  31. )
  32. target_link_options(common_compiler_flags INTERFACE -m64)
  33. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  34. set_if_empty(CPUTYPE "athlon64")
  35. target_compile_options(
  36. common_compiler_flags INTERFACE -m64 -minline-all-stringops -falign-loops -ftree-vectorize
  37. -mtune=${CPUTYPE}
  38. )
  39. target_link_options(common_compiler_flags INTERFACE -m64)
  40. else()
  41. message(
  42. WARNING
  43. "You are using an old and unsupported gcc version ${CMAKE_C_COMPILER_VERSION}, compile at your own risk!"
  44. )
  45. endif()
  46. elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
  47. set_if_empty(CPUTYPE "opteron")
  48. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  49. target_compile_options(common_compiler_flags INTERFACE -m64)
  50. target_link_options(common_compiler_flags INTERFACE -m64)
  51. elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel")
  52. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  53. target_compile_options(common_compiler_flags INTERFACE -O3 -ipo -ipo_obj -unroll -tpp6 -xK)
  54. else()
  55. message(
  56. WARNING
  57. "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for x86_64. Try GCC, Clang, or ICC. Compile at your own risk!"
  58. )
  59. endif()
  60. elseif(TARGET_ARCH MATCHES "i386")
  61. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  62. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  63. target_compile_options(common_compiler_flags INTERFACE -Wall -funroll-loops -Wcast-align)
  64. # If GCC version is greater than 4.2.0, enable the following flags
  65. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  66. set_if_empty(CPUTYPE "generic")
  67. target_compile_options(
  68. common_compiler_flags INTERFACE -m32 -minline-all-stringops -falign-loops -ftree-vectorize
  69. -fno-strict-overflow -mtune=${CPUTYPE}
  70. )
  71. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  72. set_if_empty(CPUTYPE "athlon64")
  73. target_compile_options(
  74. common_compiler_flags INTERFACE -m32 -minline-all-stringops -falign-loops -ftree-vectorize
  75. -mtune=${CPUTYPE}
  76. )
  77. else()
  78. message(
  79. WARNING
  80. "You are using an old and unsupported gcc version ${CMAKE_C_COMPILER_VERSION}, compile at your own risk!"
  81. )
  82. endif()
  83. elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
  84. set_if_empty(CPUTYPE "athlon64")
  85. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  86. target_compile_options(common_compiler_flags INTERFACE -m32 -mtune=${CPUTYPE})
  87. target_link_options(common_compiler_flags INTERFACE -m32)
  88. elseif(CMAKE_C_COMPILER_ID STREQUAL "Intel")
  89. # ICC- Intel classic specific flags
  90. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  91. target_compile_options(common_compiler_flags INTERFACE -O3 -ipo -ipo_obj -unroll -tpp6 -xK)
  92. else()
  93. message(
  94. WARNING "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for i386. Try GCC, Clang, or ICC."
  95. )
  96. endif()
  97. elseif(TARGET_ARCH MATCHES "aarch64")
  98. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  99. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  100. # target_compile_options(common INTERFACE -O0 # <$<$<BOOL:${PROFILE}>:-pg> )
  101. # target_compile_options( common INTERFACE -marm -march=armv5t
  102. # -funroll-loops -fsigned-char )
  103. # # If GCC version is greater than 4.2.0, enable the following flags
  104. # if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0) target_compile_options(
  105. # common INTERFACE -ftree-vectorize -fno-strict-overflow ) endif()
  106. endif()
  107. elseif(TARGET_ARCH MATCHES "ppc64$")
  108. # PowerPC 64-bit specific flags
  109. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  110. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  111. target_compile_options(common_compiler_flags INTERFACE -funroll-loops -fsigned-char)
  112. # Try to get CPUTYPE from the environment, fallback to "powerpc64" if not set
  113. if(DEFINED ENV{CPUTYPE} AND NOT "$ENV{CPUTYPE}" STREQUAL "")
  114. set(CPUTYPE "$ENV{CPUTYPE}")
  115. else()
  116. set(CPUTYPE "powerpc64")
  117. endif()
  118. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 4.2)
  119. target_compile_options(
  120. common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow -mtune=${CPUTYPE}
  121. -maltivec
  122. )
  123. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 4.0)
  124. target_compile_options(
  125. common_compiler_flags INTERFACE -ftree-vectorize -mtune=${CPUTYPE} -maltivec
  126. )
  127. elseif(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
  128. message(
  129. WARNING
  130. "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for ppc64. Try GCC 3.0 or newer."
  131. )
  132. endif()
  133. # else()
  134. # message(FATAL_ERROR "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for ppc64. Try GCC.")
  135. endif()
  136. elseif(TARGET_ARCH STREQUAL "ppc")
  137. # PowerPC 32-bit specific flags
  138. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  139. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  140. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 4.2)
  141. set_if_empty(CPUTYPE "powerpc")
  142. target_compile_options(
  143. common_compiler_flags INTERFACE -funroll-loops -fsigned-char -ftree-vectorize -maltivec
  144. -fno-strict-overflow -mtune=${CPUTYPE}
  145. )
  146. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 4.0)
  147. set_if_empty(CPUTYPE "powerpc")
  148. target_compile_options(
  149. common_compiler_flags INTERFACE -funroll-loops -fsigned-char -ftree-vectorize -maltivec
  150. -mtune=${CPUTYPE}
  151. )
  152. else()
  153. message(
  154. WARNING "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for ppc. Try GCC 4.0 or newer."
  155. )
  156. endif()
  157. else()
  158. message(
  159. WARNING
  160. "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for ${TARGET_ARCH}. Try GCC. Compile at your own risk!"
  161. )
  162. endif()
  163. elseif(TARGET_ARCH STREQUAL "arm7")
  164. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  165. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  166. # ARM specific flags
  167. target_compile_options(
  168. common_compiler_flags INTERFACE -march=armv7-a -funroll-loops -fsigned-char
  169. )
  170. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
  171. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow)
  172. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  173. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize)
  174. else()
  175. message(
  176. WARNING "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for arm7. Try GCC 4.0 or newer."
  177. )
  178. endif()
  179. else()
  180. message(FATAL_ERROR "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for arm7. Try GCC.")
  181. endif()
  182. elseif(TARGET_ARCH STREQUAL "arm6")
  183. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  184. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  185. target_compile_options(
  186. common_compiler_flags INTERFACE -march=armv6 -funroll-loops -fsigned-char
  187. )
  188. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
  189. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow)
  190. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  191. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize)
  192. else()
  193. message(
  194. WARNING "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for arm6. Try GCC 4.0 or newer."
  195. )
  196. endif()
  197. else()
  198. message(WARNING "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for arm6. Try GCC.")
  199. endif()
  200. elseif(TARGET_ARCH STREQUAL "arm")
  201. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  202. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  203. # ARM specific flags
  204. target_compile_options(
  205. common_compiler_flags INTERFACE -marm -march=armv5t -funroll-loops -fsigned-char
  206. )
  207. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2.0)
  208. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow)
  209. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  210. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize)
  211. else()
  212. message(
  213. WARNING "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for arm. Try GCC 4.0 or newer."
  214. )
  215. endif()
  216. else()
  217. message(FATAL_ERROR "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for arm. Try GCC.")
  218. endif()
  219. elseif(TARGET_ARCH STREQUAL "sparc64")
  220. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  221. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM SPARC64_MODE)
  222. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  223. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  224. set_if_empty(CPUTYPE "ultrasparc")
  225. target_compile_options(
  226. common_compiler_flags INTERFACE -m64 -mcpu=ultrasparc -mtune=${CPUTYPE}
  227. -fno-strict-overflow -ftree-vectorize
  228. )
  229. target_link_options(common_compiler_flags INTERFACE -m64)
  230. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  231. set_if_empty(CPUTYPE "ultrasparc")
  232. target_compile_options(
  233. common_compiler_flags INTERFACE -m64 -mcpu=ultrasparc -mtune=${CPUTYPE} -ftree-vectorize
  234. )
  235. endif()
  236. # The following CMAKE_C_COMPILER_ID is not available per cmake docs
  237. # TODO: Use some other variable to check like CC
  238. elseif(CMAKE_C_COMPILER_ID STREQUAL "Sun")
  239. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM SPARC64_MODE)
  240. target_compile_options(
  241. common_compiler_flags
  242. INTERFACE -m64
  243. -xO3
  244. -xtarget=native
  245. -xmemalign=8i
  246. -fma=fused
  247. -fns=yes
  248. -xc99
  249. )
  250. else()
  251. message(
  252. FATAL_ERROR "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for sparc64. Try GCC or Sun."
  253. )
  254. endif()
  255. elseif(TARGET_ARCH STREQUAL "sparc")
  256. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  257. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  258. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  259. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  260. set_if_empty(CPUTYPE "v8")
  261. target_compile_options(
  262. common_compiler_flags INTERFACE -mtune=${CPUTYPE} -fno-strict-overflow -ftree-vectorize
  263. )
  264. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  265. set_if_empty(CPUTYPE "v8")
  266. target_compile_options(common_compiler_flags INTERFACE -mtune=${CPUTYPE} -ftree-vectorize)
  267. endif()
  268. # The following CMAKE_C_COMPILER_ID is not available per cmake docs
  269. # TODO: Use some other variable to check like CC
  270. elseif(CMAKE_C_COMPILER_ID STREQUAL "Sun")
  271. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  272. target_compile_options(
  273. common_compiler_flags INTERFACE -xO3 -xtarget=native -xmemalign=4i -fma=fused -fns=yes -xc99
  274. )
  275. else()
  276. message(FATAL_ERROR "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for sparc. Try GCC or Sun.")
  277. endif()
  278. elseif(TARGET_ARCH STREQUAL "mips")
  279. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  280. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  281. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  282. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  283. target_compile_options(
  284. common_compiler_flags INTERFACE -mfp32 -march=r3000 -ftree-vectorize -fno-strict-overflow
  285. )
  286. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  287. target_compile_options(common_compiler_flags INTERFACE -march=r3000 -ftree-vectorize)
  288. else()
  289. message(
  290. WARNING "GCC version ${CMAKE_C_COMPILER_VERSION} is too old for mips. Try GCC 4.0 or newer."
  291. )
  292. endif()
  293. else()
  294. message(WARNING "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for mips. Try GCC.")
  295. endif()
  296. elseif(TARGET_ARCH STREQUAL "mips2")
  297. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  298. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  299. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  300. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  301. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow)
  302. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  303. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize)
  304. elseif(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
  305. message(
  306. WARNING
  307. "You are using an old and unsupported gcc version ${CMAKE_C_COMPILER_VERSION}, compile at your own risk!"
  308. )
  309. endif()
  310. else()
  311. message(
  312. WARNING
  313. "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for ${TARGET_ARCH}. Try GCC. Compile at your own risk!"
  314. )
  315. endif()
  316. elseif(TARGET_ARCH STREQUAL "mips64")
  317. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  318. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  319. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  320. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  321. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize -fno-strict-overflow)
  322. elseif(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.0)
  323. target_compile_options(common_compiler_flags INTERFACE -ftree-vectorize)
  324. elseif(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
  325. message(
  326. WARNING
  327. "You are using an old and unsupported gcc version ${CMAKE_C_COMPILER_VERSION}, compile at your own risk!"
  328. )
  329. endif()
  330. else()
  331. message(
  332. WARNING
  333. "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for ${TARGET_ARCH}. Try GCC. Compile at your own risk!"
  334. )
  335. endif()
  336. elseif(TARGET_ARCH STREQUAL "alpha")
  337. if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
  338. target_compile_definitions(common_compiler_flags INTERFACE CC_GCC_LIKE_ASM)
  339. target_compile_options(common_compiler_flags INTERFACE -funroll-loops)
  340. if(CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.2)
  341. target_compile_options(common_compiler_flags INTERFACE -fno-strict-overflow)
  342. elseif(CMAKE_C_COMPILER_VERSION VERSION_LESS 3.0)
  343. message(
  344. WARNING
  345. "You are using an old and unsupported gcc version ${CMAKE_C_COMPILER_VERSION}, compile at your own risk!"
  346. )
  347. endif()
  348. else()
  349. message(
  350. WARNING
  351. "Unsupported compiler (${CMAKE_C_COMPILER_ID}) for ${TARGET_ARCH}. Try GCC. Compile at your own risk!"
  352. )
  353. endif()
  354. else()
  355. message(
  356. WARNING
  357. "Architecture ${TARGET_ARCH} not directly supported by project. Proceeding with generic flags. \
  358. Define any compile options you might need with env variables like CFLAGS."
  359. )
  360. endif()