From f3d9d3ad67239fe0965a58f61a2e2e40ffca3f7d Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Tue, 26 Nov 2019 19:26:18 +0000 Subject: [PATCH] [cmake] Don't link python libraries in unix-conda environment Conda links python libs statically to python executable --- CMakeLists.txt | 17 ++++++++++++---- cmake/NetgenConfig.cmake.in | 1 + cmake/SuperBuild.cmake | 1 + libsrc/core/CMakeLists.txt | 10 +++------- libsrc/core/ngcore_api.hpp | 30 +++++++++++++++++++++++++++++ libsrc/csg/CMakeLists.txt | 5 +++-- libsrc/geom2d/CMakeLists.txt | 2 +- libsrc/meshing/CMakeLists.txt | 2 +- libsrc/occ/CMakeLists.txt | 4 ++-- libsrc/stlgeom/CMakeLists.txt | 11 +++++------ libsrc/visualization/CMakeLists.txt | 2 +- ng/CMakeLists.txt | 12 +++++++++--- 12 files changed, 70 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ec88a7a..ee1b532a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,6 +29,7 @@ option( USE_SPDLOG "Enable spd log logging" OFF) option( DEBUG_LOG "Enable more debug output (may increase computation time) - only works with USE_SPDLOG=ON" OFF) option( CHECK_RANGE "Check array range access, automatically enabled if built in debug mode" OFF) option( BUILD_STUB_FILES "Build stub files for better autocompletion" ON) +option( BUILD_FOR_CONDA "Link python libraries only to executables" OFF) option( USE_SUPERBUILD "use ccache" ON) @@ -227,7 +228,7 @@ if(WIN32) add_definitions(-DMSVC_EXPRESS -D_CRT_SECURE_NO_WARNINGS -DHAVE_STRUCT_TIMESPEC) # build convenience (aka object) libraries in windows) set(NG_LIB_TYPE OBJECT) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /wd4244 /wd4800") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") else(WIN32) # build shared libraries set(NG_LIB_TYPE SHARED) @@ -270,9 +271,14 @@ if (USE_GUI) endif (USE_GUI) ####################################################################### +if(WIN32) + add_library(netgen_python INTERFACE IMPORTED) +else() + add_library(netgen_python INTERFACE) +endif() + if (USE_PYTHON) add_subdirectory(external_dependencies/pybind11) - add_definitions(-DNG_PYTHON) find_path(PYBIND_INCLUDE_DIR pybind11/pybind11.h HINTS ${PYTHON_INCLUDE_DIR}) if( PYBIND_INCLUDE_DIR ) message(STATUS "Found Pybind11: ${PYBIND_INCLUDE_DIR}") @@ -280,8 +286,11 @@ if (USE_PYTHON) message(FATAL_ERROR "Could NOT find pybind11!") endif( PYBIND_INCLUDE_DIR ) - include_directories(${PYBIND_INCLUDE_DIR}) - include_directories(${PYTHON_INCLUDE_DIRS}) + target_include_directories(netgen_python INTERFACE ${PYBIND_INCLUDE_DIR} ${PYTHON_INCLUDE_DIRS}) + if(NOT ${BUILD_FOR_CONDA}) + # Don't link python libraries in conda environments + target_link_libraries(netgen_python INTERFACE ${PYTHON_LIBRARIES}) + endif() if(NG_INSTALL_PYBIND) install(DIRECTORY ${PYBIND_INCLUDE_DIR}/pybind11 DESTINATION ${NG_INSTALL_DIR_INCLUDE} COMPONENT netgen_devel) diff --git a/cmake/NetgenConfig.cmake.in b/cmake/NetgenConfig.cmake.in index 67c85756..0606332b 100644 --- a/cmake/NetgenConfig.cmake.in +++ b/cmake/NetgenConfig.cmake.in @@ -14,6 +14,7 @@ get_filename_component(NETGEN_RESOURCE_DIR "${NETGEN_CMAKE_DIR}/@NETGEN_RESOURCE set(NETGEN_SOURCE_DIR "@PROJECT_SOURCE_DIR@") +set(NETGEN_BUILD_FOR_CONDA "@BUILD_FOR_CONDA@") set(NETGEN_CHECK_RANGE "@CHECK_RANGE@") set(NETGEN_INCLUDE_DIRS "${NETGEN_INCLUDE_DIR}/include;${NETGEN_INCLUDE_DIR}") set(NETGEN_CMAKE_THREAD_LIBS_INIT "@CMAKE_THREAD_LIBS_INIT@") diff --git a/cmake/SuperBuild.cmake b/cmake/SuperBuild.cmake index 6a58c2fe..c33997b9 100644 --- a/cmake/SuperBuild.cmake +++ b/cmake/SuperBuild.cmake @@ -151,6 +151,7 @@ set_vars( NETGEN_CMAKE_ARGS DEBUG_LOG CHECK_RANGE BUILD_STUB_FILES + BUILD_FOR_CONDA ) # propagate all variables set on the command line using cmake -DFOO=BAR diff --git a/libsrc/core/CMakeLists.txt b/libsrc/core/CMakeLists.txt index 7a8567c8..5c58e7e6 100644 --- a/libsrc/core/CMakeLists.txt +++ b/libsrc/core/CMakeLists.txt @@ -20,6 +20,7 @@ endif() if(USE_PYTHON) target_sources(ngcore PRIVATE python_ngcore.cpp) + target_compile_definitions(ngcore PUBLIC NETGEN_PYTHON NG_PYTHON) endif(USE_PYTHON) target_compile_definitions(ngcore PRIVATE NGCORE_EXPORTS) @@ -52,12 +53,7 @@ endif(USE_SPDLOG) install(TARGETS ngcore DESTINATION ${NG_INSTALL_DIR} COMPONENT netgen) -if(USE_PYTHON) - target_compile_definitions(ngcore PUBLIC NETGEN_PYTHON) - target_include_directories(ngcore PRIVATE ${PYTHON_INCLUDE_DIRS}) - target_link_libraries(ngcore PRIVATE ${PYTHON_LIBRARIES}) -endif(USE_PYTHON) -target_link_libraries(ngcore PUBLIC netgen_mpi PRIVATE ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(ngcore PUBLIC netgen_mpi PRIVATE netgen_python ${CMAKE_THREAD_LIBS_INIT}) install(FILES ngcore.hpp archive.hpp type_traits.hpp version.hpp ngcore_api.hpp logging.hpp exception.hpp symboltable.hpp paje_trace.hpp utils.hpp profiler.hpp mpi_wrapper.hpp @@ -71,7 +67,7 @@ endif(ENABLE_CPP_CORE_GUIDELINES_CHECK) if(USE_PYTHON) pybind11_add_module(pyngcore SHARED python_ngcore_export.cpp) - target_link_libraries(pyngcore PUBLIC ngcore ${PYTHON_LIBRARIES}) + target_link_libraries(pyngcore PUBLIC ngcore netgen_python) set_target_properties(pyngcore PROPERTIES INSTALL_RPATH "${NG_RPATH_TOKEN}/${NETGEN_PYTHON_RPATH}") install(TARGETS pyngcore DESTINATION ${NG_INSTALL_DIR_PYTHON} COMPONENT netgen) endif(USE_PYTHON) diff --git a/libsrc/core/ngcore_api.hpp b/libsrc/core/ngcore_api.hpp index 6f6e7756..9e21847d 100644 --- a/libsrc/core/ngcore_api.hpp +++ b/libsrc/core/ngcore_api.hpp @@ -1,6 +1,36 @@ #ifndef NETGEN_CORE_NGCORE_API_HPP #define NETGEN_CORE_NGCORE_API_HPP +#ifdef WIN32 + +// This function or variable may be unsafe. Consider using _ftime64_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. +#pragma warning(disable:4244) +#pragma warning(disable:4996) + +// multiple inheritance via dominance +#pragma warning(disable:4250) + +// needs to have dll-interface to be used by clients of class +#pragma warning(disable:4251) + +// size_t to int conversion: +#pragma warning(disable:4267) + +// non dll-interface class 'std::exception' used as base for dll-interface class +#pragma warning(disable:4275) + +// C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#pragma warning(disable:4290) + +// no suitable definition provided for explicit template instantiation request +#pragma warning(disable:4661) + +// bool-int conversion +#pragma warning(disable:4800) + +#endif // WIN32 + + #ifdef WIN32 #define NGCORE_API_EXPORT __declspec(dllexport) #define NGCORE_API_IMPORT __declspec(dllimport) diff --git a/libsrc/csg/CMakeLists.txt b/libsrc/csg/CMakeLists.txt index 30d61a2d..7d196b5b 100644 --- a/libsrc/csg/CMakeLists.txt +++ b/libsrc/csg/CMakeLists.txt @@ -11,7 +11,7 @@ if(APPLE) set_target_properties( csg PROPERTIES SUFFIX ".so") endif(APPLE) -target_link_libraries(csg PUBLIC mesh PRIVATE ${PYTHON_LIBRARIES}) +target_link_libraries(csg PUBLIC mesh PRIVATE netgen_python) if(NOT WIN32) install( TARGETS csg ${NG_INSTALL_DIR}) endif(NOT WIN32) @@ -20,8 +20,9 @@ target_link_libraries(csg PUBLIC ngcore) if(USE_GUI) add_library(csgvis ${NG_LIB_TYPE} vscsg.cpp ) + target_link_libraries(csgvis PRIVATE netgen_python PUBLIC ngcore) if(NOT WIN32) - target_link_libraries(csgvis csg visual) + target_link_libraries(csgvis PUBLIC csg visual) if(APPLE) set_target_properties( csgvis PROPERTIES SUFFIX ".so") endif(APPLE) diff --git a/libsrc/geom2d/CMakeLists.txt b/libsrc/geom2d/CMakeLists.txt index 2a29f6e5..ab292315 100644 --- a/libsrc/geom2d/CMakeLists.txt +++ b/libsrc/geom2d/CMakeLists.txt @@ -4,7 +4,7 @@ if(APPLE) set_target_properties( geom2d PROPERTIES SUFFIX ".so") endif(APPLE) -target_link_libraries(geom2d PUBLIC ngcore mesh PRIVATE ${PYTHON_LIBRARIES}) +target_link_libraries(geom2d PUBLIC ngcore mesh PRIVATE netgen_python) if(NOT WIN32) install( TARGETS geom2d ${NG_INSTALL_DIR}) endif(NOT WIN32) diff --git a/libsrc/meshing/CMakeLists.txt b/libsrc/meshing/CMakeLists.txt index e3b5c562..47b99e79 100644 --- a/libsrc/meshing/CMakeLists.txt +++ b/libsrc/meshing/CMakeLists.txt @@ -23,7 +23,7 @@ endif(APPLE) target_link_libraries( mesh PUBLIC ngcore PRIVATE gprim la gen ) -target_link_libraries( mesh PRIVATE netgen_metis ${ZLIB_LIBRARIES} ${PYTHON_LIBRARIES} ) +target_link_libraries( mesh PRIVATE netgen_metis netgen_python ${ZLIB_LIBRARIES} ) if(NOT WIN32) install( TARGETS mesh ${NG_INSTALL_DIR}) endif(NOT WIN32) diff --git a/libsrc/occ/CMakeLists.txt b/libsrc/occ/CMakeLists.txt index 68d26218..39da5a89 100644 --- a/libsrc/occ/CMakeLists.txt +++ b/libsrc/occ/CMakeLists.txt @@ -8,10 +8,10 @@ if(USE_GUI) add_library(occvis ${NG_LIB_TYPE} vsocc.cpp) endif(USE_GUI) -target_link_libraries(occ PUBLIC ngcore) +target_link_libraries(occ PUBLIC ngcore PRIVATE netgen_python) if(NOT WIN32) - target_link_libraries( occ PRIVATE ${OCC_LIBRARIES} ${PYTHON_LIBRARIES}) + target_link_libraries( occ PRIVATE ${OCC_LIBRARIES} ) if(USE_OCC AND APPLE) # Link AppKit in case OCE was built as static libraries find_library(AppKit AppKit) diff --git a/libsrc/stlgeom/CMakeLists.txt b/libsrc/stlgeom/CMakeLists.txt index 90d29b86..f8ce2fd3 100644 --- a/libsrc/stlgeom/CMakeLists.txt +++ b/libsrc/stlgeom/CMakeLists.txt @@ -4,18 +4,17 @@ add_library(stl ${NG_LIB_TYPE} ) if(NOT WIN32) - target_link_libraries( stl PUBLIC mesh PRIVATE ${PYTHON_LIBRARIES}) + target_link_libraries( stl PUBLIC mesh ) install( TARGETS stl ${NG_INSTALL_DIR}) endif(NOT WIN32) -target_link_libraries( stl PUBLIC ngcore ) +target_link_libraries( stl PUBLIC ngcore PRIVATE netgen_python ) if(USE_GUI) - add_library(stlvis ${NG_LIB_TYPE} - vsstl.cpp - ) + add_library(stlvis ${NG_LIB_TYPE} vsstl.cpp) + target_link_libraries(stlvis PRIVATE netgen_python PUBLIC ngcore) if(NOT WIN32) - target_link_libraries( stlvis stl ) + target_link_libraries( stlvis PUBLIC stl ) install( TARGETS stlvis ${NG_INSTALL_DIR}) endif(NOT WIN32) endif(USE_GUI) diff --git a/libsrc/visualization/CMakeLists.txt b/libsrc/visualization/CMakeLists.txt index ddbbc03f..a5604fb6 100644 --- a/libsrc/visualization/CMakeLists.txt +++ b/libsrc/visualization/CMakeLists.txt @@ -9,7 +9,7 @@ endif(USE_GUI) add_library(visual ${NG_LIB_TYPE} ${LIB_VISUAL_SOURCES}) -target_link_libraries( visual PUBLIC ngcore PRIVATE ${PYTHON_LIBRARIES} ${MPI_CXX_LIBRARIES} ${OPENGL_LIBRARIES} ) +target_link_libraries( visual PUBLIC ngcore PRIVATE netgen_python ${MPI_CXX_LIBRARIES} ${OPENGL_LIBRARIES} ) install( TARGETS visual ${NG_INSTALL_DIR}) install(FILES diff --git a/ng/CMakeLists.txt b/ng/CMakeLists.txt index 5e4e2c4e..99b11ef6 100644 --- a/ng/CMakeLists.txt +++ b/ng/CMakeLists.txt @@ -30,7 +30,7 @@ if(USE_GUI) target_link_libraries( gui PRIVATE ${TCL_LIBRARY} ${TK_LIBRARY}) endif(NOT APPLE) - target_link_libraries( netgen nglib gui ${MPI_mpi_LIBRARY} ${MPI_CXX_LIBRARIES} ${LIBTOGL} ${ZLIB_LIBRARIES} ${JPEG_LIBRARIES} ${FFMPEG_LIBRARIES} ${X11_Xmu_LIB} ${X11_X11_LIB} ${OCC_LIBRARIES} ${TK_LIBRARY} ${TCL_LIBRARY} ${PYTHON_LIBRARIES}) + target_link_libraries( netgen nglib gui netgen_python ${MPI_mpi_LIBRARY} ${MPI_CXX_LIBRARIES} ${LIBTOGL} ${ZLIB_LIBRARIES} ${JPEG_LIBRARIES} ${FFMPEG_LIBRARIES} ${X11_Xmu_LIB} ${X11_X11_LIB} ${OCC_LIBRARIES} ${TK_LIBRARY} ${TCL_LIBRARY}) if(NOT WIN32) target_link_libraries( netgen mesh stlvis stl geom2dvis interface geom2d csg stl visual csgvis ) @@ -46,13 +46,19 @@ if(USE_GUI) if(WIN32) set_target_properties( gui PROPERTIES OUTPUT_NAME libgui ) endif(WIN32) - target_link_libraries( gui PRIVATE ${PYTHON_LIBRARIES}) + target_link_libraries( gui PRIVATE netgen_python ) endif(USE_GUI) if(USE_PYTHON) + if(${BUILD_FOR_CONDA} AND UNIX AND NOT APPLE) + target_link_libraries( netgen -Wl,--no-as-needed ${PYTHON_LIBRARIES}) + else() + target_link_libraries( netgen ${PYTHON_LIBRARIES}) + endif() + add_library(ngpy SHARED netgenpy.cpp) - target_link_libraries( ngpy PUBLIC nglib PRIVATE ${PYTHON_LIBRARIES}) + target_link_libraries( ngpy PUBLIC nglib PRIVATE netgen_python ) if(APPLE) set_target_properties( ngpy PROPERTIES SUFFIX ".so") elseif(WIN32)