Skip to content

Commit

Permalink
Fix library link order for older linkers.
Browse files Browse the repository at this point in the history
stdc++ is required to be linked first to avoid a segfault when
exceptions are thrown and pass into the Python layer.

Refs #10271
  • Loading branch information
martyngigg committed Sep 24, 2014
1 parent afa941f commit a58da79
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 49 deletions.
7 changes: 6 additions & 1 deletion Code/Mantid/Framework/Kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ 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} )
# For older gcc versions (<4.5) if we do not put stdc++ first then things crash when an exceptions are
# thrown in Python.
if ( GCC_COMPILER_VERSION AND GCC_COMPILER_VERSION VERSION_LESS "4.5" )
target_link_libraries ( Kernel stdc++ )
endif()
target_link_libraries ( Kernel ${NEXUS_LIBRARIES} ${MANTIDLIBS} ${GSL_LIBRARIES} )
if ( WIN32 )
target_link_libraries ( Kernel Psapi.lib ) # For memory usage queries
endif()
Expand Down
28 changes: 7 additions & 21 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,14 @@ 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 ${MPI_CXX_LIBRARIES} )

####################################################################################
# A macro for generating the exports
# - MODULE_TEMPLATE: The file containing the @EXPORT_FUNCTIONS@ and @EXPORT_DECALRE@ flags to replace
# - OUTPUT_FILE: The path to the generated output file
# - EXPORT_FILES: The variable containing the files to be processed
# - SRC_FILES: The variable containing the list of sources. Used to append the generated file.
# - SRC_FILES: The variable containing the list of sources. Used to append the generated file.
####################################################################################
MACRO( CREATE_MODULE MODULE_TEMPLATE OUTPUT_FILE EXPORT_FILES SRCS )
set ( EXPORT_DECLARE )
Expand All @@ -51,11 +37,11 @@ MACRO( CREATE_MODULE MODULE_TEMPLATE OUTPUT_FILE EXPORT_FILES SRCS )
set ( EXPORT_DECLARE "${EXPORT_DECLARE}\n${EXPORT};" )
string( REGEX REPLACE "void " "" EXPORT "${EXPORT}" )
set ( EXPORT_FUNCTIONS "${EXPORT_FUNCTIONS}\n${EXPORT};" )
endforeach(EXPORT ${EXPORTS})
endforeach(EXPORT ${EXPORTS})
endforeach( CPPFILE ${${EXPORT_FILES}} )
string( STRIP "${EXPORT_DECLARE}" EXPORT_DECLARE )
string( STRIP "${EXPORT_FUNCTIONS}" EXPORT_FUNCTIONS )

# Configure the final file
configure_file( ${MODULE_TEMPLATE} ${OUTPUT_FILE} )
# Set the sources
Expand All @@ -70,7 +56,7 @@ FUNCTION( SET_PYTHON_PROPERTIES TARGET TARGET_NAME )
set_target_properties( ${TARGET} PROPERTIES PREFIX "" )
# Library name needs to end in .pyd for Windows
if ( MSVC )
set_target_properties ( ${TARGET} PROPERTIES SUFFIX .pyd
set_target_properties ( ${TARGET} PROPERTIES SUFFIX .pyd
COMPILE_FLAGS "/bigobj /w44005 /w44244") # bigobj required for intensive templating
elseif ( APPLE )
# and in .so on the Mac
Expand All @@ -84,7 +70,7 @@ FUNCTION( SET_PYTHON_PROPERTIES TARGET TARGET_NAME )
endif ()
# Group within VS
set_property ( TARGET ${TARGET} PROPERTY FOLDER "MantidFramework/Python" )
ENDFUNCTION()
ENDFUNCTION()

###########################################################################
# mantid package
Expand All @@ -97,7 +83,7 @@ add_subdirectory ( mantid )
clean_orphaned_pyc_files ( ${CMAKE_CURRENT_SOURCE_DIR}/plugins )

###########################################################################
# tests
# tests
###########################################################################
add_subdirectory( test )

Expand Down
16 changes: 8 additions & 8 deletions Code/Mantid/Framework/PythonInterface/mantid/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set ( MODULE_TEMPLATE src/api.cpp.in )

# Files containing export definitions, these are automatically processed
# -- Do NOT sort this list. The order defines the order in which the export
# definitions occur and some depend on their base classes being exported first --
# definitions occur and some depend on their base classes being exported first --
set ( EXPORT_FILES
src/Exports/IAlgorithm.cpp
src/Exports/AlgorithmProxy.cpp
Expand Down Expand Up @@ -35,7 +35,7 @@ set ( EXPORT_FILES
src/Exports/MDGeometry.cpp
src/Exports/IMDWorkspace.cpp
src/Exports/IMDHistoWorkspace.cpp
src/Exports/IMDHistoWorkspaceProperty.cpp
src/Exports/IMDHistoWorkspaceProperty.cpp
src/Exports/IMDEventWorkspace.cpp
src/Exports/MatrixWorkspace.cpp
src/Exports/MatrixWorkspaceProperty.cpp
Expand Down Expand Up @@ -66,7 +66,7 @@ set ( EXPORT_FILES
src/Exports/Progress.cpp
src/Exports/FunctionProperty.cpp
src/Exports/AlgorithmProperty.cpp
)
)

# Files containing additional helper code that are not related to exporting class/functions
set ( SRC_FILES
Expand All @@ -91,7 +91,7 @@ set ( INC_FILES
${HEADER_DIR}/api/WorkspacePropertyExporter.h
)

set ( PY_FILES
set ( PY_FILES
__init__.py
_adsimports.py
_aliases.py
Expand All @@ -101,7 +101,7 @@ set ( PY_FILES
#############################################################################################
# Generate a source file from the export definitions
#############################################################################################
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/api.cpp EXPORT_FILES
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/api.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
Expand All @@ -115,7 +115,7 @@ if(CMAKE_GENERATOR STREQUAL Xcode)
set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PYTHON_PKG_ROOT}/api )
endif()

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
Expand All @@ -127,12 +127,12 @@ 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 API )

###########################################################################
# Installation settings
###########################################################################
install ( TARGETS PythonAPIModule ${SYSTEM_PACKAGE_TARGET} DESTINATION ${BIN_DIR}/mantid/api )

# Pure Python files
# Pure Python files
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR}/mantid/api )
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ set ( EXPORT_FILES
src/Exports/ReferenceFrame.cpp
src/Exports/Goniometer.cpp
src/Exports/Object.cpp
)
)

set ( SRC_FILES
)

set ( INC_FILES
set ( INC_FILES
)

set ( PY_FILES
Expand All @@ -39,7 +39,7 @@ set ( PY_FILES
#############################################################################################
# Generate a source file from the export definitions and provided template
#############################################################################################
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/geometry.cpp EXPORT_FILES
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/geometry.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
Expand All @@ -53,7 +53,7 @@ if(CMAKE_GENERATOR STREQUAL Xcode)
set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PYTHON_PKG_ROOT}/geometry )
endif()

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
Expand All @@ -64,12 +64,12 @@ 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 Geometry )

###########################################################################
# Installation settings
###########################################################################
install ( TARGETS PythonGeometryModule ${SYSTEM_PACKAGE_TARGET} DESTINATION ${BIN_DIR}/mantid/geometry )

# Pure Python files
# Pure Python files
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR}/mantid/geometry )
20 changes: 10 additions & 10 deletions Code/Mantid/Framework/PythonInterface/mantid/kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set ( MODULE_TEMPLATE src/kernel.cpp.in )

# Files containing export definitions, these are automatically processed
# -- Do NOT sort this list. The order defines the order in which the export
# definitions occur and some depend on their base classes being exported first --
# definitions occur and some depend on their base classes being exported first --
set ( EXPORT_FILES
src/Exports/ConfigService.cpp
src/Exports/DataItem.cpp
Expand Down Expand Up @@ -48,7 +48,7 @@ set ( EXPORT_FILES
src/Exports/ProgressBase.cpp
src/Exports/Material.cpp
src/Exports/Statistics.cpp
)
)

set ( SRC_FILES
src/Converters/CloneToNumpy.cpp
Expand All @@ -67,7 +67,7 @@ set ( SRC_FILES
src/Environment/WrapperHelpers.cpp
)

set ( INC_FILES
set ( INC_FILES
${HEADER_DIR}/kernel/Converters/CArrayToNDArray.h
${HEADER_DIR}/kernel/Converters/MatrixToNDArray.h
${HEADER_DIR}/kernel/Converters/NDArrayToVector.h
Expand All @@ -88,7 +88,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/Registry/PropertyWithValueFactory.h
${HEADER_DIR}/kernel/Registry/SequenceTypeHandler.h
${HEADER_DIR}/kernel/Registry/TypedPropertyValueHandler.h
${HEADER_DIR}/kernel/Registry/TypeRegistry.h
${HEADER_DIR}/kernel/Registry/TypeRegistry.h
${HEADER_DIR}/kernel/Registry/DowncastRegistry.h
${HEADER_DIR}/kernel/DataServiceExporter.h
${HEADER_DIR}/kernel/IsNone.h
Expand All @@ -99,7 +99,7 @@ set ( INC_FILES
${HEADER_DIR}/kernel/TypedValidatorExporter.h
)

set ( PY_FILES
set ( PY_FILES
__init__.py
_aliases.py
environment.py
Expand All @@ -110,7 +110,7 @@ set ( PY_FILES
#############################################################################################
# Generate a source file from the export definitions and provided template
#############################################################################################
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/kernel.cpp EXPORT_FILES
create_module ( ${MODULE_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/kernel.cpp EXPORT_FILES
SRC_FILES )

#############################################################################################
Expand All @@ -123,7 +123,7 @@ if(CMAKE_GENERATOR STREQUAL Xcode)
set ( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PYTHON_PKG_ROOT}/kernel )
endif()

copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
copy_python_files_to_dir ( "${PY_FILES}" ${CMAKE_CURRENT_SOURCE_DIR} ${OUTPUT_DIR}
PYTHON_INSTALL_FILES )

#############################################################################################
Expand Down Expand Up @@ -157,15 +157,15 @@ 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} )
target_link_libraries ( PythonKernelModule Kernel ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${PYTHON_DEPS} )

###########################################################################
# Installation settings
###########################################################################
install ( TARGETS PythonKernelModule ${SYSTEM_PACKAGE_TARGET} DESTINATION
install ( TARGETS PythonKernelModule ${SYSTEM_PACKAGE_TARGET} DESTINATION
${BIN_DIR}/mantid/kernel )

# Pure Python files
# Pure Python files
install ( FILES ${PY_FILES} DESTINATION ${BIN_DIR}/mantid/kernel )
# packagesetup.py that will overwrite the ones from the built target
install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGESETUP_PY}.install.py DESTINATION
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/PythonInterface/test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
##
## C++ unit tests
##
##
set ( TEST_FILES
PropertyWithValueFactoryTest.h
PythonObjectInstantiatorTest.h
PySequenceToVectorTest.h
RunPythonScriptTest.h
)

if ( CXXTEST_FOUND )
include_directories ( SYSTEM ${CXXTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} )

cxxtest_add_test ( PythonInterfaceCppTest ${TEST_FILES} )
if ( WIN32 )
set_target_properties( PythonInterfaceCppTest PROPERTIES COMPILE_FLAGS "/w44244" )
endif ()
target_link_libraries( PythonInterfaceCppTest PythonKernelModule PythonAPIModule )
target_link_libraries( PythonInterfaceCppTest PythonKernelModule PythonAPIModule ${PYTHON_LIBRARIES} )
add_dependencies ( FrameworkTests PythonInterfaceCppTest )
# Add to the 'UnitTests' group in VS
set_property ( TARGET PythonInterfaceCppTest PROPERTY FOLDER "UnitTests" )
Expand Down

0 comments on commit a58da79

Please sign in to comment.