Re: [CMake] Duplicating a shared library and replacing target link libraries

2016-12-05 Thread Chuck Atkins
>
> The library linking against "originalLibraryForward" has to be
> compiled from the original sources too. Actually performing the

linking step should be sufficient. Is there a way to accomplish that?
>

Object libraries are what you need here.  See
https://cmake.org/cmake/help/v3.7/manual/cmake-buildsystem.7.html#object-libraries
.  Essentially you'll do something like this:

add_library(fooObj OBJECT ${foo_Common_Sources})
add_library(foo $ ${foo_RealOnly_Sources})
add_library(fooMock $ ${foo_MockOnly_Sources})


The function does not work when the library passed to
> AddMockedLibrary() uses target_link_libraries() with IMPORTED targets
> from, e. g. calls to find_package(). In order to make creating the
> _MOCK-Library work I have to duplicate the original call to
> find_package() before calling AddMockedLibrary(). Is there a way to
> automate this as well?
>

You might be able to go down the route of creating an INTERFACE library
with all of the common properties necessary for both the actual and mock
libraries.  You could then add the interface lib as a public dependency
with target_link_libraries(foo PUBLIC fooInterface) and
target_link_libraries(fooMock PUBLIC fooInterface).  It's a bit convoluted
but I think it could work well here for you.

- Chuck
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

[CMake] Duplicating a shared library and replacing target link libraries

2016-12-04 Thread Rainer Poisel
Hello,

I am currently developing a test-framework that offers two versions of
a base-library:
  * the "originalLibrary" and
  * a library "originalLibraryForward" that has the same interface as
the "originalLibrary". Calls to "originalLibraryForward"-functions can
be forwarded to the "originalLibrary", however it is also possible to
install pre- and post-call hooks, implement different behavior,
reference counting, etc.

Other libraries linking against "originalLibrary" can be tested by
linking against "originalLibraryForward". In my build environment I
want to have both versions of a library: one linking against
"originalLibrary" and one linking against "originalLibraryForward".

In order to minimize the effort to create the library linking against
"originalLibraryForward", I wrote a function that "recreates" the
library, however the linker is instructed to link against
"originalLibraryForward" instead of "originalLibrary":

8<==
function(AddMockedLibrary libraryName)
get_target_property(sourceFiles ${libraryName} SOURCES)
get_target_property(linkLibs ${libraryName} LINK_LIBRARIES)
get_target_property(includeDirs ${libraryName} INCLUDE_DIRECTORIES)
get_target_property(compileDefinitions ${libraryName} COMPILE_DEFINITIONS)
get_target_property(compileOptions ${libraryName} COMPILE_OPTIONS)

add_library(${libraryName}_MOCK SHARED
${sourceFiles}
)

target_include_directories(${libraryName}_MOCK PRIVATE
${includeDirs}
)

list(REMOVE_ITEM linkLibs
originalLibrary
)
target_link_libraries(${libraryName}_MOCK PRIVATE
${linkLibs}
originalLibraryForward
)

if (compileOptions)
target_compile_options(${libraryName}_MOCK PRIVATE
${compileOptions}
)
endif()

if (compileDefinitions)
target_compile_definitions(${libraryName}_MOCK PRIVATE
${compileDefinitions}
)
endif()
endfunction()
8<==

Now to my problems: this approach works fairly well with two exceptions:

The library linking against "originalLibraryForward" has to be
compiled from the original sources too. Actually performing the
linking step should be sufficient. Is there a way to accomplish that?

The function does not work when the library passed to
AddMockedLibrary() uses target_link_libraries() with IMPORTED targets
from, e. g. calls to find_package(). In order to make creating the
_MOCK-Library work I have to duplicate the original call to
find_package() before calling AddMockedLibrary(). Is there a way to
automate this as well?

Thanks for your help,
  Rainer
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake