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 <env_vars.h>
24#include <footprint_info_impl.h>
25#include <thread_pool.h>
26#include <trace_helpers.h>
27#include <footprint.h>
28
29#include <magic_enum.hpp>
30#include <wx/hash.h>
31#include <wx/log.h>
32
33
34std::map<wxString, LIB_DATA> FOOTPRINT_LIBRARY_ADAPTER::GlobalLibraries;
35
37
38
43
44
46{
47 return ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) );
48}
49
50
52{
53 // TODO(JE) this shares most code with the symbol adapter
54 // TODO(JE) any reason to clean these up earlier?
55 std::erase_if( m_futures,
56 []( const std::future<void>& aFuture ) { return aFuture.valid(); } );
57
58 if( !m_futures.empty() )
59 {
60 wxLogTrace( traceLibraries, "FP: Cannot AsyncLoad, futures from a previous call remain!" );
61 return;
62 }
63
64 std::vector<LIBRARY_TABLE_ROW*> rows = m_manager.Rows( LIBRARY_TABLE_TYPE::FOOTPRINT );
65
66 m_loadTotal = rows.size();
67 m_loadCount.store( 0 );
68
69 if( m_loadTotal == 0 )
70 {
71 wxLogTrace( traceLibraries, "FP: AsyncLoad: no libraries left to load; exiting" );
72 return;
73 }
74
76
77 auto check =
78 []( const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::mutex& aMutex )
79 {
80 std::lock_guard lock( aMutex );
81
82 if( aMap.contains( aLib ) )
83 {
84 if( aMap[aLib].status.load_status == LOAD_STATUS::LOADED )
85 return true;
86
87 aMap.erase( aLib );
88 }
89
90 return false;
91 };
92
93 for( const LIBRARY_TABLE_ROW* row : rows )
94 {
95 wxString nickname = row->Nickname();
96 LIBRARY_TABLE_SCOPE scope = row->Scope();
97
98 // TODO(JE) library tables -- check for modified global files
99 if( check( nickname, m_libraries, m_libraries_mutex ) )
100 {
101 --m_loadTotal;
102 continue;
103 }
104
105 if( check( nickname, GlobalLibraries, GlobalLibraryMutex ) )
106 {
107 --m_loadTotal;
108 continue;
109 }
110
111 m_futures.emplace_back( tp.submit_task(
112 [this, nickname, scope]()
113 {
114 if( m_abort.load() )
115 return;
116
117 LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( nickname );
118
119 if( result.has_value() )
120 {
121 LIB_DATA* lib = *result;
122 wxArrayString dummyList;
123 std::lock_guard lock ( lib->mutex );
124 lib->status.load_status = LOAD_STATUS::LOADING;
125
126 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
127
128 try
129 {
130 pcbplugin( lib )->FootprintEnumerate( dummyList, getUri( lib->row ), false, &options );
131 wxLogTrace( traceLibraries, "FP: %s: library enumerated %zu items", nickname, dummyList.size() );
132 lib->status.load_status = LOAD_STATUS::LOADED;
133 }
134 catch( IO_ERROR& e )
135 {
136 lib->status.load_status = LOAD_STATUS::LOAD_ERROR;
137 lib->status.error = LIBRARY_ERROR( { e.What() } );
138 wxLogTrace( traceLibraries, "FP: %s: plugin threw exception: %s", nickname, e.What() );
139 }
140 }
141 else
142 {
143 switch( scope )
144 {
145 case LIBRARY_TABLE_SCOPE::GLOBAL:
146 {
147 std::lock_guard lock( GlobalLibraryMutex );
148
149 GlobalLibraries[nickname].status = LIB_STATUS( {
150 .load_status = LOAD_STATUS::LOAD_ERROR,
151 .error = result.error()
152 } );
153
154 break;
155 }
156
157 case LIBRARY_TABLE_SCOPE::PROJECT:
158 {
159 wxLogTrace( traceLibraries, "FP: project library error: %s: %s", nickname, result.error().message );
160 std::lock_guard lock( m_libraries_mutex );
161
162 m_libraries[nickname].status = LIB_STATUS( {
163 .load_status = LOAD_STATUS::LOAD_ERROR,
164 .error = result.error()
165 } );
166
167 break;
168 }
169
170 default:
171 wxFAIL_MSG( "Unexpected library table scope" );
172 }
173 }
174
175 ++m_loadCount;
176 }, BS::pr::lowest ) );
177 }
178
179 wxLogTrace( traceLibraries, "FP: Started async load of %zu libraries", m_loadTotal );
180}
181
182
184std::optional<LIB_STATUS> FOOTPRINT_LIBRARY_ADAPTER::LoadOne( LIB_DATA* aLib )
185{
186 std::lock_guard lock ( aLib->mutex );
188
189 std::map<std::string, UTF8> options = aLib->row->GetOptionsMap();
190
191 try
192 {
193 wxArrayString dummyList;
194 pcbplugin( aLib )->FootprintEnumerate( dummyList, getUri( aLib->row ), false, &options );
195 wxLogTrace( traceLibraries, "Sym: %s: library enumerated %zu items", aLib->row->Nickname(), dummyList.size() );
197 }
198 catch( IO_ERROR& e )
199 {
201 aLib->status.error = LIBRARY_ERROR( { e.What() } );
202 wxLogTrace( traceLibraries, "Sym: %s: plugin threw exception: %s", aLib->row->Nickname(), e.What() );
203 }
204
205 return aLib->status;
206}
207
208
209std::optional<LIB_STATUS> FOOTPRINT_LIBRARY_ADAPTER::LoadOne( const wxString& nickname )
210{
212
213 if( result.has_value() )
214 return LoadOne( *result );
215
216 return LIB_STATUS{
217 .load_status = LOAD_STATUS::LOAD_ERROR,
218 .error = LIBRARY_ERROR( { result.error() } )
219 };
220}
221
222
223std::optional<LIB_STATUS> FOOTPRINT_LIBRARY_ADAPTER::GetLibraryStatus( const wxString& aNickname ) const
224{
225 // TODO(JE) this could be deduplicated with virtual access to GlobalLibraries
226 if( m_libraries.contains( aNickname ) )
227 return m_libraries.at( aNickname ).status;
228
229 if( GlobalLibraries.contains( aNickname ) )
230 return GlobalLibraries.at( aNickname ).status;
231
232 return std::nullopt;
233}
234
235
236std::vector<FOOTPRINT*> FOOTPRINT_LIBRARY_ADAPTER::GetFootprints( const wxString& aNickname, bool aBestEfforts )
237{
238 std::vector<FOOTPRINT*> footprints;
239
240 std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname );
241
242 if( !maybeLib )
243 return footprints;
244
245 const LIB_DATA* lib = *maybeLib;
246 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
247
248 wxArrayString namesAS;
249
250 try
251 {
252 pcbplugin( lib )->FootprintEnumerate( namesAS, getUri( lib->row ), true, &options );
253 }
254 catch( IO_ERROR& e )
255 {
256 wxLogTrace( traceLibraries, "FP: Exception enumerating library %s: %s",
257 lib->row->Nickname(), e.What() );
258 }
259
260 for( const wxString& footprintName : namesAS )
261 {
262 FOOTPRINT* footprint = nullptr;
263
264 try
265 {
266 footprint = pcbplugin( lib )->FootprintLoad( getUri( lib->row ),footprintName, false, &options );
267 }
268 catch( IO_ERROR& e )
269 {
270 wxLogTrace( traceLibraries, "Sym: Exception enumerating library %s: %s",
271 lib->row->Nickname(), e.What() );
272 continue;
273 }
274
275 wxCHECK2( footprint, continue );
276
277 LIB_ID id = footprint->GetFPID();
278 id.SetLibNickname( lib->row->Nickname() );
279 footprint->SetFPID( id );
280 footprints.emplace_back( footprint );
281 }
282
283 return footprints;
284}
285
286
287std::vector<wxString> FOOTPRINT_LIBRARY_ADAPTER::GetFootprintNames( const wxString& aNickname, bool aBestEfforts )
288{
289 // TODO(JE) can we kill wxArrayString in internal API?
290 wxArrayString namesAS;
291 std::vector<wxString> names;
292
293 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
294 {
295 const LIB_DATA* lib = *maybeLib;
296 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
297
298 try
299 {
300 pcbplugin( lib )->FootprintEnumerate( namesAS, getUri( lib->row ), true, &options );
301 }
302 catch( IO_ERROR& e )
303 {
304 wxLogTrace( traceLibraries, "FP: Exception enumerating library %s: %s",
305 lib->row->Nickname(), e.What() );
306 }
307 }
308
309 for( const wxString& name : namesAS )
310 names.emplace_back( name );
311
312 return names;
313}
314
315
316long long FOOTPRINT_LIBRARY_ADAPTER::GenerateTimestamp( const wxString* aNickname )
317{
318 long long hash = 0;
319
320 if( aNickname )
321 {
322 wxCHECK( HasLibrary( *aNickname, true ), hash );
323
324 if( std::optional<const LIB_DATA*> r = fetchIfLoaded( *aNickname ); r.has_value() )
325 {
326 PCB_IO* plugin = dynamic_cast<PCB_IO*>( ( *r )->plugin.get() );
327 wxCHECK( plugin, hash );
328 return plugin->GetLibraryTimestamp( LIBRARY_MANAGER::GetFullURI( ( *r )->row, true ) ) +
329 wxHashTable::MakeKey( *aNickname );
330 }
331 }
332
333 for( const wxString& nickname : GetLibraryNames() )
334 {
335 if( std::optional<const LIB_DATA*> r = fetchIfLoaded( nickname ); r.has_value() )
336 {
337 // Note: can't use dynamic_cast across compile units on Mac
338 wxCHECK2( ( *r )->plugin->IsPCB_IO(), continue );
339 PCB_IO* plugin = static_cast<PCB_IO*>( ( *r )->plugin.get() );
340 hash += plugin->GetLibraryTimestamp( LIBRARY_MANAGER::GetFullURI( ( *r )->row, true ) ) +
341 wxHashTable::MakeKey( nickname );
342 }
343 }
344
345 return hash;
346}
347
348
349bool FOOTPRINT_LIBRARY_ADAPTER::FootprintExists( const wxString& aNickname, const wxString& aName )
350{
351 if( std::optional<const LIB_DATA*> maybeLib = fetchIfLoaded( aNickname ) )
352 {
353 const LIB_DATA* lib = *maybeLib;
354 std::map<std::string, UTF8> options = lib->row->GetOptionsMap();
355
356 return pcbplugin( lib )->FootprintExists( getUri( lib->row ), aName, &options );
357 }
358
359 return false;
360}
361
362
363FOOTPRINT* FOOTPRINT_LIBRARY_ADAPTER::LoadFootprint( const wxString& aNickname, const wxString& aName, bool aKeepUUID )
364{
365 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
366 {
367 if( FOOTPRINT* footprint = pcbplugin( *lib )->FootprintLoad( getUri( ( *lib )->row ), aName ) )
368 {
369 LIB_ID id = footprint->GetFPID();
370 id.SetLibNickname( ( *lib )->row->Nickname() );
371 footprint->SetFPID( id );
372 return footprint;
373 }
374 }
375 else
376 {
377 wxLogTrace( traceLibraries, "LoadFootprint: requested library %s not loaded", aNickname );
378 }
379
380 return nullptr;
381}
382
383
385{
386 wxString nickname = aFootprintId.GetLibNickname();
387 wxString footprintName = aFootprintId.GetLibItemName();
388
389 if( nickname.size() )
390 return LoadFootprint( nickname, footprintName, aKeepUUID );
391
392 // nickname is empty, sequentially search (alphabetically) all libs/nicks for first match:
393 for( const wxString& library : GetLibraryNames() )
394 {
395 // FootprintLoad() returns NULL on not found, does not throw exception
396 // unless there's an IO_ERROR.
397 if( FOOTPRINT* ret = LoadFootprint( library, footprintName, aKeepUUID ) )
398 return ret;
399 }
400
401 return nullptr;
402}
403
404
406 const FOOTPRINT* aFootprint,
407 bool aOverwrite )
408{
409 wxCHECK( aFootprint, SAVE_SKIPPED );
410
411 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
412 {
413 if( !aOverwrite )
414 {
415 wxString fpname = aFootprint->GetFPID().GetLibItemName();
416
417 try
418 {
419 FOOTPRINT* existing = pcbplugin( *lib )->FootprintLoad( getUri( ( *lib )->row ), fpname, false );
420
421 if( existing )
422 return SAVE_SKIPPED;
423 }
424 catch( IO_ERROR& e )
425 {
426 wxLogTrace( traceLibraries, "SaveFootprint: error checking for existing footprint %s: %s",
427 aFootprint->GetFPIDAsString(), e.What() );
428 return SAVE_SKIPPED;
429 }
430 }
431
432 try
433 {
434 pcbplugin( *lib )->FootprintSave( getUri( ( *lib )->row ), aFootprint );
435 }
436 catch( IO_ERROR& e )
437 {
438 wxLogTrace( traceLibraries, "SaveFootprint: error saving %s: %s",
439 aFootprint->GetFPIDAsString(), e.What() );
440 return SAVE_SKIPPED;
441 }
442
443 return SAVE_OK;
444 }
445 else
446 {
447 wxLogTrace( traceLibraries, "SaveFootprint: requested library %s not loaded", aNickname );
448 return SAVE_SKIPPED;
449 }
450}
451
452void FOOTPRINT_LIBRARY_ADAPTER::DeleteFootprint( const wxString& aNickname, const wxString& aFootprintName )
453{
454 if( std::optional<const LIB_DATA*> lib = fetchIfLoaded( aNickname ) )
455 {
456 try
457 {
458 pcbplugin( *lib )->FootprintDelete( getUri( ( *lib )->row ), aFootprintName );
459 }
460 catch( IO_ERROR& e )
461 {
462 wxLogTrace( traceLibraries, "DeleteFootprint: error deleting %s:%s: %s", aNickname,
463 aFootprintName, e.What() );
464 }
465 }
466 else
467 {
468 wxLogTrace( traceLibraries, "DeleteFootprint: requested library %s not loaded", aNickname );
469 }
470}
471
472
474{
475 if( m_libraries.contains( aLib ) )
476 return m_libraries[aLib].plugin->IsLibraryWritable( getUri( m_libraries[aLib].row ) );
477
478 if( GlobalLibraries.contains( aLib ) )
479 return GlobalLibraries[aLib].plugin->IsLibraryWritable( getUri( GlobalLibraries[aLib].row ) );
480
481 return false;
482}
483
484
486{
488
489 if( type == PCB_IO_MGR::PCB_FILE_UNKNOWN )
490 {
491 wxLogTrace( traceLibraries, "FP: Plugin type %s is unknown!", row->Type() );
492 wxString msg = wxString::Format( _( "Unknown library type %s " ), row->Type() );
493 return tl::unexpected( LIBRARY_ERROR( msg ) );
494 }
495
497 wxCHECK( plugin, tl::unexpected( LIBRARY_ERROR( _( "Internal error" ) ) ) );
498
499 wxLogTrace( traceLibraries, "FP: Library %s (%s) plugin created",
500 row->Nickname(), magic_enum::enum_name( row->Scope() ) );
501
502 return plugin;
503}
504
505
507{
508 // Note: can't use dynamic_cast across compile units on Mac
509 wxCHECK( aRow->plugin->IsPCB_IO(), nullptr );
510 PCB_IO* ret = static_cast<PCB_IO*>( aRow->plugin.get() );
511 return ret;
512}
const char * name
void AsyncLoad() override
Loads all available libraries for this adapter type in the background.
bool IsFootprintLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
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.
std::optional< LIB_STATUS > GetLibraryStatus(const wxString &aNickname) const override
Returns the status of a loaded library, or nullopt if the library hasn't been loaded (yet)
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 PCB_IO * pcbplugin(const LIB_DATA *aRow)
Helper to cast the ABC plugin in the LIB_DATA* to a concrete plugin.
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.
static std::map< wxString, LIB_DATA > GlobalLibraries
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.
bool FootprintExists(const wxString &aNickname, const wxString &aName)
void SetFPID(const LIB_ID &aFPID)
Definition footprint.h:270
wxString GetFPIDAsString() const
Definition footprint.h:275
const LIB_ID & GetFPID() const
Definition footprint.h:269
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()
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.
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)
std::vector< std::future< void > > m_futures
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
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 & 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
#define _(s)
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
LIBRARY_TABLE_SCOPE
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
std::mutex mutex
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.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31
wxLogTrace helper definitions.