Skip to content

Commit

Permalink
feat: now just include the project with add_subdirectory and done
Browse files Browse the repository at this point in the history
  • Loading branch information
Svalorzen committed Mar 15, 2024
1 parent 97bb2bc commit a78f46d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 31 deletions.
14 changes: 4 additions & 10 deletions GitHash.cmake → CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
### HOW TO USE ###
#
# To use GitHash, you need to add this file to your project. `include(...)`
# this file from your main CMakeLists.txt file, and run the `SetupGitHash()`
# function.
#
# You then just need to link your targets against ${GITHASH_LIBRARIES}, and add
# the `GitHash.hpp` file to your C++ project.
cmake_minimum_required(VERSION 3.25.3)

###################################
###### CUSTOMIZATION POINTS #######
Expand Down Expand Up @@ -67,8 +60,6 @@ if (NOT DEFINED GitHash_SourceDir)
set(GitHash_SourceDir "${CMAKE_SOURCE_DIR}")
endif()

message(${CMAKE_SOURCE_DIR})

function(SetupGitHash)
# Run this script when building. Note how we pass all variables we need, since we will not get them automatically
# and even the CMake source dir might be wrong (if for example the build folder is outside the original path)
Expand All @@ -84,6 +75,7 @@ function(SetupGitHash)

# Create library for user
add_library(githash ${GitHash_CppFile})
target_include_directories(githash PUBLIC "${CMAKE_CURRENT_LIST_DIR}/include")
add_dependencies(githash CheckGitHash)

# Output library name to the other CMakeLists.txt
Expand Down Expand Up @@ -159,4 +151,6 @@ endfunction()
# This is used to run this function from an external cmake process.
if (RUN_UPDATE_GIT_HASH)
UpdateGitHash()
else()
SetupGitHash()
endif()
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,24 @@ last compiled one.
Setup
-----

To use GitHash in your project, you only need to follow these steps:
- Copy the file `GitHash.cmake` in your project's `cmake/Modules` folder.
- Copy the file `GitHash.hpp` to your project's include directory.
- Add the following two lines to your main `CMakeLists.txt` file:
```
include(${PROJECT_SOURCE_DIR}/cmake/Modules/GitHash.cmake)
SetupGitHash()
```
To use GitHash in your project, you need to:
- Clone the project in some sub-folder of your project, and add to your main
`CMakeLists.txt` file the following line:
```
add_subdirectory(<GitHash_Dir_Path>)`
```
- Use these variables in your C++ files:
```
GitHash::branch; // C-string
GitHash::sha1; // C-string
GitHash::shortSha1; // C-string
GitHash::dirty; // boolean
```
- In CMake, use `target_link_libraries` to link your project to the GitHash
library (using the `${GITHASH_LIBRARIES}` variable).
```
GitHash::branch; // C-string
GitHash::sha1; // C-string
GitHash::shortSha1; // C-string
GitHash::dirty; // boolean
```
- In CMake, link your project to the GitHash library using the
`${GITHASH_LIBRARIES}` variable:
```
target_link_libraries(your_project "${GITHASH_LIBRARIES}")
```

Note that currently the `GitHash` library is outputted in a subfolder of your
`PROJECT_BINARY_DIR`. If this is not desired you will have to manually modify
Expand Down
4 changes: 1 addition & 3 deletions exampleProject/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
cmake_minimum_required(VERSION 3.9)
project(SomeApp)

include(${PROJECT_SOURCE_DIR}/cmake/Modules/GitHash.cmake)
SetupGitHash()
add_subdirectory(deps/GitHash)

include(CTest)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
1 change: 0 additions & 1 deletion exampleProject/cmake/Modules/GitHash.cmake

This file was deleted.

1 change: 1 addition & 0 deletions exampleProject/deps/GitHash
1 change: 0 additions & 1 deletion exampleProject/include/GitHash.hpp

This file was deleted.

3 changes: 3 additions & 0 deletions exampleProject/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
add_executable(main main.cpp)

# This automatically adds the GitHash header file dependency to your project
target_link_libraries(main ${GITHASH_LIBRARIES})

add_test(NAME main_test WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMAND $<TARGET_FILE:main>)
File renamed without changes.

0 comments on commit a78f46d

Please sign in to comment.