CMakeLists.txt 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. cmake_minimum_required (VERSION 2.6)
  2. project(Plugin)
  3. # Test per-target output directory properties.
  4. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/bin)
  5. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/lib/plugin)
  6. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${Plugin_BINARY_DIR}/lib/static)
  7. # We need the dynamic loader support from KWSys to load the plugin in
  8. # the executable.
  9. set(KWSYS_NAMESPACE kwsys)
  10. set(KWSYS_HEADER_ROOT ${Plugin_BINARY_DIR}/include)
  11. set(KWSYS_USE_DynamicLoader 1)
  12. add_subdirectory(${Plugin_SOURCE_DIR}/../../Source/kwsys src/kwsys)
  13. # Configure the location of plugins.
  14. configure_file(${Plugin_SOURCE_DIR}/src/example_exe.h.in
  15. ${Plugin_BINARY_DIR}/include/example_exe.h @ONLY)
  16. # We need to include headers from the source tree and configured
  17. # headers in the build tree.
  18. include_directories(
  19. ${Plugin_BINARY_DIR}/include
  20. ${Plugin_SOURCE_DIR}/include
  21. )
  22. # Create an executable that exports an API for use by plugins.
  23. add_executable(example_exe src/example_exe.cxx)
  24. set_target_properties(example_exe PROPERTIES
  25. ENABLE_EXPORTS 1
  26. OUTPUT_NAME example
  27. # Test placing exe import library in unique directory.
  28. ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/exe
  29. )
  30. target_link_libraries(example_exe kwsys)
  31. # Create a plugin that uses the API provided by the executable.
  32. # This module "links" to the executable to use the symbols.
  33. add_library(example_mod_1 MODULE src/example_mod_1.c)
  34. target_link_libraries(example_mod_1 example_exe)
  35. if(CMAKE_SHARED_LIBRARY_SONAME_C_FLAG AND
  36. "${CMAKE_C_CREATE_SHARED_MODULE}" MATCHES "SONAME_FLAG")
  37. # Add a second plugin that should not have any soname.
  38. add_library(example_mod_2 MODULE src/example_mod_1.c)
  39. target_link_libraries(example_mod_2 example_exe)
  40. set_property(TARGET example_mod_2 PROPERTY NO_SONAME 1)
  41. # Verify that targets export with proper IMPORTED SONAME properties.
  42. export(TARGETS example_mod_1 example_mod_2 NAMESPACE exp_
  43. FILE ${CMAKE_CURRENT_BINARY_DIR}/mods.cmake)
  44. include(${CMAKE_CURRENT_BINARY_DIR}/mods.cmake)
  45. get_property(configs TARGET exp_example_mod_1 PROPERTY IMPORTED_CONFIGURATIONS)
  46. foreach(c ${configs})
  47. string(TOUPPER "${c}" CONFIG)
  48. get_property(soname1 TARGET exp_example_mod_1 PROPERTY IMPORTED_SONAME_${CONFIG})
  49. get_property(soname2 TARGET exp_example_mod_2 PROPERTY IMPORTED_NO_SONAME_${CONFIG})
  50. if(soname1)
  51. message(STATUS "exp_example_mod_1 has IMPORTED_SONAME_${CONFIG} as expected: ${soname1}")
  52. else()
  53. message(SEND_ERROR "exp_example_mod_1 does not have IMPORTED_SONAME_${CONFIG} but should")
  54. endif()
  55. if(soname2)
  56. message(STATUS "exp_example_mod_2 has IMPORTED_NO_SONAME_${CONFIG} as expected: ${soname2}")
  57. else()
  58. message(SEND_ERROR "exp_example_mod_2 does not have IMPORTED_NO_SONAME_${CONFIG} but should")
  59. endif()
  60. endforeach()
  61. # Parse the binary to check for SONAME if possible.
  62. if("${CMAKE_EXECUTABLE_FORMAT}" MATCHES "ELF")
  63. find_program(READELF_EXE readelf)
  64. if(READELF_EXE)
  65. add_custom_target(check_mod_soname ALL COMMAND
  66. ${CMAKE_COMMAND} -Dreadelf=${READELF_EXE}
  67. -Dmod1=$<TARGET_FILE:example_mod_1>
  68. -Dmod2=$<TARGET_FILE:example_mod_2>
  69. -P ${CMAKE_CURRENT_SOURCE_DIR}/check_mod_soname.cmake
  70. )
  71. add_dependencies(check_mod_soname example_mod_1 example_mod_2)
  72. endif()
  73. endif()
  74. endif()
  75. # TODO:
  76. # - create a plugin that links to a static lib
  77. # - create a plugin that links to a shared lib