From 9478da10c608506869f8007f9bf8a356525e6bb4 Mon Sep 17 00:00:00 2001 From: "Watal M. Iwasaki" Date: Tue, 28 May 2024 13:53:11 +0900 Subject: [PATCH] :art: Replace git submodules with CMake FetchContent --- .github/workflows/build.yaml | 14 +++++-------- .gitmodules | 6 ------ CMakeLists.txt | 40 +++++++++++++++++++++++++++++++++--- README.md | 1 - clipp | 1 - json | 1 - 6 files changed, 42 insertions(+), 21 deletions(-) delete mode 160000 clipp delete mode 160000 json diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index cd246ff..f50ee07 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,12 +1,10 @@ -name: build - on: push: - branches: - - master + branches: [main, master] pull_request: - branches: - - master + branches: [main, master] + +name: build jobs: build: @@ -17,13 +15,11 @@ jobs: os: [macOS-latest, ubuntu-latest] steps: - uses: actions/checkout@v4 - with: - submodules: true - name: cmake run: cmake -S . -B build -DCMAKE_INSTALL_PREFIX=${HOME}/local - name: build run: | - cmake --build build -j2 + cmake --build build -j 2 cmake --install build - name: test run: cd build; ctest -V -j 2 diff --git a/.gitmodules b/.gitmodules index ff26c10..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +0,0 @@ -[submodule "clipp"] - path = clipp - url = https://github.com/heavywatal/clipp.git -[submodule "json"] - path = json - url = https://github.com/heavywatal/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 74da747..cf0284e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,15 +3,49 @@ project(clippson VERSION 0.8.6 LANGUAGES CXX) +cmake_policy(SET CMP0076 NEW) set(CMAKE_VERBOSE_MAKEFILE ON) include(CMakePrintHelpers) +include(FetchContent) include(GNUInstallDirs) cmake_print_variables(CMAKE_INSTALL_PREFIX) +function(import_env variable) + if(DEFINED ENV{${variable}}) + set(${variable} $ENV{${variable}} PARENT_SCOPE) + endif() +endfunction() + +function(git_fetch package version repo) + string(TOUPPER "${package}" upackage) + import_env("FETCHCONTENT_SOURCE_DIR_${upackage}") + if(IS_DIRECTORY "${FETCHCONTENT_SOURCE_DIR_${upackage}}") + set(tag "HEAD") + else() + set(tag "${version}") + endif() + cmake_print_variables(FETCHCONTENT_SOURCE_DIR_${upackage} tag) + set(FETCHCONTENT_QUIET OFF) + FetchContent_Declare( + ${package} + GIT_REPOSITORY https://github.com/${repo}.git + GIT_TAG ${tag} + GIT_SHALLOW ON + ) + if(NOT ${package}_POPULATED) + FetchContent_Populate(${package}) + endif() + set(${package}_SOURCE_DIR ${${package}_SOURCE_DIR} PARENT_SCOPE) + cmake_print_variables(${package}_SOURCE_DIR) +endfunction() + +git_fetch(clipp v1.2.3.1 heavywatal/clipp) +git_fetch(json v3.11.3 heavywatal/json) + set(subrepo_files - clipp/include/clipp.h - json/src/json.hpp - json/src/json_fwd.hpp + ${clipp_SOURCE_DIR}/include/clipp.h + ${json_SOURCE_DIR}/src/json.hpp + ${json_SOURCE_DIR}/src/json_fwd.hpp ) foreach(file IN LISTS subrepo_files) configure_file(${file} ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}/ COPYONLY) diff --git a/README.md b/README.md index c2b6137..bd46b86 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,6 @@ Alternatively, you can get the source code from GitHub manually: ```sh git clone https://github.com/heavywatal/clippson.git cd clippson/ -git submodule update --init --recursive DESTINATION=${HOME}/local cmake -S . -B build -DCMAKE_INSTALL_PREFIX=$DESTINATION cmake --build build -j2 diff --git a/clipp b/clipp deleted file mode 160000 index bc8045f..0000000 --- a/clipp +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bc8045f9daf0031f169da3485b331a7cdb3ea473 diff --git a/json b/json deleted file mode 160000 index 3a65cd7..0000000 --- a/json +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3a65cd7687a9f99ae0a2e1c74e35e6d72fee8f45