Skip to content

Commit 04a4750

Browse files
committed
Change CMake
1 parent 0e931bb commit 04a4750

File tree

4 files changed

+73
-64
lines changed

4 files changed

+73
-64
lines changed

CMakeLists.txt

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
cmake_minimum_required(VERSION 3.30)
22

33
set(prev_name mysvac)
4-
set(lib_name json)
5-
set(package_name "mysvac-jsonlib")
6-
7-
set(CMAKE_CXX_STANDARD 20)
8-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9-
10-
if(MYSVAC_JSONLIB_ENABLE_TEST)
11-
set(CMAKE_CXX_STANDARD 23)
12-
endif ()
4+
set(lib_name jsonlib)
5+
set(package_name "${prev_name}-${lib_name}")
136

147
# Project declaration with metadata
158
project(${package_name}
@@ -27,68 +20,40 @@ file(GLOB_RECURSE cxx_header_files "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp")
2720

2821
# Library target creation
2922
# Create the main library target that can be built as either static or shared
30-
add_library(${lib_name})
23+
add_library(${lib_name} INTERFACE)
3124
# Create an alias target for namespaced usage
3225
add_library(${prev_name}::${lib_name} ALIAS ${lib_name})
3326

34-
# Default use unordered_map
35-
# If you want to use std::map instead, define M_MYSVAC_JSON_ORDERED_MAP
36-
# target_compile_definitions(${lib_name} PUBLIC
37-
# M_MYSVAC_JSON_UNORDERED_MAP=1 # Define to use std::unordered_map for JSON objects
38-
# )
39-
4027
# Library properties configuration
4128
set_target_properties(${lib_name} PROPERTIES
42-
# CXX_MODULE_STD ON # Enable C++23 standard library modules
4329
OUTPUT_NAME ${package_name} # Set the output file name
4430
)
45-
# Source files configuration
46-
# Add C++23 module files as PUBLIC sources (exported for consumers)
47-
target_sources(${lib_name} PUBLIC
48-
FILE_SET cxx_modules # Define a file set for modules
49-
TYPE CXX_MODULES # Specify this is a C++ modules file set
50-
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/modules # Base directory for module resolution
51-
FILES ${cxx_module_files} # List of module files to include
52-
)
5331
# Add header files as INTERFACE sources (header-only, not compiled)
5432
target_sources(${lib_name} INTERFACE
55-
FILE_SET cxx_headers # Define a file set for headers
56-
TYPE HEADERS # Specify this is a headers file set
57-
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include # Base directory for header resolution
58-
FILES ${cxx_header_files} # List of header files to include
33+
FILE_SET mysvac_json_cxx_headers # Define a file set for headers
34+
TYPE HEADERS # Specify this is a headers file set
35+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
36+
FILES ${cxx_header_files}
37+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/modules
38+
FILES ${cxx_module_files}
5939
)
6040
# Include directories configuration
6141
# Set up include paths for both build and install configurations
6242
target_include_directories(${lib_name} INTERFACE
6343
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> # Path during build
6444
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> # Path after installation
6545
)
66-
target_compile_features(${lib_name} PUBLIC cxx_std_20)
67-
68-
# Dynamic library configuration (optional)
69-
# Configure additional properties when building as a shared library
70-
if(BUILD_SHARED_LIBS)
71-
set_target_properties(${lib_name} PROPERTIES
72-
WINDOWS_EXPORT_ALL_SYMBOLS ON # Export all symbols on Windows (no explicit __declspec needed)
73-
VERSION ${PROJECT_VERSION} # Set the library version (used for .so.x.y.z on Linux)
74-
SOVERSION ${PROJECT_VERSION_MAJOR} # Set the ABI version (used for .so.x on Linux)
75-
)
76-
endif()
7746

7847
# Installation configuration
7948
# Include standard CMake modules for installation and packaging
8049
include(GNUInstallDirs) # Provides standard installation directory variables
8150
include(CMakePackageConfigHelpers) # Provides functions for creating config files
8251

83-
8452
# Target installation
8553
# Install the library target and its associated file sets
8654
install(TARGETS ${lib_name}
8755
EXPORT ${package_name}-targets # Export target for find_package() support
88-
FILE_SET cxx_modules
89-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # Install modules to include directory
90-
# Note: vct prefix is already included in module paths
91-
FILE_SET cxx_headers
56+
FILE_SET mysvac_json_cxx_headers
9257
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # Install headers to include directory
9358
# Note: vct prefix is already included in header paths
9459
)
@@ -112,7 +77,7 @@ configure_package_config_file(
11277
write_basic_package_version_file(
11378
${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config-version.cmake # Output version file
11479
VERSION ${PROJECT_VERSION} # Use project version
115-
COMPATIBILITY ExactVersion # Require exact version match (strict compatibility)
80+
COMPATIBILITY SameMinorVersion # Require exact version match (strict compatibility)
11681
)
11782

11883
# Install configuration files
@@ -123,11 +88,30 @@ install(FILES
12388
DESTINATION ${CMAKE_INSTALL_DATADIR}/${package_name} # Install to share/${package_name}/
12489
)
12590

91+
###################
92+
# utilities for testing or direct usage
93+
function(target_link_mysvac_jsonlib target visibility)
94+
if(NOT TARGET ${target})
95+
message(FATAL_ERROR "Target '${target}' does not exist!")
96+
endif()
97+
# add module target
98+
target_link_libraries(${target} ${visibility} mysvac::jsonlib)
99+
target_sources(${target} ${visibility}
100+
FILE_SET mysvac_json_cxx_modules
101+
TYPE CXX_MODULES
102+
BASE_DIRS ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/modules
103+
FILES ${cxx_module_files}
104+
)
105+
endfunction()
106+
###################
107+
108+
126109
# Testing configuration (optional)
127110
# Enable testing support
128-
if(MYSVAC_JSONLIB_ENABLE_TEST)
111+
if(MYSVAC_JSON_ENABLE_TEST)
129112
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test/CMakeLists.txt)
130113
enable_testing() # Enable CTest integration
131114
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test) # Add test subdirectory
132115
endif()
133116
endif()
117+

cmake/mysvac-jsonlib-config.cmake.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,22 @@
22

33
include(${CMAKE_CURRENT_LIST_DIR}/mysvac-jsonlib-targets.cmake)
44
check_required_components(mysvac-jsonlib)
5+
6+
set(MYSVAC_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/include")
7+
set(MYSVAC_DIR "${MYSVAC_INCLUDE_DIR}/mysvac")
8+
set(MYSVAC_JSON_DIR "${MYSVAC_INCLUDE_DIR}/mysvac")
9+
set(MYSVAC_JSON_MODULES "${MYSVAC_JSON_DIR}/json.ixx")
10+
11+
function(target_link_mysvac_jsonlib target visibility)
12+
if(NOT TARGET ${target})
13+
message(FATAL_ERROR "Target '${target}' does not exist!")
14+
endif()
15+
# add module target
16+
target_link_libraries(${target} ${visibility} mysvac::jsonlib)
17+
target_sources(${target} ${visibility}
18+
FILE_SET mysvac_json_cxx_modules
19+
TYPE CXX_MODULES
20+
BASE_DIRS ${MYSVAC_JSON_DIR}
21+
FILES ${MYSVAC_JSON_MODULES}
22+
)
23+
endfunction()

modules/mysvac/json.ixx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88
module;
99

10-
#ifndef M_MYSVAC_STD_MODULE
10+
#ifndef M_MYSVAC_JSON_ENABLE_STD_MODULE
1111

12+
#include <cctype>
1213
#include <cmath>
1314
#include <cstdint>
1415
#include <charconv>
@@ -31,7 +32,7 @@ module;
3132

3233
export module mysvac.json;
3334

34-
#ifdef M_MYSVAC_STD_MODULE
35+
#ifdef M_MYSVAC_JSON_ENABLE_STD_MODULE
3536

3637
import std;
3738

@@ -65,6 +66,8 @@ namespace mysvac::json {
6566
for (int i = 0; i <= 5; ++i) table['a' + i] = 10 + i;
6667
return table;
6768
}();
69+
70+
constexpr char hex_digits[] = "0123456789abcdef";
6871
}
6972

7073
/**
@@ -286,12 +289,14 @@ export namespace mysvac::json {
286289
case '\"': out.append(R"(\")"); break;
287290
case '\r': out.append(R"(\r)"); break;
288291
case '\n': out.append(R"(\n)"); break;
289-
case '\f': out.append(R"(\f)"); break;
292+
// case '\f': out.append(R"(\f)"); break; // do not support `\f` for better performance
290293
case '\t': out.append(R"(\t)"); break;
291-
case '\b': out.append(R"(\b)"); break;
294+
// case '\b': out.append(R"(\b)"); break; // do not support `\b` for better performance
292295
default: {
293296
if (static_cast<unsigned char>(c) < 0x20) {
294-
out.append(std::format("\\u{:04x}", static_cast<unsigned char>(c)));
297+
out.append(R"(\u00)");
298+
out.push_back(hex_digits[c >> 4]);
299+
out.push_back(hex_digits[c & 0x0F]);
295300
} else out.push_back(c);
296301
} break;
297302
}
@@ -312,12 +317,14 @@ export namespace mysvac::json {
312317
case '\"': out << R"(\")"; break;
313318
case '\r': out << R"(\r)"; break;
314319
case '\n': out << R"(\n)"; break;
315-
case '\f': out << R"(\f)"; break;
320+
// case '\f': out << R"(\f)"; break; // do not support `\f` for better performance
316321
case '\t': out << R"(\t)"; break;
317-
case '\b': out << R"(\b)"; break;
322+
// case '\b': out << R"(\b)"; break; // do not support `\b` for better performance
318323
default: {
319324
if (static_cast<unsigned char>(c) < 0x20) {
320-
out << std::format("\\u{:04x}", static_cast<unsigned char>(c));
325+
out << R"(\u00)";
326+
out.put(hex_digits[c >> 4]);
327+
out.put(hex_digits[c & 0x0F]);
321328
} else out.put(c);
322329
} break;
323330
}
@@ -467,11 +474,9 @@ export namespace mysvac::json {
467474
case 'u': case 'U': if (!unescape_unicode_next(res, it, end_ptr)) return std::nullopt; break;
468475
default: return std::nullopt;
469476
}
470-
} else if ( *it == '\b' || *it == '\n' || *it == '\f' || *it == '\r' /* || *it == '\t' */) {
471-
return std::nullopt;
472-
} else {
473-
res.push_back( *it );
474-
}
477+
// } else if ( *it == '\b' || *it == '\n' || *it == '\f' || *it == '\r' /* || *it == '\t' */) {
478+
// return std::nullopt;
479+
} else res.push_back( *it );
475480
++it;
476481
}
477482
if(it == end_ptr) return std::nullopt;
@@ -1676,7 +1681,7 @@ export namespace mysvac::json {
16761681
requires std::convertible_to<K, Str> && std::convertible_to<V, Json>
16771682
bool insert(K&& key, V&& value) noexcept {
16781683
if (type() == Type::eObj) {
1679-
std::get<Obj>(m_data).insert(static_cast<Str>(std::forward<K>(key)), static_cast<Json>(std::forward<V>(value)));
1684+
std::get<Obj>(m_data).emplace(static_cast<Str>(std::forward<K>(key)), static_cast<Json>(std::forward<V>(value)));
16801685
return true;
16811686
}
16821687
return false;
@@ -1692,7 +1697,7 @@ export namespace mysvac::json {
16921697
requires std::convertible_to<V, Json>
16931698
bool insert(const std::size_t index, V&& value) noexcept {
16941699
if (type() == Type::eArr && index <= std::get<Arr>(m_data).size()) {
1695-
std::get<Arr>(m_data).insert(std::get<Arr>(m_data).begin() + index, static_cast<Json>(std::forward<V>(value)));
1700+
std::get<Arr>(m_data).emplace(std::get<Arr>(m_data).begin() + index, static_cast<Json>(std::forward<V>(value)));
16961701
return true;
16971702
}
16981703
return false;
@@ -1707,7 +1712,7 @@ export namespace mysvac::json {
17071712
requires std::convertible_to<V, Json>
17081713
bool push_back(V&& value) noexcept {
17091714
if (type() == Type::eArr) {
1710-
std::get<Arr>(m_data).push_back( static_cast<Json>(std::forward<V>(value)) );
1715+
std::get<Arr>(m_data).emplace_back( static_cast<Json>(std::forward<V>(value)) );
17111716
return true;
17121717
}
17131718
return false;

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ file(GLOB_RECURSE test_cpp_files ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
1818
# file(GLOB_RECURSE example_cpp_files ${CMAKE_CURRENT_SOURCE_DIR}/../examples/*.cpp)
1919

2020
add_executable(test_json ${test_cpp_files})
21-
target_link_libraries(test_json PRIVATE ${prev_name}::${lib_name})
2221
target_link_libraries(test_json PRIVATE vct::test-unit)
2322

23+
target_link_mysvac_jsonlib(test_json PRIVATE)
24+
2425
target_compile_definitions(test_json PRIVATE
2526
CURRENT_PATH="${CMAKE_CURRENT_SOURCE_DIR}"
2627
)

0 commit comments

Comments
 (0)