diff --git a/CMakeLists.txt b/CMakeLists.txt index 8680923..a564afe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ project(Warthog VERSION 0.5.0 LANGUAGES CXX C) +set_property(GLOBAL PROPERTY WARTHOG_warthog-core ON) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) @@ -12,7 +14,7 @@ option(WARTHOG_BMI "Enable support cpu BMI for WARTHOG_INTRIN_HAS(BMI)" OFF) option(WARTHOG_BMI2 "Enable support cpu BMI2 for WARTHOG_INTRIN_HAS(BMI2), use for Zen 3+" OFF) option(WARTHOG_INTRIN_ALL "Enable march=native and support x86 intrinsics if able (based on system), supersedes all manual instruction sets" OFF) -include(cmake/submodule.cmake) +include(cmake/warthog.cmake) add_library(warthog_compile INTERFACE) add_library(warthog::compile ALIAS warthog_compile) diff --git a/README.md b/README.md index 453f0b5..0179bcc 100644 --- a/README.md +++ b/README.md @@ -22,25 +22,104 @@ Requires `warthog-core` as a dependency. # Using warthog -Warthog dependencies are setup using git-submodules, use commands below are used to initialise and update respectively: - git submodule init - git submodule update +It is recommended to use warthog not as a fork, but included in an external repo. +This setup support FetchContent, git submodule and git subtree. -All dependencies should be placed in `/extern`. -If not project forked, we suggest setting the project up as below: +## CMake + +Setup a basic project using the following the commands: + + git init + git remote add warthog-core https://github.com/ShortestPathLab/warthog-core.git + git fetch warthog-core + git checkout warthog-core/main cmake/warthog.cmake + +Example `CMakeLists.txt`: -1. Copy `/cmake/submodule.cmake` to your repo -2. Submodule or subtree all your warthog dependencies and their dependencies into `/extern` -3. In `/CMakeLists.txt`, setup as below: ``` -include(cmake/submodule.cmake) -warthog_submodule(warthog-[module]) # repeat for each [module] +cmake_minimum_required(VERSION 3.13) + +project(App + VERSION 0.0.1 + LANGUAGES CXX C) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) + +# warthog modules +include(cmake/warthog.cmake) + +warthog_module_declare(warthog-core v0.5.0) # is optional, remove for default of main +warthog_module(warthog-core) + +add_executable(app main.cpp) +target_link_libraries(app PUBLIC warthog::core) ``` -With (2) we flatten dependencies to top-level project. -With (3) `warthog_submodule` will add `add_subdirectoy` if your CMake is the top level config. +## Submodule + +Commands for adding a module as a submodules for each repo are found below: + + git submodule add https://github.com/ShortestPathLab/warthog-core.git extern/warthog-core + git submodule add https://github.com/ShortestPathLab/warthog-jps.git extern/warthog-jps + +To update the version of warthog, for warthog module `$module`: + + cd extern/$module + git fetch + git checkout|git switch + cd .. + git add $module + +This will update the submodule to the checkout commit. +Initialise or update the submodule on other clones with +the following commands: + + git submodule init # after clone + git submodule update # after pull -An informal project not designed to be a dependency does not require this, and can just `add_subdirectory` to all dependencies. +## Subtree + +Subtree will make the module a part of your repo, allowing +for local editing without forking. + +The setup for each module: + + git subtree -P extern/warthog-core add https://github.com/ShortestPathLab/warthog-core.git main|branch|commit --squash + git subtree -P extern/warthog-jps add https://github.com/ShortestPathLab/warthog-jps.git main|branch|commit --squash + +The update commands: + + git subtree -P extern/warthog-core pull https://github.com/ShortestPathLab/warthog-core.git main|branch|commit --squash + git subtree -P extern/warthog-jps pull https://github.com/ShortestPathLab/warthog-jps.git main|branch|commit --squash + +## Advance Module Details + +File `/cmake/warthog.cmake` from warthog core should be copied to user repo and `include` in CMake. +Calling `warthog_submodule(warthog-core)` will then add `warthog-core` to your CMake in the following order: +1. `add_subdirectory(/extern/warthog-core)` if `/extern/warthog-core/CMakeLists.txt` exists (submodule/subtree) +2. `FetchContent_Declare` then `FetchContent_MakeAvailable(warthog-core)` otherwise +3. Error if cannot find `warthog-core` content + +The `warthog_module` call only adds a module once, the following calls will be ignored. +The submodule/subtree version only works if called in the top level project by default; +if this method is preferred, then it should be added to the top level `/extern/`, can be overridden +with code `warthog_module(warthog-core ON)`. + +Declare of warthog-core can be done using the following code: +``` +warthog_module_declare(warthog-core [main|branch|tag|commit]) +``` +or: +``` +FetchContent_Declare(warthog-core + GIT_REPOSITORY https://github.com/ShortestPathLab/warthog-core.git + GIT_TAG [main|branch|tag|commit]) +``` +This will declare what warthog-core version to fetched. +The `warthog_module_declare` version makes it simple, although it only supports known warthog libraries. +The optional second parameter sets the version to pull, by default is `main` branch. +This system only support warthog 0.5 or greater. # Resources diff --git a/cmake/submodule.cmake b/cmake/submodule.cmake deleted file mode 100644 index da47f29..0000000 --- a/cmake/submodule.cmake +++ /dev/null @@ -1,35 +0,0 @@ -cmake_minimum_required(VERSION 3.13) - -# include this file to check submodule include - -set(WARTHOG_SUBMODULE_ROOT_ONLY ON CACHE BOOL "add submodule if present only allowed for root project") - -function(warthog_top_level) -set(_is_top OFF) -if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.21) - if(${PROJECT_IS_TOP_LEVEL}) - set(_is_top ON) - endif() -else() - if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) - set(_is_top ON) - endif() -endif() -set(_is_top ${_is_top} PARENT_SCOPE) -endfunction() - -function(warthog_submodule dirname) -set(_is_top OFF) -if(NOT ${WARTHOG_SUBMODULE_ROOT_ONLY}) - set(_is_top ON) -else() - warthog_top_level() -endif() -if(${_is_top}) - if(NOT EXISTS "${PROJECT_SOURCE_DIR}/extern/${dirname}/.git") - message(SEND_ERROR "Failed to find submodule ${dirname}") - else() - add_subdirectory("${PROJECT_SOURCE_DIR}/extern/${dirname}" EXCLUDE_FROM_ALL) - endif() -endif() -endfunction() diff --git a/cmake/warthog.cmake b/cmake/warthog.cmake new file mode 100644 index 0000000..635f050 --- /dev/null +++ b/cmake/warthog.cmake @@ -0,0 +1,76 @@ +cmake_minimum_required(VERSION 3.13) + +# include this file to check submodule include + +# set here as only +set(warthog_https_warthog-core "https://github.com/ShortestPathLab/warthog-core.git" CACHE INTERNAL "") +set(warthog_https_warthog-jps "https://github.com/ShortestPathLab/warthog-jps.git" CACHE INTERNAL "") + +if(COMMAND warthog_module) + return() # do not redefine +endif() + +include(FetchContent) + +set(WARTHOG_MODULE_ROOT_ONLY ON CACHE BOOL "add submodule if present only allowed for root project") + +function(warthog_top_level) +set(_is_top OFF) +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.21) + if(${PROJECT_IS_TOP_LEVEL}) + set(_is_top ON) + endif() +else() + if(${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR}) + set(_is_top ON) + endif() +endif() +set(_warthog_is_top ${_is_top} PARENT_SCOPE) +endfunction() + +# warthog_module module [override_top] +# adds a module to warthog +# has checks to insure module is only added once +# if called from the top level project (or optional variable override_top is true) +# then if extern/${module}/CMakeLists.txt exists, adds that version instead +# intended for use with git module/subtree. +# otherwise, fetchcontent ${module} is made available +# user must add FetchContent_Declare +function(warthog_module module) +get_property(_module_added GLOBAL PROPERTY WARTHOG_${module} SET) +if(${_module_added}) # module already added + return() +endif() +if((${ARGC} GREATER 1) AND (${ARGV1})) + set(is_top_level ON) +else() + warthog_top_level() + set(is_top_level ${_warthog_is_top}) +endif() +if(${is_top_level}) + if(EXISTS "${PROJECT_SOURCE_DIR}/extern/${module}/CMakeLists.txt") + # module or subtree, include and exit + add_subdirectory("${PROJECT_SOURCE_DIR}/extern/${module}") + set_property(GLOBAL PROPERTY WARTHOG_${module} ON) + return() + endif() +endif() +# failed to add module, fetch if able +warthog_module_declare(${module}) +FetchContent_MakeAvailable(${module}) +set_property(GLOBAL PROPERTY WARTHOG_${module} ON) +endfunction() + +function(warthog_module_declare module) +if(NOT DEFINED warthog_https_${module}) + return() +endif() +if((${ARGC} GREATER 1)) + set(git_tag ${ARGV1}) +else() + set(git_tag "main") +endif() +FetchContent_Declare(${module} + GIT_REPOSITORY ${warthog_https_${module}} + GIT_TAG ${git_tag}) +endfunction()