@@ -6,7 +6,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
66set (CMAKE_CXX_EXTENSIONS OFF )
77
88option (BUILD_SHARED_LIBS "Build shared libraries (set to OFF to build static libs)" ON )
9- option (PYSTRING_HEADER_ONLY "Build as header-only library" OFF )
109
1110# If the user hasn't configured cmake with an explicit
1211# -DCMAKE_INSTALL_PREFIX=..., then set it to safely install into ./dist, to
@@ -19,52 +18,86 @@ endif()
1918message (STATUS "Installation path will be ${CMAKE_INSTALL_PREFIX} " )
2019include (GNUInstallDirs )
2120
22- if (PYSTRING_HEADER_ONLY)
23- message (STATUS "Building pystring as header-only library" )
24- add_library (pystring INTERFACE )
25-
26- target_compile_definitions (pystring INTERFACE PYSTRING_HEADER_ONLY )
27-
28- target_include_directories (pystring INTERFACE
29- $<BUILD_INTERFACE :${CMAKE_CURRENT_SOURCE_DIR} >
30- $<INSTALL_INTERFACE :${CMAKE_INSTALL_INCLUDEDIR} >
31- )
32-
33- # Install both headers for header-only mode
34- install (FILES pystring.h pystring_impl.h
35- DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /${PROJECT_NAME}
36- )
37- else ()
38- message (STATUS "Building pystring as compiled library" )
39-
40- add_library (pystring
41- pystring.cpp
42- pystring.h
43- )
44-
45- set_target_properties (pystring PROPERTIES
46- VERSION ${PROJECT_VERSION}
47- SOVERSION ${PROJECT_VERSION_MAJOR}
48- )
49-
50- install (TARGETS pystring
51- LIBRARY DESTINATION lib
52- RUNTIME DESTINATION bin
53- ARCHIVE DESTINATION lib
54- )
55-
56- install (FILES pystring.h
57- DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /${PROJECT_NAME}
58- COMPONENT developer
59- )
60-
61- endif ()
62-
63- # Test executable
64-
65- add_executable (pystring_test test .cpp )
66- TARGET_LINK_LIBRARIES (pystring_test pystring )
21+ # --- Compiled library target: pystring::pystring ---
22+ add_library (pystring
23+ pystring.cpp
24+ pystring.h
25+ )
26+
27+ set_target_properties (pystring PROPERTIES
28+ VERSION ${PROJECT_VERSION}
29+ SOVERSION ${PROJECT_VERSION_MAJOR}
30+ )
31+
32+ target_include_directories (pystring PUBLIC
33+ $<BUILD_INTERFACE :${CMAKE_CURRENT_SOURCE_DIR} >
34+ $<INSTALL_INTERFACE :${CMAKE_INSTALL_INCLUDEDIR} >
35+ )
36+
37+ install (TARGETS pystring
38+ EXPORT pystringTargets
39+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
40+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
41+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
42+ )
43+
44+
45+ # --- Header-only target: pystring::pystring_header_only ---
46+ add_library (pystring_header_only INTERFACE )
47+
48+ target_compile_definitions (pystring_header_only INTERFACE PYSTRING_HEADER_ONLY )
49+
50+ target_include_directories (pystring_header_only INTERFACE
51+ $<BUILD_INTERFACE :${CMAKE_CURRENT_SOURCE_DIR} >
52+ $<INSTALL_INTERFACE :${CMAKE_INSTALL_INCLUDEDIR} >
53+ )
54+
55+ install (TARGETS pystring_header_only
56+ EXPORT pystringTargets
57+ )
58+
59+ install (FILES pystring.h pystring_impl.h
60+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} /${PROJECT_NAME}
61+ )
62+
63+ # --- Export & package config ---
64+ install (EXPORT pystringTargets
65+ FILE pystringTargets.cmake
66+ NAMESPACE pystring::
67+ DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/pystring
68+ )
69+
70+ include (CMakePackageConfigHelpers )
71+
72+ configure_package_config_file (
73+ cmake/pystringConfig.cmake.in
74+ pystringConfig.cmake
75+ INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/pystring
76+ )
77+
78+ write_basic_package_version_file (
79+ pystringConfigVersion.cmake
80+ VERSION ${PROJECT_VERSION}
81+ COMPATIBILITY SameMajorVersion
82+ )
83+
84+ install (FILES
85+ ${CMAKE_CURRENT_BINARY_DIR} /pystringConfig.cmake
86+ ${CMAKE_CURRENT_BINARY_DIR} /pystringConfigVersion.cmake
87+ DESTINATION ${CMAKE_INSTALL_LIBDIR} /cmake/pystring
88+ )
89+
90+ # --- Tests ---
91+ add_executable (pystring_test test .cpp )
92+ target_link_libraries (pystring_test pystring )
93+
94+ add_executable (pystring_test_header_only test .cpp )
95+ target_link_libraries (pystring_test_header_only pystring_header_only )
96+
97+ # Compile-time check that PYSTRING_HEADER_ONLY propagates correctly
98+ add_executable (pystring_test_header_only_define test_header_only_define.cpp )
99+ target_link_libraries (pystring_test_header_only_define pystring_header_only )
67100
68101enable_testing ()
69102add_test (NAME PyStringTest COMMAND pystring_test )
70-
103+ add_test ( NAME PyStringTestHeaderOnly COMMAND pystring_test_header_only )
0 commit comments