diff options
author | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 15:39:57 -0700 |
---|---|---|
committer | Anas Nashif <anas.nashif@intel.com> | 2012-10-30 15:39:57 -0700 |
commit | 035c7fabc3b82cbc9a346c11abe2e9462b4c0379 (patch) | |
tree | 7e40f5a790eae329a8c5d3e59f046451767956ff /Tests/Plugin | |
download | cmake-035c7fabc3b82cbc9a346c11abe2e9462b4c0379.tar.gz cmake-035c7fabc3b82cbc9a346c11abe2e9462b4c0379.tar.bz2 cmake-035c7fabc3b82cbc9a346c11abe2e9462b4c0379.zip |
Imported Upstream version 2.8.9upstream/2.8.9
Diffstat (limited to 'Tests/Plugin')
-rw-r--r-- | Tests/Plugin/CMakeLists.txt | 88 | ||||
-rw-r--r-- | Tests/Plugin/check_mod_soname.cmake | 14 | ||||
-rw-r--r-- | Tests/Plugin/include/example.h | 25 | ||||
-rw-r--r-- | Tests/Plugin/src/example_exe.cxx | 60 | ||||
-rw-r--r-- | Tests/Plugin/src/example_exe.h.in | 6 | ||||
-rw-r--r-- | Tests/Plugin/src/example_mod_1.c | 22 |
6 files changed, 215 insertions, 0 deletions
diff --git a/Tests/Plugin/CMakeLists.txt b/Tests/Plugin/CMakeLists.txt new file mode 100644 index 000000000..31ca59c13 --- /dev/null +++ b/Tests/Plugin/CMakeLists.txt @@ -0,0 +1,88 @@ +cmake_minimum_required (VERSION 2.6) +PROJECT(Plugin) + +# Test per-target output directory properties. +SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/bin) +SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/lib/plugin) +SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/lib/static) + +# We need the dynamic loader support from KWSys to load the plugin in +# the executable. +SET(KWSYS_NAMESPACE kwsys) +SET(KWSYS_HEADER_ROOT ${Plugin_BINARY_DIR}/include) +SET(KWSYS_USE_DynamicLoader 1) +ADD_SUBDIRECTORY(${Plugin_SOURCE_DIR}/../../Source/kwsys src/kwsys) + +# Configure the location of plugins. +CONFIGURE_FILE(${Plugin_SOURCE_DIR}/src/example_exe.h.in + ${Plugin_BINARY_DIR}/include/example_exe.h @ONLY) + +# We need to include headers from the source tree and configured +# headers in the build tree. +INCLUDE_DIRECTORIES( + ${Plugin_BINARY_DIR}/include + ${Plugin_SOURCE_DIR}/include + ) + +# Create an executable that exports an API for use by plugins. +ADD_EXECUTABLE(example_exe src/example_exe.cxx) +SET_TARGET_PROPERTIES(example_exe PROPERTIES + ENABLE_EXPORTS 1 + OUTPUT_NAME example + # Test placing exe import library in unique directory. + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/exe + ) +TARGET_LINK_LIBRARIES(example_exe kwsys) + +# Create a plugin that uses the API provided by the executable. +# This module "links" to the executable to use the symbols. +ADD_LIBRARY(example_mod_1 MODULE src/example_mod_1.c) +TARGET_LINK_LIBRARIES(example_mod_1 example_exe) + + +IF(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG AND + "${CMAKE_C_CREATE_SHARED_MODULE}" MATCHES "SONAME_FLAG") + # Add a second plugin that should not have any soname. + ADD_LIBRARY(example_mod_2 MODULE src/example_mod_1.c) + TARGET_LINK_LIBRARIES(example_mod_2 example_exe) + SET_PROPERTY(TARGET example_mod_2 PROPERTY NO_SONAME 1) + + # Verify that targets export with proper IMPORTED SONAME properties. + EXPORT(TARGETS example_mod_1 example_mod_2 NAMESPACE exp_ + FILE ${CMAKE_CURRENT_BINARY_DIR}/mods.cmake) + INCLUDE(${CMAKE_CURRENT_BINARY_DIR}/mods.cmake) + GET_PROPERTY(configs TARGET exp_example_mod_1 PROPERTY IMPORTED_CONFIGURATIONS) + FOREACH(c ${configs}) + STRING(TOUPPER "${c}" CONFIG) + GET_PROPERTY(soname1 TARGET exp_example_mod_1 PROPERTY IMPORTED_SONAME_${CONFIG}) + GET_PROPERTY(soname2 TARGET exp_example_mod_2 PROPERTY IMPORTED_NO_SONAME_${CONFIG}) + IF(soname1) + MESSAGE(STATUS "exp_example_mod_1 has IMPORTED_SONAME_${CONFIG} as expected: ${soname1}") + ELSE() + MESSAGE(SEND_ERROR "exp_example_mod_1 does not have IMPORTED_SONAME_${CONFIG} but should") + ENDIF() + IF(soname2) + MESSAGE(STATUS "exp_example_mod_2 has IMPORTED_NO_SONAME_${CONFIG} as expected: ${soname2}") + ELSE() + MESSAGE(SEND_ERROR "exp_example_mod_2 does not have IMPORTED_NO_SONAME_${CONFIG} but should") + ENDIF() + ENDFOREACH() + + # Parse the binary to check for SONAME if possible. + IF("${CMAKE_EXECUTABLE_FORMAT}" MATCHES "ELF") + FIND_PROGRAM(READELF_EXE readelf) + IF(READELF_EXE) + ADD_CUSTOM_TARGET(check_mod_soname ALL COMMAND + ${CMAKE_COMMAND} -Dreadelf=${READELF_EXE} + -Dmod1=$<TARGET_FILE:example_mod_1> + -Dmod2=$<TARGET_FILE:example_mod_2> + -P ${CMAKE_CURRENT_SOURCE_DIR}/check_mod_soname.cmake + ) + ADD_DEPENDENCIES(check_mod_soname example_mod_1 example_mod_2) + ENDIF() + ENDIF() +ENDIF() + +# TODO: +# - create a plugin that links to a static lib +# - create a plugin that links to a shared lib diff --git a/Tests/Plugin/check_mod_soname.cmake b/Tests/Plugin/check_mod_soname.cmake new file mode 100644 index 000000000..3737b450d --- /dev/null +++ b/Tests/Plugin/check_mod_soname.cmake @@ -0,0 +1,14 @@ +execute_process(COMMAND ${readelf} -d ${mod1} OUTPUT_FILE ${mod1}.readelf.txt) +execute_process(COMMAND ${readelf} -d ${mod2} OUTPUT_FILE ${mod2}.readelf.txt) +file(STRINGS ${mod1}.readelf.txt soname1 REGEX "\\(SONAME\\)") +file(STRINGS ${mod2}.readelf.txt soname2 REGEX "\\(SONAME\\)") +if(soname1) + message(STATUS "${mod1} has soname as expected: ${soname1}") +else() + message(FATAL_ERROR "${mod1} has no soname but should:\n ${soname1}") +endif() +if(soname2) + message(FATAL_ERROR "${mod2} has soname but should not:\n ${soname2}") +else() + message(STATUS "${mod2} has no soname as expected") +endif() diff --git a/Tests/Plugin/include/example.h b/Tests/Plugin/include/example.h new file mode 100644 index 000000000..1d7e8c015 --- /dev/null +++ b/Tests/Plugin/include/example.h @@ -0,0 +1,25 @@ +#ifndef example_h +#define example_h + +#if defined(_WIN32) || defined(__CYGWIN__) +# if defined(example_exe_EXPORTS) +# define EXAMPLE_EXPORT __declspec(dllexport) +# else +# define EXAMPLE_EXPORT __declspec(dllimport) +# endif +#else +# define EXAMPLE_EXPORT +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +EXAMPLE_EXPORT int example_exe_function(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Tests/Plugin/src/example_exe.cxx b/Tests/Plugin/src/example_exe.cxx new file mode 100644 index 000000000..d2c52052c --- /dev/null +++ b/Tests/Plugin/src/example_exe.cxx @@ -0,0 +1,60 @@ +#include <example.h> + +#include <example_exe.h> + +#include <kwsys/DynamicLoader.hxx> +#include <kwsys/ios/iostream> +#include <kwsys/stl/string> + +#include <stdio.h> + +// Implement the ABI used by plugins. +extern "C" int example_exe_function() +{ + kwsys_ios::cout << "hello" << kwsys_ios::endl; + return 123; +} + +#ifdef CMAKE_INTDIR +# define CONFIG_DIR "/" CMAKE_INTDIR +#else +# define CONFIG_DIR "" +#endif + +int main() +{ + kwsys_stl::string libName = EXAMPLE_EXE_PLUGIN_DIR CONFIG_DIR "/"; + libName += kwsys::DynamicLoader::LibPrefix(); + libName += "example_mod_1"; + libName += kwsys::DynamicLoader::LibExtension(); + kwsys::DynamicLoader::LibraryHandle handle = + kwsys::DynamicLoader::OpenLibrary(libName.c_str()); + if(!handle) + { + kwsys_ios::cerr << "Could not open plugin \"" + << libName << "\"!" << kwsys_ios::endl; + return 1; + } + kwsys::DynamicLoader::SymbolPointer sym = + kwsys::DynamicLoader::GetSymbolAddress(handle, "example_mod_1_function"); + if(!sym) + { + kwsys_ios::cerr + << "Could not get plugin symbol \"example_mod_1_function\"!" + << kwsys_ios::endl; + return 1; + } +#ifdef __WATCOMC__ + int(__cdecl *f)(int) = (int(__cdecl *)(int))(sym); +#else + int(*f)(int) = reinterpret_cast<int(*)(int)>(sym); +#endif + if(f(456) != (123+456)) + { + kwsys_ios::cerr << "Incorrect return value from plugin!" + << kwsys_ios::endl; + return 1; + } + kwsys::DynamicLoader::CloseLibrary(handle); + return 0; +} diff --git a/Tests/Plugin/src/example_exe.h.in b/Tests/Plugin/src/example_exe.h.in new file mode 100644 index 000000000..62f0d9f02 --- /dev/null +++ b/Tests/Plugin/src/example_exe.h.in @@ -0,0 +1,6 @@ +#ifndef example_exe_h +#define example_exe_h + +#define EXAMPLE_EXE_PLUGIN_DIR "@CMAKE_LIBRARY_OUTPUT_DIRECTORY@" + +#endif diff --git a/Tests/Plugin/src/example_mod_1.c b/Tests/Plugin/src/example_mod_1.c new file mode 100644 index 000000000..1fc733845 --- /dev/null +++ b/Tests/Plugin/src/example_mod_1.c @@ -0,0 +1,22 @@ +#include <example.h> + +#include <stdio.h> + +#if defined(_WIN32) +# define MODULE_EXPORT __declspec(dllexport) +#else +# define MODULE_EXPORT +#endif + +#ifdef __WATCOMC__ +# define MODULE_CCONV __cdecl +#else +# define MODULE_CCONV +#endif + +MODULE_EXPORT int MODULE_CCONV example_mod_1_function(int n) +{ + int result = example_exe_function() + n; + printf("world\n"); + return result; +} |