From bcbe9b46c5e882f422388069f1762fd3743b1ad6 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Thu, 11 Jan 2024 18:21:37 +0100 Subject: [PATCH 1/5] add cmake support --- .gitignore | 1 + CMakeLists.txt | 123 +++++++++++++++++++++++++++++++++++++++++++ cmake/config.h.in | 89 +++++++++++++++++++++++++++++++ cmake/version.h.in | 70 ++++++++++++++++++++++++ tests/CMakeLists.txt | 18 +++++++ 5 files changed, 301 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/config.h.in create mode 100644 cmake/version.h.in create mode 100644 tests/CMakeLists.txt diff --git a/.gitignore b/.gitignore index f537d5a6..b0997294 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ stamp-h? /tests/*.log /tests/*.trs /tests/main +CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..633a4662 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,123 @@ +cmake_minimum_required(VERSION 3.10) +project(libsigrokdecode LANGUAGES C + VERSION 0.6.0 + HOMEPAGE_URL "http://www.sigrok.org" +) + +set(CMAKE_C_STANDARD 99) + +set(PROJECT_BUG_URL "sigrok-devel@lists.sourceforge.net") +set(PROJECT_PACKAGE_STRING "${PROJECT_NAME} ${PROJECT_VERSION}") + +set(SR_LIB_VERSION_MAJOR 4) +set(SR_LIB_VERSION_MINOR 0) +set(SR_LIB_VERSION_PATCH 0) +set(CONF_HOST "${CMAKE_SYSTEM_PROCESSOR}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_VERSION}") + +execute_process( + COMMAND git log -1 --format=%h + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + +include(CheckIncludeFile) +# Check for the existence of the header files +check_include_file("inttypes.h" HAVE_INTTYPES_H) +check_include_file("stdint.h" HAVE_STDINT_H) +check_include_file("stdio.h" HAVE_STDIO_H) +check_include_file("stdlib.h" HAVE_STDLIB_H) +check_include_file("strings.h" HAVE_STRINGS_H) +check_include_file("string.h" HAVE_STRING_H) +check_include_file("sys/stat.h" HAVE_SYS_STAT_H) +check_include_file("sys/types.h" HAVE_SYS_TYPES_H) +check_include_file("unistd.h" HAVE_UNISTD_H) +check_include_file("dlfcn.h" HAVE_DLFCN_H) + +configure_file(cmake/config.h.in config.h) +configure_file(cmake/version.h.in version.h) + +# Source files +set(LIBSIGROKDECODE_SOURCES + srd.c + session.c + decoder.c + instance.c + log.c + util.c + exception.c + module_sigrokdecode.c + type_decoder.c + error.c + version.c +) + +# Additional headers +set(LIBSIGROKDECODE_HEADERS + libsigrokdecode.h + version.h + libsigrokdecode-internal.h +) + +# Check for Python +find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + +# Check for glib-2.0 +find_package(PkgConfig REQUIRED) +pkg_check_modules(GLIB REQUIRED glib-2.0>=2.3.4) + +# Check for Check (optional) +option(ENABLE_TESTS "Build with tests" ON) +if(ENABLE_TESTS) + enable_testing(true) # must be in the root file! + add_subdirectory(tests) +endif() + +# Check for IRMP (optional) +option(WITH_IRMP "Enable IRMP support" ON) +if(WITH_IRMP) + pkg_check_modules(LIBIRMP REQUIRED) +endif() + +# Set compiler flags +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wmissing-prototypes -Wshadow -Wformat=2 -Wno-format-nonliteral -Wfloat-equal") +if(WITH_IRMP) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWITH_IRMP") +endif() + +# Build the library +add_library(sigrokdecode SHARED ${LIBSIGROKDECODE_SOURCES} ${LIBSIGROKDECODE_HEADERS}) +target_link_libraries(sigrokdecode ${GLIB_LIBRARIES} ${LIBIRMP_LIBRARIES} ${Python3_LIBRARIES}) +target_include_directories(sigrokdecode PRIVATE ${CMAKE_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h +target_include_directories(sigrokdecode PUBLIC ${Python3_INCLUDE_DIRS}) # TODO: why this must be PUBLIC? +target_include_directories(sigrokdecode PUBLIC ${CMAKE_SOURCE_DIR}) + +# Install rules +install(TARGETS sigrokdecode DESTINATION lib) +install(FILES ${LIBSIGROKDECODE_HEADERS} DESTINATION include) + +# Autotools-like configuration summary +message(STATUS "libsigrokdecode configuration summary:") +message(STATUS " - Package version................. ${PROJECT_VERSION}") +message(STATUS " - Library ABI version............. ${SR_LIB_VERSION_MAJOR}:${SR_LIB_VERSION_MINOR}:${SR_LIB_VERSION_PATCH}") +message(STATUS " - Prefix.......................... ${CMAKE_INSTALL_PREFIX}") +message(STATUS " - Building as..................... ${CMAKE_BUILD_TYPE}") +message(STATUS " - Building for.................... ${CONF_HOST}") +#message(STATUS " - Building shared / static........ ON / OFF") + +# Display additional configuration information +message(STATUS "Compile configuration:") +message(STATUS " - C compiler...................... ${CMAKE_C_COMPILER}") +message(STATUS " - C compiler version.............. ${CMAKE_C_COMPILER_VERSION}") +message(STATUS " - C compiler flags................ ${CMAKE_C_FLAGS}") +message(STATUS "Detected libraries (required):") +message(STATUS " - glib-2.0........................ ${GLIB_VERSION}") +if(WITH_IRMP) + message(STATUS " - IRMP support library............ ${LIBIRMP_VERSION}") +endif() + +if(WITH_CHECK) + message(STATUS "Optional features:") + message(STATUS " - Check unit testing framework... ENABLED") +endif() + diff --git a/cmake/config.h.in b/cmake/config.h.in new file mode 100644 index 00000000..5ddc79d5 --- /dev/null +++ b/cmake/config.h.in @@ -0,0 +1,89 @@ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define if building universal (internal helper macro) */ +#undef AC_APPLE_UNIVERSAL_BUILD + +/* The canonical host libsigrokdecode will run on. */ +#define CONF_HOST "@CONF_HOST@" + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H @HAVE_DLFCN_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H @HAVE_INTTYPES_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H @HAVE_STDINT_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDIO_H @HAVE_STDIO_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H @HAVE_STDLIB_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H @HAVE_STRINGS_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H @HAVE_STRING_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H @HAVE_SYS_STAT_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H @HAVE_SYS_TYPES_H@ + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H @HAVE_UNISTD_H@ + +/* Define to the sub-directory where libtool stores uninstalled libraries. */ +#undef LT_OBJDIR + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "@PROJECT_BUG_URL@" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "@PROJECT_NAME@" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "@PROJECT_PACKAGE_STRING@" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "@PROJECT_NAME@" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "@PROJECT_HOMEPAGE_URL@" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "@PROJECT_VERSION@" + +/* Define to 1 if all of the C90 standard headers exist (not just the ones + required in a freestanding environment). This macro is provided for + backward compatibility; new code need not use it. */ +#undef STDC_HEADERS + +/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most + significant byte first (like Motorola and SPARC, unlike Intel). */ +#if defined AC_APPLE_UNIVERSAL_BUILD +# if defined __BIG_ENDIAN__ +# define WORDS_BIGENDIAN 1 +# endif +#else +# ifndef WORDS_BIGENDIAN +# undef WORDS_BIGENDIAN +# endif +#endif + +/* Number of bits in a file offset, on hosts where this is settable. */ +#undef _FILE_OFFSET_BITS + +/* Define for large files, on AIX-style hosts. */ +#undef _LARGE_FILES + +/* The targeted POSIX standard. */ +#ifndef _POSIX_C_SOURCE +# define _POSIX_C_SOURCE 200112L +#endif + +/* Define to empty if `const' does not conform to ANSI C. */ +#undef const diff --git a/cmake/version.h.in b/cmake/version.h.in new file mode 100644 index 00000000..d3121260 --- /dev/null +++ b/cmake/version.h.in @@ -0,0 +1,70 @@ +/* + * This file is part of the libsigrokdecode project. + * + * Copyright (C) 2010 Uwe Hermann + * Copyright (C) 2012 Bert Vermeulen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LIBSIGROKDECODE_VERSION_H +#define LIBSIGROKDECODE_VERSION_H + +/** + * @file + * + * Version number definitions and macros. + */ + +/** + * @ingroup grp_versions + * + * @{ + */ + +/* + * Package version macros (can be used for conditional compilation). + */ + +/** The libsigrokdecode package 'major' version number. */ +#define SRD_PACKAGE_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ + +/** The libsigrokdecode package 'minor' version number. */ +#define SRD_PACKAGE_VERSION_MINOR @PROJECT_VERSION_MINOR@ + +/** The libsigrokdecode package 'micro' version number. */ +#define SRD_PACKAGE_VERSION_MICRO @PROJECT_VERSION_PATCH@ + +/** The libsigrokdecode package version ("major.minor.micro") as string. */ +#define SRD_PACKAGE_VERSION_STRING "@PROJECT_VERSION@-git-@GIT_HASH@" + +/* + * Library/libtool version macros (can be used for conditional compilation). + */ + +/** The libsigrokdecode libtool 'current' version number. */ +#define SRD_LIB_VERSION_CURRENT @SR_LIB_VERSION_MAJOR@ + +/** The libsigrokdecode libtool 'revision' version number. */ +#define SRD_LIB_VERSION_REVISION @SR_LIB_VERSION_MINOR@ + +/** The libsigrokdecode libtool 'age' version number. */ +#define SRD_LIB_VERSION_AGE @SR_LIB_VERSION_PATCH@ + +/** The libsigrokdecode libtool version ("current:revision:age") as string. */ +#define SRD_LIB_VERSION_STRING "@SR_LIB_VERSION_MAJOR@:@SR_LIB_VERSION_MINOR@:@SR_LIB_VERSION_PATCH@" + +/** @} */ + +#endif diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..7ffae736 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,18 @@ +find_package(Check REQUIRED) + +add_executable(MainTest main.c + core.c + decoder.c + inst.c + session.c +) +target_link_libraries(MainTest sigrokdecode check) +target_link_libraries(MainTest ${GLIB_LIBRARIES}) +target_include_directories(MainTest PRIVATE ${GLIB_INCLUDE_DIRS}) +target_include_directories(MainTest PRIVATE ${CMAKE_BINARY_DIR}) # config.h / version.h + +target_compile_definitions(MainTest PRIVATE "-DDECODERS_TESTDIR=\"${CMAKE_SOURCE_DIR}/decoders\"") + +add_test(NAME MainTest COMMAND MainTest) + + From 9f04ca1c3cd6d7fcc0fa8e74f596050304356c13 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Thu, 11 Jan 2024 19:16:05 +0100 Subject: [PATCH 2/5] Use PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR because those paths are indipendend when inserting the project into another with FetchContent_Declare and FetchContent_MakeAvailable --- CMakeLists.txt | 4 ++-- tests/CMakeLists.txt | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 633a4662..37fbb896 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,9 +88,9 @@ endif() # Build the library add_library(sigrokdecode SHARED ${LIBSIGROKDECODE_SOURCES} ${LIBSIGROKDECODE_HEADERS}) target_link_libraries(sigrokdecode ${GLIB_LIBRARIES} ${LIBIRMP_LIBRARIES} ${Python3_LIBRARIES}) -target_include_directories(sigrokdecode PRIVATE ${CMAKE_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h +target_include_directories(sigrokdecode PRIVATE ${PROJECT_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h target_include_directories(sigrokdecode PUBLIC ${Python3_INCLUDE_DIRS}) # TODO: why this must be PUBLIC? -target_include_directories(sigrokdecode PUBLIC ${CMAKE_SOURCE_DIR}) +target_include_directories(sigrokdecode PUBLIC ${PROJECT_SOURCE_DIR}) # Install rules install(TARGETS sigrokdecode DESTINATION lib) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7ffae736..ba21a8a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,9 +9,9 @@ add_executable(MainTest main.c target_link_libraries(MainTest sigrokdecode check) target_link_libraries(MainTest ${GLIB_LIBRARIES}) target_include_directories(MainTest PRIVATE ${GLIB_INCLUDE_DIRS}) -target_include_directories(MainTest PRIVATE ${CMAKE_BINARY_DIR}) # config.h / version.h +target_include_directories(MainTest PRIVATE ${PROJECT_BINARY_DIR}) # config.h / version.h -target_compile_definitions(MainTest PRIVATE "-DDECODERS_TESTDIR=\"${CMAKE_SOURCE_DIR}/decoders\"") +target_compile_definitions(MainTest PRIVATE "-DDECODERS_TESTDIR=\"${PROJECT_SOURCE_DIR}/decoders\"") add_test(NAME MainTest COMMAND MainTest) From 91a2c5f1b0822cd1951cf07987bbc91e2afd669c Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Thu, 11 Jan 2024 19:29:15 +0100 Subject: [PATCH 3/5] make at least interface --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37fbb896..60f30c11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ endif() # Build the library add_library(sigrokdecode SHARED ${LIBSIGROKDECODE_SOURCES} ${LIBSIGROKDECODE_HEADERS}) target_link_libraries(sigrokdecode ${GLIB_LIBRARIES} ${LIBIRMP_LIBRARIES} ${Python3_LIBRARIES}) -target_include_directories(sigrokdecode PRIVATE ${PROJECT_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h +target_include_directories(sigrokdecode INTERFACE ${PROJECT_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h # must be interface, because version.h is included in libsigrokdecode.h which is public target_include_directories(sigrokdecode PUBLIC ${Python3_INCLUDE_DIRS}) # TODO: why this must be PUBLIC? target_include_directories(sigrokdecode PUBLIC ${PROJECT_SOURCE_DIR}) From e40903468267d242d6ed14f4d2e58f894ed999d5 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Wed, 31 Jan 2024 18:36:35 +0100 Subject: [PATCH 4/5] use file_set --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60f30c11..f3fa09fd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.23) # file_set needs at least 3.23 project(libsigrokdecode LANGUAGES C VERSION 0.6.0 HOMEPAGE_URL "http://www.sigrok.org" @@ -88,9 +88,14 @@ endif() # Build the library add_library(sigrokdecode SHARED ${LIBSIGROKDECODE_SOURCES} ${LIBSIGROKDECODE_HEADERS}) target_link_libraries(sigrokdecode ${GLIB_LIBRARIES} ${LIBIRMP_LIBRARIES} ${Python3_LIBRARIES}) -target_include_directories(sigrokdecode INTERFACE ${PROJECT_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # config.h / version.h # must be interface, because version.h is included in libsigrokdecode.h which is public +target_include_directories(sigrokdecode PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) target_include_directories(sigrokdecode PUBLIC ${Python3_INCLUDE_DIRS}) # TODO: why this must be PUBLIC? target_include_directories(sigrokdecode PUBLIC ${PROJECT_SOURCE_DIR}) +target_sources(sigrokdecode PUBLIC + FILE_SET HEADERS + BASE_DIRS ${PROJECT_BINARY_DIR} + TYPE HEADERS + FILES ${PROJECT_BINARY_DIR}/version.h ${PROJECT_BINARY_DIR}/config.h) # Install rules install(TARGETS sigrokdecode DESTINATION lib) From d1c98b3adc85a8a8cf90bde122fed7a5850a38fa Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Wed, 31 Jan 2024 18:43:53 +0100 Subject: [PATCH 5/5] cleanup install --- CMakeLists.txt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f3fa09fd..b9c988b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,8 +54,6 @@ set(LIBSIGROKDECODE_SOURCES # Additional headers set(LIBSIGROKDECODE_HEADERS - libsigrokdecode.h - version.h libsigrokdecode-internal.h ) @@ -88,18 +86,18 @@ endif() # Build the library add_library(sigrokdecode SHARED ${LIBSIGROKDECODE_SOURCES} ${LIBSIGROKDECODE_HEADERS}) target_link_libraries(sigrokdecode ${GLIB_LIBRARIES} ${LIBIRMP_LIBRARIES} ${Python3_LIBRARIES}) -target_include_directories(sigrokdecode PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) +target_include_directories(sigrokdecode PRIVATE ${PROJECT_BINARY_DIR} ${GLIB_INCLUDE_DIRS} ${LIBIRMP_INCLUDE_DIRS}) # for config.h target_include_directories(sigrokdecode PUBLIC ${Python3_INCLUDE_DIRS}) # TODO: why this must be PUBLIC? target_include_directories(sigrokdecode PUBLIC ${PROJECT_SOURCE_DIR}) target_sources(sigrokdecode PUBLIC FILE_SET HEADERS - BASE_DIRS ${PROJECT_BINARY_DIR} + BASE_DIRS ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR} TYPE HEADERS - FILES ${PROJECT_BINARY_DIR}/version.h ${PROJECT_BINARY_DIR}/config.h) + FILES ${PROJECT_BINARY_DIR}/version.h libsigrokdecode.h) # Install rules install(TARGETS sigrokdecode DESTINATION lib) -install(FILES ${LIBSIGROKDECODE_HEADERS} DESTINATION include) +install(TARGETS sigrokdecode FILE_SET HEADERS) # Autotools-like configuration summary message(STATUS "libsigrokdecode configuration summary:")