From a78f46dc02e5c777d1ffa31a7ddca986cd928b66 Mon Sep 17 00:00:00 2001 From: Svalorzen Date: Fri, 15 Mar 2024 21:54:03 +0100 Subject: [PATCH] feat: now just include the project with add_subdirectory and done --- GitHash.cmake => CMakeLists.txt | 14 +++------ README.md | 33 +++++++++++----------- exampleProject/CMakeLists.txt | 4 +-- exampleProject/cmake/Modules/GitHash.cmake | 1 - exampleProject/deps/GitHash | 1 + exampleProject/include/GitHash.hpp | 1 - exampleProject/src/CMakeLists.txt | 3 ++ GitHash.hpp => include/GitHash.hpp | 0 8 files changed, 26 insertions(+), 31 deletions(-) rename GitHash.cmake => CMakeLists.txt (94%) delete mode 120000 exampleProject/cmake/Modules/GitHash.cmake create mode 120000 exampleProject/deps/GitHash delete mode 120000 exampleProject/include/GitHash.hpp rename GitHash.hpp => include/GitHash.hpp (100%) diff --git a/GitHash.cmake b/CMakeLists.txt similarity index 94% rename from GitHash.cmake rename to CMakeLists.txt index 6cf2092..faadd5a 100644 --- a/GitHash.cmake +++ b/CMakeLists.txt @@ -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 ####### @@ -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) @@ -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 @@ -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() diff --git a/README.md b/README.md index dedbcb2..832ba5d 100644 --- a/README.md +++ b/README.md @@ -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()` + ``` - 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 diff --git a/exampleProject/CMakeLists.txt b/exampleProject/CMakeLists.txt index 09a3154..4314e3e 100644 --- a/exampleProject/CMakeLists.txt +++ b/exampleProject/CMakeLists.txt @@ -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) diff --git a/exampleProject/cmake/Modules/GitHash.cmake b/exampleProject/cmake/Modules/GitHash.cmake deleted file mode 120000 index 542639c..0000000 --- a/exampleProject/cmake/Modules/GitHash.cmake +++ /dev/null @@ -1 +0,0 @@ -../../../GitHash.cmake \ No newline at end of file diff --git a/exampleProject/deps/GitHash b/exampleProject/deps/GitHash new file mode 120000 index 0000000..2f7747b --- /dev/null +++ b/exampleProject/deps/GitHash @@ -0,0 +1 @@ +../../../GitHash \ No newline at end of file diff --git a/exampleProject/include/GitHash.hpp b/exampleProject/include/GitHash.hpp deleted file mode 120000 index ac61125..0000000 --- a/exampleProject/include/GitHash.hpp +++ /dev/null @@ -1 +0,0 @@ -../../GitHash.hpp \ No newline at end of file diff --git a/exampleProject/src/CMakeLists.txt b/exampleProject/src/CMakeLists.txt index 6fe763a..2d9f018 100644 --- a/exampleProject/src/CMakeLists.txt +++ b/exampleProject/src/CMakeLists.txt @@ -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 $) diff --git a/GitHash.hpp b/include/GitHash.hpp similarity index 100% rename from GitHash.hpp rename to include/GitHash.hpp