Skip to content

Commit 81a2ea2

Browse files
committed
Add microtimer package and consumer support
1 parent 9742b88 commit 81a2ea2

5 files changed

Lines changed: 115 additions & 7 deletions

File tree

CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
cmake_minimum_required(VERSION 3.21)
2+
3+
project(microtimer VERSION 1.0.0 LANGUAGES C)
4+
5+
include(CMakePackageConfigHelpers)
6+
include(GNUInstallDirs)
7+
8+
set(MICROTIMER_MAX_TIMERS "8" CACHE STRING "Resolved timer capacity for this build")
9+
option(MICROTIMER_BUILD_TESTS "Build microtimer tests" ${PROJECT_IS_TOP_LEVEL})
10+
11+
configure_file(
12+
"${CMAKE_CURRENT_SOURCE_DIR}/include/mtimer_config.h.in"
13+
"${CMAKE_CURRENT_BINARY_DIR}/include/mtimer_config.h"
14+
@ONLY)
15+
16+
add_library(microtimer src/mtimer.c)
17+
add_library(microtimer::microtimer ALIAS microtimer)
18+
19+
target_compile_features(microtimer PUBLIC c_std_99)
20+
target_include_directories(microtimer
21+
PUBLIC
22+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
23+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
24+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
25+
26+
set_target_properties(microtimer PROPERTIES
27+
EXPORT_NAME microtimer
28+
VERSION ${PROJECT_VERSION}
29+
SOVERSION ${PROJECT_VERSION_MAJOR})
30+
31+
install(TARGETS microtimer
32+
EXPORT microtimerTargets
33+
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
34+
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
35+
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
36+
37+
install(FILES
38+
"${CMAKE_CURRENT_SOURCE_DIR}/include/mtimer.h"
39+
"${CMAKE_CURRENT_BINARY_DIR}/include/mtimer_config.h"
40+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
41+
42+
install(EXPORT microtimerTargets
43+
FILE microtimerTargets.cmake
44+
NAMESPACE microtimer::
45+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/microtimer")
46+
47+
write_basic_package_version_file(
48+
"${CMAKE_CURRENT_BINARY_DIR}/microtimerConfigVersion.cmake"
49+
VERSION ${PROJECT_VERSION}
50+
COMPATIBILITY SameMajorVersion)
51+
52+
configure_package_config_file(
53+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/microtimerConfig.cmake.in"
54+
"${CMAKE_CURRENT_BINARY_DIR}/microtimerConfig.cmake"
55+
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/microtimer")
56+
57+
install(FILES
58+
"${CMAKE_CURRENT_BINARY_DIR}/microtimerConfig.cmake"
59+
"${CMAKE_CURRENT_BINARY_DIR}/microtimerConfigVersion.cmake"
60+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/microtimer")
61+
62+
if(MICROTIMER_BUILD_TESTS)
63+
enable_testing()
64+
add_subdirectory(tests)
65+
endif()

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY: all test clean
2+
3+
all: test
4+
5+
test:
6+
$(MAKE) -C tests CC="$(CC)" CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)"
7+
8+
clean:
9+
$(MAKE) -C tests clean

cmake/microtimerConfig.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/microtimerTargets.cmake")
4+
5+
check_required_components(microtimer)

include/mtimer_config.h.in

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef MTIMER_CONFIG_H
2+
#define MTIMER_CONFIG_H
3+
4+
#define MTIMER_CONFIG_RESOLVED_MAX_TIMERS @MICROTIMER_MAX_TIMERS@
5+
6+
#if defined(MTIMER_MAX_TIMERS) && (MTIMER_MAX_TIMERS != MTIMER_CONFIG_RESOLVED_MAX_TIMERS)
7+
#error "microtimer: MTIMER_MAX_TIMERS conflicts with resolved mtimer_config.h"
8+
#endif
9+
10+
#undef MTIMER_MAX_TIMERS
11+
#define MTIMER_MAX_TIMERS MTIMER_CONFIG_RESOLVED_MAX_TIMERS
12+
13+
#if (MTIMER_MAX_TIMERS < 1)
14+
#error "microtimer: MTIMER_MAX_TIMERS must be >= 1"
15+
#endif
16+
17+
#if (MTIMER_MAX_TIMERS > 255)
18+
#error "microtimer: MTIMER_MAX_TIMERS must be <= 255 with uint8_t ids"
19+
#endif
20+
21+
typedef char mtimer_config_assert_max_timers_range[
22+
(MTIMER_MAX_TIMERS >= 1 && MTIMER_MAX_TIMERS <= 255) ? 1 : -1];
23+
24+
#endif /* MTIMER_CONFIG_H */

tests/Makefile

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
CC ?= gcc
2-
CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -Werror -I../include -I../../microtest/include
3-
SRC := ../src/mtimer.c
1+
CC ?= cc
2+
CSTD ?= -std=c99
3+
WARN_CFLAGS ?= -Wall -Wextra -Wpedantic -Werror
4+
CPPFLAGS ?=
5+
CFLAGS ?=
6+
LDFLAGS ?=
7+
LDLIBS ?=
8+
SRC := ../src/mtimer.c
49

510
.PHONY: all clean
611

712
all: test_all
8-
@./test_all
13+
./test_all
914

10-
test_all: test_all.c $(SRC) ../include/mtimer.h
11-
$(CC) $(CFLAGS) $(SRC) test_all.c -lm -o $@
15+
test_all: test_all.c test_harness.h $(SRC) ../include/mtimer.h ../include/mtimer_config.h
16+
$(CC) $(CPPFLAGS) -I../include $(CSTD) $(WARN_CFLAGS) $(CFLAGS) $(SRC) test_all.c $(LDFLAGS) $(LDLIBS) -o $@
1217

1318
clean:
14-
rm -f test_all
19+
$(RM) test_all test_all.exe

0 commit comments

Comments
 (0)