Skip to content

Commit

Permalink
Add unit testing framework and a static library
Browse files Browse the repository at this point in the history
* Add unit tests using check
* Set test paths and env variables accordingly
* Fix exporting symbols
* Add a static library
* Ignore more files
* Remove buildsystem clutter

Signed-off-by: Soroush Rabiei <[email protected]>
  • Loading branch information
soroush committed Jan 2, 2025
1 parent 717d792 commit fcd5c04
Show file tree
Hide file tree
Showing 25 changed files with 576 additions and 546 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,7 @@ __pycache__/
# End of https://www.gitignore.io/api/c,c++,linux,windows,autotools,visualstudio

build/*
out/*
out/*
checks.json
CMakePresets.json
.vscode/settings.json
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ project(calendars
LANGUAGES C
)

find_package(GTest CONFIG)
find_package(check CONFIG REQUIRED)

add_subdirectory(lib)

if(${GTest_FOUND})
if(${check_FOUND})
enable_testing()
add_subdirectory(tests)
else()
message(STATUS "GTest library not found. Disabling the tests.")
message(STATUS "check library not found. Disabling the tests.")
endif()
116 changes: 79 additions & 37 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
# Copyright (C) 2021-2025 - Soroush Rabiei, <[email protected]>
# This file is part of libcalendars.
#
# libcalendars 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.
#
# libcalendars 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 libcalendars. If not, see <http://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.16.0)

set(LIBCAL_SOURCES
# Intermediate Object Library
add_library(calendars_object OBJECT
# Headers
include/libcalendars/cl-calendars.h
include/libcalendars/cl-export.h
include/libcalendars/cl-gregorian.h
include/libcalendars/cl-islamic-civil.h
include/libcalendars/cl-jewish.h
include/libcalendars/cl-julian.h
include/libcalendars/cl-milankovic.h
include/libcalendars/cl-solar-hijri.h
"include/libcalendars/cl-calendars.h"
"include/libcalendars/cl-export.h"
"include/libcalendars/cl-gregorian.h"
"include/libcalendars/cl-islamic-civil.h"
"include/libcalendars/cl-jewish.h"
"include/libcalendars/cl-julian.h"
"include/libcalendars/cl-milankovic.h"
"include/libcalendars/cl-solar-hijri.h"
# Sources
src/cl-math.h
src/cl-math.c
src/cl-calendars.c
src/cl-gregorian.c
src/cl-julian.c
src/cl-milankovic.c
src/cl-solar-hijri.c
src/cl-islamic-civil.c
src/cl-jewish.c
"src/cl-math.h"
"src/cl-math.c"
"src/cl-calendars.c"
"src/cl-gregorian.c"
"src/cl-julian.c"
"src/cl-milankovic.c"
"src/cl-solar-hijri.c"
"src/cl-islamic-civil.c"
"src/cl-jewish.c"
)

if(WIN32)
list(APPEND LIBCAL_SOURCES win32/libcalendars.rc)
set(PLATFORM_SOURCES win32/libcalendars.rc)
else()
set(PLATFORM_SOURCES "")
endif()

# Define the library
add_library(calendars SHARED ${LIBCAL_SOURCES})
target_include_directories(calendars_object
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

set_target_properties(calendars_object PROPERTIES
C_STANDARD 90
DEBUG_POSTFIX "_d"
COMPILE_DEFINITIONS libcalendars_EXPORTS
)

# Define the shared library
add_library(calendars SHARED "${PLATFORM_SOURCES}" $<TARGET_OBJECTS:calendars_object>)

# Mark all public headers
file(GLOB_RECURSE ALL_HEADER_FILES "include/libcalendars/*.h")
Expand All @@ -38,7 +69,6 @@ set_target_properties(calendars PROPERTIES
SOVERSION 0
C_STANDARD 90
DEBUG_POSTFIX "_d"
PUBLIC_HEADER "${ALL_HEADER_FILES}"
DEFINE_SYMBOL libcalendars_EXPORTS
)

Expand All @@ -55,36 +85,52 @@ if(UNIX)
target_link_libraries(calendars PUBLIC m)
endif()

# Testing
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
# Define the static libray
add_library(calendars_static STATIC $<TARGET_OBJECTS:calendars_object>)

# Prepate the target
set_target_properties(calendars_static PROPERTIES
VERSION 1.1.0
SOVERSION 0
C_STANDARD 90
DEBUG_POSTFIX "_d"
)

target_compile_definitions(calendars_static INTERFACE libcalendars_STATIC)

target_include_directories(calendars_static
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)

if(UNIX)
target_link_libraries(calendars PUBLIC m)
endif()

# Generate package config file

include(CMakePackageConfigHelpers)

configure_package_config_file(libcalendars.pc.in libcalendars.pc
INSTALL_DESTINATION ${LIB_INSTALL_DIR}/libcalendars
)

# Installation

include(GNUInstallDirs)

export(TARGETS calendars FILE "libcalendarsTargets.cmake")

install(TARGETS calendars
install(TARGETS calendars calendars_static
EXPORT "calendarsTargets"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libcalendars"
# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libcalendars"
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(DIRECTORY "include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/libcalendars")
install(DIRECTORY "include/" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

include(CMakePackageConfigHelpers)

Expand All @@ -104,7 +150,3 @@ install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/libcalendarsConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/"
)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/calendars.pc"
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig"
)
File renamed without changes.
39 changes: 22 additions & 17 deletions lib/include/libcalendars/cl-export.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,34 @@
#ifndef LIBCALENDARS_EXPORT_H
#define LIBCALENDARS_EXPORT_H

#if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__
#ifdef libcalendars_EXPORTS
#ifdef __GNUC__
#define LIBCALENDAR_API __attribute__ ((dllexport))
#if defined libcalendars_STATIC
#define LIBCALENDAR_API
#define LIBCALENDAR_PRIVATE
#else
#if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__
#ifdef libcalendars_EXPORTS
#ifdef __GNUC__
#define LIBCALENDAR_API __attribute__ ((dllexport))
#else
#define LIBCALENDAR_API __declspec(dllexport)
#endif
#else
#define LIBCALENDAR_API __declspec(dllexport)
#ifdef __GNUC__
#define LIBCALENDAR_API __attribute__ ((dllimport))
#else
#define LIBCALENDAR_API __declspec(dllimport)
#endif
#endif
#define LIBCALENDAR_PRIVATE
#else
#ifdef __GNUC__
#define LIBCALENDAR_API __attribute__ ((dllimport))
#if __GNUC__ >= 4
#define LIBCALENDAR_API __attribute__ ((visibility ("default")))
#define LIBCALENDAR_PRIVATE __attribute__ ((visibility ("hidden")))
#else
#define LIBCALENDAR_API __declspec(dllimport)
#define LIBCALENDAR_API
#define LIBCALENDAR_PRIVATE
#endif
#endif
#define LIBCALENDAR_PRIVATE
#else
#if __GNUC__ >= 4
#define LIBCALENDAR_API __attribute__ ((visibility ("default")))
#define LIBCALENDAR_PRIVATE __attribute__ ((visibility ("hidden")))
#else
#define LIBCALENDAR_API
#define LIBCALENDAR_PRIVATE
#endif
#endif

#endif /* LIBCALENDARS_EXPORT_H */
64 changes: 27 additions & 37 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,43 @@

cmake_minimum_required(VERSION 3.16.0)

# Calendar tests

enable_language(CXX)
enable_testing()

link_libraries(calendars GTest::gtest GTest::gtest_main $<IF:$<TARGET_EXISTS:Check::check>,Check::check,Check::checkShared>)

include_directories(PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include/>)

file(GLOB_RECURSE ALL_SOURCES "*.c")
# Commont test mathematics
add_library(calendar_test_common OBJECT calendar-arithmetic.c)
target_link_libraries(calendar_test_common PUBLIC calendars Check::checkShared)

set_property(SOURCE ${ALL_SOURCES} PROPERTY LANGUAGE CXX)
link_libraries(calendar_test_common calendars)

add_executable(tst_gregorian tst-common.c tst-common.h tst-gregorian.c)
add_executable(tst_milankovic tst-common.c tst-common.h tst-milankovic.c)
add_executable(tst_julian tst-common.c tst-common.h tst-julian.c)
add_executable(tst_jewish tst-common.c tst-common.h tst-jewish.c)
add_executable(tst_islamic_civil tst-common.c tst-common.h tst-islamic-civil.c)
add_executable(gregorian gregorian.c)
add_executable(milankovic milankovic.c)
add_executable(julian julian.c)
add_executable(jewish jewish.c)
add_executable(islamic_civil islamic-civil.c)
add_executable(solar_hijri solar-hijri.c)

# add_executable(tst_solar_hijri
# calendar-arithmetic.cpp
# tst-solar-hijri.cpp
# )

add_executable(tst_solar_hijri
tst-solar-hijri.c
calendar-arithmetic.c
)

set_target_properties(tst_gregorian tst_julian tst_milankovic tst_solar_hijri tst_jewish tst_islamic_civil
set_target_properties(
gregorian julian milankovic solar_hijri jewish islamic_civil
PROPERTIES
C_STANDARD 90
DEBUG_POSTFIX "_d"
)

# Add tests

add_test(NAME "Gregorian" COMMAND tst_gregorian WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Julian" COMMAND tst_julian WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Milankovic" COMMAND tst_milankovic WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "SolarHijri" COMMAND tst_solar_hijri WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# add_test(NAME "SolarHijri_C" COMMAND tst_solar_hijri_c WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Jewish" COMMAND tst_jewish WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "IslamicCivil" COMMAND tst_islamic_civil WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Gregorian" COMMAND gregorian WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Julian" COMMAND julian WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Milankovic" COMMAND milankovic WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "SolarHijri" COMMAND solar_hijri WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "Jewish" COMMAND jewish WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME "IslamicCivil" COMMAND islamic_civil WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

add_custom_command(TARGET tst_gregorian tst_julian tst_milankovic tst_solar_hijri tst_jewish tst_islamic_civil
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:calendars>" "$<TARGET_FILE_DIR:tst_gregorian>"
COMMENT "Copy dll file to $<TARGET_FILE_DIR:tst_gregorian> directory" VERBATIM
)
if(WIN32)
set_tests_properties(Gregorian Julian Milankovic SolarHijri Jewish IslamicCivil PROPERTIES
ENVIRONMENT "PATH=$<TARGET_FILE_DIR:calendars>;$ENV{PATH}"
)
else()
set_tests_properties(Gregorian Julian Milankovic SolarHijri Jewish IslamicCivil PROPERTIES
ENVIRONMENT "LD_LIBRARY_PATH=$<TARGET_FILE_DIR:MyLibraryShared>:$ENV{LD_LIBRARY_PATH}"
)
endif()
Loading

0 comments on commit fcd5c04

Please sign in to comment.