Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intrelab: Switch to target based Intrelab CMake build #10631

Merged
merged 15 commits into from
Jul 18, 2022
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 32 additions & 152 deletions packages/intrepid/matlab/intrelab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@

PROJECT(Intrelab)
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
#set(MATLAB_FIND_DEBUG TRUE)

# --> ACTION: define Trilinos install directory
#set(TRILINOS_INSTALL_DIR "/Users/pakuber/Compadre/Trilinos-new/pthread/install_release")
#set(TRILINOS_INSTALL_DIR "/Users/pakuber/Compadre/Trilinos-intrelab/pthread/install_release")
set(TRILINOS_INSTALL_DIR "/ascldap/users/pakuber/Compadre/Trilinos-intrelab/build/install")
kuberry marked this conversation as resolved.
Show resolved Hide resolved
if(DEFINED ENV{TRILINOS_INSTALL_DIR})
set(TRILINOS_INSTALL_DIR $ENV{TRILINOS_INSTALL_DIR})
endif()


# --> ACTION: define Matlab binary directory
#set(MATLAB_BIN_DIR "/Applications/MATLAB_R2020a.app/bin/")
set(MATLAB_BIN_DIR "/usr/local/matlab/R2021b/bin/")
if(DEFINED ENV{MATLAB_BIN_DIR})
set(MATLAB_BIN_DIR $ENV{MATLAB_BIN_DIR})
# --> ACTION: define Matlab root directory
#set(Matlab_ROOT_DIR "/Applications/MATLAB_R2020a.app/")
set(Matlab_ROOT_DIR "/usr/local/matlab/R2021b/")
if(DEFINED ENV{Matlab_ROOT_DIR})
set(Matlab_ROOT_DIR $ENV{Matlab_ROOT_DIR})
endif()
find_package(Matlab REQUIRED)

# --> ACTION: define install directory (full path or relative to the intrelab directory)
set(INST_DIR "install")
Expand All @@ -39,159 +42,36 @@ endif()

FIND_PACKAGE(Trilinos REQUIRED)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest that you pass in the exact Trilinos packages that Intrelab needs with:

find_package(Trilinos COMPONETS <pkg1> <plg2> ... REQUIRED)

so that if someone tries to configure this CMake project against an install of Trilinos without the necessary packages, then the find_package(Trilinos ... REQUIRED) command will error out and tell them what Trilinos packages (i.e. components) are missing. Otherwise, you will not see this mistake until build time at which point would confuse whoever is trying to builds these MEX files.


# set Matlab mex path with -largeArrayDims option
set(MEX "${MATLAB_BIN_DIR}/mex")
message(STATUS "MEX: ${MEX}")

# set compilers to Trilinos compilers
set(CMAKE_CXX_COMPILER ${Trilinos_CXX_COMPILER})
set(CMAKE_C_COMPILER ${Trilinos_C_COMPILER})

# set flags to Trilinos flags
set(CMAKE_CXX_FLAGS ${Trilinos_CXX_COMPILER_FLAGS})

# Trilinos doesn't propagate its C++ standard
set(MEX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fPIC")
if (APPLE)
# Trilinos doesn't propagate its sysroot
message(STATUS "CMAKE_OSX_SYSROOT: ${CMAKE_OSX_SYSROOT}")
set(MEX_FLAGS "--sysroot=${CMAKE_OSX_SYSROOT} ${MEX_FLAGS}")
endif(APPLE)

list (APPEND CMAKE_INSTALL_RPATH ${Trilinos_LIBRARY_DIRS})
list (APPEND CMAKE_INSTALL_RPATH ${Trilinos_TPL_LIBRARY_DIRS})
set(MOBJS "intrepid_setJacobian;intrepid_setJacobianInv;intrepid_setJacobianDet;intrepid_getNumCubaturePoints;intrepid_getCubature;intrepid_getBasisValues;intrepid_computeCellMeasure;intrepid_HGRADtransformGRAD;intrepid_HGRADtransformVALUE;intrepid_multiplyMeasure;intrepid_integrate;intrepid_mapToPhysicalFrame;intrepid_mapToReferenceFrame;intrepid_scalarMultiplyDataField;intrepid_scalarMultiplyDataData;intrepid_dataIntegral;intrepid_evaluate;intrepid_multiplyVectorDataField;intrepid_dotMultiplyDataField;intrepid_vectorMultiplyDataField;intrepid_dotMultiplyDataData;intrepid_computeEdgeMeasure;intrepid_computeFaceMeasure;intrepid_getCellDeviatoricMat;intrepid_getCellVolumetricMat;intrepid_getPhysicalSideNormals;intrepid_mapToReferenceSubcell;intrepid_tensorMultiplyDataData;intrepid_tensorMultiplyDataField;defGrad;evaluateVectorField;evaluateVectorGradField;externalForceElastostaticsTL;geometricBmat;intrepid_det;intrepid_inverse;intrepid_transpose;materialBmat;strainEulerianLagrangian;stress;tangentStiffnessElastostaticsTL;tensorMultiplyDataField;myMatVecProductDataField;myMatVecProductDataData")
kuberry marked this conversation as resolved.
Show resolved Hide resolved

if (Trilinos_INCLUDE_DIRS)
set(Trilinos_INCLUDE_DIRS "-I${Trilinos_INCLUDE_DIRS}")
endif()
if (Trilinos_TPL_INCLUDE_DIRS)
set(Trilinos_TPL_INCLUDE_DIRS "-I${Trilinos_TPL_INCLUDE_DIRS}")
endif()
if (Trilinos_LIBRARY_DIRS)
set(Trilinos_LIBRARY_DIRS "-L${Trilinos_LIBRARY_DIRS}")
endif()
if (Trilinos_TPL_LIBRARY_DIRS)
set(Trilinos_TPL_LIBRARY_DIRS "-L${Trilinos_TPL_LIBRARY_DIRS}")
if (NOT APPLE)
matlab_add_mex(NAME m2i_helpers
SHARED
SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/m2i_helpers.cpp
)
target_link_libraries(m2i_helpers Trilinos::all_libs)
endif()

set(l_Trilinos "")
foreach(alib ${Trilinos_LIBRARIES})
set(l_Trilinos "${l_Trilinos} -l${alib}")
endforeach()

set(l_Trilinos_TPL "")
foreach(alib ${Trilinos_TPL_LIBRARIES})
# check if library has a "." in it
string(REGEX MATCH "\.*" lib_ext_result ${alib})
if (lib_ext_result)

if (APPLE)
string(FIND ${alib} ".tbd" alib_result)
# make sure .tbd not found in library name
else()
string(FIND ${alib} ".so" alib_result)
# make sure .so not found in library name
endif(APPLE)

if (alib_result LESS 0)
# extension found but not .tbd or .so
message(FATAL_ERROR "${alib} encountered as TPL with unrecognized file extension.")
else()
# remove extension and get library name
get_filename_component(libname ${alib} NAME_WLE)
string(SUBSTRING ${libname} 3 -1 reduced_libname)
set(l_Trilinos_TPL "${l_Trilinos_TPL} -l${reduced_libname}")
endif()
function(make_mex fname)
if (APPLE)
matlab_add_mex(NAME ${fname}
SHARED
SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/m2i_helpers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/${fname}.cpp
)
else()
# no extension on library name
set(l_Trilinos_TPL "${l_Trilinos_TPL} -l${alib}")
endif()
endforeach()

if(APPLE)
if(Trilinos_BUILD_SHARED_LIBS)
set(MEX_LD_FLAGS "LDFLAGS=")
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -rpath ${Trilinos_INSTALL_DIR}/lib")
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -rpath ${MATLAB_BIN_DIR}/maci64")
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -shared")
endif()
else()
set(MEX_LD_FLAGS "LDFLAGS=")
foreach(rp ${CMAKE_INSTALL_RPATH})
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -Wl,-R,${rp}")
endforeach()
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -Wl,-R,${MATLAB_BIN_DIR}/glnxa64")
set(MEX_LD_FLAGS "${MEX_LD_FLAGS} -Wl,--enable-new-dtags")
endif()

separate_arguments(ll UNIX_COMMAND ${l_Trilinos})
set(l_Trilinos ${ll})

separate_arguments(ll UNIX_COMMAND ${l_Trilinos_TPL})
set(l_Trilinos_TPL ${ll})

if(APPLE)
set(MEX_EXTENSION "mexmaci64")
else()
set(MEX_EXTENSION "mexa64")
endif(APPLE)

# create objs folder
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/objs)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o
COMMAND "${MEX}" "CXX=${CMAKE_CXX_COMPILER}" "CXXFLAGS=${MEX_FLAGS}" "-largeArrayDims" -c ${Trilinos_INCLUDE_DIRS} ${Trilinos_TPL_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/src/m2i_helpers.cpp"
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/m2i_helpers.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/objs
matlab_add_mex(NAME ${fname}
SHARED
SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/${fname}.cpp
)

function(make_mex fname)

add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/objs/${fname}.o
COMMAND "${MEX}" "LDFLAGS=-shared" "CXX=${CMAKE_CXX_COMPILER}" "CXXFLAGS=${MEX_FLAGS}" "-largeArrayDims" -c ${Trilinos_INCLUDE_DIRS} ${Trilinos_TPL_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/src/${fname}.cpp"
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/objs/${fname}.o
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/${fname}.cpp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/objs
)

#add_custom_command(OUTPUT ${fname}.${MEX_EXTENSION}
# COMMAND "${MEX}" "LDFLAGS=-shared" "CXX=${CMAKE_CXX_COMPILER}" "CXXFLAGS=${MEX_FLAGS}" "-largeArrayDims" ${Trilinos_INCLUDE_DIRS} ${Trilinos_TPL_INCLUDE_DIRS} ${Trilinos_LIBRARY_DIRS} ${Trilinos_TPL_LIBRARY_DIRS} ${l_Trilinos} ${l_Trilinos_TPL} "${CMAKE_CURRENT_BINARY_DIR}/objs/${fname}.o" "${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o"
# DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/objs/${fname}.o ${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o
# BYPRODUCTS ${fname}.${MEX_EXTENSION}
# WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
# )

add_custom_command(OUTPUT ${fname}.${MEX_EXTENSION}
COMMAND "${MEX}" "${MEX_LD_FLAGS}" "CXX=${CMAKE_CXX_COMPILER}" "CXXFLAGS=${MEX_FLAGS}" "-largeArrayDims" ${Trilinos_INCLUDE_DIRS} ${Trilinos_TPL_INCLUDE_DIRS} ${Trilinos_LIBRARY_DIRS} ${Trilinos_TPL_LIBRARY_DIRS} ${l_Trilinos} ${l_Trilinos_TPL} "${CMAKE_CURRENT_SOURCE_DIR}/src/${fname}.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/m2i_helpers.cpp"
#DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/objs/${fname}.o ${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o
BYPRODUCTS ${fname}.${MEX_EXTENSION}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${fname}.${MEX_EXTENSION} DESTINATION ${CMAKE_INSTALL_PREFIX})

target_link_libraries(${fname} m2i_helpers)
endif()
target_link_libraries(${fname} Trilinos::all_libs)
endfunction(make_mex)
kuberry marked this conversation as resolved.
Show resolved Hide resolved

set(MOBJS "intrepid_setJacobian;intrepid_setJacobianInv;intrepid_setJacobianDet;intrepid_getNumCubaturePoints;intrepid_getCubature;intrepid_getBasisValues;intrepid_computeCellMeasure;intrepid_HGRADtransformGRAD;intrepid_HGRADtransformVALUE;intrepid_multiplyMeasure;intrepid_integrate;intrepid_mapToPhysicalFrame;intrepid_mapToReferenceFrame;intrepid_scalarMultiplyDataField;intrepid_scalarMultiplyDataData;intrepid_dataIntegral;intrepid_evaluate;intrepid_multiplyVectorDataField;intrepid_dotMultiplyDataField;intrepid_vectorMultiplyDataField;intrepid_dotMultiplyDataData;intrepid_computeEdgeMeasure;intrepid_computeFaceMeasure;intrepid_getCellDeviatoricMat;intrepid_getCellVolumetricMat;intrepid_getPhysicalSideNormals;intrepid_mapToReferenceSubcell;intrepid_tensorMultiplyDataData;intrepid_tensorMultiplyDataField;defGrad;evaluateVectorField;evaluateVectorGradField;externalForceElastostaticsTL;geometricBmat;intrepid_det;intrepid_inverse;intrepid_transpose;materialBmat;strainEulerianLagrangian;stress;tangentStiffnessElastostaticsTL;tensorMultiplyDataField;myMatVecProductDataField;myMatVecProductDataData")

# dummy targets to build up dependencies
#add_custom_target(m2i_helpers
# ALL
# COMMAND # left intentionally blank
# DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/objs/m2i_helpers.o
# )
foreach(fname ${MOBJS})
make_mex(${fname})
# builds dependency chain
add_custom_target(${fname}
ALL
COMMAND # left intentionally blank
#DEPENDS m2i_helpers ${fname}.${MEX_EXTENSION}
DEPENDS ${fname}.${MEX_EXTENSION}
)
make_mex(${fname})
endforeach()

if(APPLE)
message("\"*.dylib was built for newer macOS version (*.*) than being linked (10.9)\" type warnings can be safely ignored. MEX compiler specifies a -mmacosx-min-version=10.9, which can not be overridden.")
endif(APPLE)

install(TARGETS ${MOBJS}
DESTINATION ${CMAKE_INSTALL_PREFIX}
)