From: Guus Sliepen <guus@meshlink.io>
Date: Mon, 12 Dec 2022 19:19:24 +0000 (+0100)
Subject: Improve CMake build system.
X-Git-Url: http://git.meshlink.io/?a=commitdiff_plain;h=f4beb1b6273e193e6298b5c5f477070fe95b24f9;p=meshlink

Improve CMake build system.

- Fix project name.
- Let CMake decide whether to build static or shared library.
- Only enable testing if this is the main project.
- Enable C11 for the test stuite.
---

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ce082569..7e373524 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,4 +1,4 @@
-project(meshlink-tiny)
+project(meshlink)
 cmake_minimum_required(VERSION 3.12)
 
 set(THREADS_PREFER_PTHREAD_FLAG ON)
@@ -24,5 +24,8 @@ configure_file(cmake_config.h.in config.h)
 include_directories(${CMAKE_CURRENT_BINARY_DIR}/src)
 
 add_subdirectory(src)
-enable_testing()
-add_subdirectory(test)
+
+if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
+	enable_testing()
+	add_subdirectory(test)
+endif()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e9bb93f2..536b826d 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,7 @@
 #include(GenerateExportHeader)
 #generate_export_header(meshlink BASE_NAME meshlink)
 
-add_library(meshlink SHARED
+set(MESHLINK_SOURCES
 	adns.c
 	buffer.c
 	chacha-poly1305/chacha-poly1305.c
@@ -62,15 +62,19 @@ set(MESHLINK_PUBLIC_HEADERS
 	../include/meshlink++.h
 )
 
+add_library(meshlink ${MESHLINK_SOURCES})
+
 target_compile_features(meshlink PUBLIC c_std_11)
 target_include_directories(meshlink PUBLIC
 	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
 	$<INSTALL_INTERFACE:include/meshlink>
 )
 target_link_libraries(meshlink PRIVATE Threads::Threads)
-set_target_properties(meshlink PROPERTIES PUBLIC_HEADER "${MESHLINK_PUBLIC_HEADERS}")
-set_property(TARGET meshlink PROPERTY C_VISIBILITY_PRESET hidden)
-set_property(TARGET meshlink PROPERTY C_STANDARD 11)
+set_target_properties(meshlink PROPERTIES
+	PUBLIC_HEADER "${MESHLINK_PUBLIC_HEADERS}"
+	C_VISIBILITY_PRESET hidden
+	C_STANDARD 11
+)
 
 include(GNUInstallDirs)
 install(TARGETS meshlink
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 2d392350..dbb0f861 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -3,6 +3,7 @@ function(add_test_executable name)
 	target_link_libraries(${name} meshlink)
 	add_test(NAME ${name} COMMAND ${name})
 	set_tests_properties(${name} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 30)
+	target_compile_features(${name} PUBLIC c_std_11)
 endfunction()
 
 add_executable(stream stream.c)