Advertisement
Guest User

Cmakelists.txt

a guest
Nov 28th, 2022
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. # CMakeLists.txt for compiling MYSTRAN with gfortran in a GNU environment
  2. # based on an older CMakeLists.txt by ceanwang@gmail.com
  3. # made to work again by Bruno Borges Paschoalinoto (2020)
  4.  
  5. # set up basic project info
  6. cmake_minimum_required(VERSION 3.18)
  7. enable_language(Fortran C)
  8. project(Mystran)
  9.  
  10. #Macros ################################# for debug
  11. macro(print_all_variables)
  12. message(STATUS "print_all_variables---------------------------------------")
  13. get_cmake_property(_variableNames VARIABLES)
  14. foreach (_variableName ${_variableNames})
  15. message(STATUS "${_variableName}=${${_variableName}}")
  16. endforeach()
  17. message(STATUS "print_all_variables---------------------------------------")
  18. endmacro()
  19. #Macros #################################
  20.  
  21.  
  22. # basic compiler and output options
  23. set(CMAKE_SOURCE_DIR "Source")
  24. set(PROJECT_BINARY_DIR "Binaries")
  25. set(CMAKE_FLAGS "-c -fbacktrace")
  26. # set(CMAKE_BUILD_TYPE Debug)
  27. if (${__WINDOWS_INTEL} MATCHES 1)
  28. set(CMAKE_Fortran_FLAGS_MINSIZEREL "/O1")
  29. set(CMAKE_Fortran_FLAGS_MINSIZEREL_INIT "/O1")
  30. set(CMAKE_Fortran_FLAGS_RELEASE "/O2 /QaxCORE-AVX2")
  31. set(CMAKE_Fortran_FLAGS_RELEASE_INIT "/O2 /QaxCORE-AVX2")
  32. set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "/O2 /debug:full")
  33. set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO_INIT "/O2 /debug:full")
  34. set(CMAKE_CXX_FLAGS_RELEASE= /O3 /Ob2 /QaxCORE-AVX2)
  35. set(CMAKE_CXX_FLAGS_RELEASE_INIT= /O3 /Ob2 /QaxCORE-AVX2)
  36. endif()
  37.  
  38.  
  39. if (Win32)
  40. if ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Intel")
  41. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /QaxCORE-AVX2")
  42. set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
  43. message("Intel Compiler used")
  44. elseif ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "IntelLLVM")
  45. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /QaxCORE-AVX2")
  46. set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
  47. message("Intel LLVM Compiler used")
  48. elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
  49. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2 -ffast-math -march=znver3")
  50. message("GNU Compiler used")
  51. elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "MSVC")
  52. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /arch:AVX2")
  53. set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
  54. elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Clang")
  55. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2 -march=znver3")
  56. else()
  57. set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2")
  58. set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
  59. endif()
  60. endif(Win32)
  61.  
  62. # set some dirs
  63. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
  64. set(CMAKE_Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/mod")
  65. include_directories("${CMAKE_SOURCE_DIR}/INCLUDE")
  66.  
  67. # uncomment this to debug
  68. # set(CMAKE_VERBOSE_MAKEFILE true)
  69.  
  70. # suppress cmake warnings for superlu
  71. if(NOT DEFINED CMAKE_SUPPRESS_DEVELOPER_WARNINGS)
  72. set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings")
  73. endif()
  74.  
  75. #Check BLAS
  76. #default internal lib? refer to superlu
  77. message ("Check Blas")
  78. # The XSDK standard does not allow using internally built BLAS
  79. if (USE_XSDK_DEFAULTS)
  80. message ("USE_XSDK_DEFAULTS TRUE")
  81. set(enable_blaslib_DEFAULT OFF)
  82. set(enable_internal_blaslib2 FALSE)
  83. else()
  84. set(enable_blaslib_DEFAULT ON)
  85. message ("USE_XSDK_DEFAULTS FALSE")
  86. set(enable_internal_blaslib2 ON)
  87. endif()
  88.  
  89. if(NOT enable_internal_blaslib2)
  90. message ("Finding blas...")
  91. find_package(BLAS REQUIRED)
  92. if(${BLA_VENDOR} MATCHES "OpenBLAS")
  93. find_package(OpenBLAS REQUIRED)
  94. elseif(${BLA_VENDOR} MATCHES "Intel10_64lp")
  95. find_package(LAPACK REQUIRED)
  96. elseif(${BLA_VENDOR} MATCHES "Intel")
  97. find_package(LAPACK REQUIRED)
  98. endif()
  99. else()
  100. message ("Building blas...")
  101. endif()
  102.  
  103. #print_all_variables()
  104.  
  105. if(NOT enable_internal_blaslib2)
  106. find_package(BLAS)
  107. message("Check the BLAS first")
  108. if (BLAS_FOUND)
  109. message ("We found the BLAS")
  110. else(BLAS_FOUND)
  111. message ("BLAS NOT FOUND, USE the integrated Superlu CBLAS and mystran BLAS")
  112. set (enable_internal_blaslib2 TRUE)
  113. # for superlu compile cblas
  114. endif(BLAS_FOUND)
  115. endif()
  116.  
  117. # submodules (i.e. SuperLU) ! passed by nmake / ninja
  118. find_package(Git QUIET)
  119. if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
  120. option(GIT_SUBMODULE "Check submodules during build" ON)
  121. if(GIT_SUBMODULE)
  122. message(STATUS "Updating submodules")
  123. execute_process(
  124. COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
  125. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  126. RESULT_VARIABLE GIT_SUBMOD_RESULT
  127. )
  128. if(NOT GIT_SUBMOD_RESULT EQUAL "0")
  129. message(
  130. FATAL_ERROR
  131. "git submodule update --init --recursive failed"
  132. )
  133. endif()
  134. endif()
  135. endif()
  136.  
  137.  
  138. # check for SuperLU
  139. set(SUPERLU_DIR "superlu")
  140. include_directories("${SUPERLU_DIR}/SRC")
  141. if(NOT EXISTS "${SUPERLU_DIR}/CMakeLists.txt")
  142. message(
  143. FATAL_ERROR
  144. "The submodules were not downloaded. You have to do it manually!"
  145. )
  146. endif()
  147.  
  148. # build the SuperLU and BLAS static libs!
  149. set (enable_double YES)
  150. set (enable_single NO)
  151. set (enable_complex NO)
  152. set (enable_complex16 NO)
  153. set (enable_testing FALSE)
  154. set (enable_matmex NO)
  155.  
  156. add_subdirectory(${SUPERLU_DIR})
  157.  
  158.  
  159. if (${__WINDOWS_INTEL} MATCHES 1)
  160. set (f2c_Compile FALSE)
  161. endif()
  162.  
  163. if (WIN32)
  164. if (${__WINDOWS_INTEL} MATCHES 1)
  165. set (f2c_Compile FALSE)
  166. endif()
  167. else(WIN32)
  168. set (f2c_Compile TRUE)
  169. endif(WIN32)
  170.  
  171.  
  172. if (f2c_Compile)
  173. # f2c stuff
  174. set(F2C_DIR "f2c")
  175. set(F2C_INCLUDE_DIR "${F2C_DIR}/include")
  176. set(F2C_FN "${F2C_DIR}/libf2c.zip")
  177. set(F2C_URL "https://www.netlib.org/${F2C_FN}")
  178.  
  179. # download f2c
  180. if(NOT EXISTS ${F2C_DIR})
  181. message(STATUS "Downloading libf2c source from ${F2C_URL}...")
  182. make_directory("${F2C_DIR}")
  183. file(DOWNLOAD ${F2C_URL} ${F2C_FN} TIMEOUT 60 STATUS DOWNLOAD_STATUS)
  184. # Check if download was successful.
  185. list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
  186. list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
  187. if(${STATUS_CODE} EQUAL 0)
  188. message(STATUS "Done downloading libf2c.")
  189. else()
  190. # Exit CMake if the download failed, printing the error message.
  191. file(REMOVE_RECURSE ${F2C_DIR})
  192. message(FATAL_ERROR "Error downloading libf2c: ${ERROR_MESSAGE}")
  193. #print_all_variables()
  194. endif()
  195. endif()
  196.  
  197. # extract libf2c source
  198. file(ARCHIVE_EXTRACT INPUT ${F2C_FN} DESTINATION ${F2C_DIR})
  199.  
  200. # prepare libf2c header files
  201. file(GLOB_RECURSE F2C_PREHEADERS "${F2C_DIR}/*.h0")
  202. foreach(H0 ${F2C_PREHEADERS})
  203. string(REGEX REPLACE "[.]h0$" ".h" H0_R ${H0})
  204. file(RENAME "${H0}" "${H0_R}")
  205. file(COPY "${H0_R}" DESTINATION "${F2C_INCLUDE_DIR}")
  206. endforeach()
  207.  
  208. # get a load of this: f2c generates its own "arith.h" on the fly
  209. # so we gotta compile arithchk and run it
  210. set(F2C_ARITHCHK_SRC "${F2C_DIR}/arithchk.c")
  211. set(F2C_ARITHCHK_BIN "${F2C_DIR}/arithchk")
  212.  
  213. if (WIN32)
  214. set(F2C_ARITHCHK_BIN "${F2C_ARITHCHK_BIN}.exe")
  215. endif()
  216.  
  217. set(F2C_ARITH_H "${F2C_INCLUDE_DIR}/arith.h")
  218. set_source_files_properties(
  219. ${F2C_ARITHCHK_SRC} PROPERTIES COMPILE_FLAGS "-DNO_LONG_LONG -DNO_FPINIT"
  220. )
  221. add_executable(arithchk ${F2C_ARITHCHK_SRC})
  222. target_link_libraries(arithchk m)
  223. set_target_properties(
  224. arithchk PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${F2C_DIR}
  225. )
  226. add_custom_command(
  227. OUTPUT ${F2C_ARITH_H}
  228. COMMAND ${F2C_ARITHCHK_BIN} > ${F2C_ARITH_H}
  229. DEPENDS ${F2C_ARITHCHK_BIN}
  230. )
  231.  
  232. # add libf2c to the compilation procedures
  233. include_directories(${F2C_INCLUDE_DIR})
  234. file(GLOB_RECURSE F2C_CFILES "${F2C_DIR}/*.c")
  235. add_definitions(-DINTEGER_STAR_8)
  236. add_library(f2c ${F2C_CFILES} ${F2C_ARITH_H})
  237.  
  238. if (WIN32)
  239. # add some extra win32 flags for libf2c
  240. add_definitions(-DUSE_CLOCK -DMSDOS)
  241. # set some extra vars for MSYS builds to make the binary portable
  242. set(CMAKE_EXE_LINKER_FLAGS "-static")
  243. endif(WIN32)
  244.  
  245. else(f2c_Compile)
  246. set(CMAKE_EXE_LINKER_FLAGS "-static")
  247. endif(f2c_Compile)
  248.  
  249. # collect modules and interfaces into a module called MODULES
  250. # the overuse of the word MODULE is brain-twisting, but bear with me
  251. set(Modules_DIR "${CMAKE_SOURCE_DIR}/Modules")
  252. file(GLOB_RECURSE Modules_FILES "${Modules_DIR}/*.f*")
  253. # FUTURE FOR BLAS
  254. if(NOT enable_internal_blaslib2)
  255. message ("LAPACK files released since using VENDOR BLAS")
  256. file(GLOB MAINModules_FILES "${Modules_DIR}/*.f*")
  257. file(GLOB ARPACKModules_FILES "${Modules_DIR}/ARPACK/*.f*")
  258. file(GLOB LAPACKModules_FILES "${Modules_DIR}/LAPACK/*.f*")
  259. file(GLOB BANDITModules_FILES "${Modules_DIR}/BANDIT/*.f*")
  260. else ()
  261. file(GLOB_RECURSE Modules_FILES "${Modules_DIR}/*.f*")
  262. endif()
  263. file(GLOB_RECURSE Interfaces_FILES "${CMAKE_SOURCE_DIR}/Interfaces/*.f*")
  264. file(GLOB_RECURSE USE_IFs_FILES "${CMAKE_SOURCE_DIR}/USE_IFs/*.f*")
  265.  
  266. # MODULES_ALL_FILES shall contain all the sources for the base modules
  267. if(NOT enable_internal_blaslib2)
  268. set(
  269. MODULES_ALL_FILES ${USE_IFs_FILES} ${Interfaces_FILES} ${MAINModules_FILES} ${ARPACKModules_FILES} ${LAPACKModules_FILES} ${BANDITModules_FILES}
  270. )
  271. else()
  272. set(
  273. MODULES_ALL_FILES ${USE_IFs_FILES} ${Interfaces_FILES} ${Modules_FILES}
  274. )
  275. endif()
  276. add_library(MODULES ${MODULES_ALL_FILES})
  277.  
  278. # print_all_variables()
  279. # collect modules
  280. list(APPEND modules_names ARPACK BANDIT LK1 LK2 LK3 LK4 LK5 LK6 LK9 EMG)
  281. foreach (modname IN LISTS modules_names)
  282. file(GLOB_RECURSE TMP_MOD_FILES "${CMAKE_SOURCE_DIR}/${modname}/*.f*")
  283. add_library(${modname} ${TMP_MOD_FILES} ${TMP_MOD_FILES_PP})
  284. target_link_libraries(${modname} MODULES)
  285. endforeach()
  286.  
  287. # add the DTRSV and DGSSV C modules.
  288. add_library(dgssv "${SUPERLU_DIR}/FORTRAN/c_fortran_dgssv.c")
  289. if (NOT enable_internal_blaslib2)
  290. target_link_libraries(dgssv)
  291. else ()
  292. target_link_libraries(dgssv blas)
  293. endif()
  294.  
  295. # prepare the main executable, linked against the specifics and the m
  296. # it appears utils used to be a module, but that is no longer the case?
  297. file(GLOB_RECURSE UTIL_FILES "${CMAKE_SOURCE_DIR}/UTIL/*.f*")
  298. file(GLOB_RECURSE MAIN_FILES "${CMAKE_SOURCE_DIR}/MAIN/*.[fF]*")
  299. add_executable(mystran ${MAIN_FILES} ${MODULES_ALL_FILES} ${UTIL_FILES})
  300. #target_link_libraries(mystran ${modules_names})
  301. #target_link_libraries(mystran dgssv superlu)
  302. if(NOT enable_internal_blaslib2)
  303. target_link_libraries(mystran ${modules_names} dgssv superlu ${BLAS_LIBRARIES})
  304. else ()
  305. target_link_libraries(mystran ${modules_names} dgssv superlu)
  306. endif()
  307.  
  308. set_target_properties(
  309. mystran PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
  310. )
  311.  
  312. # issue a couple messages about compilation
  313.  
  314. If (${CMAKE_BUILD_TOOL} MATCHES "nmake")
  315. message(STATUS "You can now compile MYSTRAN with nmake: " )
  316. elseIf (${CMAKE_BUILD_TOOL} MATCHES "ninja")
  317. message(STATUS "You can now compile MYSTRAN with ninja, download first to this folder")
  318. elseIf (${CMAKE_GENERATOR} MATCHES "Visual Studio 17 2022")
  319. message(STATUS "You can now compile MYSTRAN with clicking Mystran.SLN file")
  320. elseIf (${CMAKE_GENERATOR} MATCHES "Visual Studio 16 2019")
  321. message(STATUS "You can now compile MYSTRAN with clicking Mystran.SLN file")
  322. elseIf (${CMAKE_BUILD_TOOL} MATCHES "jom")
  323. include(ProcessorCount)
  324. ProcessorCount(NCPU)
  325. message(STATUS "You can now compile MYSTRAN with jom.")
  326. if (NOT NCPU EQUAL 0)
  327. message(STATUS "Compile faster by passing /j${NCPU} to make.")
  328. endif()
  329. else()
  330. include(ProcessorCount)
  331. ProcessorCount(NCPU)
  332. message(STATUS "You can now compile MYSTRAN with make.")
  333. if (NOT NCPU EQUAL 0)
  334. message(STATUS "Compile faster by passing -j${NCPU} to make.")
  335. endif()
  336. endif()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement