summaryrefslogtreecommitdiff
path: root/cmake/utils
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2025-12-23 11:59:45 +0100
committerSander Vrijders <sander@ouroboros.rocks>2026-01-07 10:00:06 +0100
commit48c294105f5123dc876fbad199ec1e0166d82a18 (patch)
treec49ce8ac75a7d63c10ea1ff960eeff750c680a8e /cmake/utils
parent145be13e8c18fcb39476d8f65fed23d82320f22f (diff)
downloadouroboros-48c294105f5123dc876fbad199ec1e0166d82a18.tar.gz
ouroboros-48c294105f5123dc876fbad199ec1e0166d82a18.zip
build: Refactor CMake modules
This moves the CMake build logic out of the source tree and splits it up into a more modular form. The tests now have a CMakeLists.txt file in their respective source directory. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
Diffstat (limited to 'cmake/utils')
-rw-r--r--cmake/utils/AddCompileFlags.cmake17
-rw-r--r--cmake/utils/CMakeUninstall.cmake.in29
-rw-r--r--cmake/utils/CompilerUtils.cmake13
-rw-r--r--cmake/utils/FindProtobufC.cmake74
4 files changed, 133 insertions, 0 deletions
diff --git a/cmake/utils/AddCompileFlags.cmake b/cmake/utils/AddCompileFlags.cmake
new file mode 100644
index 00000000..8f3877d9
--- /dev/null
+++ b/cmake/utils/AddCompileFlags.cmake
@@ -0,0 +1,17 @@
+# - MACRO_ADD_COMPILE_FLAGS(<_target> "flags...")
+
+# Copyright (c) 2006, Oswald Buddenhagen, <ossi@kde.org>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+
+macro(add_compile_flags _target _flg)
+
+ get_target_property(_flags ${_target} COMPILE_FLAGS)
+ if (_flags)
+ set(_flags "${_flags} ${_flg}")
+ else (_flags)
+ set(_flags "${_flg}")
+ endif (_flags)
+ set_target_properties(${_target} PROPERTIES COMPILE_FLAGS "${_flags}")
+
+endmacro(add_compile_flags)
diff --git a/cmake/utils/CMakeUninstall.cmake.in b/cmake/utils/CMakeUninstall.cmake.in
new file mode 100644
index 00000000..985b31b2
--- /dev/null
+++ b/cmake/utils/CMakeUninstall.cmake.in
@@ -0,0 +1,29 @@
+if(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
+ message(FATAL_ERROR "Cannot find install manifest: @CMAKE_BINARY_DIR@/install_manifest.txt")
+endif(NOT EXISTS "@CMAKE_BINARY_DIR@/install_manifest.txt")
+
+file(READ "@CMAKE_BINARY_DIR@/install_manifest.txt" files)
+string(REGEX REPLACE "\n" ";" files "${files}")
+foreach(file ${files})
+ message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
+ if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+ if(CMAKE_VERSION VERSION_LESS "3.28.0")
+ exec_program(
+ "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
+ OUTPUT_VARIABLE rm_out
+ RETURN_VALUE rm_retval
+ )
+ else()
+ execute_process(
+ COMMAND @CMAKE_COMMAND@ -E remove "$ENV{DESTDIR}${file}"
+ RESULT_VARIABLE rm_out
+ ERROR_VARIABLE rm_retval
+ )
+ endif ()
+ if(NOT "${rm_retval}" STREQUAL "" AND NOT "${rm_retval}" STREQUAL 0)
+ message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
+ endif(NOT "${rm_retval}" STREQUAL "" AND NOT "${rm_retval}" STREQUAL 0)
+ else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+ message(STATUS "File $ENV{DESTDIR}${file} does not exist.")
+ endif(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
+endforeach(file)
diff --git a/cmake/utils/CompilerUtils.cmake b/cmake/utils/CompilerUtils.cmake
new file mode 100644
index 00000000..7c8b022f
--- /dev/null
+++ b/cmake/utils/CompilerUtils.cmake
@@ -0,0 +1,13 @@
+include(CheckCCompilerFlag)
+
+function(test_and_set_c_compiler_flag_global _flag)
+
+ string(REGEX REPLACE "-" "_" _sflag ${_flag})
+ set(CMAKE_REQUIRED_FLAGS ${_flag})
+ check_c_compiler_flag(${_flag} COMPILER_SUPPORTS_FLAG_${_sflag})
+
+ if(COMPILER_SUPPORTS_FLAG_${_sflag})
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flag}" PARENT_SCOPE)
+ endif()
+
+endfunction(test_and_set_c_compiler_flag_global)
diff --git a/cmake/utils/FindProtobufC.cmake b/cmake/utils/FindProtobufC.cmake
new file mode 100644
index 00000000..ff892b5b
--- /dev/null
+++ b/cmake/utils/FindProtobufC.cmake
@@ -0,0 +1,74 @@
+function(PROTOBUF_GENERATE_C SRCS HDRS)
+ if (NOT ARGN)
+ message(SEND_ERROR "Error: PROTOBUF_GENERATE_C() called without any proto files")
+ return()
+ endif ()
+
+ if (PROTOBUF_GENERATE_C_APPEND_PATH)
+ # Create an include path for each file specified
+ foreach (FIL ${ARGN})
+ get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
+ get_filename_component(ABS_PATH ${ABS_FIL} PATH)
+ list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
+ if (${_contains_already} EQUAL -1)
+ list(APPEND _protobuf_include_path -I ${ABS_PATH})
+ endif ()
+ endforeach ()
+ else ()
+ set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
+ endif ()
+
+ set(${SRCS})
+ set(${HDRS})
+ foreach(FIL ${ARGN})
+ get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
+ get_filename_component(FIL_WE ${FIL} NAME_WE)
+
+ list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.c")
+ list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.h")
+
+ add_custom_command(
+ OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.c"
+ "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb-c.h"
+ COMMAND ${PROTOBUF_PROTOC_C_EXECUTABLE}
+ ARGS --c_out=${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
+ DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_C_EXECUTABLE}
+ COMMENT "Running C protocol buffer compiler on ${FIL}"
+ VERBATIM )
+ endforeach()
+
+ set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
+ set(${SRCS} ${${SRCS}} PARENT_SCOPE)
+ set(${HDRS} ${${HDRS}} PARENT_SCOPE)
+endfunction()
+
+# By default have PROTOBUF_GENERATE_C macro pass -I to protoc
+# for each directory where a proto file is referenced.
+if (NOT DEFINED PROTOBUF_GENERATE_C_APPEND_PATH)
+ set(PROTOBUF_GENERATE_C_APPEND_PATH TRUE)
+endif ()
+
+# Find library
+find_library(PROTOBUF_C_LIBRARY
+ NAMES libprotobuf-c.so libprotobuf-c libprotobuf-c.dylib
+ )
+mark_as_advanced(PROTOBUF_C_LIBRARY)
+
+# Find the include directory
+find_path(PROTOBUF_C_INCLUDE_DIR
+ google/protobuf-c/protobuf-c.h
+ )
+mark_as_advanced(PROTOBUF_C_INCLUDE_DIR)
+
+# Find the protoc-c Executable
+find_program(PROTOBUF_PROTOC_C_EXECUTABLE
+ NAMES protoc protoc-c
+ DOC "The Google Protocol Buffers C Compiler"
+ )
+mark_as_advanced(PROTOBUF_PROTOC_C_EXECUTABLE)
+
+find_package(PackageHandleStandardArgs)
+find_package_handle_standard_args(ProtobufC DEFAULT_MSG
+ PROTOBUF_C_LIBRARY PROTOBUF_C_INCLUDE_DIR PROTOBUF_PROTOC_C_EXECUTABLE)
+
+set(PROTOBUF_C_INCLUDE_DIRS ${PROTOBUF_C_INCLUDE_DIR})