Skip to content

Commit a58cfe4

Browse files
committed
Support BUILD_SHARED_LIBS
1 parent 8a5e88b commit a58cfe4

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,50 @@ 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)
49+
add_library(_eggs_assert src/assert.cpp)
50+
set_target_properties(_eggs_assert PROPERTIES EXPORT_NAME Assert)
51+
target_compile_features(_eggs_assert PUBLIC cxx_std_23)
52+
53+
include(GenerateExportHeader)
54+
generate_export_header(
55+
_eggs_assert
56+
BASE_NAME EGGS_ASSERT
57+
EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/include/eggs/assert-export.h"
58+
)
59+
if(NOT BUILD_SHARED_LIBS)
60+
target_compile_definitions(_eggs_assert PUBLIC EGGS_ASSERT_STATIC_DEFINE)
61+
endif()
62+
4363
target_sources(
4464
_eggs_assert
45-
PUBLIC FILE_SET HEADERS BASE_DIRS include FILES include/eggs/assert.h
65+
PUBLIC
66+
FILE_SET HEADERS
67+
BASE_DIRS include "${CMAKE_CURRENT_BINARY_DIR}/include"
68+
FILES
69+
include/eggs/assert.h
70+
"${CMAKE_CURRENT_BINARY_DIR}/include/eggs/assert-export.h"
4671
)
47-
set_target_properties(_eggs_assert PROPERTIES EXPORT_NAME Assert)
48-
target_compile_features(_eggs_assert PUBLIC cxx_std_23)
4972

5073
include(Cxx23Stacktrace)
5174
target_link_libraries(_eggs_assert PUBLIC ${EGGS_CXX23_STACKTRACE_LIB})
@@ -70,6 +93,8 @@ if(ENABLE_INSTALL)
7093
EXPORT EggsAssertTargets
7194
FILE_SET HEADERS DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
7295
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
96+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
97+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
7398
)
7499

75100
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)