KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_library_adapter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
23#include <chrono>
24#include <env_vars.h>
25#include <footprint_info_impl.h>
26#include <thread_pool.h>
27#include <trace_helpers.h>
28#include <footprint.h>
29
30#include <magic_enum.hpp>
31#include <wx/hash.h>
32#include <wx/log.h>
33
34using namespace std::chrono_literals;
35
36
38
40
42
44
45
50
51
53{
54 return ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) );
55}
56
57
58void FOOTPRINT_LIBRARY_ADAPTER::enumerateLibrary( LIB_DATA* aLib, const wxString& aUri )
59{
60 wxArrayString namesAS;
61 std::map<std::string, UTF8> options = aLib->row->GetOptionsMap();
62 PCB_IO* plugin = pcbplugin( aLib );
63 wxString nickname = aLib->row->Nickname();
64
65 // FootprintEnumerate populates the plugin's internal FP_CACHE with parsed footprints
66 plugin->FootprintEnumerate( namesAS, aUri, false, &options );
67
68 std::vector<std::unique_ptr<FOOTPRINT>> footprints;
69 footprints.reserve( namesAS.size() );
70
71 // For plugins with internal caches (like kicad_sexpr), GetEnumeratedFootprint returns
72 // a borrowed pointer and ClearCachedFootprints handles cleanup. For other plugins,
73 // GetEnumeratedFootprint allocates new memory that we must delete after cloning.
74 const bool pluginCaches = plugin->CachesEnumeratedFootprints();
75
76 for( const wxString& footprintName : namesAS )
77 {
78 try
79 {
80 const FOOTPRINT* cached = plugin->GetEnumeratedFootprint( aUri, footprintName, &options );
81
82 if( !cached )
83 continue;
84
85 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( cached->Duplicate( IGNORE_PARENT_GROUP ) );
86 footprint->SetParent( nullptr );
87
88 // For non-caching plugins, delete the allocated footprint now that we've cloned it
89 if( !pluginCaches )
90 delete cached;
91
92 LIB_ID id = footprint->GetFPID();
93 id.SetLibNickname( nickname );
94 footprint->SetFPID( id );
95 footprints.emplace_back( footprint );
96 }
97 catch( IO_ERROR& e )
98 {
99 wxLogTrace( traceLibraries, "FP: %s:%s enumeration error: %s",
100 nickname, footprintName, e.What() );
101 }
102 }
103
104 {
105 std::unique_lock lock( PreloadedFootprintsMutex );
106 PreloadedFootprints.Get()[nickname] = std::move( footprints );
107 }
108
109 // Clear the plugin's FP_CACHE now that we've copied footprints to PreloadedFootprints.
110 // This eliminates the double-caching that was consuming ~1.2GB of extra RAM.
111 plugin->ClearCachedFootprints( aUri );
112}
113
114
115std::optional<LIB_STATUS> FOOTPRINT_LIBRARY_ADAPTER::LoadOne( LIB_DATA* aLib )
116{
118
119 std::map<std::string, UTF8> options = aLib->row->GetOptionsMap();
120
121 try
122 {
123 wxArrayString dummyList;
124 pcbplugin( aLib )->FootprintEnumerate( dummyList, getUri( aLib->row ), false, &options );
126 }
127 catch( IO_ERROR& e )
128 {
130 aLib->status.error = LIBRARY_ERROR( { e.What() } );
131 wxLogTrace( traceLibraries, "FP: %s: plugin threw exception: %s", aLib->row->Nickname(), e.What() );
132 }
133
134 return aLib->status;
135}
136
137
138std::optional<LIB_STATUS> FOOTPRINT_LIBRARY_ADAPTER::LoadOne( const wxString& nickname )
139{
141
142 if( result.has_value() )
143 return LoadOne( *result );
144
145 return LIB_STATUS{
146 .load_status = LOAD_STATUS::LOAD_ERROR,
147 .error = LIBRARY_ERROR( { result.error() } )
148 };
149}
150
151
152std::vector<FOOTPRINT*> FOOTPRINT_LIBRARY_ADAPTER::GetFootprints( const wxString& aNickname, bool aBestEfforts )
153{
154 std::vector<FOOTPRINT*> footprints;
155
156 std::shared_lock lock( PreloadedFootprintsMutex );
157 auto it = PreloadedFootprints.Get().find( aNickname );
158
159 if( it == PreloadedFootprints.Get().end() )
160 return footprints;
161
162 footprints.reserve( it->second.size() );
163
164 for( const auto& fp : it->second )
165 footprints.push_back( fp.get() );
166
167 return footprints;
168}
169
170
171
172std::vector<wxString> FOOTPRINT_LIBRARY_ADAPTER::GetFootprintNames( const wxString& aNickname, bool aBestEfforts )
173{
174 // TODO(JE) can we kill wxArrayString in internal API?
175 wxArrayString namesAS;
176 std::vector<wxString> names;
177
178 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
179 {
180 const LIB_DATA* lib = *maybeLib;
181 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
182
183 try
184 {
185 pcbplugin( lib )->FootprintEnumerate( namesAS, getUri( lib->row ), true, &options );
186 }
187 catch( IO_ERROR& e )
188 {
189 wxLogTrace( traceLibraries, "FP: Exception enumerating library %s: %s", lib->row->Nickname(), e.What() );
190 }
191 }
192
193 for( const wxString& name : namesAS )
194 names.emplace_back( name );
195
196 return names;
197}
198
199
200long long FOOTPRINT_LIBRARY_ADAPTER::GenerateTimestamp( const wxString* aNickname )
201{
202 long long hash = 0;
203
204 if( aNickname )
205 {
206 wxCHECK( HasLibrary( *aNickname, true ), hash );
207
208 if( std::optional<const LIB_DATA*> r = fetchIfLoaded( *aNickname ); r.has_value() )
209 {
210 PCB_IO* plugin = dynamic_cast<PCB_IO*>( ( *r )->plugin.get() );
211 wxCHECK( plugin, hash );
212 return plugin->GetLibraryTimestamp( LIBRARY_MANAGER::GetFullURI( ( *r )->row, true ) )
213 + wxHashTable::MakeKey( *aNickname );
214 }
215 }
216
217 for( const wxString& nickname : GetLibraryNames() )
218 {
219 if( std::optional<const LIB_DATA*> r = fetchIfLoaded( nickname ); r.has_value() )
220 {
221 wxCHECK2( ( *r )->plugin->IsPCB_IO(), continue );
222 PCB_IO* plugin = static_cast<PCB_IO*>( ( *r )->plugin.get() );
223 hash += plugin->GetLibraryTimestamp( LIBRARY_MANAGER::GetFullURI( ( *r )->row, true ) )
224 + wxHashTable::MakeKey( nickname );
225 }
226 }
227
228 return hash;
229}
230
231
232bool FOOTPRINT_LIBRARY_ADAPTER::FootprintExists( const wxString& aNickname, const wxString& aName )
233{
234 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
235 {
236 const LIB_DATA* lib = *maybeLib;
237 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
238 return pcbplugin( lib )->FootprintExists( getUri( lib->row ), aName, &options );
239 }
240
241 return false;
242}
243
244
245FOOTPRINT* FOOTPRINT_LIBRARY_ADAPTER::LoadFootprint( const wxString& aNickname, const wxString& aName, bool aKeepUUID )
246{
247 // First check if the footprint is in PreloadedFootprints and clone from there.
248 // This avoids re-parsing the file and keeps FP_CACHE from being repopulated.
249 {
250 std::shared_lock lock( PreloadedFootprintsMutex );
251 auto libIt = PreloadedFootprints.Get().find( aNickname );
252
253 if( libIt != PreloadedFootprints.Get().end() )
254 {
255 for( const auto& fp : libIt->second )
256 {
257 if( fp->GetFPID().GetLibItemName() == UTF8( aName ) )
258 {
260
261 if( aKeepUUID )
262 copy = static_cast<FOOTPRINT*>( fp->Clone() );
263 else
264 copy = static_cast<FOOTPRINT*>( fp->Duplicate( IGNORE_PARENT_GROUP ) );
265
266 copy->SetParent( nullptr );
267 return copy;
268 }
269 }
270 }
271 }
272
273 // Footprint not found in PreloadedFootprints, fall back to plugin.
274 // This re-parses the file but is needed for footprints not yet enumerated.
275 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
276 {
277 try
278 {
279 if( FOOTPRINT* footprint = pcbplugin( *lib )->FootprintLoad( getUri( ( *lib )->row ), aName, aKeepUUID ) )
280 {
281 LIB_ID id = footprint->GetFPID();
282 id.SetLibNickname( ( *lib )->row->Nickname() );
283 footprint->SetFPID( id );
284 return footprint;
285 }
286 }
287 catch( const IO_ERROR& ioe )
288 {
289 wxLogTrace( traceLibraries, "LoadFootprint: error loading %s:%s: %s", aNickname, aName, ioe.What() );
290 }
291 }
292 else
293 {
294 wxLogTrace( traceLibraries, "LoadFootprint: requested library %s not loaded", aNickname );
295 }
296
297 return nullptr;
298}
299
300
302{
303 wxString nickname = aFootprintId.GetLibNickname();
304 wxString footprintName = aFootprintId.GetLibItemName();
305
306 if( nickname.size() )
307 return LoadFootprint( nickname, footprintName, aKeepUUID );
308
309 // nickname is empty, sequentially search (alphabetically) all libs/nicks for first match:
310 for( const wxString& library : GetLibraryNames() )
311 {
312 // FootprintLoad() returns NULL on not found, does not throw exception
313 // unless there's an IO_ERROR.
314 if( FOOTPRINT* ret = LoadFootprint( library, footprintName, aKeepUUID ) )
315 return ret;
316 }
317
318 return nullptr;
319}
320
321
323 const FOOTPRINT* aFootprint,
324 bool aOverwrite )
325{
326 wxCHECK( aFootprint, SAVE_SKIPPED );
327
328 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
329 {
330 if( !aOverwrite )
331 {
332 wxString fpname = aFootprint->GetFPID().GetLibItemName();
333
334 try
335 {
336 FOOTPRINT* existing = pcbplugin( *lib )->FootprintLoad( getUri( ( *lib )->row ), fpname, false );
337
338 if( existing )
339 {
340 delete existing;
341 return SAVE_SKIPPED;
342 }
343 }
344 catch( IO_ERROR& e )
345 {
346 wxLogTrace( traceLibraries, "SaveFootprint: error checking for existing footprint %s: %s",
347 aFootprint->GetFPIDAsString(), e.What() );
348 return SAVE_SKIPPED;
349 }
350 }
351
352 try
353 {
354 pcbplugin( *lib )->FootprintSave( getUri( ( *lib )->row ), aFootprint );
355 }
356 catch( IO_ERROR& e )
357 {
358 wxLogTrace( traceLibraries, "SaveFootprint: error saving %s: %s",
359 aFootprint->GetFPIDAsString(), e.What() );
360 return SAVE_SKIPPED;
361 }
362
363 {
364 std::unique_lock lock( PreloadedFootprintsMutex );
365 auto it = PreloadedFootprints.Get().find( aNickname );
366
367 if( it != PreloadedFootprints.Get().end() )
368 {
369 wxString fpName = aFootprint->GetFPID().GetLibItemName();
370
371 if( aOverwrite )
372 {
373 auto& footprints = it->second;
374 footprints.erase( std::remove_if( footprints.begin(), footprints.end(),
375 [&fpName]( const std::unique_ptr<FOOTPRINT>& fp )
376 {
377 return fp->GetFPID().GetLibItemName().wx_str() == fpName;
378 } ),
379 footprints.end() );
380 }
381
382 FOOTPRINT* clone = static_cast<FOOTPRINT*>( aFootprint->Duplicate( IGNORE_PARENT_GROUP ) );
383 clone->SetParent( nullptr );
384
385 LIB_ID id = clone->GetFPID();
386 id.SetLibNickname( aNickname );
387 clone->SetFPID( id );
388
389 it->second.emplace_back( clone );
390 }
391 }
392
393 return SAVE_OK;
394 }
395 else
396 {
397 wxLogTrace( traceLibraries, "SaveFootprint: requested library %s not loaded", aNickname );
398 return SAVE_SKIPPED;
399 }
400}
401
402
403void FOOTPRINT_LIBRARY_ADAPTER::DeleteFootprint( const wxString& aNickname, const wxString& aFootprintName )
404{
405 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
406 {
407 try
408 {
409 pcbplugin( *lib )->FootprintDelete( getUri( ( *lib )->row ), aFootprintName );
410 }
411 catch( IO_ERROR& e )
412 {
413 wxLogTrace( traceLibraries, "DeleteFootprint: error deleting %s:%s: %s", aNickname,
414 aFootprintName, e.What() );
415 return;
416 }
417
418 {
419 std::unique_lock lock( PreloadedFootprintsMutex );
420 auto it = PreloadedFootprints.Get().find( aNickname );
421
422 if( it != PreloadedFootprints.Get().end() )
423 {
424 auto& footprints = it->second;
425 footprints.erase( std::remove_if( footprints.begin(), footprints.end(),
426 [&aFootprintName]( const std::unique_ptr<FOOTPRINT>& fp )
427 {
428 return fp->GetFPID().GetLibItemName().wx_str() == aFootprintName;
429 } ),
430 footprints.end() );
431 }
432 }
433 }
434 else
435 {
436 wxLogTrace( traceLibraries, "DeleteFootprint: requested library %s not loaded", aNickname );
437 }
438}
439
440
442{
443 {
444 std::shared_lock lock( m_librariesMutex );
445
446 if( auto it = m_libraries.find( aLib ); it != m_libraries.end() )
447 return it->second.plugin->IsLibraryWritable( getUri( it->second.row ) );
448 }
449
450 {
451 std::shared_lock lock( GlobalLibraryMutex );
452
453 if( auto it = GlobalLibraries.Get().find( aLib ); it != GlobalLibraries.Get().end() )
454 return it->second.plugin->IsLibraryWritable( getUri( it->second.row ) );
455 }
456
457 return false;
458}
459
460
462{
464
465 if( type == PCB_IO_MGR::NESTED_TABLE )
466 {
467 wxString msg;
468 wxFileName fileName( row->URI() );
469
470 if( fileName.FileExists() )
471 return tl::unexpected( LIBRARY_TABLE_OK() );
472 else
473 msg = wxString::Format( _( "Nested table '%s' not found." ), row->URI() );
474
475 return tl::unexpected( LIBRARY_ERROR( msg ) );
476 }
477 else if( type == PCB_IO_MGR::PCB_FILE_UNKNOWN )
478 {
479 wxLogTrace( traceLibraries, "FP: Plugin type %s is unknown!", row->Type() );
480 wxString msg = wxString::Format( _( "Unknown library type %s " ), row->Type() );
481 return tl::unexpected( LIBRARY_ERROR( msg ) );
482 }
483
485 wxCHECK( plugin, tl::unexpected( LIBRARY_ERROR( _( "Internal error" ) ) ) );
486
487 wxLogTrace( traceLibraries, "FP: Library %s (%s) plugin created", row->Nickname(),
488 magic_enum::enum_name( row->Scope() ) );
489
490 return plugin;
491}
492
493
495{
496 // Note: can't use dynamic_cast across compile units on Mac
497 wxCHECK( aRow->plugin->IsPCB_IO(), nullptr );
498 PCB_IO* ret = static_cast<PCB_IO*>( aRow->plugin.get() );
499 return ret;
500}
const char * name
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
bool IsFootprintLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
static std::shared_mutex GlobalLibraryMutex
void DeleteFootprint(const wxString &aNickname, const wxString &aFootprintName)
Deletes the aFootprintName from the library given by aNickname.
std::vector< FOOTPRINT * > GetFootprints(const wxString &aNickname, bool aBestEfforts=false)
Retrieves a list of footprints contained in a given loaded library.
void enumerateLibrary(LIB_DATA *aLib, const wxString &aUri) override
Override in derived class to perform library-specific enumeration.
IO_BASE * plugin(const LIB_DATA *aRow) override
SAVE_T SaveFootprint(const wxString &aNickname, const FOOTPRINT *aFootprint, bool aOverwrite=true)
Write aFootprint to an existing library given by aNickname.
std::vector< wxString > GetFootprintNames(const wxString &aNickname, bool aBestEfforts=false)
Retrieves a list of footprint names contained in a given loaded library.
FOOTPRINT * LoadFootprint(const wxString &aNickname, const wxString &aName, bool aKeepUUID)
Load a FOOTPRINT having aName from the library given by aNickname.
static LEAK_AT_EXIT< std::map< wxString, LIB_DATA > > GlobalLibraries
static PCB_IO * pcbplugin(const LIB_DATA *aRow)
FOOTPRINT * LoadFootprintWithOptionalNickname(const LIB_ID &aFootprintId, bool aKeepUUID)
Load a footprint having aFootprintId with possibly an empty nickname.
LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row) override
Creates a concrete plugin for the given row.
FOOTPRINT_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
long long GenerateTimestamp(const wxString *aNickname)
Generates a filesystem timestamp / hash value for library(ies)
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
SAVE_T
The set of return values from SaveSymbol() below.
static LEAK_AT_EXIT< std::map< wxString, std::vector< std::unique_ptr< FOOTPRINT > > > > PreloadedFootprints
Storage for preloaded footprints, indexed by library nickname.
bool FootprintExists(const wxString &aNickname, const wxString &aName)
static std::shared_mutex PreloadedFootprintsMutex
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:352
wxString GetFPIDAsString() const
Definition footprint.h:357
const LIB_ID & GetFPID() const
Definition footprint.h:351
BOARD_ITEM * Duplicate(bool addToParentGroup, BOARD_COMMIT *aCommit=nullptr) const override
Create a copy of this BOARD_ITEM.
virtual bool IsPCB_IO() const
Work-around for lack of dynamic_cast across compile units on Mac.
Definition io_base.h:84
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
A wrapper for static data that should not be destroyed at program exit.
LIBRARY_MANAGER_ADAPTER(LIBRARY_MANAGER &aManager)
Constructs a type-specific adapter into the library manager.
LIBRARY_RESULT< LIB_DATA * > loadIfNeeded(const wxString &aNickname)
Fetches a loaded library, triggering a load of that library if it isn't loaded yet.
std::shared_mutex m_librariesMutex
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::map< wxString, LIB_DATA > m_libraries
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
LIBRARY_TABLE_SCOPE Scope() const
std::map< std::string, UTF8 > GetOptionsMap() const
const wxString & Type() const
const wxString & URI() const
const wxString & Nickname() const
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
Definition lib_id.cpp:100
const UTF8 & GetLibItemName() const
Definition lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition lib_id.h:87
static PCB_FILE_T EnumFromStr(const wxString &aFileType)
Return the PCB_FILE_T from the corresponding plugin type name: "kicad", "legacy", etc.
PCB_FILE_T
The set of file types that the PCB_IO_MGR knows about, and for which there has been a plugin written,...
Definition pcb_io_mgr.h:56
@ PCB_FILE_UNKNOWN
0 is not a legal menu id on Mac
Definition pcb_io_mgr.h:57
static PCB_IO * FindPlugin(PCB_FILE_T aFileType)
Return a #PLUGIN which the caller can use to import, export, save, or load design documents.
A base class that BOARD loading and saving plugins should derive from.
Definition pcb_io.h:70
virtual void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
Return a list of footprint names contained within the library at aLibraryPath.
Definition pcb_io.cpp:95
virtual bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Check for the existence of a footprint.
Definition pcb_io.cpp:136
virtual void FootprintDelete(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete aFootprintName from the library at aLibraryPath.
Definition pcb_io.cpp:160
virtual FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
Definition pcb_io.cpp:144
virtual void FootprintSave(const wxString &aLibraryPath, const FOOTPRINT *aFootprint, const std::map< std::string, UTF8 > *aProperties=nullptr)
Write aFootprint to an existing library located at aLibraryPath.
Definition pcb_io.cpp:152
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:71
#define _(s)
#define IGNORE_PARENT_GROUP
Definition eda_item.h:56
Functions related to environment variables, including help functions.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:77
Storage for an actual loaded library (including library content owned by the plugin)
LIB_STATUS status
std::unique_ptr< IO_BASE > plugin
const LIBRARY_TABLE_ROW * row
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
LOAD_STATUS load_status
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.