Skip to content

Commit 7e43ffb

Browse files
authored
Merge pull request #37 from jamessan/cmake-fixes
Flesh out CMake support
2 parents 32b6bf2 + 6fc8639 commit 7e43ffb

8 files changed

Lines changed: 209 additions & 10 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ concurrency:
1212
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
1313

1414
jobs:
15-
# Test the minimum supported cmake.
1615
linux:
1716
runs-on: ubuntu-latest
1817
timeout-minutes: 15
@@ -32,3 +31,22 @@ jobs:
3231
3332
- name: Run tests
3433
run: make test
34+
35+
linux-cmake:
36+
runs-on: ubuntu-latest
37+
timeout-minutes: 15
38+
steps:
39+
- uses: actions/checkout@v7.0.0
40+
with:
41+
persist-credentials: false
42+
43+
- name: Install dependencies
44+
run: sudo apt --update install build-essential libncurses-dev cmake
45+
46+
- name: Build
47+
run: |
48+
cmake -B build .
49+
cmake --build build
50+
51+
- name: Run tests
52+
run: ctest --test-dir build --output-on-failure

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ tools/*
99
/.maint
1010
/tags
1111

12+
!CMakeLists.txt
13+
1214
# autoreconf
1315
aclocal.m4
1416
autom4te.cache/

‎CMakeLists.txt‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(unibilium LANGUAGES C VERSION 2.1.3)
33

4-
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/*.c)
5-
add_library(unibilium ${SRC_FILES})
6-
set_target_properties(unibilium PROPERTIES PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/unibilium.h
7-
VERSION "${VERSION_MAJOR}.${VERSION_MINOR}")
4+
add_library(unibilium
5+
unibilium.c
6+
uninames.c
7+
uniutil.c
8+
)
9+
set_target_properties(unibilium PROPERTIES
10+
PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/unibilium.h
11+
# Keep in sync with Makefile.in
12+
# "$LT_CURRENT - $LT_AGE.$LT_AGE.$LT_REVISION"
13+
VERSION "4.0.1"
14+
# "$LT_CURRENT - $LT_AGE"
15+
SOVERSION "4")
16+
target_compile_features(unibilium PRIVATE c_std_99)
817

918
if(NOT WIN32)
1019
execute_process(COMMAND sh -c "ncursesw6-config --terminfo-dirs 2>/dev/null || \
@@ -15,13 +24,43 @@ if(NOT WIN32)
1524
OUTPUT_VARIABLE TERMINFO_DIRS_DEFAULT)
1625
# Remove trailing newline
1726
string(STRIP "${TERMINFO_DIRS_DEFAULT}" TERMINFO_DIRS_DEFAULT)
27+
28+
execute_process(COMMAND sh -c "ncursesw6-config --terminfo 2>/dev/null || \
29+
ncurses6-config --terminfo 2>/dev/null || \
30+
ncursesw5-config --terminfo 2>/dev/null || \
31+
ncurses5-config --terminfo 2>/dev/null || \
32+
echo '/usr/share/terminfo'"
33+
OUTPUT_VARIABLE TERMINFO_DEFAULT)
34+
# Remove trailing newline
35+
string(STRIP "${TERMINFO_DEFAULT}" TERMINFO_DEFAULT)
1836
else()
1937
set(TERMINFO_DIRS_DEFAULT "")
38+
set(TERMINFO_DEFAULT "")
2039
endif()
2140
set(TERMINFO_DIRS "${TERMINFO_DIRS_DEFAULT}" CACHE STRING "Colon-separated list of directories where unibilium should look for compiled terminfo files at runtime")
2241
target_compile_definitions(unibilium PUBLIC "TERMINFO_DIRS=\"${TERMINFO_DIRS}\"")
42+
set(TERMINFO "${TERMINFO_DEFAULT}" CACHE STRING "Directory where unibilium should look for compiled terminfo files at runtime")
43+
target_compile_definitions(unibilium PUBLIC "TERMINFO=\"${TERMINFO}\"")
2344

2445
include(GNUInstallDirs)
46+
47+
if(NOT WIN32)
48+
add_subdirectory(doc)
49+
set(LIBDIR ${CMAKE_INSTALL_FULL_LIBDIR})
50+
set(INCDIR ${CMAKE_INSTALL_FULL_INCLUDEDIR})
51+
set(VERSION ${PROJECT_VERSION})
52+
configure_file(unibilium.pc.in unibilium.pc @ONLY)
53+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/unibilium.pc"
54+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
55+
unset(LIBDIR)
56+
unset(INCDIR)
57+
unset(VERSION)
58+
endif()
59+
add_subdirectory(tools)
60+
61+
enable_testing()
62+
add_subdirectory(t)
63+
2564
install(TARGETS unibilium
2665
PUBLIC_HEADER
2766
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

‎Makefile.in‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ STATIC_TEST_FILES:= \
141141
t/static_screen-256color.c \
142142
t/static_screen.c \
143143
t/static_tmux.c \
144-
t/static_xterm-256color.c
144+
t/static_xterm-256color.c \
145+
t/static_xterm.c
145146
.PHONY: regenerate-tests
146147
regenerate-tests:
147148
$(RM) $(STATIC_TEST_FILES)
148149
$(MAKE) $(STATIC_TEST_FILES)
149150

150151
t/static_%.c: | tools/gen-static-test
151-
$< $(patsubst t/static_%.c,%,$@) > $@
152+
$| $(patsubst t/static_%.c,%,$@) > $@

‎doc/CMakeLists.txt‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
if(NOT CMAKE_SKIP_INSTALL_RULES)
2+
find_program(PROG_GZIP gzip)
3+
if(NOT PROG_GZIP)
4+
message(FATAL_ERROR "Missing gzip. Set CMAKE_SKIP_INSTALL_RULES to skip man pages.")
5+
endif()
6+
7+
find_program(PROG_POD2MAN pod2man)
8+
if(NOT PROG_POD2MAN)
9+
message(FATAL_ERROR "Missing pod2man. Set CMAKE_SKIP_INSTALL_RULES to skip man pages.")
10+
endif()
11+
12+
macro(unibi_pod2man basename)
13+
set(OUT_MAN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${basename}.3")
14+
set(OUT_GZ_FILE "${CMAKE_CURRENT_BINARY_DIR}/${basename}.3.gz")
15+
add_custom_command(
16+
OUTPUT "${OUT_MAN_FILE}"
17+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${basename}.pod"
18+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
19+
COMMAND pod2man --center "${PROJECT_NAME}" --section 3 --release "${PROJECT_NAME}-${PROJECT_VERSION}" "${basename}.pod" "${OUT_MAN_FILE}")
20+
add_custom_command(
21+
OUTPUT "${OUT_GZ_FILE}"
22+
DEPENDS "${OUT_MAN_FILE}"
23+
COMMAND gzip --stdout "${OUT_MAN_FILE}" > "${OUT_GZ_FILE}")
24+
add_custom_target(man_page_${basename}
25+
DEPENDS "${OUT_GZ_FILE}")
26+
add_dependencies(man_pages man_page_${basename})
27+
install(FILES "${OUT_GZ_FILE}"
28+
DESTINATION "${CMAKE_INSTALL_MANDIR}/man3/")
29+
endmacro()
30+
31+
add_custom_target(man_pages ALL)
32+
33+
unibi_pod2man(unibi_add_ext_bool)
34+
unibi_pod2man(unibi_add_ext_num)
35+
unibi_pod2man(unibi_add_ext_str)
36+
unibi_pod2man(unibi_count_ext_bool)
37+
unibi_pod2man(unibi_count_ext_num)
38+
unibi_pod2man(unibi_count_ext_str)
39+
unibi_pod2man(unibi_del_ext_bool)
40+
unibi_pod2man(unibi_destroy)
41+
unibi_pod2man(unibi_dummy)
42+
unibi_pod2man(unibi_dump)
43+
unibi_pod2man(unibi_format)
44+
unibi_pod2man(unibi_from_env)
45+
unibi_pod2man(unibi_from_fd)
46+
unibi_pod2man(unibi_from_file)
47+
unibi_pod2man(unibi_from_fp)
48+
unibi_pod2man(unibi_from_mem)
49+
unibi_pod2man(unibi_from_term)
50+
unibi_pod2man(unibi_get_aliases)
51+
unibi_pod2man(unibi_get_bool)
52+
unibi_pod2man(unibi_get_ext_bool_name)
53+
unibi_pod2man(unibi_get_ext_bool)
54+
unibi_pod2man(unibi_get_ext_num_name)
55+
unibi_pod2man(unibi_get_ext_num)
56+
unibi_pod2man(unibi_get_ext_str_name)
57+
unibi_pod2man(unibi_get_ext_str)
58+
unibi_pod2man(unibi_get_name)
59+
unibi_pod2man(unibi_get_num)
60+
unibi_pod2man(unibi_get_str)
61+
unibi_pod2man(unibi_int_from_var)
62+
unibi_pod2man(unibilium.h)
63+
unibi_pod2man(unibi_name_bool)
64+
unibi_pod2man(unibi_name_num)
65+
unibi_pod2man(unibi_name_str)
66+
unibi_pod2man(unibi_num_from_var)
67+
unibi_pod2man(unibi_run)
68+
unibi_pod2man(unibi_set_aliases)
69+
unibi_pod2man(unibi_set_bool)
70+
unibi_pod2man(unibi_set_ext_bool_name)
71+
unibi_pod2man(unibi_set_ext_bool)
72+
unibi_pod2man(unibi_set_ext_num_name)
73+
unibi_pod2man(unibi_set_ext_num)
74+
unibi_pod2man(unibi_set_ext_str_name)
75+
unibi_pod2man(unibi_set_ext_str)
76+
unibi_pod2man(unibi_set_name)
77+
unibi_pod2man(unibi_set_num)
78+
unibi_pod2man(unibi_set_str)
79+
unibi_pod2man(unibi_short_name_bool)
80+
unibi_pod2man(unibi_short_name_num)
81+
unibi_pod2man(unibi_short_name_str)
82+
unibi_pod2man(unibi_str_from_var)
83+
unibi_pod2man(unibi_terminfo_dirs)
84+
unibi_pod2man(unibi_var_from_num)
85+
unibi_pod2man(unibi_var_from_str)
86+
endif()

‎t/CMakeLists.txt‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
macro(unibi_test basename)
2+
add_executable(${basename} "${basename}.c")
3+
target_include_directories(${basename} PRIVATE ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
4+
target_link_libraries(${basename} unibilium)
5+
add_test(
6+
NAME ${basename}
7+
COMMAND prove -f $<TARGET_FILE:${basename}>
8+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
9+
endmacro()
10+
11+
unibi_test(00_load)
12+
unibi_test(if-then-else)
13+
unibi_test(new-extended-format-static_screen)
14+
unibi_test(static_screen-256color)
15+
unibi_test(static_screen)
16+
unibi_test(static_tmux)
17+
unibi_test(static_xterm-256color)
18+
unibi_test(static_xterm)
19+
unibi_test(wide-format)
20+
21+
macro(regen termname)
22+
set(OUT_STATIC_FILE "static_${termname}.c")
23+
add_custom_target(regen-${termname}
24+
COMMAND gen-static-test "${termname}" > "${CMAKE_CURRENT_BINARY_DIR}/${OUT_STATIC_FILE}")
25+
add_custom_target(copy-${termname}
26+
DEPENDS regen-${termname}
27+
COMMAND cmake -E copy "${CMAKE_CURRENT_BINARY_DIR}/${OUT_STATIC_FILE}" ${CMAKE_CURRENT_SOURCE_DIR})
28+
add_dependencies(regenerate-tests regen-${termname})
29+
endmacro()
30+
31+
add_custom_target(regenerate-tests)
32+
regen(screen-256color)
33+
regen(screen)
34+
regen(tmux)
35+
regen(xterm-256color)
36+
regen(xterm)

‎tools/CMakeLists.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
macro(unibi_tool basename)
2+
add_executable(${basename} "${basename}.c")
3+
target_link_libraries(${basename} unibilium)
4+
target_include_directories(${basename} PRIVATE ${PROJECT_SOURCE_DIR})
5+
target_compile_features(${basename} PRIVATE c_std_99)
6+
endmacro()
7+
8+
unibi_tool(unibi-dump)
9+
unibi_tool(unibi-put)
10+
unibi_tool(gen-static-test)

‎tools/gen-static-test.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static void print_str_esc(const char *s, int twice) {
4949
}
5050
}
5151

52+
enum {MAX_BUF = 32768};
5253
int main(int argc, char **argv) {
5354
unibi_term *ut;
5455
if (argc < 2) {
@@ -68,10 +69,15 @@ int main(int argc, char **argv) {
6869
return EXIT_FAILURE;
6970
}
7071

71-
char buf[4096];
72-
size_t r = unibi_dump(ut, buf, sizeof buf);
73-
if (r > sizeof buf) {
72+
char *buf = malloc(MAX_BUF);
73+
if (!buf) {
74+
perror("malloc()");
75+
return EXIT_FAILURE;
76+
}
77+
size_t r = unibi_dump(ut, buf, MAX_BUF);
78+
if (r > MAX_BUF) {
7479
perror("unibi_dump()");
80+
free(buf);
7581
return EXIT_FAILURE;
7682
}
7783

@@ -324,6 +330,7 @@ int main(int argc, char **argv) {
324330
say("}");
325331

326332
unibi_destroy(ut);
333+
free(buf);
327334

328335
say("");
329336
say("static void setup(void) {");

0 commit comments

Comments
 (0)