Skip to content

Commit 50d2a17

Browse files
authored
fix version (#198)
* Refactor versioning system to use external version.txt file and add version parsing utilities in CMake. Update CMakeLists.txt to include version setup and remove hardcoded version numbers. * fix js-binding version
1 parent 9b6b61c commit 50d2a17

File tree

11 files changed

+226
-28
lines changed

11 files changed

+226
-28
lines changed

.github/workflows/npm-release.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ jobs:
2424
- name: Checkout code
2525
uses: actions/checkout@v4
2626

27+
- name: Synchronize Node.js binding version
28+
run: |
29+
echo "Synchronizing Node.js binding version with main project..."
30+
python3 scripts/sync_node_version.py
31+
2732
- name: Get package version
2833
id: package
2934
working-directory: libCacheSim-node
@@ -81,6 +86,11 @@ jobs:
8186
with:
8287
fetch-depth: 0
8388

89+
- name: Synchronize Node.js binding version
90+
run: |
91+
echo "Synchronizing Node.js binding version with main project..."
92+
python3 scripts/sync_node_version.py
93+
8494
- name: Setup Node.js
8595
uses: actions/setup-node@v4
8696
with:
@@ -140,6 +150,11 @@ jobs:
140150
- name: Checkout code
141151
uses: actions/checkout@v4
142152

153+
- name: Synchronize Node.js binding version
154+
run: |
155+
echo "Synchronizing Node.js binding version with main project..."
156+
python3 scripts/sync_node_version.py
157+
143158
- name: Setup Node.js
144159
uses: actions/setup-node@v4
145160
with:

CMakeLists.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ project(libCacheSim)
33
set(DESCRIPTION "a high performance cache simulation library")
44
set(PROJECT_WEB "http://cachesim.com")
55

6-
set(${PROJECT_NAME}_VERSION_MAJOR 0)
7-
set(${PROJECT_NAME}_VERSION_MINOR 1)
8-
set(${PROJECT_NAME}_VERSION_PATCH 0)
9-
set(${PROJECT_NAME}_RELEASE_VERSION ${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR})
10-
set(${PROJECT_NAME}_VERSION ${${PROJECT_NAME}_RELEASE_VERSION}.${${PROJECT_NAME}_VERSION_PATCH})
6+
# Include version utilities
7+
include(cmake/Version.cmake)
8+
9+
# Setup version from version.txt
10+
setup_project_version(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/version.txt")
1111

1212
set(CMAKE_CXX_STANDARD 17)
1313
set(CMAKE_CXX_STANDARD_REQUIRED On)

cmake/Version.cmake

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1+
# Version parsing utilities for libCacheSim
2+
# Based on xgboost version handling
13

2-
# from xgboost
3-
function (write_version)
4-
message(STATUS "xgboost VERSION: ${xgboost_VERSION}")
5-
configure_file(
6-
${xgboost_SOURCE_DIR}/include/config.h.in
7-
${xgboost_SOURCE_DIR}/include/config.h @ONLY)
4+
function(parse_version_from_file VERSION_FILE VERSION_MAJOR_VAR VERSION_MINOR_VAR VERSION_PATCH_VAR)
5+
if(EXISTS "${VERSION_FILE}")
6+
file(READ "${VERSION_FILE}" VERSION_STRING)
7+
string(STRIP "${VERSION_STRING}" VERSION_STRING)
8+
9+
# Parse MAJOR.MINOR.PATCH format
10+
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" VERSION_MATCH "${VERSION_STRING}")
11+
if(VERSION_MATCH)
12+
set(${VERSION_MAJOR_VAR} ${CMAKE_MATCH_1} PARENT_SCOPE)
13+
set(${VERSION_MINOR_VAR} ${CMAKE_MATCH_2} PARENT_SCOPE)
14+
set(${VERSION_PATCH_VAR} ${CMAKE_MATCH_3} PARENT_SCOPE)
15+
message(STATUS "Version from ${VERSION_FILE}: ${VERSION_STRING}")
16+
else()
17+
message(FATAL_ERROR "Invalid version format in ${VERSION_FILE}: ${VERSION_STRING}. Expected format: MAJOR.MINOR.PATCH")
18+
endif()
19+
else()
20+
message(FATAL_ERROR "Version file not found: ${VERSION_FILE}")
21+
endif()
22+
endfunction()
23+
24+
function(setup_project_version PROJECT_NAME VERSION_FILE)
25+
if(EXISTS "${VERSION_FILE}")
26+
parse_version_from_file("${VERSION_FILE}" VERSION_MAJOR VERSION_MINOR VERSION_PATCH)
27+
set(${PROJECT_NAME}_VERSION_MAJOR ${VERSION_MAJOR} PARENT_SCOPE)
28+
set(${PROJECT_NAME}_VERSION_MINOR ${VERSION_MINOR} PARENT_SCOPE)
29+
set(${PROJECT_NAME}_VERSION_PATCH ${VERSION_PATCH} PARENT_SCOPE)
30+
set(${PROJECT_NAME}_RELEASE_VERSION ${VERSION_MAJOR}.${VERSION_MINOR} PARENT_SCOPE)
31+
set(${PROJECT_NAME}_VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} PARENT_SCOPE)
32+
else()
33+
message(STATUS "${VERSION_FILE} not found, using default version")
34+
set(${PROJECT_NAME}_VERSION_MAJOR 0 PARENT_SCOPE)
35+
set(${PROJECT_NAME}_VERSION_MINOR 1 PARENT_SCOPE)
36+
set(${PROJECT_NAME}_VERSION_PATCH 0 PARENT_SCOPE)
37+
set(${PROJECT_NAME}_RELEASE_VERSION 0.1 PARENT_SCOPE)
38+
set(${PROJECT_NAME}_VERSION 0.1.0 PARENT_SCOPE)
39+
endif()
40+
endfunction()
41+
42+
# Legacy function for xgboost compatibility
43+
function(write_version)
44+
message(STATUS "xgboost VERSION: ${xgboost_VERSION}")
45+
configure_file(
46+
${xgboost_SOURCE_DIR}/include/config.h.in
47+
${xgboost_SOURCE_DIR}/include/config.h @ONLY)
848
# configure_file(
949
# ${xgboost_SOURCE_DIR}/cmake/Python_version.in
1050
# ${xgboost_SOURCE_DIR}/python-package/xgboost/VERSION @ONLY)
11-
endfunction (write_version)
51+
endfunction(write_version)

libCacheSim-node/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ console.log('Simulation results:', result);
4646

4747
## API Reference
4848

49+
### `getVersion()`
50+
51+
Get the version of the libCacheSim Node.js binding.
52+
53+
**Returns:** String containing the version number (e.g., "1.0.1")
54+
4955
### `runSimulation(tracePath, traceType, algorithm, cacheSize)`
5056

5157
Run a cache simulation with specified parameters.
@@ -100,6 +106,9 @@ npm install -g libcachesim-node
100106

101107
# Run simulation from command line
102108
cachesim-js --trace /path/to/trace.vscsi --algorithm s3fifo --size 1mb
109+
110+
# Check version
111+
cachesim-js --version
103112
```
104113

105114
## Development
@@ -132,6 +141,16 @@ npm test
132141
- Linux: `build-essential cmake libglib2.0-dev libzstd-dev`
133142
- macOS: `cmake glib zstd` (via Homebrew)
134143

144+
### Version Synchronization
145+
146+
The Node.js binding version is automatically synchronized with the main libCacheSim project version. To manually sync versions, run:
147+
148+
```bash
149+
python3 scripts/sync_node_version.py
150+
```
151+
152+
This ensures that the Node.js binding version in `package.json` matches the version in the main project's `version.txt` file.
153+
135154
## Architecture
136155

137156
This package uses `prebuild-install` for binary distribution:

libCacheSim-node/cli.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function parseArgs() {
1111
type: null,
1212
algorithm: null,
1313
size: null,
14-
help: false
14+
help: false,
15+
version: false
1516
};
1617

1718
for (let i = 0; i < args.length; i++) {
@@ -35,6 +36,10 @@ function parseArgs() {
3536
case '-h':
3637
options.help = true;
3738
break;
39+
case '--version':
40+
case '-v':
41+
options.version = true;
42+
break;
3843
default:
3944
console.error(`Unknown option: ${args[i]}`);
4045
process.exit(1);
@@ -60,6 +65,7 @@ Options:
6065
--size, -s <size> Cache size (required)
6166
Examples: 1mb, 512kb, 2gb, 1024 (bytes)
6267
--help, -h Show this help message
68+
--version, -v Show version information
6369
6470
Examples:
6571
cachesim-js -t trace.vscsi --type vscsi -a lru -s 10mb
@@ -75,6 +81,11 @@ function main() {
7581
return;
7682
}
7783

84+
if (options.version) {
85+
console.log(`libcachesim-node v${libCacheSim.getVersion()}`);
86+
return;
87+
}
88+
7889
// Check that all required parameters are provided
7990
if (!options.trace || !options.type || !options.algorithm || !options.size) {
8091
console.error('Error: All parameters are required.');

libCacheSim-node/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,30 @@ function getSupportedTraceTypes() {
3737
return ['vscsi', 'csv', 'txt', 'binary', 'oracle'];
3838
}
3939

40+
/**
41+
* Get the version of the libCacheSim Node.js binding
42+
* @returns {string} Version string
43+
*/
44+
function getVersion() {
45+
try {
46+
const packageJson = require('./package.json');
47+
return packageJson.version;
48+
} catch (error) {
49+
return 'unknown';
50+
}
51+
}
52+
4053
module.exports = {
4154
runSimulation,
4255
runSim,
4356
getSupportedAlgorithms,
44-
getSupportedTraceTypes
57+
getSupportedTraceTypes,
58+
getVersion
4559
};
4660

4761
// Example usage if run directly
4862
if (require.main === module) {
49-
console.log('libCacheSim Node.js Bindings');
63+
console.log(`libCacheSim Node.js Bindings v${getVersion()}`);
5064
console.log('Supported algorithms:', getSupportedAlgorithms());
5165
console.log('Supported trace types:', getSupportedTraceTypes());
5266

libCacheSim-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libcachesim-node",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"main": "index.js",
55
"bin": {
66
"cachesim-js": "./cli.js"

libCacheSim-node/test.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ function runTests() {
5353
assert(typeof libCacheSim.runSim === 'function', 'runSim function exists') &&
5454
assert(typeof libCacheSim.runSimulation === 'function', 'runSimulation function exists') &&
5555
assert(typeof libCacheSim.getSupportedAlgorithms === 'function', 'getSupportedAlgorithms function exists') &&
56-
assert(typeof libCacheSim.getSupportedTraceTypes === 'function', 'getSupportedTraceTypes function exists');
56+
assert(typeof libCacheSim.getSupportedTraceTypes === 'function', 'getSupportedTraceTypes function exists') &&
57+
assert(typeof libCacheSim.getVersion === 'function', 'getVersion function exists');
5758
});
5859

5960
// Test 2: Check supported algorithms
@@ -75,7 +76,15 @@ function runTests() {
7576
assert(traceTypes.includes('csv'), 'Includes CSV');
7677
});
7778

78-
// Test 4: Run default simulation
79+
// Test 4: Check version function
80+
test('Get version', () => {
81+
const version = libCacheSim.getVersion();
82+
return assert(typeof version === 'string', 'Returns a string') &&
83+
assert(version.length > 0, 'Version is not empty') &&
84+
assert(/^\d+\.\d+\.\d+/.test(version), 'Version follows semantic versioning format');
85+
});
86+
87+
// Test 5: Run default simulation
7988
test('Run default simulation', () => {
8089
const result = libCacheSim.runSim();
8190
return assert(typeof result === 'object', 'Returns an object') &&
@@ -88,7 +97,7 @@ function runTests() {
8897
assert(Math.abs(result.hitRatio + result.missRatio - 1.0) < 0.0001, 'Hit ratio + miss ratio ≈ 1.0');
8998
});
9099

91-
// Test 5: Run custom simulation with different algorithms
100+
// Test 6: Run custom simulation with different algorithms
92101
test('Run custom simulations with different algorithms', () => {
93102
const algorithms = ['lru', 'fifo', 's3fifo'];
94103
let allPassed = true;
@@ -110,7 +119,7 @@ function runTests() {
110119
return allPassed;
111120
});
112121

113-
// Test 6: Test different cache sizes
122+
// Test 7: Test different cache sizes
114123
test('Test different cache sizes', () => {
115124
const sizes = ['512kb', '1mb', '2mb'];
116125
let allPassed = true;
@@ -131,7 +140,7 @@ function runTests() {
131140
return allPassed;
132141
});
133142

134-
// Test 7: Error handling - invalid trace file
143+
// Test 8: Error handling - invalid trace file
135144
test('Error handling for invalid trace file', () => {
136145
try {
137146
libCacheSim.runSimulation('/nonexistent/file.vscsi', 'vscsi', 'lru', '1mb');
@@ -141,7 +150,7 @@ function runTests() {
141150
}
142151
});
143152

144-
// Test 8: Error handling - invalid algorithm
153+
// Test 9: Error handling - invalid algorithm
145154
test('Error handling for invalid algorithm', () => {
146155
try {
147156
libCacheSim.runSimulation('../data/cloudPhysicsIO.vscsi', 'vscsi', 'invalid_algo', '1mb');
@@ -151,7 +160,7 @@ function runTests() {
151160
}
152161
});
153162

154-
// Test 9: Error handling - invalid trace type
163+
// Test 10: Error handling - invalid trace type
155164
test('Error handling for invalid trace type', () => {
156165
try {
157166
libCacheSim.runSimulation('../data/cloudPhysicsIO.vscsi', 'invalid_type', 'lru', '1mb');
@@ -161,7 +170,7 @@ function runTests() {
161170
}
162171
});
163172

164-
// Test 10: Performance test - measure execution time
173+
// Test 11: Performance test - measure execution time
165174
test('Performance measurement', () => {
166175
const startTime = process.hrtime.bigint();
167176
const result = libCacheSim.runSim();

libCacheSim/include/config.h.in

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
21
/* not used */
32

43
#define VERSION_MAJOR @libCacheSim_VERSION_MAJOR @
54
#define VERSION_MINOR @libCacheSim_VERSION_MINOR @
65
#define VERSION_PATCH @libCacheSim_VERSION_PATCH @
76

8-
#define VERSION_STRING \
9-
"@libCacheSim_VERSION_MAJOR@.@libCacheSim_VERSION_MINOR@.@libCacheSim_" \
10-
"VERSION_PATCH@"
7+
#define VERSION_STRING "@libCacheSim_VERSION_MAJOR@.@libCacheSim_VERSION_MINOR@.@libCacheSim_VERSION_PATCH@"
118

129
#cmakedefine HASH_TYPE @HASH_TYPE @
1310

0 commit comments

Comments
 (0)