Skip to content

Commit f69ce1e

Browse files
authored
Merge pull request #165 from hobbes1069/cmake-improvements
CMake config improvements:
2 parents 99903af + 17b536c commit f69ce1e

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

CMakeLists.txt

+39-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ option(MQTT_C_OpenSSL_SUPPORT "Build MQTT-C with OpenSSL support?" OFF)
66
option(MQTT_C_MbedTLS_SUPPORT "Build MQTT-C with mbed TLS support?" OFF)
77
option(MQTT_C_BearSSL_SUPPORT "Build MQTT-C with Bear SSL support?" OFF)
88
option(MQTT_C_EXAMPLES "Build MQTT-C examples?" ON)
9+
option(MQTT_C_INSTALL_EXAMPLES "Install MQTT-C examples?" OFF)
910
option(MQTT_C_TESTS "Build MQTT-C tests?" OFF)
1011

1112
list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
@@ -74,25 +75,41 @@ if(MQTT_C_EXAMPLES)
7475
add_executable(bio_publisher examples/bio_publisher.c)
7576
add_executable(openssl_publisher examples/openssl_publisher.c)
7677
endif()
77-
78+
if(MQTT_C_INSTALL_EXAMPLES)
79+
install(TARGETS bio_publisher openssl_publisher)
80+
endif()
7881
target_link_libraries(bio_publisher Threads::Threads mqttc)
7982
target_link_libraries(openssl_publisher Threads::Threads mqttc)
80-
elseif(MQTT_C_MbedTLS_SUPPORT)
83+
84+
elseif(MQTT_C_MbedTLS_SUPPORT)
8185
add_executable(mbedtls_publisher examples/mbedtls_publisher.c)
8286
target_link_libraries(mbedtls_publisher Threads::Threads mqttc ${MBEDX509_LIBRARY} ${MBEDCRYPTO_LIBRARY})
87+
if(MQTT_C_INSTALL_EXAMPLES)
88+
install(TARGETS mbedtls_publisher)
89+
endif()
90+
8391
elseif(MQTT_C_BearSSL_SUPPORT)
8492
add_executable(bearssl_publisher examples/bearssl_publisher.c)
8593
target_link_libraries(bearssl_publisher mqttc bearssl)
94+
if(MQTT_C_INSTALL_EXAMPLES)
95+
install(TARGETS bearssl_publisher)
96+
endif()
8697
else()
8798
add_executable(simple_publisher examples/simple_publisher.c)
8899
target_link_libraries(simple_publisher Threads::Threads mqttc)
89-
90-
add_executable(simple_subscriber examples/simple_subscriber.c)
91-
target_link_libraries(simple_subscriber Threads::Threads mqttc)
92-
93-
add_executable(reconnect_subscriber examples/reconnect_subscriber.c)
94-
target_link_libraries(reconnect_subscriber Threads::Threads mqttc)
100+
if(MQTT_C_INSTALL_EXAMPLES)
101+
install(TARGETS simple_publisher)
102+
endif()
95103
endif()
104+
105+
# Always install subscriber targets
106+
add_executable(simple_subscriber examples/simple_subscriber.c)
107+
target_link_libraries(simple_subscriber Threads::Threads mqttc)
108+
add_executable(reconnect_subscriber examples/reconnect_subscriber.c)
109+
target_link_libraries(reconnect_subscriber Threads::Threads mqttc)
110+
if(MQTT_C_INSTALL_EXAMPLES)
111+
install(TARGETS simple_subscriber reconnect_subscriber)
112+
endif()
96113
endif()
97114

98115
# Build tests
@@ -108,9 +125,21 @@ if(MQTT_C_TESTS)
108125
target_include_directories(tests PRIVATE ${CMOCKA_INCLUDE_DIR})
109126
endif()
110127

128+
# Handle multi-lib linux systems correctly and allow custom installation locations.
129+
if(UNIX)
130+
include(GNUInstallDirs)
131+
mark_as_advanced(CLEAR
132+
CMAKE_INSTALL_BINDIR
133+
CMAKE_INSTALL_LIBDIR
134+
CMAKE_INSTALL_INCLUDEDIR)
135+
else()
136+
set(CMAKE_INSTALL_LIBDIR "lib")
137+
set(CMAKE_INSTALL_INCLUDEDIR "include")
138+
endif()
139+
111140
# Install includes and library
112141
install(TARGETS mqttc
113-
DESTINATION lib
142+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
114143
)
115144
install(DIRECTORY include/
116-
DESTINATION include)
145+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

0 commit comments

Comments
 (0)