Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CMakeLists.txt for compiling MYSTRAN with gfortran in a GNU environment
- # based on an older CMakeLists.txt by ceanwang@gmail.com
- # made to work again by Bruno Borges Paschoalinoto (2020)
- # set up basic project info
- cmake_minimum_required(VERSION 3.18)
- enable_language(Fortran C)
- project(Mystran)
- #Macros ################################# for debug
- macro(print_all_variables)
- message(STATUS "print_all_variables---------------------------------------")
- get_cmake_property(_variableNames VARIABLES)
- foreach (_variableName ${_variableNames})
- message(STATUS "${_variableName}=${${_variableName}}")
- endforeach()
- message(STATUS "print_all_variables---------------------------------------")
- endmacro()
- #Macros #################################
- # basic compiler and output options
- set(CMAKE_SOURCE_DIR "Source")
- set(PROJECT_BINARY_DIR "Binaries")
- set(CMAKE_FLAGS "-c -fbacktrace")
- # set(CMAKE_BUILD_TYPE Debug)
- if (${__WINDOWS_INTEL} MATCHES 1)
- set(CMAKE_Fortran_FLAGS_MINSIZEREL "/O1")
- set(CMAKE_Fortran_FLAGS_MINSIZEREL_INIT "/O1")
- set(CMAKE_Fortran_FLAGS_RELEASE "/O2 /QaxCORE-AVX2")
- set(CMAKE_Fortran_FLAGS_RELEASE_INIT "/O2 /QaxCORE-AVX2")
- set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "/O2 /debug:full")
- set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO_INIT "/O2 /debug:full")
- set(CMAKE_CXX_FLAGS_RELEASE= /O3 /Ob2 /QaxCORE-AVX2)
- set(CMAKE_CXX_FLAGS_RELEASE_INIT= /O3 /Ob2 /QaxCORE-AVX2)
- endif()
- if (Win32)
- if ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Intel")
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /QaxCORE-AVX2")
- set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
- message("Intel Compiler used")
- elseif ("${CMAKE_Fortran_COMPILER_ID}" MATCHES "IntelLLVM")
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /QaxCORE-AVX2")
- set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
- message("Intel LLVM Compiler used")
- elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2 -ffast-math -march=znver3")
- message("GNU Compiler used")
- elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "MSVC")
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /O2 /arch:AVX2")
- set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
- elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "Clang")
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2 -march=znver3")
- else()
- set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O2")
- set(CMAKE_FORTRAN_FLAGS_RELEASE_INIT /O2)
- endif()
- endif(Win32)
- # set some dirs
- set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
- set(CMAKE_Fortran_MODULE_DIRECTORY "${PROJECT_BINARY_DIR}/mod")
- include_directories("${CMAKE_SOURCE_DIR}/INCLUDE")
- # uncomment this to debug
- # set(CMAKE_VERBOSE_MAKEFILE true)
- # suppress cmake warnings for superlu
- if(NOT DEFINED CMAKE_SUPPRESS_DEVELOPER_WARNINGS)
- set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE INTERNAL "No dev warnings")
- endif()
- #Check BLAS
- #default internal lib? refer to superlu
- message ("Check Blas")
- # The XSDK standard does not allow using internally built BLAS
- if (USE_XSDK_DEFAULTS)
- message ("USE_XSDK_DEFAULTS TRUE")
- set(enable_blaslib_DEFAULT OFF)
- set(enable_internal_blaslib2 FALSE)
- else()
- set(enable_blaslib_DEFAULT ON)
- message ("USE_XSDK_DEFAULTS FALSE")
- set(enable_internal_blaslib2 ON)
- endif()
- if(NOT enable_internal_blaslib2)
- message ("Finding blas...")
- find_package(BLAS REQUIRED)
- if(${BLA_VENDOR} MATCHES "OpenBLAS")
- find_package(OpenBLAS REQUIRED)
- elseif(${BLA_VENDOR} MATCHES "Intel10_64lp")
- find_package(LAPACK REQUIRED)
- elseif(${BLA_VENDOR} MATCHES "Intel")
- find_package(LAPACK REQUIRED)
- endif()
- else()
- message ("Building blas...")
- endif()
- #print_all_variables()
- if(NOT enable_internal_blaslib2)
- find_package(BLAS)
- message("Check the BLAS first")
- if (BLAS_FOUND)
- message ("We found the BLAS")
- else(BLAS_FOUND)
- message ("BLAS NOT FOUND, USE the integrated Superlu CBLAS and mystran BLAS")
- set (enable_internal_blaslib2 TRUE)
- # for superlu compile cblas
- endif(BLAS_FOUND)
- endif()
- # submodules (i.e. SuperLU) ! passed by nmake / ninja
- find_package(Git QUIET)
- if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
- option(GIT_SUBMODULE "Check submodules during build" ON)
- if(GIT_SUBMODULE)
- message(STATUS "Updating submodules")
- execute_process(
- COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
- WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
- RESULT_VARIABLE GIT_SUBMOD_RESULT
- )
- if(NOT GIT_SUBMOD_RESULT EQUAL "0")
- message(
- FATAL_ERROR
- "git submodule update --init --recursive failed"
- )
- endif()
- endif()
- endif()
- # check for SuperLU
- set(SUPERLU_DIR "superlu")
- include_directories("${SUPERLU_DIR}/SRC")
- if(NOT EXISTS "${SUPERLU_DIR}/CMakeLists.txt")
- message(
- FATAL_ERROR
- "The submodules were not downloaded. You have to do it manually!"
- )
- endif()
- # build the SuperLU and BLAS static libs!
- set (enable_double YES)
- set (enable_single NO)
- set (enable_complex NO)
- set (enable_complex16 NO)
- set (enable_testing FALSE)
- set (enable_matmex NO)
- add_subdirectory(${SUPERLU_DIR})
- if (${__WINDOWS_INTEL} MATCHES 1)
- set (f2c_Compile FALSE)
- endif()
- if (WIN32)
- if (${__WINDOWS_INTEL} MATCHES 1)
- set (f2c_Compile FALSE)
- endif()
- else(WIN32)
- set (f2c_Compile TRUE)
- endif(WIN32)
- if (f2c_Compile)
- # f2c stuff
- set(F2C_DIR "f2c")
- set(F2C_INCLUDE_DIR "${F2C_DIR}/include")
- set(F2C_FN "${F2C_DIR}/libf2c.zip")
- set(F2C_URL "https://www.netlib.org/${F2C_FN}")
- # download f2c
- if(NOT EXISTS ${F2C_DIR})
- message(STATUS "Downloading libf2c source from ${F2C_URL}...")
- make_directory("${F2C_DIR}")
- file(DOWNLOAD ${F2C_URL} ${F2C_FN} TIMEOUT 60 STATUS DOWNLOAD_STATUS)
- # Check if download was successful.
- list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
- list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE)
- if(${STATUS_CODE} EQUAL 0)
- message(STATUS "Done downloading libf2c.")
- else()
- # Exit CMake if the download failed, printing the error message.
- file(REMOVE_RECURSE ${F2C_DIR})
- message(FATAL_ERROR "Error downloading libf2c: ${ERROR_MESSAGE}")
- #print_all_variables()
- endif()
- endif()
- # extract libf2c source
- file(ARCHIVE_EXTRACT INPUT ${F2C_FN} DESTINATION ${F2C_DIR})
- # prepare libf2c header files
- file(GLOB_RECURSE F2C_PREHEADERS "${F2C_DIR}/*.h0")
- foreach(H0 ${F2C_PREHEADERS})
- string(REGEX REPLACE "[.]h0$" ".h" H0_R ${H0})
- file(RENAME "${H0}" "${H0_R}")
- file(COPY "${H0_R}" DESTINATION "${F2C_INCLUDE_DIR}")
- endforeach()
- # get a load of this: f2c generates its own "arith.h" on the fly
- # so we gotta compile arithchk and run it
- set(F2C_ARITHCHK_SRC "${F2C_DIR}/arithchk.c")
- set(F2C_ARITHCHK_BIN "${F2C_DIR}/arithchk")
- if (WIN32)
- set(F2C_ARITHCHK_BIN "${F2C_ARITHCHK_BIN}.exe")
- endif()
- set(F2C_ARITH_H "${F2C_INCLUDE_DIR}/arith.h")
- set_source_files_properties(
- ${F2C_ARITHCHK_SRC} PROPERTIES COMPILE_FLAGS "-DNO_LONG_LONG -DNO_FPINIT"
- )
- add_executable(arithchk ${F2C_ARITHCHK_SRC})
- target_link_libraries(arithchk m)
- set_target_properties(
- arithchk PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${F2C_DIR}
- )
- add_custom_command(
- OUTPUT ${F2C_ARITH_H}
- COMMAND ${F2C_ARITHCHK_BIN} > ${F2C_ARITH_H}
- DEPENDS ${F2C_ARITHCHK_BIN}
- )
- # add libf2c to the compilation procedures
- include_directories(${F2C_INCLUDE_DIR})
- file(GLOB_RECURSE F2C_CFILES "${F2C_DIR}/*.c")
- add_definitions(-DINTEGER_STAR_8)
- add_library(f2c ${F2C_CFILES} ${F2C_ARITH_H})
- if (WIN32)
- # add some extra win32 flags for libf2c
- add_definitions(-DUSE_CLOCK -DMSDOS)
- # set some extra vars for MSYS builds to make the binary portable
- set(CMAKE_EXE_LINKER_FLAGS "-static")
- endif(WIN32)
- else(f2c_Compile)
- set(CMAKE_EXE_LINKER_FLAGS "-static")
- endif(f2c_Compile)
- # collect modules and interfaces into a module called MODULES
- # the overuse of the word MODULE is brain-twisting, but bear with me
- set(Modules_DIR "${CMAKE_SOURCE_DIR}/Modules")
- file(GLOB_RECURSE Modules_FILES "${Modules_DIR}/*.f*")
- # FUTURE FOR BLAS
- if(NOT enable_internal_blaslib2)
- message ("LAPACK files released since using VENDOR BLAS")
- file(GLOB MAINModules_FILES "${Modules_DIR}/*.f*")
- file(GLOB ARPACKModules_FILES "${Modules_DIR}/ARPACK/*.f*")
- file(GLOB LAPACKModules_FILES "${Modules_DIR}/LAPACK/*.f*")
- file(GLOB BANDITModules_FILES "${Modules_DIR}/BANDIT/*.f*")
- else ()
- file(GLOB_RECURSE Modules_FILES "${Modules_DIR}/*.f*")
- endif()
- file(GLOB_RECURSE Interfaces_FILES "${CMAKE_SOURCE_DIR}/Interfaces/*.f*")
- file(GLOB_RECURSE USE_IFs_FILES "${CMAKE_SOURCE_DIR}/USE_IFs/*.f*")
- # MODULES_ALL_FILES shall contain all the sources for the base modules
- if(NOT enable_internal_blaslib2)
- set(
- MODULES_ALL_FILES ${USE_IFs_FILES} ${Interfaces_FILES} ${MAINModules_FILES} ${ARPACKModules_FILES} ${LAPACKModules_FILES} ${BANDITModules_FILES}
- )
- else()
- set(
- MODULES_ALL_FILES ${USE_IFs_FILES} ${Interfaces_FILES} ${Modules_FILES}
- )
- endif()
- add_library(MODULES ${MODULES_ALL_FILES})
- # print_all_variables()
- # collect modules
- list(APPEND modules_names ARPACK BANDIT LK1 LK2 LK3 LK4 LK5 LK6 LK9 EMG)
- foreach (modname IN LISTS modules_names)
- file(GLOB_RECURSE TMP_MOD_FILES "${CMAKE_SOURCE_DIR}/${modname}/*.f*")
- add_library(${modname} ${TMP_MOD_FILES} ${TMP_MOD_FILES_PP})
- target_link_libraries(${modname} MODULES)
- endforeach()
- # add the DTRSV and DGSSV C modules.
- add_library(dgssv "${SUPERLU_DIR}/FORTRAN/c_fortran_dgssv.c")
- if (NOT enable_internal_blaslib2)
- target_link_libraries(dgssv)
- else ()
- target_link_libraries(dgssv blas)
- endif()
- # prepare the main executable, linked against the specifics and the m
- # it appears utils used to be a module, but that is no longer the case?
- file(GLOB_RECURSE UTIL_FILES "${CMAKE_SOURCE_DIR}/UTIL/*.f*")
- file(GLOB_RECURSE MAIN_FILES "${CMAKE_SOURCE_DIR}/MAIN/*.[fF]*")
- add_executable(mystran ${MAIN_FILES} ${MODULES_ALL_FILES} ${UTIL_FILES})
- #target_link_libraries(mystran ${modules_names})
- #target_link_libraries(mystran dgssv superlu)
- if(NOT enable_internal_blaslib2)
- target_link_libraries(mystran ${modules_names} dgssv superlu ${BLAS_LIBRARIES})
- else ()
- target_link_libraries(mystran ${modules_names} dgssv superlu)
- endif()
- set_target_properties(
- mystran PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}
- )
- # issue a couple messages about compilation
- If (${CMAKE_BUILD_TOOL} MATCHES "nmake")
- message(STATUS "You can now compile MYSTRAN with nmake: " )
- elseIf (${CMAKE_BUILD_TOOL} MATCHES "ninja")
- message(STATUS "You can now compile MYSTRAN with ninja, download first to this folder")
- elseIf (${CMAKE_GENERATOR} MATCHES "Visual Studio 17 2022")
- message(STATUS "You can now compile MYSTRAN with clicking Mystran.SLN file")
- elseIf (${CMAKE_GENERATOR} MATCHES "Visual Studio 16 2019")
- message(STATUS "You can now compile MYSTRAN with clicking Mystran.SLN file")
- elseIf (${CMAKE_BUILD_TOOL} MATCHES "jom")
- include(ProcessorCount)
- ProcessorCount(NCPU)
- message(STATUS "You can now compile MYSTRAN with jom.")
- if (NOT NCPU EQUAL 0)
- message(STATUS "Compile faster by passing /j${NCPU} to make.")
- endif()
- else()
- include(ProcessorCount)
- ProcessorCount(NCPU)
- message(STATUS "You can now compile MYSTRAN with make.")
- if (NOT NCPU EQUAL 0)
- message(STATUS "Compile faster by passing -j${NCPU} to make.")
- endif()
- endif()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement