From 756d965e2ae9a0db2a726722e6f7edc75a3fac7a Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 11 Oct 2025 01:11:30 +0100 Subject: [PATCH] Refactor CMakeLists.txt for improved organization and add test data copying --- CMakeLists.txt | 77 ++++++++++++++++++-------------- tests/CMakeLists.txt | 102 +++++++++++++++++++++++++++++++------------ tool/CMakeLists.txt | 48 +++++++++++++++++--- 3 files changed, 161 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4501e4d..d48192c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,12 @@ cmake_minimum_required(VERSION 3.13) # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, see . +if(APPLE) + if("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "") + set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures for Mac OS X" FORCE) + endif() +endif() + # Integrate vcpkg toolchain if available but not explicitly provided. if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) if(DEFINED ENV{VCPKG_ROOT} AND EXISTS "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") @@ -29,39 +35,41 @@ if(DEFINED CMAKE_TOOLCHAIN_FILE AND EXISTS "${CMAKE_TOOLCHAIN_FILE}") include("${CMAKE_TOOLCHAIN_FILE}") endif() -IF(APPLE) - IF("${CMAKE_OSX_ARCHITECTURES}" STREQUAL "") - SET(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures for Mac OS X" FORCE) - ENDIF() -ENDIF(APPLE) - project(libaaruformat C) # Option to enable slog logging (disabled by default) option(USE_SLOG "Enable slog logging" OFF) -add_compile_definitions(__STDC_FORMAT_MACROS=1) +# Set C standard +set(CMAKE_C_STANDARD 99) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_C_EXTENSIONS OFF) +# Enable position independent code by default +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +# MSVC ARM specific override if("${CMAKE_C_COMPILER_ID}" MATCHES "MSVC" AND "${CMAKE_C_COMPILER_ARCHITECTURE_ID}" MATCHES "ARMV7") set(CMAKE_C_STANDARD 11) -else() - set(CMAKE_C_STANDARD 99) endif() +# Global compile definitions +add_compile_definitions(__STDC_FORMAT_MACROS=1) + +# MinGW specific configuration if("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW") if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "aarch64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm") set(WIN32 TRUE) endif() - add_link_options(-static-libgcc) endif() -message("Detected system processor: ${CMAKE_SYSTEM_PROCESSOR}") -message("Detected vs platform name: ${CMAKE_C_COMPILER_ARCHITECTURE_ID}") -message("Detected compiler: ${CMAKE_C_COMPILER_ID}") -message("Detected build type: ${CMAKE_BUILD_TYPE}") -message("Detected platform: ${CMAKE_C_PLATFORM_ID}") -message("Size of (void*): ${CMAKE_SIZEOF_VOID_P}") +message(STATUS "Detected system processor: ${CMAKE_SYSTEM_PROCESSOR}") +message(STATUS "Detected vs platform name: ${CMAKE_C_COMPILER_ARCHITECTURE_ID}") +message(STATUS "Detected compiler: ${CMAKE_C_COMPILER_ID}") +message(STATUS "Detected build type: ${CMAKE_BUILD_TYPE}") +message(STATUS "Detected platform: ${CMAKE_C_PLATFORM_ID}") +message(STATUS "Size of (void*): ${CMAKE_SIZEOF_VOID_P}") # Check if target is 64-bit if("${CMAKE_SIZEOF_VOID_P}" MATCHES "8" @@ -188,7 +196,18 @@ add_library(aaruformat SHARED include/aaruformat/structs/tape.h src/blocks/tape.c) -include_directories(include include/aaruformat 3rdparty/uthash/include 3rdparty/uthash/src) +# Set up include directories for the target +target_include_directories(aaruformat + PUBLIC + $ + $ + $ + ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/uthash/include + ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/uthash/src +) + +# Load third-party dependencies +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake-modules") include(3rdparty/flac.cmake) include(3rdparty/lzma.cmake) @@ -215,15 +234,16 @@ macro(TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE target) endforeach() elseif(APPLE) foreach(lib IN LISTS ARGN) - target_link_libraries(${target} "LINKER:--whole-archive" ${lib} "LINKER:--no-whole-archive") + target_link_libraries(${target} -Wl,-force_load ${lib}) endforeach() elseif(UNIX) foreach(lib IN LISTS ARGN) - target_link_libraries(${target} "LINKER:--whole-archive" ${lib} "LINKER:--no-whole-archive") + target_link_libraries(${target} -Wl,--whole-archive ${lib} -Wl,--no-whole-archive) endforeach() endif() endmacro() +# MinGW/ARM specific: disable PIC if(NOT "${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW" OR (NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" AND NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")) @@ -232,10 +252,6 @@ else() set_property(TARGET aaruformat PROPERTY POSITION_INDEPENDENT_CODE FALSE) endif() -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake-modules") - -set(CMAKE_POSITION_INDEPENDENT_CODE ON) - # Add slog submodule if(POLICY CMP0000) cmake_policy(VERSION 3.5) @@ -246,18 +262,20 @@ if(USE_SLOG) add_subdirectory(3rdparty/slog) # Include slog headers - include_directories(aaruformat 3rdparty/slog/src) + target_include_directories(aaruformat PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/slog/src) # Enable TRACE and slog output - add_compile_definitions(aaruformat ENABLE_TRACE ENABLE_FATAL USE_SLOG) + target_compile_definitions(aaruformat PRIVATE ENABLE_TRACE ENABLE_FATAL USE_SLOG) + + # Link slog + target_link_libraries(aaruformat PRIVATE slog) message(STATUS "slog logging enabled") else() message(STATUS "slog logging disabled (enable with -DUSE_SLOG=ON)") endif() -include_directories(include 3rdparty/uthash/src) - +# Check for math library include(CheckLibraryExists) check_library_exists(m log "" HAVE_LIB_M) @@ -265,11 +283,6 @@ if(HAVE_LIB_M) TARGET_LINK_LIBRARIES_WHOLE_ARCHIVE(aaruformat m) endif() -# Link slog conditionally -if(USE_SLOG) - target_link_libraries(aaruformat slog) -endif() - # Find Doxygen for documentation generation find_package(Doxygen) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2f012e4..522fc33 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,17 +1,19 @@ cmake_minimum_required(VERSION 3.13) -# 'Google_test' is the subproject name -project(tests) +# Test suite project +project(tests CXX) -# 'lib' is the folder with Google Test sources +# Temporarily disable deprecation warnings for Google Test set(_aaru_saved_warn_deprecated __aaru_warn_not_set__) if(DEFINED CMAKE_WARN_DEPRECATED) set(_aaru_saved_warn_deprecated ${CMAKE_WARN_DEPRECATED}) endif() set(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE) +# Add Google Test subdirectory add_subdirectory(lib) +# Restore original deprecation warning setting if(_aaru_saved_warn_deprecated STREQUAL __aaru_warn_not_set__) unset(CMAKE_WARN_DEPRECATED CACHE) unset(CMAKE_WARN_DEPRECATED) @@ -19,38 +21,82 @@ else() set(CMAKE_WARN_DEPRECATED ${_aaru_saved_warn_deprecated} CACHE BOOL "" FORCE) endif() unset(_aaru_saved_warn_deprecated) -include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR} ../include ../3rdparty/uthash/include ../3rdparty/uthash/src) -# Copy test data files into build tree -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/random DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/flac.flac DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/audio.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/lzma.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/data.bin DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mf2hd_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mf2hd.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/floptical_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/floptical.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/gigamo_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/gigamo.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/hifd_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/hifd.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mo540_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mo540.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mo640_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/mo640.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/cdmode1_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/cdmode1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/cdmode2_v1.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/data/cdmode2.aif DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/data/) +# Define test data files to copy +set(TEST_DATA_FILES + random + flac.flac + audio.bin + lzma.bin + data.bin + mf2hd_v1.aif + mf2hd.aif + floptical_v1.aif + floptical.aif + gigamo_v1.aif + gigamo.aif + hifd_v1.aif + hifd.aif + mo540_v1.aif + mo540.aif + mo640_v1.aif + mo640.aif + cdmode1_v1.aif + cdmode1.aif + cdmode2_v1.aif + cdmode2.aif +) + +# Create data directory in build tree +file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/data) + +# Copy test data files at build time (not configure time) +foreach(data_file ${TEST_DATA_FILES}) + add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/data/${data_file} + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_SOURCE_DIR}/data/${data_file} + ${CMAKE_CURRENT_BINARY_DIR}/data/${data_file} + DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/data/${data_file} + COMMENT "Copying test data file: ${data_file}" + ) + list(APPEND TEST_DATA_OUTPUTS ${CMAKE_CURRENT_BINARY_DIR}/data/${data_file}) +endforeach() + +# Add custom target to ensure data files are copied +add_custom_target(copy_test_data ALL DEPENDS ${TEST_DATA_OUTPUTS}) # Test executable (all unit tests) -add_executable(tests_run crc64.cpp spamsum.cpp crc32.c crc32.h flac.cpp lzma.cpp sha256.cpp md5.cpp sha1.cpp open_image.cpp - create_image.cpp) +add_executable(tests_run + crc64.cpp + spamsum.cpp + crc32.c + crc32.h + flac.cpp + lzma.cpp + sha256.cpp + md5.cpp + sha1.cpp + open_image.cpp + create_image.cpp +) + +# Set up include directories using modern target-specific approach +target_include_directories(tests_run + PRIVATE + ${gtest_SOURCE_DIR}/include + ${gtest_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/uthash/include + ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/uthash/src +) # Link libraries target_link_libraries(tests_run PRIVATE gtest gtest_main aaruformat) +# Ensure test data is copied before running tests +add_dependencies(tests_run copy_test_data) + # Integrate with CTest (per-test reporting) enable_testing() include(GoogleTest) diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index 48316d5..3febaac 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -1,11 +1,47 @@ -project(aaruformattool) +# Tool executable project +project(aaruformattool C CXX) +# Find required dependencies find_package(ICU COMPONENTS uc REQUIRED) find_package(Argtable3 CONFIG REQUIRED) -include_directories(${ICU_INCLUDE_DIRS}) +# Tool executable +add_executable(aaruformattool + main.c + version.h + aaruformattool.h + identify.c + info.c + helpers.c + read.c + printhex.c + verify.c + ecc_cd.c + commands.h + commands.c + usage.h + usage.c + compare.c + cli_compare.c + convert.c + termbox2.h +) -add_executable(aaruformattool main.c version.h aaruformattool.h identify.c info.c helpers.c read.c printhex.c verify.c ecc_cd.c - commands.h commands.c usage.h usage.c compare.c cli_compare.c convert.c termbox2.h) -target_link_libraries(aaruformattool "aaruformat" argtable3::argtable3) -target_link_libraries(aaruformattool "aaruformat" ICU::uc) +# Set C as the linker language (even though we enable CXX for stdlib) +set_target_properties(aaruformattool PROPERTIES LINKER_LANGUAGE C) + +# Set up include directories +target_include_directories(aaruformattool PRIVATE ${ICU_INCLUDE_DIRS}) + +# Link libraries +target_link_libraries(aaruformattool + PRIVATE + aaruformat + argtable3::argtable3 + ICU::uc +) + +# On macOS/iOS, explicitly link the C++ standard library for ICU dependencies +if(APPLE) + target_link_libraries(aaruformattool PRIVATE "-lc++") +endif()