Skip to content

Commit

Permalink
Fix link order of libaries so that tcmalloc is first.
Browse files Browse the repository at this point in the history
This allows tcmalloc to replace malloc and report the correct memory
usage when running through Python, with the exception of systems running
gcc 4.4. On these systems, if stdc++ is not linked/loaded first then
a segfault occurs when throwing an exception across a dll boundary.
Refs #7798
  • Loading branch information
martyngigg committed Aug 7, 2014
1 parent e9cc7a9 commit 3746a91
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif ()

# Might just as well link everything to Boost & Poco (found in CommonSetup)
# TCMalloc & MPI_CXX variables will be empty if not using
set ( MANTIDLIBS ${Boost_LIBRARIES} ${POCO_LIBRARIES} ${TCMALLOC_LIBRARY} ${MPI_CXX_LIBRARIES} )
set ( MANTIDLIBS ${TCMALLOC_LIBRARY} ${Boost_LIBRARIES} ${POCO_LIBRARIES} ${MPI_CXX_LIBRARIES} )

###########################################################################
# Now add the packages one-by-one, building up the dependencies as we go
Expand All @@ -67,7 +67,7 @@ add_dependencies ( check FrameworkTests )

include_directories (Kernel/inc)
add_subdirectory (Kernel)
set ( MANTIDLIBS ${MANTIDLIBS} Kernel )
set ( MANTIDLIBS Kernel )

include_directories (Geometry/inc)
# muParser needed by Geometry and subsequent packages
Expand Down
15 changes: 14 additions & 1 deletion Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,20 @@ set_target_properties ( Kernel PROPERTIES OUTPUT_NAME MantidKernel
# Add to the 'Framework' group in VS
set_property ( TARGET Kernel PROPERTY FOLDER "MantidFramework" )

target_link_libraries ( Kernel ${MANTIDLIBS} ${GSL_LIBRARIES} ${NEXUS_LIBRARIES} )
# Using gcc, for tcmalloc to report the correct memory usage we must link it first.
# However, if we do not put stdc++ first on gcc 4.4 systems then things crash when an exceptions are thrown in Python
# If we are not using tcmalloc then the variable is empty and it makes no difference
if ( GCC_COMPILER_VERSION )
if ( GCC_COMPILER_VERSION VERSION_GREATER "4.4" )
target_link_libraries ( Kernel ${TCMALLOC_LIBRARY} stdc++ )
else ()
target_link_libraries ( Kernel stdc++ ${TCMALLOC_LIBRARY} )
endif()
endif()

# Main libraries
target_link_libraries ( Kernel ${NEXUS_LIBRARIES} ${MANTIDLIBS} ${GSL_LIBRARIES} )

if ( WIN32 )
target_link_libraries ( Kernel Psapi.lib ) # For memory usage queries
endif()
Expand Down
16 changes: 1 addition & 15 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@ include_directories ( SYSTEM ${PYTHON_NUMPY_INCLUDE_DIR} )
set ( HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/inc/MantidPythonInterface )
include_directories ( inc )
add_definitions ( -DBOOST_DEBUG_PYTHON -DBOOST_PYTHON_NO_LIB -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION )

if ( UNIX )
# On some Linux systems (seen on various Ubuntu versions) importing Mantid into a standalone python
# interpreter causes a segfault. It is some issue due to exception handling but the fix is
# to ensure that the stdc++ library appears as early in the link list as possible so that it
# is loaded first, hence the hard coding of it here rather than leaving it to be implicitly defined
# by the linker.
#
# MG 2011/11/15: A similar issue regarding the Nexus library has now been observed. Nexus 4.3 introduced
# thread-local variables and this now causes problems whenever we try and load the library from
# python if other libraries appear before it in the link list.
set ( PYTHON_DEPS stdc++ ${NEXUS_C_LIBRARIES} ${MANTIDLIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} )
else ()
set ( PYTHON_DEPS ${MANTIDLIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} )
endif ()
set ( PYTHON_DEPS ${MANTIDLIBS} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} )

####################################################################################
# A macro for generating the exports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ set_python_properties( PythonAPIModule _api )
set_target_output_directory ( PythonAPIModule ${OUTPUT_DIR} .pyd )

# Add the required dependencies
target_link_libraries ( PythonAPIModule PythonGeometryModule PythonKernelModule ${PYTHON_DEPS} )
target_link_libraries ( PythonAPIModule PythonGeometryModule PythonKernelModule ${PYTHON_DEPS} API )

###########################################################################
# Installation settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ add_library ( PythonGeometryModule ${SRC_FILES} ${INC_FILES} ${PYTHON_INSTALL_FI
set_python_properties( PythonGeometryModule _geometry )
set_target_output_directory ( PythonGeometryModule ${OUTPUT_DIR} .pyd )
# Add the required dependencies
target_link_libraries ( PythonGeometryModule PythonKernelModule ${PYTHON_DEPS} )
target_link_libraries ( PythonGeometryModule PythonKernelModule ${PYTHON_DEPS} Geometry )

###########################################################################
# Installation settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ add_library ( PythonKernelModule ${SRC_FILES} ${INC_FILES} ${PYTHON_INSTALL_FILE
set_python_properties( PythonKernelModule _kernel )
set_target_output_directory ( PythonKernelModule ${OUTPUT_DIR} .pyd )
# Add the required dependencies
target_link_libraries ( PythonKernelModule ${PYTHON_DEPS} )
# Kernel must be first so that stdc++ is loaded first on Linux
target_link_libraries ( PythonKernelModule Kernel ${PYTHON_DEPS} )

if ( UNIX )
add_library ( _dlopen src/dlopen.c )
Expand Down

0 comments on commit 3746a91

Please sign in to comment.