2
\$\begingroup\$

I'm using QScintilla in my project. There's no good FindQScintilla.cmake out there, so I'm writing one myself. However, I don't really know what I'm doing. This is my attempt; it works, but I don't know how it'll work in reality:

find_path(QSCINTILLA_INCLUDE_DIR
  NAMES Qsci qsciglobal.h
  PATHS "${Qt5Core_INCLUDE_DIRS}" /usr/include/x86_64-linux-gnu/qt5
  PATH_SUFFIXES Qsci
)

find_library(QSCINTILLA_LIBRARY
  NAMES qt5scintilla2 libqt5scintilla2
  PATHS ${Qt5Core_LIBRARIES}
)

include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(
  QScintilla
  FOUND_VAR QSCINTILLA_FOUND
  REQUIRED_VARS QSCINTILLA_INCLUDE_DIR QSCINTILLA_LIBRARY
  VERSION_VAR QSCINTILLA_VERSION
)

if(QSCINTILLA_LIBRARY AND QSCINTILLA_INCLUDE_DIR)
  set(QSCINTILLA_LIBRARIES ${QSCINTILLA_LIBRARY})
  set(QSCINTILLA_INCLUDE_DIRS ${QSCINTILLA_INCLUDE_DIR})
endif()

mark_as_advanced(QSCINTILLA_INCLUDE_DIR QSCINTILLA_LIBRARY)

My development environment is Ubuntu 16.04, but I want this package to work on all major platforms.

More specifically:

  • Am I missing anything?
  • How will this hold up on different operating systems?
    • I'm targeting Windows, Mac, and Linux, though if there's a quick and easy way I can target another OS then I'll do that too.
\$\endgroup\$
2
\$\begingroup\$

Overall I think you have all the major pieces there, but I would also taking your find module a step further and create a target if QScintilla is found. You can do this by adding the following:

if(QSCINTILLA_LIBRARY AND QSCINTILLA_INCLUDE_DIR)

  set(QSCINTILLA_LIBRARIES ${QSCINTILLA_LIBRARY})
  set(QSCINTILLA_INCLUDE_DIRS ${QSCINTILLA_INCLUDE_DIR})

  if(NOT TARGET QScintilla::QScintilla)
      add_library(QScintilla::QScintilla UNKNOWN IMPORTED)
      set_target_properties(QScintilla::QScintilla PROPERTIES
           IMPORTED_IMPLIB ${QSCINTILLA_LIBRARY}
           INTERFACE_INCLUDE_DIRECTORIES ${QSCINTILLA_INCLUDE_DIR})
  endif(NOT TARGET QScintilla::QSintilla)

endif(QSCINTILLA_LIBRARY AND QSCINTILLA_INCLUDE_DIR)

Now you can use the library like so: target_link_libraries(MyProject PUBLIC QScintilla::QScintilla). Feel free to change the imported target name.


One minor thing I would suggest (that you can see in the code above ^) it to have your if() conditions match inside the corresponding end() call. So if you have if(condition), the matching end() should also look like end(condition).


As for cross platform support it's difficult to say without trying it on multiple platforms. I'm not sure about your find_path call. The use of ${Qt5Core_INCLUDE_DIRS} should be portable enough but it's tough to say.

One other nuance with find modules that I personally find can be troublesome is that on windows, if the library is a shared library you need to find the path to the .lib file and the .dll since that binary file isn't necessarily on the user's environment path. This is more of a nice to have and something I do for all find modules I write, but I work mostly on windows anyway.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.