Skip to content

Commit e5f8f8c

Browse files
committed
Use object library
1 parent aae3fa0 commit e5f8f8c

4 files changed

Lines changed: 44 additions & 12 deletions

File tree

cmake/compileroptions.cmake

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,25 @@ function(target_externals_include_directories TARGET)
2424
endif()
2525
endfunction()
2626

27-
function(target_dll_compile_definitions TARGET EXPORT IMPORT)
27+
function(target_dll_compile_definitions TARGET)
28+
set(options)
29+
set(oneValueArgs IMPORT EXPORT)
30+
set(multiValueArgs)
31+
32+
cmake_parse_arguments(PARSE "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
33+
if(PARSE_UNPARSED_ARGUMENTS)
34+
message(
35+
FATAL_ERROR "Unknown keywords given to target_dll_compile_definitions(): \"${PARSE_UNPARSED_ARGUMENTS}\"")
36+
endif()
37+
38+
2839
if (BUILD_SHARED_LIBS AND MSVC)
29-
target_compile_definitions(simplecpp_objs PRIVATE ${EXPORT})
30-
target_compile_definitions(simplecpp_objs INTERFACE ${IMPORT})
40+
if(PARSE_EXPORT)
41+
target_compile_definitions(simplecpp_objs PRIVATE ${PARSE_EXPORT})
42+
endif()
43+
if(PARSE_IMPORT)
44+
target_compile_definitions(simplecpp_objs INTERFACE ${PARSE_IMPORT})
45+
endif()
3146
endif()
3247
endfunction()
3348

externals/simplecpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ file(GLOB hdrs "*.h")
22
file(GLOB srcs "*.cpp")
33

44
add_library(simplecpp ${srcs} ${hdrs})
5-
target_dll_compile_definitions(simplecpp SIMPLECPP_EXPORT SIMPLECPP_IMPORT)
5+
target_dll_compile_definitions(simplecpp EXPORT SIMPLECPP_EXPORT IMPORT SIMPLECPP_IMPORT)
66

77
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
88
target_compile_options_safe(simplecpp -Wno-zero-as-null-pointer-constant)

externals/tinyxml2/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ file(GLOB hdrs "*.h")
22
file(GLOB srcs "*.cpp")
33

44
add_library(tinyxml2 ${srcs} ${hdrs})
5-
target_dll_compile_definitions(tinyxml2 TINYXML2_EXPORT TINYXML2_IMPORT)
5+
target_dll_compile_definitions(tinyxml2 EXPORT TINYXML2_EXPORT IMPORT TINYXML2_IMPORT)
66

77
# TODO: needs to be fixed upstream
88
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

lib/CMakeLists.txt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,39 @@ else()
3737
set(srcs_lib ${srcs})
3838
endif()
3939

40-
add_library(cppcheck-core ${srcs_lib} ${hdrs})
41-
target_dll_compile_definitions(simplecpp_objs CPPCHECKLIB_EXPORT CPPCHECKLIB_IMPORT)
40+
if(BUILD_SHARED_LIBS)
41+
add_library(cppcheck-core ${srcs_lib} ${hdrs})
42+
add_library(cppcheck-core-private ALIAS cppcheck-core)
43+
else()
44+
# A OBJECT library is used because the auto-registration doesn't work with static libraries
45+
add_library(cppcheck-core-private OBJECT ${srcs_lib} ${hdrs})
46+
add_library(cppcheck-core INTERFACE)
47+
target_link_libraries(cppcheck-core INTERFACE $<TARGET_OBJECTS:cppcheck-core-private>)
48+
add_dependencies(cppcheck-core cppcheck-core-private)
49+
endif()
4250
if (BUILD_SHARED_LIBS AND MSVC)
4351
target_sources(cppcheck-core PRIVATE version.rc)
4452
endif()
4553

46-
target_include_directories(cppcheck-core PUBLIC .)
47-
target_link_libraries(cppcheck-core PRIVATE tinyxml2 simplecpp picojson)
54+
target_dll_compile_definitions(cppcheck-core EXPORT CPPCHECKLIB_EXPORT)
55+
target_dll_compile_definitions(cppcheck-core-private IMPORT CPPCHECKLIB_IMPORT)
56+
57+
target_include_directories(cppcheck-core INTERFACE .)
58+
foreach(lib tinyxml2 simplecpp picojson)
59+
if (NOT BUILD_SHARED_LIBS)
60+
target_link_libraries(cppcheck-core INTERFACE $<LINK_ONLY:${lib}>)
61+
endif()
62+
target_link_libraries(cppcheck-core-private PRIVATE ${lib})
63+
endforeach()
4864

4965
if (HAVE_RULES)
50-
target_include_directories(cppcheck-core SYSTEM PRIVATE ${PCRE_INCLUDE})
66+
target_include_directories(cppcheck-core-private SYSTEM PRIVATE ${PCRE_INCLUDE})
5167
endif()
5268
if (Boost_FOUND)
53-
target_include_directories(cppcheck-core SYSTEM PUBLIC ${Boost_INCLUDE_DIRS})
69+
target_include_directories(cppcheck-core SYSTEM INTERFACE ${Boost_INCLUDE_DIRS})
70+
target_include_directories(cppcheck-core-private SYSTEM PRIVATE ${Boost_INCLUDE_DIRS})
5471
endif()
5572

5673
if (NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
57-
target_precompile_headers(cppcheck-core PRIVATE precompiled.h)
74+
target_precompile_headers(cppcheck-core-private PRIVATE precompiled.h)
5875
endif()

0 commit comments

Comments
 (0)