From dd3959990342224edb6a2c1a6919d4bfb8d0a438 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Mon, 29 Oct 2018 14:23:03 +0100 Subject: [PATCH] Link eeschema with ngspice DLL Adds a link-time dependency for libngspice, so that other tools may detect ngspice as a KiCad dependency. The library is still loaded with dlopen() as it gives a way to reload it in case of problems. The DLL name is recognized during CMake configuration and used to load the library at runtime. --- CMakeModules/Findngspice.cmake | 15 +++++++++++---- eeschema/CMakeLists.txt | 16 ++++++++++++++++ eeschema/sim/ngspice.cpp | 6 +++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/CMakeModules/Findngspice.cmake b/CMakeModules/Findngspice.cmake index 5a09afbe1f..6f7a50ae03 100644 --- a/CMakeModules/Findngspice.cmake +++ b/CMakeModules/Findngspice.cmake @@ -12,6 +12,15 @@ find_library( NGSPICE_LIBRARY ngspice PATH_SUFFIXES src/.libs lib ) +if( WIN32 AND MSYS ) + # NGSPICE_LIBRARY points to libngspice.dll.a on Windows, + # but the goal is to find out the DLL name. + find_library( NGSPICE_DLL NAMES libngspice-0.dll libngspice-1.dll ) +else() + set( NGSPICE_DLL "${NGSPICE_LIBRARY}" ) +endif() + + include( FindPackageHandleStandardArgs ) if( ${NGSPICE_INCLUDE_DIR} STREQUAL "NGSPICE_INCLUDE_DIR-NOTFOUND" OR ${NGSPICE_LIBRARY} STREQUAL "NGSPICE_LIBRARY-NOTFOUND" ) @@ -28,12 +37,10 @@ if( ${NGSPICE_INCLUDE_DIR} STREQUAL "NGSPICE_INCLUDE_DIR-NOTFOUND" OR ${NGSPICE_ endif() find_package_handle_standard_args( ngspice - REQUIRED_VARS - NGSPICE_INCLUDE_DIR - NGSPICE_LIBRARY -) + REQUIRED_VARS NGSPICE_INCLUDE_DIR NGSPICE_LIBRARY NGSPICE_DLL ) mark_as_advanced( NGSPICE_INCLUDE_DIR NGSPICE_LIBRARY + NGSPICE_DLL ) diff --git a/eeschema/CMakeLists.txt b/eeschema/CMakeLists.txt index 819812d07d..4725a8f0a5 100644 --- a/eeschema/CMakeLists.txt +++ b/eeschema/CMakeLists.txt @@ -7,6 +7,15 @@ add_definitions( -DEESCHEMA ) if( KICAD_SPICE ) set( INC_AFTER ${INC_AFTER} ${NGSPICE_INCLUDE_DIR} ) + + # Find out the exact libngspice file name + get_filename_component( NGSPICE_DLL_REALPATH "${NGSPICE_DLL}" REALPATH ) + get_filename_component( NGSPICE_DLL_FILE "${NGSPICE_DLL_REALPATH}" NAME ) + + set_property( SOURCE sim/ngspice.cpp + APPEND PROPERTY COMPILE_DEFINITIONS + NGSPICE_DLL_FILE="${NGSPICE_DLL_FILE}" + ) endif() include_directories( BEFORE ${INC_BEFORE} ) @@ -315,6 +324,13 @@ target_link_libraries( eeschema_kiface ${wxWidgets_LIBRARIES} ${GDI_PLUS_LIBRARIES} ) + +if( KICAD_SPICE ) + target_link_libraries( eeschema_kiface + ${NGSPICE_LIBRARY} + ) +endif() + set_target_properties( eeschema_kiface PROPERTIES # Decorate OUTPUT_NAME with PREFIX and SUFFIX, creating something like # _eeschema.so, _eeschema.dll, or _eeschema.kiface diff --git a/eeschema/sim/ngspice.cpp b/eeschema/sim/ngspice.cpp index 3b6e4dd749..31885c2a63 100644 --- a/eeschema/sim/ngspice.cpp +++ b/eeschema/sim/ngspice.cpp @@ -289,11 +289,11 @@ void NGSPICE::init_dll() // Extra effort to find libngspice #if defined(__WINDOWS__) || (__WXMAC__) #ifdef __WINDOWS__ - wxFileName dllFile( "", "libngspice-0.dll" ); + wxFileName dllFile( "", NGSPICE_DLL_FILE ); const vector dllPaths = { "", "/mingw64/bin", "/mingw32/bin" }; #endif /* __WINDOWS__ */ #ifdef __WXMAC__ - wxFileName dllFile( "", "libngspice.0.dylib" ); + wxFileName dllFile( "", NGSPICE_DLL_FILE ); const vector dllPaths = { GetOSXKicadUserDataDir() + "/PlugIns/ngspice", GetOSXKicadMachineDataDir() + "/PlugIns/ngspice", @@ -320,7 +320,7 @@ void NGSPICE::init_dll() if( !m_dll.IsLoaded() ) // try also the system libraries m_dll.Load( wxDynamicLibrary::CanonicalizeName( "ngspice" ) ); #else - m_dll.Load("libngspice.so.0"); + m_dll.Load( NGSPICE_DLL_FILE ); #endif /* __WINDOWS || __WXMAC__ */ if( !m_dll.IsLoaded() )