Skip to content

Commit ab5c25a

Browse files
committed
updated meson and cmake build systems to have two available targets for the user to pick from compiled and header only
1 parent 0c58743 commit ab5c25a

3 files changed

Lines changed: 130 additions & 88 deletions

File tree

CMakeLists.txt

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
66
set(CMAKE_CXX_EXTENSIONS OFF)
77

88
option (BUILD_SHARED_LIBS "Build shared libraries (set to OFF to build static libs)" ON)
9-
option(PYSTRING_HEADER_ONLY "Build as header-only library" OFF)
109

1110
# If the user hasn't configured cmake with an explicit
1211
# -DCMAKE_INSTALL_PREFIX=..., then set it to safely install into ./dist, to
@@ -19,52 +18,86 @@ endif()
1918
message (STATUS "Installation path will be ${CMAKE_INSTALL_PREFIX}")
2019
include(GNUInstallDirs)
2120

22-
if(PYSTRING_HEADER_ONLY)
23-
message(STATUS "Building pystring as header-only library")
24-
add_library(pystring INTERFACE)
25-
26-
target_compile_definitions(pystring INTERFACE PYSTRING_HEADER_ONLY)
27-
28-
target_include_directories(pystring INTERFACE
29-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
30-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
31-
)
32-
33-
# Install both headers for header-only mode
34-
install(FILES pystring.h pystring_impl.h
35-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
36-
)
37-
else()
38-
message(STATUS "Building pystring as compiled library")
39-
40-
add_library(pystring
41-
pystring.cpp
42-
pystring.h
43-
)
44-
45-
set_target_properties(pystring PROPERTIES
46-
VERSION ${PROJECT_VERSION}
47-
SOVERSION ${PROJECT_VERSION_MAJOR}
48-
)
49-
50-
install(TARGETS pystring
51-
LIBRARY DESTINATION lib
52-
RUNTIME DESTINATION bin
53-
ARCHIVE DESTINATION lib
54-
)
55-
56-
install (FILES pystring.h
57-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
58-
COMPONENT developer
59-
)
60-
61-
endif()
62-
63-
# Test executable
64-
65-
add_executable (pystring_test test.cpp)
66-
TARGET_LINK_LIBRARIES (pystring_test pystring)
21+
# --- Compiled library target: pystring::pystring ---
22+
add_library(pystring
23+
pystring.cpp
24+
pystring.h
25+
)
26+
27+
set_target_properties(pystring PROPERTIES
28+
VERSION ${PROJECT_VERSION}
29+
SOVERSION ${PROJECT_VERSION_MAJOR}
30+
)
31+
32+
target_include_directories(pystring PUBLIC
33+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
34+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
35+
)
36+
37+
install(TARGETS pystring
38+
EXPORT pystringTargets
39+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
40+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
41+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
42+
)
43+
44+
45+
# --- Header-only target: pystring::pystring_header_only ---
46+
add_library(pystring_header_only INTERFACE)
47+
48+
target_compile_definitions(pystring_header_only INTERFACE PYSTRING_HEADER_ONLY)
49+
50+
target_include_directories(pystring_header_only INTERFACE
51+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
52+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
53+
)
54+
55+
install(TARGETS pystring_header_only
56+
EXPORT pystringTargets
57+
)
58+
59+
install(FILES pystring.h pystring_impl.h
60+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
61+
)
62+
63+
# --- Export & package config ---
64+
install(EXPORT pystringTargets
65+
FILE pystringTargets.cmake
66+
NAMESPACE pystring::
67+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
68+
)
69+
70+
include(CMakePackageConfigHelpers)
71+
72+
configure_package_config_file(
73+
cmake/pystringConfig.cmake.in
74+
pystringConfig.cmake
75+
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
76+
)
77+
78+
write_basic_package_version_file(
79+
pystringConfigVersion.cmake
80+
VERSION ${PROJECT_VERSION}
81+
COMPATIBILITY SameMajorVersion
82+
)
83+
84+
install(FILES
85+
${CMAKE_CURRENT_BINARY_DIR}/pystringConfig.cmake
86+
${CMAKE_CURRENT_BINARY_DIR}/pystringConfigVersion.cmake
87+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
88+
)
89+
90+
# --- Tests ---
91+
add_executable(pystring_test test.cpp)
92+
target_link_libraries(pystring_test pystring)
93+
94+
add_executable(pystring_test_header_only test.cpp)
95+
target_link_libraries(pystring_test_header_only pystring_header_only)
96+
97+
# Compile-time check that PYSTRING_HEADER_ONLY propagates correctly
98+
add_executable(pystring_test_header_only_define test_header_only_define.cpp)
99+
target_link_libraries(pystring_test_header_only_define pystring_header_only)
67100

68101
enable_testing()
69102
add_test(NAME PyStringTest COMMAND pystring_test)
70-
103+
add_test(NAME PyStringTestHeaderOnly COMMAND pystring_test_header_only)

meson.build

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,44 @@ project(
88
default_options: ['cpp_std=c++17,c++11', 'warning_level=3'],
99
)
1010

11-
# Option to build as header-only library
12-
header_only = get_option('header_only')
13-
1411
inc = include_directories('.')
1512
hdrs = files('pystring.h')
1613

17-
if header_only
18-
# Header-only mode: create a header-only dependency
19-
message('Building pystring as header-only library')
20-
21-
pystring_dep = declare_dependency(
22-
include_directories: inc,
23-
compile_args: ['-DPYSTRING_HEADER_ONLY'],
24-
)
25-
26-
# Install headers for header-only mode
27-
install_headers(hdrs, files('pystring_impl.h'), subdir: 'pystring')
28-
29-
else
30-
# Compiled mode: build as normal library
31-
message('Building pystring as compiled library')
14+
# --- Compiled library target ---
15+
pystring_lib = library(
16+
'pystring',
17+
files('pystring.cpp'),
18+
implicit_include_directories: false,
19+
include_directories: inc,
20+
version: meson.project_version(),
21+
install: true,
22+
)
3223

33-
srcs = files('pystring.cpp')
24+
pystring_dep = declare_dependency(
25+
link_with: pystring_lib,
26+
include_directories: inc,
27+
)
3428

35-
pystring_lib = library(
36-
'pystring',
37-
srcs,
38-
implicit_include_directories: false,
39-
include_directories: inc,
40-
version: meson.project_version(),
41-
install: true,
42-
)
29+
install_headers(hdrs, subdir: 'pystring')
4330

44-
pystring_dep = declare_dependency(
45-
link_with: pystring_lib,
46-
include_directories: inc,
47-
)
31+
pkgconfig = import('pkgconfig')
32+
pkgconfig.generate(
33+
pystring_lib,
34+
description: 'C++ functions matching the interface and behavior of python string methods with std::string',
35+
)
4836

49-
# Install headers for compiled mode
50-
install_headers(hdrs, subdir: 'pystring')
37+
# --- Header-only target ---
38+
pystring_header_only_dep = declare_dependency(
39+
include_directories: inc,
40+
compile_args: ['-DPYSTRING_HEADER_ONLY'],
41+
)
5142

52-
# Generate pkg-config file
53-
pkgconfig = import('pkgconfig')
54-
pkgconfig.generate(
55-
pystring_lib,
56-
description: 'C++ functions matching the interface and behavior of python string methods with std::string',
57-
)
58-
endif
43+
install_headers(hdrs, files('pystring_impl.h'), subdir: 'pystring')
5944

45+
# --- Override default dependency ---
6046
meson.override_dependency('pystring', pystring_dep)
6147

62-
# Build and run tests
48+
# --- Tests ---
6349
test(
6450
'PyStringTest',
6551
executable(
@@ -69,3 +55,21 @@ test(
6955
build_by_default: false,
7056
),
7157
)
58+
59+
test(
60+
'PyStringTestHeaderOnly',
61+
executable(
62+
'pystring_test_header_only',
63+
'test.cpp',
64+
dependencies: pystring_header_only_dep,
65+
build_by_default: false,
66+
),
67+
)
68+
69+
# Compile-time check that PYSTRING_HEADER_ONLY propagates correctly
70+
executable(
71+
'pystring_test_header_only_define',
72+
'test_header_only_define.cpp',
73+
dependencies: pystring_header_only_dep,
74+
build_by_default: true,
75+
)

test_header_only_define.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// test_header_only_mode.cpp
2+
#ifndef PYSTRING_HEADER_ONLY
3+
#error "PYSTRING_HEADER_ONLY must be defined when using the header-only target"
4+
#endif
5+
int main() { return 0; }

0 commit comments

Comments
 (0)