UsewxWidgets.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. UsewxWidgets
  5. ------------
  6. This module serves as a convenience wrapper for using the wxWidgets
  7. library (formerly known as wxWindows) and propagates its usage requirements,
  8. such as library directories, include directories, and compiler flags, into
  9. the current directory scope for use by targets.
  10. Load this module in a CMake project with:
  11. .. code-block:: cmake
  12. include(UsewxWidgets)
  13. This module calls :command:`include_directories` and
  14. :command:`link_directories`, sets compile definitions for the current
  15. directory and appends some compile flags to use wxWidgets library after
  16. calling the :module:`find_package(wxWidgets) <FindwxWidgets>`.
  17. Examples
  18. ^^^^^^^^
  19. Include this module in a project after finding wxWidgets to configure its
  20. usage requirements:
  21. .. code-block:: cmake
  22. :caption: ``CMakeLists.txt``
  23. # Note that for MinGW users the order of libraries is important.
  24. find_package(wxWidgets COMPONENTS net gl core base)
  25. add_library(example example.cxx)
  26. if(wxWidgets_FOUND)
  27. include(UsewxWidgets)
  28. # Link wxWidgets libraries for each dependent executable/library target.
  29. target_link_libraries(example PRIVATE ${wxWidgets_LIBRARIES})
  30. endif()
  31. As of CMake 3.27, a better approach is to link only the
  32. :module:`wxWidgets::wxWidgets <FindwxWidgets>` imported target to specific
  33. targets that require it, rather than including this module. Imported
  34. targets provide better control of the package usage properties, such as
  35. include directories and compile flags, by applying them only to the targets
  36. they are linked to, avoiding unnecessary propagation to all targets in the
  37. current directory.
  38. .. code-block:: cmake
  39. :caption: ``CMakeLists.txt``
  40. find_package(wxWidgets COMPONENTS net gl core base)
  41. add_library(example example.cxx)
  42. # Link the imported target for each dependent executable/library target.
  43. target_link_libraries(example PRIVATE wxWidgets::wxWidgets)
  44. See Also
  45. ^^^^^^^^
  46. * The :module:`FindwxWidgets` module to find wxWidgets.
  47. #]=======================================================================]
  48. # Author: Jan Woetzel <jw -at- mip.informatik.uni-kiel.de>
  49. if (wxWidgets_FOUND)
  50. if (wxWidgets_INCLUDE_DIRS)
  51. if(wxWidgets_INCLUDE_DIRS_NO_SYSTEM)
  52. include_directories(${wxWidgets_INCLUDE_DIRS})
  53. else()
  54. include_directories(SYSTEM ${wxWidgets_INCLUDE_DIRS})
  55. endif()
  56. endif()
  57. if (wxWidgets_LIBRARY_DIRS)
  58. link_directories(${wxWidgets_LIBRARY_DIRS})
  59. endif()
  60. if (wxWidgets_DEFINITIONS)
  61. set_property(DIRECTORY APPEND
  62. PROPERTY COMPILE_DEFINITIONS ${wxWidgets_DEFINITIONS})
  63. endif()
  64. if (wxWidgets_DEFINITIONS_DEBUG)
  65. set_property(DIRECTORY APPEND
  66. PROPERTY COMPILE_DEFINITIONS_DEBUG ${wxWidgets_DEFINITIONS_DEBUG})
  67. endif()
  68. if (wxWidgets_CXX_FLAGS)
  69. # Flags are expected to be a string here, not a list.
  70. string(REPLACE ";" " " wxWidgets_CXX_FLAGS_str "${wxWidgets_CXX_FLAGS}")
  71. string(APPEND CMAKE_CXX_FLAGS " ${wxWidgets_CXX_FLAGS_str}")
  72. unset(wxWidgets_CXX_FLAGS_str)
  73. endif()
  74. else ()
  75. message("wxWidgets requested but not found.")
  76. endif()