Skip to content

Commit b3208bc

Browse files
committed
header only build option
Signed-off-by: grdanny <danny.gr@gmail.com>
1 parent a9df4d1 commit b3208bc

File tree

9 files changed

+2143
-2149
lines changed

9 files changed

+2143
-2149
lines changed

.github/workflows/ci.yml

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: ubuntu-latest
2222
strategy:
2323
matrix:
24-
build: [1, 2, 3, 4]
24+
build: [1, 2, 3, 4, 5, 6]
2525
include:
2626
# -------------------------------------------------------------------
2727
# CLANG, Release
@@ -75,6 +75,36 @@ jobs:
7575
compiler-desc: gcc
7676
os: ubuntu-latest
7777

78+
79+
# -------------------------------------------------------------------
80+
# CLANG, Release header only
81+
# -------------------------------------------------------------------
82+
- build: 5
83+
build-type: Release
84+
build-shared: 'ON'
85+
header-only: 'ON'
86+
cxx-standard: 17
87+
cxx-compiler: clang++
88+
cxx-flags: ''
89+
cc-compiler: clang
90+
compiler-desc: clang
91+
os: ubuntu-latest
92+
93+
# -------------------------------------------------------------------
94+
# gcc, Release header only
95+
# -------------------------------------------------------------------
96+
- build: 6
97+
build-type: Release
98+
build-shared: 'ON'
99+
header-only: 'ON'
100+
cxx-standard: 17
101+
cxx-compiler: g++
102+
cxx-flags: ''
103+
cc-compiler: gcc
104+
compiler-desc: gcc
105+
os: ubuntu-latest
106+
107+
78108
env:
79109
CXX: ${{ matrix.cxx-compiler }}
80110
CC: ${{ matrix.cc-compiler }}
@@ -88,17 +118,17 @@ jobs:
88118
- name: Configure
89119
run: |
90120
cmake .. \
91-
-DCMAKE_INSTALL_PREFIX=../_install \
121+
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
92122
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
93123
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
94124
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
95125
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
96126
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
127+
-DPYSTRING_HEADER_ONLY=${{ matrix.header-only }}
97128
working-directory: _build
98129
- name: Build
99130
run: |
100131
cmake --build . \
101-
--target install \
102132
--config ${{ matrix.build-type }}
103133
working-directory: _build
104134
- name: Test
@@ -115,7 +145,7 @@ jobs:
115145
runs-on: macos-latest
116146
strategy:
117147
matrix:
118-
build: [1, 2]
148+
build: [1, 2, 3]
119149
include:
120150

121151
# Release
@@ -135,6 +165,16 @@ jobs:
135165
cxx-flags: ''
136166
os: macos-latest
137167

168+
169+
# Release header only
170+
- build: 3
171+
build-type: Release
172+
build-shared: 'ON'
173+
header-only: 'ON'
174+
cxx-standard: 17
175+
cxx-flags: ''
176+
os: macos-latest
177+
138178
steps:
139179
- name: Checkout
140180
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -146,17 +186,17 @@ jobs:
146186
- name: Configure
147187
run: |
148188
cmake ../. \
149-
-DCMAKE_INSTALL_PREFIX=../_install \
189+
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
150190
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
151191
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
152192
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
153193
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
154-
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }}
194+
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
195+
-DPYSTRING_HEADER_ONLY=${{ matrix.header-only }}
155196
working-directory: _build
156197
- name: Build
157198
run: |
158199
cmake --build . \
159-
--target install \
160200
--config ${{ matrix.build-type }} \
161201
working-directory: _build
162202
- name: Test
@@ -192,6 +232,15 @@ jobs:
192232
cxx-flags: ''
193233
os: windows-latest
194234

235+
# Release header only
236+
- build: 3
237+
build-type: Release
238+
build-shared: 'ON'
239+
header-only: 'ON'
240+
cxx-standard: 17
241+
cxx-flags: ''
242+
os: windows-latest
243+
195244

196245
steps:
197246
- name: Checkout
@@ -205,19 +254,19 @@ jobs:
205254
# the windows build needs the -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to work
206255
run: |
207256
cmake ../. \
208-
-DCMAKE_INSTALL_PREFIX=../_install \
257+
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
209258
-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS='ON'\
210259
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
211260
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
212261
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
213262
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
214-
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }}
263+
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
264+
-DPYSTRING_HEADER_ONLY=${{ matrix.header-only }}
215265
shell: bash
216266
working-directory: _build
217267
- name: Build
218268
run: |
219269
cmake --build . \
220-
--target install \
221270
--config ${{ matrix.build-type }}
222271
shell: bash
223272
working-directory: _build

.github/workflows/meson.yml

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ on:
77
jobs:
88
meson-build-and-tests:
99
runs-on: ${{ matrix.platform }}
10-
name: ${{ matrix.platform }}, ${{ matrix.mode.name }} ${{ matrix.flavor }}
10+
name: ${{ matrix.platform }}, ${{ matrix.mode.name }} ${{ matrix.flavor }} ${{ matrix.library_mode }}
1111
strategy:
1212
fail-fast: false
1313
matrix:
1414
flavor:
1515
- debug
1616
- release
17+
library_mode:
18+
- compiled
19+
- header-only
1720
mode:
1821
- name: default
1922
extra_envs: {}
@@ -61,6 +64,29 @@ jobs:
6164
- macos-latest
6265

6366
exclude:
67+
# Only test header-only with a subset of configurations to reduce CI time
68+
# Test header-only only with default compiler in release mode
69+
- library_mode: header-only
70+
flavor: debug
71+
- library_mode: header-only
72+
mode:
73+
name: gcc
74+
- library_mode: header-only
75+
mode:
76+
name: clang
77+
- library_mode: header-only
78+
mode:
79+
name: sanitize
80+
- library_mode: header-only
81+
mode:
82+
name: sanitize+asanonly
83+
- library_mode: header-only
84+
mode:
85+
name: clang+sanitize
86+
- library_mode: header-only
87+
mode:
88+
name: clang-cl+sanitize
89+
6490
# clang-cl only makes sense on windows.
6591
- platform: ubuntu-22.04
6692
mode:
@@ -125,21 +151,21 @@ jobs:
125151
if: ${{ matrix.platform == 'windows-2022' }}
126152
env: ${{ matrix.mode.extra_envs }}
127153
run: |
128-
meson setup build-${{ matrix.flavor }} --buildtype=${{ matrix.flavor }} -Ddefault_library=static ${{ matrix.mode.args }} --vsenv
154+
meson setup build-${{ matrix.flavor }}-${{ matrix.library_mode }} --buildtype=${{ matrix.flavor }} -Ddefault_library=static -Dheader_only=${{ matrix.library_mode == 'header-only' && 'true' || 'false' }} ${{ matrix.mode.args }} --vsenv
129155
- name: Configuring
130156
if: ${{ matrix.platform != 'windows-2022' }}
131157
env: ${{ matrix.mode.extra_envs }}
132158
run: |
133-
meson setup build-${{ matrix.flavor }} --buildtype=${{ matrix.flavor }} ${{ matrix.mode.args }}
159+
meson setup build-${{ matrix.flavor }}-${{ matrix.library_mode }} --buildtype=${{ matrix.flavor }} -Dheader_only=${{ matrix.library_mode == 'header-only' && 'true' || 'false' }} ${{ matrix.mode.args }}
134160
- name: Building
135161
run: |
136-
meson compile -C build-${{ matrix.flavor }}
162+
meson compile -C build-${{ matrix.flavor }}-${{ matrix.library_mode }}
137163
- name: Running tests
138164
env: ${{ matrix.mode.extra_envs }}
139165
run: |
140-
meson test -C build-${{ matrix.flavor }} --timeout-multiplier 0
166+
meson test -C build-${{ matrix.flavor }}-${{ matrix.library_mode }} --timeout-multiplier 0
141167
- uses: actions/upload-artifact@v4
142168
if: failure()
143169
with:
144-
name: ${{ matrix.platform }}-${{ matrix.mode.name }}-${{ matrix.flavor }}-logs
145-
path: build-${{ matrix.flavor }}/meson-logs
170+
name: ${{ matrix.platform }}-${{ matrix.mode.name }}-${{ matrix.flavor }}-${{ matrix.library_mode }}-logs
171+
path: build-${{ matrix.flavor }}-${{ matrix.library_mode }}/meson-logs

CMakeLists.txt

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.10)
22
project(pystring LANGUAGES CXX VERSION 1.1.4)
33

44
option (BUILD_SHARED_LIBS "Build shared libraries (set to OFF to build static libs)" ON)
5+
option(PYSTRING_HEADER_ONLY "Build as header-only library" OFF)
56

67
# If the user hasn't configured cmake with an explicit
78
# -DCMAKE_INSTALL_PREFIX=..., then set it to safely install into ./dist, to
@@ -13,14 +14,48 @@ if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
1314
endif()
1415
message (STATUS "Installation path will be ${CMAKE_INSTALL_PREFIX}")
1516

16-
add_library(pystring
17-
pystring.cpp
18-
pystring.h
19-
)
20-
set_target_properties(pystring PROPERTIES
21-
VERSION ${PROJECT_VERSION}
22-
SOVERSION ${PROJECT_VERSION_MAJOR}
23-
)
17+
if(PYSTRING_HEADER_ONLY)
18+
message(STATUS "Building pystring as header-only library")
19+
add_library(pystring INTERFACE)
20+
21+
target_compile_definitions(pystring INTERFACE PYSTRING_HEADER_ONLY)
22+
23+
target_include_directories(pystring INTERFACE
24+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
25+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
26+
)
27+
28+
# Install both headers for header-only mode
29+
install(FILES pystring.h pystring_impl.h
30+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
31+
)
32+
else()
33+
message(STATUS "Building pystring as compiled library")
34+
35+
add_library(pystring
36+
pystring.cpp
37+
pystring.h
38+
)
39+
40+
set_target_properties(pystring PROPERTIES
41+
VERSION ${PROJECT_VERSION}
42+
SOVERSION ${PROJECT_VERSION_MAJOR}
43+
)
44+
45+
install(TARGETS pystring
46+
LIBRARY DESTINATION lib
47+
RUNTIME DESTINATION bin
48+
ARCHIVE DESTINATION lib
49+
)
50+
51+
install (FILES pystring.h
52+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
53+
COMPONENT developer
54+
)
55+
56+
endif()
57+
58+
# Test executable
2459

2560
add_executable (pystring_test test.cpp)
2661
TARGET_LINK_LIBRARIES (pystring_test pystring)
@@ -29,12 +64,3 @@ enable_testing()
2964
add_test(NAME PyStringTest COMMAND pystring_test)
3065

3166
include(GNUInstallDirs)
32-
33-
install(TARGETS pystring
34-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
35-
)
36-
install (FILES pystring.h
37-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
38-
COMPONENT developer
39-
)
40-

meson.build

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

11-
inc = include_directories('.')
11+
# Option to build as header-only library
12+
header_only = get_option('header_only')
1213

13-
srcs = files('pystring.cpp')
14+
inc = include_directories('.')
1415
hdrs = files('pystring.h')
1516

16-
pystring_lib = library(
17-
'pystring',
18-
srcs,
19-
implicit_include_directories: false,
20-
include_directories: inc,
21-
version: meson.project_version(),
22-
install: true,
23-
)
24-
pystring_dep = declare_dependency(
25-
link_with: pystring_lib,
26-
include_directories: inc,
27-
)
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')
32+
33+
srcs = files('pystring.cpp')
34+
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+
)
43+
44+
pystring_dep = declare_dependency(
45+
link_with: pystring_lib,
46+
include_directories: inc,
47+
)
48+
49+
# Install headers for compiled mode
50+
install_headers(hdrs, subdir: 'pystring')
51+
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
59+
2860
meson.override_dependency('pystring', pystring_dep)
2961

62+
# Build and run tests
3063
test(
3164
'PyStringTest',
3265
executable(
@@ -36,11 +69,3 @@ test(
3669
build_by_default: false,
3770
),
3871
)
39-
40-
install_headers(hdrs, subdir: 'pystring')
41-
42-
pkgconfig = import('pkgconfig')
43-
pkgconfig.generate(
44-
pystring_lib,
45-
description: 'C++ functions matching the interface and behavior of python string methods with std::string',
46-
)

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
option('header_only', type: 'boolean', value: false, description: 'Build as header-only library')

0 commit comments

Comments
 (0)