release 0.1.7
[liba.git] / cmake / FindQuickJS.cmake
blob7ff91093a69d5be23c690e9cc6d4134d066738cd
1 # .rst:
2 # FindQuickJS
3 # -----------
5 # Find quickjs include dirs and libraries.
7 # Imported Targets
8 # ^^^^^^^^^^^^^^^^
10 # This module defines the following :ref:`Imported Targets <Imported Targets>`:
12 # ``quickjs``
14 # Result Variables
15 # ^^^^^^^^^^^^^^^^
17 # This module defines the following variables:
19 # ``QUICKJS_FOUND``
21 # ``QUICKJS_INCLUDE_DIR``
23 # ``QUICKJS_LIBRARIES``
25 # ``QUICKJS_VERSION``
27 # ``QJSC_EXECUTABLE``
29 # ``QJS_EXECUTABLE``
31 # Cache variables
32 # ^^^^^^^^^^^^^^^
34 # Search results are saved persistently in CMake cache entries:
36 # ``QUICKJS_INCLUDE_DIR``
38 # ``QUICKJS_LIBRARY_IPO``
40 # ``QUICKJS_LIBRARY``
42 # ``QJSC_EXECUTABLE``
44 # ``QJS_EXECUTABLE``
46 include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
47 find_path(QUICKJS_INCLUDE_DIR NAMES quickjs.h PATH_SUFFIXES quickjs)
48 mark_as_advanced(QUICKJS_INCLUDE_DIR)
50 find_program(QJSC_EXECUTABLE NAMES qjsc)
51 mark_as_advanced(QJSC_EXECUTABLE)
53 find_program(QJS_EXECUTABLE NAMES qjs)
54 mark_as_advanced(QJS_EXECUTABLE)
56 if(EXISTS "${QJS_EXECUTABLE}")
57   execute_process(COMMAND ${QJS_EXECUTABLE} -h OUTPUT_VARIABLE QUICKJS_VERSION)
58 elseif(EXISTS "${QJSC_EXECUTABLE}")
59   execute_process(COMMAND ${QJSC_EXECUTABLE} -h OUTPUT_VARIABLE QUICKJS_VERSION)
60 endif()
61 if(QUICKJS_VERSION)
62   string(REGEX REPLACE ".*version[ ]+([^\n ]+).*" "\\1" QUICKJS_VERSION "${QUICKJS_VERSION}")
63 endif()
65 find_library(QUICKJS_LIBRARY_IPO NAMES quickjs.lto PATH_SUFFIXES quickjs)
66 find_library(QUICKJS_LIBRARY NAMES quickjs PATH_SUFFIXES quickjs)
67 mark_as_advanced(QUICKJS_LIBRARY_IPO QUICKJS_LIBRARY)
68 if(EXISTS "${QUICKJS_LIBRARY}" OR EXISTS "${QUICKJS_LIBRARY_IPO}")
69   if(EXISTS "${QUICKJS_LIBRARY_IPO}")
70     list(APPEND QUICKJS_LIBRARIES ${QUICKJS_LIBRARY_IPO})
71   elseif(EXISTS "${QUICKJS_LIBRARY}")
72     list(APPEND QUICKJS_LIBRARIES ${QUICKJS_LIBRARY})
73   endif()
74   if(UNIX)
75     list(APPEND QUICKJS_LIBRARIES m ${CMAKE_DL_LIBS})
76   endif()
77   find_library(QUICKJS_PTHREAD_LIBRARY pthread)
78   mark_as_advanced(QUICKJS_PTHREAD_LIBRARY)
79   if(QUICKJS_PTHREAD_LIBRARY)
80     list(APPEND QUICKJS_LIBRARIES pthread)
81   endif()
82 endif()
84 find_package_handle_standard_args(QuickJS
85   FOUND_VAR
86     QUICKJS_FOUND
87   REQUIRED_VARS
88     QUICKJS_LIBRARIES
89     QUICKJS_INCLUDE_DIR
90     QJSC_EXECUTABLE
91     QJS_EXECUTABLE
92   VERSION_VAR
93     QUICKJS_VERSION
96 if(NOT TARGET quickjs AND QUICKJS_FOUND)
97   set(libraries ${QUICKJS_LIBRARIES})
98   if(EXISTS "${QUICKJS_LIBRARY_IPO}" OR EXISTS "${QUICKJS_LIBRARY}")
99     add_library(quickjs UNKNOWN IMPORTED)
100     set_target_properties(quickjs PROPERTIES
101       INTERFACE_INCLUDE_DIRECTORIES "${QUICKJS_INCLUDE_DIR}"
102       IMPORTED_LINK_INTERFACE_LANGUAGES C
103     )
104     if(EXISTS "${QUICKJS_LIBRARY_IPO}")
105       list(REMOVE_ITEM libraries ${QUICKJS_LIBRARY_IPO})
106       set_property(TARGET quickjs PROPERTY
107         IMPORTED_LOCATION "${QUICKJS_LIBRARY_IPO}"
108       )
109     elseif(EXISTS "${QUICKJS_LIBRARY}")
110       list(REMOVE_ITEM libraries ${QUICKJS_LIBRARY})
111       set_property(TARGET quickjs PROPERTY
112         IMPORTED_LOCATION "${QUICKJS_LIBRARY}"
113       )
114     endif()
115     set_property(TARGET quickjs APPEND PROPERTY
116       INTERFACE_LINK_LIBRARIES ${libraries}
117     )
118   endif()
119   set(libraries)
120 endif()