Skip to content

Commit c8213df

Browse files
committed
Support BUILD_SHARED_LIBS
1 parent 8a5e88b commit c8213df

3 files changed

Lines changed: 39 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,54 @@ if(PROJECT_IS_TOP_LEVEL)
2525
set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
2626

2727
option(BUILD_EXAMPLE "Build the example" ON)
28+
option(BUILD_SHARED_LIBS "Build as a shared library" OFF)
2829
option(ENABLE_INSTALL "Generate install rules" ON)
2930
else()
3031
option(EGGS_ASSERT_BUILD_EXAMPLE "Build the example" OFF)
32+
option(
33+
EGGS_ASSERT_BUILD_SHARED_LIBS
34+
"Build as a shared library"
35+
${BUILD_SHARED_LIBS}
36+
)
3137
option(EGGS_ASSERT_ENABLE_INSTALL "Generate install rules" OFF)
3238

3339
# Map prefixed options back to the canonical names used below
3440
set(BUILD_EXAMPLE ${EGGS_ASSERT_BUILD_EXAMPLE})
41+
set(BUILD_SHARED_LIBS ${EGGS_ASSERT_BUILD_SHARED_LIBS})
3542
set(ENABLE_INSTALL ${EGGS_ASSERT_ENABLE_INSTALL})
3643
endif()
3744

3845
#
39-
# Library target — static library.
46+
# Library target — static or shared depending on BUILD_SHARED_LIBS.
4047
# Internal name _eggs_assert; downstream CMake code uses the alias Eggs::Assert.
4148
#
42-
add_library(_eggs_assert STATIC src/assert.cpp)
43-
target_sources(
49+
add_library(_eggs_assert src/assert.cpp)
50+
set_target_properties(
4451
_eggs_assert
45-
PUBLIC FILE_SET HEADERS BASE_DIRS include FILES include/eggs/assert.h
52+
PROPERTIES EXPORT_NAME Assert OUTPUT_NAME eggs_assert
4653
)
47-
set_target_properties(_eggs_assert PROPERTIES EXPORT_NAME Assert)
4854
target_compile_features(_eggs_assert PUBLIC cxx_std_23)
4955

56+
include(GenerateExportHeader)
57+
generate_export_header(
58+
_eggs_assert
59+
BASE_NAME EGGS_ASSERT
60+
EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/include/eggs/assert-export.h"
61+
)
62+
if(NOT BUILD_SHARED_LIBS)
63+
target_compile_definitions(_eggs_assert PUBLIC EGGS_ASSERT_STATIC_DEFINE)
64+
endif()
65+
66+
target_sources(
67+
_eggs_assert
68+
PUBLIC
69+
FILE_SET HEADERS
70+
BASE_DIRS include "${CMAKE_CURRENT_BINARY_DIR}/include"
71+
FILES
72+
include/eggs/assert.h
73+
"${CMAKE_CURRENT_BINARY_DIR}/include/eggs/assert-export.h"
74+
)
75+
5076
include(Cxx23Stacktrace)
5177
target_link_libraries(_eggs_assert PUBLIC ${EGGS_CXX23_STACKTRACE_LIB})
5278

@@ -70,6 +96,8 @@ if(ENABLE_INSTALL)
7096
EXPORT EggsAssertTargets
7197
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
7298
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
99+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
100+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
73101
)
74102

75103
install(

include/eggs/assert.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
#else
2020

21+
# include <eggs/assert-export.h>
22+
2123
# ifdef __cplusplus
22-
extern "C" [[noreturn]] void eggs_assert_failed(
24+
extern "C" [[noreturn]] EGGS_ASSERT_EXPORT void eggs_assert_failed(
2325
char const* message, char const* file, unsigned line, char const* function
2426
);
2527
# else
26-
_Noreturn void eggs_assert_failed(
28+
EGGS_ASSERT_EXPORT _Noreturn void eggs_assert_failed(
2729
char const* message, char const* file, unsigned line, char const* function
2830
);
2931
# endif

src/assert.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying
66
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

8+
#include <eggs/assert-export.h>
89
#include <eggs/assert.h>
910

1011
#include <cstdio>
@@ -29,7 +30,7 @@ void println(std::format_string<Args...> fmt, Args&&... args)
2930

3031
} // namespace
3132

32-
extern "C" [[noreturn]] void eggs_assert_failed(
33+
extern "C" [[noreturn]] EGGS_ASSERT_EXPORT void eggs_assert_failed(
3334
char const* message, char const* file, unsigned line, char const* function
3435
)
3536
{

0 commit comments

Comments
 (0)