Skip to content

Commit f379ef2

Browse files
committed
Enhancement/submodule
* bugfix * renamed to connect with warthog for user * update readme with submodule or subtree command * test new module system * bugs in warthog.cmake * update readme * removed debug messages * added default FetchContent_Declare * updated example version number * module tag bugfix * fixed module name * moved details down
1 parent b7ae595 commit f379ef2

File tree

4 files changed

+171
-49
lines changed

4 files changed

+171
-49
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project(Warthog
44
VERSION 0.5.0
55
LANGUAGES CXX C)
66

7+
set_property(GLOBAL PROPERTY WARTHOG_warthog-core ON)
8+
79
set(CMAKE_CXX_STANDARD 20)
810
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
911

@@ -12,7 +14,7 @@ option(WARTHOG_BMI "Enable support cpu BMI for WARTHOG_INTRIN_HAS(BMI)" OFF)
1214
option(WARTHOG_BMI2 "Enable support cpu BMI2 for WARTHOG_INTRIN_HAS(BMI2), use for Zen 3+" OFF)
1315
option(WARTHOG_INTRIN_ALL "Enable march=native and support x86 intrinsics if able (based on system), supersedes all manual instruction sets" OFF)
1416

15-
include(cmake/submodule.cmake)
17+
include(cmake/warthog.cmake)
1618

1719
add_library(warthog_compile INTERFACE)
1820
add_library(warthog::compile ALIAS warthog_compile)

README.md

Lines changed: 92 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,104 @@ Requires `warthog-core` as a dependency.
2222

2323
# Using warthog
2424

25-
Warthog dependencies are setup using git-submodules, use commands below are used to initialise and update respectively:
26-
git submodule init
27-
git submodule update
25+
It is recommended to use warthog not as a fork, but included in an external repo.
26+
This setup support FetchContent, git submodule and git subtree.
2827

29-
All dependencies should be placed in `/extern`.
30-
If not project forked, we suggest setting the project up as below:
28+
## CMake
29+
30+
Setup a basic project using the following the commands:
31+
32+
git init
33+
git remote add warthog-core https://github.com/ShortestPathLab/warthog-core.git
34+
git fetch warthog-core
35+
git checkout warthog-core/main cmake/warthog.cmake
36+
37+
Example `CMakeLists.txt`:
3138

32-
1. Copy `/cmake/submodule.cmake` to your repo
33-
2. Submodule or subtree all your warthog dependencies and their dependencies into `/extern`
34-
3. In `/CMakeLists.txt`, setup as below:
3539
```
36-
include(cmake/submodule.cmake)
37-
warthog_submodule(warthog-[module]) # repeat for each [module]
40+
cmake_minimum_required(VERSION 3.13)
41+
42+
project(App
43+
VERSION 0.0.1
44+
LANGUAGES CXX C)
45+
46+
set(CMAKE_CXX_STANDARD 20)
47+
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
48+
49+
# warthog modules
50+
include(cmake/warthog.cmake)
51+
52+
warthog_module_declare(warthog-core v0.5.0) # is optional, remove for default of main
53+
warthog_module(warthog-core)
54+
55+
add_executable(app main.cpp)
56+
target_link_libraries(app PUBLIC warthog::core)
3857
```
3958

40-
With (2) we flatten dependencies to top-level project.
41-
With (3) `warthog_submodule` will add `add_subdirectoy` if your CMake is the top level config.
59+
## Submodule
60+
61+
Commands for adding a module as a submodules for each repo are found below:
62+
63+
git submodule add https://github.com/ShortestPathLab/warthog-core.git extern/warthog-core
64+
git submodule add https://github.com/ShortestPathLab/warthog-jps.git extern/warthog-jps
65+
66+
To update the version of warthog, for warthog module `$module`:
67+
68+
cd extern/$module
69+
git fetch
70+
git checkout|git switch
71+
cd ..
72+
git add $module
73+
74+
This will update the submodule to the checkout commit.
75+
Initialise or update the submodule on other clones with
76+
the following commands:
77+
78+
git submodule init # after clone
79+
git submodule update # after pull
4280

43-
An informal project not designed to be a dependency does not require this, and can just `add_subdirectory` to all dependencies.
81+
## Subtree
82+
83+
Subtree will make the module a part of your repo, allowing
84+
for local editing without forking.
85+
86+
The setup for each module:
87+
88+
git subtree -P extern/warthog-core add https://github.com/ShortestPathLab/warthog-core.git main|branch|commit --squash
89+
git subtree -P extern/warthog-jps add https://github.com/ShortestPathLab/warthog-jps.git main|branch|commit --squash
90+
91+
The update commands:
92+
93+
git subtree -P extern/warthog-core pull https://github.com/ShortestPathLab/warthog-core.git main|branch|commit --squash
94+
git subtree -P extern/warthog-jps pull https://github.com/ShortestPathLab/warthog-jps.git main|branch|commit --squash
95+
96+
## Advance Module Details
97+
98+
File `/cmake/warthog.cmake` from warthog core should be copied to user repo and `include` in CMake.
99+
Calling `warthog_submodule(warthog-core)` will then add `warthog-core` to your CMake in the following order:
100+
1. `add_subdirectory(/extern/warthog-core)` if `/extern/warthog-core/CMakeLists.txt` exists (submodule/subtree)
101+
2. `FetchContent_Declare` then `FetchContent_MakeAvailable(warthog-core)` otherwise
102+
3. Error if cannot find `warthog-core` content
103+
104+
The `warthog_module` call only adds a module once, the following calls will be ignored.
105+
The submodule/subtree version only works if called in the top level project by default;
106+
if this method is preferred, then it should be added to the top level `/extern/`, can be overridden
107+
with code `warthog_module(warthog-core ON)`.
108+
109+
Declare of warthog-core can be done using the following code:
110+
```
111+
warthog_module_declare(warthog-core [main|branch|tag|commit])
112+
```
113+
or:
114+
```
115+
FetchContent_Declare(warthog-core
116+
GIT_REPOSITORY https://github.com/ShortestPathLab/warthog-core.git
117+
GIT_TAG [main|branch|tag|commit])
118+
```
119+
This will declare what warthog-core version to fetched.
120+
The `warthog_module_declare` version makes it simple, although it only supports known warthog libraries.
121+
The optional second parameter sets the version to pull, by default is `main` branch.
122+
This system only support warthog 0.5 or greater.
44123

45124
# Resources
46125

cmake/submodule.cmake

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

cmake/warthog.cmake

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
# include this file to check submodule include
4+
5+
# set here as only
6+
set(warthog_https_warthog-core "https://github.com/ShortestPathLab/warthog-core.git" CACHE INTERNAL "")
7+
set(warthog_https_warthog-jps "https://github.com/ShortestPathLab/warthog-jps.git" CACHE INTERNAL "")
8+
9+
if(COMMAND warthog_module)
10+
return() # do not redefine
11+
endif()
12+
13+
include(FetchContent)
14+
15+
set(WARTHOG_MODULE_ROOT_ONLY ON CACHE BOOL "add submodule if present only allowed for root project")
16+
17+
function(warthog_top_level)
18+
set(_is_top OFF)
19+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.21)
20+
if(${PROJECT_IS_TOP_LEVEL})
21+
set(_is_top ON)
22+
endif()
23+
else()
24+
if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
25+
set(_is_top ON)
26+
endif()
27+
endif()
28+
set(_warthog_is_top ${_is_top} PARENT_SCOPE)
29+
endfunction()
30+
31+
# warthog_module module [override_top]
32+
# adds a module to warthog
33+
# has checks to insure module is only added once
34+
# if called from the top level project (or optional variable override_top is true)
35+
# then if extern/${module}/CMakeLists.txt exists, adds that version instead
36+
# intended for use with git module/subtree.
37+
# otherwise, fetchcontent ${module} is made available
38+
# user must add FetchContent_Declare
39+
function(warthog_module module)
40+
get_property(_module_added GLOBAL PROPERTY WARTHOG_${module} SET)
41+
if(${_module_added}) # module already added
42+
return()
43+
endif()
44+
if((${ARGC} GREATER 1) AND (${ARGV1}))
45+
set(is_top_level ON)
46+
else()
47+
warthog_top_level()
48+
set(is_top_level ${_warthog_is_top})
49+
endif()
50+
if(${is_top_level})
51+
if(EXISTS "${PROJECT_SOURCE_DIR}/extern/${module}/CMakeLists.txt")
52+
# module or subtree, include and exit
53+
add_subdirectory("${PROJECT_SOURCE_DIR}/extern/${module}")
54+
set_property(GLOBAL PROPERTY WARTHOG_${module} ON)
55+
return()
56+
endif()
57+
endif()
58+
# failed to add module, fetch if able
59+
warthog_module_declare(${module})
60+
FetchContent_MakeAvailable(${module})
61+
set_property(GLOBAL PROPERTY WARTHOG_${module} ON)
62+
endfunction()
63+
64+
function(warthog_module_declare module)
65+
if(NOT DEFINED warthog_https_${module})
66+
return()
67+
endif()
68+
if((${ARGC} GREATER 1))
69+
set(git_tag ${ARGV1})
70+
else()
71+
set(git_tag "main")
72+
endif()
73+
FetchContent_Declare(${module}
74+
GIT_REPOSITORY ${warthog_https_${module}}
75+
GIT_TAG ${git_tag})
76+
endfunction()

0 commit comments

Comments
 (0)