KiCad PCB EDA Suite
Loading...
Searching...
No Matches
library_manager.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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <chrono>
22#include <common.h>
23#include <env_vars.h>
24#include <list>
25#include <magic_enum.hpp>
26#include <thread_pool.h>
27#include <ranges>
28#include <set>
29#include <thread>
30#include <unordered_set>
31
32#include <paths.h>
33#include <pgm_base.h>
34#include <progress_reporter.h>
35#include <richio.h>
36#include <string_utils.h>
37#include <trace_helpers.h>
39
41
42using namespace std::chrono_literals;
46#include <wx/dir.h>
47#include <wx/log.h>
48
50{
51 std::vector<LIBRARY_TABLE> tables;
52};
53
54
55std::recursive_mutex& LIBRARY_MANAGER_ADAPTER::pluginMutex( const wxString& aNickname )
56{
57 // Leaked and never erased: must outlive every caller including static teardown, and stable node
58 // addresses keep the returned reference valid.
59 static std::mutex& registryLock = *new std::mutex;
60 static std::map<wxString, std::unique_ptr<std::recursive_mutex>>& registry =
61 *new std::map<wxString, std::unique_ptr<std::recursive_mutex>>;
62
63 std::lock_guard guard( registryLock );
64 std::unique_ptr<std::recursive_mutex>& slot = registry[aNickname];
65
66 if( !slot )
67 slot = std::make_unique<std::recursive_mutex>();
68
69 return *slot;
70}
71
72
73LIBRARY_MANAGER::LIBRARY_MANAGER( const PROJECT* aProject ) : m_project( aProject )
74{
75}
76
77
79{
80 // Adapters join plugin worker threads that still query the row cache, so they must die before it
81 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_MANAGER_ADAPTER>> adapters;
82
83 {
84 std::scoped_lock lock( m_adaptersMutex );
85 adapters.swap( m_adapters );
86 }
87}
88
89
91{
93}
94
95
96void LIBRARY_MANAGER::loadTables( const wxString& aTablePath, LIBRARY_TABLE_SCOPE aScope,
97 std::vector<LIBRARY_TABLE_TYPE> aTablesToLoad )
98{
99 {
100 std::lock_guard lock( m_rowCacheMutex );
101 m_rowCache.clear();
102 }
103
104 auto getTarget =
105 [&]() -> std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>&
106 {
107 switch( aScope )
108 {
110 return m_tables;
111
113 return m_projectTables;
114
115 default:
116 wxCHECK_MSG( false, m_tables, "Invalid scope passed to loadTables" );
117 }
118 };
119
120 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>& aTarget = getTarget();
121
122 if( aTablesToLoad.size() == 0 )
124
125 for( LIBRARY_TABLE_TYPE type : aTablesToLoad )
126 {
127 aTarget.erase( type );
128
129 wxFileName fn( aTablePath, tableFileName( type ) );
130
131 if( fn.IsFileReadable() )
132 {
133 std::unique_ptr<LIBRARY_TABLE> table = std::make_unique<LIBRARY_TABLE>( fn, aScope );
134
135 if( table->Type() != type )
136 {
137 auto actualName = magic_enum::enum_name( table->Type() );
138 auto expectedName = magic_enum::enum_name( type );
139 wxLogWarning( wxS( "Library table '%s' has type %s but expected %s; skipping" ),
140 fn.GetFullPath(),
141 wxString( actualName.data(), actualName.size() ),
142 wxString( expectedName.data(), expectedName.size() ) );
143 continue;
144 }
145
146 aTarget[type] = std::move( table );
147 loadNestedTables( *aTarget[type] );
148 }
149 else
150 {
151 wxLogTrace( traceLibraries, "No library table found at %s", fn.GetFullPath() );
152 }
153 }
154}
155
156
158{
159 std::unordered_set<wxString> seenTables;
160
161 std::function<void( LIBRARY_TABLE& )> processOneTable =
162 [&]( LIBRARY_TABLE& aTable )
163 {
164 seenTables.insert( aTable.Path() );
165
166 if( !aTable.IsOk() )
167 return;
168
169 for( LIBRARY_TABLE_ROW& row : aTable.Rows() )
170 {
171 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
172 {
173 wxFileName file( ExpandEnvVarSubstitutions( row.URI(), &Project() ) );
174
175 // URI may be relative to parent
176 file.MakeAbsolute( wxFileName( aTable.Path() ).GetPath() );
177
179 wxString src = file.GetFullPath();
180
181 if( seenTables.contains( src ) )
182 {
183 wxLogTrace( traceLibraries, "Library table %s has already been loaded!", src );
184 row.SetOk( false );
185 row.SetErrorDescription( _( "A reference to this library table already exists" ) );
186 continue;
187 }
188
189 auto child = std::make_unique<LIBRARY_TABLE>( file, aRootTable.Scope() );
190
191 processOneTable( *child );
192
193 if( !child->IsOk() )
194 {
195 row.SetOk( false );
196 row.SetErrorDescription( child->ErrorDescription() );
197 }
198
199 applyLibOverrides( *child );
200
201 m_childTables.insert_or_assign( row.URI(), std::move( child ) );
202 }
203 }
204 };
205
206 processOneTable( aRootTable );
207}
208
209
211{
212 if( !aTable.IsReadOnly() )
213 return;
214
216 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
217
218 if( !settings )
219 return;
220
221 wxString tablePath = aTable.Path();
222
223 auto it = settings->m_LibOverrides.find( tablePath );
224
225 if( it == settings->m_LibOverrides.end() )
226 return;
227
228 const std::map<wxString, LIB_OVERRIDE>& overrides = it->second;
229
230 for( LIBRARY_TABLE_ROW& row : aTable.Rows() )
231 {
232 auto overIt = overrides.find( row.Nickname() );
233
234 if( overIt != overrides.end() )
235 {
236 row.SetDisabled( overIt->second.disabled );
237 row.SetHidden( overIt->second.hidden );
238 }
239 }
240}
241
242
247
248
249void LIBRARY_MANAGER::SetLibOverride( const wxString& aTablePath, const wxString& aNickname,
250 bool aDisabled, bool aHidden )
251{
253 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
254
255 wxCHECK( settings, /* void */ );
256
257 if( !aDisabled && !aHidden )
258 {
259 ClearLibOverride( aTablePath, aNickname );
260 return;
261 }
262
263 settings->m_LibOverrides[aTablePath][aNickname] = { aDisabled, aHidden };
264}
265
266
267void LIBRARY_MANAGER::ClearLibOverride( const wxString& aTablePath, const wxString& aNickname )
268{
270 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
271
272 wxCHECK( settings, /* void */ );
273
274 auto tableIt = settings->m_LibOverrides.find( aTablePath );
275
276 if( tableIt == settings->m_LibOverrides.end() )
277 return;
278
279 tableIt->second.erase( aNickname );
280
281 if( tableIt->second.empty() )
282 settings->m_LibOverrides.erase( tableIt );
283}
284
285
287{
288 switch( aType )
289 {
293 default: wxCHECK( false, wxEmptyString );
294 }
295}
296
297
299{
300 if( aScope == LIBRARY_TABLE_SCOPE::GLOBAL )
301 {
302 wxCHECK( !m_tables.contains( aType ), /* void */ );
303 wxFileName fn( PATHS::GetUserSettingsPath(), tableFileName( aType ) );
304
305 m_tables[aType] = std::make_unique<LIBRARY_TABLE>( fn, LIBRARY_TABLE_SCOPE::GLOBAL );
306 m_tables[aType]->SetType( aType );
307 }
308 else if( aScope == LIBRARY_TABLE_SCOPE::PROJECT )
309 {
310 wxCHECK( !m_projectTables.contains( aType ), /* void */ );
311 wxFileName fn( Project().GetProjectDirectory(), tableFileName( aType ) );
312
313 m_projectTables[aType] = std::make_unique<LIBRARY_TABLE>( fn, LIBRARY_TABLE_SCOPE::PROJECT );
314 m_projectTables[aType]->SetType( aType );
315 m_projectTables[aType]->SetOk();
316 }
317}
318
319
320class PCM_LIB_TRAVERSER final : public wxDirTraverser
321{
322public:
323 explicit PCM_LIB_TRAVERSER( const wxString& aBasePath, LIBRARY_MANAGER& aManager,
324 const wxString& aPrefix ) :
325 m_manager( aManager ),
326 m_project( Pgm().GetSettingsManager().Prj() ),
327 m_path_prefix( aBasePath ),
328 m_lib_prefix( aPrefix )
329 {
330 wxFileName f( aBasePath, "" );
331 m_prefix_dir_count = f.GetDirCount();
332
336 .value_or( nullptr );
337 }
338
340 wxDirTraverseResult OnFile( const wxString& aFilePath ) override
341 {
342 wxFileName file = wxFileName::FileName( aFilePath );
343
344 // consider a file to be a lib if it's name ends with .kicad_sym and
345 // it is under $KICADn_3RD_PARTY/symbols/<pkgid>/ i.e. has nested level of at least +2
346 if( file.GetExt() == wxT( "kicad_sym" )
347 && file.GetDirCount() >= m_prefix_dir_count + 2
348 && file.GetDirs()[m_prefix_dir_count] == wxT( "symbols" ) )
349 {
351 }
352
353 return wxDIR_CONTINUE;
354 }
355
357 wxDirTraverseResult OnDir( const wxString& dirPath ) override
358 {
359 static wxString designBlockExt = wxString::Format( wxS( ".%s" ), FILEEXT::KiCadDesignBlockLibPathExtension );
360 wxFileName dir = wxFileName::DirName( dirPath );
361
362 // consider a directory to be a lib if it's name ends with .pretty and
363 // it is under $KICADn_3RD_PARTY/footprints/<pkgid>/ i.e. has nested level of at least +3
364 if( dirPath.EndsWith( wxS( ".pretty" ) )
365 && dir.GetDirCount() >= m_prefix_dir_count + 3
366 && dir.GetDirs()[m_prefix_dir_count] == wxT( "footprints" ) )
367 {
369 }
370 else if( dirPath.EndsWith( designBlockExt )
371 && dir.GetDirCount() >= m_prefix_dir_count + 3
372 && dir.GetDirs()[m_prefix_dir_count] == wxT( "design_blocks" ) )
373 {
374 addRowIfNecessary( m_designBlockTable, dir, ADD_MODE::AM_DIRECTORY, designBlockExt.Len() );
375 }
376
377 return wxDIR_CONTINUE;
378 }
379
380 std::set<LIBRARY_TABLE*> Modified() const { return m_modified; }
381
382private:
383 void ensureUnique( LIBRARY_TABLE* aTable, const wxString& aBaseName, wxString& aNickname ) const
384 {
385 if( aTable->HasRow( aNickname ) )
386 {
387 int increment = 1;
388
389 do
390 {
391 aNickname = wxString::Format( "%s%s_%d", m_lib_prefix, aBaseName, increment );
392 increment++;
393 } while( aTable->HasRow( aNickname ) );
394 }
395 }
396
397 enum class ADD_MODE
398 {
401 };
402
403 void addRowIfNecessary( LIBRARY_TABLE* aTable, const wxFileName& aSource, ADD_MODE aMode,
404 int aExtensionLength )
405 {
406 // No global table file yet (first run): nothing to auto-add.
407 if( !aTable )
408 return;
409
410 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
411 ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ) );
412
413 wxArrayString parts = aSource.GetDirs();
414 parts.RemoveAt( 0, m_prefix_dir_count );
415 parts.Insert( versionedPath, 0 );
416
417 if( aMode == ADD_MODE::AM_FILE )
418 parts.Add( aSource.GetFullName() );
419
420 wxString libPath = wxJoin( parts, '/' );
421
422 if( !aTable->HasRowWithURI( libPath, m_project ) )
423 {
424 wxString name = parts.Last().substr( 0, parts.Last().length() - aExtensionLength );
425 wxString nickname = wxString::Format( "%s%s", m_lib_prefix, name );
426
427 ensureUnique( aTable, name, nickname );
428
429 wxLogTrace( traceLibraries, "Manager: Adding PCM lib '%s' as '%s'", libPath, nickname );
430
431 LIBRARY_TABLE_ROW& row = aTable->InsertRow();
432
433 row.SetNickname( nickname );
434 row.SetURI( libPath );
435 row.SetType( wxT( "KiCad" ) );
436 row.SetDescription( _( "Added by Plugin and Content Manager" ) );
437 m_modified.insert( aTable );
438 }
439 else
440 {
441 wxLogTrace( traceLibraries, "Manager: Not adding existing PCM lib '%s'", libPath );
442 }
443 }
444
445private:
449 wxString m_lib_prefix;
451 std::set<LIBRARY_TABLE*> m_modified;
452
456};
457
458
460{
461 wxString basePath = PATHS::GetUserSettingsPath();
462
463 wxFileName fn( basePath, tableFileName( aType ) );
464 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
465
466 return fn.GetFullPath();
467}
468
469
471{
472 wxString basePath = PATHS::GetStockTemplatesPath();
473
474 wxFileName fn( basePath, tableFileName( aType ) );
475 fn.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
476
477 return fn.GetFullPath();
478}
479
480
482{
483 wxString templateDirVar = ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) );
484
485 return wxString::Format( wxS( "${%s}/%s" ), templateDirVar, tableFileName( aType ) );
486}
487
488
490{
491 const wxString templateDirVar = ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) );
492
493 // Only relocatable installs (AppImage, Nix) export the template-dir variable so the stock
494 // path follows the remounted prefix. There we store the unresolved token, which re-resolves
495 // each launch instead of dangling. A standard install leaves the variable at its built-in
496 // default, so we keep the historical resolved absolute path.
497 if( COMMON_SETTINGS* common = Pgm().GetCommonSettings() )
498 {
499 auto it = common->m_Env.vars.find( templateDirVar );
500
501 if( it != common->m_Env.vars.end() && it->second.GetDefinedExternally() )
502 return StockTableTokenizedURI( aType );
503 }
504
505 return StockTablePath( aType );
506}
507
508
509bool LIBRARY_MANAGER::IsTableValid( const wxString& aPath )
510{
511 if( wxFileName fn( aPath ); fn.IsFileReadable() )
512 {
514
515 if( temp.IsOk() )
516 return true;
517 }
518
519 return false;
520}
521
522
524{
525 return InvalidGlobalTables().empty();
526}
527
528
529std::vector<LIBRARY_TABLE_TYPE> LIBRARY_MANAGER::InvalidGlobalTables()
530{
531 std::vector<LIBRARY_TABLE_TYPE> invalidTables;
532 wxString basePath = PATHS::GetUserSettingsPath();
533
537 {
538 wxFileName fn( basePath, tableFileName( tableType ) );
539
540 if( !IsTableValid( fn.GetFullPath() ) )
541 invalidTables.emplace_back( tableType );
542 }
543
544 return invalidTables;
545}
546
547
548bool LIBRARY_MANAGER::CreateGlobalTable( LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries )
549{
550 wxFileName fn( DefaultGlobalTablePath( aType ) );
551
553 table.SetType( aType );
554 table.Rows().clear();
555
556 wxFileName defaultLib( StockTablePath( aType ) );
557
558 if( aPopulateDefaultLibraries && defaultLib.IsFileReadable() )
559 {
560 LIBRARY_TABLE_ROW& chained = table.InsertRow();
562 chained.SetNickname( wxT( "KiCad" ) );
563 chained.SetDescription( _( "KiCad Default Libraries" ) );
564 chained.SetURI( StockTableReferenceURI( aType ) );
565 }
566 else if( aPopulateDefaultLibraries )
567 {
568 auto typeName = magic_enum::enum_name( aType );
569 wxLogTrace( traceLibraries, "No stock library table for %s at '%s'; creating empty global table",
570 wxString( typeName.data(), typeName.size() ), defaultLib.GetFullPath() );
571 }
572
573 try
574 {
575 PRETTIFIED_FILE_OUTPUTFORMATTER formatter( fn.GetFullPath(), KICAD_FORMAT::FORMAT_MODE::LIBRARY_TABLE );
576 table.Format( &formatter );
577 formatter.Finish();
578 }
579 catch( IO_ERROR& e )
580 {
581 wxLogTrace( traceLibraries, "Exception while saving: %s", e.What() );
582 return false;
583 }
584
585 return true;
586}
587
588
589void LIBRARY_MANAGER::LoadGlobalTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
590{
591 // Cancel any in-progress load
592 {
593 std::scoped_lock lock( m_adaptersMutex );
594
595 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
596 adapter->GlobalTablesChanged( aTablesToLoad );
597 }
598
600
601 // A passive load must not maintain or save the active user's library tables.
602 if( IsProjectScoped() )
603 return;
604
606 KICAD_SETTINGS* settings = mgr.GetAppSettings<KICAD_SETTINGS>( "kicad" );
607
608 wxCHECK( settings, /* void */ );
609
610 const ENV_VAR_MAP& vars = Pgm().GetLocalEnvVariables();
611 std::optional<wxString> packagesPath = ENV_VAR::GetVersionedEnvVarValue( vars, wxT( "3RD_PARTY" ) );
612
613 if( packagesPath && settings->m_PcmLibAutoAdd )
614 {
615 // Scan for libraries in PCM packages directory
616 wxFileName d( *packagesPath, "" );
617
618 if( d.DirExists() )
619 {
620 PCM_LIB_TRAVERSER traverser( *packagesPath, *this, settings->m_PcmLibPrefix );
621 wxDir dir( d.GetPath() );
622
623 dir.Traverse( traverser );
624
625 for( LIBRARY_TABLE* table : traverser.Modified() )
626 {
627 table->Save().map_error(
628 []( const LIBRARY_ERROR& aError )
629 {
630 wxLogTrace( traceLibraries, wxT( "Warning: save failed after PCM auto-add: %s" ),
631 aError.message );
632 } );
633 }
634 }
635 }
636
637 auto cleanupRemovedPCMLibraries =
638 [&]( LIBRARY_TABLE_TYPE aType )
639 {
640 LIBRARY_TABLE* table = Table( aType, LIBRARY_TABLE_SCOPE::GLOBAL ).value_or( nullptr );
641 // No global table file yet (first run): nothing to scan for PCM removals.
642 if( !table )
643 return;
644
645 auto toErase = std::ranges::remove_if( table->Rows(),
646 [&]( const LIBRARY_TABLE_ROW& aRow )
647 {
648 if( !IsPcmManagedRow( aRow ) )
649 return false;
650
651 wxString path = GetFullURI( &aRow, true );
652 return !wxFileName::Exists( path );
653 } );
654
655 bool hadRemovals = !toErase.empty();
656 table->Rows().erase( toErase.begin(), toErase.end() );
657
658 if( hadRemovals )
659 {
660 table->Save().map_error(
661 []( const LIBRARY_ERROR& aError )
662 {
663 wxLogTrace( traceLibraries, wxT( "Warning: save failed after PCM auto-remove: %s" ),
664 aError.message );
665 } );
666 }
667 };
668
669 if( packagesPath && settings->m_PcmLibAutoRemove )
670 {
671 cleanupRemovedPCMLibraries( LIBRARY_TABLE_TYPE::SYMBOL );
672 cleanupRemovedPCMLibraries( LIBRARY_TABLE_TYPE::FOOTPRINT );
673 cleanupRemovedPCMLibraries( LIBRARY_TABLE_TYPE::DESIGN_BLOCK );
674 }
675}
676
677
678void LIBRARY_MANAGER::LoadProjectTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
679{
680 LoadProjectTables( Project().GetProjectDirectory(), aTablesToLoad );
681}
682
683
685{
686 // Abort any running async library loads before reloading project tables.
687 // Background workers hold raw LIBRARY_TABLE_ROW pointers that become dangling
688 // when loadTables() destroys and replaces the table objects.
690
691 LoadProjectTables( Project().GetProjectDirectory() );
692
693 std::scoped_lock lock( m_adaptersMutex );
694
695 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
696 adapter->ProjectChanged();
697}
698
699
700void LIBRARY_MANAGER::AbortAsyncLoads( bool aCancelPlugins )
701{
702 std::scoped_lock lock( m_adaptersMutex );
703
704 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
705 adapter->AbortAsyncLoad( aCancelPlugins );
706}
707
708
710{
711 std::scoped_lock lock( m_adaptersMutex );
712
713 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
714 adapter->RestartAsyncLoad();
715}
716
717
719{
720 std::scoped_lock lock( m_adaptersMutex );
721
722 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
723 adapter->LeakCachesAtExit();
724}
725
726
728 std::unique_ptr<LIBRARY_MANAGER_ADAPTER>&& aAdapter )
729{
730 std::scoped_lock lock( m_adaptersMutex );
731
732 wxCHECK_MSG( !m_adapters.contains( aType ), /**/, "You should only register an adapter once!" );
733
734 m_adapters[aType] = std::move( aAdapter );
735}
736
737
739{
740 std::unique_ptr<LIBRARY_MANAGER_ADAPTER> retired;
741 std::scoped_lock lock( m_adaptersMutex );
742 if( !m_adapters.contains( aType ) )
743 return false;
744
745 if( m_adapters[aType].get() != aAdapter )
746 return false;
747
748 // The adapter joins plugin worker threads, so it must not be destroyed under the adapters mutex
749 retired = std::move( m_adapters[aType] );
750 m_adapters.erase( aType );
751 return true;
752}
753
754
755std::optional<LIBRARY_MANAGER_ADAPTER*> LIBRARY_MANAGER::Adapter( LIBRARY_TABLE_TYPE aType ) const
756{
757 std::scoped_lock lock( m_adaptersMutex );
758
759 if( m_adapters.contains( aType ) )
760 return m_adapters.at( aType ).get();
761
762 return std::nullopt;
763}
764
765
766std::optional<LIBRARY_TABLE*> LIBRARY_MANAGER::Table( LIBRARY_TABLE_TYPE aType,
767 LIBRARY_TABLE_SCOPE aScope )
768{
769 switch( aScope )
770 {
773 wxCHECK_MSG( false, std::nullopt, "Table() requires a single scope" );
774
776 {
777 if( !m_tables.contains( aType ) )
778 {
779 wxLogTrace( traceLibraries, "WARNING: missing global table (%s)",
780 magic_enum::enum_name( aType ) );
781 return std::nullopt;
782 }
783
784 return m_tables.at( aType ).get();
785 }
786
788 {
789 if( !m_projectTables.contains( aType ) )
790 {
791 if( !Project().IsNullProject() )
793 else
794 return std::nullopt;
795 }
796
797 return m_projectTables.at( aType ).get();
798 }
799 }
800
801 return std::nullopt;
802}
803
804
805std::vector<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER::Rows( LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope,
806 bool aIncludeInvalid ) const
807{
808 std::map<wxString, LIBRARY_TABLE_ROW*> rows;
809 std::vector<wxString> rowOrder;
810
811 std::list<std::ranges::ref_view<const std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>>> tables;
812
813 switch( aScope )
814 {
816 tables = { std::views::all( m_tables ) };
817 break;
818
820 tables = { std::views::all( m_projectTables ) };
821 break;
822
824 tables = { std::views::all( m_tables ), std::views::all( m_projectTables ) };
825 break;
826
828 wxFAIL;
829 }
830
831 std::function<void(const std::unique_ptr<LIBRARY_TABLE>&, bool parentHidden)> processTable =
832 [&]( const std::unique_ptr<LIBRARY_TABLE>& aTable, const bool parentHidden )
833 {
834 if( aTable->Type() != aType )
835 return;
836
837 if( aTable->IsOk() || aIncludeInvalid )
838 {
839 for( LIBRARY_TABLE_ROW& row : aTable->Rows() )
840 {
841 if( row.IsOk() || aIncludeInvalid )
842 {
843 // Hide child row if parent is hidden
844 if( parentHidden )
845 row.SetHidden( true );
846
847 if( row.Type() == LIBRARY_TABLE_ROW::TABLE_TYPE_NAME )
848 {
849 if( !m_childTables.contains( row.URI() ) )
850 continue;
851
852 // Don't include libraries from disabled nested tables
853 if( row.Disabled() )
854 continue;
855
856 processTable( m_childTables.at( row.URI() ), row.Hidden() );
857 }
858 else
859 {
860 if( !rows.contains( row.Nickname() ) )
861 rowOrder.emplace_back( row.Nickname() );
862
863 rows[ row.Nickname() ] = &row;
864 }
865 }
866 }
867 }
868 };
869
870 for( const std::unique_ptr<LIBRARY_TABLE>& table :
871 std::views::join( tables ) | std::views::values )
872 {
873 processTable( table, false );
874 }
875
876 std::vector<LIBRARY_TABLE_ROW*> ret;
877
878 for( const wxString& row : rowOrder )
879 ret.emplace_back( rows[row] );
880
881 return ret;
882}
883
884
885std::optional<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER::GetRow( LIBRARY_TABLE_TYPE aType, const wxString& aNickname,
886 LIBRARY_TABLE_SCOPE aScope )
887{
888 {
889 std::lock_guard lock( m_rowCacheMutex );
890 auto key = std::make_tuple( aType, aScope, aNickname );
891
892 if( auto it = m_rowCache.find( key ); it != m_rowCache.end() )
893 return it->second;
894 }
895
896 for( LIBRARY_TABLE_ROW* row : Rows( aType, aScope, true ) )
897 {
898 if( row->Nickname() == aNickname )
899 {
900 std::lock_guard lock( m_rowCacheMutex );
901 m_rowCache[std::make_tuple( aType, aScope, aNickname )] = row;
902 return row;
903 }
904 }
905
906 return std::nullopt;
907}
908
909
910std::optional<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER::FindRowByURI( LIBRARY_TABLE_TYPE aType,
911 const wxString& aUri,
912 LIBRARY_TABLE_SCOPE aScope ) const
913{
914 for( LIBRARY_TABLE_ROW* row : Rows( aType, aScope, true ) )
915 {
916 if( UrisAreEquivalent( GetFullURI( row, true, &Project() ), aUri ) )
917 return row;
918 }
919
920 return std::nullopt;
921}
922
923
924void LIBRARY_MANAGER::ReloadLibraryEntry( LIBRARY_TABLE_TYPE aType, const wxString& aNickname,
925 LIBRARY_TABLE_SCOPE aScope )
926{
927 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter = Adapter( aType ); adapter )
928 ( *adapter )->ReloadLibraryEntry( aNickname, aScope );
929}
930
931
933 const wxString& aNickname )
934{
935 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter = Adapter( aType ); adapter )
936 return ( *adapter )->LoadLibraryEntry( aNickname );
937
938 return std::nullopt;
939}
940
941
942void LIBRARY_MANAGER::LoadProjectTables( const wxString& aProjectPath,
943 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
944{
945 // Cancel any in-progress loads and clear adapter caches before destroying project
946 // tables. Cached LIB_DATA entries hold raw LIBRARY_TABLE_ROW pointers into the old
947 // tables, which would dangle once loadTables() replaces them. Mirrors the safety
948 // ordering in LoadGlobalTables().
949 {
950 std::scoped_lock lock( m_adaptersMutex );
951
952 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
953 adapter->ProjectTablesChanged( aTablesToLoad );
954 }
955
956 if( wxFileName::IsDirReadable( aProjectPath ) )
957 {
958 loadTables( aProjectPath, LIBRARY_TABLE_SCOPE::PROJECT, aTablesToLoad );
959 }
960 else
961 {
962 // loadTables() would have cleared m_rowCache before rebuilding the new
963 // table; do the same here so cached entries don't point into the
964 // project tables we are about to destroy.
965 {
966 std::lock_guard lock( m_rowCacheMutex );
967 m_rowCache.clear();
968 }
969
970 if( aTablesToLoad.size() == 0 )
971 {
972 m_projectTables.clear();
973 }
974 else
975 {
976 for( LIBRARY_TABLE_TYPE type : aTablesToLoad )
977 m_projectTables.erase( type );
978 }
979
980 wxLogTrace( traceLibraries, "New project path %s is not readable, not loading project tables", aProjectPath );
981 }
982
983 // Phase 2: let adapters reconcile their cache against the rebuilt project
984 // table. This erases sentinels installed by ProjectTablesChanged() for any
985 // nickname that no longer has a project row, so a library removed from the
986 // project table stops masking a same-named global library.
987 {
988 std::scoped_lock lock( m_adaptersMutex );
989
990 for( const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter : m_adapters | std::views::values )
991 adapter->ProjectTablesReloaded( aTablesToLoad );
992 }
993}
994
995
997 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
998{
999 if( aScope == LIBRARY_TABLE_SCOPE::PROJECT )
1000 {
1002 LoadProjectTables( aTablesToLoad );
1003 }
1004 else
1005 {
1006 LoadGlobalTables( aTablesToLoad );
1007 }
1008}
1009
1010
1011std::optional<wxString> LIBRARY_MANAGER::GetFullURI( LIBRARY_TABLE_TYPE aType, const wxString& aNickname,
1012 bool aSubstituted )
1013{
1014 if( std::optional<const LIBRARY_TABLE_ROW*> result = GetRow( aType, aNickname ) )
1015 return GetFullURI( *result, aSubstituted, &Project() );
1016
1017 return std::nullopt;
1018}
1019
1020
1021wxString LIBRARY_MANAGER::GetFullURI( const LIBRARY_TABLE_ROW* aRow, bool aSubstituted,
1022 const PROJECT* aProject )
1023{
1024 if( aSubstituted )
1025 return ExpandEnvVarSubstitutions( aRow->URI(), aProject ? aProject : &Pgm().GetSettingsManager().Prj() );
1026
1027 return aRow->URI();
1028}
1029
1030
1031wxString LIBRARY_MANAGER::ExpandURI( const wxString& aShortURI, const PROJECT& aProject )
1032{
1033 wxLogNull doNotLog; // We do our own error reporting; we don't want to hear about missing envvars
1034
1035 wxFileName path( ExpandEnvVarSubstitutions( aShortURI, &aProject ) );
1036 path.MakeAbsolute();
1037 return path.GetFullPath();
1038}
1039
1040
1042{
1043 // PCM_LIB_TRAVERSER stores URIs of the form
1044 // ${KICADn_3RD_PARTY}/<category>/<pkgid>/<name>.<ext>
1045 // where <category> is one of the fixed PCM content folders. Matching this full
1046 // template is what uniquely identifies a PCM-added row. Matching only the leading
1047 // ${KICADn_3RD_PARTY} token is not sufficient because users routinely repurpose
1048 // that env var to point at their own library collection (as they did with
1049 // KICAD8_3RD_PARTY in earlier versions). A row the user added by hand under their
1050 // repurposed 3RD_PARTY directory must never be treated as PCM-managed, or the
1051 // auto-remove pass would silently delete it.
1052 const wxString& uri = aRow.URI();
1053
1054 if( !uri.StartsWith( wxS( "${" ) ) )
1055 return false;
1056
1057 size_t end = uri.find( wxS( '}' ) );
1058
1059 if( end == wxString::npos || end <= 2 )
1060 return false;
1061
1062 wxString varName = uri.SubString( 2, end - 1 );
1063
1064 if( !ENV_VAR::IsVersionedEnvVar( varName, wxS( "3RD_PARTY" ) ) )
1065 return false;
1066
1067 // PCM_LIB_TRAVERSER always joins the URI with '/', so the token must be followed by a
1068 // forward slash; a backslash or missing separator (e.g. "${KICAD10_3RD_PARTY}symbols/...")
1069 // was never emitted by PCM.
1070 if( end + 1 >= uri.length() || uri[end + 1] != wxS( '/' ) )
1071 return false;
1072
1073 // PCM_LIB_TRAVERSER nests libraries at least as <category>/<pkgid>/<library>, so there
1074 // must be a category folder, at least one package-id folder, and a library leaf, with no
1075 // empty components. A user library placed directly under the repurposed 3RD_PARTY root
1076 // (or in a same-named folder with no package level) does not match.
1077 wxArrayString parts = wxSplit( uri.Mid( end + 2 ), '/', '\0' );
1078
1079 if( parts.size() < 3 )
1080 return false;
1081
1082 for( const wxString& part : parts )
1083 {
1084 if( part.IsEmpty() )
1085 return false;
1086 }
1087
1088 wxString category = parts[0];
1089 wxString leaf = parts.Last();
1090
1091 // The leaf must carry the category-appropriate library extension over a non-empty stem;
1092 // a bare extension (e.g. a hidden ".kicad_sym") is never a PCM library and matching it
1093 // would let the auto-remove pass delete an unrelated file in a same-named folder.
1094 auto hasLibExtension = [&leaf]( const wxString& aExt )
1095 {
1096 return leaf.length() > aExt.length() && leaf.EndsWith( aExt );
1097 };
1098
1099 if( category == wxS( "symbols" ) )
1100 return hasLibExtension( wxS( ".kicad_sym" ) );
1101
1102 if( category == wxS( "footprints" ) )
1103 return hasLibExtension( wxS( ".pretty" ) );
1104
1105 if( category == wxS( "design_blocks" ) )
1106 {
1107 static const wxString designBlockExt =
1108 wxString::Format( wxS( ".%s" ), FILEEXT::KiCadDesignBlockLibPathExtension );
1109
1110 return hasLibExtension( designBlockExt );
1111 }
1112
1113 return false;
1114}
1115
1116
1117bool LIBRARY_MANAGER::UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 )
1118{
1119 // Avoid comparing filenames as wxURIs
1120 if( aURI1.Find( "://" ) != wxNOT_FOUND )
1121 {
1122 // found as full path
1123 return aURI1 == aURI2;
1124 }
1125 else
1126 {
1127 const wxFileName fn1( aURI1 );
1128 const wxFileName fn2( aURI2 );
1129
1130 // This will also test if the file is a symlink so if we are comparing
1131 // a symlink to the same real file, the comparison will be true. See
1132 // wxFileName::SameAs() in the wxWidgets source.
1133
1134 // found as full path and file name
1135 return fn1 == fn2;
1136 }
1137}
1138
1139
1143
1144
1149
1150
1154
1155
1160
1161
1166
1167
1169{
1170 abortLoad( aCancelPlugins );
1171
1173
1174 // Declared before the lock so plugins die after it is released; their worker threads may need it
1175 std::vector<std::unique_ptr<IO_BASE>> retired;
1176 std::unique_lock lock( m_librariesMutex );
1177
1178 // Reset entries in place rather than erasing them. Erasing would let
1179 // fetchIfLoaded() fall through to globalLibs() for any nickname that is
1180 // shadowed by a project library, defeating the project-over-global
1181 // precedence enforced by LIBRARY_MANAGER::Rows(). ProjectTablesReloaded()
1182 // later prunes any sentinels whose nicknames are no longer in the rebuilt
1183 // project table, so stale shadowing cannot persist.
1184 for( auto& entry : m_libraries )
1185 {
1186 retired.push_back( std::move( entry.second.plugin ) );
1187 entry.second = LIB_DATA{};
1188 }
1189}
1190
1191
1193{
1194 abortLoad( aCancelPlugins );
1195}
1196
1197
1199{
1200 // Blocking, since a lost try-lock would leave the cancelled libraries with nothing to restart them
1201 std::lock_guard lock( m_loadMutex );
1202
1203 if( m_rearmPending )
1205}
1206
1207
1209{
1210 // Else a load still running could publish a plugin after the marking below
1211 abortLoad( true );
1212
1213 {
1214 std::shared_lock lock( m_librariesMutex );
1215
1216 for( LIB_DATA& lib : m_libraries | std::views::values )
1217 {
1218 if( lib.plugin )
1219 lib.plugin->LeakCacheAtExit();
1220 }
1221 }
1222
1223 std::shared_lock lock( globalLibsMutex() );
1224
1225 for( LIB_DATA& lib : globalLibs() | std::views::values )
1226 {
1227 if( lib.global_owner == this && lib.plugin )
1228 lib.plugin->LeakCacheAtExit();
1229 }
1230}
1231
1232
1233void LIBRARY_MANAGER_ADAPTER::GlobalTablesChanged( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables )
1234{
1235 bool me = aChangedTables.size() == 0;
1236
1237 for( LIBRARY_TABLE_TYPE type : aChangedTables )
1238 {
1239 if( type == Type() )
1240 {
1241 me = true;
1242 break;
1243 }
1244 }
1245
1246 if( !me )
1247 return;
1248
1249 abortLoad();
1250
1252
1253 // Plugins die after unlocking; their worker threads may need the mutex
1254 std::map<wxString, LIB_DATA> retired;
1255
1256 {
1257 std::unique_lock lock( globalLibsMutex() );
1258 retired.swap( globalLibs() );
1259 }
1260}
1261
1262
1264{
1265 abortLoad( true );
1266
1268 [this]( const wxString&, const LIB_DATA& aLib )
1269 {
1270 return aLib.global_owner == this;
1271 } );
1272
1273 // Declared before the lock so plugins die after it is released
1274 std::vector<std::map<wxString, LIB_DATA>::node_type> retired;
1275 std::unique_lock lock( globalLibsMutex() );
1276
1277 // Drop entries this adapter owns, identified by global_owner rather than the row pointer,
1278 // which another manager could reuse at the same address after this manager frees its tables.
1279 for( auto it = globalLibs().begin(); it != globalLibs().end(); )
1280 {
1281 if( it->second.global_owner == this )
1282 retired.push_back( globalLibs().extract( it++ ) );
1283 else
1284 ++it;
1285 }
1286}
1287
1288
1290{
1291 abortLoad( true );
1292
1294
1296 [this]( const wxString&, const LIB_DATA& aLib )
1297 {
1298 return aLib.global_owner == this;
1299 } );
1300
1301 resetProjectCache( true );
1303}
1304
1305
1307 std::map<wxString, LIB_DATA>& aLibs, std::shared_mutex& aMutex,
1308 const std::function<bool( const wxString& aNickname, const LIB_DATA& aLib )>& aRetiring )
1309{
1310 std::vector<IO_BASE*> plugins;
1311
1312 {
1313 std::shared_lock lock( aMutex );
1314
1315 for( const auto& [nickname, lib] : aLibs )
1316 {
1317 if( lib.plugin && ( !aRetiring || aRetiring( nickname, lib ) ) )
1318 plugins.push_back( lib.plugin.get() );
1319 }
1320 }
1321
1322 // Unlocked as a refresh thread may wait on aMutex; the pointers stay valid since only this thread retires plugins
1323 for( IO_BASE* plugin : plugins )
1324 plugin->StopBackgroundRefresh();
1325}
1326
1327
1328void LIBRARY_MANAGER_ADAPTER::ProjectTablesChanged( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables )
1329{
1330 bool me = aChangedTables.size() == 0;
1331
1332 for( LIBRARY_TABLE_TYPE type : aChangedTables )
1333 {
1334 if( type == Type() )
1335 {
1336 me = true;
1337 break;
1338 }
1339 }
1340
1341 if( !me )
1342 return;
1343
1345}
1346
1347
1349 std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables )
1350{
1351 bool me = aChangedTables.size() == 0;
1352
1353 for( LIBRARY_TABLE_TYPE type : aChangedTables )
1354 {
1355 if( type == Type() )
1356 {
1357 me = true;
1358 break;
1359 }
1360 }
1361
1362 if( !me )
1363 return;
1364
1365 // Erase sentinels installed by resetProjectCache() for nicknames that no
1366 // longer appear in the rebuilt project table. Without this, a library
1367 // removed from the project would remain masked in m_libraries and hide a
1368 // same-named global library from HasLibrary() / fetchIfLoaded() / etc.
1369 // GetRow() is safe to call here: loadTables() reset m_rowCache before
1370 // building the new table, and async loads were aborted in phase 1.
1371 auto pruned =
1372 [this]( const wxString& aNickname )
1373 {
1374 return !m_manager.GetRow( Type(), aNickname, LIBRARY_TABLE_SCOPE::PROJECT ).has_value();
1375 };
1376
1378 [&]( const wxString& aNickname, const LIB_DATA& )
1379 {
1380 return pruned( aNickname );
1381 } );
1382
1383 // Declared before the lock so any plugin loaded since the reset dies after it is released
1384 std::vector<std::map<wxString, LIB_DATA>::node_type> retired;
1385 std::unique_lock lock( m_librariesMutex );
1386
1387 for( auto it = m_libraries.begin(); it != m_libraries.end(); )
1388 {
1389 if( pruned( it->first ) )
1390 retired.push_back( m_libraries.extract( it++ ) );
1391 else
1392 ++it;
1393 }
1394}
1395
1396
1398{
1399 // A disabled row is never used; skip validation so a disabled unreachable HTTP backend
1400 // can't stall the library table dialog, and clear any stale error from an earlier check
1401 if( aRow.Disabled() )
1402 {
1403 aRow.SetOk( true );
1404 aRow.SetErrorDescription( wxEmptyString );
1405 return;
1406 }
1407
1408 // Testing is expensive; skip it if we already have a library with the same
1409 // nickname and URI as the row under test
1410 if( std::optional<LIB_DATA*> libData = fetchIfLoaded( aRow.Nickname() ) )
1411 {
1412 const LIBRARY_TABLE_ROW* loadedRow = ( *libData )->row;
1413
1414 if( loadedRow->URI() == aRow.URI() && loadedRow->Type() == aRow.Type() )
1415 {
1416 aRow.SetOk( loadedRow->IsOk() );
1417 return;
1418 }
1419 }
1420
1421 abortLoad();
1422
1424
1425 if( plugin.has_value() )
1426 {
1427 LIB_DATA lib;
1428 lib.row = &aRow;
1429 lib.plugin.reset( *plugin );
1430
1431 std::optional<LIB_STATUS> status = CheckLibrary( &lib );
1432
1433 if( status.has_value() )
1434 {
1435 aRow.SetOk( status.value().load_status == LOAD_STATUS::LOADED );
1436
1437 if( status.value().error.has_value() )
1438 aRow.SetErrorDescription( status.value().error.value().message );
1439 }
1440 }
1441 else if( plugin.error().message == LIBRARY_TABLE_OK().message )
1442 {
1443 aRow.SetOk( true );
1444 aRow.SetErrorDescription( wxEmptyString );
1445 }
1446 else
1447 {
1448 aRow.SetOk( false );
1449 aRow.SetErrorDescription( plugin.error().message );
1450 }
1451}
1452
1453
1454
1456{
1457 wxCHECK( m_manager.Table( Type(), LIBRARY_TABLE_SCOPE::GLOBAL ), nullptr );
1458 return *m_manager.Table( Type(), LIBRARY_TABLE_SCOPE::GLOBAL );
1459}
1460
1461
1462std::optional<LIBRARY_TABLE*> LIBRARY_MANAGER_ADAPTER::ProjectTable() const
1463{
1464 return m_manager.Table( Type(), LIBRARY_TABLE_SCOPE::PROJECT );
1465}
1466
1467
1468std::optional<wxString> LIBRARY_MANAGER_ADAPTER::FindLibraryByURI( const wxString& aURI ) const
1469{
1470 for( const LIBRARY_TABLE_ROW* row : m_manager.Rows( Type() ) )
1471 {
1472 if( LIBRARY_MANAGER::UrisAreEquivalent( LIBRARY_MANAGER::GetFullURI( row, true, &m_manager.Project() ), aURI ) )
1473 return row->Nickname();
1474 }
1475
1476 return std::nullopt;
1477}
1478
1479
1480std::vector<wxString> LIBRARY_MANAGER_ADAPTER::GetLibraryNames() const
1481{
1482 std::vector<wxString> ret;
1483 std::vector<LIBRARY_TABLE_ROW*> rows = m_manager.Rows( Type() );
1484
1485 wxLogTrace( traceLibraries, "GetLibraryNames: checking %zu rows from table", rows.size() );
1486
1487 for( const LIBRARY_TABLE_ROW* row : rows )
1488 {
1489 wxString nickname = row->Nickname();
1490 std::optional<const LIB_DATA*> loaded = fetchIfLoaded( nickname );
1491
1492 if( loaded )
1493 ret.emplace_back( nickname );
1494 }
1495
1497
1498 wxLogTrace( traceLibraries, "GetLibraryNames: returning %zu of %zu libraries", ret.size(), rows.size() );
1499 return ret;
1500}
1501
1502
1503bool LIBRARY_MANAGER_ADAPTER::HasLibrary( const wxString& aNickname, bool aCheckEnabled ) const
1504{
1505 std::optional<const LIB_DATA*> r = fetchIfLoaded( aNickname );
1506
1507 if( r.has_value() )
1508 return !aCheckEnabled || !r.value()->row->Disabled();
1509
1510 return false;
1511}
1512
1513
1514bool LIBRARY_MANAGER_ADAPTER::DeleteLibrary( const wxString& aNickname )
1515{
1516 if( LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( aNickname ); result.has_value() )
1517 {
1518 LIB_DATA* data = *result;
1519 std::map<std::string, UTF8> options = data->row->GetOptionsMap();
1520
1521 // Serialize cache teardown against readers. loadIfNeeded() already dropped the manager lock
1522 // (manager > pluginMutex).
1523 std::lock_guard pluginGuard( pluginMutex( aNickname ) );
1524
1525 try
1526 {
1527 return data->plugin->DeleteLibrary( getUri( data->row ), &options );
1528 }
1529 catch( ... )
1530 {
1531 return false;
1532 }
1533 }
1534
1535 return false;
1536}
1537
1538
1539std::optional<wxString> LIBRARY_MANAGER_ADAPTER::GetLibraryDescription( const wxString& aNickname ) const
1540{
1541 if( std::optional<const LIB_DATA*> optRow = fetchIfLoaded( aNickname ); optRow )
1542 return ( *optRow )->row->Description();
1543
1544 return std::nullopt;
1545}
1546
1547
1548std::vector<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER_ADAPTER::Rows( LIBRARY_TABLE_SCOPE aScope,
1549 bool aIncludeInvalid ) const
1550{
1551 return m_manager.Rows( Type(), aScope, aIncludeInvalid );
1552}
1553
1554
1555std::optional<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER_ADAPTER::GetRow( const wxString &aNickname,
1556 LIBRARY_TABLE_SCOPE aScope ) const
1557{
1558 return m_manager.GetRow( Type(), aNickname, aScope );
1559}
1560
1561
1562std::optional<LIBRARY_TABLE_ROW*> LIBRARY_MANAGER_ADAPTER::FindRowByURI(
1563 const wxString& aUri,
1564 LIBRARY_TABLE_SCOPE aScope ) const
1565{
1566 return m_manager.FindRowByURI( Type(), aUri, aScope );
1567}
1568
1569
1570void LIBRARY_MANAGER_ADAPTER::withPlugin( const LOADING_KEY& aKey, const std::function<void( IO_BASE& )>& aAction )
1571{
1572 const bool global = aKey.second == LIBRARY_TABLE_SCOPE::GLOBAL;
1573
1574 std::map<wxString, LIB_DATA>& libs = global ? globalLibs() : m_libraries;
1575
1576 // A plugin leaves the map only under the exclusive lock, so it stays alive for the call
1577 std::shared_lock lock( global ? globalLibsMutex() : m_librariesMutex );
1578
1579 if( auto it = libs.find( aKey.first ); it != libs.end() && it->second.plugin )
1580 aAction( *it->second.plugin );
1581}
1582
1583
1584std::vector<LIBRARY_MANAGER_ADAPTER::LOADING_KEY> LIBRARY_MANAGER_ADAPTER::loadingSnapshot()
1585{
1586 std::lock_guard lock( m_loadingMutex );
1587 return std::vector<LOADING_KEY>( m_loading.begin(), m_loading.end() );
1588}
1589
1590
1591void LIBRARY_MANAGER_ADAPTER::cancelLoadingPlugins( const std::vector<LOADING_KEY>& aSnapshot )
1592{
1593 for( const LOADING_KEY& key : aSnapshot )
1594 {
1595 withPlugin( key,
1596 []( IO_BASE& aPlugin )
1597 {
1598 aPlugin.CancelLoad();
1599 } );
1600 }
1601}
1602
1603
1604void LIBRARY_MANAGER_ADAPTER::resumeLoadingPlugins( const std::vector<LOADING_KEY>& aCancelled )
1605{
1606 for( const LOADING_KEY& key : aCancelled )
1607 {
1608 withPlugin( key,
1609 []( IO_BASE& aPlugin )
1610 {
1611 aPlugin.ResumeLoad();
1612 } );
1613 }
1614}
1615
1616
1617void LIBRARY_MANAGER_ADAPTER::abortLoad( bool aCancelPlugins )
1618{
1619 {
1620 std::lock_guard lock( m_loadMutex );
1621
1622 // Only abortLoad sets m_abort, so a ready future has worked through its whole queue
1623 std::erase_if( m_futures,
1624 []( std::future<void>& aFuture )
1625 {
1626 return aFuture.valid() && aFuture.wait_for( 0s ) == std::future_status::ready;
1627 } );
1628
1629 if( m_futures.empty() )
1630 return;
1631
1632 wxLogTrace( traceLibraries, "Aborting library load..." );
1633 m_abort.store( true );
1634
1635 if( aCancelPlugins )
1636 {
1637 m_cancelling.store( true );
1638 m_rearmPending = true;
1639
1640 // Bumped after the flags, so a load that reads the new generation also reads the cancel
1641 m_abortGeneration.fetch_add( 1 );
1642 }
1643 }
1644
1645 // The snapshot follows m_cancelling, so a worker that lists itself later sees the flag instead
1646 struct ABORT_SCOPE
1647 {
1648 LIBRARY_MANAGER_ADAPTER& adapter;
1649 std::vector<LOADING_KEY> cancelled;
1650
1651 // Joins and resets on every exit, so a throwing cancel cannot leave m_abort set or a plugin cancelled
1652 ~ABORT_SCOPE()
1653 {
1654 adapter.BlockUntilLoaded();
1655 adapter.resumeLoadingPlugins( cancelled );
1656 wxLogTrace( traceLibraries, "Aborted" );
1657
1658 std::lock_guard lock( adapter.m_loadMutex );
1659 adapter.m_abort.store( false );
1660 adapter.m_cancelling.store( false );
1661 adapter.m_futures.clear();
1662 adapter.m_loadTotal.store( 0 );
1663 adapter.m_loadCount.store( 0 );
1664 }
1665 } scope{ *this, {} };
1666
1667 if( aCancelPlugins )
1668 scope.cancelled = loadingSnapshot();
1669
1670 cancelLoadingPlugins( scope.cancelled );
1671}
1672
1673
1675{
1676 size_t total = m_loadTotal.load();
1677
1678 if( total == 0 )
1679 return std::nullopt;
1680
1681 size_t loaded = m_loadCount.load();
1682 return loaded / static_cast<float>( total );
1683}
1684
1685
1686bool LIBRARY_MANAGER_ADAPTER::RunPreload( PROGRESS_REPORTER& aReporter, std::atomic_bool& aAbort )
1687{
1688 constexpr std::chrono::milliseconds interval( 150 );
1689
1690 AsyncLoad();
1691
1692 while( !aAbort.load() )
1693 {
1694 std::this_thread::sleep_for( interval );
1695
1696 std::optional<float> progress = AsyncLoadProgress();
1697 aReporter.SetCurrentProgress( progress.value_or( 1 ) );
1698
1699 if( progress.value_or( 1 ) >= 1 )
1700 {
1701 // Workers can still be retiring once the count reaches the total
1703 return true;
1704 }
1705 }
1706
1707 aAbort.store( false );
1708
1709 // Plugins that can end a load early do so instead of being waited out
1710 AbortAsyncLoad( true );
1711 return false;
1712}
1713
1714
1716{
1717 wxLogTrace( traceLibraries, "BlockUntilLoaded: entry, acquiring m_loadMutex" );
1718 std::unique_lock<std::mutex> asyncLock( m_loadMutex );
1719
1720 wxLogTrace( traceLibraries, "BlockUntilLoaded: waiting on %zu futures", m_futures.size() );
1721
1722 for( const std::future<void>& future : m_futures )
1723 future.wait();
1724
1725 wxLogTrace( traceLibraries, "BlockUntilLoaded: all futures complete, loadCount=%zu, loadTotal=%zu",
1726 m_loadCount.load(), m_loadTotal.load() );
1727}
1728
1729
1730bool LIBRARY_MANAGER_ADAPTER::IsLibraryLoaded( const wxString& aNickname )
1731{
1732 {
1733 std::shared_lock lock( m_librariesMutex );
1734
1735 if( auto it = m_libraries.find( aNickname ); it != m_libraries.end() )
1736 return it->second.status.load_status == LOAD_STATUS::LOADED;
1737 }
1738
1739 {
1740 std::shared_lock lock( globalLibsMutex() );
1741
1742 if( auto it = globalLibs().find( aNickname ); it != globalLibs().end() )
1743 return it->second.status.load_status == LOAD_STATUS::LOADED;
1744 }
1745
1746 return false;
1747}
1748
1749
1750std::optional<LIBRARY_ERROR> LIBRARY_MANAGER_ADAPTER::LibraryError( const wxString& aNickname ) const
1751{
1752 {
1753 std::shared_lock lock( m_librariesMutex );
1754
1755 if( auto it = m_libraries.find( aNickname ); it != m_libraries.end() )
1756 return it->second.status.error;
1757 }
1758
1759 {
1760 std::shared_lock lock( globalLibsMutex() );
1761
1762 if( auto it = globalLibs().find( aNickname ); it != globalLibs().end() )
1763 return it->second.status.error;
1764 }
1765
1766 return std::nullopt;
1767}
1768
1769
1770std::vector<std::pair<wxString, LIB_STATUS>> LIBRARY_MANAGER_ADAPTER::GetLibraryStatuses() const
1771{
1772 std::vector<std::pair<wxString, LIB_STATUS>> ret;
1773
1774 for( const LIBRARY_TABLE_ROW* row : m_manager.Rows( Type() ) )
1775 {
1776 if( row->Disabled() )
1777 continue;
1778
1779 if( std::optional<LIB_STATUS> result = GetLibraryStatus( row->Nickname() ) )
1780 {
1781 ret.emplace_back( std::make_pair( row->Nickname(), *result ) );
1782 }
1783 else
1784 {
1785 // The row came from the library table, so the missing entry means the async
1786 // preload has not reached it yet (or was pre-empted by a project change), not
1787 // that the library is absent. Reporting a load error here surfaces a spurious
1788 // error indicator while background loading races schematic auto-open. Genuine
1789 // failures carry their own LOAD_ERROR entry, and the library still loads on
1790 // demand, so report it as not-yet-loaded instead.
1791 ret.emplace_back( std::make_pair( row->Nickname(),
1792 LIB_STATUS( { .load_status = LOAD_STATUS::INVALID } ) ) );
1793 }
1794 }
1795
1796 return ret;
1797}
1798
1799
1801{
1802 std::vector<KI_ERROR> errors;
1803
1804 for( const auto& [nickname, status] : GetLibraryStatuses() )
1805 {
1806 if( status.load_status == LOAD_STATUS::LOAD_ERROR && status.error )
1807 {
1808 wxString title = status.error->message.BeforeFirst( '\n' );
1809 title.StartsWith( wxS( "IO_ERROR: " ), &title );
1810 errors.emplace_back(
1811 KI_ERROR( RPT_SEVERITY_ERROR, wxString::Format( _( "Error loading library '%s'" ), nickname ) )
1812 .SetDescription( title )
1813#ifdef DEBUG
1814 .SetDebugText( status.error->details )
1815#endif
1816 );
1817 }
1818 }
1819
1820 return errors;
1821}
1822
1823
1824std::optional<LIB_STATUS> LIBRARY_MANAGER_ADAPTER::LoadLibraryEntry( const wxString& aNickname )
1825{
1827
1828 if( result.has_value() )
1829 return LoadOne( *result );
1830
1831 return std::nullopt;
1832}
1833
1834
1836{
1837 // Drain the async load before erasing, else a worker mid-enumerate writes status through the
1838 // freed LIB_DATA (every other invalidator already does this).
1839 abortLoad( true );
1840
1841 auto reloadScope =
1842 [&]( LIBRARY_TABLE_SCOPE aScopeToReload, std::map<wxString, LIB_DATA>& aTarget,
1843 std::shared_mutex& aMutex )
1844 {
1845 bool wasLoaded = false;
1846 std::map<wxString, LIB_DATA>::node_type retired;
1847
1848 {
1849 std::unique_lock lock( aMutex );
1850 auto it = aTarget.find( aNickname );
1851
1852 if( it != aTarget.end() && it->second.plugin )
1853 {
1854 wasLoaded = true;
1855 retired = aTarget.extract( it );
1856 }
1857 }
1858
1859 // Destroyed unlocked since the plugin's worker thread may be blocked on aMutex
1860 retired = {};
1861
1862 if( wasLoaded )
1863 {
1864 LIBRARY_RESULT<LIB_DATA*> result = loadFromScope( aNickname, aScopeToReload, aTarget, aMutex );
1865
1866 if( result.has_value() && *result )
1867 {
1868 LoadOne( *result );
1869 }
1870 else if( !result.has_value() )
1871 {
1872 wxLogTrace( traceLibraries, "ReloadLibraryEntry: failed to reload %s (%s): %s",
1873 aNickname, magic_enum::enum_name( aScopeToReload ),
1874 result.error().message );
1875 }
1876 }
1877 };
1878
1879 switch( aScope )
1880 {
1883 break;
1884
1887 break;
1888
1893 break;
1894 }
1895
1896 // Both scopes are swapped by now, so the cancelled libraries can be queued again
1898}
1899
1900
1901bool LIBRARY_MANAGER_ADAPTER::IsWritable( const wxString& aNickname ) const
1902{
1903 if( std::optional<const LIB_DATA*> result = fetchIfLoaded( aNickname ) )
1904 {
1905 const LIB_DATA* rowData = *result;
1906
1907 // IsLibraryWritable() may rebuild the cache via validateCache(); serialize against readers.
1908 // fetchIfLoaded() already dropped the manager lock.
1909 std::lock_guard pluginGuard( pluginMutex( aNickname ) );
1910
1911 return rowData->plugin->IsLibraryWritable( getUri( rowData->row ) );
1912 }
1913
1914 return false;
1915}
1916
1917
1918bool LIBRARY_MANAGER_ADAPTER::CreateLibrary( const wxString& aNickname )
1919{
1920 if( LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( aNickname ); result.has_value() )
1921 {
1922 LIB_DATA* data = *result;
1923 std::map<std::string, UTF8> options = data->row->GetOptionsMap();
1924
1925 // Serialize cache replacement against readers; loadIfNeeded() already dropped the manager lock.
1926 std::lock_guard pluginGuard( pluginMutex( aNickname ) );
1927
1928 try
1929 {
1930 data->plugin->CreateLibrary( getUri( data->row ), &options );
1931 return true;
1932 }
1933 catch( const IO_ERROR& ioe )
1934 {
1935 wxLogTrace( traceLibraries, "CreateLibrary: IO_ERROR for %s: %s",
1936 aNickname, ioe.What() );
1937 return false;
1938 }
1939 catch( const std::exception& e )
1940 {
1941 wxLogTrace( traceLibraries, "CreateLibrary: std::exception for %s: %s",
1942 aNickname, e.what() );
1943 return false;
1944 }
1945 }
1946
1947 wxLogTrace( traceLibraries, "CreateLibrary: library row '%s' not found", aNickname );
1948 return false;
1949}
1950
1951
1953{
1954 return LIBRARY_MANAGER::ExpandURI( aRow->URI(), m_manager.Project() );
1955}
1956
1957
1958std::optional<const LIB_DATA*> LIBRARY_MANAGER_ADAPTER::fetchIfLoaded( const wxString& aNickname ) const
1959{
1960 {
1961 std::shared_lock lock( m_librariesMutex );
1962
1963 if( auto it = m_libraries.find( aNickname ); it != m_libraries.end() )
1964 {
1965 if( it->second.status.load_status == LOAD_STATUS::LOADED )
1966 return &it->second;
1967
1968 return std::nullopt;
1969 }
1970 }
1971
1972 if( m_manager.GetRow( Type(), aNickname, LIBRARY_TABLE_SCOPE::PROJECT ) )
1973 return std::nullopt; // project row exists but is not loaded
1974
1975 {
1976 std::shared_lock lock( globalLibsMutex() );
1977
1978 if( auto it = globalLibs().find( aNickname ); it != globalLibs().end() )
1979 {
1980 if( it->second.status.load_status == LOAD_STATUS::LOADED )
1981 return &it->second;
1982
1983 return std::nullopt;
1984 }
1985 }
1986
1987 return std::nullopt;
1988}
1989
1990
1991std::optional<LIB_DATA*> LIBRARY_MANAGER_ADAPTER::fetchIfLoaded( const wxString& aNickname )
1992{
1993 {
1994 std::shared_lock lock( m_librariesMutex );
1995
1996 if( auto it = m_libraries.find( aNickname ); it != m_libraries.end() )
1997 {
1998 if( it->second.status.load_status == LOAD_STATUS::LOADED )
1999 return &it->second;
2000
2001 return std::nullopt;
2002 }
2003 }
2004
2005 if( m_manager.GetRow( Type(), aNickname, LIBRARY_TABLE_SCOPE::PROJECT ) )
2006 return std::nullopt; // project row exists but is not loaded
2007
2008 {
2009 std::shared_lock lock( globalLibsMutex() );
2010
2011 if( auto it = globalLibs().find( aNickname ); it != globalLibs().end() )
2012 {
2013 if( it->second.status.load_status == LOAD_STATUS::LOADED )
2014 return &it->second;
2015
2016 return std::nullopt;
2017 }
2018 }
2019
2020 return std::nullopt;
2021}
2022
2023
2025 LIBRARY_TABLE_SCOPE aScope,
2026 std::map<wxString, LIB_DATA>& aTarget,
2027 std::shared_mutex& aMutex )
2028{
2029 bool present = false;
2030
2031 {
2032 std::shared_lock lock( aMutex );
2033 present = aTarget.contains( aNickname ) && aTarget.at( aNickname ).plugin;
2034 }
2035
2036 if( !present )
2037 {
2038 if( auto result = m_manager.GetRow( Type(), aNickname, aScope ) )
2039 {
2040 const LIBRARY_TABLE_ROW* row = *result;
2041 wxLogTrace( traceLibraries, "Library %s (%s) not yet loaded, will attempt...",
2042 aNickname, magic_enum::enum_name( aScope ) );
2043
2044 if( LIBRARY_RESULT<IO_BASE*> plugin = createPlugin( row ); plugin.has_value() )
2045 {
2046 // Declared before the lock so a losing candidate dies unlocked; its worker may need the mutex
2047 std::unique_ptr<IO_BASE> candidate( *plugin );
2048 std::unique_lock lock( aMutex );
2049 LIB_DATA& slot = aTarget[row->Nickname()];
2050
2051 // Another creator may have installed a plugin while the lock was released
2052 if( !slot.plugin )
2053 {
2055 slot.row = row;
2056 slot.plugin = std::move( candidate );
2057
2058 if( aScope == LIBRARY_TABLE_SCOPE::GLOBAL )
2059 slot.global_owner = this;
2060 }
2061
2062 return &slot;
2063 }
2064 else
2065 {
2066 return tl::unexpected( plugin.error() );
2067 }
2068 }
2069
2070 return nullptr;
2071 }
2072
2073 std::shared_lock lock( aMutex );
2074 return &aTarget.at( aNickname );
2075}
2076
2077
2079{
2082
2083 if( !result.has_value() || *result )
2084 return result;
2085
2087
2088 if( !result.has_value() || *result )
2089 return result;
2090
2091 wxString msg = wxString::Format( _( "Library %s not found" ), aNickname );
2092 return tl::unexpected( LIBRARY_ERROR( msg ) );
2093}
2094
2095
2096std::optional<LIB_STATUS> LIBRARY_MANAGER_ADAPTER::GetLibraryStatus( const wxString& aNickname ) const
2097{
2098 {
2099 std::shared_lock lock( m_librariesMutex );
2100
2101 if( auto it = m_libraries.find( aNickname ); it != m_libraries.end() )
2102 return it->second.status;
2103 }
2104
2105 {
2106 std::shared_lock lock( globalLibsMutex() );
2107
2108 if( auto it = globalLibs().find( aNickname ); it != globalLibs().end() )
2109 return it->second.status;
2110 }
2111
2112 return std::nullopt;
2113}
2114
2115
2117{
2118 std::unique_lock<std::mutex> asyncLock( m_loadMutex, std::try_to_lock );
2119
2120 if( !asyncLock )
2121 return;
2122
2124}
2125
2126
2128{
2129 std::erase_if( m_futures,
2130 []( std::future<void>& aFuture )
2131 {
2132 return aFuture.valid()
2133 && aFuture.wait_for( 0s ) == std::future_status::ready;
2134 } );
2135
2136 if( !m_futures.empty() )
2137 {
2138 wxLogTrace( traceLibraries, "Cannot AsyncLoad, futures from a previous call remain!" );
2139 return;
2140 }
2141
2142 std::vector<LIBRARY_TABLE_ROW*> rows = m_manager.Rows( Type() );
2143
2144 m_loadTotal.store( rows.size() );
2145 m_loadCount.store( 0 );
2146
2147 if( rows.empty() )
2148 {
2149 wxLogTrace( traceLibraries, "AsyncLoad: no libraries left to load; exiting" );
2150 m_rearmPending = false;
2151 return;
2152 }
2153
2155
2156 auto check =
2157 [&]( const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::shared_mutex& aMutex )
2158 {
2159 std::shared_lock lock( aMutex );
2160
2161 if( auto it = aMap.find( aLib ); it != aMap.end() )
2162 {
2163 LOAD_STATUS status = it->second.status.load_status;
2164
2165 if( status == LOAD_STATUS::LOADED || status == LOAD_STATUS::LOADING )
2166 return true;
2167 }
2168
2169 return false;
2170 };
2171
2172 // Collect work items with pre-resolved URIs. URI expansion accesses PROJECT data
2173 // (text variables, env vars) that is not thread-safe, so resolve on the calling thread.
2174 auto workQueue = std::make_shared<std::vector<LOAD_WORK>>();
2175 workQueue->reserve( rows.size() );
2176
2177 for( const LIBRARY_TABLE_ROW* row : rows )
2178 {
2179 wxString nickname = row->Nickname();
2180 LIBRARY_TABLE_SCOPE scope = row->Scope();
2181
2182 // Disabled libraries must never be contacted; skipping here keeps a broken or slow
2183 // backend (e.g. an unreachable HTTP library) from blocking startup loads
2184 if( row->Disabled() )
2185 {
2186 m_loadTotal.fetch_sub( 1 );
2187 continue;
2188 }
2189
2190 if( check( nickname, m_libraries, m_librariesMutex ) )
2191 {
2192 m_loadTotal.fetch_sub( 1 );
2193 continue;
2194 }
2195
2196 if( check( nickname, globalLibs(), globalLibsMutex() ) && scope != LIBRARY_TABLE_SCOPE::PROJECT )
2197 {
2198 m_loadTotal.fetch_sub( 1 );
2199 continue;
2200 }
2201
2202 workQueue->emplace_back( LOAD_WORK{ nickname, scope, getUri( row ) } );
2203 }
2204
2205 if( workQueue->empty() )
2206 {
2207 wxLogTrace( traceLibraries, "AsyncLoad: all libraries already loaded; exiting" );
2208 m_rearmPending = false;
2209 return;
2210 }
2211
2212 // Cap loading threads to leave headroom for the GUI and other thread pool work.
2213 // Each worker pulls libraries from a shared queue, so we submit fewer tasks than
2214 // libraries and avoid flooding the pool.
2215 size_t poolSize = tp.get_thread_count();
2216 size_t maxLoadThreads = std::max<size_t>( 1, poolSize > 2 ? poolSize - 2 : 1 );
2217 size_t numWorkers = std::min( maxLoadThreads, workQueue->size() );
2218
2219 auto workIndex = std::make_shared<std::atomic<size_t>>( 0 );
2220
2221 wxLogTrace( traceLibraries, "AsyncLoad: %zu libraries to load, using %zu worker threads (pool has %zu)",
2222 workQueue->size(), numWorkers, poolSize );
2223
2224 for( size_t w = 0; w < numWorkers; ++w )
2225 {
2226 m_futures.emplace_back( tp.submit_task(
2227 [this, workQueue, workIndex]()
2228 {
2229 while( true )
2230 {
2231 if( m_abort.load() )
2232 return;
2233
2234 size_t idx = workIndex->fetch_add( 1 );
2235
2236 if( idx >= workQueue->size() )
2237 return;
2238
2239 const LOAD_WORK& work = ( *workQueue )[idx];
2240
2241 // Any other exception, from the factory or the enumerate, is still counted, else the progress
2242 // never reaches 1
2243 try
2244 {
2245 if( !loadQueuedRow( work ) )
2246 continue;
2247 }
2248 catch( std::exception& e )
2249 {
2250 std::unique_lock lock( scopeMutex( work.scope ) );
2251
2252 LIB_STATUS& status = queuedRowEntry( work ).status;
2253
2254 // Counted even when cancelled, since the abort resets the count anyway
2255 if( m_cancelling.load() )
2256 {
2257 status.load_status = LOAD_STATUS::INVALID;
2258 status.error.reset();
2259 }
2260 else
2261 {
2262 status.load_status = LOAD_STATUS::LOAD_ERROR;
2263 status.error = LIBRARY_ERROR( wxString::FromUTF8( e.what() ) );
2264 }
2265 }
2266
2267 ++m_loadCount;
2268 }
2269 },
2270 BS::pr::lowest ) );
2271 }
2272
2273 m_rearmPending = false;
2274 wxLogTrace( traceLibraries, "Started async load of %zu libraries", workQueue->size() );
2275}
2276
2277
2279{
2281
2282 if( result.has_value() )
2283 {
2284 LIB_DATA* lib = *result;
2285
2286 // Listed while enumerating so a cancel can reach the plugin; the cancel flag is read
2287 // after listing, so either the abort sees the entry or the worker sees the flag
2288 struct LOADING_ENTRY
2289 {
2290 LIBRARY_MANAGER_ADAPTER& adapter;
2291 LOADING_KEY key;
2292
2293 ~LOADING_ENTRY()
2294 {
2295 std::lock_guard lock( adapter.m_loadingMutex );
2296 adapter.m_loading.erase( key );
2297 }
2298 } loadingEntry{ *this, { aWork.nickname, aWork.scope } };
2299
2300 {
2301 std::lock_guard lock( m_loadingMutex );
2302 m_loading.insert( loadingEntry.key );
2303 }
2304
2305 try
2306 {
2307 {
2308 std::unique_lock lock( scopeMutex( aWork.scope ) );
2309
2310 // A cancelled load is picked up again by the next AsyncLoad
2311 if( m_cancelling.load() )
2312 {
2314 return false;
2315 }
2316
2318 }
2319
2320 enumerateLibrary( lib, aWork.uri );
2321
2322 {
2323 std::unique_lock lock( scopeMutex( aWork.scope ) );
2325 }
2326 }
2327 catch( IO_ERROR& e )
2328 {
2329 std::unique_lock lock( scopeMutex( aWork.scope ) );
2330
2331 // A load the abort cut short is not a failure of the library
2332 if( m_cancelling.load() )
2333 {
2335 lib->status.error.reset();
2336 return false;
2337 }
2338
2340 lib->status.error = LIBRARY_ERROR( e.Problem(), e.Where() );
2341 wxLogTrace( traceLibraries, "%s: plugin threw exception: %s", aWork.nickname, e.What() );
2342 }
2343 }
2344 else
2345 {
2346 std::unique_lock lock( scopeMutex( aWork.scope ) );
2347
2348 queuedRowEntry( aWork ).status =
2349 LIB_STATUS( { .load_status = LOAD_STATUS::LOAD_ERROR, .error = result.error() } );
2350 }
2351
2352 return true;
2353}
2354
2355
2360
2361
2363{
2364 if( aWork.scope != LIBRARY_TABLE_SCOPE::GLOBAL )
2365 return m_libraries[aWork.nickname];
2366
2367 LIB_DATA& entry = globalLibs()[aWork.nickname];
2368
2369 // An entry nobody owns would outlive the rows it names, since only its owner evicts it
2370 if( !entry.global_owner )
2371 entry.global_owner = this;
2372
2373 return entry;
2374}
const char * name
virtual bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete an existing library and returns true, or if library does not exist returns false,...
Definition io_base.cpp:53
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
Definition io_base.cpp:60
virtual void CancelLoad()
Ask a library load running on this plugin to end early.
Definition io_base.h:168
virtual void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Create a new empty library at aLibraryPath empty.
Definition io_base.cpp:46
virtual void ResumeLoad() noexcept
Called after the loads that were cancelled have ended, to clear what CancelLoad() left behind.
Definition io_base.h:171
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()
virtual const wxString Problem() const
what was the problem?
virtual const wxString Where() const
where did the Problem() occur?
wxString m_PcmLibPrefix
std::map< wxString, std::map< wxString, LIB_OVERRIDE > > m_LibOverrides
Overrides for libraries in read-only nested tables.
Holds a structured error message.
Definition ki_error.h:38
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
std::optional< float > AsyncLoadProgress() const
Returns the fraction of the libraries queued by AsyncLoad() that have been processed,...
virtual std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib)=0
std::vector< KI_ERROR > GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
void resetProjectCache(bool aCancelPlugins=false)
Aborts pending loads and resets every project-scope cache entry in place (plugin and row cleared,...
bool loadQueuedRow(const LOAD_WORK &aWork)
Loads one queued library on a worker thread.
std::atomic_bool m_cancelling
Set with m_abort by an abort that cancels, so only such an abort turns a failed load into INVALID.
virtual std::optional< LIBRARY_ERROR > LibraryError(const wxString &aNickname) const
void ReloadLibraryEntry(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
std::optional< LIB_STATUS > GetLibraryStatus(const wxString &aNickname) const
Returns the status of a loaded library, or nullopt if the library hasn't been loaded (yet)
void stopRefreshThreads(std::map< wxString, LIB_DATA > &aLibs, std::shared_mutex &aMutex, const std::function< bool(const wxString &aNickname, const LIB_DATA &aLib)> &aRetiring={})
Stops the refresh threads of the plugins in aLibs that aRetiring selects, or of all of them when it i...
void AbortAsyncLoad(bool aCancelPlugins=false)
Aborts any async load in progress; blocks until fully done aborting.
std::pair< wxString, LIBRARY_TABLE_SCOPE > LOADING_KEY
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
void ProjectTablesChanged(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Notify the adapter that the project library tables are about to be rebuilt.
virtual void enumerateLibrary(LIB_DATA *aLib, const wxString &aUri)=0
Override in derived class to perform library-specific enumeration.
LIBRARY_TABLE * GlobalTable() const
Retrieves the global library table for this adapter type.
virtual std::shared_mutex & globalLibsMutex()=0
std::atomic< unsigned > m_abortGeneration
Counts cancelling aborts, so a load can tell one that began after it started from one that already en...
void evictOwnedGlobalEntries()
Erases this adapter's own entries (LIB_DATA::global_owner == this) from the process-wide globalLibs()...
LIBRARY_MANAGER_ADAPTER(LIBRARY_MANAGER &aManager)
Constructs a type-specific adapter into the library manager.
void CheckTableRow(LIBRARY_TABLE_ROW &aRow)
bool IsLibraryLoaded(const wxString &aNickname)
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Like LIBRARY_MANAGER::Rows but filtered to the LIBRARY_TABLE_TYPE of this adapter.
std::optional< LIBRARY_TABLE_ROW * > FindRowByURI(const wxString &aUri, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::FindRowByURI but filtered to the LIBRARY_TABLE_TYPE of this adapter.
wxString getUri(const LIBRARY_TABLE_ROW *aRow) const
bool RunPreload(PROGRESS_REPORTER &aReporter, std::atomic_bool &aAbort)
Starts AsyncLoad() and waits for it on the calling thread, reporting progress to aReporter.
virtual std::map< wxString, LIB_DATA > & globalLibs()=0
void cancelLoadingPlugins(const std::vector< LOADING_KEY > &aSnapshot)
Asks the plugins in aSnapshot to end their load early, so abortLoad() does not wait out their waits.
virtual LIBRARY_TABLE_TYPE Type() const =0
The type of library table this adapter works with.
void retireAllLibraries()
For derived destructors, while globalLibs() is still resolvable and plugin worker threads can still c...
bool DeleteLibrary(const wxString &aNickname)
Deletes the given library from disk if it exists; returns true if deleted.
void asyncLoadLocked()
The body of AsyncLoad(), run with m_loadMutex already held by the caller.
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::optional< wxString > FindLibraryByURI(const wxString &aURI) const
std::mutex m_loadingMutex
Libraries an async worker is enumerating, so a cancel reaches exactly those plugins.
std::vector< LOADING_KEY > loadingSnapshot()
The libraries an async worker is enumerating right now.
LIB_DATA & queuedRowEntry(const LOAD_WORK &aWork)
The cache entry of a queued library, created and owned by this adapter when it is missing,...
void LeakCachesAtExit()
Ends every load, then marks the cache of each library this adapter owns to be left unfreed when its p...
std::shared_mutex m_librariesMutex
std::set< LOADING_KEY > m_loading
LIBRARY_MANAGER & Manager() const
std::optional< wxString > GetLibraryDescription(const wxString &aNickname) const
virtual LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row)=0
Creates a concrete plugin for the given row.
void GlobalTablesChanged(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Notify the adapter that the global library tables have changed.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::optional< LIBRARY_TABLE_ROW * > GetRow(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::GetRow but filtered to the LIBRARY_TABLE_TYPE of this adapter.
std::map< wxString, LIB_DATA > m_libraries
virtual IO_BASE * plugin(const LIB_DATA *aRow)=0
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
virtual void ProjectChanged()
Notify the adapter that the active project has changed.
LIBRARY_RESULT< LIB_DATA * > loadFromScope(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope, std::map< wxString, LIB_DATA > &aTarget, std::shared_mutex &aMutex)
virtual std::optional< LIB_STATUS > CheckLibrary(LIB_DATA *aLib)
Validates that a library is loadable. May not necessarily load the library!
std::vector< std::future< void > > m_futures
void withPlugin(const LOADING_KEY &aKey, const std::function< void(IO_BASE &)> &aAction)
Runs aAction on the plugin of a library when it is still in the map.
static std::recursive_mutex & pluginMutex(const wxString &aNickname)
Serializes access to a library's shared plugin instance so its single mutable cache is not raced by c...
std::shared_mutex & scopeMutex(LIBRARY_TABLE_SCOPE aScope)
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
void resumeLoadingPlugins(const std::vector< LOADING_KEY > &aCancelled)
Tells the plugins cancelLoadingPlugins() reached that the loads they were asked to end have ended.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
virtual bool IsWritable(const wxString &aNickname) const
Return true if the given nickname exists and is not a read-only library.
std::vector< std::pair< wxString, LIB_STATUS > > GetLibraryStatuses() const
Returns a list of all library nicknames and their status (even if they failed to load)
std::atomic< size_t > m_loadTotal
void ProjectTablesReloaded(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Complements ProjectTablesChanged by erasing project-scope cache entries whose nicknames no longer app...
void abortLoad(bool aCancelPlugins=false)
Aborts any async load in progress; blocks until fully done aborting.
void RestartAsyncLoad()
Queues again the libraries a cancelling abort left INVALID, when one has cut a load short since the l...
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
std::optional< LIB_STATUS > LoadLibraryEntry(const wxString &aNickname)
Synchronously loads the named library to LOADED state.
bool m_rearmPending
Set when a cancelling abort cut a worker short, and cleared only by an AsyncLoad() that starts.
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
void ReloadTables(LIBRARY_TABLE_SCOPE aScope, std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
static wxString StockTableTokenizedURI(LIBRARY_TABLE_TYPE aType)
static wxString ExpandURI(const wxString &aShortURI, const PROJECT &aProject)
const PROJECT *const m_project
std::mutex m_rowCacheMutex
void RestartAsyncLoads()
Queues again the libraries that any cancelling abort left INVALID, for every registered adapter.
void ClearLibOverride(const wxString &aTablePath, const wxString &aNickname)
Removes any override for a library that no longer needs one.
std::optional< LIBRARY_MANAGER_ADAPTER * > Adapter(LIBRARY_TABLE_TYPE aType) const
void applyLibOverrides(LIBRARY_TABLE &aTable)
Applies user overrides (disabled/hidden) to rows of a read-only nested table.
std::map< wxString, std::unique_ptr< LIBRARY_TABLE > > m_childTables
Map of full URI to table object for tables that are referenced by global or project tables.
void loadNestedTables(LIBRARY_TABLE &aTable)
std::map< ROW_CACHE_KEY, LIBRARY_TABLE_ROW * > m_rowCache
bool RemoveAdapter(LIBRARY_TABLE_TYPE aType, LIBRARY_MANAGER_ADAPTER *aAdapter)
const PROJECT & Project() const
void ReloadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
static bool UrisAreEquivalent(const wxString &aURI1, const wxString &aURI2)
void AbortAsyncLoads(bool aCancelPlugins=false)
Abort any async library loading operations in progress.
std::mutex m_adaptersMutex
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void ApplyLibOverrides(LIBRARY_TABLE &aTable)
Applies stored user overrides (disabled/hidden) to rows of a read-only table.
void RegisterAdapter(LIBRARY_TABLE_TYPE aType, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > &&aAdapter)
static wxString DefaultGlobalTablePath(LIBRARY_TABLE_TYPE aType)
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false)
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
static std::vector< LIBRARY_TABLE_TYPE > InvalidGlobalTables()
void createEmptyTable(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
std::optional< LIB_STATUS > LoadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname)
Synchronously loads the named library to LOADED state for the given type.
static bool GlobalTablesValid()
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_TABLE > > m_projectTables
std::optional< LIBRARY_TABLE_ROW * > FindRowByURI(LIBRARY_TABLE_TYPE aType, const wxString &aUri, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
void SetLibOverride(const wxString &aTablePath, const wxString &aNickname, bool aDisabled, bool aHidden)
Set a user override for a library in a read-only nested table.
bool IsProjectScoped() const
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
static bool CreateGlobalTable(LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries)
void loadTables(const wxString &aTablePath, LIBRARY_TABLE_SCOPE aScope, std::vector< LIBRARY_TABLE_TYPE > aTablesToLoad={})
static wxString tableFileName(LIBRARY_TABLE_TYPE aType)
static bool IsPcmManagedRow(const LIBRARY_TABLE_ROW &aRow)
Return true if a library table row was added by the Plugin and Content Manager.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_TABLE > > m_tables
void LeakCachesAtExit()
Marks the library caches of every adapter to be left unfreed when their plugins are destroyed.
static wxString StockTableReferenceURI(LIBRARY_TABLE_TYPE aType)
static wxString StockTablePath(LIBRARY_TABLE_TYPE aType)
std::optional< LIBRARY_TABLE_ROW * > GetRow(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
void ProjectChanged()
Notify all adapters that the project has changed.
static bool IsTableValid(const wxString &aPath)
LIBRARY_MANAGER(const PROJECT *aProject=nullptr)
An explicit project is borrowed until this manager and its adapters are destroyed.
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > > m_adapters
void SetNickname(const wxString &aNickname)
void SetOk(bool aOk=true)
void SetType(const wxString &aType)
bool Disabled() const
void SetErrorDescription(const wxString &aDescription)
std::map< std::string, UTF8 > GetOptionsMap() const
void SetDescription(const wxString &aDescription)
const wxString & Type() const
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
bool IsOk() const
const wxString & URI() const
const wxString & Nickname() const
bool IsReadOnly() const
Returns true if the underlying file exists but is not writable.
LIBRARY_TABLE_ROW & InsertRow()
Builds a new row and inserts it at the end of the table; returning a reference to the row.
const wxString & Path() const
LIBRARY_TABLE_SCOPE Scope() const
bool HasRow(const wxString &aNickname) const
bool HasRowWithURI(const wxString &aUri, const PROJECT &aProject, bool aSubstituted=false) const
Returns true if the given (fully-expanded) URI exists as a library in this table.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
bool IsOk() const
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition paths.cpp:317
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition paths.cpp:586
LIBRARY_TABLE * m_designBlockTable
std::set< LIBRARY_TABLE * > m_modified
LIBRARY_MANAGER & m_manager
std::set< LIBRARY_TABLE * > Modified() const
const PROJECT & m_project
PCM_LIB_TRAVERSER(const wxString &aBasePath, LIBRARY_MANAGER &aManager, const wxString &aPrefix)
void ensureUnique(LIBRARY_TABLE *aTable, const wxString &aBaseName, wxString &aNickname) const
LIBRARY_TABLE * m_symbolTable
wxDirTraverseResult OnDir(const wxString &dirPath) override
Handles footprint library and design block library directories, minimum nest level 3.
void addRowIfNecessary(LIBRARY_TABLE *aTable, const wxFileName &aSource, ADD_MODE aMode, int aExtensionLength)
LIBRARY_TABLE * m_fpTable
wxDirTraverseResult OnFile(const wxString &aFilePath) override
Handles symbol library files, minimum nest level 2.
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition pgm_base.cpp:846
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:124
bool Finish() override
Runs prettification over the buffered bytes, writes them to the sibling temp file,...
Definition richio.cpp:710
A progress reporter interface for use in multi-threaded environments.
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
Container for project specific data.
Definition project.h:63
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
static void ResolvePossibleSymlinks(wxFileName &aFilename)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject, bool aAllowEscapes)
Replace any environment variable & text variable references with their values.
Definition common.cpp:777
#define _(s)
Functions related to environment variables, including help functions.
static const std::string KiCadDesignBlockLibPathExtension
static const std::string SymbolLibraryTableFileName
static const std::string DesignBlockLibraryTableFileName
static const std::string FootprintLibraryTableFileName
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
std::string message
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
PROJECT & Prj()
Definition kicad.cpp:730
LOAD_STATUS
Status of a library load managed by a library adapter.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_TYPE
LIBRARY_TABLE_SCOPE
KICOMMON_API bool IsVersionedEnvVar(const wxString &aName, const wxString &aBaseName)
Test whether a name is a versioned KiCad environment variable for the given base, i....
Definition env_vars.cpp:88
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
Definition env_vars.cpp:104
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Definition env_vars.cpp:79
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
void StrNumSort(T &aList, CMP_T aCaseSensitivity)
Sort a container of wxString objects, in place, using the StrNumCmp() function.
@ IGNORE_CASE
wxString message
A library queued by AsyncLoad(), with its URI resolved on the calling thread.
std::vector< LIBRARY_TABLE > tables
Storage for an actual loaded library (including library content owned by the plugin)
LIB_STATUS status
std::unique_ptr< IO_BASE > plugin
Never destroyed while holding the map's mutex; a plugin's worker thread may be blocked on it.
const LIBRARY_MANAGER_ADAPTER * global_owner
const LIBRARY_TABLE_ROW * row
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
LOAD_STATUS load_status
const std::string table
std::vector< DATABASE_CONNECTION::ROW > rows
std::string path
VECTOR2I end
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:27
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition wx_filename.h:35