From b9b80201b3f0b344e20efa8b41f8aaa57cd51c86 Mon Sep 17 00:00:00 2001 From: Jakov Petrina Date: Wed, 24 Sep 2025 12:55:37 +0200 Subject: [PATCH 1/3] chore: allow in-source builds using CMake Forbidding in-source builds makes it harder to integrate with existing firmware build systems like Buildroot. Signed-off-by: Jakov Petrina --- CMakeLists.txt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01bd3fa6..422ceb83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,13 +1,6 @@ cmake_minimum_required(VERSION 3.20) cmake_policy(SET CMP0095 NEW) -if ("${CMAKE_BINARY_DIR}" STREQUAL "${CMAKE_SOURCE_DIR}") - message(FATAL_ERROR "In-source builds are not allowed. - Please create a build directory and use `cmake ..` inside it. - NOTE: cmake will now create CMakeCache.txt and CMakeFiles/*. - You must delete them, or cmake will refuse to work.") -endif() - project(note_c) # Automatically ignore CMake build directory. From 7cefdd8100f189f2f715d5d4e443b279231d87d3 Mon Sep 17 00:00:00 2001 From: Jakov Petrina Date: Wed, 24 Sep 2025 13:01:33 +0200 Subject: [PATCH 2/3] feat: add install directives for CMake This makes it possible to install the library and public includes using standard commands. For example: $ DESTDIR=./rootfs cmake --install ./build/ --prefix '/usr' -- Install configuration: "" -- Installing: ./rootfs/usr/lib/libnote_c.so -- Installing: ./rootfs/usr/include/note-c/n_cjson.h -- Installing: ./rootfs/usr/include/note-c/note.h Signed-off-by: Jakov Petrina --- CMakeLists.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 422ceb83..65099b3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,3 +161,16 @@ endif(NOTE_C_BUILD_TESTS) if(NOTE_C_BUILD_DOCS) add_subdirectory(docs) endif(NOTE_C_BUILD_DOCS) + +set_target_properties(note_c PROPERTIES PRIVATE_HEADER "${NOTE_C_SRC_DIR}/n_cjson.h") +set_target_properties(note_c PROPERTIES PUBLIC_HEADER "${NOTE_C_SRC_DIR}/note.h") + +install(TARGETS note_c + EXPORT note_c + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/note-c + PRIVATE_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/note-c + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/note-c +) From 0a7e6159009824e43065fc72546e34cd8add1ced Mon Sep 17 00:00:00 2001 From: Jakov Petrina Date: Wed, 24 Sep 2025 13:32:19 +0200 Subject: [PATCH 3/3] feat: add CompileOptions.cmake and apply options conditionally This fixes x86-specific arguments being set for non-x86 platforms, allowing cross-compilation. Signed-off-by: Jakov Petrina --- CMakeLists.txt | 30 +++--------------------------- CompileOptions.cmake | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) create mode 100644 CompileOptions.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 65099b3e..396ac652 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,37 +40,13 @@ target_sources( ${NOTE_C_SRC_DIR}/n_serial.c ${NOTE_C_SRC_DIR}/n_str.c ) -target_compile_options( - note_c - PRIVATE - -Wall - -Wextra - -Wpedantic - -Werror - -Og - -ggdb - PUBLIC - -m32 - -mfpmath=sse - -msse2 -) + target_include_directories( note_c PUBLIC ${NOTE_C_SRC_DIR} ) -target_link_directories( - note_c - PUBLIC - /lib32 - /usr/lib32 - /usr/lib/gcc/x86_64-linux-gnu/12/32 -) -target_link_options( - note_c - PUBLIC - -m32 - -Wl,-melf_i386 -) + +include(CompileOptions.cmake) if(NOTE_C_LOW_MEM) target_compile_definitions( diff --git a/CompileOptions.cmake b/CompileOptions.cmake new file mode 100644 index 00000000..d852694e --- /dev/null +++ b/CompileOptions.cmake @@ -0,0 +1,24 @@ + +target_compile_options( + note_c + PRIVATE + -Wall + -Wextra + -Wpedantic + -Werror + -Wno-old-style-definition +) + +if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") + target_compile_options(note_c PUBLIC -m32 -mfpmath=sse -msse2) + target_link_directories(note_c PUBLIC /lib32 /usr/lib32 /usr/lib/gcc/x86_64-linux-gnu/12/32) + target_link_options(note_c PUBLIC -m32 -Wl,-melf_i386) +endif() + +if(CMAKE_BUILD_TYPE) + string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_TOLOWER) + + if(CMAKE_BUILD_TYPE_TOLOWER STREQUAL "debug") + target_link_options(note_c PUBLIC -Og -ggdb) + endif() +endif()