Frameworks/Epics/kdelibs cleanups/link private details: Difference between revisions

From KDE Community Wiki
(Created page with "<pre> Find out what should be part of the link interface and what should not be. If a framework uses a library, but does not contain any of its symbols in its API, then it ca...")
 
(Make page easier to read)
 
Line 1: Line 1:
<pre>
Find out what should be part of the link interface and what should not  
Find out what should be part of the link interface and what should not  
be.  
be.  
Line 8: Line 7:
See how this works in kcoreaddons (edited):
See how this works in kcoreaddons (edited):


<pre>
target_link_libraries(kcoreaddons
target_link_libraries(kcoreaddons
   LINK_PUBLIC ${QT_QTCORE_LIBRARY}
   LINK_PUBLIC ${QT_QTCORE_LIBRARY}
   LINK_PRIVATE pthread
   LINK_PRIVATE pthread
)
)
</pre>


pthread is linked to privately because none of the pthread symbols are in  
pthread is linked to privately because none of the pthread symbols are in  
Line 26: Line 27:
The way to figure it out is this:
The way to figure it out is this:


git grep "QT_.*_LIBRAR"
<pre>git grep "QT_.*_LIBRAR"</pre>


this gives you the target_link_libraries lines in frameworks.
this gives you the target_link_libraries lines in frameworks.
Line 32: Line 33:
For example in solid:
For example in solid:


 
<pre>
stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep "QT_.*_LIBRAR"
stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep "QT_.*_LIBRAR"
solid/CMakeLists.txt:target_link_libraries(solid ${QT_QTCORE_LIBRARY}  
solid/CMakeLists.txt:target_link_libraries(solid ${QT_QTCORE_LIBRARY}  
Line 57: Line 58:
${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY}  
${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY}  
${QT_QTTEST_LIBRARY})
${QT_QTTEST_LIBRARY})
 
</pre>


There are results there for the QtXml and the QtDBus libraries for example.  
There are results there for the QtXml and the QtDBus libraries for example.  
We can use grep to see if either is in the public API of solid:
We can use grep to see if either is in the public API of solid:


 
<pre>
stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep Xml
stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep Xml
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomDocument>
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomDocument>
Line 96: Line 97:
solid/backends/fakehw/fakemanager.h:    FakeDevice *parseDeviceElement(const  
solid/backends/fakehw/fakemanager.h:    FakeDevice *parseDeviceElement(const  
QDomElement &element);
QDomElement &element);
 
</pre>


QtXml is only an implementation detail of solid, so we can link to it  
QtXml is only an implementation detail of solid, so we can link to it  
Line 104: Line 105:


If in doubt, put what you are not certain about after LINK_PRIVATE.
If in doubt, put what you are not certain about after LINK_PRIVATE.
</pre>

Latest revision as of 15:54, 8 June 2012

Find out what should be part of the link interface and what should not be.

If a framework uses a library, but does not contain any of its symbols in its API, then it can be linked to privately.

See how this works in kcoreaddons (edited):

target_link_libraries(kcoreaddons
  LINK_PUBLIC ${QT_QTCORE_LIBRARY}
  LINK_PRIVATE pthread
)

pthread is linked to privately because none of the pthread symbols are in the kcoreaddons API. That means that if an application links to kcoreaddons they don't automatically have to link to pthread too.

Conversely, lots of the QtCore API is in the kcoreaddons API (returning QString, inheriting QObject etc), so downstreams using kcoreaddons also have to link to QtCore too.

CMake makes things easier for downstreams if we list our public and private link libraries separately, so we should do that.

The way to figure it out is this:

git grep "QT_.*_LIBRAR"

this gives you the target_link_libraries lines in frameworks.

For example in solid:

stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep "QT_.*_LIBRAR"
solid/CMakeLists.txt:target_link_libraries(solid ${QT_QTCORE_LIBRARY} 
${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTGUI_LIBRARY} 
${solid_OPTIONAL_LIBS} )
solid/CMakeLists.txt:target_link_libraries(solid LINK_INTERFACE_LIBRARIES 
${QT_CORE_LIBRARY} )
solid/CMakeLists.txt:target_link_libraries(solid_static ${QT_QTCORE_LIBRARY} 
${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTGUI_LIBRARY} 
${solid_OPTIONAL_LIBS})
tests/CMakeLists.txt:target_link_libraries(fakehardwaretest solid_static 
${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} 
${QT_QTTEST_LIBRARY} )
tests/CMakeLists.txt:target_link_libraries(halbasictest solid_static 
${KDEWIN_LIBRARIES} ${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} 
${QT_QTXML_LIBRARY} ${QT_QTTEST_LIBRARY} )
tests/CMakeLists.txt:target_link_libraries(solidhwtest ${QT_QTCORE_LIBRARY} 
${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBS} 
solid_static)
tests/CMakeLists.txt:target_link_libraries(solidmttest ${QT_QTCORE_LIBRARY} 
${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} ${QT_QTTEST_LIBRARY} ${LIBS} 
solid_static)
tests/CMakeLists.txt:#target_link_libraries(solidnettestdbusservice 
${QT_QTCORE_LIBRARY} ${QT_QTDBUS_LIBRARY} ${QT_QTXML_LIBRARY} 
${QT_QTTEST_LIBRARY})

There are results there for the QtXml and the QtDBus libraries for example. We can use grep to see if either is in the public API of solid:

stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep Xml
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomDocument>
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomElement>
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomNode>
solid/backends/fakehw/fakemanager.cpp:    QString machineXmlFile = xmlFile;
solid/backends/fakehw/fakemanager.cpp:    d->xmlFile = machineXmlFile;
solid/managerbase.cpp:    QString 
solidFakeXml(QString::fromLocal8Bit(qgetenv("SOLID_FAKEHW")));
solid/managerbase.cpp:    if (!solidFakeXml.isEmpty()) {
solid/managerbase.cpp:        m_backends << new 
Solid::Backends::Fake::FakeManager(0, solidFakeXml);
stephen@hal:~/dev/src/kf5/tier1/solid{frameworks}$ git grep Dom
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomDocument>
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomElement>
solid/backends/fakehw/fakemanager.cpp:#include <QtXml/QDomNode>
solid/backends/fakehw/fakemanager.cpp:    QDomDocument fakeDocument;
solid/backends/fakehw/fakemanager.cpp:        qWarning() << Q_FUNC_INFO << 
"Error while creating the QDomDocument." << endl;
solid/backends/fakehw/fakemanager.cpp:    QDomElement mainElement = 
fakeDocument.documentElement();
solid/backends/fakehw/fakemanager.cpp:    QDomNode node = 
mainElement.firstChild();
solid/backends/fakehw/fakemanager.cpp:        QDomElement tempElement = 
node.toElement();
solid/backends/fakehw/fakemanager.cpp:FakeDevice 
*FakeManager::parseDeviceElement(const QDomElement &deviceElement)
solid/backends/fakehw/fakemanager.cpp:    QDomNode propertyNode = 
deviceElement.firstChild();
solid/backends/fakehw/fakemanager.cpp:        QDomElement propertyElement = 
propertyNode.toElement();
solid/backends/fakehw/fakemanager.h:class QDomElement;
solid/backends/fakehw/fakemanager.h:    FakeDevice *parseDeviceElement(const 
QDomElement &element);

QtXml is only an implementation detail of solid, so we can link to it privately. Repeat for all link dependencies. If you can say for certain that one or some arguments to link_interface_libraries should be public, then go ahead and change them to use the LINK_PUBLIC and LINK_PRIVATE keywords.

If in doubt, put what you are not certain about after LINK_PRIVATE.