Skip to content

Commit ea13c80

Browse files
authored
Merge pull request #40 from Doy-lee/cmake-make-version-file
Use cmake to generate the src/version.h file
2 parents 70cc278 + a25150e commit ea13c80

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ jobs:
4444
uses: microsoft/[email protected]
4545
if: runner.os == 'Windows'
4646

47-
- name: generate fake src/version.h so we can try to build
48-
shell: bash
49-
run: yarn update_version
50-
51-
5247
- name: build libsession-util-nodejs
5348
shell: bash
5449
run: yarn install --frozen-lockfile

CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
66

77
set(VERBOSE ON)
88

9+
# Get current commit hash as GIT_COMMIT
10+
find_package(Git)
11+
set(GIT_COMMIT "unknown") # Default value for when Git is not found
12+
if (EXISTS "${CMAKE_SOURCE_DIR}/.git/index" AND GIT_FOUND)
13+
execute_process(COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD RESULT_VARIABLE GIT_GET_HASH_RESULT OUTPUT_VARIABLE GIT_COMMIT OUTPUT_STRIP_TRAILING_WHITESPACE)
14+
if(GIT_GET_HASH_RESULT)
15+
message(STATUS "You are currently on commit ${GIT_COMMIT}")
16+
else()
17+
message(WARNING "Cannot determine current commit. Make sure that you are building either from a Git working tree or from a source archive.")
18+
endif()
19+
endif()
20+
21+
# Read the package.json file and extract the top-level .version as LIBSESSION_NODEJS_VERSION
22+
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/package.json" PACKAGE_JSON)
23+
string(JSON LIBSESSION_NODEJS_VERSION GET "${PACKAGE_JSON}" version)
24+
25+
# Generate src/version.h
26+
file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/src/version.h"
27+
"const char* LIBSESSION_NODEJS_VERSION = \"${LIBSESSION_NODEJS_VERSION}\";
28+
const char* LIBSESSION_NODEJS_COMMIT = \"${GIT_COMMIT}\";\n"
29+
)
30+
931
# Detect the number of processors
1032
include(ProcessorCount)
1133
ProcessorCount(N)
@@ -18,7 +40,6 @@ else()
1840
endif()
1941
message(STATUS "Number of processors detected: ${N}")
2042

21-
2243
add_definitions(-DNAPI_VERSION=8)
2344
set(CMAKE_CONFIGURATION_TYPES Release)
2445

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ git clone --recursive [email protected]:session-foundation/libsession-util-nodejs.g
2020
```
2121

2222
Always do your changes in `[FOLDER_NOT_IN_SESSION_DESKTOP]/libsession-util-nodejs`, never in the one under session-desktop's `node_modules` as you might override your local changes.
23-
Then, you can quickly compile a non-electron build by first running `yarn naughty-patch && yarn update_version` and then `yarn install` from that folder. You should only have to run `yarn naughty-patch && yarn update_version` manually once when you first setup the project. This is a quick incremental build which can check for C++ compilation errors.
23+
Then, you can quickly compile a non-electron build by first running `yarn naughty-patch` and then `yarn install` from that folder. You should only have to run `yarn naughty-patch` manually once when you first setup the project. This is a quick incremental build which can check for C++ compilation errors.
2424
Once your changes are ready to be tested in the `session-desktop` you can compile an electron build using this command:
2525

2626
```

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"email": "[email protected]"
1010
},
1111
"scripts": {
12-
"update_version": "sh update_version.sh",
1312
"clean": "rimraf .cache build",
1413
"lint:cpp": "cppcheck --std=c++20 -j8 --quiet src libsession-util/src",
1514
"install": "cmake-js build --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",

prepare_release.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ rm -f ./libsession_util_nodejs*.tar.gz
1313
python -m venv .venv
1414
. .venv/bin/activate
1515
pip install git-archive-all
16+
1617
PACKAGE_VERSION=$(node -p "require('./package.json').version")
17-
yarn update_version
18-
echo "PACKAGE_VERSION: $PACKAGE_VERSION"
18+
GIT_COMMIT=$(git rev-parse HEAD)
19+
20+
HEADER_PACKAGE_VERSION=$(grep 'LIBSESSION_NODEJS_VERSION' src/version.h | sed -E 's/.*"([0-9.]+)".*/\1/')
21+
HEADER_GIT_COMMIT=$(grep 'LIBSESSION_NODEJS_COMMIT' src/version.h | sed -E 's/.*"([A-Za-z0-9.]+)".*/\1/')
22+
23+
echo "Package: $PACKAGE_VERSION; Commit: $GIT_COMMIT"
24+
if [ "$PACKAGE_VERSION" != "$HEADER_PACKAGE_VERSION" ]; then
25+
echo "Error: Version mismatch! package.json version is $PACKAGE_VERSION, but src/version.h has $HEADER_PACKAGE_VERSION. Build the project first before packaging 'yarn install'"
26+
exit 1
27+
fi
28+
29+
if [ "$GIT_COMMIT" != "$HEADER_GIT_COMMIT" ]; then
30+
echo "Error: Version mismatch! Git commit is $GIT_COMMIT, but src/version.h has $HEADER_GIT_COMMIT. Build the project first before packaging 'yarn install'"
31+
exit 1
32+
fi
33+
1934
echo "Is '$PACKAGE_VERSION' the correct version? If yes, press 'y' to create the release. Press anything else to exit."
2035
read_char char_read
2136
case "$char_read" in

update_version.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)