Skip to content

Commit 6ae46d5

Browse files
committed
CMake: gate -nostdlib for executables via GITLEDGER_USE_NOSTDLIB (OFF); keep -nostdlib on library (non‑MSVC)
1 parent 2ade8e0 commit 6ae46d5

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if(MSVC)
2626
else()
2727

2828
include(CheckCCompilerFlag)
29+
option(GITLEDGER_USE_NOSTDLIB "Link executables with -nostdlib (non-MSVC)" OFF)
2930
# Project policy: strict warnings, C99, no fun, visibility hidden.
3031
set(PROJECT_WARNING_FLAGS -Wall -Wextra -Werror -pedantic -Wshadow -Wconversion -fvisibility=hidden -std=c99)
3132
check_c_compiler_flag("-Wno-fun" COMPILER_SUPPORTS_WNOFUN)
@@ -54,10 +55,14 @@ add_library(gitledger STATIC ${LIBGITLEDGER_SOURCES} ${LIBGITLEDGER_HEADERS})
5455
target_compile_options(gitledger PRIVATE ${PROJECT_WARNING_FLAGS})
5556
target_compile_definitions(gitledger PRIVATE GITLEDGER_BUILD=1)
5657
function(add_nostdlib target)
57-
if(NOT MSVC)
58+
if(GITLEDGER_USE_NOSTDLIB AND NOT MSVC)
5859
target_link_options(${target} PRIVATE -nostdlib)
5960
endif()
6061
endfunction()
62+
if(NOT MSVC)
63+
# Keep library pure/freestanding from the linker perspective.
64+
target_link_options(gitledger PRIVATE -nostdlib)
65+
endif()
6166

6267
if(GITLEDGER_VENDOR_LIBGIT2)
6368
message(FATAL_ERROR "Vendored libgit2 support is not implemented yet.\n"
@@ -130,7 +135,6 @@ if(BUILD_TESTING)
130135
add_test(NAME gitledger_cli_smoke COMMAND gitledger_tests)
131136
add_test(NAME error COMMAND gitledger_error_test)
132137
endif()
133-
add_nostdlib(gitledger)
134138
add_nostdlib(gitledger_version_test)
135139
add_nostdlib(gitledger_tests)
136140
add_nostdlib(mg-ledger)

0 commit comments

Comments
 (0)